QFontIconEngine: if we can't find the named glyph, try ligatures

Use QTextLayout to layout and shape the text, which will respect
ligatures. Many icon fonts might not name their icons, but have a
replacement ligature for the actual icon name replacing the text with
the respective glyph.

So if for the name of the icon we get a single glyph run with a single
glyph in it, use that to render the icon.

Pick-to: 6.9
Change-Id: If0e5c528c3ac4cccdbb7df5fb7fd32ca232f2a66
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
This commit is contained in:
Volker Hilsheimer 2024-12-04 21:53:45 +01:00
parent 9d47233d2c
commit d10e9174fe

View File

@ -13,6 +13,7 @@
#include <QtGui/qpainter.h>
#include <QtGui/qpainterpath.h>
#include <QtGui/qpalette.h>
#include <QtGui/qtextlayout.h>
#include <QtGui/private/qfont_p.h>
#include <QtGui/private/qfontengine_p.h>
@ -167,6 +168,20 @@ glyph_t QFontIconEngine::glyph() const
QFontEngine *engine = QFontPrivate::get(m_iconFont)->engineForScript(QChar::Script_Common);
if (engine)
m_glyph = engine->findGlyph(QLatin1StringView(m_iconName.toLatin1()));
if (!m_glyph) {
// May not be a named glyph, but there might be a ligature for the
// icon name.
QTextLayout layout(m_iconName, m_iconFont);
layout.beginLayout();
layout.createLine();
layout.endLayout();
const auto glyphRuns = layout.glyphRuns();
if (glyphRuns.size() == 1) {
const auto glyphIndexes = glyphRuns.first().glyphIndexes();
if (glyphIndexes.size() == 1)
m_glyph = glyphIndexes.first();
}
}
}
return m_glyph;
}