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++
Implement loading/saving for text contours.
Wolthera van Hövell tot Westerflier263539785680
diff --git a/libs/flake/svg/SvgParser.cpp b/libs/flake/svg/SvgParser.cppindex f76943b69b..ad3a0b778d 100644--- a/libs/flake/svg/SvgParser.cpp+++ b/libs/flake/svg/SvgParser.cpp@@ -1759,6 +1759,18 @@ KoShape* SvgParser::parseGroup(const QDomElement &b, const QDomElement &override// handle idapplyId(b.attribute("id"), group);+ if (b.hasAttribute(KoSvgTextShape_TEXTCONTOURGROUP)) {+ Q_FOREACH(KoShape *shape, childShapes) {+ if (shape->shapeId() == KoSvgTextShape_SHAPEID) {+ shape->setTransformation(group->transformation());++ if (createContext) {+ m_context.popGraphicsContext();+ }+ return shape;+ }+ }+ }addToGroup(childShapes, group);applyCurrentStyle(group, extraOffset); // apply style to this group after size is set@@ -1795,9 +1807,10 @@ KoShape* SvgParser::getTextPath(const QDomElement &e) {if (!pathId.isNull()) {KoShape *s = m_context.shapeById(pathId);if (s) {- const QTransform absTf = s->absoluteTransformation();KoShape *cloned = s->cloneShape();+ const QTransform absTf = s->absoluteTransformation();cloned->setTransformation(absTf * m_shapeParentTransform.value(s).inverted());+ if(cloned && s->parent()) cloned->setVisible(false);return cloned;}}@@ -2273,7 +2286,9 @@ 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);+ // When we have a parent, the shape is inside the defs, but when not,+ // it's in the group we're in the currently parsing.+ if (cloned && s->parent()) cloned->setVisible(false);return cloned;}} else if (value.startsWith("circle(")) {diff --git a/libs/flake/tests/TestSvgText.cpp b/libs/flake/tests/TestSvgText.cppindex 2b928fcf79..d582785076 100644--- a/libs/flake/tests/TestSvgText.cpp+++ b/libs/flake/tests/TestSvgText.cpp@@ -2998,7 +2998,7 @@ void TestSvgText::testTextSplit()// test split text pathforest.erase(forest.childBegin(), forest.childEnd());KoSvgTextContentElement textPath = el;- textPath.textPath.reset(new KoPathShape());+ textPath.textPathId = "textPath";auto root = forest.insert(forest.childEnd(), KoSvgTextContentElement());forest.insert(childEnd(root), textPath);KoSvgTextShape::Private::splitContentElement(forest, 10);diff --git a/libs/flake/text/KoSvgTextContentElement.cpp b/libs/flake/text/KoSvgTextContentElement.cppindex 2599a89aba..81c7220415 100644--- a/libs/flake/text/KoSvgTextContentElement.cpp+++ b/libs/flake/text/KoSvgTextContentElement.cpp@@ -268,13 +268,14 @@ bool KoSvgTextContentElement::loadSvgTextNode(const QDomText &text, SvgLoadingCobool KoSvgTextContentElement::saveSvg(SvgSavingContext &context,bool rootText,bool saveText,- QMap<QString, QString> shapeSpecificAttributes)+ QMap<QString, QString> shapeSpecificAttributes,+ KoShape *textPath){if (textPath) {if (textPath) {// we'll always save as an embedded shape as "path" is an svg 2.0// feature.- QString id = SvgStyleWriter::embedShape(textPath.data(), context);+ QString id = textPath->isVisible(false)? context.getID(textPath): SvgStyleWriter::embedShape(textPath, context);// inkscape can only read 'xlink:href'if (!id.isEmpty()) {context.shapeWriter().addAttribute("xlink:href", "#" + id);diff --git a/libs/flake/text/KoSvgTextContentElement.h b/libs/flake/text/KoSvgTextContentElement.hindex bdcc3d18c4..6fe1f8bfae 100644--- a/libs/flake/text/KoSvgTextContentElement.h+++ b/libs/flake/text/KoSvgTextContentElement.h@@ -32,6 +32,7 @@ public:: properties(rhs.properties), localTransformations(rhs.localTransformations), textPathInfo(rhs.textPathInfo)+ , textPathId(rhs.textPathId), textLength(rhs.textLength), lengthAdjust(rhs.lengthAdjust), textDecorations(rhs.textDecorations)@@ -57,6 +58,7 @@ public:/// The textpath, if any. Defaults to null.QScopedPointer<KoShape> textPath{nullptr};+ QString textPathId;/// the value 'textLength' attribute of the associated dom elementKoSvgText::AutoValue textLength;@@ -92,7 +94,7 @@ public:bool loadSvgTextNode(const QDomText &text, SvgLoadingContext &context);- bool saveSvg(SvgSavingContext &context, bool rootText, bool saveText, QMap<QString, QString> shapeSpecificAttributes);+ bool saveSvg(SvgSavingContext &context, bool rootText, bool saveText, QMap<QString, QString> shapeSpecificAttributes, KoShape *textPath = nullptr);/*** The number of characters contained in the currentChunk.diff --git a/libs/flake/text/KoSvgTextLoader.cpp b/libs/flake/text/KoSvgTextLoader.cppindex 8451283efe..2c31219c94 100644--- a/libs/flake/text/KoSvgTextLoader.cpp+++ b/libs/flake/text/KoSvgTextLoader.cpp@@ -31,6 +31,7 @@ KoSvgTextLoader::KoSvgTextLoader(KoSvgTextShape *shape)KoSvgTextLoader::~KoSvgTextLoader(){// run clean-up after parsing to remove empty spans and the like.+ d->shape->d->updateInternalShapesList();d->shape->cleanUp();d->shape->d->isLoading = false;}@@ -115,8 +116,9 @@ void KoSvgTextLoader::setStyleInfo(KoShape *s)void KoSvgTextLoader::setTextPathOnCurrentNode(KoShape *s){- if (!KisForestDetail::isEnd(d->currentNode)) {- d->currentNode->textPath.reset(s);+ if (!KisForestDetail::isEnd(d->currentNode) && s) {+ d->currentNode->textPathId = s->name();+ d->shape->d->textPaths.append(s);}}diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 1732e4e335..3c83510815 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1546,9 +1546,26 @@ void KoSvgTextShape::setCharacterTransformsFromLayout()}#include "KoXmlWriter.h"+#include "SvgWriter.h"bool KoSvgTextShape::saveSvg(SvgSavingContext &context){bool success = false;++ QList<KoShape*> visibleShapes;+ Q_FOREACH(KoShape *shape, d->internalShapes()) {+ if (shape->isVisible(false)) {+ visibleShapes.append(shape);+ }+ }+ if (!visibleShapes.isEmpty()) {+ context.shapeWriter().startElement("g", false);+ context.shapeWriter().addAttribute("id", context.createUID("group"));+ context.shapeWriter().addAttribute(KoSvgTextShape_TEXTCONTOURGROUP, "true");++ SvgUtil::writeTransformAttributeLazy("transform", transformation(), context.shapeWriter());+ SvgWriter writer(visibleShapes);+ writer.saveDetached(context);+ }for (auto it = d->textData.compositionBegin(); it != d->textData.compositionEnd(); it++) {if (it.state() == KisForestDetail::Enter) {bool isTextPath = false;@@ -1567,7 +1584,9 @@ bool KoSvgTextShape::saveSvg(SvgSavingContext &context)// 3: Wrong font-size-adjust.context.shapeWriter().addAttribute("krita:textVersion", 3);- SvgUtil::writeTransformAttributeLazy("transform", transformation(), context.shapeWriter());+ if (visibleShapes.isEmpty()) {+ SvgUtil::writeTransformAttributeLazy("transform", transformation(), context.shapeWriter());+ }SvgStyleWriter::saveSvgStyle(this, context);} else {SvgStyleWriter::saveSvgFill(this->background(), false, this->outlineRect(), this->size(), this->absoluteTransformation(), context);@@ -1591,10 +1610,12 @@ bool KoSvgTextShape::saveSvg(SvgSavingContext &context)}+ KoShape *textPath = d->textPathByName(it->textPathId);success = it->saveSvg(context,it == d->textData.compositionBegin(),d->childCount(siblingCurrent(it)) == 0,- shapeSpecificStyles);+ shapeSpecificStyles,+ textPath);} else {if (it == d->textData.compositionBegin()) {SvgStyleWriter::saveMetadata(this, context);@@ -1602,6 +1623,9 @@ bool KoSvgTextShape::saveSvg(SvgSavingContext &context)context.shapeWriter().endElement();}}+ if (!visibleShapes.isEmpty()) {+ context.shapeWriter().endElement();+ }return success;}@@ -2030,7 +2054,7 @@ QMap<QString, QString> KoSvgTextShape::shapeTypeSpecificStyles(SvgSavingContextif (!d->shapesInside.isEmpty()) {QStringList shapesInsideList;Q_FOREACH(KoShape* shape, d->shapesInside) {- QString id = SvgStyleWriter::embedShape(shape, context);+ QString id = shape->isVisible(false)? context.getID(shape): SvgStyleWriter::embedShape(shape, context);shapesInsideList.append(QString("url(#%1)").arg(id));}map.insert("shape-inside", shapesInsideList.join(" "));@@ -2038,7 +2062,7 @@ QMap<QString, QString> KoSvgTextShape::shapeTypeSpecificStyles(SvgSavingContextif (!d->shapesSubtract.isEmpty()) {QStringList shapesInsideList;Q_FOREACH(KoShape* shape, d->shapesSubtract) {- QString id = SvgStyleWriter::embedShape(shape, context);+ QString id = shape->isVisible(false)? context.getID(shape): SvgStyleWriter::embedShape(shape, context);shapesInsideList.append(QString("url(#%1)").arg(id));}map.insert("shape-subtract", shapesInsideList.join(" "));diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex 5d591218ca..bb69e0d392 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -23,6 +23,7 @@ struct KoSvgTextCharacterInfo;typedef QSharedPointer<KoSvgTextShapeMemento> KoSvgTextShapeMementoSP;#define KoSvgTextShape_SHAPEID "KoSvgTextShapeID"+#define KoSvgTextShape_TEXTCONTOURGROUP "krita:textContourGroup"/*** KoSvgTextShape is a root chunk of the \<text\> element subtree.*/diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex 56f72e62fa..513da70940 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -512,6 +512,12 @@ public:QList<KoShape*> shapesSubtract;QList<KoShape*> textPaths;++ KoShape *textPathByName(QString name) {+ auto it = std::find_if(textPaths.begin(), textPaths.end(), [&name](const KoShape *s) -> bool {return s->name() == name;});+ return it != textPaths.end()? *it: nullptr;+ }+KisForest<KoSvgTextContentElement> textData;bool isLoading = false; ///< Turned on when loading in text data, blocks updates to shape listeners.@@ -561,7 +567,7 @@ public:bool isHorizontal,qreal offset,bool isClosed);- static void applyTextPath(KisForest<KoSvgTextContentElement>::child_iterator parent, QVector<CharacterResult> &result, bool isHorizontal, QPointF &startPos, const KoSvgTextProperties resolvedProps);+ static void applyTextPath(KisForest<KoSvgTextContentElement>::child_iterator parent, QVector<CharacterResult> &result, bool isHorizontal, QPointF &startPos, const KoSvgTextProperties resolvedProps, Private *d);static void computeFontMetrics(KisForest<KoSvgTextContentElement>::child_iterator parent, const KoSvgTextProperties &parentProps,const KoSvgText::FontMetrics &parentBaselineTable, const KoSvgText::Baseline parentBaseline,const QPointF superScript,@@ -586,7 +592,7 @@ public:bool isHorizontal,bool ltr,bool wrapping,- const KoSvgTextProperties resolvedProps);+ const KoSvgTextProperties resolvedProps, Private *d);QMap<KoSvgText::TextDecoration, QPainterPath> generateDecorationPaths(const int &start, const int &end,const KoSvgText::ResolutionHandler resHandler,const QVector<CharacterResult> &result,diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex 8e87a036b0..0cd205f38d 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -739,12 +739,12 @@ void KoSvgTextShape::Private::relayout()globalIndex,isHorizontal,direction == KoSvgText::DirectionLeftToRight,- false, KoSvgTextProperties::defaultProperties());+ false, KoSvgTextProperties::defaultProperties(), this);// 8. Position on pathdebugFlake << "8. Position on path";- this->applyTextPath(textData.childBegin(), result, isHorizontal, startPos, KoSvgTextProperties::defaultProperties());+ this->applyTextPath(textData.childBegin(), result, isHorizontal, startPos, KoSvgTextProperties::defaultProperties(), this);} else {globalIndex = 0;debugFlake << "Computing text-decorationsfor inline-size";@@ -758,7 +758,7 @@ void KoSvgTextShape::Private::relayout()globalIndex,isHorizontal,direction == KoSvgText::DirectionLeftToRight,- true, KoSvgTextProperties::defaultProperties());+ true, KoSvgTextProperties::defaultProperties(), this);}// 9. return result.@@ -866,7 +866,7 @@ void KoSvgTextShape::Private::resolveTransforms(KisForest<KoSvgTextContentElemenint index = currentIndex;int j = index + numChars(currentTextElement, withControls, resolvedProps);- if (currentTextElement->textPath) {+ if (!currentTextElement->textPathId.isEmpty()) {textInPath = true;} else {int i = 0;@@ -911,7 +911,7 @@ void KoSvgTextShape::Private::resolveTransforms(KisForest<KoSvgTextContentElemen}- if (currentTextElement->textPath) {+ if (!currentTextElement->textPathId.isEmpty()) {bool first = true;for (int k = index; k < j; k++ ) {@@ -1251,7 +1251,8 @@ void KoSvgTextShape::Private::computeTextDecorations(// NOLINT(readability-functbool isHorizontal,bool ltr,bool wrapping,- const KoSvgTextProperties resolvedProps)+ const KoSvgTextProperties resolvedProps,+ Private *d){const int i = currentIndex;@@ -1262,9 +1263,10 @@ void KoSvgTextShape::Private::computeTextDecorations(// NOLINT(readability-functqreal currentTextPathOffset = textPathoffset;bool textPathSide = side;if (!wrapping) {- currentTextPath = textPath ? textPath : dynamic_cast<KoPathShape *>(currentTextElement->textPath.data());+ KoShape *cTextPath = d->textPathByName(currentTextElement->textPathId);+ currentTextPath = textPath ? textPath : dynamic_cast<KoPathShape *>(cTextPath);- if (currentTextElement->textPath) {+ if (cTextPath) {textPathSide = currentTextElement->textPathInfo.side == TextPathSideRight;if (currentTextElement->textPathInfo.startOffsetIsPercentage) {KIS_ASSERT(currentTextPath);@@ -1293,7 +1295,7 @@ void KoSvgTextShape::Private::computeTextDecorations(// NOLINT(readability-functisHorizontal,ltr,wrapping,- properties+ properties, d);}@@ -1824,7 +1826,7 @@ void KoSvgTextShape::Private::applyTextPath(KisForest<KoSvgTextContentElement>::QVector<CharacterResult> &result,bool isHorizontal,QPointF &startPos,- const KoSvgTextProperties resolvedProps)+ const KoSvgTextProperties resolvedProps, Private *d){// Unlike all the other applying functions, this one only iterates over the// top-level. SVG is not designed to have nested textPaths. Source:@@ -1836,7 +1838,8 @@ void KoSvgTextShape::Private::applyTextPath(KisForest<KoSvgTextContentElement>::for (auto textShapeElement = KisForestDetail::childBegin(root); textShapeElement != KisForestDetail::childEnd(root); textShapeElement++) {int endIndex = currentIndex + numChars(textShapeElement, true, resolvedProps);- KoPathShape *shape = dynamic_cast<KoPathShape *>(textShapeElement->textPath.data());+ KoShape *cTextPath = d->textPathByName(textShapeElement->textPathId);+ KoPathShape *shape = dynamic_cast<KoPathShape *>(cTextPath);if (shape) {QPainterPath path = shape->outline();path = shape->transformation().map(path);@@ -1927,7 +1930,8 @@ void KoSvgTextShape::Private::applyTextPath(KisForest<KoSvgTextContentElement>::QVector<SubChunk> KoSvgTextShape::Private::collectSubChunks(KisForest<KoSvgTextContentElement>::child_iterator it, KoSvgTextProperties parentProps, bool textInPath, bool &firstTextInPath){QVector<SubChunk> result;- if (it->textPath) {++ if (!it->textPathId.isEmpty()) {textInPath = true;firstTextInPath = true;}@@ -1974,7 +1978,7 @@ QVector<SubChunk> KoSvgTextShape::Private::collectSubChunks(KisForest<KoSvgTextCfirstTextInPath = false;}- if (it->textPath) {+ if (!it->textPathId.isEmpty()) {textInPath = false;firstTextInPath = false;}