From 17d435fd8b2ed3a8ac6f93d17d0e78cd61bd7851 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 2 Mar 2016 16:37:40 +0100 Subject: Add property for setting hinting preference on fonts As screen density is rapidly increasing and user interfaces are moving more and more towards using unhinted text as default, we have to make it possible to select the hint level from Qt Quick as well. While this is already partially covered by the unhinted QtRendering render type, it is still interesting to be able to tweak the native rendering accordingly, since this is a more precise rasterization. QtRendering also doesn't support medium hinting. [ChangeLog][Text] Added "hintingPreference" property to Text, TextEdit and TextInput. Change-Id: Ib99dbea24aba082481629deddde88c04cdeb8cdb Reviewed-by: Konstantin Ritt Reviewed-by: Simon Hausmann --- src/imports/qtquick2/plugins.qmltypes | 10 ++++++++ src/quick/items/qquicktext.cpp | 33 ++++++++++++++++++++++++++ src/quick/items/qquicktextedit.cpp | 33 ++++++++++++++++++++++++++ src/quick/items/qquicktextinput.cpp | 32 +++++++++++++++++++++++++ src/quick/util/qquickglobal.cpp | 5 ++++ src/quick/util/qquickvaluetypes.cpp | 10 ++++++++ src/quick/util/qquickvaluetypes_p.h | 12 ++++++++++ tests/auto/quick/qquicktext/tst_qquicktext.cpp | 29 ++++++++++++++++++++++ 8 files changed, 164 insertions(+) diff --git a/src/imports/qtquick2/plugins.qmltypes b/src/imports/qtquick2/plugins.qmltypes index b11906ee09..a1a3723600 100644 --- a/src/imports/qtquick2/plugins.qmltypes +++ b/src/imports/qtquick2/plugins.qmltypes @@ -1730,6 +1730,15 @@ Module { "Capitalize": 4 } } + Enum { + name: "HintingPreference" + values: { + "PreferDefaultHinting": 0, + "PreferNoHinting": 1, + "PreferVerticalHinting": 2, + "PreferFullHinting": 3 + } + } Property { name: "family"; type: "string" } Property { name: "styleName"; type: "string" } Property { name: "bold"; type: "bool" } @@ -1743,6 +1752,7 @@ Module { Property { name: "capitalization"; type: "Capitalization" } Property { name: "letterSpacing"; type: "double" } Property { name: "wordSpacing"; type: "double" } + Property { name: "hintingPreference"; type: "HintingPreference" } Method { name: "toString"; type: "string" } } Component { diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp index f3254cf8d7..65044d33d4 100644 --- a/src/quick/items/qquicktext.cpp +++ b/src/quick/items/qquicktext.cpp @@ -1423,6 +1423,39 @@ QQuickText::~QQuickText() Text { text: "Hello"; font.capitalization: Font.AllLowercase } \endqml */ + +/*! + \qmlproperty enumeration QtQuick::Text::font.hintingPreference + \since 5.8 + + Sets the preferred hinting on the text. This is a hint to the underlying text rendering system + to use a certain level of hinting, and has varying support across platforms. See the table in + the documentation for QFont::HintingPreference for more details. + + \note This property only has an effect when used together with render type Text.NativeRendering. + + \list + \value Font.PreferDefaultHinting - Use the default hinting level for the target platform. + \value Font.PreferNoHinting - If possible, render text without hinting the outlines + of the glyphs. The text layout will be typographically accurate, using the same metrics + as are used e.g. when printing. + \value Font.PreferVerticalHinting - If possible, render text with no horizontal hinting, + but align glyphs to the pixel grid in the vertical direction. The text will appear + crisper on displays where the density is too low to give an accurate rendering + of the glyphs. But since the horizontal metrics of the glyphs are unhinted, the text's + layout will be scalable to higher density devices (such as printers) without impacting + details such as line breaks. + \value Font.PreferFullHinting - If possible, render text with hinting in both horizontal and + vertical directions. The text will be altered to optimize legibility on the target + device, but since the metrics will depend on the target size of the text, the positions + of glyphs, line breaks, and other typographical detail will not scale, meaning that a + text layout may look different on devices with different pixel densities. + \endlist + + \qml + Text { text: "Hello"; renderType: Text.NativeRendering; font.hintingPreference: Font.PreferVerticalHinting } + \endqml +*/ QFont QQuickText::font() const { Q_D(const QQuickText); diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index a3ceb1344c..d2b719e8ef 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -322,6 +322,39 @@ QString QQuickTextEdit::text() const \endqml */ +/*! + \qmlproperty enumeration QtQuick::TextEdit::font.hintingPreference + \since 5.8 + + Sets the preferred hinting on the text. This is a hint to the underlying text rendering system + to use a certain level of hinting, and has varying support across platforms. See the table in + the documentation for QFont::HintingPreference for more details. + + \note This property only has an effect when used together with render type TextEdit.NativeRendering. + + \list + \value Font.PreferDefaultHinting - Use the default hinting level for the target platform. + \value Font.PreferNoHinting - If possible, render text without hinting the outlines + of the glyphs. The text layout will be typographically accurate, using the same metrics + as are used e.g. when printing. + \value Font.PreferVerticalHinting - If possible, render text with no horizontal hinting, + but align glyphs to the pixel grid in the vertical direction. The text will appear + crisper on displays where the density is too low to give an accurate rendering + of the glyphs. But since the horizontal metrics of the glyphs are unhinted, the text's + layout will be scalable to higher density devices (such as printers) without impacting + details such as line breaks. + \value Font.PreferFullHinting - If possible, render text with hinting in both horizontal and + vertical directions. The text will be altered to optimize legibility on the target + device, but since the metrics will depend on the target size of the text, the positions + of glyphs, line breaks, and other typographical detail will not scale, meaning that a + text layout may look different on devices with different pixel densities. + \endlist + + \qml + TextEdit { text: "Hello"; renderType: TextEdit.NativeRendering; font.hintingPreference: Font.PreferVerticalHinting } + \endqml +*/ + /*! \qmlproperty string QtQuick::TextEdit::text diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index 9664326af5..3b1901e075 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -345,6 +345,38 @@ QString QQuickTextInputPrivate::realText() const \endqml */ +/*! + \qmlproperty enumeration QtQuick::TextInput::font.hintingPreference + \since 5.8 + + Sets the preferred hinting on the text. This is a hint to the underlying text rendering system + to use a certain level of hinting, and has varying support across platforms. See the table in + the documentation for QFont::HintingPreference for more details. + + \note This property only has an effect when used together with render type TextInput.NativeRendering. + + \list + \value Font.PreferDefaultHinting - Use the default hinting level for the target platform. + \value Font.PreferNoHinting - If possible, render text without hinting the outlines + of the glyphs. The text layout will be typographically accurate, using the same metrics + as are used e.g. when printing. + \value Font.PreferVerticalHinting - If possible, render text with no horizontal hinting, + but align glyphs to the pixel grid in the vertical direction. The text will appear + crisper on displays where the density is too low to give an accurate rendering + of the glyphs. But since the horizontal metrics of the glyphs are unhinted, the text's + layout will be scalable to higher density devices (such as printers) without impacting + details such as line breaks. + \value Font.PreferFullHinting - If possible, render text with hinting in both horizontal and + vertical directions. The text will be altered to optimize legibility on the target + device, but since the metrics will depend on the target size of the text, the positions + of glyphs, line breaks, and other typographical detail will not scale, meaning that a + text layout may look different on devices with different pixel densities. + \endlist + + \qml + TextInput { text: "Hello"; renderType: TextInput.NativeRendering; font.hintingPreference: Font.PreferVerticalHinting } + \endqml +*/ QFont QQuickTextInput::font() const { Q_D(const QQuickTextInput); diff --git a/src/quick/util/qquickglobal.cpp b/src/quick/util/qquickglobal.cpp index ef54917434..4ad6fdd854 100644 --- a/src/quick/util/qquickglobal.cpp +++ b/src/quick/util/qquickglobal.cpp @@ -301,6 +301,7 @@ public: QV4::ScopedValue vundl(scope, obj->get((s = v4->newString(QStringLiteral("underline"))))); QV4::ScopedValue vweight(scope, obj->get((s = v4->newString(QStringLiteral("weight"))))); QV4::ScopedValue vwspac(scope, obj->get((s = v4->newString(QStringLiteral("wordSpacing"))))); + QV4::ScopedValue vhint(scope, obj->get((s = v4->newString(QStringLiteral("hintingPreference"))))); // pull out the values, set ok to true if at least one valid field is given. if (vbold->isBoolean()) { @@ -351,6 +352,10 @@ public: retn.setWordSpacing(vwspac->asDouble()); if (ok) *ok = true; } + if (vhint->isInt32()) { + retn.setHintingPreference(static_cast(vhint->integerValue())); + if (ok) *ok = true; + } return retn; } diff --git a/src/quick/util/qquickvaluetypes.cpp b/src/quick/util/qquickvaluetypes.cpp index 416a325238..7b9b6068bd 100644 --- a/src/quick/util/qquickvaluetypes.cpp +++ b/src/quick/util/qquickvaluetypes.cpp @@ -676,4 +676,14 @@ void QQuickFontValueType::setWordSpacing(qreal size) v.setWordSpacing(size); } +QQuickFontValueType::HintingPreference QQuickFontValueType::hintingPreference() const +{ + return QQuickFontValueType::HintingPreference(v.hintingPreference()); +} + +void QQuickFontValueType::setHintingPreference(QQuickFontValueType::HintingPreference hintingPreference) +{ + v.setHintingPreference(QFont::HintingPreference(hintingPreference)); +} + QT_END_NAMESPACE diff --git a/src/quick/util/qquickvaluetypes_p.h b/src/quick/util/qquickvaluetypes_p.h index 80b9ce3109..05e954f915 100644 --- a/src/quick/util/qquickvaluetypes_p.h +++ b/src/quick/util/qquickvaluetypes_p.h @@ -304,6 +304,7 @@ class QQuickFontValueType Q_PROPERTY(Capitalization capitalization READ capitalization WRITE setCapitalization FINAL) Q_PROPERTY(qreal letterSpacing READ letterSpacing WRITE setLetterSpacing FINAL) Q_PROPERTY(qreal wordSpacing READ wordSpacing WRITE setWordSpacing FINAL) + Q_PROPERTY(HintingPreference hintingPreference READ hintingPreference WRITE setHintingPreference FINAL) public: enum FontWeight { Thin = QFont::Thin, @@ -323,6 +324,14 @@ public: Capitalize = QFont::Capitalize }; Q_ENUM(Capitalization) + enum HintingPreference { + PreferDefaultHinting = QFont::PreferDefaultHinting, + PreferNoHinting = QFont::PreferNoHinting, + PreferVerticalHinting = QFont::PreferVerticalHinting, + PreferFullHinting = QFont::PreferFullHinting + }; + Q_ENUM(HintingPreference) + Q_INVOKABLE QString toString() const; QString family() const; @@ -363,6 +372,9 @@ public: qreal wordSpacing() const; void setWordSpacing(qreal spacing); + + HintingPreference hintingPreference() const; + void setHintingPreference(HintingPreference); }; QT_END_NAMESPACE diff --git a/tests/auto/quick/qquicktext/tst_qquicktext.cpp b/tests/auto/quick/qquicktext/tst_qquicktext.cpp index d163ee785e..1cd3d1f9e2 100644 --- a/tests/auto/quick/qquicktext/tst_qquicktext.cpp +++ b/tests/auto/quick/qquicktext/tst_qquicktext.cpp @@ -146,6 +146,8 @@ private slots: void padding(); + void hintingPreference(); + private: QStringList standard; QStringList richText; @@ -4152,6 +4154,33 @@ void tst_qquicktext::padding() delete root; } +void tst_qquicktext::hintingPreference() +{ + { + QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }"; + QQmlComponent textComponent(&engine); + textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); + QQuickText *textObject = qobject_cast(textComponent.create()); + + QVERIFY(textObject != 0); + QCOMPARE((int)textObject->font().hintingPreference(), (int)QFont::PreferDefaultHinting); + + delete textObject; + } + { + QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.hintingPreference: Font.PreferNoHinting }"; + QQmlComponent textComponent(&engine); + textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); + QQuickText *textObject = qobject_cast(textComponent.create()); + + QVERIFY(textObject != 0); + QCOMPARE((int)textObject->font().hintingPreference(), (int)QFont::PreferNoHinting); + + delete textObject; + } +} + + QTEST_MAIN(tst_qquicktext) #include "tst_qquicktext.moc" -- cgit v1.2.3