diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-10-09 20:55:47 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-10-20 21:36:52 +0200 |
commit | bde443801fe9dfda6c84ead939be7a1fa1ff72f3 (patch) | |
tree | 9d908662950e724a6d9a006bad6c83d2025cd863 /tests/auto/gui/text/qfont | |
parent | fa8943640c02adbe100cdbca2b44a102b45de53f (diff) |
QFont: add a tag type for features and other advanced properties
Instead of overloads and generic string literals with runtime checks,
use a dedicated Tag type for specifying the font feature. The Tag type
can only be instantiated with a string literal of exactly 4 characters
(plus the terminating null)l; longer or shorter literals result in a
compile time error.
Constructing a Tag from any other string type is possible through the
named fromString constructor, in which case we can only check the length
and warn at runtime.
The type's API is almost completely constexpr so that we can use it to
calculate e.g. enum values.
Task-number: QTBUG-117046
Change-Id: I31038c7c6fd2b843a105b032f021e506b0b60822
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
Diffstat (limited to 'tests/auto/gui/text/qfont')
-rw-r--r-- | tests/auto/gui/text/qfont/tst_qfont.cpp | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/tests/auto/gui/text/qfont/tst_qfont.cpp b/tests/auto/gui/text/qfont/tst_qfont.cpp index 2438756bf0..bbcbf11d09 100644 --- a/tests/auto/gui/text/qfont/tst_qfont.cpp +++ b/tests/auto/gui/text/qfont/tst_qfont.cpp @@ -22,6 +22,8 @@ #include <qlist.h> #include <QtTest/private/qemulationdetector_p.h> +using namespace Qt::StringLiterals; + class tst_QFont : public QObject { Q_OBJECT @@ -846,34 +848,55 @@ void tst_QFont::setFamiliesAndFamily() void tst_QFont::featureAccessors() { + const QFont::Tag abcdTag("abcd"); + QCOMPARE(abcdTag.toString(), "abcd"); + QVERIFY(abcdTag.isValid()); + QFont font; QVERIFY(font.featureTags().isEmpty()); font.setFeature("abcd", 0xc0ffee); - quint32 abcdTag = QFont::stringToTag("abcd"); - quint32 bcdeTag = QFont::stringToTag("bcde"); QVERIFY(font.isFeatureSet(abcdTag)); - QVERIFY(!font.isFeatureSet(bcdeTag)); + QVERIFY(!font.isFeatureSet("bcde")); QCOMPARE(font.featureTags().size(), 1); QCOMPARE(font.featureTags().first(), abcdTag); - QCOMPARE(QFont::tagToString(font.featureTags().first()), QByteArray("abcd")); + QCOMPARE(font.featureTags().first(), "abcd"); QCOMPARE(font.featureValue(abcdTag), 0xc0ffeeU); - QCOMPARE(font.featureValue(bcdeTag), 0U); + QCOMPARE(font.featureValue("bcde"), 0U); font.setFeature(abcdTag, 0xf00d); QCOMPARE(font.featureTags().size(), 1); QCOMPARE(font.featureValue(abcdTag), 0xf00dU); - font.setFeature("abcde", 0xcaca0); - QVERIFY(!font.isFeatureSet(QFont::stringToTag("abcde"))); + + QFont::Tag invalidTag; + QVERIFY(!invalidTag.isValid()); + font.setFeature(invalidTag, 0xcaca0); + QVERIFY(!font.isFeatureSet(invalidTag)); QCOMPARE(font.featureTags().size(), 1); QFont font2 = font; font.unsetFeature("abcd"); - QVERIFY(!font.isFeatureSet(QFont::stringToTag("abcd"))); + QVERIFY(!font.isFeatureSet("abcd")); QVERIFY(font.featureTags().isEmpty()); - QVERIFY(font2.isFeatureSet(QFont::stringToTag("abcd"))); + QVERIFY(font2.isFeatureSet("abcd")); font2.clearFeatures(); QVERIFY(font.featureTags().isEmpty()); + + // various constructor compile tests + QFont::Tag tag; + tag = QFont::Tag("1234"); + QVERIFY(QFont::Tag::fromString(QByteArray("abcd"))); + QVERIFY(QFont::Tag::fromString(u"frac"_s)); + + // named constructors with invalid input + QTest::ignoreMessage(QtWarningMsg, "The tag name must be exactly 4 characters long!"); + QVERIFY(!QFont::Tag::fromString(u"fraction"_s)); + QVERIFY(!QFont::Tag::fromValue(0)); + QVERIFY(QFont::Tag::fromValue(abcdTag.value())); + + enum Features { + Frac = QFont::Tag("frac").value() + }; } QTEST_MAIN(tst_QFont) |