Skip to content

armgit / krita

Public Imported from GitHub
0

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++

Ensure SVG characters transforms get added/removed when inserting plain text.

Wolthera van Hövell tot Westerflierab5f02704d0b
diff --git a/libs/flake/tests/TestSvgText.cpp b/libs/flake/tests/TestSvgText.cpp
index f2104631d5..efb61e39cc 100644
--- a/libs/flake/tests/TestSvgText.cpp
+++ b/libs/flake/tests/TestSvgText.cpp
@@ -3195,7 +3195,68 @@ void TestSvgText::testSetPropertiesOnTreeIndex()
KoSvgTextProperties props2 = textShape->propertiesForTreeIndex(treeIndex);
QVERIFY(!props2.hasProperty(KoSvgTextProperties::FontStyleId));
QVERIFY(props2.hasProperty(KoSvgTextProperties::FontWeightId));
+}
+
+void TestSvgText::testInsertTransforms_data()
+{
+ QTest::addColumn<QString>("svg");
+ QTest::addColumn<int>("pos");
+ QTest::addColumn<QString>("insert");
+ QTest::addColumn<QPoint>("caretp1");
+
+ QTest::addRow("no transforms") << "<text>Text</text>" << 3 << "test" << QPoint(12, 3);
+ QTest::addRow("base transform") << "<text x=\"20\" y=\"20\">Text</text>" << 3 << "test" << QPoint(32,23);
+ QTest::addRow("list transforms") << "<text x=\"20\" y=\"20 10 10 10 10 10\">Text</text>" << 3 << "test" << QPoint(27, 13);
+ QTest::addRow("nested transforms") << "<text x=\"20\" y=\"20 10 10 10 10 10\">Te<tspan dy=\"20 10 10 10 10 10\">xt</tspan></text>" << 3 << "test" << QPoint(27, 13);
+ QTest::addRow("nested transforms start") << "<text x=\"20\" y=\"20 10 10 10 10 10\">Te<tspan dy=\"20 10 10 10 10 10\">xt</tspan></text>" << 3 << "A" << QPoint(27, 13);
+}
+
+void TestSvgText::testInsertTransforms()
+{
+ QFETCH(QString, svg);
+ QFETCH(int, pos);
+ QFETCH(QString, insert);
+ QFETCH(QPoint, caretp1);
+ KoSvgTextShape *textShape = new KoSvgTextShape();
+ KoSvgTextShapeMarkupConverter converter(textShape);
+ converter.convertFromSvg(svg, QString(), QRectF(0, 0, 300, 300), 72.0);
+
+ textShape->insertText(pos, insert);
+ QLineF caret;
+ QColor color;
+ textShape->cursorForPos(2, caret, color);
+ QVERIFY(caret.p1().toPoint() == caretp1);
+}
+void TestSvgText::testRemoveTransforms_data()
+{
+ QTest::addColumn<QString>("svg");
+ QTest::addColumn<int>("pos");
+ QTest::addColumn<int>("length");
+ QTest::addColumn<QPoint>("caretp1");
+
+ QTest::addRow("no transforms") << "<text>Texttest</text>" << 3 << 4 << QPoint(12, 3);
+ QTest::addRow("base transform") << "<text x=\"20\" y=\"20\">Texttest</text>" << 3 << 4 << QPoint(32, 23);
+ QTest::addRow("list transforms") << "<text x=\"20\" y=\"20 10 10 10 10 10\">Texttest</text>" << 3 << 4 << QPoint(27, 13);
+ QTest::addRow("nested transforms") << "<text x=\"20\" y=\"20 10 10 10 10 10\">Text<tspan dy=\"20 10 10 10 10 10\">test</tspan></text>" << 3 << 4 << QPoint(27, 13);
+ QTest::addRow("nested transforms start") << "<text x=\"20\" y=\"20 10 10 10 10 10\">Text<tspan dy=\"20 10 10 10 10 10\">test</tspan></text>" << 3 << 1 << QPoint(27, 13);
+}
+
+void TestSvgText::testRemoveTransforms()
+{
+ QFETCH(QString, svg);
+ QFETCH(int, pos);
+ QFETCH(int, length);
+ QFETCH(QPoint, caretp1);
+ KoSvgTextShape *textShape = new KoSvgTextShape();
+ KoSvgTextShapeMarkupConverter converter(textShape);
+ converter.convertFromSvg(svg, QString(), QRectF(0, 0, 300, 300), 72.0);
+
+ textShape->removeText(pos, length);
+ QLineF caret;
+ QColor color;
+ textShape->cursorForPos(2, caret, color);
+ QVERIFY(caret.p1().toPoint() == caretp1);
}
#include "kistest.h"
diff --git a/libs/flake/tests/TestSvgText.h b/libs/flake/tests/TestSvgText.h
index fe64722d03..f8a68cbd47 100644
--- a/libs/flake/tests/TestSvgText.h
+++ b/libs/flake/tests/TestSvgText.h
@@ -124,6 +124,12 @@ private Q_SLOTS:
void testSearchingTreeIndex();
void testRangeForTreeIndex();
void testSetPropertiesOnTreeIndex();
+
+ void testInsertTransforms_data();
+ void testInsertTransforms();
+
+ void testRemoveTransforms_data();
+ void testRemoveTransforms();
};
#endif // TESTSVGTEXT_H
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cpp
index ba1da494de..805ed1daa0 100644
--- a/libs/flake/text/KoSvgTextShape.cpp
+++ b/libs/flake/text/KoSvgTextShape.cpp
@@ -769,20 +769,26 @@ QPointF KoSvgTextShape::initialTextPosition() const
bool KoSvgTextShape::insertText(int pos, QString text)
{
bool success = false;
- int currentIndex = 0;
- int index = 0;
- int oldIndex = 0;
+ int currentIndex = 0; ///< Current position of the search.
+
+ /// The distinction between element and insertion index allows us to insert text at
+ /// the start or end of a content element, without ambiguity on whether we're
+ /// inserting into the next element.
+ int elementIndex = 0;
+ int insertionIndex = 0;
if (pos > -1 && !d->cursorPos.isEmpty()) {
CursorPos cursorPos = d->cursorPos.at(pos);
CharacterResult res = d->result.at(cursorPos.cluster);
- index = res.plaintTextIndex;
- oldIndex = cursorPos.index;
- index = qMin(index, d->result.size()-1);
+ elementIndex = res.plaintTextIndex;
+ insertionIndex = cursorPos.index;
+ elementIndex = qMin(elementIndex, d->result.size()-1);
}
- auto it = d->findTextContentElementForIndex(d->textData, currentIndex, index);
+ auto it = d->findTextContentElementForIndex(d->textData, currentIndex, elementIndex);
if (it != d->textData.depthFirstTailEnd()) {
- int offset = oldIndex - currentIndex;
+ const int offset = insertionIndex - currentIndex;
it->insertText(offset, text);
+
+ d->insertTransforms(d->textData, insertionIndex, text.size(), (elementIndex == insertionIndex));
notifyChanged();
shapeChangedPriv(ContentChanged);
success = true;
@@ -813,6 +819,9 @@ bool KoSvgTextShape::removeText(int &index, int &length)
if (index >= currentIndex) {
index = currentIndex + offset;
}
+
+ d->removeTransforms(d->textData, index, endLength);
+
success = true;
} else {
currentLength = -1;
@@ -1004,8 +1013,8 @@ bool KoSvgTextShape::insertRichText(int pos, const KoSvgTextShape *richText)
{
bool success = false;
int currentIndex = 0;
- int index = 0;
- int oldIndex = 0;
+ int elementIndex = 0;
+ int insertionIndex = 0;
if (isEnd(richText->d->textData.childBegin())) {
// rich text is empty.
@@ -1015,20 +1024,20 @@ bool KoSvgTextShape::insertRichText(int pos, const KoSvgTextShape *richText)
if (pos > -1 && !d->cursorPos.isEmpty()) {
CursorPos cursorPos = d->cursorPos.at(qMin(d->cursorPos.size()-1, pos));
CharacterResult res = d->result.at(cursorPos.cluster);
- index = res.plaintTextIndex;
- oldIndex = cursorPos.index;
- index = qMin(index, d->result.size()-1);
+ elementIndex = res.plaintTextIndex;
+ insertionIndex = cursorPos.index;
+ elementIndex = qMin(elementIndex, d->result.size()-1);
}
- KoSvgTextShape::Private::splitContentElement(this->d->textData, oldIndex);
+ KoSvgTextShape::Private::splitContentElement(this->d->textData, insertionIndex);
- auto it = d->findTextContentElementForIndex(d->textData, currentIndex, oldIndex);
+ auto it = d->findTextContentElementForIndex(d->textData, currentIndex, insertionIndex);
if (it != d->textData.depthFirstTailEnd()) {
d->textData.move(richText->d->textData.childBegin(), siblingCurrent(it));
success = true;
} else {
currentIndex = 0;
- it = d->findTextContentElementForIndex(d->textData, currentIndex, index);
+ it = d->findTextContentElementForIndex(d->textData, currentIndex, elementIndex);
if (it != d->textData.depthFirstTailEnd()) {
d->textData.move(richText->d->textData.childBegin(), siblingEnd(siblingCurrent(it)));
success = true;
diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.h
index 71373eb5b2..6c029a5200 100644
--- a/libs/flake/text/KoSvgTextShape_p.h
+++ b/libs/flake/text/KoSvgTextShape_p.h
@@ -13,6 +13,8 @@
#include "KoSvgText.h"
#include "KoSvgTextContentElement.h"
+#include "KoCssTextUtils.h"
+
#include <kis_assert.h>
#include <KisForest.h>
@@ -689,6 +691,156 @@ public:
return false;
}
+ /**
+ * @brief collapsedWhiteSpacesForText
+ * This returns the collapsed spaces for a given piece of text, without transforms.
+ * @param tree -- text data tree.
+ * @param alsoCollapseLowSurrogate -- whether to mark utf16 surrogates as collapsed too.
+ * @return list of collapsed characters
+ */
+ static QVector<bool> collapsedWhiteSpacesForText(KisForest<KoSvgTextContentElement> &tree, const bool alsoCollapseLowSurrogate = false) {
+ QString allText;
+ QMap<int, KoSvgText::TextSpaceCollapse> collapseModes;
+
+ for (auto it = tree.depthFirstTailBegin(); it != tree.depthFirstTailEnd(); it++) {
+ const int children = childCount(siblingCurrent(it));
+ if (children == 0) {
+ QString text = it->text;
+ KoSvgText::TextSpaceCollapse collapse = KoSvgText::TextSpaceCollapse(it->properties.propertyOrDefault(KoSvgTextProperties::TextCollapseId).toInt());
+ collapseModes.insert(allText.size(), collapse);
+ allText += text;
+ } else {
+ break;
+ }
+ }
+ QVector<bool> collapsed = KoCssTextUtils::collapseSpaces(&allText, collapseModes);
+ if (alsoCollapseLowSurrogate) {
+ for (int i = 0; i < allText.size(); i++) {
+ if (i > 0 && allText.at(i).isLowSurrogate() && allText.at(i-1).isHighSurrogate()) {
+ collapsed[i] = true;
+ }
+ }
+ }
+ return collapsed;
+ }
+
+ /**
+ * @brief removeTransforms
+ * Remove all local SVG character transforms in a certain range.
+ * Local transforms are influenced by whitespace collapse and
+ * whether they are set to unicode codepoints, and they also accumulate
+ * from parent to child. This function removes all local transforms in
+ * a certain section.
+ * @param tree -- tree to remove transforms from.
+ * @param start -- start at which to remove transforms.
+ * @param length -- end to remove from.
+ */
+ static void removeTransforms(KisForest<KoSvgTextContentElement> &tree, const int start, const int length) {
+ QVector<bool> collapsedCharacters = collapsedWhiteSpacesForText(tree, true);
+
+ auto root = tree.childBegin();
+ removeTransformsImpl(root, 0, start, length, collapsedCharacters);
+ }
+
+ /**
+ * @brief removeTransformsImpl
+ * recursive function that handles removing local transforms in a certain range.
+ * Used by removeTransform.
+ */
+ static int removeTransformsImpl(KisForest<KoSvgTextContentElement>::child_iterator currentTextElement, const int globalIndex, const int start, const int length, const QVector<bool> collapsedCharacters) {
+ int currentLength = 0;
+ auto it = childBegin(currentTextElement);
+ if (it != childEnd(currentTextElement)) {
+ for (; it != childEnd(currentTextElement); it++) {
+ currentLength += removeTransformsImpl(it, globalIndex + currentLength, start, length, collapsedCharacters);
+ }
+ } else {
+ currentLength = currentTextElement->text.size();
+ }
+
+ if (!currentTextElement->localTransformations.isEmpty()) {
+ int transformOffset = 0;
+ int transformOffsetEnd = 0;
+
+ for (int i = globalIndex; i < globalIndex + currentLength; i++) {
+ if (i < start) {
+ transformOffset += collapsedCharacters.at(i)? 0: 1;
+ }
+ if (i < start + length) {
+ transformOffsetEnd += collapsedCharacters.at(i)? 0: 1;
+ } else {
+ break;
+ }
+ }
+ if (transformOffset < currentTextElement->localTransformations.size()) {
+ currentTextElement->localTransformations.remove(transformOffset, transformOffsetEnd-transformOffset);
+ }
+
+ }
+ return currentLength;
+ }
+
+ /**
+ * @brief insertTransforms
+ * Inserts empty transforms into tree recursively.
+ * @param tree -- tree to insert transforms on.
+ * @param start -- start index.
+ * @param length -- amount of transforms to insert.
+ * @param allowSkipFirst -- whether we're allowed to skip the first
+ * transform because it is at the start of a text element.
+ */
+ static void insertTransforms(KisForest<KoSvgTextContentElement> &tree, const int start, const int length, const bool allowSkipFirst) {
+ QVector<bool> collapsedCharacters = collapsedWhiteSpacesForText(tree, true);
+
+ auto root = tree.childBegin();
+ insertTransformsImpl(root, 0, start, length, collapsedCharacters, allowSkipFirst);
+ }
+ /**
+ * @brief insertTransformsImpl
+ * Recursive function that handles inserting empty transforms into a given range.
+ * Used by insertTransforms.
+ */
+ static int insertTransformsImpl(KisForest<KoSvgTextContentElement>::child_iterator currentTextElement, const int globalIndex, const int start, const int length, const QVector<bool> collapsedCharacters, const bool allowSkipFirst) {
+ int currentLength = 0;
+ auto it = childBegin(currentTextElement);
+ if (it != childEnd(currentTextElement)) {
+ for (; it != childEnd(currentTextElement); it++) {
+ currentLength += insertTransformsImpl(it, globalIndex + currentLength, start, length, collapsedCharacters, allowSkipFirst);
+ }
+ } else {
+ currentLength = currentTextElement->text.size();
+ }
+
+ if (!currentTextElement->localTransformations.isEmpty()) {
+ int transformOffset = 0;
+ int transformOffsetEnd = 0;
+
+ for (int i = globalIndex; i < globalIndex + currentLength; i++) {
+ if (i < start) {
+ transformOffset += collapsedCharacters.at(i)? 0: 1;
+ }
+ if (i < start + length) {
+ transformOffsetEnd += collapsedCharacters.at(i)? 0: 1;
+ } else {
+ break;
+ }
+ }
+
+ // When at the start, skip the first transform.
+ if (transformOffset == 0 && allowSkipFirst && currentTextElement->localTransformations.at(0).startsNewChunk()) {
+ transformOffset += 1;
+ }
+
+ if (transformOffset < currentTextElement->localTransformations.size()) {
+ for (int i = transformOffset; i < transformOffsetEnd; i++) {
+ currentTextElement->localTransformations.insert(i, KoSvgText::CharTransformation());
+ }
+ }
+
+ }
+ return currentLength;
+ }
+
/**
* @brief cleanUp
* This cleans up the tree by...