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++
Introduce scaleCharacterResult to both handle <1pt font sizes as simplify code.
Wolthera van Hövell tot Westerflier3dbc6d857ab8
diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cppindex 1c0c77f6f7..76cb83e47a 100644--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp@@ -66,11 +66,10 @@ void addWordToLine(QVector<CharacterResult> &result,CharacterResult cr = result.at(j);if (currentLine.isEmpty() && j == wordIndices.first()) {if (result.at(j).lineStart == LineEdgeBehaviour::Collapse) {- result[j].advance = QPointF();if (isHorizontal) {- result[j].inkBoundingBox.setWidth(0);+ result[j].scaleCharacterResult(0.0, 1.0);} else {- result[j].inkBoundingBox.setHeight(0);+ result[j].scaleCharacterResult(1.0, 0.0);}result[j].hidden = true;continue;diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex dc18c777f4..e2a35b5b07 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -119,6 +119,7 @@ struct CharacterResult {bool textLengthApplied = false;bool overflowWrap = false;+ qreal extraFontScaling = 1.0; ///< Freetype doesn't allow us to scale below 1pt, so we need to do an extra transformation in these cases.qreal fontHalfLeading; ///< Leading for both sides, can be either negative or positive.KoSvgText::FontMetrics metrics; ///< Fontmetrics for current font, in Freetype scanline coordinates.qreal scaledHalfLeading{}; ///< Leading for both sides, can be either negative or positive, in pt@@ -186,6 +187,66 @@ struct CharacterResult {}}+ /**+ * @brief scaleCharacterResult+ * convenience function to scale the whole character result.+ * @param xScale -- the factor by which the width should be scaled.+ * @param yScale -- the factor by which the height should be scaled.+ */+ void scaleCharacterResult(qreal xScale, qreal yScale) {+ QTransform scale = QTransform::fromScale(xScale, yScale);+ if (scale.isIdentity()) return;+ const bool scaleToZero = !(xScale > 0 && yScale > 0);++ if (Glyph::Outline *outlineGlyph = std::get_if<Glyph::Outline>(&glyph)) {+ if (!outlineGlyph->path.isEmpty()) {+ if (scaleToZero) {+ outlineGlyph->path = QPainterPath();+ } else {+ outlineGlyph->path = scale.map(outlineGlyph->path);+ }+ }+ } else if (Glyph::Bitmap *bitmapGlyph = std::get_if<Glyph::Bitmap>(&glyph)) {+ if (scaleToZero) {+ bitmapGlyph->drawRects.clear();+ bitmapGlyph->images.clear();+ } else {+ for (int i = 0; i< bitmapGlyph->drawRects.size(); i++) {+ bitmapGlyph->drawRects[i] = scale.mapRect(bitmapGlyph->drawRects[i]);+ }+ }+ } else if (Glyph::ColorLayers *colorGlyph = std::get_if<Glyph::ColorLayers>(&glyph)) {+ for (int i = 0; i< colorGlyph->paths.size(); i++) {+ if (scaleToZero) {+ colorGlyph->paths[i] = QPainterPath();+ } else {+ colorGlyph->paths[i] = scale.map(colorGlyph->paths[i]);+ }+ }+ }+ advance = scale.map(advance);+ cursorInfo.caret = scale.map(cursorInfo.caret);+ inkBoundingBox = scale.mapRect(inkBoundingBox);++ if (isHorizontal) {+ scaledDescent *= yScale;+ scaledAscent *= yScale;+ scaledHalfLeading *= yScale;+ metrics.scaleBaselines(yScale);+ if (tabSize) {+ tabSize = scale.map(QPointF(*tabSize, *tabSize)).x();+ }+ } else {+ scaledDescent *= xScale;+ scaledAscent *= xScale;+ scaledHalfLeading *= xScale;+ metrics.scaleBaselines(xScale);+ if (tabSize) {+ tabSize = scale.map(QPointF(*tabSize, *tabSize)).y();+ }+ }+ }+QPointF totalBaselineOffset() const {return baselineOffset+dominantBaselineOffset;}diff --git a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp b/libs/flake/text/KoSvgTextShape_p_glyphs.cppindex 76070ab184..e0a5e8691d 100644--- a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp+++ b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp@@ -604,6 +604,7 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,charResult.scaledHalfLeading = ftTF.map(QPointF(charResult.fontHalfLeading, charResult.fontHalfLeading)).x();charResult.scaledAscent = isHorizontal? scaledBBox.top(): scaledBBox.right();charResult.scaledDescent = isHorizontal? scaledBBox.bottom(): scaledBBox.left();+if (charResult.tabSize) {charResult.tabSize = ftTF.map(QPointF(*charResult.tabSize, *charResult.tabSize)).x();}@@ -611,6 +612,9 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,if(!qFuzzyCompare(bitmapScale, 1.0)) {charResult.metrics.scaleBaselines(bitmapScale);}+ if (charResult.extraFontScaling < 1.0) {+ charResult.scaleCharacterResult(charResult.extraFontScaling, charResult.extraFontScaling);+ }if (bitmapGlyph) {charResult.inkBoundingBox |= bitmapGlyph->drawRects.last();diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex bec1b9f403..6268926012 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -444,6 +444,7 @@ void KoSvgTextShape::Private::relayout()static_cast<quint32>(finalRes),static_cast<quint32>(finalRes),disableFontMatching);+ const qreal fontSize = properties.cssFontInfo().size;if (properties.hasProperty(KoSvgTextProperties::TextLanguage)) {raqm_set_language(layout.data(),properties.property(KoSvgTextProperties::TextLanguage).toString().toUtf8(),@@ -495,6 +496,9 @@ void KoSvgTextShape::Private::relayout()metricsList.insert(currentScript, KoFontRegistry::generateFontMetrics(face, isHorizontal, KoWritingSystemUtils::scriptTagForQCharScript(currentScript)));}result[j].metrics = metricsList.value(currentScript);+ if (fontSize < 1.0) {+ result[j].extraFontScaling = fontSize;+ }const KoSvgText::FontMetrics currentMetrics = properties.applyLineHeight(result[j].metrics);@@ -647,11 +651,9 @@ void KoSvgTextShape::Private::relayout()dummy.finalPosition = dummy.cssPosition;dummy.inkBoundingBox = hardbreak.inkBoundingBox;if (isHorizontal) {- dummy.advance.setX(0);- dummy.inkBoundingBox.setWidth(0);+ dummy.scaleCharacterResult(0.0, 1.0);} else {- dummy.advance.setY(0);- dummy.inkBoundingBox.setHeight(0);+ dummy.scaleCharacterResult(1.0, 0.0);}dummy.plaintTextIndex = hardbreak.cursorInfo.graphemeIndices.last();dummy.cursorInfo.caret = hardbreak.cursorInfo.caret;@@ -1032,13 +1034,7 @@ void KoSvgTextShape::Private::applyTextLength(KisForest<KoSvgTextContentElement>cr.finalPosition += shift;if (spacingAndGlyphs) {QPointF scale(d.x() != 0 ? (d.x() / cr.advance.x()) + 1 : 1.0, d.y() != 0 ? (d.y() / cr.advance.y()) + 1 : 1.0);- QTransform tf = QTransform::fromScale(scale.x(), scale.y());- // FIXME: What about other glyph formats?- if (auto *outlineGlyph = std::get_if<Glyph::Outline>(&cr.glyph)) {- outlineGlyph->path = tf.map(outlineGlyph->path);- }- cr.advance = tf.map(cr.advance);- cr.inkBoundingBox = tf.mapRect(cr.inkBoundingBox);+ cr.scaleCharacterResult(scale.x(), scale.y());}bool last = spacingAndGlyphs ? false : k == visualToLogical.keys().last();