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++
Rework textContours so there's a single internal shape list that can be painted.
Wolthera van Hövell tot Westerflierc999d130c38a
diff --git a/libs/flake/svg/SvgParser.cpp b/libs/flake/svg/SvgParser.cppindex ae920e7d24..f76943b69b 100644--- a/libs/flake/svg/SvgParser.cpp+++ b/libs/flake/svg/SvgParser.cpp@@ -2273,6 +2273,7 @@ KoShape *SvgParser::createShapeFromCSS(const QDomElement e, const QString value,const QTransform absTf = s->absoluteTransformation();KoShape *cloned = s->cloneShape();cloned->setTransformation(absTf * m_shapeParentTransform.value(s).inverted());+ if (cloned) cloned->setVisible(false);return cloned;}} else if (value.startsWith("circle(")) {@@ -2315,7 +2316,9 @@ KoShape *SvgParser::createShapeFromCSS(const QDomElement e, const QString value,}el.setAttribute("fill-rule", fillRule);- return createShapeFromElement(el, context);+ KoShape *shape = createShapeFromElement(el, context);+ if (shape) shape->setVisible(false);+ return shape;}QList<KoShape *> SvgParser::createListOfShapesFromCSS(const QDomElement e, const QString value, SvgLoadingContext &context)diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 2459427446..67c8d6ce5b 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -163,7 +163,7 @@ void KoSvgTextShape::shapeChanged(ChangeType type, KoShape *shape)}KoShape::shapeChanged(type, shape);- if ((d->shapesInside.contains(shape) || d->shapesSubtract.contains(shape))+ if ((d->internalShapes.contains(shape))&& (type == PositionChanged|| type == RotationChanged|| type == ScaleChanged@@ -1015,7 +1015,7 @@ void KoSvgTextShape::mergePropertiesIntoRange(const int startPos,bool changed = false;int currentIndex = 0;KoSvgText::AutoValue inlineSize = d->textData.childBegin()->properties.propertyOrDefault(KoSvgTextProperties::InlineSizeId).value<KoSvgText::AutoValue>();- bool isWrapping = !d->shapesInside.isEmpty() || !inlineSize.isAuto;+ bool isWrapping = !d->shapesInside().isEmpty() || !inlineSize.isAuto;for (auto it = d->textData.depthFirstTailBegin(); it != d->textData.depthFirstTailEnd(); it++) {if (KoSvgTextShape::Private::childCount(siblingCurrent(it)) > 0) {continue;@@ -1784,6 +1784,23 @@ bool KoSvgTextShape::fontMatchingDisabled() constvoid KoSvgTextShape::paint(QPainter &painter) const{painter.save();+ if (!d->internalShapes.isEmpty()) {+ Q_FOREACH (KoShape* shape, d->internalShapes.keys()) {+ if (!shape->isVisible(false)) continue;+ painter.save();+ painter.setTransform(shape->transformation() * painter.transform());+ Q_FOREACH(const KoShape::PaintOrder p, shape->paintOrder()) {+ if (p == KoShape::Fill) {+ shape->paint(painter);+ } else if (p == KoShape::Stroke) {+ shape->paintStroke(painter);+ } else if (p == KoShape::Markers) {+ shape->paintMarkers(painter);+ }+ }+ painter.restore();+ }+ }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 QPainter@@ -1816,9 +1833,9 @@ void KoSvgTextShape::paintStroke(QPainter &painter) constQPainterPath KoSvgTextShape::outline() const {QPainterPath result;- if (!d->shapesInside.isEmpty()) {+ if (!d->internalShapes.isEmpty()) {//TODO: what about shape subtract?- Q_FOREACH(KoShape *shape, d->shapesInside) {+ Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {result.addPath(shape->transformation().map(shape->outline()));}} else {@@ -1840,31 +1857,36 @@ QRectF KoSvgTextShape::outlineRect() constQRectF KoSvgTextShape::boundingRect() const{QRectF result;- QList<KoShapeStrokeModelSP> parentStrokes;- for (auto it = d->textData.compositionBegin(); it != d->textData.compositionEnd(); it++) {- if (it.state() == KisForestDetail::Enter) {- if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {- parentStrokes.append(it->properties.property(KoSvgTextProperties::StrokeId).value<KoSvgText::StrokeProperty>().property);- }- } else {- KoShapeStrokeModelSP stroke = parentStrokes.size() > 0? parentStrokes.last(): nullptr;- 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 (!bb.isEmpty()) {- if (stroke) {- KoInsets insets;- stroke->strokeInsets(this, insets);- result |= bb.adjusted(-insets.left, -insets.top, insets.right, insets.bottom);- } else {- result |= bb;+ if (!d->internalShapes.isEmpty()) {+ //TODO: what about invisible shapes?+ result = KoShape::boundingRect(d->internalShapes.keys());+ } else {+ QList<KoShapeStrokeModelSP> parentStrokes;+ for (auto it = d->textData.compositionBegin(); it != d->textData.compositionEnd(); it++) {+ if (it.state() == KisForestDetail::Enter) {+ if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {+ parentStrokes.append(it->properties.property(KoSvgTextProperties::StrokeId).value<KoSvgText::StrokeProperty>().property);+ }+ } else {+ KoShapeStrokeModelSP stroke = parentStrokes.size() > 0? parentStrokes.last(): nullptr;+ 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 (!bb.isEmpty()) {+ if (stroke) {+ KoInsets insets;+ stroke->strokeInsets(this, insets);+ result |= bb.adjusted(-insets.left, -insets.top, insets.right, insets.bottom);+ } else {+ result |= bb;+ }+ }+ if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {+ // reset stroke to use parent stroke.+ parentStrokes.pop_back();}- }- if (it->properties.hasProperty(KoSvgTextProperties::StrokeId)) {- // reset stroke to use parent stroke.- parentStrokes.pop_back();}}}@@ -1886,11 +1908,8 @@ void KoSvgTextShape::setSize(const QSizeF &size)const qreal scaleX = size.width() / oldSize.width();const qreal scaleY = size.height() / oldSize.height();- if (!d->shapesInside.isEmpty()) {- Q_FOREACH(KoShape *shape, d->shapesInside) {- KoFlake::resizeShapeCommon(shape, scaleX, scaleY, shape->boundingRect().topLeft() - oRect.topLeft(), false, false, this->transformation());- }- Q_FOREACH(KoShape *shape, d->shapesSubtract) {+ if (!d->internalShapes.isEmpty()) {+ Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {KoFlake::resizeShapeCommon(shape, scaleX, scaleY, shape->boundingRect().topLeft() - oRect.topLeft(), false, false, this->transformation());}} else {@@ -1900,7 +1919,6 @@ void KoSvgTextShape::setSize(const QSizeF &size)notifyChanged();shapeChangedPriv(ScaleChanged);}-}void KoSvgTextShape::paintDebug(QPainter &painter, const DebugElements elements) const@@ -1914,12 +1932,12 @@ void KoSvgTextShape::paintDebug(QPainter &painter, const DebugElements elements)}//Debug shape outlines.- Q_FOREACH (KoShape *shapeInside, d->shapesInside) {+ Q_FOREACH (KoShape *shapeInside, d->shapesInside()) {QPainterPath p = shapeInside->outline();p = shapeInside->transformation().map(p);painter.strokePath(p, QPen(Qt::green));}- Q_FOREACH (KoShape *shapeInside, d->shapesSubtract) {+ Q_FOREACH (KoShape *shapeInside, d->shapesSubtract()) {QPainterPath p = shapeInside->outline();p = shapeInside->transformation().map(p);painter.strokePath(p, QPen(Qt::red));@@ -1991,11 +2009,14 @@ KoSvgTextShape::TextType KoSvgTextShape::textType() constvoid KoSvgTextShape::setShapesInside(QList<KoShape *> shapesInside){- Q_FOREACH(KoShape *shape, d->shapesInside) {- shape->removeDependee(this);+ Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {+ if (d->internalShapes.value(shape) == KoSvgTextShape::Private::InternalShapeState::ShapeInside) {+ shape->removeDependee(this);+ d->internalShapes.remove(shape);+ }}- d->shapesInside = shapesInside;- Q_FOREACH(KoShape *shape, d->shapesInside) {+ Q_FOREACH(KoShape *shape, shapesInside) {+ d->internalShapes.insert(shape, KoSvgTextShape::Private::InternalShapeState::ShapeInside);shape->addDependee(this);}shapeChangedPriv(ContentChanged);@@ -2003,16 +2024,19 @@ void KoSvgTextShape::setShapesInside(QList<KoShape *> shapesInside)QList<KoShape *> KoSvgTextShape::shapesInside() const{- return d->shapesInside;+ return d->shapesInside();}void KoSvgTextShape::setShapesSubtract(QList<KoShape *> shapesSubtract){- Q_FOREACH(KoShape *shape, d->shapesSubtract) {- shape->removeDependee(this);+ Q_FOREACH(KoShape *shape, d->internalShapes.keys()) {+ if (d->internalShapes.value(shape) == KoSvgTextShape::Private::InternalShapeState::ShapeSubtract) {+ shape->removeDependee(this);+ d->internalShapes.remove(shape);+ }}- d->shapesSubtract = shapesSubtract;- Q_FOREACH(KoShape *shape, d->shapesSubtract) {+ Q_FOREACH(KoShape *shape, shapesSubtract) {+ d->internalShapes.insert(shape, KoSvgTextShape::Private::InternalShapeState::ShapeSubtract);shape->addDependee(this);}shapeChangedPriv(ContentChanged);@@ -2020,23 +2044,23 @@ void KoSvgTextShape::setShapesSubtract(QList<KoShape *> shapesSubtract)QList<KoShape *> KoSvgTextShape::shapesSubtract() const{- return d->shapesSubtract;+ return d->shapesSubtract();}QMap<QString, QString> KoSvgTextShape::shapeTypeSpecificStyles(SvgSavingContext &context) const{QMap<QString, QString> map = this->textProperties().convertParagraphProperties();- if (!d->shapesInside.isEmpty()) {+ if (!d->shapesInside().isEmpty()) {QStringList shapesInsideList;- Q_FOREACH(KoShape* shape, d->shapesInside) {+ Q_FOREACH(KoShape* shape, d->shapesInside()) {QString id = SvgStyleWriter::embedShape(shape, context);shapesInsideList.append(QString("url(#%1)").arg(id));}map.insert("shape-inside", shapesInsideList.join(" "));}- if (!d->shapesSubtract.isEmpty()) {+ if (!d->shapesSubtract().isEmpty()) {QStringList shapesInsideList;- Q_FOREACH(KoShape* shape, d->shapesSubtract) {+ Q_FOREACH(KoShape* shape, d->shapesSubtract()) {QString id = SvgStyleWriter::embedShape(shape, context);shapesInsideList.append(QString("url(#%1)").arg(id));}diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex e351f155b5..1e1b84ce04 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -440,19 +440,21 @@ public:Private() = default;+ enum InternalShapeState {+ Decorative,+ ShapeInside,+ ShapeSubtract,+ TextPath+ };+Private(const Private &rhs): textData(rhs.textData) {- Q_FOREACH (KoShape *shape, rhs.shapesInside) {- KoShape *clonedShape = shape->cloneShape();- KIS_ASSERT_RECOVER(clonedShape) { continue; }- shapesInside.append(clonedShape);- }- Q_FOREACH (KoShape *shape, rhs.shapesSubtract) {+ Q_FOREACH (KoShape *shape, rhs.internalShapes.keys()) {KoShape *clonedShape = shape->cloneShape();KIS_ASSERT_RECOVER(clonedShape) { continue; }- shapesSubtract.append(clonedShape);+ internalShapes.insert(clonedShape, rhs.internalShapes.value(shape));}yRes = rhs.yRes;xRes = rhs.xRes;@@ -470,14 +472,34 @@ public:}~Private() {- qDeleteAll(shapesInside);- qDeleteAll(shapesSubtract);+ QList<KoShape*> internalShapeList = internalShapes.keys();+ internalShapes.clear();+ qDeleteAll(internalShapeList);}int xRes = 72;int yRes = 72;- QList<KoShape*> shapesInside;- QList<KoShape*> shapesSubtract;++ QMap<KoShape*, InternalShapeState> internalShapes;++ QList<KoShape*> shapesInside() {+ QList<KoShape*> shapes;+ Q_FOREACH(KoShape *shape, internalShapes.keys()) {+ if (internalShapes.value(shape) == InternalShapeState::ShapeInside) {+ shapes.append(shape);+ }+ }+ return shapes;+ }+ QList<KoShape*> shapesSubtract() {+ QList<KoShape*> shapes;+ Q_FOREACH(KoShape *shape, internalShapes.keys()) {+ if (internalShapes.value(shape) == InternalShapeState::ShapeSubtract) {+ shapes.append(shape);+ }+ }+ return shapes;+ }KisForest<KoSvgTextContentElement> textData;bool isLoading = false; ///< Turned on when loading in text data, blocks updates to shape listeners.diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex 8e87a036b0..1de8c65c1e 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -241,7 +241,7 @@ void KoSvgTextShape::Private::relayout()// to consider it anything but required to have both shaping and bidi-reorder break.QVector<KoSvgText::CharTransformation> resolvedTransforms(text.size());globalIndex = 0;- bool wrapped = !(inlineSize.isAuto && this->shapesInside.isEmpty());+ bool wrapped = !(inlineSize.isAuto && this->shapesInside().isEmpty());if (!resolvedTransforms.isEmpty()) {resolvedTransforms[0].xPos = 0;resolvedTransforms[0].yPos = 0;@@ -644,8 +644,8 @@ void KoSvgTextShape::Private::relayout()// Handle linebreaking.QPointF startPos = resolvedTransforms.value(0).absolutePos() - result.value(0).dominantBaselineOffset;- if (!this->shapesInside.isEmpty()) {- QList<QPainterPath> shapes = getShapes(this->shapesInside, this->shapesSubtract, rootProperties);+ if (!this->shapesInside().isEmpty()) {+ QList<QPainterPath> shapes = getShapes(this->shapesInside(), this->shapesSubtract(), rootProperties);this->lineBoxes = flowTextInShapes(rootProperties, logicalToVisual, result, shapes, startPos, resHandler);} else {this->lineBoxes = breakLines(rootProperties, logicalToVisual, result, startPos, resHandler);@@ -655,7 +655,7 @@ void KoSvgTextShape::Private::relayout()globalIndex = 0;this->handleLineBoxAlignment(textData.childBegin(), result, this->lineBoxes, globalIndex, isHorizontal, KoSvgTextProperties::defaultProperties());- if (inlineSize.isAuto && this->shapesInside.isEmpty()) {+ if (inlineSize.isAuto && this->shapesInside().isEmpty()) {debugFlake << "Starting with SVG 1.1 specific portion";debugFlake << "4. Adjust positions: dx, dy";// 4. Adjust positions: dx, dydiff --git a/plugins/tools/svgtexttool/CMakeLists.txt b/plugins/tools/svgtexttool/CMakeLists.txtindex 6650debdce..892b017092 100644--- a/plugins/tools/svgtexttool/CMakeLists.txt+++ b/plugins/tools/svgtexttool/CMakeLists.txt@@ -29,6 +29,7 @@ set (SvgTextTool_SRCSSvgTextChangeTransformsOnRange.cppSvgTextTypeSettingStrategy.cppSvgTextRemoveTransformsFromRange.cpp+ SvgChangeTextContoursCommand.cppglyphpalette/GlyphPaletteDialog.cppglyphpalette/GlyphPaletteProxyModel.cppdiff --git a/plugins/tools/svgtexttool/SvgChangeTextContoursCommand.cpp b/plugins/tools/svgtexttool/SvgChangeTextContoursCommand.cppnew file mode 100644index 0000000000..05f42050c3--- /dev/null+++ b/plugins/tools/svgtexttool/SvgChangeTextContoursCommand.cpp@@ -0,0 +1,36 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-3.0-or-later+ */++#include <KoShape.h>+#include <KoSvgTextShape.h>++#include "SvgChangeTextContoursCommand.h"+++SvgChangeTextContoursCommand::SvgChangeTextContoursCommand(KoSvgTextShape *textShape, QList<KoShape*> shapes, KUndo2Command *parent)+ : KUndo2Command(parent)+ , m_textShape(textShape)+ , m_newShapes(shapes)+{+ setText(kundo2_i18n("Change Text Contours"));+ m_oldShapes = m_textShape->shapesInside();+}++void SvgChangeTextContoursCommand::redo()+{+ QRectF updateRect = m_textShape->boundingRect();+ m_textShape->setShapesInside(m_newShapes);+ updateRect |= m_textShape->boundingRect();+ m_textShape->updateAbsolute(updateRect);+}++void SvgChangeTextContoursCommand::undo()+{+ QRectF updateRect = m_textShape->boundingRect();+ m_textShape->setShapesInside(m_oldShapes);+ updateRect |= m_textShape->boundingRect();+ m_textShape->updateAbsolute(updateRect);+}diff --git a/plugins/tools/svgtexttool/SvgChangeTextContoursCommand.h b/plugins/tools/svgtexttool/SvgChangeTextContoursCommand.hnew file mode 100644index 0000000000..3eea833e59--- /dev/null+++ b/plugins/tools/svgtexttool/SvgChangeTextContoursCommand.h@@ -0,0 +1,29 @@+/*+ * SPDX-FileCopyrightText: 2025 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: GPL-3.0-or-later+ */+#ifndef SVGCHANGETEXTCONTOURSCOMMAND_H+#define SVGCHANGETEXTCONTOURSCOMMAND_H++#include <kundo2command.h>++class KoSvgTextShape;+class KoShape;++// TODO: This only supports shapeInside for now.++class SvgChangeTextContoursCommand : public KUndo2Command+{+public:+ SvgChangeTextContoursCommand(KoSvgTextShape *textShape, QList<KoShape*> shapes, KUndo2Command *parent = nullptr);++ void redo() override;+ void undo() override;+private:+ KoSvgTextShape *m_textShape;+ QList<KoShape*> m_oldShapes;+ QList<KoShape*> m_newShapes;+};++#endif // SVGCHANGETEXTCONTOURSCOMMAND_Hdiff --git a/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp b/plugins/tools/svgtexttool/SvgCreateTextStrategy.cppindex de9fd0780c..43946e4b03 100644--- a/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp+++ b/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp@@ -20,6 +20,8 @@#include "KoViewConverter.h"#include "KoSnapGuide.h"#include "commands/KoKeepShapesSelectedCommand.h"+#include "commands/KoShapeMoveCommand.h"+#include "SvgChangeTextContoursCommand.h"#include "kis_global.h"#include "kundo2command.h"@@ -134,11 +136,6 @@ KUndo2Command *SvgCreateTextStrategy::createCommand()KoSvgTextShape *textShape = dynamic_cast<KoSvgTextShape *>(factory->createShape( params, tool->canvas()->shapeController()->resourceManager()));- if (m_flowShape) {- textShape->setPosition(QPointF());- textShape->setShapesInside({m_flowShape});- }-KUndo2Command *parentCommand = new KUndo2Command();new KoKeepShapesSelectedCommand(tool->koSelection()->selectedShapes(), {}, tool->canvas()->selectedShapesProxy(), false, parentCommand);@@ -146,6 +143,19 @@ KUndo2Command *SvgCreateTextStrategy::createCommand()KUndo2Command *cmd = tool->canvas()->shapeController()->addShape(textShape, 0, parentCommand);parentCommand->setText(cmd->text());+ if (m_flowShape) {+ textShape->setPosition(m_flowShape->absolutePosition(KoFlake::TopLeft));+ QList<QPointF> pos {m_flowShape->absolutePosition(KoFlake::TopLeft)};+ QList<QPointF> newPos {QPointF()};+ QList<KoShape*> shapes{m_flowShape};+ new KoShapeMoveCommand(shapes, pos, newPos, KoFlake::TopLeft, parentCommand);+ new SvgChangeTextContoursCommand(textShape, shapes, parentCommand);+ if (m_flowShape->parent()) {+ // text is not a shape container, but we do need to remove the shape from the canvas properly.+ tool->canvas()->shapeController()->removeShape(m_flowShape, parentCommand);+ }+ }+new KoKeepShapesSelectedCommand({}, {textShape}, tool->canvas()->selectedShapesProxy(), true, parentCommand);tool->canvas()->snapGuide()->reset();diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cppindex 5483314e40..b97e2fefaa 100644--- a/plugins/tools/svgtexttool/SvgTextTool.cpp+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp@@ -791,7 +791,7 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)event->accept();} else if (hoveredFlowShape) {QPointF point = canvas()->snapGuide()->snap(event->point, event->modifiers());- m_interactionStrategy.reset(new SvgCreateTextStrategy(this, point, hoveredFlowShape->cloneShape()));+ m_interactionStrategy.reset(new SvgCreateTextStrategy(this, point, hoveredFlowShape));m_dragging = DragMode::Create;event->accept();} else if (crossLayerPossible) {