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

Implement text on path creation.

Wolthera van Hövell tot Westerflier532c00cab38e
diff --git a/libs/flake/KoPathShape.cpp b/libs/flake/KoPathShape.cpp
index cd63065290..1585f39a3d 100644
--- a/libs/flake/KoPathShape.cpp
+++ b/libs/flake/KoPathShape.cpp
@@ -32,6 +32,7 @@
#include <QPainterPath>
#include "kis_global.h"
+#include <kis_algebra_2d.h>
#include <qnumeric.h> // for qIsNaN
static bool qIsNaNPoint(const QPointF &p) {
@@ -1337,6 +1338,38 @@ void KoPathShape::setAutoFillMarkers(bool value)
d->autoFillMarkers = value;
}
+KoPathSegment KoPathShape::segmentAtPoint(const QPointF &point, const QRectF &grabRoi) const
+{
+ const qreal distanceThreshold = 0.5 * KisAlgebra2D::maxDimension(grabRoi);
+ KoPathSegment segment;
+
+ // convert document point to shape coordinates
+ const QPointF p = documentToShape(point);
+ // our region of interest, i.e. a region around our mouse position
+ const QRectF roi = documentToShape(grabRoi);
+
+ qreal minDistance = std::numeric_limits<qreal>::max();
+
+ // check all segments of this shape which intersect the region of interest
+ const QList<KoPathSegment> segments = segmentsAt(roi);
+
+ foreach (KoPathSegment s, segments) {
+ const qreal nearestPointParam = s.nearestPoint(p);
+ const QPointF nearestPoint = s.pointAt(nearestPointParam);
+ const qreal distance = kisDistance(p, nearestPoint);
+
+ // are we within the allowed distance ?
+ if (distance > distanceThreshold)
+ continue;
+ // are we closer to the last closest point ?
+ if (distance < minDistance) {
+ segment = s;
+ }
+ }
+
+ return segment;
+}
+
void KoPathShape::recommendPointSelectionChange(const QList<KoPathPointIndex> &newSelection)
{
Q_FOREACH (KoShape::ShapeChangeListener *listener, listeners()) {
diff --git a/libs/flake/KoPathShape.h b/libs/flake/KoPathShape.h
index e6852e74f8..153801a52c 100644
--- a/libs/flake/KoPathShape.h
+++ b/libs/flake/KoPathShape.h
@@ -462,6 +462,8 @@ public:
bool autoFillMarkers() const;
void setAutoFillMarkers(bool value);
+ KoPathSegment segmentAtPoint(const QPointF &point, const QRectF &grabRoi) const;
+
public:
struct KRITAFLAKE_EXPORT PointSelectionChangeListener : public ShapeChangeListener {
void notifyShapeChanged(ChangeType type, KoShape *shape) override;
diff --git a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp
index e92dfc3764..bc63c40565 100644
--- a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp
+++ b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.cpp
@@ -12,10 +12,12 @@
#include <KoShapeContainer.h>
struct KoSvgTextAddRemoveShapeCommandImpl::Private {
- Private(KoSvgTextShape *_text, KoShape *_shape)
+ Private(KoSvgTextShape *_text, KoShape *_shape, int _startPos, int _endPos)
: textShape(_text)
, shape(_shape)
, memento(textShape->getMemento())
+ , startPos(_startPos)
+ , endPos(_endPos)
{
}
@@ -25,13 +27,15 @@ struct KoSvgTextAddRemoveShapeCommandImpl::Private {
KoShape* shape = nullptr;
std::optional<KoShapeContainer*> originalShapeParent;
KoSvgTextShapeMementoSP memento;
+ int startPos = -1;
+ int endPos = -1;
KoSvgTextAddRemoveShapeCommandImpl::ContourType type;
bool removeCommand;
};
-KoSvgTextAddRemoveShapeCommandImpl::KoSvgTextAddRemoveShapeCommandImpl(KoSvgTextShape *textShape, KoShape* shape, ContourType type, State state, KUndo2Command *parent)
+KoSvgTextAddRemoveShapeCommandImpl::KoSvgTextAddRemoveShapeCommandImpl(KoSvgTextShape *textShape, KoShape* shape, ContourType type, State state, int startPos, int endPos, KUndo2Command *parent)
: KisCommandUtils::FlipFlopCommand(state, parent)
- , d(new Private(textShape, shape))
+ , d(new Private(textShape, shape, startPos, endPos))
{
d->removeCommand = state == FINALIZING;
@@ -103,7 +107,9 @@ void KoSvgTextAddRemoveShapeCommandImpl::partA()
d->textShape->addShapeContours({d->shape}, true);
} else if (d->type == Subtract) {
d->textShape->addShapeContours({d->shape}, false);
- } // TextPath is handled by setMemento.
+ } else if (!d->removeCommand && d->type == TextPath) {
+ d->textShape->setTextPathOnRange(d->shape, d->startPos, d->endPos);
+ }
if (d->removeCommand) {
d->textShape->setMemento(d->memento);
@@ -118,7 +124,7 @@ void KoSvgTextAddRemoveShapeCommandImpl::partA()
}
KoSvgTextAddShapeCommand::KoSvgTextAddShapeCommand(KoSvgTextShape *textShape, KoShape *shape, bool inside, KUndo2Command *parentCommand)
- : KoSvgTextAddRemoveShapeCommandImpl(textShape, shape, (inside? Inside: Subtract), INITIALIZING, parentCommand)
+ : KoSvgTextAddRemoveShapeCommandImpl(textShape, shape, (inside? Inside: Subtract), INITIALIZING, -1, -1, parentCommand)
{
/**
* 1) The shapes should belong to the same parent or have no parent at all.
@@ -135,7 +141,7 @@ KoSvgTextAddShapeCommand::~KoSvgTextAddShapeCommand()
}
KoSvgTextRemoveShapeCommand::KoSvgTextRemoveShapeCommand(KoSvgTextShape *textShape, KoShape *shape, KUndo2Command *parentCommand)
-: KoSvgTextAddRemoveShapeCommandImpl(textShape, shape, Unknown, FINALIZING, parentCommand)
+: KoSvgTextAddRemoveShapeCommandImpl(textShape, shape, Unknown, FINALIZING, -1, -1, parentCommand)
{
// the \p shape will be ungrouped into the parent of \p textShape
KIS_SAFE_ASSERT_RECOVER_NOOP(textShape->shapeInContours(shape));
@@ -145,3 +151,14 @@ KoSvgTextRemoveShapeCommand::~KoSvgTextRemoveShapeCommand()
{
}
+
+KoSvgTextSetTextPathOnRangeCommand::KoSvgTextSetTextPathOnRangeCommand(KoSvgTextShape *textShape, KoShape *shape, int startPos, int endPos, KUndo2Command *parentCommand)
+: KoSvgTextAddRemoveShapeCommandImpl(textShape, shape, TextPath, INITIALIZING, startPos, endPos, parentCommand)
+{
+
+}
+
+KoSvgTextSetTextPathOnRangeCommand::~KoSvgTextSetTextPathOnRangeCommand()
+{
+
+}
diff --git a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.h b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.h
index b1b3a21d83..44a6e52b45 100644
--- a/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.h
+++ b/libs/flake/commands/KoSvgTextAddRemoveShapeCommands.h
@@ -23,7 +23,7 @@ public:
TextPath
};
- KoSvgTextAddRemoveShapeCommandImpl(KoSvgTextShape *textShape, KoShape *shape, ContourType type, State state, KUndo2Command *parent = nullptr);
+ KoSvgTextAddRemoveShapeCommandImpl(KoSvgTextShape *textShape, KoShape *shape, ContourType type, State state, int startPos, int endPos, KUndo2Command *parent = nullptr);
~KoSvgTextAddRemoveShapeCommandImpl();
void partA() override;
void partB() override;
@@ -44,4 +44,10 @@ public:
~KoSvgTextRemoveShapeCommand();
};
+class KRITAFLAKE_EXPORT KoSvgTextSetTextPathOnRangeCommand : public KoSvgTextAddRemoveShapeCommandImpl {
+public:
+ KoSvgTextSetTextPathOnRangeCommand(KoSvgTextShape *textShape, KoShape *shape, int startPos, int endPos, KUndo2Command *parentCommand = 0);
+ ~KoSvgTextSetTextPathOnRangeCommand();
+};
+
#endif // KOSVGTEXTADDREMOVESHAPECOMMANDS_H
diff --git a/libs/flake/tests/TestSvgTextShape.cpp b/libs/flake/tests/TestSvgTextShape.cpp
index d09ba92fdd..c69ad2e861 100644
--- a/libs/flake/tests/TestSvgTextShape.cpp
+++ b/libs/flake/tests/TestSvgTextShape.cpp
@@ -13,6 +13,7 @@
#include <kistest.h>
#include <KoSvgTextShape.h>
+#include <KoSvgTextShapeMarkupConverter.h>
#include <KoShape.h>
#include <KoPathShape.h>
#include <KoShapeGroup.h>
@@ -556,6 +557,52 @@ void TestSvgTextShape::testReorderShapesInside()
}
}
+void TestSvgTextShape::testTextPathOnRange_data()
+{
+ QTest::addColumn<QString>("svg");
+ QTest::addColumn<int>("startPos");
+ QTest::addColumn<int>("endPos");
+
+ QTest::addRow("Single node, full range") << "<text>Test</text>" << 0 << 4;
+ QTest::addRow("Single node, front") << "<text>Test test</text>" << 0 << 4;
+ QTest::addRow("Single node, back") << "<text>Test test</text>" << 4 << 10;
+ QTest::addRow("Complex text") << "<text>Text <tspan fill=\"#ff0000\">path</tspan> test</text>" << 6 << 12;
+
+ QTest::addRow("Complex with paths") << "<text>"
+ "<textPath path=\"M0 0L10 10\">Text </textPath>"
+ "<textPath path=\"M0 0L10 10\" fill=\"#ff0000\">path</textPath>"
+ "<textPath path=\"M0 0L10 10\"> test</textPath>"
+ "</text>" << 2 << 14;
+ QTest::addRow("Complex with paths and deep tree")
+ << "<text>"
+ "<textPath path=\"M0 0L10 10\">The quick <tspan fill=\"#aa8800\">brown </tspan></textPath>"
+ "<textPath path=\"M0 0L10 10\" fill=\"#ff0000\">fox</textPath>"
+ "<textPath path=\"M0 0L10 10\"> jumps <tspan fill=\"#00ff00\">over the lazy dog</tspan></textPath>"
+ "</text>" << 14 << 26;
+}
+
+void TestSvgTextShape::testTextPathOnRange()
+{
+ QFETCH(QString, svg);
+ QFETCH(int, startPos);
+ QFETCH(int, endPos);
+ KoSvgTextShape *textShape = new KoSvgTextShape();
+ KoSvgTextShapeMarkupConverter converter(textShape);
+ converter.convertFromSvg(svg, QString(), QRectF(0, 0, 300, 300), 72.0);
+ KoPathShape *path = createPolygon();
+ KoSvgTextSetTextPathOnRangeCommand *cmd = new KoSvgTextSetTextPathOnRangeCommand(textShape, path, startPos, endPos);
+
+ cmd->redo();
+
+ KoSvgTextNodeIndex idx = textShape->topLevelNodeForPos((startPos+endPos)/2);
+
+ KoPathShape *textPath = dynamic_cast<KoPathShape*>(idx.textPath());
+ QVERIFY(textPath);
+ QCOMPARE(textPath->toString(), path->toString());
+
+ //TODO test undo.
+}
+
KISTEST_MAIN(TestSvgTextShape)
#include "TestSvgTextShape.moc"
diff --git a/libs/flake/tests/TestSvgTextShape.h b/libs/flake/tests/TestSvgTextShape.h
index 1406bc15f2..312d4efc9c 100644
--- a/libs/flake/tests/TestSvgTextShape.h
+++ b/libs/flake/tests/TestSvgTextShape.h
@@ -30,6 +30,9 @@ private Q_SLOTS:
void testReorderShapesInside_data();
void testReorderShapesInside();
+ void testTextPathOnRange_data();
+ void testTextPathOnRange();
+
};
#endif // TESTSVGTEXTSHAPE_H
diff --git a/libs/flake/text/KoSvgTextLoader.cpp b/libs/flake/text/KoSvgTextLoader.cpp
index e302c918ef..ffe241099a 100644
--- a/libs/flake/text/KoSvgTextLoader.cpp
+++ b/libs/flake/text/KoSvgTextLoader.cpp
@@ -118,6 +118,22 @@ void KoSvgTextLoader::setStyleInfo(KoShape *s)
void KoSvgTextLoader::setTextPathOnCurrentNode(KoShape *s)
{
if (!KisForestDetail::isEnd(d->currentNode) && s) {
+ if (s->name().isEmpty()) {
+ int count = 0;
+ while(s->name().isEmpty()) {
+ QString newName("path"+QString::number(count));
+ bool nameIsUnique = true;
+ Q_FOREACH(KoShape *shape, d->shape->d->textPaths) {
+ if (shape->name() == newName) {
+ nameIsUnique = false;
+ }
+ }
+ if(nameIsUnique) {
+ s->setName(newName);
+ }
+ count+=1;
+ }
+ }
d->currentNode->textPathId = s->name();
s->addDependee(d->shape);
d->shape->d->textPaths.append(s);
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cpp
index b8528a3ba7..12dd34774d 100644
--- a/libs/flake/text/KoSvgTextShape.cpp
+++ b/libs/flake/text/KoSvgTextShape.cpp
@@ -72,7 +72,6 @@ public:
}
~KoSvgTextShapeMementoImpl() {
- qDeleteAll(textPaths);
}
private:
@@ -92,9 +91,12 @@ private:
};
struct KoSvgTextNodeIndex::Private {
- Private(KisForest<KoSvgTextContentElement>::child_iterator _textElement)
- :textElement(_textElement) {}
+ Private(KisForest<KoSvgTextContentElement>::child_iterator _textElement, KoShape *_textPath)
+ :textElement(_textElement)
+ , textPath(_textPath){}
KisForest<KoSvgTextContentElement>::child_iterator textElement;
+ // textPath is owned by its textshape.
+ KoShape *textPath = nullptr;
};
// for the use in KoSvgTextShape::Private::createTextNodeIndex() only
@@ -103,10 +105,10 @@ KoSvgTextNodeIndex::KoSvgTextNodeIndex()
{
}
-KoSvgTextNodeIndex KoSvgTextShape::Private::createTextNodeIndex(KisForest<KoSvgTextContentElement>::child_iterator textElement)
+KoSvgTextNodeIndex KoSvgTextShape::Private::createTextNodeIndex(KisForest<KoSvgTextContentElement>::child_iterator textElement) const
{
KoSvgTextNodeIndex index;
- index.d.reset(new KoSvgTextNodeIndex::Private(textElement));
+ index.d.reset(new KoSvgTextNodeIndex::Private(textElement, Private::textPathByName(textElement->textPathId, textPaths)));
return index;
}
@@ -128,8 +130,7 @@ KoSvgText::TextOnPathInfo *KoSvgTextNodeIndex::textPathInfo() {
}
KoShape *KoSvgTextNodeIndex::textPath() {
- // TODO: implement this properly in the text-in-shape branch.
- return nullptr;
+ return d->textPath;
}
@@ -1432,15 +1433,15 @@ KoSvgTextNodeIndex KoSvgTextShape::findNodeIndexForPropertyId(KoSvgTextPropertie
{
for (auto it = d->textData.childBegin(); it != d->textData.childEnd(); it++) {
if (it->properties.hasProperty(propertyId)) {
- return Private::createTextNodeIndex(it);
+ return d->createTextNodeIndex(it);
} else if (KisForestDetail::childBegin(it) != KisForestDetail::childEnd(it)) {
auto found = findNodeIndexForPropertyIdImpl(it, propertyId);
if (found != it) {
- return Private::createTextNodeIndex(found);
+ return d->createTextNodeIndex(found);
}
}
}
- return Private::createTextNodeIndex(d->textData.childBegin());
+ return d->createTextNodeIndex(d->textData.childBegin());
}
QPair<int, int> KoSvgTextShape::findRangeForNodeIndex(const KoSvgTextNodeIndex &node) const
@@ -1455,6 +1456,16 @@ QPair<int, int> KoSvgTextShape::findRangeForNodeIndex(const KoSvgTextNodeIndex &
return qMakePair(posForIndex(startIndex), posForIndex(endIndex));
}
+KoSvgTextNodeIndex KoSvgTextShape::topLevelNodeForPos(int pos) const
+{
+ const int finalPos = d->cursorPos.size() - 1;
+ const int index = d->cursorPos.at(qBound(0, pos, finalPos)).index;
+ int currentIndex = 0;
+ auto element = KisForestDetail::siblingCurrent(Private::findTextContentElementForIndex(d->textData, currentIndex, index, true));
+ if (element == d->textData.childBegin()) return d->createTextNodeIndex(d->textData.childBegin());
+ return d->createTextNodeIndex(Private::findTopLevelParent(d->textData.childBegin(), element));
+}
+
KoSvgTextProperties KoSvgTextShape::textProperties() const
{
return KisForestDetail::size(d->textData)? d->textData.childBegin()->properties: KoSvgTextProperties();
@@ -1808,6 +1819,8 @@ void KoSvgTextShape::setMementoImpl(const KoSvgTextShapeMementoSP memento)
d->plainText = impl->plainText;
d->isBidi = impl->isBidi;
d->initialTextPosition = impl->initialTextPosition;
+
+ // This is a problem :|
d->textPaths = impl->textPaths;
Q_FOREACH(KoShape * shape, d->textPaths) {
if (!shape->hasDependee(this)) {
@@ -1926,7 +1939,8 @@ QPainterPath KoSvgTextShape::outline() const {
Q_FOREACH(KoShape *shape, d->internalShapes()) {
result.addPath(shape->transformation().map(shape->outline()));
}
- } else {
+ }
+ if ((d->shapesInside.isEmpty() && d->shapesSubtract.isEmpty())) {
for (auto it = d->textData.depthFirstTailBegin(); it != d->textData.depthFirstTailEnd(); it++) {
result.addPath(it->associatedOutline);
for (int i = 0; i < it->textDecorations.values().size(); ++i) {
@@ -1944,10 +1958,13 @@ QRectF KoSvgTextShape::outlineRect() const
QRectF KoSvgTextShape::boundingRect() const
{
+ QRectF result;
if (d->internalShapesPainter->contentRect().isValid()) {
- return d->internalShapesPainter->contentRect();
+ result |= d->internalShapesPainter->contentRect();
+ if (!(d->shapesInside.isEmpty() && d->shapesSubtract.isEmpty())) {
+ return result;
+ }
}
- QRectF result;
QList<KoShapeStrokeModelSP> parentStrokes;
for (auto it = d->textData.compositionBegin(); it != d->textData.compositionEnd(); it++) {
@@ -2139,12 +2156,11 @@ bool KoSvgTextShape::shapeInContours(KoShape *shape)
return (shape->parent() == d->shapeGroup.data());
}
-void KoSvgTextShape::removeShapesFromContours(QList<KoShape *> shapes, bool callUpdate)
+void KoSvgTextShape::removeShapesFromContours(QList<KoShape *> shapes, bool callUpdate, bool cleanup)
{
Q_FOREACH(KoShape *shape, shapes) {
if (shape) {
d->removeTextPathId(d->textData.childBegin(), shape->name());
- d->cleanUp(d->textData);
shape->removeDependee(this);
d->shapesInside.removeAll(shape);
d->shapesSubtract.removeAll(shape);
@@ -2153,6 +2169,9 @@ void KoSvgTextShape::removeShapesFromContours(QList<KoShape *> shapes, bool call
}
d->shapeGroup->removeShape(shape);
}
+ if (cleanup) {
+ d->cleanUp(d->textData);
+ }
if (callUpdate) {
notifyChanged(); // notify shape manager that our geometry has changed
d->updateTextWrappingAreas();
@@ -2175,6 +2194,100 @@ void KoSvgTextShape::moveShapeInsideToIndex(KoShape *shapeInside, const int inde
update();
}
+bool KoSvgTextShape::setTextPathOnRange(KoShape *textPath, const int startPos, const int endPos)
+{
+ const int finalPos = d->cursorPos.size() - 1;
+ const int startIndex = (startPos == endPos && startPos < 0)? 0: d->cursorPos.at(qBound(0, startPos, finalPos)).index;
+ const int endIndex = (startPos == endPos && startPos < 0)? finalPos: d->cursorPos.at(qBound(0, endPos, finalPos)).index;
+
+ Private::splitTree(d->textData, startIndex, false);
+ Private::splitTree(d->textData, endIndex, true);
+ int currentIndex = 0;
+
+ bool textPathNameUnique = false;
+ int textPathNumber = d->textPaths.size();
+ while(!textPathNameUnique) {
+ QString newTextPathName("textPath"+QString::number(textPathNumber));
+ textPathNameUnique = true;
+ Q_FOREACH(KoShape *shape, d->textPaths) {
+ if (shape->name() == newTextPathName) {
+ textPathNameUnique = false;
+ textPathNumber += 1;
+ break;
+ }
+ }
+ if (textPathNameUnique) {
+ textPath->setName(newTextPathName);
+ }
+ }
+ KoSvgTextContentElement textPathElement;
+ textPathElement.textPathId = textPath->name();
+ d->shapeGroup->addShape(textPath);
+ d->textPaths.append(textPath);
+ textPath->addDependee(this);
+
+
+ if (KisForestDetail::depth(d->textData) == 1) {
+ textPathElement.text = d->textData.childBegin()->text;
+ QList<KoSvgTextProperties::PropertyId> copyIds = {KoSvgTextProperties::TextDecorationLineId,
+ KoSvgTextProperties::TextDecorationColorId,
+ KoSvgTextProperties::TextDecorationStyleId};
+ Q_FOREACH(KoSvgTextProperties::PropertyId p, copyIds) {
+ if (d->textData.childBegin()->properties.hasProperty(KoSvgTextProperties::TextDecorationLineId)) {
+ textPathElement.properties.setProperty(p, d->textData.childBegin()->properties.property(p));
+ d->textData.childBegin()->properties.removeProperty(p);
+ }
+ }
+
+ d->textData.childBegin()->text = QString();
+ d->textData.insert(childEnd(d->textData.childBegin()), textPathElement);
+ } else {
+ // find nodes
+ auto startElement = Private::findTextContentElementForIndex(d->textData, currentIndex, startIndex, true);
+ currentIndex = 0;
+ auto endElement = Private::findTextContentElementForIndex(d->textData, currentIndex, endIndex, true);
+
+ auto first = startElement.node()?Private::findTopLevelParent(d->textData.childBegin(), KisForestDetail::siblingCurrent(startElement))
+ : childEnd(d->textData.childBegin());
+ auto last = endElement.node()? Private::findTopLevelParent(d->textData.childBegin(), KisForestDetail::siblingCurrent(endElement))
+ : childEnd(d->textData.childBegin());
+
+ // move children. we collect them before inserting the text path,
+ // so we don't get iterator issues.
+ KisForest<KoSvgTextContentElement> textPathTree;
+ auto textPathIt = textPathTree.insert(
+ textPathTree.childEnd(),
+ textPathElement);
+ QVector<KisForest<KoSvgTextContentElement>::child_iterator> movableChildren;
+ for (auto child = first;
+ (child != last && child != childEnd(d->textData.childBegin()));
+ child++) {
+ if (!child->textPathId.isEmpty()) {
+ Q_FOREACH(KoShape *shape, d->textPaths) {
+ if (shape->name() == child->textPathId) {
+ removeShapesFromContours({shape}, false, false);
+ break;
+ }
+ }
+ child->textPathId = QString();
+ }
+ movableChildren.append(child);
+ }
+ while (!movableChildren.isEmpty()) {
+ auto child = movableChildren.takeLast();
+ textPathTree.move(child, KisForestDetail::childBegin(textPathIt));
+ }
+ d->textData.move(textPathIt, last);
+ }
+ Private::cleanUp(d->textData);
+
+ d->updateInternalShapesList();
+ notifyChanged();
+ shapeChangedPriv(ContentChanged);
+ update();
+ return true;
+}
+
QList<KoShape *> KoSvgTextShape::shapesInside() const
{
return d->shapesInside;
diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.h
index bca212f7a1..6aaafedf61 100644
--- a/libs/flake/text/KoSvgTextShape.h
+++ b/libs/flake/text/KoSvgTextShape.h
@@ -20,6 +20,7 @@
class KoSvgTextShapeMemento;
class KoSvgTextNodeIndex;
struct KoSvgTextCharacterInfo;
+class KoPathShape;
typedef QSharedPointer<KoSvgTextShapeMemento> KoSvgTextShapeMementoSP;
#define KoSvgTextShape_SHAPEID "KoSvgTextShapeID"
@@ -118,8 +119,9 @@ public:
* Remove list of shapes from any of the internal lists.
* @param shapes -- shapes to remove.
* @param update -- whether to call an update.
+ * @param cleanup -- whether to cleanup the textdata.
*/
- void removeShapesFromContours(QList<KoShape*> shapes, bool callUpdate = true);
+ void removeShapesFromContours(QList<KoShape*> shapes, bool callUpdate = true, bool cleanup = true);
/**
* @brief moveShapeInsideToIndex
@@ -132,6 +134,17 @@ public:
*/
void moveShapeInsideToIndex(KoShape* shapeInside, const int index);
+ /**
+ * @brief setTextPathOnRange
+ * Set a text path on the specified range.
+ * In SVG text paths are always at the first child.
+ * @param textPath -- TextPath to set.
+ * @param posStart -- Start of the range, as cursor pos.
+ * @param posEnd -- End of the range, as cursor pos.
+ * @return whether successfull.
+ */
+ bool setTextPathOnRange(KoShape *textPath, const int startPos = -1, const int endPos = -1);
+
/**
* @brief shapesInside
* @return the list of shapes that make up the content area.
@@ -485,6 +498,16 @@ public:
*/
QPair<int, int> findRangeForNodeIndex(const KoSvgTextNodeIndex &node) const;
+ /**
+ * @brief topLevelNodeForPos
+ * Get, if possible, an index for the child element of the root at pos.
+ * If not possible, returns an index for the root. This is used primarily for text-on-path.
+ * @param pos -- cursor position at which to find the index.
+ * @return if the text only has a root, it will still return the index for the root,
+ * but otherwise it will return a child element of root at pos.
+ */
+ KoSvgTextNodeIndex topLevelNodeForPos(int pos) const;
+
/*--------------- Properties ---------------*/
KoSvgTextProperties textProperties() const;
diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.h
index 3327860490..e688e24fdb 100644
--- a/libs/flake/text/KoSvgTextShape_p.h
+++ b/libs/flake/text/KoSvgTextShape_p.h
@@ -733,9 +733,11 @@ public:
* split the contentElement in tree at index into two nodes.
* @param tree -- tree to work on.
* @param index -- index
+ * @param allowEmptyText -- when false, will return true without splitting
+ * the text content element, if the split text would be empty.
* @return whether it was successful.
*/
- static bool splitContentElement(KisForest<KoSvgTextContentElement> &tree, int index) {
+ static bool splitContentElement(KisForest<KoSvgTextContentElement> &tree, int index, bool allowEmptyText = true) {
int currentIndex = 0;
// If there's only a single root element, don't bother searching.
@@ -753,6 +755,10 @@ public:
int zero = 0;
duplicate.removeText(start, length);
+ if (!allowEmptyText && (duplicate.text.isEmpty() || length == 0)) {
+ return true;
+ }
+
// TODO: handle localtransforms better; annoyingly, this requires whitespace handling
if (siblingCurrent(contentElement) != tree.childBegin()
@@ -775,6 +781,83 @@ public:
return false;
}
+ /**
+ * @brief splitTree
+ * Split the whole hierarchy of nodes at the given index.
+ * @param tree - tree to split.
+ * @param index - index to split at.
+ * @param textPathAfterSplit - whether to put any found textPaths before or after the split.
+ */
+ static void splitTree(KisForest<KoSvgTextContentElement> &tree, int index, bool textPathAfterSplit) {
+ splitContentElement(tree, index, false);
+ int currentIndex = 0;
+ auto contentElement = depth(tree) == 1? tree.depthFirstTailBegin(): findTextContentElementForIndex(tree, currentIndex, index, true);
+
+ // We're either at the start or end.
+ if (contentElement == tree.depthFirstTailEnd()) return;
+ if (siblingCurrent(contentElement) == tree.childBegin()) return;
+
+ auto lastNode = siblingCurrent(contentElement);
+ for (auto parentIt = KisForestDetail::hierarchyBegin(siblingCurrent(contentElement));
+ parentIt != KisForestDetail::hierarchyEnd(siblingCurrent(contentElement)); parentIt++) {
+ if (lastNode == siblingCurrent(parentIt)) continue;
+ if (siblingCurrent(parentIt) == tree.childBegin()) {
+ break;
+ }
+
+ if (lastNode != childBegin(siblingCurrent(parentIt))) {
+ KoSvgTextContentElement duplicate = KoSvgTextContentElement();
+ duplicate.properties = parentIt->properties;
+ if (textPathAfterSplit) {
+ duplicate.textPathId = parentIt->textPathId;
+ duplicate.textPathInfo = parentIt->textPathInfo;
+ parentIt->textPathId = QString();
+ parentIt->textPathInfo = KoSvgText::TextOnPathInfo();
+ }
+ auto insert = siblingCurrent(parentIt);
+ insert ++;
+ auto it = tree.insert(insert, duplicate);
+
+ QVector<KisForest<KoSvgTextContentElement>::child_iterator> movableChildren;
+ for (auto child = lastNode; child != childEnd(siblingCurrent(parentIt)); child++) {
+ movableChildren.append(child);
+ }
+ while(!movableChildren.isEmpty()) {
+ auto child = movableChildren.takeLast();
+ tree.move(child, childBegin(it));
+ }
+ lastNode = it;
+ } else {
+ lastNode = siblingCurrent(parentIt);
+ }
+ }
+ }
+
+ /**
+ * @brief findTopLevelParent
+ * Returns the toplevel parent of child that is not root.
+ * @param root -- root under which the toplevel item is.
+ * @param child -- child for which to search the parent for.
+ * @return toplevel parent of child that is itself a child of root,
+ * will return childEnd(root) if the child isn't inside root.
+ */
+ static KisForest<KoSvgTextContentElement>::child_iterator findTopLevelParent(KisForest<KoSvgTextContentElement>::child_iterator root,
+ KisForest<KoSvgTextContentElement>::child_iterator child) {
+ // An earlier version of the code used Hierarchy iterator,
+ // but that had too many exceptions when the child was not in the root.
+ if (!child.node()) return childEnd(root);
+ if (KisForestDetail::parent(child) == root) return child;
+ for (auto rootChild = childBegin(root); rootChild != childEnd(root); rootChild++) {
+ for (auto leaf = KisForestDetail::tailSubtreeBegin(rootChild);
+ leaf != KisForestDetail::tailSubtreeEnd(rootChild); leaf++) {
+ if (siblingCurrent(leaf) == child) {
+ return rootChild;
+ }
+ }
+ }
+ return childEnd(root);
+ }
+
/**
* @brief collapsedWhiteSpacesForText
* This returns the collapsed spaces for a given piece of text, without transforms.
@@ -1244,7 +1327,7 @@ public:
return false;
}
- static KoSvgTextNodeIndex createTextNodeIndex(KisForest<KoSvgTextContentElement>::child_iterator textElement);
+ KoSvgTextNodeIndex createTextNodeIndex(KisForest<KoSvgTextContentElement>::child_iterator textElement) const;
/**
* @brief removeTextPathId
diff --git a/libs/flake/tools/KoPathTool.cpp b/libs/flake/tools/KoPathTool.cpp
index f4ac88a664..a393c0400a 100644
--- a/libs/flake/tools/KoPathTool.cpp
+++ b/libs/flake/tools/KoPathTool.cpp
@@ -835,8 +835,6 @@ KoPathTool::PathSegment* KoPathTool::segmentAtPoint(const QPointF &point)
{
// the max allowed distance from a segment
const QRectF grabRoi = handleGrabRect(point);
- const qreal distanceThreshold = 0.5 * KisAlgebra2D::maxDimension(grabRoi);
-
QScopedPointer<PathSegment> segment(new PathSegment);
Q_FOREACH (KoPathShape *shape, m_pointSelection.selectedShapes()) {
@@ -844,30 +842,11 @@ KoPathTool::PathSegment* KoPathTool::segmentAtPoint(const QPointF &point)
if (parameterShape && parameterShape->isParametricShape())
continue;
- // convert document point to shape coordinates
- const QPointF p = shape->documentToShape(point);
- // our region of interest, i.e. a region around our mouse position
- const QRectF roi = shape->documentToShape(grabRoi);
-
- qreal minDistance = std::numeric_limits<qreal>::max();
-
- // check all segments of this shape which intersect the region of interest
- const QList<KoPathSegment> segments = shape->segmentsAt(roi);
-
- foreach (const KoPathSegment &s, segments) {
- const qreal nearestPointParam = s.nearestPoint(p);
- const QPointF nearestPoint = s.pointAt(nearestPointParam);
- const qreal distance = kisDistance(p, nearestPoint);
-
- // are we within the allowed distance ?
- if (distance > distanceThreshold)
- continue;
- // are we closer to the last closest point ?
- if (distance < minDistance) {
- segment->path = shape;
- segment->segmentStart = s.first();
- segment->positionOnSegment = nearestPointParam;
- }
+ const KoPathSegment s = shape->segmentAtPoint(point, grabRoi);
+ if (s.isValid()) {
+ segment->path = shape;
+ segment->segmentStart = s.first();
+ segment->positionOnSegment = s.nearestPoint(shape->documentToShape(point));
}
}
diff --git a/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp b/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp
index 82d9b0b636..4a6ee5c471 100644
--- a/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp
+++ b/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp
@@ -25,6 +25,9 @@
#include "kis_global.h"
#include "kundo2command.h"
+#include <KoPathShape.h>
+#include <KoPathSegment.h>
+
SvgCreateTextStrategy::SvgCreateTextStrategy(SvgTextTool *tool, const QPointF &clicked, KoShape *shape)
: KoInteractionStrategy(tool)
, m_dragStart(clicked)
@@ -145,7 +148,14 @@ KUndo2Command *SvgCreateTextStrategy::createCommand()
if (m_flowShape) {
textShape->setPosition(m_flowShape->absolutePosition(KoFlake::TopLeft));
- new KoSvgTextAddShapeCommand(textShape, m_flowShape, true, parentCommand);
+
+ KoPathShape *path = dynamic_cast<KoPathShape*>(m_flowShape);
+ if (path && path->segmentAtPoint(m_dragStart, tool->handleGrabRect(m_dragStart)).isValid()) {
+ int pos = textShape->posForIndex(textShape->plainText().size());
+ new KoSvgTextSetTextPathOnRangeCommand(textShape, m_flowShape, 0, pos, parentCommand);
+ } else {
+ new KoSvgTextAddShapeCommand(textShape, m_flowShape, true, parentCommand);
+ }
}
new KoKeepShapesSelectedCommand({}, {textShape}, tool->canvas()->selectedShapesProxy(), true, parentCommand);
diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cpp
index 5c5d000e59..556ed47a09 100644
--- a/plugins/tools/svgtexttool/SvgTextTool.cpp
+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp
@@ -944,7 +944,11 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)
}
} else if (hoveredFlowShape) {
m_hoveredShapeHighlightRect.addPath(hoveredFlowShape->absoluteTransformation().map(hoveredFlowShape->outline()));
- cursor = m_text_in_shape;
+ if (hoveredFlowShape->segmentAtPoint(event->point, handleGrabRect(event->point)).isValid()) {
+ cursor = m_text_on_path;
+ } else {
+ cursor = m_text_in_shape;
+ }
} else if (!hoverPath.isEmpty() && shapeType == KoSvgTextShape_SHAPEID && m_highlightItem == HighlightItem::None) {
m_hoveredShapeHighlightRect = hoverPath;
if (isHorizontal) {