diff options
author | Pierre Rossi <pierre.rossi@digia.com> | 2014-10-22 17:14:29 +0200 |
---|---|---|
committer | Konstantin Ritt <ritt.ks@gmail.com> | 2014-12-12 06:59:46 +0100 |
commit | 517da68893be9e6d97c7993922c573de9560604d (patch) | |
tree | 9da6027604c97023fd6cacc3114c2f2fd43dd26f /src | |
parent | 0478bc15bd3b4c616b4860e460600ba8877ee216 (diff) |
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 <ritt.ks@gmail.com>
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/text/qfont.cpp | 6 | ||||
-rw-r--r-- | src/gui/text/qfont.h | 17 | ||||
-rw-r--r-- | src/gui/text/qfontdatabase.cpp | 70 | ||||
-rw-r--r-- | src/gui/text/qfontengine_p.h | 4 | ||||
-rw-r--r-- | src/gui/text/qplatformfontdatabase.cpp | 28 | ||||
-rw-r--r-- | src/gui/text/qplatformfontdatabase.h | 1 | ||||
-rw-r--r-- | src/platformsupport/fontdatabases/basic/qbasicfontdatabase.cpp | 31 | ||||
-rw-r--r-- | src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp | 16 | ||||
-rw-r--r-- | src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm | 22 | ||||
-rw-r--r-- | src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm | 66 | ||||
-rw-r--r-- | src/platformsupport/fontdatabases/mac/qfontengine_coretext_p.h | 2 | ||||
-rw-r--r-- | src/plugins/platforms/windows/qwindowsfontdatabase.cpp | 20 | ||||
-rw-r--r-- | src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp | 16 | ||||
-rw-r--r-- | src/plugins/platforms/winrt/qwinrtfontdatabase.cpp | 26 | ||||
-rw-r--r-- | src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp | 24 | ||||
-rw-r--r-- | src/widgets/styles/qgtkstyle_p.cpp | 14 |
16 files changed, 161 insertions, 202 deletions
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<QFont::Weight>(57); -const QFont::Weight qt_extralightFontWeight = static_cast<QFont::Weight>(12); -const QFont::Weight qt_thinFontWeight = static_cast<QFont::Weight>(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 @@ -599,6 +599,34 @@ QSupportedWritingSystems QPlatformFontDatabase::writingSystemsFromTrueTypeBits(q } /*! + 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. \since 5.2 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); diff --git a/src/platformsupport/fontdatabases/basic/qbasicfontdatabase.cpp b/src/platformsupport/fontdatabases/basic/qbasicfontdatabase.cpp index 52505c570b..f831f3b908 100644 --- a/src/platformsupport/fontdatabases/basic/qbasicfontdatabase.cpp +++ b/src/platformsupport/fontdatabases/basic/qbasicfontdatabase.cpp @@ -281,41 +281,26 @@ QStringList QBasicFontDatabase::addTTFile(const QByteArray &fontData, const QByt if (supportedWritingSystems) *supportedWritingSystems = writingSystems; - if (os2->usWeightClass == 0) - ; - else if (os2->usWeightClass < 150) - weight = qt_thinFontWeight; - else if (os2->usWeightClass < 250) - weight = qt_extralightFontWeight; - else if (os2->usWeightClass < 350) - weight = QFont::Light; - else if (os2->usWeightClass < 450) - weight = QFont::Normal; - else if (os2->usWeightClass < 550) - weight = qt_mediumFontWeight; - else if (os2->usWeightClass < 650) - weight = QFont::DemiBold; - else if (os2->usWeightClass < 750) - weight = QFont::Bold; - else if (os2->usWeightClass < 1000) - weight = QFont::Black; - - if (os2->panose[2] >= 2) { + if (os2->usWeightClass) { + weight = QPlatformFontDatabase::weightFromInteger(os2->usWeightClass); + } else if (os2->panose[2]) { int w = os2->panose[2]; if (w <= 1) - weight = qt_thinFontWeight; + weight = QFont::Thin; else if (w <= 2) - weight = qt_extralightFontWeight; + weight = QFont::ExtraLight; else if (w <= 3) weight = QFont::Light; else if (w <= 5) weight = QFont::Normal; else if (w <= 6) - weight = qt_mediumFontWeight; + weight = QFont::Medium; else if (w <= 7) weight = QFont::DemiBold; else if (w <= 8) weight = QFont::Bold; + else if (w <= 9) + weight = QFont::ExtraBold; else if (w <= 10) weight = QFont::Black; } diff --git a/src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp b/src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp index 686563893f..f129bb40dd 100644 --- a/src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp +++ b/src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp @@ -76,18 +76,24 @@ static inline int weightFromFcWeight(int fcweight) // FC_WEIGHT_DEMIBOLD and QFont::DemiBold) we map one to the other but other values map // to intermediate Qt weights. - if (fcweight < 0) - return 0; + if (fcweight <= FC_WEIGHT_THIN) + return QFont::Thin; + if (fcweight <= FC_WEIGHT_ULTRALIGHT) + return mapToQtWeightForRange(fcweight, FC_WEIGHT_THIN, FC_WEIGHT_ULTRALIGHT, QFont::Thin, QFont::ExtraLight); if (fcweight <= FC_WEIGHT_LIGHT) - return mapToQtWeightForRange(fcweight, 0, FC_WEIGHT_LIGHT, 0, QFont::Light); + return mapToQtWeightForRange(fcweight, FC_WEIGHT_ULTRALIGHT, FC_WEIGHT_LIGHT, QFont::ExtraLight, QFont::Light); if (fcweight <= FC_WEIGHT_NORMAL) return mapToQtWeightForRange(fcweight, FC_WEIGHT_LIGHT, FC_WEIGHT_NORMAL, QFont::Light, QFont::Normal); + if (fcweight <= FC_WEIGHT_MEDIUM) + return mapToQtWeightForRange(fcweight, FC_WEIGHT_NORMAL, FC_WEIGHT_MEDIUM, QFont::Normal, QFont::Medium); if (fcweight <= FC_WEIGHT_DEMIBOLD) - return mapToQtWeightForRange(fcweight, FC_WEIGHT_NORMAL, FC_WEIGHT_DEMIBOLD, QFont::Normal, QFont::DemiBold); + return mapToQtWeightForRange(fcweight, FC_WEIGHT_MEDIUM, FC_WEIGHT_DEMIBOLD, QFont::Medium, QFont::DemiBold); if (fcweight <= FC_WEIGHT_BOLD) return mapToQtWeightForRange(fcweight, FC_WEIGHT_DEMIBOLD, FC_WEIGHT_BOLD, QFont::DemiBold, QFont::Bold); + if (fcweight <= FC_WEIGHT_ULTRABOLD) + return mapToQtWeightForRange(fcweight, FC_WEIGHT_BOLD, FC_WEIGHT_ULTRABOLD, QFont::Bold, QFont::ExtraBold); if (fcweight <= FC_WEIGHT_BLACK) - return mapToQtWeightForRange(fcweight, FC_WEIGHT_BOLD, FC_WEIGHT_BLACK, QFont::Bold, QFont::Black); + return mapToQtWeightForRange(fcweight, FC_WEIGHT_ULTRABOLD, FC_WEIGHT_BLACK, QFont::ExtraBold, QFont::Black); if (fcweight <= FC_WEIGHT_ULTRABLACK) return mapToQtWeightForRange(fcweight, FC_WEIGHT_BLACK, FC_WEIGHT_ULTRABLACK, QFont::Black, maxWeight); return maxWeight; diff --git a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm index b5df38734d..ac73cd04ad 100644 --- a/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm +++ b/src/platformsupport/fontdatabases/mac/qcoretextfontdatabase.mm @@ -286,25 +286,9 @@ static void getFontDescription(CTFontDescriptorRef font, FontDescription *fd) if (styles) { if (CFNumberRef weightValue = (CFNumberRef) CFDictionaryGetValue(styles, kCTFontWeightTrait)) { - double normalizedWeight; - if (CFNumberGetValue(weightValue, kCFNumberDoubleType, &normalizedWeight)) { - if (normalizedWeight >= 0.62) - fd->weight = QFont::Black; - else if (normalizedWeight >= 0.4) - fd->weight = QFont::Bold; - else if (normalizedWeight >= 0.3) - fd->weight = QFont::DemiBold; - else if (normalizedWeight >= 0.2) - fd->weight = qt_mediumFontWeight; - else if (normalizedWeight == 0.0) - fd->weight = QFont::Normal; - else if (normalizedWeight <= -0.4) - fd->weight = QFont::Light; - else if (normalizedWeight <= -0.6) - fd->weight = qt_extralightFontWeight; - else if (normalizedWeight <= -0.8) - fd->weight = qt_thinFontWeight; - } + float normalizedWeight; + if (CFNumberGetValue(weightValue, kCFNumberFloatType, &normalizedWeight)) + fd->weight = QCoreTextFontEngine::qtWeightFromCFWeight(normalizedWeight); } if (CFNumberRef italic = (CFNumberRef) CFDictionaryGetValue(styles, kCTFontSlantTrait)) { double d; diff --git a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm index 44f6be21c7..425c7471d4 100644 --- a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm +++ b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm @@ -41,6 +41,7 @@ #include "qfontengine_coretext_p.h" +#include <qpa/qplatformfontdatabase.h> #include <QtCore/qendian.h> #include <QtCore/qsettings.h> @@ -66,6 +67,29 @@ bool QCoreTextFontEngine::ct_getSfntTable(void *user_data, uint tag, uchar *buff return true; } +QFont::Weight QCoreTextFontEngine::qtWeightFromCFWeight(float value) +{ + if (value >= 0.62) + return QFont::Black; + if (value >= 0.5) + return QFont::ExtraBold; + if (value >= 0.4) + return QFont::Bold; + if (value >= 0.3) + return QFont::DemiBold; + if (value >= 0.2) + return QFont::Medium; + if (value == 0.0) + return QFont::Normal; + if (value <= -0.4) + return QFont::Light; + if (value <= -0.6) + return QFont::ExtraLight; + if (value <= -0.8) + return QFont::Thin; + return QFont::Normal; +} + static void loadAdvancesForGlyphs(CTFontRef ctfont, QVarLengthArray<CGGlyph> &cgGlyphs, QGlyphLayout *glyphs, int len, @@ -88,6 +112,16 @@ static void loadAdvancesForGlyphs(CTFontRef ctfont, } } +static float getTraitValue(CFDictionaryRef allTraits, CFStringRef trait) +{ + if (CFDictionaryContainsKey(allTraits, trait)) { + CFNumberRef traitNum = (CFNumberRef) CFDictionaryGetValue(allTraits, trait); + float v = 0; + CFNumberGetValue(traitNum, kCFNumberFloatType, &v); + return v; + } + return 0; +} int QCoreTextFontEngine::antialiasingThreshold = 0; QFontEngine::GlyphFormat QCoreTextFontEngine::defaultGlyphFormat = QFontEngine::Format_A32; @@ -129,34 +163,6 @@ QCoreTextFontEngine::~QCoreTextFontEngine() CFRelease(ctfont); } -static 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; -} - -int getTraitValue(CFDictionaryRef allTraits, CFStringRef trait) -{ - if (CFDictionaryContainsKey(allTraits, trait)) { - CFNumberRef traitNum = (CFNumberRef) CFDictionaryGetValue(allTraits, trait); - float v = 0; - CFNumberGetValue(traitNum, kCFNumberFloatType, &v); - // the value we get from CFNumberRef is from -1.0 to 1.0 - int value = v * 500 + 500; - return value; - } - - return 0; -} - void QCoreTextFontEngine::init() { Q_ASSERT(ctfont != NULL); @@ -182,8 +188,8 @@ void QCoreTextFontEngine::init() fontDef.style = QFont::StyleItalic; CFDictionaryRef allTraits = CTFontCopyTraits(ctfont); - fontDef.weight = weightFromInteger(getTraitValue(allTraits, kCTFontWeightTrait)); - int slant = getTraitValue(allTraits, kCTFontSlantTrait); + fontDef.weight = QCoreTextFontEngine::qtWeightFromCFWeight(getTraitValue(allTraits, kCTFontWeightTrait)); + int slant = static_cast<int>(getTraitValue(allTraits, kCTFontSlantTrait) * 500 + 500); if (slant > 500 && !(traits & kCTFontItalicTrait)) fontDef.style = QFont::StyleOblique; CFRelease(allTraits); diff --git a/src/platformsupport/fontdatabases/mac/qfontengine_coretext_p.h b/src/platformsupport/fontdatabases/mac/qfontengine_coretext_p.h index 998a6abb18..0f4eb7187b 100644 --- a/src/platformsupport/fontdatabases/mac/qfontengine_coretext_p.h +++ b/src/platformsupport/fontdatabases/mac/qfontengine_coretext_p.h @@ -122,10 +122,10 @@ public: } static bool ct_getSfntTable(void *user_data, uint tag, uchar *buffer, uint *length); + static QFont::Weight qtWeightFromCFWeight(float value); static int antialiasingThreshold; static QFontEngine::GlyphFormat defaultGlyphFormat; - private: friend class QRawFontPrivate; diff --git a/src/plugins/platforms/windows/qwindowsfontdatabase.cpp b/src/plugins/platforms/windows/qwindowsfontdatabase.cpp index 214273e685..c9851a1c65 100644 --- a/src/plugins/platforms/windows/qwindowsfontdatabase.cpp +++ b/src/plugins/platforms/windows/qwindowsfontdatabase.cpp @@ -618,20 +618,6 @@ QDebug operator<<(QDebug d, const QFontDef &def) return d; } -// convert 0 ~ 1000 integer to QFont::Weight -static inline QFont::Weight weightFromInteger(long weight) -{ - if (weight < 400) - return QFont::Light; - if (weight < 600) - return QFont::Normal; - if (weight < 700) - return QFont::DemiBold; - if (weight < 800) - return QFont::Bold; - return QFont::Black; -} - static inline QFontDatabase::WritingSystem writingSystemFromCharSet(uchar charSet) { switch (charSet) { @@ -865,7 +851,7 @@ static bool addFontToDatabase(const QString &familyName, uchar charSet, const int size = scalable ? SMOOTH_SCALABLE : tm->tmHeight; const QFont::Style style = tm->tmItalic ? QFont::StyleItalic : QFont::StyleNormal; const bool antialias = false; - const QFont::Weight weight = weightFromInteger(tm->tmWeight); + const QFont::Weight weight = QPlatformFontDatabase::weightFromInteger(tm->tmWeight); const QFont::Stretch stretch = QFont::Unstretched; #ifndef QT_NO_DEBUG_OUTPUT @@ -1253,7 +1239,7 @@ QFontEngine *QWindowsFontDatabase::fontEngine(const QByteArray &fontData, qreal else fontEngine->fontDef.style = QFont::StyleNormal; - fontEngine->fontDef.weight = weightFromInteger(qFromBigEndian<quint16>(os2Table->weightClass)); + fontEngine->fontDef.weight = QPlatformFontDatabase::weightFromInteger(qFromBigEndian<quint16>(os2Table->weightClass)); } } @@ -1854,7 +1840,7 @@ QFont QWindowsFontDatabase::LOGFONT_to_QFont(const LOGFONT& logFont, int vertica QFont qFont(QString::fromWCharArray(logFont.lfFaceName)); qFont.setItalic(logFont.lfItalic); if (logFont.lfWeight != FW_DONTCARE) - qFont.setWeight(weightFromInteger(logFont.lfWeight)); + qFont.setWeight(QPlatformFontDatabase::weightFromInteger(logFont.lfWeight)); const qreal logFontHeight = qAbs(logFont.lfHeight); qFont.setPointSizeF(logFontHeight * 72.0 / qreal(verticalDPI_In)); qFont.setUnderline(logFont.lfUnderline); diff --git a/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp b/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp index 42e5a8c6ad..41e767dd1b 100644 --- a/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp +++ b/src/plugins/platforms/windows/qwindowsfontdatabase_ft.cpp @@ -52,20 +52,6 @@ QT_BEGIN_NAMESPACE -// convert 0 ~ 1000 integer to QFont::Weight -static inline QFont::Weight weightFromInteger(long weight) -{ - if (weight < 400) - return QFont::Light; - if (weight < 600) - return QFont::Normal; - if (weight < 700) - return QFont::DemiBold; - if (weight < 800) - return QFont::Bold; - return QFont::Black; -} - static inline QFontDatabase::WritingSystem writingSystemFromCharSet(uchar charSet) { switch (charSet) { @@ -139,7 +125,7 @@ static bool addFontToDatabase(const QString &familyName, uchar charSet, const int size = scalable ? SMOOTH_SCALABLE : tm->tmHeight; const QFont::Style style = tm->tmItalic ? QFont::StyleItalic : QFont::StyleNormal; const bool antialias = false; - const QFont::Weight weight = weightFromInteger(tm->tmWeight); + const QFont::Weight weight = QPlatformFontDatabase::weightFromInteger(tm->tmWeight); const QFont::Stretch stretch = QFont::Unstretched; #ifndef QT_NO_DEBUG_OUTPUT diff --git a/src/plugins/platforms/winrt/qwinrtfontdatabase.cpp b/src/plugins/platforms/winrt/qwinrtfontdatabase.cpp index 5d6b077abd..41bd2e108d 100644 --- a/src/plugins/platforms/winrt/qwinrtfontdatabase.cpp +++ b/src/plugins/platforms/winrt/qwinrtfontdatabase.cpp @@ -209,31 +209,7 @@ void QWinRTFontDatabase::populateFamily(const QString &familyName) } } - QFont::Weight weight; - switch (font->GetWeight()) { - case DWRITE_FONT_WEIGHT_THIN: - case DWRITE_FONT_WEIGHT_EXTRA_LIGHT: - case DWRITE_FONT_WEIGHT_LIGHT: - case DWRITE_FONT_WEIGHT_SEMI_LIGHT: - weight = QFont::Light; - break; - default: - case DWRITE_FONT_WEIGHT_NORMAL: - case DWRITE_FONT_WEIGHT_MEDIUM: - weight = QFont::Normal; - break; - case DWRITE_FONT_WEIGHT_DEMI_BOLD: - weight = QFont::DemiBold; - break; - case DWRITE_FONT_WEIGHT_BOLD: - case DWRITE_FONT_WEIGHT_EXTRA_BOLD: - weight = QFont::Bold; - break; - case DWRITE_FONT_WEIGHT_BLACK: - case DWRITE_FONT_WEIGHT_EXTRA_BLACK: - weight = QFont::Black; - break; - } + QFont::Weight weight = QPlatformFontDatabase::weightFromInteger(font->GetWeight()); QFont::Style style; switch (font->GetStyle()) { diff --git a/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp b/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp index d7e73c873d..0c7ff1a717 100644 --- a/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp +++ b/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp @@ -40,6 +40,7 @@ #include <qfont.h> #include <private/qguiapplication_p.h> +#include <qpa/qplatformfontdatabase.h> #undef signals #include <gtk/gtk.h> @@ -511,14 +512,22 @@ static QString qt_fontToString(const QFont &font) int weight = font.weight(); if (weight >= QFont::Black) pango_font_description_set_weight(desc, PANGO_WEIGHT_HEAVY); + else if (weight >= QFont::ExtraBold) + pango_font_description_set_weight(desc, PANGO_WEIGHT_ULTRABOLD); else if (weight >= QFont::Bold) pango_font_description_set_weight(desc, PANGO_WEIGHT_BOLD); else if (weight >= QFont::DemiBold) pango_font_description_set_weight(desc, PANGO_WEIGHT_SEMIBOLD); + else if (weight >= QFont::Medium) + pango_font_description_set_weight(desc, PANGO_WEIGHT_MEDIUM); else if (weight >= QFont::Normal) pango_font_description_set_weight(desc, PANGO_WEIGHT_NORMAL); - else + else if (weight >= QFont::Light) pango_font_description_set_weight(desc, PANGO_WEIGHT_LIGHT); + else if (weight >= QFont::ExtraLight) + pango_font_description_set_weight(desc, PANGO_WEIGHT_ULTRALIGHT); + else + pango_font_description_set_weight(desc, PANGO_WEIGHT_THIN); int style = font.style(); if (style == QFont::StyleItalic) @@ -545,17 +554,8 @@ static QFont qt_fontFromString(const QString &name) if (!family.isEmpty()) font.setFamily(family); - int weight = pango_font_description_get_weight(desc); - if (weight >= PANGO_WEIGHT_HEAVY) - font.setWeight(QFont::Black); - else if (weight >= PANGO_WEIGHT_BOLD) - font.setWeight(QFont::Bold); - else if (weight >= PANGO_WEIGHT_SEMIBOLD) - font.setWeight(QFont::DemiBold); - else if (weight >= PANGO_WEIGHT_NORMAL) - font.setWeight(QFont::Normal); - else - font.setWeight(QFont::Light); + const int weight = pango_font_description_get_weight(desc); + font.setWeight(QPlatformFontDatabase::weightFromInteger(weight)); PangoStyle style = pango_font_description_get_style(desc); if (style == PANGO_STYLE_ITALIC) diff --git a/src/widgets/styles/qgtkstyle_p.cpp b/src/widgets/styles/qgtkstyle_p.cpp index 61d1cd796e..beb52c01a7 100644 --- a/src/widgets/styles/qgtkstyle_p.cpp +++ b/src/widgets/styles/qgtkstyle_p.cpp @@ -57,6 +57,7 @@ #include <private/qgtk2painter_p.h> #include <private/qapplication_p.h> #include <private/qiconloader_p.h> +#include <qpa/qplatformfontdatabase.h> #include <QtWidgets/QMenu> #include <QtWidgets/QStyle> @@ -823,17 +824,8 @@ QFont QGtkStylePrivate::getThemeFont() if (!family.isEmpty()) font.setFamily(family); - int weight = pango_font_description_get_weight(gtk_font); - if (weight >= PANGO_WEIGHT_HEAVY) - font.setWeight(QFont::Black); - else if (weight >= PANGO_WEIGHT_BOLD) - font.setWeight(QFont::Bold); - else if (weight >= PANGO_WEIGHT_SEMIBOLD) - font.setWeight(QFont::DemiBold); - else if (weight >= PANGO_WEIGHT_NORMAL) - font.setWeight(QFont::Normal); - else - font.setWeight(QFont::Light); + const int weight = pango_font_description_get_weight(gtk_font); + font.setWeight(QPlatformFontDatabase::weightFromInteger(weight)); PangoStyle fontstyle = pango_font_description_get_style(gtk_font); if (fontstyle == PANGO_STYLE_ITALIC) |