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++
Split up charResult bounding box into layout and ink bounding boxes.
Wolthera van Hövell tot Westerflier4b8c2bf00aea
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex e2ccd82609..22631765a9 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -676,7 +676,7 @@ int KoSvgTextShape::posForPoint(QPointF point, int start, int end, bool *overlapcandidate = i;closest = distance;if (overlaps) {- *overlaps = res.finalTransform().map(res.boundingBox).containsPoint(point, Qt::WindingFill);+ *overlaps = res.finalTransform().map(res.layoutBox()).containsPoint(point, Qt::WindingFill);}}}@@ -1483,13 +1483,17 @@ QRectF KoSvgTextShape::boundingRect() constif (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {stroke = it->properties.property(KoSvgTextProperties::StrokeId).value<KoSvgText::StrokeProperty>().property;}+ QRectF bb = it->associatedOutline.boundingRect();+ QMap<KoSvgText::TextDecoration, QPainterPath> decorations = it->textDecorations;+ for (int i = 0; i < decorations.values().size(); ++i) {+ bb |= decorations.values().at(i).boundingRect();+ }if (stroke) {- QRectF bb = it->associatedOutline.boundingRect();KoInsets insets;stroke->strokeInsets(this, insets);result |= bb.adjusted(-insets.left, -insets.top, insets.right, insets.bottom);} else {- result |= it->associatedOutline.boundingRect();+ result |= bb;}}return this->absoluteTransformation().mapRect(result);diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cppindex e3ddf2992f..744696debe 100644--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp@@ -490,10 +490,10 @@ QVector<LineBox> flowTextInShapes(const KoSvgTextProperties &properties,&& !(currentLine.isEmpty() && wordIndices.isEmpty()));if (!doNotCountAdvance) {if (wordIndices.isEmpty()) {- wordBox = charResult.lineHeightBox.translated(charResult.baselineOffset);+ wordBox = charResult.lineHeightBox().translated(charResult.baselineOffset);wordAdvance = charResult.advance;} else {- wordBox |= charResult.lineHeightBox.translated(wordAdvance+charResult.baselineOffset);+ wordBox |= charResult.lineHeightBox().translated(wordAdvance+charResult.baselineOffset);wordAdvance += charResult.advance;}}@@ -604,7 +604,7 @@ QVector<LineBox> flowTextInShapes(const KoSvgTextProperties &properties,if (!foundFirst && firstLine && !wordIndices.isEmpty() && !currentShape.isEmpty()) {// Last-ditch attempt to get any kind of positioning to happen.// This typically happens when wrapping has been disabled.- wordBox = result[wordIndices.first()].lineHeightBox.translated(result[wordIndices.first()].baselineOffset);+ wordBox = result[wordIndices.first()].lineHeightBox().translated(result[wordIndices.first()].baselineOffset);foundFirst = getFirstPosition(currentPos, currentShape, wordBox, lineOffset, writingMode, ltr);lastDitch = true;}diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cppindex 2728b8e395..81a62ce3b3 100644--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_lines.cpp@@ -77,9 +77,9 @@ void addWordToLine(QVector<CharacterResult> &result,if (result.at(j).lineStart == LineEdgeBehaviour::Collapse) {result[j].advance = QPointF();if (isHorizontal) {- result[j].boundingBox.setWidth(0);+ result[j].inkBoundingBox.setWidth(0);} else {- result[j].boundingBox.setHeight(0);+ result[j].inkBoundingBox.setHeight(0);}result[j].hidden = true;continue;@@ -100,7 +100,7 @@ void addWordToLine(QVector<CharacterResult> &result,lineAdvance = currentPos;result[j] = cr;- currentChunk.boundingBox |= cr.boundingBox.translated(cr.cssPosition + cr.baselineOffset);+ currentChunk.boundingBox |= cr.layoutBox().translated(cr.cssPosition + cr.baselineOffset);}currentPos = lineAdvance;currentChunk.chunkIndices += wordIndices;@@ -213,9 +213,9 @@ handleCollapseAndHang(QVector<CharacterResult> &result, LineChunk &chunk, bool l// still be possible to track it by cursor movement.result[lastIndex].advance = QPointF();if (isHorizontal) {- result[lastIndex].boundingBox.setWidth(0);+ result[lastIndex].inkBoundingBox.setWidth(0);} else {- result[lastIndex].boundingBox.setHeight(0);+ result[lastIndex].inkBoundingBox.setHeight(0);}} else if (result.at(lastIndex).lineEnd == LineEdgeBehaviour::ConditionallyHang) {if (ltr) {diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex 4252d5790d..c071feabd1 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -100,7 +100,8 @@ struct CharacterResult {Glyph::Variant glyph;- QRectF boundingBox;+ QRectF inkBoundingBox; ///< The bounds of the drawn glyph. Different from the bounds the charresult takes up in the layout, @see layoutBox();+ bool isHorizontal = true; ///< Whether the current glyph lays out horizontal or vertical. Currently same as paragraph, but in future may change.int visualIndex = -1;int plaintTextIndex = -1;QPointF cssPosition = QPointF(); ///< the position in accordance with the CSS specs, as opossed to the SVG spec.@@ -121,7 +122,25 @@ struct CharacterResult {qreal scaledHalfLeading{}; ///< Leading for both sides, can be either negative or positive, in ptqreal scaledAscent{}; ///< Ascender, in ptqreal scaledDescent{}; ///< Descender, in pt- QRectF lineHeightBox; ///< The box representing the line height of this char++ /**+ * @brief layoutBox+ * @return a dynamically calculated layoutBox, this is different from the Ink bounding box.+ */+ QRectF layoutBox() const {+ return isHorizontal? QRectF(0, advance.y()+scaledAscent, advance.x(), scaledDescent-scaledAscent)+ : QRectF(advance.x()-scaledDescent, 0, scaledDescent-scaledAscent, advance.y());+ }+ /**+ * @brief lineHeightBox+ * @return The box representing the line height of this char.+ */+ QRectF lineHeightBox () const {+ QRectF lBox = layoutBox();+ return isHorizontal? lBox.adjusted(0, -scaledHalfLeading, 0, scaledHalfLeading)+ : lBox.adjusted(-scaledHalfLeading, 0, scaledHalfLeading, 0);+ }+QFont::Style fontStyle = QFont::StyleNormal;int fontWeight = 400;diff --git a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp b/libs/flake/text/KoSvgTextShape_p_glyphs.cppindex abc35fccb3..787796d74b 100644--- a/libs/flake/text/KoSvgTextShape_p_glyphs.cpp+++ b/libs/flake/text/KoSvgTextShape_p_glyphs.cpp@@ -626,23 +626,19 @@ bool KoSvgTextShape::Private::loadGlyph(const QTransform &ftTF,ftTF.inverted().map(charResult.advance).y());bbox = glyphObliqueTf.mapRect(bbox);}- charResult.boundingBox = ftTF.mapRect(bbox);+ charResult.isHorizontal = isHorizontal;+ QRectF scaledBBox = ftTF.mapRect(bbox);charResult.scaledHalfLeading = ftTF.map(QPointF(charResult.fontHalfLeading, charResult.fontHalfLeading)).x();- charResult.scaledAscent = isHorizontal? charResult.boundingBox.top(): charResult.boundingBox.right();- charResult.scaledDescent = isHorizontal? charResult.boundingBox.bottom(): charResult.boundingBox.left();- if (isHorizontal) {- charResult.lineHeightBox = charResult.boundingBox.adjusted(0, -charResult.scaledHalfLeading, 0, charResult.scaledHalfLeading);- } else {- charResult.lineHeightBox = charResult.boundingBox.adjusted(-charResult.scaledHalfLeading, 0, charResult.scaledHalfLeading, 0);- }+ charResult.scaledAscent = isHorizontal? scaledBBox.top(): scaledBBox.right();+ charResult.scaledDescent = isHorizontal? scaledBBox.bottom(): scaledBBox.left();if (bitmapGlyph) {- charResult.boundingBox |= bitmapGlyph->drawRect;+ charResult.inkBoundingBox |= bitmapGlyph->drawRect;} else if (const auto *outlineGlyph = std::get_if<Glyph::Outline>(&charResult.glyph)) {- charResult.boundingBox |= outlineGlyph->path.boundingRect();+ charResult.inkBoundingBox |= outlineGlyph->path.boundingRect();} else if (const auto *colorGlyph = std::get_if<Glyph::ColorLayers>(&charResult.glyph)) {Q_FOREACH (const QPainterPath &p, colorGlyph->paths) {- charResult.boundingBox |= p.boundingRect();+ charResult.inkBoundingBox |= p.boundingRect();}} else if (!std::holds_alternative<std::monostate>(charResult.glyph)) {warnFlake << "Unhandled glyph type" << charResult.glyph.index();diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex b7d1fd8f17..64c975a664 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -602,14 +602,13 @@ void KoSvgTextShape::Private::relayout()dummy.scaledHalfLeading = hardbreak.scaledHalfLeading;dummy.cssPosition = hardbreak.cssPosition + hardbreak.advance;dummy.finalPosition = dummy.cssPosition;- dummy.boundingBox = hardbreak.boundingBox;- dummy.lineHeightBox = hardbreak.lineHeightBox;+ dummy.inkBoundingBox = hardbreak.inkBoundingBox;if (isHorizontal) {- dummy.boundingBox.setWidth(0);- dummy.lineHeightBox.setWidth(0);+ dummy.advance.setX(0);+ dummy.inkBoundingBox.setWidth(0);} else {- dummy.boundingBox.setHeight(0);- dummy.lineHeightBox.setHeight(0);+ dummy.advance.setY(0);+ dummy.inkBoundingBox.setHeight(0);}dummy.plaintTextIndex = hardbreak.cursorInfo.graphemeIndices.last();dummy.cursorInfo.caret = hardbreak.cursorInfo.caret;@@ -794,7 +793,7 @@ void KoSvgTextShape::Private::relayout()if (!result.at(i).hidden) {const QTransform tf = result.at(i).finalTransform();- chunk->associatedLeaf->associatedOutline.addRect(tf.mapRect(result.at(i).boundingBox));+ chunk->associatedLeaf->associatedOutline.addRect(tf.mapRect(result.at(i).inkBoundingBox));}}}@@ -812,7 +811,7 @@ void KoSvgTextShape::Private::relayout()pos.synthetic = true;cursorPos.append(pos);if (!textChunks.isEmpty()) {- textChunks.last().associatedLeaf->associatedOutline.addRect(result.at(dummyIndex).finalTransform().mapRect(result[dummyIndex].boundingBox));+ textChunks.last().associatedLeaf->associatedOutline.addRect(result.at(dummyIndex).finalTransform().mapRect(result[dummyIndex].inkBoundingBox));}}}@@ -983,7 +982,7 @@ void KoSvgTextShape::Private::applyTextLength(KisForest<KoSvgTextContentElement>outlineGlyph->path = tf.map(outlineGlyph->path);}cr.advance = tf.map(cr.advance);- cr.boundingBox = tf.mapRect(cr.boundingBox);+ cr.inkBoundingBox = tf.mapRect(cr.inkBoundingBox);}bool last = spacingAndGlyphs ? false : k == visualToLogical.keys().last();@@ -1384,7 +1383,7 @@ KoSvgTextShape::Private::generateDecorationPaths(KisForest<KoSvgTextContentElemecurrentFinalPos = charResult.finalPosition;- const QRectF bbox = charResult.boundingBox;+ const QRectF bbox = charResult.layoutBox();top = isHorizontal ? qMin(top, bbox.top()) : qMax(top, bbox.right());bottom = isHorizontal ? qMax(bottom, bbox.bottom()) : qMin(bottom, bbox.left());diff --git a/libs/flake/text/KoSvgTextShape_p_output.cpp b/libs/flake/text/KoSvgTextShape_p_output.cppindex 5370658a75..4c92f38147 100644--- a/libs/flake/text/KoSvgTextShape_p_output.cpp+++ b/libs/flake/text/KoSvgTextShape_p_output.cpp@@ -136,7 +136,7 @@ void KoSvgTextShape::Private::paintPaths(QPainter &painter,* Make sure the character touches the painter's clip rect,* otherwise we can just skip it*/- const QRectF boundingRect = tf.mapRect(result.at(i).boundingBox);+ const QRectF boundingRect = tf.mapRect(result.at(i).inkBoundingBox);const QRectF clipRect = painter.clipBoundingRect();if (boundingRect.isEmpty() ||(!clipRect.contains(boundingRect) &&@@ -524,19 +524,19 @@ void KoSvgTextShape::Private::paintDebug(QPainter &painter,penColor.setAlpha(192);pen.setColor(penColor);painter.setPen(pen);- painter.drawPolygon(tf.map(result.at(i).boundingBox));+ painter.drawPolygon(tf.map(result.at(i).inkBoundingBox));penColor.setAlpha(96);pen.setColor(penColor);pen.setWidth(1);pen.setStyle(Qt::DotLine);painter.setPen(pen);- painter.drawPolygon(tf.map(result.at(i).lineHeightBox));+ painter.drawPolygon(tf.map(result.at(i).lineHeightBox()));pen.setWidth(2);pen.setStyle(Qt::SolidLine);- const QPointF center = tf.mapRect(result.at(i).boundingBox).center();+ const QPointF center = tf.mapRect(result.at(i).layoutBox()).center();QString text = "#";text += QString::number(i);{