diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/text/freetype/qfreetypefontdatabase.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/qcssparser.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/qfont.cpp | 111 | ||||
-rw-r--r-- | src/gui/text/qfont.h | 23 | ||||
-rw-r--r-- | src/gui/text/qfont_p.h | 37 | ||||
-rw-r--r-- | src/gui/text/qfontdatabase.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/qfontdatabase_p.h | 2 | ||||
-rw-r--r-- | src/gui/text/qplatformfontdatabase.cpp | 29 | ||||
-rw-r--r-- | src/gui/text/qplatformfontdatabase.h | 1 | ||||
-rw-r--r-- | src/gui/text/qtextdocument.cpp | 4 | ||||
-rw-r--r-- | src/gui/text/qtextformat.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/qtextodfwriter.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/unix/qfontconfigdatabase.cpp | 9 | ||||
-rw-r--r-- | src/gui/text/windows/qwindowsdirectwritefontdatabase.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/windows/qwindowsfontdatabase.cpp | 6 | ||||
-rw-r--r-- | src/gui/text/windows/qwindowsfontdatabase_ft.cpp | 2 | ||||
-rw-r--r-- | src/gui/text/windows/qwindowsfontdatabasebase.cpp | 8 | ||||
-rw-r--r-- | src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp | 3 | ||||
-rw-r--r-- | src/tools/uic/cpp/cppwriteinitialization.cpp | 53 | ||||
-rw-r--r-- | src/tools/uic/ui4.cpp | 13 | ||||
-rw-r--r-- | src/tools/uic/ui4.h | 13 |
21 files changed, 219 insertions, 107 deletions
diff --git a/src/gui/text/freetype/qfreetypefontdatabase.cpp b/src/gui/text/freetype/qfreetypefontdatabase.cpp index c0b93389c6..ac7520c495 100644 --- a/src/gui/text/freetype/qfreetypefontdatabase.cpp +++ b/src/gui/text/freetype/qfreetypefontdatabase.cpp @@ -169,7 +169,7 @@ QStringList QFreeTypeFontDatabase::addTTFile(const QByteArray &fontData, const Q writingSystems = QPlatformFontDatabase::writingSystemsFromTrueTypeBits(unicodeRange, codePageRange); if (os2->usWeightClass) { - weight = QPlatformFontDatabase::weightFromInteger(os2->usWeightClass); + weight = static_cast<QFont::Weight>(os2->usWeightClass); } else if (os2->panose[2]) { int w = os2->panose[2]; if (w <= 1) diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index 86d3f77fee..f67c0f485a 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -1192,7 +1192,7 @@ static bool setFontWeightFromValue(const QCss::Value &value, QFont *font) } if (value.type != Value::Number) return false; - font->setWeight(qMin(value.variant.toInt() / 8, 99)); + font->setWeight(QFont::Weight(value.variant.toInt())); return true; } diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index a618179f98..195904a4b1 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -179,6 +179,47 @@ Q_GUI_EXPORT int qt_defaultDpi() return qt_defaultDpiY(); } +/* Helper function to convert between legacy Qt and OpenType font weights. */ +static int convertWeights(int weight, bool inverted) +{ + static const QVarLengthArray<QPair<int, int>, 9> legacyToOpenTypeMap = { + { 0, QFont::Thin }, { 12, QFont::ExtraLight }, { 25, QFont::Light }, + { 50, QFont::Normal }, { 57, QFont::Medium }, { 63, QFont::DemiBold }, + { 75, QFont::Bold }, { 81, QFont::ExtraBold }, { 87, QFont::Black }, + }; + + int closestDist = INT_MAX; + int result = -1; + + // Go through and find the closest mapped value + for (auto mapping : legacyToOpenTypeMap) { + const int weightOld = inverted ? mapping.second : mapping.first; + const int weightNew = inverted ? mapping.first : mapping.second; + const int dist = qAbs(weightOld - weight); + if (dist < closestDist) { + result = weightNew; + closestDist = dist; + } else { + // Break early since following values will be further away + break; + } + } + + return result; +} + +/* Converts from legacy Qt font weight (Qt < 6.0) to OpenType font weight (Qt >= 6.0) */ +Q_GUI_EXPORT int qt_legacyToOpenTypeWeight(int weight) +{ + return convertWeights(weight, false); +} + +/* Converts from OpenType font weight (Qt >= 6.0) to legacy Qt font weight (Qt < 6.0) */ +Q_GUI_EXPORT int qt_openTypeToLegacyWeight(int weight) +{ + return convertWeights(weight, true); +} + QFontPrivate::QFontPrivate() : engineData(nullptr), dpi(qt_defaultDpi()), underline(false), overline(false), strikeOut(false), kerning(true), @@ -1066,29 +1107,28 @@ void QFont::setStyle(Style style) \sa setWeight(), Weight, QFontInfo */ -int QFont::weight() const +QFont::Weight QFont::weight() const { - return d->request.weight; + return static_cast<Weight>(d->request.weight); } /*! \enum QFont::Weight - Qt uses a weighting scale from 0 to 99 similar to, but not the - same as, the scales used in Windows or CSS. A weight of 0 will be - thin, whilst 99 will be extremely black. + Qt uses a weighting scale from 1 to 1000 compatible with OpenType. A weight of 1 will be + thin, whilst 1000 will be extremely black. 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 + \value Thin 100 + \value ExtraLight 200 + \value Light 300 + \value Normal 400 + \value Medium 500 + \value DemiBold 600 + \value Bold 700 + \value ExtraBold 800 + \value Black 900 */ /*! @@ -1099,16 +1139,20 @@ int QFont::weight() const \sa weight(), QFontInfo */ -void QFont::setWeight(int weight) +void QFont::setWeight(QFont::Weight weight) { - Q_ASSERT_X(weight >= 0 && weight <= 99, "QFont::setWeight", "Weight must be between 0 and 99"); + const int weightValue = qBound(QFONT_WEIGHT_MIN, static_cast<int>(weight), QFONT_WEIGHT_MAX); + if (weightValue != static_cast<int>(weight)) { + qWarning() << "QFont::setWeight: Weight must be between 1 and 1000, attempted to set " + << static_cast<int>(weight); + } - if ((resolve_mask & QFont::WeightResolved) && d->request.weight == weight) + if ((resolve_mask & QFont::WeightResolved) && d->request.weight == weightValue) return; detach(); - d->request.weight = weight; + d->request.weight = weightValue; resolve_mask |= QFont::WeightResolved; } @@ -2040,7 +2084,7 @@ bool QFont::fromString(const QString &descrip) setPointSizeF(l[1].toDouble()); if (count == 9) { setStyleHint((StyleHint) l[2].toInt()); - setWeight(qMax(qMin(99, l[3].toInt()), 0)); + setWeight(QFont::Weight(l[3].toInt())); setItalic(l[4].toInt()); setUnderline(l[5].toInt()); setStrikeOut(l[6].toInt()); @@ -2049,7 +2093,7 @@ bool QFont::fromString(const QString &descrip) if (l[2].toInt() > 0) setPixelSize(l[2].toInt()); setStyleHint((StyleHint) l[3].toInt()); - setWeight(qMax(qMin(99, l[4].toInt()), 0)); + setWeight(QFont::Weight(l[4].toInt())); setStyle((QFont::Style)l[5].toInt()); setUnderline(l[6].toInt()); setStrikeOut(l[7].toInt()); @@ -2214,9 +2258,13 @@ QDataStream &operator<<(QDataStream &s, const QFont &font) else s << (quint8) font.d->request.styleStrategy; } - s << (quint8) 0 - << (quint8) font.d->request.weight - << get_font_bits(s.version(), font.d.data()); + + if (s.version() < QDataStream::Qt_6_0) + s << quint8(0) << quint8(qt_openTypeToLegacyWeight(font.d->request.weight)); + else + s << quint16(font.d->request.weight); + + s << get_font_bits(s.version(), font.d.data()); if (s.version() >= QDataStream::Qt_4_3) s << (quint16)font.d->request.stretch; if (s.version() >= QDataStream::Qt_4_4) @@ -2248,7 +2296,7 @@ QDataStream &operator>>(QDataStream &s, QFont &font) font.d = new QFontPrivate; font.resolve_mask = QFont::AllPropertiesResolved; - quint8 styleHint, charSet, weight, bits; + quint8 styleHint, bits; quint16 styleStrategy = QFont::PreferDefault; if (s.version() == 1) { @@ -2288,13 +2336,22 @@ QDataStream &operator>>(QDataStream &s, QFont &font) } } - s >> charSet; - s >> weight; + if (s.version() < QDataStream::Qt_6_0) { + quint8 charSet; + quint8 weight; + s >> charSet; + s >> weight; + font.d->request.weight = qt_legacyToOpenTypeWeight(weight); + } else { + quint16 weight; + s >> weight; + font.d->request.weight = weight; + } + s >> bits; font.d->request.styleHint = styleHint; font.d->request.styleStrategy = styleStrategy; - font.d->request.weight = weight; set_font_bits(s.version(), bits, font.d.data()); diff --git a/src/gui/text/qfont.h b/src/gui/text/qfont.h index b520421e89..65d126374e 100644 --- a/src/gui/text/qfont.h +++ b/src/gui/text/qfont.h @@ -94,17 +94,16 @@ public: }; Q_ENUM(HintingPreference) - // Mapping OpenType weight value. enum Weight { - 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 + Thin = 100, + ExtraLight = 200, + Light = 300, + Normal = 400, + Medium = 500, + DemiBold = 600, + Bold = 700, + ExtraBold = 800, + Black = 900 }; Q_ENUM(Weight) @@ -197,8 +196,8 @@ public: int pixelSize() const; void setPixelSize(int); - int weight() const; - void setWeight(int); + Weight weight() const; + void setWeight(Weight weight); inline bool bold() const; inline void setBold(bool); diff --git a/src/gui/text/qfont_p.h b/src/gui/text/qfont_p.h index d2d240b134..abf6d000e7 100644 --- a/src/gui/text/qfont_p.h +++ b/src/gui/text/qfont_p.h @@ -66,14 +66,24 @@ QT_BEGIN_NAMESPACE class QFontCache; class QFontEngine; +#define QFONT_WEIGHT_MIN 1 +#define QFONT_WEIGHT_MAX 1000 + struct QFontDef { inline QFontDef() - : pointSize(-1.0), pixelSize(-1), - styleStrategy(QFont::PreferDefault), styleHint(QFont::AnyStyle), - weight(50), fixedPitch(false), style(QFont::StyleNormal), stretch(QFont::AnyStretch), - hintingPreference(QFont::PreferDefaultHinting), ignorePitch(true), - fixedPitchComputed(0), reserved(0) + : pointSize(-1.0), + pixelSize(-1), + styleStrategy(QFont::PreferDefault), + stretch(QFont::AnyStretch), + style(QFont::StyleNormal), + hintingPreference(QFont::PreferDefaultHinting), + styleHint(QFont::AnyStyle), + weight(QFont::Normal), + fixedPitch(false), + ignorePitch(true), + fixedPitchComputed(0), + reserved(0) { } @@ -86,18 +96,18 @@ struct QFontDef qreal pointSize; qreal pixelSize; + // Note: Variable ordering matters to make sure no variable overlaps two 32-bit registers. uint styleStrategy : 16; - uint styleHint : 8; + uint stretch : 12; // 0-4000 + uint style : 2; + uint hintingPreference : 2; - uint weight : 7; // 0-99 + uint styleHint : 8; + uint weight : 10; // 1-1000 uint fixedPitch : 1; - uint style : 2; - uint stretch : 12; // 0-4000 - - uint hintingPreference : 2; uint ignorePitch : 1; uint fixedPitchComputed : 1; // for Mac OS X only - uint reserved : 14; // for future extensions + uint reserved : 11; // for future extensions bool exactMatch(const QFontDef &other) const; bool operator==(const QFontDef &other) const @@ -297,6 +307,9 @@ Q_GUI_EXPORT int qt_defaultDpiX(); Q_GUI_EXPORT int qt_defaultDpiY(); Q_GUI_EXPORT int qt_defaultDpi(); +Q_GUI_EXPORT int qt_legacyToOpenTypeWeight(int weight); +Q_GUI_EXPORT int qt_openTypeToLegacyWeight(int weight); + QT_END_NAMESPACE #endif // QFONT_P_H diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 550215ee64..1afaedaa09 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -888,7 +888,7 @@ static QtFontStyle *bestStyle(QtFontFoundry *foundry, const QtFontStyle::Key &st break; } - int d = qAbs( styleKey.weight - style->key.weight ); + int d = qAbs( (int(styleKey.weight) - int(style->key.weight)) / 10 ); if ( styleKey.stretch != 0 && style->key.stretch != 0 ) { d += qAbs( styleKey.stretch - style->key.stretch ); diff --git a/src/gui/text/qfontdatabase_p.h b/src/gui/text/qfontdatabase_p.h index f60671ccb3..6b7d9c2f47 100644 --- a/src/gui/text/qfontdatabase_p.h +++ b/src/gui/text/qfontdatabase_p.h @@ -113,7 +113,7 @@ struct Q_GUI_EXPORT QtFontStyle {} uint style : 2; - signed int weight : 8; + uint weight : 10; signed int stretch : 12; bool operator==(const Key &other) const noexcept diff --git a/src/gui/text/qplatformfontdatabase.cpp b/src/gui/text/qplatformfontdatabase.cpp index e101f8b3e5..d54bda4941 100644 --- a/src/gui/text/qplatformfontdatabase.cpp +++ b/src/gui/text/qplatformfontdatabase.cpp @@ -603,35 +603,6 @@ QSupportedWritingSystems QPlatformFontDatabase::writingSystemsFromTrueTypeBits(q } /*! - Helper function that returns the Qt font weight matching - a given opentype integer value. Converts the integer - \a weight (0 ~ 1000) to QFont::Weight and returns it. - - \since 5.5 -*/ - -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 045b3c964e..e464855bfd 100644 --- a/src/gui/text/qplatformfontdatabase.h +++ b/src/gui/text/qplatformfontdatabase.h @@ -131,7 +131,6 @@ public: // helper static QSupportedWritingSystems writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2]); static QSupportedWritingSystems writingSystemsFromOS2Table(const char *os2Table, size_t length); - static QFont::Weight weightFromInteger(int weight); //callback static void registerFont(const QString &familyname, const QString &stylename, diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index f066853792..a4ecbd157f 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -2308,7 +2308,7 @@ QString QTextHtmlExporter::toHtml(ExportMode mode) } html += QLatin1String(" font-weight:"); - html += QString::number(defaultCharFormat.fontWeight() * 8); + html += QString::number(defaultCharFormat.fontWeight()); html += QLatin1Char(';'); html += QLatin1String(" font-style:"); @@ -2421,7 +2421,7 @@ bool QTextHtmlExporter::emitCharFormatStyle(const QTextCharFormat &format) if (format.hasProperty(QTextFormat::FontWeight) && format.fontWeight() != defaultCharFormat.fontWeight()) { html += QLatin1String(" font-weight:"); - html += QString::number(format.fontWeight() * 8); + html += QString::number(format.fontWeight()); html += QLatin1Char(';'); attributesEmitted = true; } diff --git a/src/gui/text/qtextformat.cpp b/src/gui/text/qtextformat.cpp index faf1765219..043a5c9102 100644 --- a/src/gui/text/qtextformat.cpp +++ b/src/gui/text/qtextformat.cpp @@ -375,7 +375,7 @@ void QTextFormatPrivate::recalcFont() const const QVariant weightValue = props.at(i).value; int weight = weightValue.toInt(); if (weight >= 0 && weightValue.isValid()) - f.setWeight(weight); + f.setWeight(QFont::Weight(weight)); break; } case QTextFormat::FontItalic: f.setItalic(props.at(i).value.toBool()); diff --git a/src/gui/text/qtextodfwriter.cpp b/src/gui/text/qtextodfwriter.cpp index ef51c967b0..953c05c74c 100644 --- a/src/gui/text/qtextodfwriter.cpp +++ b/src/gui/text/qtextodfwriter.cpp @@ -681,7 +681,7 @@ void QTextOdfWriter::writeCharacterFormat(QXmlStreamWriter &writer, QTextCharFor if (format.fontWeight() == QFont::Bold) value = QString::fromLatin1("bold"); else - value = QString::number(format.fontWeight() * 10); + value = QString::number(format.fontWeight()); writer.writeAttribute(foNS, QString::fromLatin1("font-weight"), value); } if (format.hasProperty(QTextFormat::FontFamily)) diff --git a/src/gui/text/unix/qfontconfigdatabase.cpp b/src/gui/text/unix/qfontconfigdatabase.cpp index f54af2e59e..a8e28618f4 100644 --- a/src/gui/text/unix/qfontconfigdatabase.cpp +++ b/src/gui/text/unix/qfontconfigdatabase.cpp @@ -65,8 +65,6 @@ QT_BEGIN_NAMESPACE -static const int maxWeight = 99; - static inline int mapToQtWeightForRange(int fcweight, int fcLower, int fcUpper, int qtLower, int qtUpper) { return qtLower + ((fcweight - fcLower) * (qtUpper - qtLower)) / (fcUpper - fcLower); @@ -75,7 +73,7 @@ static inline int mapToQtWeightForRange(int fcweight, int fcLower, int fcUpper, static inline int weightFromFcWeight(int fcweight) { // Font Config uses weights from 0 to 215 (the highest enum value) while QFont ranges from - // 0 to 99. The spacing between the values for the enums are uneven so a linear mapping from + // 1 to 1000. The spacing between the values for the enums are uneven so a linear mapping from // Font Config values to Qt would give surprising results. So, we do a piecewise linear // mapping. This ensures that where there is a corresponding enum on both sides (for example // FC_WEIGHT_DEMIBOLD and QFont::DemiBold) we map one to the other but other values map @@ -100,8 +98,9 @@ static inline int weightFromFcWeight(int fcweight) if (fcweight <= FC_WEIGHT_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; + return mapToQtWeightForRange(fcweight, FC_WEIGHT_BLACK, FC_WEIGHT_ULTRABLACK, QFont::Black, + QFONT_WEIGHT_MAX); + return QFONT_WEIGHT_MAX; } static inline int stretchFromFcWidth(int fcwidth) diff --git a/src/gui/text/windows/qwindowsdirectwritefontdatabase.cpp b/src/gui/text/windows/qwindowsdirectwritefontdatabase.cpp index 6033e8aeb9..c0d9e820c6 100644 --- a/src/gui/text/windows/qwindowsdirectwritefontdatabase.cpp +++ b/src/gui/text/windows/qwindowsdirectwritefontdatabase.cpp @@ -100,7 +100,7 @@ static QFont::Stretch fromDirectWriteStretch(DWRITE_FONT_STRETCH stretch) static QFont::Weight fromDirectWriteWeight(DWRITE_FONT_WEIGHT weight) { - return QPlatformFontDatabase::weightFromInteger(int(weight)); + return static_cast<QFont::Weight>(weight); } static DWRITE_FONT_STYLE toDirectWriteStyle(QFont::Style style) diff --git a/src/gui/text/windows/qwindowsfontdatabase.cpp b/src/gui/text/windows/qwindowsfontdatabase.cpp index 003d61d805..f180e91e97 100644 --- a/src/gui/text/windows/qwindowsfontdatabase.cpp +++ b/src/gui/text/windows/qwindowsfontdatabase.cpp @@ -526,7 +526,7 @@ static bool addFontToDatabase(QString familyName, const int size = scalable ? SMOOTH_SCALABLE : textmetric->tmHeight; const QFont::Style style = textmetric->tmItalic ? QFont::StyleItalic : QFont::StyleNormal; const bool antialias = false; - const QFont::Weight weight = QPlatformFontDatabase::weightFromInteger(textmetric->tmWeight); + const QFont::Weight weight = static_cast<QFont::Weight>(textmetric->tmWeight); const QFont::Stretch stretch = QFont::Unstretched; #ifndef QT_NO_DEBUG_OUTPUT @@ -995,7 +995,7 @@ QStringList QWindowsFontDatabase::addApplicationFont(const QByteArray &fontData, if (applicationFont != nullptr) { QFontDatabasePrivate::ApplicationFont::Properties properties; properties.style = values.isItalic ? QFont::StyleItalic : QFont::StyleNormal; - properties.weight = QPlatformFontDatabase::weightFromInteger(values.weight); + properties.weight = static_cast<int>(values.weight); properties.familyName = familyName; properties.styleName = styleName; @@ -1042,7 +1042,7 @@ QStringList QWindowsFontDatabase::addApplicationFont(const QByteArray &fontData, QFontDatabasePrivate::ApplicationFont::Properties properties; properties.style = values.isItalic ? QFont::StyleItalic : QFont::StyleNormal; - properties.weight = QPlatformFontDatabase::weightFromInteger(values.weight); + properties.weight = static_cast<int>(values.weight); properties.familyName = familyName; properties.styleName = styleName; diff --git a/src/gui/text/windows/qwindowsfontdatabase_ft.cpp b/src/gui/text/windows/qwindowsfontdatabase_ft.cpp index a576302dda..91d3b5b74d 100644 --- a/src/gui/text/windows/qwindowsfontdatabase_ft.cpp +++ b/src/gui/text/windows/qwindowsfontdatabase_ft.cpp @@ -187,7 +187,7 @@ static bool addFontToDatabase(QString familyName, const int size = scalable ? SMOOTH_SCALABLE : textmetric->tmHeight; const QFont::Style style = textmetric->tmItalic ? QFont::StyleItalic : QFont::StyleNormal; const bool antialias = false; - const QFont::Weight weight = QPlatformFontDatabase::weightFromInteger(textmetric->tmWeight); + const QFont::Weight weight = static_cast<QFont::Weight>(textmetric->tmWeight); const QFont::Stretch stretch = QFont::Unstretched; #ifndef QT_NO_DEBUG_STREAM diff --git a/src/gui/text/windows/qwindowsfontdatabasebase.cpp b/src/gui/text/windows/qwindowsfontdatabasebase.cpp index 52149d3b8f..9a7c1c3578 100644 --- a/src/gui/text/windows/qwindowsfontdatabasebase.cpp +++ b/src/gui/text/windows/qwindowsfontdatabasebase.cpp @@ -226,7 +226,7 @@ void QWindowsFontDatabaseBase::EmbeddedFont::updateFromOS2Table(QFontEngine *fon else fontEngine->fontDef.style = QFont::StyleNormal; - fontEngine->fontDef.weight = QPlatformFontDatabase::weightFromInteger(qFromBigEndian<quint16>(os2Table->weightClass)); + fontEngine->fontDef.weight = qFromBigEndian<quint16>(os2Table->weightClass); } } @@ -642,10 +642,10 @@ LOGFONT QWindowsFontDatabaseBase::fontDefToLOGFONT(const QFontDef &request, cons lf.lfWidth = 0; lf.lfEscapement = 0; lf.lfOrientation = 0; - if (request.weight == 50) + if (request.weight == QFont::Normal) lf.lfWeight = FW_DONTCARE; else - lf.lfWeight = (request.weight*900)/99; + lf.lfWeight = request.weight; lf.lfItalic = request.style != QFont::StyleNormal; lf.lfCharSet = DEFAULT_CHARSET; @@ -735,7 +735,7 @@ QFont QWindowsFontDatabaseBase::LOGFONT_to_QFont(const LOGFONT& logFont, int ver QFont qFont(QString::fromWCharArray(logFont.lfFaceName)); qFont.setItalic(logFont.lfItalic); if (logFont.lfWeight != FW_DONTCARE) - qFont.setWeight(QPlatformFontDatabase::weightFromInteger(logFont.lfWeight)); + qFont.setWeight(QFont::Weight(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/platformthemes/gtk3/qgtk3dialoghelpers.cpp b/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp index 65564b59a1..258c4305ff 100644 --- a/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp +++ b/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp @@ -575,8 +575,7 @@ static QFont qt_fontFromString(const QString &name) if (!family.isEmpty()) font.setFamily(family); - const int weight = pango_font_description_get_weight(desc); - font.setWeight(QPlatformFontDatabase::weightFromInteger(weight)); + font.setWeight(QFont::Weight(pango_font_description_get_weight(desc))); PangoStyle style = pango_font_description_get_style(desc); if (style == PANGO_STYLE_ITALIC) diff --git a/src/tools/uic/cpp/cppwriteinitialization.cpp b/src/tools/uic/cpp/cppwriteinitialization.cpp index 29c054bc22..2a3c9bdd70 100644 --- a/src/tools/uic/cpp/cppwriteinitialization.cpp +++ b/src/tools/uic/cpp/cppwriteinitialization.cpp @@ -1623,8 +1623,57 @@ QString WriteInitialization::writeFontProperties(const DomFont *f) << language::boolValue(f->elementUnderline()) << ')' << language::eol; } if (f->hasElementWeight() && f->elementWeight() > 0) { - m_output << m_indent << fontName << ".setWeight(" - << f->elementWeight() << ")" << language::eol; + int weight = f->elementWeight(); + if (!f->hasAttributeScale()) { + // Convert from old Qt scale to OpenType scale. + // (not a linear conversion, so we adapt the known values and approximate the others). + // Note that we cannot use qt_legacyToOpenTypeWeight from qfont_p.h here due to + // dependency issues. + + switch (f->elementWeight()) { + case 0: + weight = 100; + break; + case 12: + weight = 200; + break; + case 25: + weight = 300; + break; + case 50: + weight = 400; + break; + case 57: + weight = 500; + break; + case 63: + weight = 600; + break; + case 75: + weight = 700; + break; + case 81: + weight = 800; + break; + case 87: + weight = 900; + break; + default: + weight *= 8; + weight += 100; + break; + } + } + + switch (language::language()) { + case Language::Cpp: + m_output << m_indent << fontName << ".setWeight(QFont::Weight(" << weight << "))" + << language::eol; + break; + case Language::Python: + m_output << m_indent << fontName << ".setWeight(" << weight << ")" << language::eol; + break; + } } if (f->hasElementStrikeOut()) { m_output << m_indent << fontName << ".setStrikeOut(" diff --git a/src/tools/uic/ui4.cpp b/src/tools/uic/ui4.cpp index a155df9b6c..8a535582b9 100644 --- a/src/tools/uic/ui4.cpp +++ b/src/tools/uic/ui4.cpp @@ -3099,6 +3099,16 @@ DomFont::~DomFont() = default; void DomFont::read(QXmlStreamReader &reader) { + const QXmlStreamAttributes &attributes = reader.attributes(); + for (const QXmlStreamAttribute &attribute : attributes) { + const auto name = attribute.name(); + if (name == QLatin1String("scale")) { + setAttributeScale(attribute.value().toString()); + continue; + } + reader.raiseError(QLatin1String("Unexpected attribute ") + name); + } + while (!reader.hasError()) { switch (reader.readNext()) { case QXmlStreamReader::StartElement : { @@ -3158,6 +3168,9 @@ void DomFont::write(QXmlStreamWriter &writer, const QString &tagName) const { writer.writeStartElement(tagName.isEmpty() ? QStringLiteral("font") : tagName.toLower()); + if (hasAttributeScale()) + writer.writeAttribute(QStringLiteral("scale"), attributeScale()); + if (m_children & Family) writer.writeTextElement(QStringLiteral("family"), m_family); diff --git a/src/tools/uic/ui4.h b/src/tools/uic/ui4.h index a5ac51f521..41f11644bd 100644 --- a/src/tools/uic/ui4.h +++ b/src/tools/uic/ui4.h @@ -1619,6 +1619,16 @@ public: void read(QXmlStreamReader &reader); void write(QXmlStreamWriter &writer, const QString &tagName = QString()) const; + // attribute accessors + inline bool hasAttributeScale() const { return m_has_attr_scale; } + inline QString attributeScale() const { return m_attr_scale; } + inline void setAttributeScale(const QString &a) + { + m_attr_scale = a; + m_has_attr_scale = true; + } + inline void clearAttributeScale() { m_has_attr_scale = false; } + // child element accessors inline QString elementFamily() const { return m_family; } void setElementFamily(const QString &a); @@ -1671,6 +1681,9 @@ public: void clearElementKerning(); private: + // attribute data + QString m_attr_scale; + bool m_has_attr_scale = false; // child element data uint m_children = 0; |