diff options
author | Andy Shaw <andy.shaw@qt.io> | 2020-05-11 14:34:27 +0200 |
---|---|---|
committer | Andy Shaw <andy.shaw@qt.io> | 2020-07-06 14:35:57 +0200 |
commit | 96cea3b1681dd24a0ec3a53078b78f902e3211a6 (patch) | |
tree | 61f912d2560c256fb286b523412aa4be82f98bba /src/gui | |
parent | 60c6f4a51ae6d3de7f9b765f89be41e193f5c19a (diff) |
Export the letter and word spacing settings set on the default format
When the default format has letter and word spacing set then these
should be exported in the HTML's body tag. This also adds support
for the reading of letter-spacing and word-spacing set too, so that
the same html outputted can be read back in.
Fixes: QTBUG-83718
Change-Id: Ic4afca21eb05efb779dbf99c6b3c13373e851f15
Pick-to: 5.15
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/text/qcssparser.cpp | 39 | ||||
-rw-r--r-- | src/gui/text/qcssparser_p.h | 2 | ||||
-rw-r--r-- | src/gui/text/qtextdocument.cpp | 18 |
3 files changed, 58 insertions, 1 deletions
diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index 2f7ccfb07e..e6f58eeb14 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -127,6 +127,7 @@ static const QCssKnownValue properties[NumProperties - 1] = { { "image", QtImage }, { "image-position", QtImageAlignment }, { "left", Left }, + { "letter-spacing", LetterSpacing }, { "line-height", LineHeight }, { "list-style", ListStyle }, { "list-style-type", ListStyleType }, @@ -171,7 +172,8 @@ static const QCssKnownValue properties[NumProperties - 1] = { { "top", Top }, { "vertical-align", VerticalAlignment }, { "white-space", Whitespace }, - { "width", Width } + { "width", Width }, + { "word-spacing", WordSpacing } }; static const QCssKnownValue values[NumKnownValues - 1] = { @@ -386,6 +388,8 @@ static inline bool isInheritable(Property propertyId) case FontVariant: case TextTransform: case LineHeight: + case LetterSpacing: + case WordSpacing: return true; default: break; @@ -1247,6 +1251,37 @@ static void setTextDecorationFromValues(const QVector<QCss::Value> &values, QFon } } +static void setLetterSpacingFromValue(const QCss::Value &value, QFont *font) +{ + QString s = value.variant.toString(); + qreal val; + bool ok = false; + if (s.endsWith(QLatin1String("em"), Qt::CaseInsensitive)) { + s.chop(2); + val = s.toDouble(&ok); + if (ok) + font->setLetterSpacing(QFont::PercentageSpacing, (val + 1.0) * 100); + } else if (s.endsWith(QLatin1String("px"), Qt::CaseInsensitive)) { + s.chop(2); + val = s.toDouble(&ok); + if (ok) + font->setLetterSpacing(QFont::AbsoluteSpacing, val); + } +} + +static void setWordSpacingFromValue(const QCss::Value &value, QFont *font) +{ + QString s = value.variant.toString(); + if (s.endsWith(QLatin1String("px"), Qt::CaseInsensitive)) { + s.chop(2); + qreal val; + bool ok = false; + val = s.toDouble(&ok); + if (ok) + font->setWordSpacing(val); + } +} + static void parseShorthandFontProperty(const QVector<QCss::Value> &values, QFont *font, int *fontSizeAdjustment) { font->setStyle(QFont::StyleNormal); @@ -1319,6 +1354,8 @@ bool ValueExtractor::extractFont(QFont *font, int *fontSizeAdjustment) case Font: parseShorthandFontProperty(decl.d->values, font, fontSizeAdjustment); break; case FontVariant: setFontVariantFromValue(val, font); break; case TextTransform: setTextTransformFromValue(val, font); break; + case LetterSpacing: setLetterSpacingFromValue(val, font); break; + case WordSpacing: setWordSpacingFromValue(val, font); break; default: continue; } hit = true; diff --git a/src/gui/text/qcssparser_p.h b/src/gui/text/qcssparser_p.h index f340a23cde..1f71a1c4af 100644 --- a/src/gui/text/qcssparser_p.h +++ b/src/gui/text/qcssparser_p.h @@ -199,6 +199,8 @@ enum Property { FontKerning, QtForegroundTextureCacheKey, QtIcon, + LetterSpacing, + WordSpacing, NumProperties }; diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index 04edf89430..0890614be9 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -2228,6 +2228,24 @@ QString QTextHtmlExporter::toHtml(ExportMode mode) html += (defaultCharFormat.fontItalic() ? QLatin1String("italic") : QLatin1String("normal")); html += QLatin1Char(';'); + const bool percentSpacing = (defaultCharFormat.fontLetterSpacingType() == QFont::PercentageSpacing); + if (defaultCharFormat.hasProperty(QTextFormat::FontLetterSpacing) && + (!percentSpacing || defaultCharFormat.fontLetterSpacing() != 0.0)) { + html += QLatin1String(" letter-spacing:"); + qreal value = defaultCharFormat.fontLetterSpacing(); + if (percentSpacing) // Map to em (100% == 0em) + value = (value / 100) - 1; + html += QString::number(value); + html += percentSpacing ? QLatin1String("em;") : QLatin1String("px;"); + } + + if (defaultCharFormat.hasProperty(QTextFormat::FontWordSpacing) && + defaultCharFormat.fontWordSpacing() != 0.0) { + html += QLatin1String(" word-spacing:"); + html += QString::number(defaultCharFormat.fontWordSpacing()); + html += QLatin1String("px;"); + } + // do not set text-decoration on the default font since those values are /always/ propagated // and cannot be turned off with CSS |