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++
Make the text contour outline decorations nicer.
Wolthera van Hövell tot Westerflier45626298d89d
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex d53c9a4f8d..a30347fc8a 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -1923,6 +1923,11 @@ bool KoSvgTextShape::fontMatchingDisabled() constreturn d->disableFontMatching;}+QList<QPainterPath> KoSvgTextShape::generateTextAreas(const QList<KoShape *> shapesInside, const QList<KoShape *> shapesSubtract, const KoSvgTextProperties &props)+{+ return Private::generateShapes(shapesInside, shapesSubtract, props);+}+void KoSvgTextShape::paint(QPainter &painter) const{painter.save();diff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex 6aaafedf61..cec65af02a 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -508,6 +508,17 @@ public:*/KoSvgTextNodeIndex topLevelNodeForPos(int pos) const;+ /**+ * @brief nodeForTextPath+ * TextPaths are set on toplevel content elements. This function+ * allows for searching which node has said textPath, which should be+ * less clunky than running topLevelNodeForPos for every pos.+ * @param textPath -- the text path to find the node for.+ * @return toplevel node that has the given shape set at it's text path.+ * Returns the root node when none is found.+ */+ KoSvgTextNodeIndex nodeForTextPath(KoShape *textPath) const;+/*--------------- Properties ---------------*/KoSvgTextProperties textProperties() const;@@ -617,6 +628,17 @@ public:*/bool fontMatchingDisabled() const;+ /**+ * @brief generateTextAreas+ * Generates text areas with the given shapes and properties.+ * This is used to paint previews in the PaddingMargin strategy.+ * @param shapesInside -- the shapes inside to compute the text areas from.+ * @param shapesSubtract -- the subtract shapes to remove from the text areas.+ * @param props -- the properties to use. ShapePadding and ShapeMargin id will be taken from this.+ * @return list of QPainterPaths.+ */+ static QList<QPainterPath> generateTextAreas(const QList<KoShape*> shapesInside, const QList<KoShape*> shapesSubtract, const KoSvgTextProperties &props);+protected:void shapeChanged(ChangeType type, KoShape *shape) override;diff --git a/libs/flake/text/KoSvgTextShapeOutlineHelper.cpp b/libs/flake/text/KoSvgTextShapeOutlineHelper.cppindex 117abfeef2..c4cd76f5a8 100644--- a/libs/flake/text/KoSvgTextShapeOutlineHelper.cpp+++ b/libs/flake/text/KoSvgTextShapeOutlineHelper.cpp@@ -32,6 +32,7 @@ struct KoSvgTextShapeOutlineHelper::Private {bool drawBoundingRect = true;bool drawTextWrappingArea = false;bool drawOutline = false;+ bool textWrappingAreasHovered = false;KoSvgTextShape *getTextModeShape() {return canvas->textShapeManagerEnabled();@@ -76,6 +77,45 @@ KoSvgTextShapeOutlineHelper::~KoSvgTextShapeOutlineHelper()}+QList<QLineF> getTextAreaOrderArrows(QList<QPainterPath> areas) {+ QList<QLineF> lines;+ if (areas.size() <= 1) return lines;+ for (int i = 1; i < areas.size(); i++) {+ const QPainterPath previous = areas.at(i-1);+ const QPainterPath next = areas.at(i);+ const bool overlap = previous.intersects(next);+ QLineF arrow(previous.boundingRect().center(), next.boundingRect().center());+ if (!overlap) {++ Q_FOREACH (QPolygonF p, previous.toSubpathPolygons()) {+ if (p.size() == 1) continue;+ for (int j = 1; j < p.size(); j++) {+ QLineF l2(p.at(j-1), p.at(j));+ QPointF intersect;+ if (l2.intersects(arrow, &intersect) == QLineF::BoundedIntersection) {+ arrow.setP1(intersect);+ break;+ }+ }+ }+ Q_FOREACH (QPolygonF p, next.toSubpathPolygons()) {+ if (p.size() == 1) continue;+ for (int j = 1; j < p.size(); j++) {+ QLineF l2(p.at(j-1), p.at(j));+ QPointF intersect;+ if (l2.intersects(arrow, &intersect) == QLineF::BoundedIntersection) {+ arrow.setP2(intersect);+ break;+ }+ }+ }++ }+ lines.append(arrow);+ }+ return lines;+}+void KoSvgTextShapeOutlineHelper::paintTextShape(QPainter *painter, const KoViewConverter &converter,const QPalette &pal, KoSvgTextShape *text,bool contourModeActive) {@@ -85,8 +125,16 @@ void KoSvgTextShapeOutlineHelper::paintTextShape(QPainter *painter, const KoViewhelper.setHandleStyle(KisHandleStyle::secondarySelection());if (contourModeActive) {if (d->drawOutline) {+ QList<QPainterPath> areas;Q_FOREACH(KoShape *shape, text->internalShapeManager()->shapes()) {- helper.drawPath(shape->transformation().map(shape->outline()));+ QPainterPath p = shape->transformation().map(shape->outline());+ helper.drawPath(p);+ if (!d->drawTextWrappingArea && text->shapesInside().contains(shape)) {+ areas.append(p);+ }+ }+ Q_FOREACH(QLineF arrow, getTextAreaOrderArrows(areas)) {+ helper.drawGradientArrow(arrow.p1(), arrow.p2(), 1.5 * d->handleRadius);}}if (d->drawBoundingRect) {@@ -96,41 +144,12 @@ void KoSvgTextShapeOutlineHelper::paintTextShape(QPainter *painter, const KoView}}if (d->drawTextWrappingArea) {+ if (d->textWrappingAreasHovered) {+ helper.setHandleStyle(KisHandleStyle::partiallyHighlightedPrimaryHandles());+ }QList<QPainterPath> areas = text->textWrappingAreas();- if (areas.size() > 1) {- for (int i = 1; i < areas.size(); i++) {- const QPainterPath previous = areas.at(i-1);- const QPainterPath next = areas.at(i);- const bool overlap = previous.intersects(next);- QLineF arrow(previous.boundingRect().center(), next.boundingRect().center());- if (!overlap) {-- Q_FOREACH (QPolygonF p, previous.toSubpathPolygons()) {- if (p.size() == 1) continue;- for (int j = 1; j < p.size(); j++) {- QLineF l2(p.at(j-1), p.at(j));- QPointF intersect;- if (l2.intersects(arrow, &intersect) == QLineF::BoundedIntersection) {- arrow.setP1(intersect);- break;- }- }- }- Q_FOREACH (QPolygonF p, next.toSubpathPolygons()) {- if (p.size() == 1) continue;- for (int j = 1; j < p.size(); j++) {- QLineF l2(p.at(j-1), p.at(j));- QPointF intersect;- if (l2.intersects(arrow, &intersect) == QLineF::BoundedIntersection) {- arrow.setP2(intersect);- break;- }- }- }-- }- helper.drawGradientArrow(arrow.p1(), arrow.p2(), 1.5 * d->handleRadius);- }+ Q_FOREACH(QLineF arrow, getTextAreaOrderArrows(areas)) {+ helper.drawGradientArrow(arrow.p1(), arrow.p2(), 1.5 * d->handleRadius);}Q_FOREACH(const QPainterPath path, areas) {helper.drawPath(path);@@ -139,8 +158,6 @@ void KoSvgTextShapeOutlineHelper::paintTextShape(QPainter *painter, const KoViewpainter->restore();painter->save();- //painter->setTransform(converter.viewToDocument() *- // painter->transform());QIcon icon = contourModeActive? KisIconUtils::loadIcon(ICON_EXIT): KisIconUtils::loadIcon(ICON_ENTER);QPixmap pm = icon.pixmap(BUTTON_ICON_SIZE, BUTTON_ICON_SIZE);painter->setBrush(contourModeActive? pal.highlight(): pal.button());@@ -258,3 +275,8 @@ void KoSvgTextShapeOutlineHelper::toggleTextContourMode(KoSvgTextShape *shape)}}}++void KoSvgTextShapeOutlineHelper::setTextAreasHovered(bool enabled)+{+ d->textWrappingAreasHovered = enabled;+}diff --git a/libs/flake/text/KoSvgTextShapeOutlineHelper.h b/libs/flake/text/KoSvgTextShapeOutlineHelper.hindex be0b3dbdf2..ca6b8b96c3 100644--- a/libs/flake/text/KoSvgTextShapeOutlineHelper.h+++ b/libs/flake/text/KoSvgTextShapeOutlineHelper.h@@ -74,6 +74,8 @@ public:void toggleTextContourMode(KoSvgTextShape *shape);+ void setTextAreasHovered(bool enabled);+private:void paintTextShape(QPainter *painter, const KoViewConverter &converter,const QPalette &pal, KoSvgTextShape *text,diff --git a/libs/flake/text/KoSvgTextShape_p.h b/libs/flake/text/KoSvgTextShape_p.hindex e688e24fdb..c878e544b1 100644--- a/libs/flake/text/KoSvgTextShape_p.h+++ b/libs/flake/text/KoSvgTextShape_p.h@@ -554,6 +554,7 @@ public:* calls computing them, and then calls relayout();*/void updateTextWrappingAreas();+ static QList<QPainterPath> generateShapes(const QList<KoShape*> shapesInside, const QList<KoShape*> shapesSubtract, const KoSvgTextProperties &properties);static KoShape *textPathByName(QString name, QList<KoShape*> textPaths) {auto it = std::find_if(textPaths.begin(), textPaths.end(), [&name](const KoShape *s) -> bool {return s->name() == name;});diff --git a/libs/flake/text/KoSvgTextShape_p_layout.cpp b/libs/flake/text/KoSvgTextShape_p_layout.cppindex 20d5cc6c76..4a20cdcb3b 100644--- a/libs/flake/text/KoSvgTextShape_p_layout.cpp+++ b/libs/flake/text/KoSvgTextShape_p_layout.cpp@@ -118,6 +118,11 @@ void KoSvgTextShape::Private::updateTextWrappingAreas()relayout();}}++QList<QPainterPath> KoSvgTextShape::Private::generateShapes(const QList<KoShape *> shapesInside, const QList<KoShape *> shapesSubtract, const KoSvgTextProperties &properties)+{+ return getShapes(shapesInside, shapesSubtract, properties);+}// NOLINTNEXTLINE(readability-function-cognitive-complexity)void KoSvgTextShape::Private::relayout(){diff --git a/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.cpp b/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.cppindex 4aee0c0e87..6564056d33 100644--- a/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.cpp+++ b/plugins/tools/svgtexttool/SvgChangeTextPaddingMarginStrategy.cpp@@ -126,17 +126,33 @@ QLineF getLine(QPointF mousePos, KoPathShape *referenceShape, bool isPadding) {return l;}+KoSvgTextProperties getProperties(bool isPadding, QLineF line, KoSvgTextProperties previous = KoSvgTextProperties()) {+ KoSvgTextProperties::PropertyId propId = isPadding? KoSvgTextProperties::ShapePaddingId: KoSvgTextProperties::ShapeMarginId;+ KoSvgText::CssLengthPercentage length;+ length.value = line.length();+ previous.setProperty(propId, QVariant::fromValue(length));+ return previous;+}+void SvgChangeTextPaddingMarginStrategy::paint(QPainter &painter, const KoViewConverter &converter){if (!(m_referenceShape && m_shape)) return;- const QTransform originalPainterTransform = painter.transform();- painter.setTransform(converter.documentToView(), true);- KisHandlePainterHelper handlePainter(&painter, originalPainterTransform, handleRadius(), decorationThickness());-- const QLineF l = m_referenceShape->absoluteTransformation().map(getLine(m_lastMousePos, m_referenceShape, m_isPadding));- QPolygonF poly;- poly << l.p1() << l.p2();- handlePainter.drawRubberLine(poly);+ painter.save();+ KisHandlePainterHelper handlePainter =+ KoShape::createHandlePainterHelperView(&painter, m_shape, converter, handleRadius(), decorationThickness());+ handlePainter.setHandleStyle(KisHandleStyle::selectedPrimaryHandles());++ const QLineF line = getLine(m_lastMousePos, m_referenceShape, m_isPadding);+ const QTransform lineTf = m_referenceShape->absoluteTransformation() * m_shape->absoluteTransformation().inverted();+ handlePainter.drawConnectionLine(lineTf.map(line));++ KoSvgTextProperties props = getProperties(m_isPadding, line, m_shape->textProperties());+ QList<QPainterPath> areas = m_shape->generateTextAreas(m_shape->shapesInside(), m_shape->shapesSubtract(), props);+ Q_FOREACH(QPainterPath area, areas) {+ handlePainter.drawPath(area);+ }++ painter.restore();}void SvgChangeTextPaddingMarginStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)@@ -149,12 +165,7 @@ KUndo2Command *SvgChangeTextPaddingMarginStrategy::createCommand(){if (!(m_referenceShape && m_shape)) return nullptr;const QLineF l = getLine(m_lastMousePos, m_referenceShape, m_isPadding);-- KoSvgTextProperties::PropertyId propId = m_isPadding? KoSvgTextProperties::ShapePaddingId: KoSvgTextProperties::ShapeMarginId;- KoSvgText::CssLengthPercentage length;- length.value = l.length();- KoSvgTextProperties props;- props.setProperty(propId, QVariant::fromValue(length));+ KoSvgTextProperties props = getProperties(m_isPadding, l);return new SvgTextMergePropertiesRangeCommand(m_shape, props, -1, -1);}diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cppindex f05ca7365c..869ba11306 100644--- a/plugins/tools/svgtexttool/SvgTextTool.cpp+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp@@ -785,6 +785,7 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)} else if (SvgChangeTextPaddingMarginStrategy::hitTest(selectedShape, event->point, grabSensitivityInPt())) {m_interactionStrategy.reset(new SvgChangeTextPaddingMarginStrategy(this, selectedShape, event->point));m_dragging = DragMode::InShapeOffset;+ m_textOutlineHelper->setDrawTextWrappingArea(false);event->accept();return;} else if (m_textOnPathHelper.hitTest(event->point, canvas()->viewConverter()->viewToDocument())) {@@ -953,10 +954,13 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)const KoSvgTextShape *hoveredShape = dynamic_cast<KoSvgTextShape *>(canvas()->shapeManager()->shapeAt(event->point));const KoPathShape *hoveredFlowShape = dynamic_cast<KoPathShape *>(canvas()->shapeManager()->shapeAt(event->point));QPainterPath hoverPath = KisToolUtils::shapeHoverInfoCrossLayer(canvas(), event->point, shapeType, &isHorizontal);++ bool textAreasHovered = false;if (m_textOnPathHelper.hitTest(event->point, canvas()->viewConverter()->viewToDocument()) ) {cursor = Qt::ArrowCursor;} else if(std::optional<QPointF> offsetVector = SvgChangeTextPaddingMarginStrategy::hitTest(selectedShape, event->point, grabSensitivityInPt())) {cursor = lineToCursor(QLineF(QPointF(0, 0), offsetVector.value()).normalVector(), canvas());+ textAreasHovered = true;} else if (selectedShape && selectedShape == hoveredShape && m_highlightItem == HighlightItem::None) {if (selectedShape->writingMode() == KoSvgText::HorizontalTB) {cursor = m_ibeam_horizontal;@@ -991,6 +995,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)cursor = m_ibeam_vertical;}}+ m_textOutlineHelper->setTextAreasHovered(textAreasHovered);useCursor(cursor);event->ignore();}@@ -1013,6 +1018,7 @@ void SvgTextTool::mouseReleaseEvent(KoPointerEvent *event)useCursor(m_base_cursor);}m_textOnPathHelper.setStrategyActive(false);+ m_textOutlineHelper->setDrawTextWrappingArea(true);m_dragging = DragMode::None;m_textCursor.setDrawTypeSettingHandle(true);event->accept();