diff options
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/text/qtextformat.cpp | 103 | ||||
-rw-r--r-- | src/gui/text/qtextformat.h | 8 | ||||
-rw-r--r-- | src/gui/text/qtexthtmlparser.cpp | 28 |
3 files changed, 88 insertions, 51 deletions
diff --git a/src/gui/text/qtextformat.cpp b/src/gui/text/qtextformat.cpp index 641a2ceb8a..d2b91c1f9f 100644 --- a/src/gui/text/qtextformat.cpp +++ b/src/gui/text/qtextformat.cpp @@ -1879,36 +1879,93 @@ QStringList QTextCharFormat::anchorNames() const */ /*! + \enum QTextCharFormat::FontPropertiesInheritanceBehavior + \since 5.3 + + This enum specifies how the setFont() function should behave with + respect to unset font properties. + + \value FontPropertiesSpecifiedOnly If a property is not explicitly set, do not + change the text format's property value. + \value FontPropertiesAll If a property is not explicitly set, override the + text format's property with a default value. + + \sa setFont() +*/ + +/*! + \overload + Sets the text format's \a font. + + \sa font() */ void QTextCharFormat::setFont(const QFont &font) { - setFontFamily(font.family()); + setFont(font, FontPropertiesAll); +} - const qreal pointSize = font.pointSizeF(); - if (pointSize > 0) { - setFontPointSize(pointSize); - } else { - const int pixelSize = font.pixelSize(); - if (pixelSize > 0) - setProperty(QTextFormat::FontPixelSize, pixelSize); +/*! + \since 5.3 + + Sets the text format's \a font. + + If \a behavior is QTextCharFormat::FontPropertiesAll, the font property that + has not been explicitly set is treated like as it were set with default value; + If \a behavior is QTextCharFormat::FontPropertiesAll, the font property that + has not been explicitly set is ignored and the respective property value + remains unchanged. + + \sa font() +*/ +void QTextCharFormat::setFont(const QFont &font, FontPropertiesInheritanceBehavior behavior) +{ + const uint mask = behavior == FontPropertiesAll ? uint(QFont::AllPropertiesResolved) + : font.resolve(); + + if (mask & QFont::FamilyResolved) + setFontFamily(font.family()); + if (mask & QFont::SizeResolved) { + const qreal pointSize = font.pointSizeF(); + if (pointSize > 0) { + setFontPointSize(pointSize); + } else { + const int pixelSize = font.pixelSize(); + if (pixelSize > 0) + setProperty(QTextFormat::FontPixelSize, pixelSize); + } } - setFontWeight(font.weight()); - setFontItalic(font.italic()); - setUnderlineStyle(font.underline() ? SingleUnderline : NoUnderline); - setFontOverline(font.overline()); - setFontStrikeOut(font.strikeOut()); - setFontFixedPitch(font.fixedPitch()); - setFontCapitalization(font.capitalization()); - setFontWordSpacing(font.wordSpacing()); - setFontLetterSpacingType(font.letterSpacingType()); - setFontLetterSpacing(font.letterSpacing()); - setFontStretch(font.stretch()); - setFontStyleHint(font.styleHint()); - setFontStyleStrategy(font.styleStrategy()); - setFontHintingPreference(font.hintingPreference()); - setFontKerning(font.kerning()); + if (mask & QFont::WeightResolved) + setFontWeight(font.weight()); + if (mask & QFont::StyleResolved) + setFontItalic(font.style() != QFont::StyleNormal); + if (mask & QFont::UnderlineResolved) + setUnderlineStyle(font.underline() ? SingleUnderline : NoUnderline); + if (mask & QFont::OverlineResolved) + setFontOverline(font.overline()); + if (mask & QFont::StrikeOutResolved) + setFontStrikeOut(font.strikeOut()); + if (mask & QFont::FixedPitchResolved) + setFontFixedPitch(font.fixedPitch()); + if (mask & QFont::CapitalizationResolved) + setFontCapitalization(font.capitalization()); + if (mask & QFont::LetterSpacingResolved) + setFontWordSpacing(font.wordSpacing()); + if (mask & QFont::LetterSpacingResolved) { + setFontLetterSpacingType(font.letterSpacingType()); + setFontLetterSpacing(font.letterSpacing()); + } + if (mask & QFont::StretchResolved) + setFontStretch(font.stretch()); + if (mask & QFont::StyleHintResolved) + setFontStyleHint(font.styleHint()); + if (mask & QFont::StyleStrategyResolved) + setFontStyleStrategy(font.styleStrategy()); + if (mask & QFont::HintingPreferenceResolved) + setFontHintingPreference(font.hintingPreference()); + if (mask & QFont::KerningResolved) + setFontKerning(font.kerning()); } /*! diff --git a/src/gui/text/qtextformat.h b/src/gui/text/qtextformat.h index c04ac3876b..1906a8a0c2 100644 --- a/src/gui/text/qtextformat.h +++ b/src/gui/text/qtextformat.h @@ -408,7 +408,13 @@ public: QTextCharFormat(); bool isValid() const { return isCharFormat(); } - void setFont(const QFont &font); + + enum FontPropertiesInheritanceBehavior { + FontPropertiesSpecifiedOnly, + FontPropertiesAll + }; + void setFont(const QFont &font, FontPropertiesInheritanceBehavior behavior); + void setFont(const QFont &font); // ### Qt6: Merge with above QFont font() const; inline void setFontFamily(const QString &family) diff --git a/src/gui/text/qtexthtmlparser.cpp b/src/gui/text/qtexthtmlparser.cpp index 3cb61b9eae..e8a02c44b2 100644 --- a/src/gui/text/qtexthtmlparser.cpp +++ b/src/gui/text/qtexthtmlparser.cpp @@ -1361,33 +1361,7 @@ void QTextHtmlParserNode::applyCssDeclarations(const QVector<QCss::Declaration> QFont f; int adjustment = -255; extractor.extractFont(&f, &adjustment); - if (f.resolve() & QFont::SizeResolved) { - if (f.pointSize() > 0) { - charFormat.setFontPointSize(f.pointSize()); - } else if (f.pixelSize() > 0) { - charFormat.setProperty(QTextFormat::FontPixelSize, f.pixelSize()); - } - } - if (f.resolve() & QFont::StyleResolved) - charFormat.setFontItalic(f.style() != QFont::StyleNormal); - - if (f.resolve() & QFont::WeightResolved) - charFormat.setFontWeight(f.weight()); - - if (f.resolve() & QFont::FamilyResolved) - charFormat.setFontFamily(f.family()); - - if (f.resolve() & QFont::UnderlineResolved) - charFormat.setUnderlineStyle(f.underline() ? QTextCharFormat::SingleUnderline : QTextCharFormat::NoUnderline); - - if (f.resolve() & QFont::OverlineResolved) - charFormat.setFontOverline(f.overline()); - - if (f.resolve() & QFont::StrikeOutResolved) - charFormat.setFontStrikeOut(f.strikeOut()); - - if (f.resolve() & QFont::CapitalizationResolved) - charFormat.setFontCapitalization(f.capitalization()); + charFormat.setFont(f, QTextCharFormat::FontPropertiesSpecifiedOnly); if (adjustment >= -1) charFormat.setProperty(QTextFormat::FontSizeAdjustment, adjustment); |