Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Turn text rendering into a css property.
Wolthera van Hövell tot Westerflier387615bf06fe
diff --git a/libs/flake/svg/SvgParser.cpp b/libs/flake/svg/SvgParser.cppindex 2d5411a5dc..29bd8d8501 100644--- a/libs/flake/svg/SvgParser.cpp+++ b/libs/flake/svg/SvgParser.cpp@@ -1893,10 +1893,6 @@ KoShape *SvgParser::parseTextElement(const QDomElement &e, KoSvgTextShape *mergem_context.popGraphicsContext();- if (e.hasAttribute("text-rendering")) {- rootTextShape->setTextRenderingFromString(e.attribute("text-rendering"));- }-m_isInsideTextSubtree = false;//rootTextShape->debugParsing();diff --git a/libs/flake/svg/SvgStyleParser.cpp b/libs/flake/svg/SvgStyleParser.cppindex 6225334015..d99af96905 100644--- a/libs/flake/svg/SvgStyleParser.cpp+++ b/libs/flake/svg/SvgStyleParser.cpp@@ -341,7 +341,7 @@ void SvgStyleParser::parsePA(SvgGraphicsContext *gc, const QString &command, con|| command == "word-break" || command == "line-break" || command == "hanging-punctuation" || command == "text-align"|| command == "text-align-all" || command == "text-align-last" || command == "inline-size" || command == "overflow" || command == "text-overflow"|| command == "tab-size" || command == "overflow-wrap" || command == "word-wrap" || command == "vertical-align"- || command == "shape-padding" || command == "shape-margin" || command == "text-orientation") {+ || command == "shape-padding" || command == "shape-margin" || command == "text-orientation" || command == "text-rendering") {gc->textProperties.parseSvgTextAttribute(d->context, command, params);} else if (command == "krita:marker-fill-method") {gc->autoFillMarkers = params == "auto";diff --git a/libs/flake/text/KoSvgText.cpp b/libs/flake/text/KoSvgText.cppindex 6c2a0f8eba..39a1a5ff0c 100644--- a/libs/flake/text/KoSvgText.cpp+++ b/libs/flake/text/KoSvgText.cpp@@ -1659,4 +1659,29 @@ QDebug operator<<(QDebug dbg, const KoSvgText::TextUnderlinePosition &value)return dbg.space();}+TextRendering parseTextRendering(const QString &value)+{+ if (value == "optimizeSpeed") {+ return RenderingOptimizeSpeed;+ } else if (value == "optimizeLegibility") {+ return RenderingOptimizeLegibility;+ } else if (value == "geometricPrecision") {+ return RenderingGeometricPrecision;+ }+ return RenderingAuto;+}++QString writeTextRendering(TextRendering value)+{+ if (value == RenderingOptimizeSpeed) {+ return "optimizeSpeed";+ } else if (value == RenderingOptimizeLegibility) {+ return "optimizeLegibility";+ } else if (value == RenderingGeometricPrecision) {+ return "geometricPrecision";+ } else {+ return "auto";+ }+}+} // namespace KoSvgTextdiff --git a/libs/flake/text/KoSvgText.h b/libs/flake/text/KoSvgText.hindex 441dd4de77..75a50b07fc 100644--- a/libs/flake/text/KoSvgText.h+++ b/libs/flake/text/KoSvgText.h@@ -311,6 +311,14 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(TextSpaceTrims)Q_DECLARE_FLAGS(HangingPunctuations, HangingPunctuation)Q_DECLARE_OPERATORS_FOR_FLAGS(HangingPunctuations)+enum TextRendering {+ RenderingAuto,+ RenderingOptimizeSpeed,+ RenderingOptimizeLegibility,+ RenderingGeometricPrecision+};+Q_ENUM_NS(TextRendering)+/*** @brief The FontMetrics class* A class to keep track of a variety of font metrics.@@ -500,6 +508,8 @@ TextOrientation parseTextOrientation(const QString &value);TextOrientation parseTextOrientationFromGlyphOrientation(AutoValue value);TextAnchor parseTextAnchor(const QString &value);+TextRendering parseTextRendering(const QString &value);+/*** @brief whiteSpaceValueToLongHands* CSS-Text-4 takes CSS-Text-3 whitespace values and treats them as a shorthand@@ -580,6 +590,8 @@ QString writeTextAlign(TextAlign value);QString writeFontStyle(CssFontStyleData value);+QString writeTextRendering(TextRendering value);+/*** @brief writeWhiteSpaceValue* determine the CSS-3-Whitespace shorthand value.diff --git a/libs/flake/text/KoSvgTextProperties.cpp b/libs/flake/text/KoSvgTextProperties.cppindex f546f5858e..c80d50ae50 100644--- a/libs/flake/text/KoSvgTextProperties.cpp+++ b/libs/flake/text/KoSvgTextProperties.cpp@@ -588,6 +588,8 @@ void KoSvgTextProperties::parseSvgTextAttribute(const SvgLoadingContext &contextsetProperty(FontSynthesisSmallCapsId, (value == "auto"));} else if (command == "font-synthesis-position") {setProperty(FontSynthesisSuperSubId, (value == "auto"));+ } else if (command == "text-rendering") {+ setProperty(TextRenderingId, KoSvgText::parseTextRendering(value));} else {qFatal("FATAL: Unknown SVG property: %s = %s", command.toUtf8().data(), value.toUtf8().data());}@@ -941,6 +943,10 @@ QMap<QString, QString> KoSvgTextProperties::convertToSvgTextAttributes() const}}+ if (hasProperty(TextRenderingId)) {+ result.insert("text-rendering", KoSvgText::writeTextRendering(TextRendering(property(TextRenderingId).toInt())));+ }+return result;}@@ -1169,7 +1175,8 @@ QStringList KoSvgTextProperties::supportedXmlAttributes()<< "letter-spacing"<< "word-spacing"<< "xml:space"- << "xml:lang";+ << "xml:lang"+ << "text-rendering";return attributes;}@@ -1208,6 +1215,7 @@ const KoSvgTextProperties &KoSvgTextProperties::defaultProperties()s_defaultProperties->setProperty(FontSynthesisSuperSubId, true);s_defaultProperties->setProperty(FontOpticalSizingId, true);+ s_defaultProperties->setProperty(TextRenderingId, RenderingAuto);{using namespace KoSvgText;TextDecorations deco = DecorationNone;@@ -1241,7 +1249,8 @@ bool KoSvgTextProperties::propertyIsBlockOnly(PropertyId id)id == TextAlignAllId ||id == TextAlignLastId ||id == TextIndentId ||- id == HangingPunctuationId;+ id == HangingPunctuationId ||+ id == TextRenderingId;}bool KoSvgTextProperties::propertyIsInheritable(PropertyId id) constdiff --git a/libs/flake/text/KoSvgTextProperties.h b/libs/flake/text/KoSvgTextProperties.hindex 9cd8e19665..df854c962e 100644--- a/libs/flake/text/KoSvgTextProperties.h+++ b/libs/flake/text/KoSvgTextProperties.h@@ -114,6 +114,8 @@ public:FontSynthesisSmallCapsId, ///< BoolFontSynthesisSuperSubId, ///< Bool+ TextRenderingId, ///< Enum+KraTextVersionId ///< Int, used for handling incorrectly saved files.};diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 8f56aeaf9c..fd53593340 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1182,8 +1182,6 @@ bool KoSvgTextShape::saveSvg(SvgSavingContext &context)if (!context.strippedTextMode()) {context.shapeWriter().addAttribute("id", context.getID(this));- context.shapeWriter().addAttribute("text-rendering", textRenderingString());-// save the version to distinguish from the buggy Krita version// 2: Wrong font-size.// 3: Wrong font-size-adjust.@@ -1192,7 +1190,6 @@ bool KoSvgTextShape::saveSvg(SvgSavingContext &context)SvgUtil::writeTransformAttributeLazy("transform", transformation(), context.shapeWriter());SvgStyleWriter::saveSvgStyle(this, context);} else {- context.shapeWriter().addAttribute("text-rendering", textRenderingString());SvgStyleWriter::saveSvgFill(this->background(), false, this->outlineRect(), this->size(), this->absoluteTransformation(), context);SvgStyleWriter::saveSvgStroke(this->stroke(), context);SvgStyleWriter::saveSvgBasicStyle(true, 0, paintOrder(),@@ -1404,7 +1401,8 @@ bool KoSvgTextShape::fontMatchingDisabled() constvoid KoSvgTextShape::paint(QPainter &painter) const{painter.save();- if (d->textRendering == OptimizeSpeed || !painter.testRenderHint(QPainter::Antialiasing)) {+ KoSvgText::TextRendering textRendering = KoSvgText::TextRendering(textProperties().propertyOrDefault(KoSvgTextProperties::TextRenderingId).toInt());+ if (textRendering == KoSvgText::RenderingOptimizeSpeed || !painter.testRenderHint(QPainter::Antialiasing)) {// also apply antialiasing only if antialiasing is active on provided target QPainterpainter.setRenderHint(QPainter::Antialiasing, false);painter.setRenderHint(QPainter::SmoothPixmapTransform, false);@@ -1418,10 +1416,10 @@ void KoSvgTextShape::paint(QPainter &painter) constif (!d->result.isEmpty()) {QPainterPath rootBounds;rootBounds.addRect(this->outline().boundingRect());- d->paintTextDecoration(painter, rootBounds, this, KoSvgText::DecorationUnderline);- d->paintTextDecoration(painter, rootBounds, this, KoSvgText::DecorationOverline);- d->paintPaths(painter, rootBounds, this, d->result, chunk, currentIndex);- d->paintTextDecoration(painter, rootBounds, this, KoSvgText::DecorationLineThrough);+ d->paintTextDecoration(painter, rootBounds, this, KoSvgText::DecorationUnderline, textRendering);+ d->paintTextDecoration(painter, rootBounds, this, KoSvgText::DecorationOverline, textRendering);+ d->paintPaths(painter, rootBounds, this, d->result, textRendering, chunk, currentIndex);+ d->paintTextDecoration(painter, rootBounds, this, KoSvgText::DecorationLineThrough, textRendering);}#if 0 // DebugQ_FOREACH (KoShape *child, this->shapes()) {@@ -1563,32 +1561,6 @@ QList<KoShape *> KoSvgTextShape::textOutline() constreturn shapes;}-void KoSvgTextShape::setTextRenderingFromString(const QString &textRendering)-{- if (textRendering == "optimizeSpeed") {- d->textRendering = OptimizeSpeed;- } else if (textRendering == "optimizeLegibility") {- d->textRendering = OptimizeLegibility;- } else if (textRendering == "geometricPrecision") {- d->textRendering = GeometricPrecision;- } else {- d->textRendering = Auto;- }-}--QString KoSvgTextShape::textRenderingString() const-{- if (d->textRendering == OptimizeSpeed) {- return "optimizeSpeed";- } else if (d->textRendering == OptimizeLegibility) {- return "optimizeLegibility";- } else if (d->textRendering == GeometricPrecision) {- return "geometricPrecision";- } else {- return "auto";- }-}-void KoSvgTextShape::setShapesInside(QList<KoShape *> shapesInside){d->shapesInside = shapesInside;diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex d69eead940..b0758bfff8 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -33,13 +33,6 @@ public:static const QString &defaultPlaceholderText();- enum TextRendering {- Auto,- OptimizeSpeed,- OptimizeLegibility,- GeometricPrecision- };-KoShape* cloneShape() const override;void paint(QPainter &painter) const override;@@ -65,29 +58,6 @@ public:QList<KoShape *> textOutline() const;- /**- * @brief setTextRenderingFromString- * Set the quality of the text rendering.- *- * Based on: https://www.w3.org/TR/SVG11/painting.html#TextRenderingProperty- *- * "auto" is the default.- * "optimizeSpeed" will turn off anti-aliasing.- * "optimizeLegibility" will have us load the hinting metrics.- * "geometricPrecision" will not load the hinting metrics.- *- * @param textRendering the textRendering to use.- */- void setTextRenderingFromString(const QString &textRendering);-- /**- * @brief textRenderingString- * @see setTextRenderingFromString- * @return the Text Rendering type as a string.- */-- QString textRenderingString() const;-/*** @brief setShapesInside* @param shapesInside the list of shapes to make up the content area.diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex d00f9296d1..46a2700346 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -452,7 +452,6 @@ public:shapesSubtract.append(clonedShape);}- textRendering = rhs.textRendering;yRes = rhs.yRes;xRes = rhs.xRes;result = rhs.result;@@ -468,7 +467,6 @@ public:disableFontMatching = rhs.disableFontMatching;}- TextRendering textRendering = Auto;int xRes = 72;int yRes = 72;QList<KoShape*> shapesInside;@@ -491,19 +489,20 @@ public:void relayout();- bool loadGlyph(const QTransform &ftTF,- FT_Int32 faceLoadFlags,- bool isHorizontal,- char32_t firstCodepoint,+ static bool loadGlyph(const QTransform &ftTF,+ const FT_Int32 faceLoadFlags,+ const bool isHorizontal,+ const char32_t firstCodepoint,+ const KoSvgText::TextRendering rendering,raqm_glyph_t ¤tGlyph,CharacterResult &charResult,- QPointF &totalAdvanceFTFontCoordinates) const;+ QPointF &totalAdvanceFTFontCoordinates);- std::pair<QTransform, qreal> loadGlyphOnly(const QTransform &ftTF,+ static std::pair<QTransform, qreal> loadGlyphOnly(const QTransform &ftTF,FT_Int32 faceLoadFlags,bool isHorizontal,raqm_glyph_t ¤tGlyph,- CharacterResult &charResult) const;+ CharacterResult &charResult, const KoSvgText::TextRendering rendering);void clearAssociatedOutlines();void resolveTransforms(KisForest<KoSvgTextContentElement>::child_iterator currentTextElement,@@ -576,12 +575,13 @@ public:void paintTextDecoration(QPainter &painter,const QPainterPath &outlineRect,const KoShape *rootShape,- const KoSvgText::TextDecoration type- );+ const KoSvgText::TextDecoration type,+ const KoSvgText::TextRendering rendering);void paintPaths(QPainter &painter,const QPainterPath &outlineRect,const KoShape *rootShape,const QVector<CharacterResult> &result,+ const KoSvgText::TextRendering rendering,QPainterPath &chunk,int ¤tIndex);QList<KoShape *> collectPaths(const KoShape *rootShape, QVector<CharacterResult> &result, int ¤tIndex);diff --git a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp b/libs/flake/text/KoSvgTextShape_p_glyphs.cppindex 78c6ea6b35..7838937104 100644--- a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp+++ b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp@@ -279,7 +279,8 @@ std::pair<QTransform, qreal> KoSvgTextShape::Private::loadGlyphOnly(const QTransconst FT_Int32 faceLoadFlags,const bool isHorizontal,raqm_glyph_t ¤tGlyph,- CharacterResult &charResult) const+ CharacterResult &charResult,+ const KoSvgText::TextRendering rendering){/// The matrix for Italic (oblique) synthesis of outline glyphs, or for/// adjusting the bounding box of bitmap glyphs.@@ -454,7 +455,7 @@ std::pair<QTransform, qreal> KoSvgTextShape::Private::loadGlyphOnly(const QTransconst QSize srcSize = image.size();bitmapGlyph->images.replace(bitmapGlyph->images.size()-1, std::move(image).transformed(bitmapTf,- this->textRendering == OptimizeSpeed ? Qt::FastTransformation : Qt::SmoothTransformation));+ rendering == KoSvgText::RenderingOptimizeSpeed ? Qt::FastTransformation : Qt::SmoothTransformation));// This does the same as `QImage::trueMatrix` to get the image// offset after transforming.@@ -477,9 +478,10 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,const FT_Int32 faceLoadFlags,const bool isHorizontal,const char32_t firstCodepoint,+ const KoSvgText::TextRendering rendering,raqm_glyph_t ¤tGlyph,CharacterResult &charResult,- QPointF &totalAdvanceFTFontCoordinates) const+ QPointF &totalAdvanceFTFontCoordinates){// Whenever the freetype docs talk about a 26.6 floating point unit, they// mean a 1/64 value.@@ -493,7 +495,7 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,qreal bitmapScale = 1.0;// Try to load the glyph- std::tie(glyphObliqueTf, bitmapScale) = loadGlyphOnly(ftTF, faceLoadFlags, isHorizontal, currentGlyph, charResult);+ std::tie(glyphObliqueTf, bitmapScale) = loadGlyphOnly(ftTF, faceLoadFlags, isHorizontal, currentGlyph, charResult, rendering);if (charResult.visualIndex == -1) {hb_font_t_sp font(hb_ft_font_create_referenced(currentGlyph.ftface));diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex 2995c9b4bf..cedefebb5d 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -129,12 +129,13 @@ void KoSvgTextShape::Private::relayout()KoSvgText::WritingMode writingMode = KoSvgText::WritingMode(rootProperties.propertyOrDefault(KoSvgTextProperties::WritingModeId).toInt());KoSvgText::Direction direction = KoSvgText::Direction(rootProperties.propertyOrDefault(KoSvgTextProperties::DirectionId).toInt());KoSvgText::AutoValue inlineSize = rootProperties.propertyOrDefault(KoSvgTextProperties::InlineSizeId).value<KoSvgText::AutoValue>();+ const KoSvgText::TextRendering textRendering = KoSvgText::TextRendering(rootProperties.propertyOrDefault(KoSvgTextProperties::TextRenderingId).toInt());QString lang = rootProperties.property(KoSvgTextProperties::TextLanguage).toString();const bool isHorizontal = writingMode == KoSvgText::HorizontalTB;FT_Int32 loadFlags = 0;- if (this->textRendering == GeometricPrecision || this->textRendering == Auto) {+ if (textRendering == KoSvgText::RenderingGeometricPrecision || textRendering == KoSvgText::RenderingAuto) {// without load_no_hinting, the advance and offset will be rounded// to nearest pixel, which we don't want as we're using the vector// outline.@@ -575,6 +576,7 @@ void KoSvgTextShape::Private::relayout()faceLoadFlags,isHorizontal,codepoint,+ textRendering,currentGlyph,charResult,totalAdvanceFTFontCoordinates)) {diff --git a/libs/flake/text/KoSvgTextShape_p_output.cpp b/libs/flake/text/KoSvgTextShape_p_output.cppindex 4df05128ff..4a20ad03b1 100644--- a/libs/flake/text/KoSvgTextShape_p_output.cpp+++ b/libs/flake/text/KoSvgTextShape_p_output.cpp@@ -52,8 +52,8 @@ void inheritPaintProperties(KisForest<KoSvgTextContentElement>::composition_iter}}-void setRenderHints(QPainter &painter, const KoSvgTextShape::TextRendering textRendering, const bool testAntialiasing) {- if (textRendering != KoSvgTextShape::OptimizeSpeed && testAntialiasing) {+void setRenderHints(QPainter &painter, const KoSvgText::TextRendering textRendering, const bool testAntialiasing) {+ if (textRendering != KoSvgText::RenderingOptimizeSpeed && testAntialiasing) {// also apply antialiasing only if antialiasing is active on provided target QPainterpainter.setRenderHint(QPainter::Antialiasing, true);painter.setRenderHint(QPainter::SmoothPixmapTransform, true);@@ -63,7 +63,11 @@ void setRenderHints(QPainter &painter, const KoSvgTextShape::TextRendering textR}}-void KoSvgTextShape::Private::paintTextDecoration(QPainter &painter, const QPainterPath &rootOutline, const KoShape *rootShape, const KoSvgText::TextDecoration type)+void KoSvgTextShape::Private::paintTextDecoration(QPainter &painter,+ const QPainterPath &rootOutline,+ const KoShape *rootShape,+ const KoSvgText::TextDecoration type,+ const KoSvgText::TextRendering rendering){KoShapeStrokeModelSP stroke = rootShape->stroke();QSharedPointer<KoShapeBackground> background = rootShape->background();@@ -99,7 +103,7 @@ void KoSvgTextShape::Private::paintTextDecoration(QPainter &painter, const QPainif (background && !colorValid) {KoClipMaskPainter fillPainter(&painter, shapeGlobalClipRect);- setRenderHints(*fillPainter.maskPainter(), textRendering, painter.testRenderHint(QPainter::Antialiasing));+ setRenderHints(*fillPainter.maskPainter(), rendering, painter.testRenderHint(QPainter::Antialiasing));background->paint(*fillPainter.shapePainter(), rootOutline);fillPainter.maskPainter()->fillPath(rootOutline, Qt::black);fillPainter.maskPainter()->fillPath(decorPath, Qt::white);@@ -124,7 +128,7 @@ void KoSvgTextShape::Private::paintTextDecoration(QPainter &painter, const QPainmaskStroke->setLineBrush(Qt::white);- setRenderHints(*strokePainter.maskPainter(), textRendering, painter.testRenderHint(QPainter::Antialiasing));+ setRenderHints(*strokePainter.maskPainter(), rendering, painter.testRenderHint(QPainter::Antialiasing));{QScopedPointer<KoShape> shape(KoPathShape::createShapeFromPainterPath(decorPath));maskStroke->paint(shape.data(), *strokePainter.maskPainter());@@ -148,7 +152,7 @@ void KoSvgTextShape::Private::paintTextDecoration(QPainter &painter, const QPainvoid KoSvgTextShape::Private::paintPaths(QPainter &painter,const QPainterPath &rootOutline,const KoShape *rootShape,- const QVector<CharacterResult> &result,+ const QVector<CharacterResult> &result, const KoSvgText::TextRendering rendering,QPainterPath &chunk,int ¤tIndex){@@ -177,7 +181,7 @@ void KoSvgTextShape::Private::paintPaths(QPainter &painter,if (background) {background->paint(*fillPainter.shapePainter(), rootOutline);fillPainter.maskPainter()->fillPath(rootOutline, Qt::black);- setRenderHints(*fillPainter.maskPainter(), textRendering, painter.testRenderHint(QPainter::Antialiasing));+ setRenderHints(*fillPainter.maskPainter(), rendering, painter.testRenderHint(QPainter::Antialiasing));}QPainterPath textDecorationsRest;textDecorationsRest.setFillRule(Qt::WindingFill);@@ -268,7 +272,7 @@ void KoSvgTextShape::Private::paintPaths(QPainter &painter,maskStroke = KoShapeStrokeSP(new KoShapeStroke(*strokeSP.data()));maskStroke->setColor(Qt::white);maskStroke->setLineBrush(Qt::white);- setRenderHints(*strokePainter.maskPainter(), textRendering, painter.testRenderHint(QPainter::Antialiasing));+ setRenderHints(*strokePainter.maskPainter(), rendering, painter.testRenderHint(QPainter::Antialiasing));{QScopedPointer<KoShape> shape(KoPathShape::createShapeFromPainterPath(chunk));maskStroke->paint(shape.data(), *strokePainter.maskPainter());