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++
Replace KoSvgTextShape tree index with a KoSvgTextNodeEditor.
Wolthera van Hövell tot Westerflier7e24d74be064
diff --git a/libs/flake/resources/KoCssStylePreset.cpp b/libs/flake/resources/KoCssStylePreset.cppindex 8c3f1c0e64..c605a81001 100644--- a/libs/flake/resources/KoCssStylePreset.cpp+++ b/libs/flake/resources/KoCssStylePreset.cpp@@ -422,10 +422,10 @@ bool KoCssStylePreset::loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP reif (textShape) {setName(textShape->additionalAttribute(TITLE));addMetaData(DESCRIPTION, textShape->additionalAttribute(DESC));- QVector<int> treeIndex = textShape->findTreeIndexForPropertyId(KoSvgTextProperties::KraTextStyleType);+ KoSvgTextNodeEditorSP node = textShape->findNodeEditorForPropertyId(KoSvgTextProperties::KraTextStyleType);QString styleType = STYLE_TYPE_PARAGRAPH;- if (!treeIndex.isEmpty()) {- KoSvgTextProperties props = textShape->propertiesForTreeIndex(treeIndex);+ if (node->properties()) {+ KoSvgTextProperties props = *(node->properties());styleType = props.property(KoSvgTextProperties::KraTextStyleType).toString();if (props.hasProperty(KoSvgTextProperties::KraTextStyleResolution)) {addMetaData(STORED_PPI, props.property(KoSvgTextProperties::KraTextStyleResolution));@@ -436,7 +436,7 @@ bool KoCssStylePreset::loadFromDevice(QIODevice *dev, KisResourcesInterfaceSP re}setProperties(props);- QPair<int, int> pos = textShape->findRangeForTreeIndex(treeIndex);+ QPair<int, int> pos = textShape->findRangeForNodeEditor(node);pos.first = textShape->indexForPos(pos.first);pos.second = textShape->indexForPos(pos.second);diff --git a/libs/flake/tests/TestSvgText.cpp b/libs/flake/tests/TestSvgText.cppindex 98428f391d..22fdcbe544 100644--- a/libs/flake/tests/TestSvgText.cpp+++ b/libs/flake/tests/TestSvgText.cpp@@ -3146,10 +3146,9 @@ void TestSvgText::testSearchingTreeIndex()KoSvgTextShapeMarkupConverter converter(textShape);converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);- QVector<int> refIndex = {0, 1};- QVector<int> treeIndex = textShape->findTreeIndexForPropertyId(KoSvgTextProperties::FontStyleId);+ KoSvgTextNodeEditorSP node = textShape->findNodeEditorForPropertyId(KoSvgTextProperties::FontStyleId);- QCOMPARE(refIndex, treeIndex);+ QVERIFY(node->properties());}/**@@ -3164,8 +3163,8 @@ void TestSvgText::testRangeForTreeIndex()KoSvgTextShapeMarkupConverter converter(textShape);converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);- QVector<int> treeIndex = textShape->findTreeIndexForPropertyId(KoSvgTextProperties::FontStyleId);- QPair<int, int> range = textShape->findRangeForTreeIndex(treeIndex);+ KoSvgTextNodeEditorSP node = textShape->findNodeEditorForPropertyId(KoSvgTextProperties::FontStyleId);+ QPair<int, int> range = textShape->findRangeForNodeEditor(node);QCOMPARE(range.first, 10);QCOMPARE(range.second, 15);@@ -3183,18 +3182,15 @@ void TestSvgText::testSetPropertiesOnTreeIndex()KoSvgTextShapeMarkupConverter converter(textShape);converter.convertFromSvg(ref, QString(), QRectF(0, 0, 300, 300), 72.0);- QVector<int> treeIndex = textShape->findTreeIndexForPropertyId(KoSvgTextProperties::FontStyleId);- KoSvgTextProperties props = textShape->propertiesForTreeIndex(treeIndex);+ KoSvgTextNodeEditorSP node = textShape->findNodeEditorForPropertyId(KoSvgTextProperties::FontStyleId);- QVERIFY(props.hasProperty(KoSvgTextProperties::FontStyleId));+ QVERIFY(node->properties()->hasProperty(KoSvgTextProperties::FontStyleId));- props.setProperty(KoSvgTextProperties::FontWeightId, 700);- props.removeProperty(KoSvgTextProperties::FontStyleId);- textShape->setPropertiesAtTreeIndex(treeIndex, props);+ node->properties()->setProperty(KoSvgTextProperties::FontWeightId, 700);+ node->properties()->removeProperty(KoSvgTextProperties::FontStyleId);- KoSvgTextProperties props2 = textShape->propertiesForTreeIndex(treeIndex);- QVERIFY(!props2.hasProperty(KoSvgTextProperties::FontStyleId));- QVERIFY(props2.hasProperty(KoSvgTextProperties::FontWeightId));+ QVERIFY(!node->properties()->hasProperty(KoSvgTextProperties::FontStyleId));+ QVERIFY(node->properties()->hasProperty(KoSvgTextProperties::FontWeightId));}void TestSvgText::testInsertTransforms_data()diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 537cdf9643..62710b9307 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -84,6 +84,32 @@ private:QPointF initialTextPosition = QPointF();};+class KoSvgTextNodeEditorImpl: public KoSvgTextNodeEditor+{+public:+ KoSvgTextNodeEditorImpl(+ KisForest<KoSvgTextContentElement>::child_iterator textElement)+ : KoSvgTextNodeEditor()+ ,m_textElement(textElement) {}++ ~KoSvgTextNodeEditorImpl() {}++ KoSvgTextProperties *properties() override {+ return &m_textElement->properties;+ }++ KoSvgText::TextOnPathInfo *textPathInfo() override {+ return &m_textElement->textPathInfo;+ }++ KoShape *textPath() override {+ // TODO: implement this properly in the text-in-shape branch.+ return nullptr;+ }+private:+ friend class KoSvgTextShape;+ KisForest<KoSvgTextContentElement>::child_iterator m_textElement;+};KoSvgTextShape::KoSvgTextShape(): KoShape()@@ -1058,114 +1084,46 @@ void KoSvgTextShape::cleanUp()shapeChangedPriv(ContentChanged);}-QVector<int> findTreeIndexForPropertyIdImpl(KisForest<KoSvgTextContentElement>::child_iterator parent, KoSvgTextProperties::PropertyId propertyId) {- int count = 0;+KisForest<KoSvgTextContentElement>::child_iterator findNodeEditorForPropertyIdImpl(KisForest<KoSvgTextContentElement>::child_iterator parent, KoSvgTextProperties::PropertyId propertyId) {for (auto child = KisForestDetail::childBegin(parent); child != KisForestDetail::childEnd(parent); child++) {if (child->properties.hasProperty(propertyId)) {- return QVector<int>({count});+ return child;} else if (KisForestDetail::childBegin(child) != KisForestDetail::childEnd(child)) {- QVector<int> current = QVector<int>({count});- QVector<int> childIndex = findTreeIndexForPropertyIdImpl(child, propertyId);- if (!childIndex.isEmpty()) {- current.append(childIndex);- return current;+ auto found = findNodeEditorForPropertyIdImpl(child, propertyId);+ if (found != child) {+ return found;}}- count += 1;}- return QVector<int>();+ return parent;}-QVector<int> KoSvgTextShape::findTreeIndexForPropertyId(KoSvgTextProperties::PropertyId propertyId)+KoSvgTextNodeEditorSP KoSvgTextShape::findNodeEditorForPropertyId(KoSvgTextProperties::PropertyId propertyId){- QVector<int> treeIndex;-for (auto it = d->textData.childBegin(); it != d->textData.childEnd(); it++) {if (it->properties.hasProperty(propertyId)) {- return QVector<int>({0});- } else {- treeIndex = findTreeIndexForPropertyIdImpl(it, propertyId);- if (!treeIndex.isEmpty()) {- treeIndex.insert(0, 0);+ return KoSvgTextNodeEditorSP(new KoSvgTextNodeEditorImpl(it));+ } else if (KisForestDetail::childBegin(it) != KisForestDetail::childEnd(it)) {+ auto found = findNodeEditorForPropertyIdImpl(it, propertyId);+ if (found != it) {+ return KoSvgTextNodeEditorSP(new KoSvgTextNodeEditorImpl(found));}}}-- return treeIndex;+ return KoSvgTextNodeEditorSP(new KoSvgTextNodeEditorImpl(d->textData.childBegin()));}-KoSvgTextProperties KoSvgTextShape::propertiesForTreeIndex(const QVector<int> &treeIndex) const+QPair<int, int> KoSvgTextShape::findRangeForNodeEditor(KoSvgTextNodeEditorSP node) const{- if (treeIndex.isEmpty()) return KoSvgTextProperties();- QVector<int> idx = treeIndex;- idx.removeFirst();- for (auto it = d->textData.childBegin(); it != d->textData.childEnd(); it++) {- if (idx.isEmpty()) {- if (it != d->textData.childEnd()) {- return it->properties;- } else {- return KoSvgTextProperties();- }- }- auto child = d->iteratorForTreeIndex(idx, it);- if (child != d->textData.childEnd()) {- return child->properties;- }- }- return KoSvgTextProperties();-}--bool KoSvgTextShape::setPropertiesAtTreeIndex(const QVector<int> treeIndex, const KoSvgTextProperties props)-{- QVector<int> idx = treeIndex;- bool success = false;- if (treeIndex.isEmpty()) return success;- idx.removeFirst();- for (auto it = d->textData.childBegin(); it != d->textData.childEnd(); it++) {- if (idx.isEmpty()) {- if (it != d->textData.childEnd()) {- it->properties = props;- success = true;- break;- } else {- success = false;- break;- }- }- auto child = d->iteratorForTreeIndex(idx, it);- if (child != d->textData.childEnd()) {- child->properties = props;- success = true;- break;- }- }- if (success) {- notifyChanged();- shapeChangedPriv(ContentChanged);- }- return success;-}--QPair<int, int> KoSvgTextShape::findRangeForTreeIndex(const QVector<int> treeIndex) const-{- QVector<int> idx = treeIndex;- if (idx.isEmpty()) {- return qMakePair(-1, -1);- }- if (idx.size() == 1) {- return qMakePair(0, d->cursorPos.size());- }- idx.removeFirst();+ KoSvgTextNodeEditorImpl *nodeImpl = dynamic_cast<KoSvgTextNodeEditorImpl*>(node.data());+ if (!nodeImpl) return qMakePair(-1, -1);int startIndex = 0;int endIndex = 0;- for (auto it = d->textData.childBegin(); it != d->textData.childEnd(); it++) {- auto child = d->iteratorForTreeIndex(idx, it);- if (child != d->textData.childEnd()) {- // count children- d->startIndexOfIterator(d->textData.childBegin(), child, startIndex);- endIndex = d->numChars(child) + startIndex;- }+ for (auto child = d->textData.childBegin(); child != d->textData.childEnd(); child++) {+ // count children+ d->startIndexOfIterator(child, nodeImpl->m_textElement, startIndex);+ endIndex = d->numChars(nodeImpl->m_textElement) + startIndex;}return qMakePair(posForIndex(startIndex), posForIndex(endIndex));}diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex 9f11dd8ff4..a1c580d267 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -18,7 +18,9 @@#include <QFlags>class KoSvgTextShapeMemento;+class KoSvgTextNodeEditor;typedef QSharedPointer<KoSvgTextShapeMemento> KoSvgTextShapeMementoSP;+typedef QSharedPointer<KoSvgTextNodeEditor> KoSvgTextNodeEditorSP;#define KoSvgTextShape_SHAPEID "KoSvgTextShapeID"/**@@ -373,53 +375,31 @@ public:void cleanUp();/*****************************************************************************- * Tree Index functions.+ * KoSvgTextNodeEditor functions.** SVG Text is internally a tree of nodes, with each node having text* properties, and the child nodes furthest from the root having text content.* While we can access the latter by using ranges, and we can easily access* the root node by using -1, there's no way to access the nodes that may- * be inbetween. Tree index allows accessing these nodes.- *- * Each entry in the index is represents the index it is in the parent's list- * of child nodes. This means that the root index is always {0}. The second- * child of the root would be represented as {0, 1}, and the first child of- * the second child of the root as {0, 1, 0}, etc.+ * be inbetween. KoSvgTextNodeEditor allows accessing these nodes.*****************************************************************************//**- * @brief findTreeIndexForPropertyId- * @return the tree index of the first content element found with a given property id.- * Empty vector means none was found.+ * @brief findNodeEditorForPropertyId+ * @return the nodeEditor of the first content element found with a given property id.* @propertyId -- id to search for.*/- QVector<int> findTreeIndexForPropertyId(KoSvgTextProperties::PropertyId propertyId);-- /**- * @brief propertiesForTreeIndex- * @param treeIndex -- vector representing the tree index.- * @return properties at the given tree index. If the tree index is wrong, empty properties will be returned.- */- KoSvgTextProperties propertiesForTreeIndex(const QVector<int> &treeIndex) const;-- /**- * @brief setPropertiesAtTreeIndex- * @param treeIndex -- set text properties at given tree index.- * @return if successful.- */- bool setPropertiesAtTreeIndex(const QVector<int> treeIndex, const KoSvgTextProperties props);+ KoSvgTextNodeEditorSP findNodeEditorForPropertyId(KoSvgTextProperties::PropertyId propertyId);/**- * @brief findStartAndEndPosForTreeIndex- * Find the start and end cursor position for a given tree index.- * @param treeIndex the tree index to find the range for. This range will encompass any children.- * The treeindex will always start with the index of the paragraph (0)- * and an empty tree index is considered invalid.+ * @brief findRangeForNodeEditor+ * Find the start and end cursor position for a given nodeEditor.+ * @param node the tree index to find the range for.* @return A QPair<int,int> describing cursor position range encompassed by the tree index and it's children.* Will return {-1, -1} when the tree index is invalid.*/- QPair<int, int> findRangeForTreeIndex(const QVector<int> treeIndex) const;+ QPair<int, int> findRangeForNodeEditor(KoSvgTextNodeEditorSP node) const;/*--------------- Properties ---------------*/@@ -571,7 +551,45 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(KoSvgTextShape::DebugElements)class KRITAFLAKE_EXPORT KoSvgTextShapeMemento {public:KoSvgTextShapeMemento() {}- virtual ~KoSvgTextShapeMemento() {};+ virtual ~KoSvgTextShapeMemento() {}+private:+ friend class KoSvgTextShape;+};++/**+ * @brief The KoSvgTextNodeEditor class+ *+ * Because SVG text is a tree, it is not always+ * possible to edit nodes by range. This class can+ * be used to access a node in the tree directly,+ * allow the direct editing of properties or any+ * text path on the given node.+ */+class KRITAFLAKE_EXPORT KoSvgTextNodeEditor {+public:+ KoSvgTextNodeEditor () {}+ virtual ~KoSvgTextNodeEditor() {}++ /**+ * @brief properties+ * The properties for this node as a pointer.+ * @return properties for this node.+ */+ virtual KoSvgTextProperties *properties() {return nullptr;}++ /**+ * @brief textPathInfo+ * the text path info for this node as a pointer.+ * @return the current text path info.+ */+ virtual KoSvgText::TextOnPathInfo *textPathInfo() {return nullptr;}++ /**+ * @brief textPath+ * @return the textPath of this node, may be null.+ */+ virtual KoShape *textPath() {return nullptr;}+private:friend class KoSvgTextShape;};