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

Merge "Text in Shape" feature branch

Dmitry Kazakovbc6e4f12efba
diff --cc libs/global/kis_algebra_2d.cpp
index f77cf3719e,120a389be6..6ffbfbca81
--- a/libs/global/kis_algebra_2d.cpp
+++ b/libs/global/kis_algebra_2d.cpp
@@@ -1561,633 -1386,87 +1561,715 @@@ qreal pointToLineDistSquared(const QPoi
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;
+ }
+
++
+QList<QLineF> getParallelLines(const QLineF& line, const qreal distance) {
+
+ QPointF lineNormalVector = QPointF(line.dy(), -line.dx());
+ QLineF line1 = QLineF(movePointInTheDirection(line.p1(), lineNormalVector, distance), movePointInTheDirection(line.p2(), lineNormalVector, distance));
+ QLineF line2 = QLineF(movePointInTheDirection(line.p1(), -lineNormalVector, distance), movePointInTheDirection(line.p2(), -lineNormalVector, distance));
+
+ if (line1.isNull() || line2.isNull()) {
+ return QList<QLineF>();
+ }
+ return {line1, line2};
+}
+
-
-
+QPainterPath getOnePathFromRectangleCutThrough(const QList<QPointF> &points, const QLineF &line, bool left) {
+
+ auto onTheLine = [](QPointF first, QPointF second) {
+ float delta = 0.1f;
+ return qAbs(first.x() - second.x()) < delta || qAbs(first.y() - second.y()) < delta;
+ };
+
+ bool started = false;
+ QPainterPath path;
+
+ path.moveTo(line.p1());
+ path.lineTo(line.p2());
+
+ QList<QPointF> availablePoints;
+ int maxRectPointsNumber = 4; // in case the list has five points to count the first one twice
+ for(int i = 0; i < maxRectPointsNumber; i++) {
+ qreal whichSide = KisAlgebra2D::crossProduct(line.p2() - line.p1(), line.p2() - points[i]);
+ if (whichSide*(left ? 1 : -1) >= 0) {
+ availablePoints << points[i];
+ }
+ }
+
+ int startValue, increment;
+ if (!left) {
+ startValue = 2*availablePoints.length();
+ increment = -1;
+ } else {
+ startValue = 0;
+ increment = 1;
+ }
+
+ auto stopFor = [](int index, int max, bool left) {
+ if (!left) {
+ return index <= 0;
+ } else {
+ return index >= max;
+ }
+ };
+
+ for (int i = startValue; !stopFor(i, 2*availablePoints.length(), left); i += increment) {
+ int index = KisAlgebra2D::wrapValue(i, 0, (int)availablePoints.length());
+
+ if (onTheLine(availablePoints[index], path.currentPosition())) {
+ if (!started) {
+ started = true;
+
+ // TODO: if it's not the same point?
+ path.lineTo(availablePoints[index]);
+ if (onTheLine(availablePoints[index], line.p1())) {
+ break;
+ }
+
+ } else {
+ // usually would add, unless it's the ending
+ if (onTheLine(availablePoints[index], line.p1())) {
+ path.lineTo(availablePoints[index]);
+ break;
+ }
+ path.lineTo(availablePoints[index]);
+ }
+ }
+ }
+
+
+ path.lineTo(line.p1());
+
+ return path;
+}
+
+QList<QPainterPath> getPathsFromRectangleCutThrough(const QRectF &rect, const QLineF &leftLine, const QLineF &rightLine) {
+
+
+ QPainterPath left;
+ QPainterPath right;
+
+ left.moveTo(leftLine.p1());
+ left.lineTo(leftLine.p2());
+
+ right.moveTo(rightLine.p1());
+ right.lineTo(rightLine.p2());
+
+ QList<QPointF> rectPoints;
+ rectPoints << rect.topLeft() << rect.bottomLeft() << rect.bottomRight() << rect.topRight();
+
+ left = getOnePathFromRectangleCutThrough(rectPoints, leftLine, true);
+ right = getOnePathFromRectangleCutThrough(rectPoints, rightLine, false);
+
+ QList<QPainterPath> paths;
+ paths << left << right;
+
+
+ return paths;
+}
+
+QPointF findNearestPointOnLine(const QPointF &point, const QLineF &line, bool unbounded)
+{
+ // TODO: can we reuse (or deduplicate with) KisBezierUtils.cpp:nearestPoint()?
+
+ if (line.length() == 0) {
+ return point;
+ }
+
+ QPointF lineVector = line.p2() - line.p1();
+ QPointF lineNormalVector = QPointF(-lineVector.y(), lineVector.x());
+
+ QLineF pointToLineNormalLine = QLineF(point, point + lineNormalVector);
+ QPointF result;
+ QLineF::IntersectionType intersectionType = pointToLineNormalLine.intersects(line, &result);
+ if (unbounded || intersectionType == QLineF::IntersectionType::BoundedIntersection) {
+ return result;
+ }
+ qreal distance1 = kisDistance(line.p1(), result);
+ qreal distance2 = kisDistance(line.p2(), result);
+ if (distance1 + distance2 <= line.length() || qFuzzyCompare(distance1 + distance2, line.length())) {
+ return result; // it's still on the line
+ }
+ if (distance1 < distance2) {
+ return line.p1();
+ }
+ return line.p2();
+}
+
+QPointF movePointInTheDirection(const QPointF &point, const QPointF &direction, qreal distance)
+{
+ QPointF response = point;
+ QLineF line = QLineF(QPointF(0, 0), direction);
+ return response + distance*direction/line.length();
+}
+
+QPointF movePointAlongTheLine(const QPointF &point, const QLineF &direction, qreal distance, bool ensureOnLine)
+{
+ QPointF response = point;
+ if (ensureOnLine) {
+ response = findNearestPointOnLine(point, direction);
+ }
+ return movePointInTheDirection(response, direction.p2() - direction.p1(), distance);
+}
+
+
+
+QLineF getLineFromElements(const QPainterPath &shape, int index)
+{
+ QPainterPath::Element element1 = shape.elementAt(wrapValue(index, 0, shape.elementCount() - 1));
+ QPainterPath::Element element2 = shape.elementAt(wrapValue(index + 1, 0, shape.elementCount() - 1));
+ // it doesn't handle isCurveTo, just assumes it's a line... sorry!
+
+ return QLineF(element1, element2);
+}
+
+// local
+QLineF reverseDirection(const QLineF& line)
+{
+ return QLineF(line.p2(), line.p1());
+}
+
+// local
+QPainterPath removeGutterOneEndSmart(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2, QLineF middleLine, bool reverseFirstPoly, bool reverseSecondPoly)
+{
+ // moving from shape1.index1.p1 to shape1.index2.p2
+
+ auto reverseIfNeeded = [] (const QLineF &line, bool reverse) {
+ if (reverse) {
+ return reverseDirection(line);
+ }
+ return line;
+ };
+
+ QLineF line1 = reverseIfNeeded(getLineFromElements(shape1, index1), reverseFirstPoly);
+ QLineF line2 = reverseIfNeeded(getLineFromElements(shape2, index2), reverseSecondPoly);
+
+ QLineF leftLine;
+ QLineF rightLine;
+
+ int indexMultiplierFirst = reverseFirstPoly ? -1 : 1;
+ int indexMultiplierSecond = reverseSecondPoly ? -1 : 1;
+
+ leftLine = reverseIfNeeded(getLineFromElements(shape1, index1 - indexMultiplierFirst*1), reverseFirstPoly);
+ rightLine = reverseIfNeeded(getLineFromElements(shape2, index2 + indexMultiplierSecond*1), reverseSecondPoly);
+
+ qreal leftPointDistanceFromMiddle;
+ qreal rightPointDistanceFromMiddle;
+
+ QPointF leftPoint = line1.p1();
+ QPointF rightPoint = line2.p2();
+
+ leftPointDistanceFromMiddle = kisDistanceToLine(leftPoint, middleLine);
+ rightPointDistanceFromMiddle = kisDistanceToLine(rightPoint, middleLine);
+
+ QPainterPath simpleResult;
+ simpleResult.moveTo(leftPoint);
+ simpleResult.lineTo(rightPoint);
+
+ auto makeTrianglePath = [leftPoint, rightPoint] (QPointF middle) {
+ QPainterPath result;
+ result.moveTo(leftPoint);
+ result.lineTo(middle);
+ result.lineTo(rightPoint);
+ return result;
+ };
+
+ QPointF intersectionPoint;
+ QLineF::IntersectionType intersectionType = leftLine.intersects(rightLine, &intersectionPoint);
+
+ qreal distanceToMiddle = 0;
+ if (intersectionType != QLineF::NoIntersection) {
+ distanceToMiddle = kisDistanceToLine(intersectionPoint, middleLine);
+ }
+
+
+ if (intersectionType == QLineF::NoIntersection || distanceToMiddle > qMax(leftPointDistanceFromMiddle, rightPointDistanceFromMiddle)) {
+
+ // first arg: bounded line, second: unbounded
+ boost::optional<QPointF> leftIntersection = intersectLines(line2, leftLine);
+ boost::optional<QPointF> rightIntersection = intersectLines(line1, rightLine);
+
+ if (leftIntersection.has_value()) {
+ return makeTrianglePath(leftIntersection.value());
+ } else if (rightIntersection.has_value()) {
+ return makeTrianglePath(rightIntersection.value());
+ } else {
+ return simpleResult;
+ }
+
+ }
+
+ QPainterPath betterResult = makeTrianglePath(intersectionPoint);
+
+ return betterResult;
+
+}
+
+QPainterPath simplifyShape(const QPainterPath& path) {
+
+ VectorPath vector(path);
+ return vector.trulySimplified().asPainterPath();
+}
+
+VectorPath mergeShapesWithGutter(const VectorPath& shape1, const VectorPath& shape2, const VectorPath& oneEnd, const VectorPath& otherEnd, int index1, int index2, bool reverseSecondPoly, bool isSameShape)
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ QList<VPoint> result;
+
+ result.append(shape1.pointAt(0));
+
+ auto appendFrom = [] (QList<VPoint>& res, int start, int end, const VectorPath& shape) {
+ for (int i = start; i < end; i++) {
+ QList<VPoint> segment = shape.segmentAt(i);
+ KIS_SAFE_ASSERT_RECOVER_RETURN(segment.length() == 2);
+ res.append(segment[1]);
+ }
+ };
+
+ VectorPath reversedShape2 = reverseSecondPoly ? shape2.reversed() : VectorPath(shape2);
+ int reversedIndex2 = reverseSecondPoly ? (shape2.segmentsCount() - index2 - 1) : index2;
+
+ if (isSameShape) {
+ int minIndex = qMin(index1, index2);
+ int maxIndex = qMax(index1, index2);
+
+ // the indexes define a way to cut the shape into two shapes
+ // and one of them is kind of more outer than the other
+ // (they could intersect but... that's the user problem at that point, they can fix it point by point)
+ // in normal situation, it's like an outer ring of edges and inner ring of edges (which might be just one edge, which is the most common case)
+ // and we can find that out by checking whether the ends of the selected edges are inside or outside of the shape
+ // and if they're inside a shape, we're assuming that's the resulting shape (and the other is discarded)
+ // no islands in comic panels on my watch!
+
+ QList<VPoint> result1;
+ // min index to max index
+ result1 << VPoint::moveTo(shape1.segmentAt(minIndex)[1].endPoint);
+ appendFrom(result1, minIndex + 1, maxIndex, shape1);
+
+ QList<VPoint> result2;
+ result2 << VPoint::moveTo(shape1.segmentAt(maxIndex)[1].endPoint);
+ appendFrom(result2, maxIndex + 1, shape1.segmentsCount(), shape1);
+ appendFrom(result2, 0, minIndex, shape1);
+
+
+ VectorPath minEnd = index1 > index2 ? oneEnd : otherEnd;
+ VectorPath maxEnd = index1 > index2 ? otherEnd : oneEnd;
+
+ appendFrom(result1, 0, minEnd.segmentsCount(), minEnd);
+ appendFrom(result2, 0, maxEnd.segmentsCount(), maxEnd);
+
+ VectorPath result1Vec(result1);
+ VectorPath result2Vec(result2);
+
+ QPolygonF result2Poly = result2Vec.asPainterPath().toFillPolygon();
+
+ // TODO: assumes winding fill
+ if (result2Poly.containsPoint(shape1.segmentAt(minIndex)[1].endPoint, Qt::WindingFill)) {
+ // could check all
+ return result2Vec;
+ }
+ return result1Vec;
+
+ } else {
+ appendFrom(result, 0, index1, shape1);
+ appendFrom(result, 0, oneEnd.segmentsCount(), oneEnd);
+ appendFrom(result, reversedIndex2 + 1, reversedShape2.segmentsCount(), reversedShape2);
+ appendFrom(result, 0, reversedIndex2, reversedShape2);
+ appendFrom(result, 0, otherEnd.segmentsCount(), otherEnd);
+ appendFrom(result, index1 + 1, shape1.segmentsCount(), shape1);
+ }
+
+ return VectorPath(result);
+
+}
+
+
+QPainterPath removeGutterSmart(const QPainterPath &shape1, int index1, const QPainterPath &shape2, int index2, bool isSameShape)
+{
+ if (index1 + 1 >= shape1.elementCount() || index2 + 1 >= shape2.elementCount()) {
+ return QPainterPath();
+ }
+
+ QLineF line1 = getLineFromElements(shape1, index1);
+ QLineF line2 = getLineFromElements(shape2, index2);
+
+ QVector<QPointF> poly1 = QVector<QPointF>() << line1.p1() << line1.p2() << line2.p1();
+ QVector<QPointF> poly2 = QVector<QPointF>() << line1.p2() << line2.p1() << line2.p2();
+ bool reverseSecondPoly = false;
+ if (polygonDirection(poly1) != polygonDirection(poly2)) {
+ line2 = QLineF(line2.p2(), line2.p1()); // quick fix to ensure that the shape will be correct (convex)
+ reverseSecondPoly = true;
+ }
+
+ QLineF middleLine = QLineF((line1.p1() + line2.p2())/2, (line1.p2() + line2.p1())/2);
+
+ // more difficult version:
+ QPainterPath oneEnd = removeGutterOneEndSmart(shape1, index1, shape2, index2, middleLine, false, reverseSecondPoly);
+ QPainterPath otherEnd = removeGutterOneEndSmart(shape2, index2, shape1, index1, middleLine, reverseSecondPoly, false);
+
+ VectorPath vectorShape1 = VectorPath(shape1);
+ VectorPath vectorShape2 = VectorPath(shape2);
+ VectorPath vectorOneEnd = VectorPath(oneEnd);
+ VectorPath vectorOtherEnd = VectorPath(otherEnd);
+
+ VectorPath vectorResultHere = mergeShapesWithGutter(vectorShape1, vectorShape2, vectorOneEnd, vectorOtherEnd, index1, index2, reverseSecondPoly, isSameShape);
+ return vectorResultHere.trulySimplified(1e-02).asPainterPath();
+
+
+
+}
+
+
+QList<int> getLineSegmentCrossingLineIndexes(const QLineF &line, const QPainterPath &shape)
+{
+ QList<int> indexes;
+ for (int i = 0; i < shape.elementCount() - 1; i++) { // last element would wrap around to be the first segment again
+ QLineF lineSegment = getLineFromElements(shape, i);
+ QPointF intersection;
+ QLineF::IntersectionType type = lineSegment.intersects(line, &intersection);
+ if (type == QLineF::IntersectionType::BoundedIntersection) {
+ indexes << i;
+ }
+ }
+ return indexes;
+}
+
+VectorPath::VectorPath(const QPainterPath &path)
+{
+ using VPoint = VectorPath::VectorPathPoint;
+
+ m_points = QList<VectorPath::VectorPathPoint>();
+ for (int i = 0; i < path.elementCount(); i++) {
+ QPainterPath::Element el = path.elementAt(i);
+ switch(el.type) {
+ case QPainterPath::MoveToElement:
+ m_points.append(VPoint(VPoint::MoveTo, el));
+ break;
+ case QPainterPath::LineToElement:
+ m_points.append(VPoint(VPoint::LineTo, el));
+ break;
+ case QPainterPath::CurveToElement:
+ KIS_SAFE_ASSERT_RECOVER(i + 2 < path.elementCount()) { continue; }
+ KIS_SAFE_ASSERT_RECOVER(path.elementAt(i + 1).type == QPainterPath::CurveToDataElement) { continue; }
+ KIS_SAFE_ASSERT_RECOVER(path.elementAt(i + 2).type == QPainterPath::CurveToDataElement) { continue; }
+ // Qt saves the data this way: [control point 1, CurveToElement] [control point 2, CurveToDataElement] [end point, CurveToDataElement]
+ m_points.append(VPoint(VPoint::BezierTo, path.elementAt(i + 2), el, path.elementAt(i + 1)));
+ i += 2; // skip next two points
+ break;
+ }
+ }
+}
+
+VectorPath::VectorPath(const QList<VectorPathPoint> path)
+{
+ m_points = path;
+}
+
+int VectorPath::pointsCount() const
+{
+ return m_points.length();
+}
+
+VectorPath::VectorPathPoint VectorPath::pointAt(int i) const
+{
+ return m_points[i];
+}
+
+int VectorPath::segmentsCount() const
+{
+ return m_points.length() - 1 >= 0 ? m_points.length() - 1 : 0;
+}
+
+QList<VectorPath::VectorPathPoint> VectorPath::segmentAt(int i) const
+{
+ QList<VectorPath::VectorPathPoint> response;
+ if (i < 0 || i >= segmentsCount()) {
+ return response;
+ }
+ response << m_points[i] << m_points[i + 1];
+ return response;
}
+
+QLineF VectorPath::segmentAtAsLine(int i) const
+{
+ QList<VectorPath::VectorPathPoint> points = segmentAt(i);
+ if (points.length() < 1) {
+ return QLineF();
+ }
+ return QLineF(points[0].endPoint, points[1].endPoint);
+}
+
+VectorPath VectorPath::trulySimplified(qreal epsDegrees) const
+{
+ qreal eps = kisDegreesToRadians(epsDegrees);
+
+
+ using VPoint = VectorPath::VectorPathPoint;
+
+ if (m_points.length() < 1) {
+ return VectorPath(QList<VectorPath::VectorPathPoint>());
+ }
+
+ QList<VPoint> response;
+ VPoint lineBeginPoint = VPoint(VPoint::MoveTo, QPointF(0, 0));
+ VPoint previousPoint = m_points[0];
+
+
+ //qreal eps = 1e-05;
+ auto onTheLine = [eps] (QPointF start, QPointF middle, QPointF end) {
+ QLineF line1(start, end);
+ QLineF line2(start, middle);
+
+ return (qAbs(crossProduct(end - start, middle - start)/line1.length()/line2.length()) < eps);
+ };
+
+ for (int i = 1; i < m_points.length(); i++) {
+ if (m_points[i].type == VPoint::MoveTo) {
+ if (previousPoint.type == VPoint::MoveTo) {
+ previousPoint = m_points[i]; // let's skip the previous point since they're both just moving
+ continue;
+ } else { // LineTo or BezierTo, both need to be added
+ response << previousPoint;
+ previousPoint = m_points[i];
+ continue;
+ }
+ } else if (m_points[i].type == VPoint::LineTo) {
+ if (previousPoint.type == VPoint::LineTo) {
+ // check whether they're parallel
+ if (!onTheLine(lineBeginPoint.endPoint, previousPoint.endPoint, m_points[i].endPoint)) {
+ response << previousPoint;
+ lineBeginPoint = previousPoint;
+ }
+ previousPoint = m_points[i]; // let's skip the previous point since they're parallel
+ continue;
+ } else { // MoveTo or BezierTo, both need to be added
+ response << previousPoint;
+ lineBeginPoint = previousPoint;
+ previousPoint = m_points[i];
+ continue;
+ }
+ } else { // currently in BezierTo
+ response << previousPoint;
+ previousPoint = m_points[i];
+ continue;
+ }
+ }
+ response << previousPoint;
+
+ // now eliminate additional point at the beginning of the shape if it's in the middle of a line
+ // and wasn't simplified only because it's at the beginning
+
+ if (response.length() < 3) {
+ return VectorPath(response);
+ }
+
+ VPoint start = response[0];
+ VPoint second = response[1];
+
+ if (start.type == VPoint::MoveTo) {
+ if (fuzzyPointCompare(previousPoint.endPoint, start.endPoint)) {
+ // they are the same point, now whether they're on the line
+ if (second.type == VPoint::LineTo && onTheLine(response[response.length()-2].endPoint, start.endPoint, second.endPoint)) {
+ response.pop_front();
+ response[0].type = VPoint::MoveTo;
+ response[response.length() - 1].endPoint = second.endPoint;
+ }
+ }
+
+ } else if (response[0].type == VPoint::LineTo) { // first point is then QPointF(0, 0)
+ if (qFuzzyIsNull(previousPoint.endPoint.x()) && qFuzzyIsNull(previousPoint.endPoint.y())) {
+ // they are the same point, now whether they're on the line
+ if (second.type == VPoint::LineTo && onTheLine(previousPoint.endPoint, QPointF(0, 0), start.endPoint)) {
+ // note: leaving this debug for future reference
+ qCritical() << "We're in this case";
+ //response.pop_front();
+ //response[0].type = VPoint::MoveTo;
+ //response[response.length() - 1].endPoint = start.endPoint;
+ }
+ }
+ }
+
+
+
+
+ return VectorPath(response);
+}
+
+VectorPath VectorPath::reversed() const
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ QList<VPoint> response;
+ response.append(VPoint(VPoint::MoveTo, m_points[m_points.length() - 1].endPoint));
+ for (int i = m_points.length() - 1; i > 0; i--) {
+ // to reverse a segment, we take the end VPoint and change the .endPoint to the start VPoint coords
+ VPoint point = m_points[i];
+ point.endPoint = m_points[i - 1].endPoint;
+ response.append(point);
+ }
+ return VectorPath(response);
+}
+
+bool VectorPath::fuzzyComparePointsCyclic(const VectorPath &path, qreal eps) const
+{
+ using VPoint = VectorPath::VectorPathPoint;
+
+ if (segmentsCount() != path.segmentsCount()) {
+ return false;
+ }
+
+ bool fuzzy = qFuzzyIsNull(eps);
+
+ auto comparePoints = [fuzzy, eps] (QPointF left, QPointF right) {
+ if (fuzzy) {
+ return fuzzyPointCompare(left, right);
+ } else {
+ return fuzzyPointCompare(left, right, eps);
+ }
+ };
+
+
+ VPoint leftStart = path.pointAt(0);
+ QList<int> rightStartingPoints;
+ for (int i = 0; i < path.pointsCount(); i++) {
+ if (comparePoints(leftStart.endPoint, path.pointAt(i).endPoint)) {
+ rightStartingPoints << i;
+ }
+ }
+
+ Q_FOREACH(int startId, rightStartingPoints) {
+ bool isEqual = true;
+ for (int i = 0; i < pointsCount(); i++) {
+ int rightId = wrapValue(i + startId, 0, pointsCount());
+ if (!comparePoints(pointAt(i).endPoint, path.pointAt(rightId).endPoint)) {
+ isEqual = false;
+ break;
+ }
+ }
+ if (isEqual) {
+ return true;
+ }
+ for (int i = 0; i < pointsCount(); i++) {
+ int rightId = wrapValue(startId - i, 0, pointsCount());
+ if (!comparePoints(pointAt(i).endPoint, path.pointAt(rightId).endPoint)) {
+ isEqual = false;
+ break;
+ }
+ }
+ if (isEqual) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+QPainterPath VectorPath::asPainterPath() const
+{
+ using VPoint = VectorPath::VectorPathPoint;
+ if (m_originalPath.elementCount() > 0) {
+ return m_originalPath;
+ }
+ QPainterPath response;
+ for (int i = 0; i < pointsCount(); i++) {
+ switch(m_points[i].type) {
+ case VPoint::MoveTo:
+ response.moveTo(m_points[i].endPoint);
+ break;
+ case VPoint::LineTo:
+ response.lineTo(m_points[i].endPoint);
+ break;
+ case VPoint::BezierTo:
+ response.cubicTo(m_points[i].controlPoint1, m_points[i].controlPoint2, m_points[i].endPoint);
+ break;
+ }
+ }
+ response.closeSubpath();
+ return response;
+}
+
+
+
+} // namespace
diff --cc libs/global/kis_algebra_2d.h
index 5404d82ef1,7cb6df5441..51709e909b
--- a/libs/global/kis_algebra_2d.h
+++ b/libs/global/kis_algebra_2d.h
@@@ -1042,52 -924,18 +1042,60 @@@ qreal KRITAGLOBAL_EXPORT findMinimumGol
// requirements: only one local minimum between xA and xB
// Golden Section is supposed to be usually faster than Ternary Section
// NOTE: tiar: this function was debugged and should be working correctly but is not used anywhere any longer
-qreal findMinimumTernarySection(std::function<qreal(qreal)> f, qreal xA, qreal xB, qreal eps, int maxIter);
+qreal KRITAGLOBAL_EXPORT findMinimumTernarySection(std::function<qreal(qreal)> f, qreal xA, qreal xB, qreal eps, int maxIter);
+// kis_global has the same function, this needs to be removed
qreal KRITAGLOBAL_EXPORT pointToLineDistSquared(const QPointF& pt, const QLineF& line);
+QList<QLineF> KRITAGLOBAL_EXPORT getParallelLines(const QLineF& line, const qreal distance);
+
+QPainterPath KRITAGLOBAL_EXPORT getOnePathFromRectangleCutThrough(const QList<QPointF> &points, const QLineF &line, bool left);
+
+///
+/// \brief getPathsFromRectangleCutThrough get paths defining both sides of a rectangle cut through using two (supposedly parallel) lines
+/// It is used in the Knife Tool
+/// If you just want to cut a rectangle, you can use the same line in both
+/// \param rect rectangle to cut
+/// \param leftLine left line of the rectangle (used for the left-(top) side of the rectangle
+/// \param rightLine right line of the rectangle (used for the right-(bottom) side of the rectangle
+/// \return
+///
+QList<QPainterPath> KRITAGLOBAL_EXPORT getPathsFromRectangleCutThrough(const QRectF &rect, const QLineF &leftLine, const QLineF &rightLine);
+
+
+// isAlgebra2D::getLineSegmentCrossingLineIndexes(QLineF const&, QPainterPath const&)
+QList<int> KRITAGLOBAL_EXPORT getLineSegmentCrossingLineIndexes(const QLineF &line, const QPainterPath& shape);
+
+///
+/// \brief removeGutterSmart
+/// \param shape1
+/// \param index1
+/// \param shape2
+/// \param index2
+/// \return
+///
+QPainterPath KRITAGLOBAL_EXPORT removeGutterSmart(const QPainterPath& shape1, int index1, const QPainterPath& shape2, int index2, bool isSameShape);
+
+
+///
+/// \brief mergeShapesWithGutter merges two shapes with a gutter shape (defined as two paths)
+/// \param shape1
+/// \param shape2
+/// \param oneEnd
+/// \param otherEnd
+/// \param index1 index of the segment in the first shape
+/// \param index2 index of the segment in the second shape
+/// \return
+///
+VectorPath mergeShapesWithGutter(const VectorPath& shape1, const VectorPath& shape2, const VectorPath& oneEnd, const VectorPath& otherEnd, int index1, int index2, bool reverseSecondPoly, bool isSameShape);
+ /**
+ * @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);
}