diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2022-08-19 11:59:10 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2022-08-19 14:04:27 +0200 |
commit | a8b49216d0a397e19cbc3095525f4bac69b7fe9e (patch) | |
tree | 5be89a6da3fca789de184c557dd77f29ecba3136 /src/gui | |
parent | 4842cc176881ae22e14ca193fba46c6a04d09530 (diff) |
freetype: Propagate font data from font database to engine if available
QFreeTypeFontDatabase allows registering fonts either by filename,
ending up with FT_New_Face, or via raw data, resulting in a
FT_New_Memory_Face. We then register the font with QFontDatabase
using a custom FontFile handle, where store the filename.
When then getting a callback to QFreeTypeFontDatabase::fontEngine
we would pull out the filename from the handle and call
QFontEngineFT::create with this filename.
This meant that if a font was registered with raw data, the font
engine creation would only work if there was a corresponding valid
file name/path. The only reason this works for addApplicationFont
is that QFreetypeFace::getFace() has a hard-coded workaround for
:qmemoryfonts where it uses qt_fontdata_from_index to get at the
original data.
Luckily for us QFontEngineFT::create already takes a QByteArray
data argument, which is plumbed to QFreetypeFace::getFace as well,
so all we need to do is fix the first part of the plumbing by
adding the original data to the FontFile handle and passing it
on in QFreeTypeFontDatabase::fontEngine.
This would potentially allow us to remove the hard-coded logic
for qmemoryfonts, but this has not been further investigated.
QPlatformFontDatabase also has another fontEngine overload that
takes a QByteArray directly, but this code path is only used by
QRawFont for now. A future improvement would be to unify some
of this logic.
Finally, it's unclear why the FT font database uses a custom
FontFile as a handle, instead of the original FT_Face that was
already created in addTTFile(). As a result of the current
approach we end up creating the face both when registering it,
and when creating a font engine for it. Investigating this is
left for later.
Change-Id: I7e01820a82d2664e8f34c3553bdcebe6a7afa078
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/text/freetype/qfreetypefontdatabase.cpp | 10 | ||||
-rw-r--r-- | src/gui/text/freetype/qfreetypefontdatabase_p.h | 5 | ||||
-rw-r--r-- | src/gui/text/qfontdatabase_p.h | 4 |
3 files changed, 15 insertions, 4 deletions
diff --git a/src/gui/text/freetype/qfreetypefontdatabase.cpp b/src/gui/text/freetype/qfreetypefontdatabase.cpp index fd02b80619..e941614b4f 100644 --- a/src/gui/text/freetype/qfreetypefontdatabase.cpp +++ b/src/gui/text/freetype/qfreetypefontdatabase.cpp @@ -55,7 +55,7 @@ QFontEngine *QFreeTypeFontDatabase::fontEngine(const QFontDef &fontDef, void *us faceId.filename = QFile::encodeName(fontfile->fileName); faceId.index = fontfile->indexValue; - return QFontEngineFT::create(fontDef, faceId); + return QFontEngineFT::create(fontDef, faceId, fontfile->data); } QFontEngine *QFreeTypeFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize, @@ -191,9 +191,11 @@ QStringList QFreeTypeFontDatabase::addTTFile(const QByteArray &fontData, const Q } QString family = QString::fromLatin1(face->family_name); - FontFile *fontFile = new FontFile; - fontFile->fileName = QFile::decodeName(file); - fontFile->indexValue = index; + FontFile *fontFile = new FontFile{ + QFile::decodeName(file), + index, + fontData + }; QString styleName = QString::fromLatin1(face->style_name); diff --git a/src/gui/text/freetype/qfreetypefontdatabase_p.h b/src/gui/text/freetype/qfreetypefontdatabase_p.h index f9ad0824b9..ffb10a2e5c 100644 --- a/src/gui/text/freetype/qfreetypefontdatabase_p.h +++ b/src/gui/text/freetype/qfreetypefontdatabase_p.h @@ -26,6 +26,11 @@ struct FontFile { QString fileName; int indexValue; + + // Note: The data may be implicitly shared throughout the + // font database and platform font database, so be careful + // to never detach when accessing this member! + const QByteArray data; }; class Q_GUI_EXPORT QFreeTypeFontDatabase : public QPlatformFontDatabase diff --git a/src/gui/text/qfontdatabase_p.h b/src/gui/text/qfontdatabase_p.h index b40a651bbb..5428e18071 100644 --- a/src/gui/text/qfontdatabase_p.h +++ b/src/gui/text/qfontdatabase_p.h @@ -207,6 +207,10 @@ public: QCache<QtFontFallbacksCacheKey, QStringList> fallbacksCache; struct ApplicationFont { QString fileName; + + // Note: The data may be implicitly shared throughout the + // font database and platform font database, so be careful + // to never detach when accessing this member! QByteArray data; bool isNull() const { return fileName.isEmpty(); } |