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

Close and merge KoPathShapes coming from QPainterPath

Agata Cackode81b35ee5bb
diff --git a/libs/flake/KoPathShape.cpp b/libs/flake/KoPathShape.cpp
index c8f4f12206..11cec612a0 100644
--- a/libs/flake/KoPathShape.cpp
+++ b/libs/flake/KoPathShape.cpp
@@ -1237,19 +1237,30 @@ KoPathShape * KoPathShape::createShapeFromPainterPath(const QPainterPath &path)
KoPathShape * shape = new KoPathShape();
int elementCount = path.elementCount();
+ QPointF lastTeleportedToPoint = QPointF();
for (int i = 0; i < elementCount; i++) {
QPainterPath::Element element = path.elementAt(i);
+ bool nextIsMove = (i == elementCount - 1) || (path.elementAt(i + 1).isMoveTo());
+ bool merge = nextIsMove && KisAlgebra2D::fuzzyPointCompare(lastTeleportedToPoint, QPointF(element.x, element.y));
+
switch (element.type) {
case QPainterPath::MoveToElement:
shape->moveTo(QPointF(element.x, element.y));
+ lastTeleportedToPoint = QPointF(element.x, element.y);
break;
case QPainterPath::LineToElement:
shape->lineTo(QPointF(element.x, element.y));
+ if (merge) {
+ shape->closeMerge();
+ }
break;
case QPainterPath::CurveToElement:
shape->curveTo(QPointF(element.x, element.y),
QPointF(path.elementAt(i + 1).x, path.elementAt(i + 1).y),
QPointF(path.elementAt(i + 2).x, path.elementAt(i + 2).y));
+ if (merge) {
+ shape->closeMerge();
+ }
break;
default:
continue;
diff --git a/libs/flake/tests/TestPathShape.cpp b/libs/flake/tests/TestPathShape.cpp
index 8563031af0..9237e05de2 100644
--- a/libs/flake/tests/TestPathShape.cpp
+++ b/libs/flake/tests/TestPathShape.cpp
@@ -12,6 +12,7 @@
#include "KoPathSegment.h"
#include <simpletest.h>
+Q_DECLARE_METATYPE(QPainterPath)
void TestPathShape::close()
{
@@ -769,4 +770,43 @@ void TestPathShape::closeMerge()
QVERIFY(path.outline() == ppath);
}
+
+
+void TestPathShape::createFromPainterPath_data()
+{
+ QTest::addColumn<QPainterPath>("path");
+ QTest::addColumn<QString>("expected");
+
+ //QTest::addRow() << path << expected;
+ QPainterPath path;
+ path.addPolygon(QPolygonF({QPointF(10, 10), QPointF(100, 10), QPointF(100, 200), QPointF(10, 200)}));
+ QTest::addRow("no last point") << path << "M10 10L100 10L100 200L10 200";
+
+ QPainterPath path2;
+ path2.addPolygon(QPolygonF({QPointF(10, 10), QPointF(100, 10), QPointF(100, 200), QPointF(10, 200), QPointF(10, 10)}));
+ QTest::addRow("last point exists") << path2 << "M10 10L100 10L100 200L10 200Z";
+
+ QPainterPath path3;
+ path3.lineTo(QPointF(100, 0));
+ path3.lineTo(QPointF(100, 200));
+ path3.lineTo(QPointF(0, 200));
+ path3.lineTo(QPointF(0, 0));
+
+ QTest::addRow("no MoveTo point in the beginning (in theory, at least)") << path3 << "M0 0L100 0L100 200L0 200Z";
+}
+
+
+void TestPathShape::createFromPainterPath()
+{
+ QFETCH(QPainterPath, path);
+ QFETCH(QString, expected);
+
+ QScopedPointer<KoPathShape> result(KoPathShape::createShapeFromPainterPath(path));
+
+ QCOMPARE(result->outline(), path);
+ QCOMPARE(result->toString(), expected);
+}
+
+
+
SIMPLE_TEST_MAIN(TestPathShape)
diff --git a/libs/flake/tests/TestPathShape.h b/libs/flake/tests/TestPathShape.h
index 30b53bb61d..00d706a8a1 100644
--- a/libs/flake/tests/TestPathShape.h
+++ b/libs/flake/tests/TestPathShape.h
@@ -37,6 +37,9 @@ private Q_SLOTS:
void closeMerge();
void koPathPointDataLess();
+
+ void createFromPainterPath_data();
+ void createFromPainterPath();
};
#endif // TESTPATHSHAPE_H