diff options
author | Andy Shaw <andy.shaw@qt.io> | 2020-10-12 13:08:06 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2020-10-13 07:27:04 +0000 |
commit | bc294899e588a743d96a0110f7575902c482a806 (patch) | |
tree | 2bf876e09d72a5add0ae5c1ad157a4be684595d0 | |
parent | b89d68f8610fa74c90905ffde4e1479ac78ddf45 (diff) |
Fallback to using the family when doing an exact match
If the difference between the families sizes is just 1 where one of them
is 0 in size then we can fallback to the family in that case.
Fixes: QTBUG-87267
Change-Id: I62b25b06c88000b4d7defe91871c07873b1fc792
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
(cherry picked from commit bdc9d272eebff66827b566b1b24b6697c797807a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/gui/text/qfont.cpp | 29 | ||||
-rw-r--r-- | tests/auto/gui/text/qfont/tst_qfont.cpp | 15 |
2 files changed, 38 insertions, 6 deletions
diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 2bea0e9a07..94020dc665 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -116,19 +116,36 @@ bool QFontDef::exactMatch(const QFontDef &other) const if (stretch != 0 && other.stretch != 0 && stretch != other.stretch) return false; - if (families.size() != other.families.size()) + // If either families or other.families just has 1 entry and the other has 0 then + // we will fall back to using the family in that case + const int sizeDiff = qAbs(families.size() - other.families.size()); + if (sizeDiff > 1) + return false; + if (sizeDiff == 1 && (families.size() > 1 || other.families.size() > 1)) return false; + QStringList origFamilies = families; + QStringList otherFamilies = other.families; + if (sizeDiff != 0) { + if (origFamilies.size() != 1) + origFamilies << family; + else + otherFamilies << other.family; + } + QString this_family, this_foundry, other_family, other_foundry; - for (int i = 0; i < families.size(); ++i) { - QFontDatabase::parseFontName(families.at(i), this_foundry, this_family); - QFontDatabase::parseFontName(other.families.at(i), other_foundry, other_family); + for (int i = 0; i < origFamilies.size(); ++i) { + QFontDatabase::parseFontName(origFamilies.at(i), this_foundry, this_family); + QFontDatabase::parseFontName(otherFamilies.at(i), other_foundry, other_family); if (this_family != other_family || this_foundry != other_foundry) return false; } - QFontDatabase::parseFontName(family, this_foundry, this_family); - QFontDatabase::parseFontName(other.family, other_foundry, other_family); + // Check family only if families is not set + if (origFamilies.size() == 0) { + QFontDatabase::parseFontName(family, this_foundry, this_family); + QFontDatabase::parseFontName(other.family, other_foundry, other_family); + } return (styleHint == other.styleHint && styleStrategy == other.styleStrategy diff --git a/tests/auto/gui/text/qfont/tst_qfont.cpp b/tests/auto/gui/text/qfont/tst_qfont.cpp index 2d72eef459..9b70308092 100644 --- a/tests/auto/gui/text/qfont/tst_qfont.cpp +++ b/tests/auto/gui/text/qfont/tst_qfont.cpp @@ -134,6 +134,21 @@ void tst_QFont::exactMatch() QVERIFY(!QFont("serif").exactMatch()); QVERIFY(!QFont("monospace").exactMatch()); + // Confirm that exactMatch is true for a valid font + QFontDatabase db; + const QString family = db.families().first(); + const QString style = db.styles(family).first(); + const int pointSize = db.pointSizes(family, style).first(); + font = db.font(family, style, pointSize); + QVERIFY(font.exactMatch()); + + if (db.families().contains("Arial")) { + font = QFont("Arial"); + QVERIFY(font.exactMatch()); + font = QFont(QString()); + font.setFamilies({"Arial"}); + QVERIFY(font.exactMatch()); + } } void tst_QFont::italicOblique() |