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++
Add more tests for KoSvgTextShape newline insertion.
Wolthera van Hövell tot Westerflier856e17d3b9a7
diff --git a/libs/flake/tests/TestSvgText.cpp b/libs/flake/tests/TestSvgText.cppindex e80393b1e8..71c0b6180d 100644--- a/libs/flake/tests/TestSvgText.cpp+++ b/libs/flake/tests/TestSvgText.cpp@@ -3286,11 +3286,32 @@ void TestSvgText::testApplyWhiteSpace()QVERIFY2(textShape->plainText() == result, QString("Apply whitespace result is incorrect: \"%1\"").arg(textShape->plainText()).toLatin1());}+void TestSvgText::testInsertNewLinesAtAnchors_data()+{+ QTest::addColumn<QString>("svg");+ QTest::addColumn<QString>("result");++ QTest::addRow("Basic") <<"<text style=\"white-space:pre-wrap\"><tspan x=\"0\" y=\"0\">Text</tspan><tspan x=\"0\" y=\"12\">\ntest</tspan><tspan x=\"0\" y=\"24\">text</tspan></text>"+ << "Text\ntest\ntext";+++ QTest::addRow("Nested") <<"<text style=\"white-space:pre-wrap\" x=\"10 15 20 25\"><tspan x=\"0\" y=\"0\">Text</tspan><tspan x=\"0\" y=\"12\">\ntest</tspan><tspan x=\"0\" y=\"24\">text</tspan></text>"+ << "Text\ntest\ntext";++ QTest::addRow("TextPath") <<"<text style=\"white-space:pre-wrap\"><tspan x=\"0\" y=\"0\">Text </tspan><textPath d=\"M0 12 L0 10\">test</textPath><tspan x=\"0\" y=\"24\">text</tspan></text>"+ << "Text test\ntext";++ QTest::addRow("Mulitransform") <<"<text style=\"white-space:pre-wrap\"><tspan x=\"0\" y=\"0\">Text</tspan><tspan x=\"0 5 10 15\" y=\"12 12 13 11\">test</tspan><tspan x=\"0\" y=\"24\">text</tspan></text>"+ << "Text\ntest\ntext";++ QTest::addRow("Single Dimension Absolute") <<"<text style=\"white-space:pre-wrap\"><tspan x=\"0\" y=\"0\">Text</tspan><tspan x=\"0 5 10 15\" dy=\"12 12 13 11\">\ntest</tspan><tspan x=\"0 5 10\" y=\"12\" dy=\"0 1 -1\">text</tspan></text>"+ << "Text\ntest\ntext";+}+void TestSvgText::testInsertNewLinesAtAnchors(){- const QString svg = "<text style=\"white-space:pre-wrap\"><tspan x=\"0\" y=\"0\">Text</tspan><tspan x=\"0\" y=\"12\">\ntest</tspan><tspan x=\"0\" y=\"24\">text</tspan></text>";- const QString result = "Text\ntest\ntext";- //TODO: test text path and nested transforms.+ QFETCH(QString, svg);+ QFETCH(QString, result);KoSvgTextShape *textShape = new KoSvgTextShape();KoSvgTextShapeMarkupConverter converter(textShape);diff --git a/libs/flake/tests/TestSvgText.h b/libs/flake/tests/TestSvgText.hindex 11762d7594..5d30d97766 100644--- a/libs/flake/tests/TestSvgText.h+++ b/libs/flake/tests/TestSvgText.h@@ -134,6 +134,7 @@ private Q_SLOTS:void testApplyWhiteSpace_data();void testApplyWhiteSpace();+ void testInsertNewLinesAtAnchors_data();void testInsertNewLinesAtAnchors();};diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex d81ce8fd2a..1d3c2a3827 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1291,6 +1291,7 @@ void KoSvgTextShape::convertCharTransformsToPreformatted(const bool makeInlineSiconst int inlineSize = writingMode() == KoSvgText::HorizontalTB? outlineRect().width(): outlineRect().height();d->applyWhiteSpace(d->textData, true);d->insertNewLinesAtAnchors(d->textData, !d->shapesInside.isEmpty());+ d->cleanUp(d->textData);KoSvgTextProperties props = this->propertiesForPos(-1);if (makeInlineSize) {diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex fec0893250..b6622cfce4 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -924,29 +924,42 @@ public:QVector<bool> collapsed = collapsedWhiteSpacesForText(tree, all, false);QVector<CharacterResult> result(all.size());int globalIndex = 0;- const KoSvgTextProperties props = tree.childBegin()->properties;- KoSvgText::WritingMode mode = KoSvgText::WritingMode(props.propertyOrDefault(KoSvgTextProperties::WritingModeId).toInt());+ KoSvgTextProperties props = tree.childBegin()->properties;+ props.inheritFrom(KoSvgTextProperties::defaultProperties(), true);+ KoSvgText::WritingMode mode = KoSvgText::WritingMode(+ props.propertyOrDefault(KoSvgTextProperties::WritingModeId).toInt());bool isHorizontal = mode == KoSvgText::HorizontalTB;- bool isWrapped = (props.hasProperty(KoSvgTextProperties::InlineSizeId) || shapesInside);+ bool isWrapped = (props.hasProperty(KoSvgTextProperties::InlineSizeId)+ || shapesInside);QVector<KoSvgText::CharTransformation> resolvedTransforms(all.size());- resolveTransforms(tree.childBegin(), all, result, globalIndex, isHorizontal, isWrapped, false, resolvedTransforms, collapsed, KoSvgTextProperties::defaultProperties(), false);+ resolveTransforms(tree.childBegin(), all, result, globalIndex,+ isHorizontal, isWrapped, false, resolvedTransforms,+ collapsed, KoSvgTextProperties::defaultProperties(),+ false);auto end = std::make_reverse_iterator(tree.childBegin());auto begin = std::make_reverse_iterator(tree.childEnd());+ bool inTextPath = false;for (; begin != end; begin++) {- insertNewLinesAtAnchorsImpl(begin, resolvedTransforms);+ insertNewLinesAtAnchorsImpl(begin, resolvedTransforms, inTextPath);}}- static void insertNewLinesAtAnchorsImpl(std::reverse_iterator<KisForest<KoSvgTextContentElement>::child_iterator> current, QVector<KoSvgText::CharTransformation> &resolvedTransforms) {+ static void insertNewLinesAtAnchorsImpl(std::reverse_iterator<KisForest<KoSvgTextContentElement>::child_iterator> current,+ QVector<KoSvgText::CharTransformation> &resolvedTransforms,+ bool &inTextPath) {++ inTextPath = (current->textPath);auto base = current.base();base--;if(base != siblingEnd(base)) {auto end = std::make_reverse_iterator(childBegin(base));auto begin = std::make_reverse_iterator(childEnd(base));for (; begin != end; begin++) {- insertNewLinesAtAnchorsImpl(begin, resolvedTransforms);+ insertNewLinesAtAnchorsImpl(begin,+ resolvedTransforms,+ inTextPath);}}@@ -956,7 +969,21 @@ public:for (int i = 0; i < total; i++) {const int j = total - (i+1);KoSvgText::CharTransformation transform = resolvedTransforms.takeLast();- if (transform.startsNewChunk() && !resolvedTransforms.isEmpty() && current->text.at(j) != QChar::LineFeed) {+ /**+ * Because sometimes we have to deal with multiple transforms on a span+ * we only really want to insert newlines when at a content element start.+ * In theory the other transforms can be tested, but this is hard,+ * and semantically it doesn't make sense if a svg text does this.+ */+ bool startsNewChunk = transform.startsNewChunk() && j == 0;++ if (inTextPath) {+ // First transform in path is always absolute, so we don't insert a newline.+ startsNewChunk = false;+ inTextPath = false;+ }+ // When there's no new chunk, we're not at the start of the text and there isn't already a line feed, insert a line feed.+ if (startsNewChunk && !resolvedTransforms.isEmpty() && current->text.at(j) != QChar::LineFeed) {current->text.insert(j, "\n");}}