From 517da68893be9e6d97c7993922c573de9560604d Mon Sep 17 00:00:00 2001 From: Pierre Rossi Date: Wed, 22 Oct 2014 17:14:29 +0200 Subject: Add QFont::Weight enum values And try to make good use of them in order to match the QFont request more closely. Task-number: QTBUG-38482 Change-Id: I768dfa8828e370d77a1c17ecf4796d750b3edd9b Reviewed-by: Konstantin Ritt Reviewed-by: Gabriel de Dietrich --- src/gui/text/qfont.cpp | 6 ++- src/gui/text/qfont.h | 17 ++++++--- src/gui/text/qfontdatabase.cpp | 70 ++++++++++++++++++---------------- src/gui/text/qfontengine_p.h | 4 -- src/gui/text/qplatformfontdatabase.cpp | 28 ++++++++++++++ src/gui/text/qplatformfontdatabase.h | 1 + 6 files changed, 82 insertions(+), 44 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index b867dc6ef2..9059092538 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -1033,10 +1033,14 @@ int QFont::weight() const This enum contains the predefined font weights: + \value Thin 0 + \value ExtraLight 12 \value Light 25 \value Normal 50 + \value Medium 57 \value DemiBold 63 \value Bold 75 + \value ExtraBold 81 \value Black 87 */ @@ -1063,7 +1067,7 @@ void QFont::setWeight(int weight) \fn bool QFont::bold() const Returns \c true if weight() is a value greater than - \l{Weight}{QFont::Normal}; otherwise returns \c false. + \l{Weight}{QFont::Medium}; otherwise returns \c false. \sa weight(), setBold(), QFontInfo::bold() */ diff --git a/src/gui/text/qfont.h b/src/gui/text/qfont.h index 2a84fa680d..df2815ae73 100644 --- a/src/gui/text/qfont.h +++ b/src/gui/text/qfont.h @@ -86,12 +86,17 @@ public: PreferFullHinting = 3 }; + // Mapping OpenType weight value. enum Weight { - Light = 25, - Normal = 50, - DemiBold = 63, - Bold = 75, - Black = 87 + Thin = 0, // 100 + ExtraLight = 12, // 200 + Light = 25, // 300 + Normal = 50, // 400 + Medium = 57, // 500 + DemiBold = 63, // 600 + Bold = 75, // 700 + ExtraBold = 81, // 800 + Black = 87 // 900 }; enum Style { @@ -313,7 +318,7 @@ Q_DECLARE_SHARED(QFont) Q_GUI_EXPORT uint qHash(const QFont &font, uint seed = 0) Q_DECL_NOTHROW; inline bool QFont::bold() const -{ return weight() > Normal; } +{ return weight() > Medium; } inline void QFont::setBold(bool enable) diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 37d3756e52..dd11cd8382 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -90,22 +90,29 @@ static int getFontWeight(const QString &weightString) // order of "expense". // // A simple string test is the cheapest, so let's do that first. - if (s == QLatin1String("normal")) + // Test in decreasing order of commonness + if (s == QLatin1String("normal") || s == QLatin1String("regular")) return QFont::Normal; - if (s == QLatin1String("medium")) - return qt_mediumFontWeight; if (s == QLatin1String("bold")) return QFont::Bold; - if (s == QLatin1String("demibold") || s == QLatin1String("demi bold")) + if (s == QLatin1String("semibold") || s == QLatin1String("semi bold") + || s == QLatin1String("demibold") || s == QLatin1String("demi bold")) return QFont::DemiBold; + if (s == QLatin1String("medium")) + return QFont::Medium; if (s == QLatin1String("black")) return QFont::Black; if (s == QLatin1String("light")) return QFont::Light; if (s == QLatin1String("thin")) - return qt_thinFontWeight; - if (s == QLatin1String("extralight")) - return qt_extralightFontWeight; + return QFont::Thin; + const QStringRef s2 = s.midRef(2); + if (s.startsWith(QLatin1String("ex")) || s.startsWith(QLatin1String("ul"))) { + if (s2 == QLatin1String("tralight") || s == QLatin1String("tra light")) + return QFont::ExtraLight; + if (s2 == QLatin1String("trabold") || s2 == QLatin1String("tra bold")) + return QFont::ExtraBold; + } // Next up, let's see if contains() matches: slightly more expensive, but // still fast enough. @@ -123,49 +130,46 @@ static int getFontWeight(const QString &weightString) // These are (very) slow compared to simple string ops, so we do these last. // As using translated values for such things is not very common, this should // not be too bad. - QString translatedNormal = QCoreApplication::translate("QFontDatabase", "Normal").toLower(); - if (s == translatedNormal) + if (s.compare(QCoreApplication::translate("QFontDatabase", "Normal", "The Normal or Regular font weight"), Qt::CaseInsensitive) == 0) return QFont::Normal; - QString translatedBold = QCoreApplication::translate("QFontDatabase", "Bold").toLower(); + const QString translatedBold = QCoreApplication::translate("QFontDatabase", "Bold").toLower(); if (s == translatedBold) return QFont::Bold; - QString translatedDemiBold = QCoreApplication::translate("QFontDatabase", "Demi Bold").toLower(); - if (s == translatedDemiBold) + if (s.compare(QCoreApplication::translate("QFontDatabase", "Demi Bold"), Qt::CaseInsensitive) == 0) return QFont::DemiBold; - QString translatedBlack = QCoreApplication::translate("QFontDatabase", "Black").toLower(); - if (s == translatedBlack) + if (s.compare(QCoreApplication::translate("QFontDatabase", "Medium", "The Medium font weight"), Qt::CaseInsensitive) == 0) + return QFont::Medium; + if (s.compare(QCoreApplication::translate("QFontDatabase", "Black"), Qt::CaseInsensitive) == 0) return QFont::Black; + const QString translatedLight = QCoreApplication::translate("QFontDatabase", "Light").toLower(); + if (s == translatedLight) + return QFont::Light; + if (s.compare(QCoreApplication::translate("QFontDatabase", "Thin"), Qt::CaseInsensitive) == 0) + return QFont::Thin; + if (s.compare(QCoreApplication::translate("QFontDatabase", "Extra Light"), Qt::CaseInsensitive) == 0) + return QFont::ExtraLight; + if (s.compare(QCoreApplication::translate("QFontDatabase", "Extra Bold"), Qt::CaseInsensitive) == 0) + return QFont::ExtraBold; // And now the contains() checks for the translated strings. + const QString translatedExtra = QCoreApplication::translate("QFontDatabase", "Extra").toLower(); if (s.contains(translatedBold)) { QString translatedDemi = QCoreApplication::translate("QFontDatabase", "Demi").toLower(); - if (s == translatedDemi) + if (s .contains(translatedDemi)) return QFont::DemiBold; + if (s.contains(translatedExtra)) + return QFont::ExtraBold; return QFont::Bold; } - QString translatedLight = QCoreApplication::translate("QFontDatabase", "Light").toLower(); - if (s == translatedLight || s.contains(translatedLight)) + if (s.contains(translatedLight)) { + if (s.contains(translatedExtra)) + return QFont::ExtraLight; return QFont::Light; - + } return QFont::Normal; } -// convert 0 ~ 1000 integer to QFont::Weight -QFont::Weight weightFromInteger(int weight) -{ - if (weight < 400) - return QFont::Light; - else if (weight < 600) - return QFont::Normal; - else if (weight < 700) - return QFont::DemiBold; - else if (weight < 800) - return QFont::Bold; - else - return QFont::Black; -} - struct QtFontEncoding { signed int encoding : 16; diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h index 9b88a78020..cd42cc89d7 100644 --- a/src/gui/text/qfontengine_p.h +++ b/src/gui/text/qfontengine_p.h @@ -78,10 +78,6 @@ enum HB_Compat_Error { typedef void (*qt_destroy_func_t) (void *user_data); typedef bool (*qt_get_font_table_func_t) (void *user_data, uint tag, uchar *buffer, uint *length); -const QFont::Weight qt_mediumFontWeight = static_cast(57); -const QFont::Weight qt_extralightFontWeight = static_cast(12); -const QFont::Weight qt_thinFontWeight = static_cast(0); - class Q_GUI_EXPORT QFontEngine { public: diff --git a/src/gui/text/qplatformfontdatabase.cpp b/src/gui/text/qplatformfontdatabase.cpp index d10cc8fed0..8f47933e6d 100644 --- a/src/gui/text/qplatformfontdatabase.cpp +++ b/src/gui/text/qplatformfontdatabase.cpp @@ -598,6 +598,34 @@ QSupportedWritingSystems QPlatformFontDatabase::writingSystemsFromTrueTypeBits(q return writingSystems; } +/*! + Helper function that returns the Qt font weight matching a given opentype integer value. + + \since 5.5 +*/ + +// convert 0 ~ 1000 integer to QFont::Weight +QFont::Weight QPlatformFontDatabase::weightFromInteger(int weight) +{ + if (weight < 150) + return QFont::Thin; + if (weight < 250) + return QFont::ExtraLight; + if (weight < 350) + return QFont::Light; + if (weight < 450) + return QFont::Normal; + if (weight < 550) + return QFont::Medium; + if (weight < 650) + return QFont::DemiBold; + if (weight < 750) + return QFont::Bold; + if (weight < 850) + return QFont::ExtraBold; + return QFont::Black; +} + /*! Helper function that register the \a alias for the \a familyName. diff --git a/src/gui/text/qplatformfontdatabase.h b/src/gui/text/qplatformfontdatabase.h index a75d55f91a..2497ec2ff3 100644 --- a/src/gui/text/qplatformfontdatabase.h +++ b/src/gui/text/qplatformfontdatabase.h @@ -110,6 +110,7 @@ public: // helper static QSupportedWritingSystems writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2]); + static QFont::Weight weightFromInteger(int weight); //callback static void registerQPF2Font(const QByteArray &dataArray, void *handle); -- cgit v1.2.3