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++
Attempt to improve speed of text-in-shape.
Wolthera van Hövell tot Westerflier4e38c95e432e
diff --git a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cppindex 203e62fd77..477d328a3b 100644--- a/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp+++ b/libs/flake/text/KoSvgTextShapeLayoutFunc_inShape.cpp@@ -16,6 +16,7 @@#include <KoShapeStroke.h>#include <kis_global.h>+#include <kis_algebra_2d.h>#include <QPainter>#include <QtMath>@@ -56,6 +57,7 @@ getShapes(QList<KoShape *> shapesInside, QList<KoShape *> shapesSubtract, const} else {p = precisionTF.map(p);}+subtract.addPath(p);}}@@ -84,12 +86,25 @@ getShapes(QList<KoShape *> shapesInside, QList<KoShape *> shapesSubtract, const}p2.addPolygon(subpathPoly);}- shapes.append(precisionTF.inverted().map(p2));+ p2 = KisAlgebra2D::trySimplifyPath(precisionTF.inverted().map(p2), 1.0);+ p2.closeSubpath();+ shapes.append(p2);}}return shapes;}+bool pastTerminator(const QPointF point, const QPointF terminator, const KoSvgText::WritingMode writingMode) {+ if (writingMode == KoSvgText::HorizontalTB) {+ return (terminator.y() - point.y() < SHAPE_PRECISION);+ } else if (writingMode == KoSvgText::VerticalRL) {+ return (point.x() - terminator.x() < SHAPE_PRECISION);+ } else {+ return (terminator.x() - point.x() < SHAPE_PRECISION);+ }+ return false;+}+static bool getFirstPosition(QPointF &firstPoint,QPainterPath p,QRectF wordBox,@@ -123,6 +138,8 @@ static bool getFirstPosition(QPointF &firstPoint,QLineF line;line.setP1(polygon.at(i));line.setP2(polygon.at(i+1));+ if (!(pastTerminator(line.p1(), terminatorAdjusted, writingMode)+ || pastTerminator(line.p2(), terminatorAdjusted, writingMode))) continue;if (line.angle() == 0.0 || line.angle() == 180.0) {qreal offset = word.center().y();@@ -290,6 +307,8 @@ findLineBoxesForFirstPos(QPainterPath shape, QPointF firstPos, const QRectF wordQLineF bottomLine = baseLine.translated(lineBottom);for(int i = 0; i < polygon.size()-1; i++) {QLineF line(polygon.at(i), polygon.at(i+1));+ if (!(pastTerminator(line.p1(), topLine.center(), writingMode)+ || pastTerminator(line.p2(), topLine.center(), writingMode))) continue;bool addedA = false;QPointF intersectA;QPointF intersectB;@@ -331,8 +350,6 @@ findLineBoxesForFirstPos(QPainterPath shape, QPointF firstPos, const QRectF wordQVector<QPointF> relevant;for(int i = 0; i < polygon.size()-1; i++) {- QLineF edgeLine(polygon.at(i), polygon.at(i+1));-if (lineBox.contains(polygon.at(i))) {relevant.append(polygon.at(i));}diff --git a/libs/global/kis_algebra_2d.cpp b/libs/global/kis_algebra_2d.cppindex 99c2bd9ad8..120a389be6 100644--- a/libs/global/kis_algebra_2d.cpp+++ b/libs/global/kis_algebra_2d.cpp@@ -1386,4 +1386,87 @@ qreal pointToLineDistSquared(const QPointF &pt, const QLineF &line)return cross * cross / (line.dx() * line.dx() + line.dy() * line.dy());}+bool tryMergePoints(QPainterPath &path,+ const QPointF &startPoint,+ const QPointF &endPoint,+ qreal &distance,+ qreal distanceThreshold,+ bool lastSegment)+{+ qreal length = (endPoint - startPoint).manhattanLength();++ if (lastSegment || length > distanceThreshold) {+ if (lastSegment) {+ qreal wrappedLength =+ (endPoint - QPointF(path.elementAt(0))).manhattanLength();++ if (length < distanceThreshold ||+ wrappedLength < distanceThreshold) {++ return true;+ }+ }++ distance = 0;+ return false;+ }++ distance += length;++ if (distance > distanceThreshold) {+ path.lineTo(endPoint);+ distance = 0;+ }++ return true;+}++QPainterPath trySimplifyPath(const QPainterPath &path, qreal lengthThreshold)+{+ QPainterPath newPath;+ QPointF startPoint;+ qreal distance = 0;++ int count = path.elementCount();+ for (int i = 0; i < count; i++) {+ QPainterPath::Element e = path.elementAt(i);+ QPointF endPoint = QPointF(e.x, e.y);++ switch (e.type) {+ case QPainterPath::MoveToElement:+ newPath.moveTo(endPoint);+ break;+ case QPainterPath::LineToElement:+ if (!tryMergePoints(newPath, startPoint, endPoint,+ distance, lengthThreshold, i == count - 1)) {++ newPath.lineTo(endPoint);+ }+ break;+ case QPainterPath::CurveToElement: {+ Q_ASSERT(i + 2 < count);++ if (!tryMergePoints(newPath, startPoint, endPoint,+ distance, lengthThreshold, i == count - 1)) {++ e = path.elementAt(i + 1);+ Q_ASSERT(e.type == QPainterPath::CurveToDataElement);+ QPointF ctrl1 = QPointF(e.x, e.y);+ e = path.elementAt(i + 2);+ Q_ASSERT(e.type == QPainterPath::CurveToDataElement);+ QPointF ctrl2 = QPointF(e.x, e.y);+ newPath.cubicTo(ctrl1, ctrl2, endPoint);+ }++ i += 2;+ }+ default:+ ;+ }+ startPoint = endPoint;+ }++ return newPath;+}+}diff --git a/libs/global/kis_algebra_2d.h b/libs/global/kis_algebra_2d.hindex 58a08f6787..7cb6df5441 100644--- a/libs/global/kis_algebra_2d.h+++ b/libs/global/kis_algebra_2d.h@@ -928,6 +928,14 @@ qreal findMinimumTernarySection(std::function<qreal(qreal)> f, qreal xA, qreal xqreal KRITAGLOBAL_EXPORT pointToLineDistSquared(const QPointF& pt, const QLineF& line);+/**+ * @brief trySimplifyPath+ * Tries to simplify a QPainterPath+ * @param path -- path to simplify.+ * @param lengthThreshold -- length at which points get merged.+ * @return path with less nodes.+ */+QPainterPath KRITAGLOBAL_EXPORT trySimplifyPath(const QPainterPath &path, qreal lengthThreshold);}diff --git a/libs/image/krita_utils.cpp b/libs/image/krita_utils.cppindex d94436c072..53b4f20ae5 100644--- a/libs/image/krita_utils.cpp+++ b/libs/image/krita_utils.cpp@@ -193,89 +193,6 @@ namespace KritaUtilsreturn qMax(portion * maxDimension, minValue);}- bool tryMergePoints(QPainterPath &path,- const QPointF &startPoint,- const QPointF &endPoint,- qreal &distance,- qreal distanceThreshold,- bool lastSegment)- {- qreal length = (endPoint - startPoint).manhattanLength();-- if (lastSegment || length > distanceThreshold) {- if (lastSegment) {- qreal wrappedLength =- (endPoint - QPointF(path.elementAt(0))).manhattanLength();-- if (length < distanceThreshold ||- wrappedLength < distanceThreshold) {-- return true;- }- }-- distance = 0;- return false;- }-- distance += length;-- if (distance > distanceThreshold) {- path.lineTo(endPoint);- distance = 0;- }-- return true;- }-- QPainterPath trySimplifyPath(const QPainterPath &path, qreal lengthThreshold)- {- QPainterPath newPath;- QPointF startPoint;- qreal distance = 0;-- int count = path.elementCount();- for (int i = 0; i < count; i++) {- QPainterPath::Element e = path.elementAt(i);- QPointF endPoint = QPointF(e.x, e.y);-- switch (e.type) {- case QPainterPath::MoveToElement:- newPath.moveTo(endPoint);- break;- case QPainterPath::LineToElement:- if (!tryMergePoints(newPath, startPoint, endPoint,- distance, lengthThreshold, i == count - 1)) {-- newPath.lineTo(endPoint);- }- break;- case QPainterPath::CurveToElement: {- Q_ASSERT(i + 2 < count);-- if (!tryMergePoints(newPath, startPoint, endPoint,- distance, lengthThreshold, i == count - 1)) {-- e = path.elementAt(i + 1);- Q_ASSERT(e.type == QPainterPath::CurveToDataElement);- QPointF ctrl1 = QPointF(e.x, e.y);- e = path.elementAt(i + 2);- Q_ASSERT(e.type == QPainterPath::CurveToDataElement);- QPointF ctrl2 = QPointF(e.x, e.y);- newPath.cubicTo(ctrl1, ctrl2, endPoint);- }-- i += 2;- }- default:- ;- }- startPoint = endPoint;- }-- return newPath;- }-QList<QPainterPath> splitDisjointPaths(const QPainterPath &path){QList<QPainterPath> resultList;diff --git a/libs/image/krita_utils.h b/libs/image/krita_utils.hindex 768d620200..2483cdb54d 100644--- a/libs/image/krita_utils.h+++ b/libs/image/krita_utils.h@@ -41,7 +41,7 @@ namespace KritaUtilsQString KRITAIMAGE_EXPORT prettyFormatReal(qreal value);qreal KRITAIMAGE_EXPORT maxDimensionPortion(const QRectF &bounds, qreal portion, qreal minValue);- QPainterPath KRITAIMAGE_EXPORT trySimplifyPath(const QPainterPath &path, qreal lengthThreshold);+/*** Split a path \p path into a set of disjoint (non-intersectable)diff --git a/libs/image/tests/kis_gradient_painter_test.cpp b/libs/image/tests/kis_gradient_painter_test.cppindex f97b619309..1bb7b4244b 100644--- a/libs/image/tests/kis_gradient_painter_test.cpp+++ b/libs/image/tests/kis_gradient_painter_test.cpp@@ -20,6 +20,7 @@#include <resources/KoStopGradient.h>#include "krita_utils.h"+#include <kis_algebra_2d.h>#include <testutil.h>@@ -37,7 +38,7 @@ void KisGradientPainterTest::testSimplifyPath()path.addPolygon(selectionPolygon);QPainterPath simplifiedPath;- simplifiedPath = KritaUtils::trySimplifyPath(path, 10.0);+ simplifiedPath = KisAlgebra2D::trySimplifyPath(path, 10.0);QPainterPath ref;ref.moveTo(100,100);diff --git a/plugins/paintops/experiment/kis_experiment_paintop.cpp b/plugins/paintops/experiment/kis_experiment_paintop.cppindex e7673292e1..00677c858f 100644--- a/plugins/paintops/experiment/kis_experiment_paintop.cpp+++ b/plugins/paintops/experiment/kis_experiment_paintop.cpp@@ -21,6 +21,7 @@#include <kis_image.h>#include <kis_spacing_information.h>#include <krita_utils.h>+#include <kis_algebra_2d.h>KisExperimentPaintOp::KisExperimentPaintOp(const KisPaintOpSettingsSP settings, KisPainter *painter, KisNodeSP node, KisImageSP image)@@ -184,7 +185,7 @@ void KisExperimentPaintOp::paintLine(const KisPaintInformation &pi1, const KisPabounds |= m_path.boundingRect();qreal threshold = simplifyThreshold(bounds);- m_path = KritaUtils::trySimplifyPath(m_path, threshold);+ m_path = KisAlgebra2D::trySimplifyPath(m_path, threshold);}else {m_path = applyDisplace(m_path, m_displaceCoeff - length);