diff options
author | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> | 2019-04-02 15:33:16 +0200 |
---|---|---|
committer | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> | 2019-05-23 09:49:10 +0200 |
commit | 922d195020d54d7e599d135f6a5e0338100e08f1 (patch) | |
tree | 5ca0bfce1b904ce40d06dc38c2e3c3b7e3301b3f /src/platformsupport | |
parent | cceeb1ee7e18aac2a21ec72ef669bd6d4490ce2e (diff) |
Fix font matching of typographic families on Windows
9204b8c31ea1b5f0c05870c5b5d74c33b1a4f622 broke font matching on Windows.
This was then attempted fixed by bcd2fa484a4fe93e77743195d7f72cce9e580d43,
but this caused an infinite recursion for some cases, so it was reverted
again by 9d1905da9c59e9062a157199c81c076efc20eb28.
The original issue was that if we populate a specific face of a family,
such as "Arial Black", then the typographic/preferred name will be
detected as "Arial" and this family will be set as populated=true, even
though we have not yet registered any additional subfamilies. In this case,
we need to call populateFamily() for the typographic family name to
ensure we get Windows to enumerate all the subfamilies in that family
before it sets it as populated=true.
But this broke for some fonts where the font naming was unconventional.
In particular, "Yu Gothic" would have its Japanese name as the
typographic name, and there would be no font in the system where
the old-style font family name matched the typographic name. In
that case we would go into a loop where we would try populating
"<Japanese font name>", Windows would translate this to "Yu Gothic", we would
translate it back to "<Japanese font name>", ad infinitum.
In order to avoid the infinite recursion, we add a recursion guard
as well, ensuring that we never call populateFamily() for the main
family we are currently populating.
[ChangeLog][Windows][Fonts] Fixed a bug where it would be impossible
to request different faces of a font family after a specific type face
has been in use.
Task-number: QTBUG-74748
Task-number: QTBUG-74983
Change-Id: Ibe6239f67c45d67ebf75947c8f231cfa177e347f
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/platformsupport')
-rw-r--r-- | src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp | 43 | ||||
-rw-r--r-- | src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h | 2 |
2 files changed, 37 insertions, 8 deletions
diff --git a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp index e1167823cf..9d25422ca5 100644 --- a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp +++ b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase.cpp @@ -1007,12 +1007,27 @@ static QChar *createFontFile(const QString &faceName) return faceNamePtr; } +namespace { + struct StoreFontPayload { + StoreFontPayload(const QString &family, + QWindowsFontDatabase *fontDatabase) + : populatedFontFamily(family) + , windowsFontDatabase(fontDatabase) + {} + + QString populatedFontFamily; + QSet<QPair<QString,QString> > foundFontAndStyles; + QWindowsFontDatabase *windowsFontDatabase; + }; +} + static bool addFontToDatabase(QString familyName, QString styleName, const LOGFONT &logFont, const TEXTMETRIC *textmetric, const FONTSIGNATURE *signature, - int type) + int type, + StoreFontPayload *sfp) { // the "@family" fonts are just the same as "family". Ignore them. if (familyName.isEmpty() || familyName.at(0) == QLatin1Char('@') || familyName.startsWith(QLatin1String("WST_"))) @@ -1092,6 +1107,16 @@ static bool addFontToDatabase(QString familyName, writingSystems.setSupported(ws); } + // We came here from populating a different font family, so we have + // to ensure the entire typographic family is populated before we + // mark it as such inside registerFont() + if (!subFamilyName.isEmpty() + && familyName != subFamilyName + && sfp->populatedFontFamily != familyName + && !QPlatformFontDatabase::isFamilyPopulated(familyName)) { + sfp->windowsFontDatabase->populateFamily(familyName); + } + QPlatformFontDatabase::registerFont(familyName, styleName, foundryName, weight, style, stretch, antialias, scalable, size, fixed, writingSystems, createFontFile(faceName)); @@ -1128,17 +1153,18 @@ static int QT_WIN_CALLBACK storeFont(const LOGFONT *logFont, const TEXTMETRIC *t // to the documentation is identical to a TEXTMETRIC except for the last four // members, which we don't use anyway const FONTSIGNATURE *signature = nullptr; + StoreFontPayload *sfp = reinterpret_cast<StoreFontPayload *>(lparam); + Q_ASSERT(sfp != nullptr); if (type & TRUETYPE_FONTTYPE) { signature = &reinterpret_cast<const NEWTEXTMETRICEX *>(textmetric)->ntmFontSig; // We get a callback for each script-type supported, but we register them all // at once using the signature, so we only need one call to addFontToDatabase(). - QSet<QPair<QString,QString>> *foundFontAndStyles = reinterpret_cast<QSet<QPair<QString,QString>> *>(lparam); QPair<QString,QString> fontAndStyle(familyName, styleName); - if (foundFontAndStyles->contains(fontAndStyle)) + if (sfp->foundFontAndStyles.contains(fontAndStyle)) return 1; - foundFontAndStyles->insert(fontAndStyle); + sfp->foundFontAndStyles.insert(fontAndStyle); } - addFontToDatabase(familyName, styleName, *logFont, textmetric, signature, type); + addFontToDatabase(familyName, styleName, *logFont, textmetric, signature, type, sfp); // keep on enumerating return 1; @@ -1157,8 +1183,8 @@ void QWindowsFontDatabase::populateFamily(const QString &familyName) familyName.toWCharArray(lf.lfFaceName); lf.lfFaceName[familyName.size()] = 0; lf.lfPitchAndFamily = 0; - QSet<QPair<QString,QString>> foundFontAndStyles; - EnumFontFamiliesEx(dummy, &lf, storeFont, reinterpret_cast<intptr_t>(&foundFontAndStyles), 0); + StoreFontPayload sfp(familyName, this); + EnumFontFamiliesEx(dummy, &lf, storeFont, reinterpret_cast<intptr_t>(&sfp), 0); ReleaseDC(0, dummy); } @@ -1598,8 +1624,9 @@ QStringList QWindowsFontDatabase::addApplicationFont(const QByteArray &fontData, TEXTMETRIC textMetrics; GetTextMetrics(hdc, &textMetrics); + StoreFontPayload sfp(familyName, this); addFontToDatabase(familyName, styleName, lf, &textMetrics, &signatures.at(j), - TRUETYPE_FONTTYPE); + TRUETYPE_FONTTYPE, &sfp); SelectObject(hdc, oldobj); DeleteObject(hfont); diff --git a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h index b85a2dceee..a1cab17a87 100644 --- a/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h +++ b/src/platformsupport/fontdatabases/windows/qwindowsfontdatabase_p.h @@ -96,6 +96,8 @@ public: QWindowsFontDatabase(); ~QWindowsFontDatabase() override; + void ensureFamilyPopulated(const QString &familyName); + void populateFontDatabase() override; void populateFamily(const QString &familyName) override; QFontEngine *fontEngine(const QFontDef &fontDef, void *handle) override; |