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 proof-of-concept for text-in-shape.
Wolthera van Hövell tot Westerflierd7d8d44dfb33
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cppindex 4ef1ae9fc1..2459427446 100644--- a/libs/flake/text/KoSvgTextShape.cpp+++ b/libs/flake/text/KoSvgTextShape.cpp@@ -163,7 +163,19 @@ void KoSvgTextShape::shapeChanged(ChangeType type, KoShape *shape)}KoShape::shapeChanged(type, shape);- if (type == ContentChanged) {+ if ((d->shapesInside.contains(shape) || d->shapesSubtract.contains(shape))+ && (type == PositionChanged+ || type == RotationChanged+ || type == ScaleChanged+ || type == ShearChanged+ || type == SizeChanged+ || type == GenericMatrixChange+ || type == ParameterChanged)) {+ // update shape. relayout.+ //TODO: also handle type == Deleted. Need to figure out the order of operations though.+ relayout();+ }+ if ((!shape || shape == this) && type == ContentChanged) {relayout();}}@@ -1792,37 +1804,6 @@ void KoSvgTextShape::paint(QPainter &painter) constd->paintPaths(painter, rootBounds, this, d->result, textRendering, chunk, currentIndex);d->paintTextDecoration(painter, rootBounds, this, KoSvgText::DecorationLineThrough, textRendering);}-#if 0 // Debug- Q_FOREACH (KoShape *child, this->shapes()) {- const KoSvgTextChunkShape *textPathChunk = dynamic_cast<const KoSvgTextChunkShape *>(child);- if (textPathChunk) {- painter.save();- painter.setPen(Qt::magenta);- painter.setOpacity(0.5);- if (textPathChunk->layoutInterface()->textPath()) {- QPainterPath p = textPathChunk->layoutInterface()->textPath()->outline();- p = textPathChunk->layoutInterface()->textPath()->transformation().map(p);- painter.strokePath(p, QPen(Qt::green));- painter.drawPoint(p.pointAtPercent(0));- painter.drawPoint(p.pointAtPercent(p.percentAtLength(p.length() * 0.5)));- painter.drawPoint(p.pointAtPercent(1.0));- }- painter.restore();- }- }-#endif-#if 0 // Debug- Q_FOREACH (KoShape *shapeInside, d->shapesInside) {- QPainterPath p = shapeInside->outline();- p = shapeInside->transformation().map(p);- painter.strokePath(p, QPen(Qt::green));- }- Q_FOREACH (KoShape *shapeInside, d->shapesSubtract) {- QPainterPath p = shapeInside->outline();- p = shapeInside->transformation().map(p);- painter.strokePath(p, QPen(Qt::red));- }-#endifpainter.restore();}@@ -1835,12 +1816,19 @@ void KoSvgTextShape::paintStroke(QPainter &painter) constQPainterPath KoSvgTextShape::outline() const {QPainterPath result;- 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) {- result.addPath(it->textDecorations.values().at(i));+ if (!d->shapesInside.isEmpty()) {+ //TODO: what about shape subtract?+ Q_FOREACH(KoShape *shape, d->shapesInside) {+ result.addPath(shape->transformation().map(shape->outline()));}+ } else {+ 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) {+ result.addPath(it->textDecorations.values().at(i));+ }+ }}return result;}@@ -1883,6 +1871,38 @@ QRectF KoSvgTextShape::boundingRect() constreturn this->absoluteTransformation().mapRect(result);}+QSizeF KoSvgTextShape::size() const+{+ return outlineRect().size();+}++void KoSvgTextShape::setSize(const QSizeF &size)+{+ const QRectF oRect = this->outlineRect();+ const QSizeF oldSize = oRect.size();++ if (size == oldSize) return;++ const qreal scaleX = size.width() / oldSize.width();+ const qreal scaleY = size.height() / oldSize.height();++ if (!d->shapesInside.isEmpty()) {+ Q_FOREACH(KoShape *shape, d->shapesInside) {+ KoFlake::resizeShapeCommon(shape, scaleX, scaleY, shape->boundingRect().topLeft() - oRect.topLeft(), false, false, this->transformation());+ }+ Q_FOREACH(KoShape *shape, d->shapesSubtract) {+ KoFlake::resizeShapeCommon(shape, scaleX, scaleY, shape->boundingRect().topLeft() - oRect.topLeft(), false, false, this->transformation());+ }+ } else {+ this->scale(scaleX, scaleY);+ //TODO: use scaling function for kosvgtextproperties when styles presets are merged.+ //TODO: Support text-on-path+ notifyChanged();+ shapeChangedPriv(ScaleChanged);+ }++}+void KoSvgTextShape::paintDebug(QPainter &painter, const DebugElements elements) const{if (elements & DebugElement::CharBbox) {@@ -1892,6 +1912,18 @@ void KoSvgTextShape::paintDebug(QPainter &painter, const DebugElements elements)rootBounds.addRect(this->outline().boundingRect());d->paintDebug(painter, d->result, currentIndex);}++ //Debug shape outlines.+ Q_FOREACH (KoShape *shapeInside, d->shapesInside) {+ QPainterPath p = shapeInside->outline();+ p = shapeInside->transformation().map(p);+ painter.strokePath(p, QPen(Qt::green));+ }+ Q_FOREACH (KoShape *shapeInside, d->shapesSubtract) {+ QPainterPath p = shapeInside->outline();+ p = shapeInside->transformation().map(p);+ painter.strokePath(p, QPen(Qt::red));+ }}if (elements & DebugElement::LineBox) {@@ -1959,7 +1991,14 @@ KoSvgTextShape::TextType KoSvgTextShape::textType() constvoid KoSvgTextShape::setShapesInside(QList<KoShape *> shapesInside){+ Q_FOREACH(KoShape *shape, d->shapesInside) {+ shape->removeDependee(this);+ }d->shapesInside = shapesInside;+ Q_FOREACH(KoShape *shape, d->shapesInside) {+ shape->addDependee(this);+ }+ shapeChangedPriv(ContentChanged);}QList<KoShape *> KoSvgTextShape::shapesInside() const@@ -1969,7 +2008,14 @@ QList<KoShape *> KoSvgTextShape::shapesInside() constvoid KoSvgTextShape::setShapesSubtract(QList<KoShape *> shapesSubtract){+ Q_FOREACH(KoShape *shape, d->shapesSubtract) {+ shape->removeDependee(this);+ }d->shapesSubtract = shapesSubtract;+ Q_FOREACH(KoShape *shape, d->shapesSubtract) {+ shape->addDependee(this);+ }+ shapeChangedPriv(ContentChanged);}QList<KoShape *> KoSvgTextShape::shapesSubtract() constdiff --git a/libs/flake/text/KoSvgTextShape.h b/libs/flake/text/KoSvgTextShape.hindex 12c0785c63..5d591218ca 100644--- a/libs/flake/text/KoSvgTextShape.h+++ b/libs/flake/text/KoSvgTextShape.h@@ -44,6 +44,9 @@ public:QRectF outlineRect() const override;QRectF boundingRect() const override;+ QSizeF size() const override;+ void setSize(const QSizeF &size) override;+enum class DebugElement {CharBbox = 1 << 0,LineBox = 1 << 1,diff --git a/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp b/plugins/tools/svgtexttool/SvgCreateTextStrategy.cppindex 189e3e69c0..de9fd0780c 100644--- a/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp+++ b/plugins/tools/svgtexttool/SvgCreateTextStrategy.cpp@@ -23,10 +23,11 @@#include "kis_global.h"#include "kundo2command.h"-SvgCreateTextStrategy::SvgCreateTextStrategy(SvgTextTool *tool, const QPointF &clicked)+SvgCreateTextStrategy::SvgCreateTextStrategy(SvgTextTool *tool, const QPointF &clicked, KoShape *shape): KoInteractionStrategy(tool), m_dragStart(clicked), m_dragEnd(clicked)+ , m_flowShape(shape){KoSvgTextProperties properties = tool->propertiesForNewText();properties.inheritFrom(KoSvgTextProperties::defaultProperties(), true);@@ -131,7 +132,12 @@ KUndo2Command *SvgCreateTextStrategy::createCommand()params->setProperty("shapeRect", QVariant(rectangle));params->setProperty("origin", QVariant(origin));- KoShape *textShape = factory->createShape( params, tool->canvas()->shapeController()->resourceManager());+ KoSvgTextShape *textShape = dynamic_cast<KoSvgTextShape *>(factory->createShape( params, tool->canvas()->shapeController()->resourceManager()));++ if (m_flowShape) {+ textShape->setPosition(QPointF());+ textShape->setShapesInside({m_flowShape});+ }KUndo2Command *parentCommand = new KUndo2Command();@@ -163,3 +169,8 @@ bool SvgCreateTextStrategy::draggingInlineSize()QRectF rectangle = QRectF(m_dragStart, m_dragEnd).normalized();return (rectangle.width() >= m_minSizeInline.width() || rectangle.height() >= m_minSizeInline.height()) && !m_modifiers.testFlag(Qt::ControlModifier);}++bool SvgCreateTextStrategy::hasWrappingShape()+{+ return (m_flowShape)? true: false;+}diff --git a/plugins/tools/svgtexttool/SvgCreateTextStrategy.h b/plugins/tools/svgtexttool/SvgCreateTextStrategy.hindex b7a77afa81..90f1fb8413 100644--- a/plugins/tools/svgtexttool/SvgCreateTextStrategy.h+++ b/plugins/tools/svgtexttool/SvgCreateTextStrategy.h@@ -15,11 +15,12 @@class SvgTextTool;class KoSvgTextShape;+class KoShape;class SvgCreateTextStrategy : public KoInteractionStrategy{public:- SvgCreateTextStrategy(SvgTextTool *tool, const QPointF &clicked);+ SvgCreateTextStrategy(SvgTextTool *tool, const QPointF &clicked, KoShape *shape = nullptr);~SvgCreateTextStrategy() override = default;void paint(QPainter &painter, const KoViewConverter &converter) override;@@ -29,11 +30,13 @@ public:void finishInteraction(Qt::KeyboardModifiers modifiers) override;bool draggingInlineSize();+ bool hasWrappingShape();private:QPointF m_dragStart;QPointF m_dragEnd;QSizeF m_minSizeInline;+ KoShape *m_flowShape;Qt::KeyboardModifiers m_modifiers;};diff --git a/plugins/tools/svgtexttool/SvgTextTool.cpp b/plugins/tools/svgtexttool/SvgTextTool.cppindex cc1aef7063..5483314e40 100644--- a/plugins/tools/svgtexttool/SvgTextTool.cpp+++ b/plugins/tools/svgtexttool/SvgTextTool.cpp@@ -770,11 +770,12 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)}KoSvgTextShape *hoveredShape = dynamic_cast<KoSvgTextShape *>(canvas()->shapeManager()->shapeAt(event->point));+ KoPathShape *hoveredFlowShape = dynamic_cast<KoPathShape *>(canvas()->shapeManager()->shapeAt(event->point));QString shapeType;QPainterPath hoverPath = KisToolUtils::shapeHoverInfoCrossLayer(canvas(), event->point, shapeType);bool crossLayerPossible = !hoverPath.isEmpty() && shapeType == KoSvgTextShape_SHAPEID;- if (!selectedShape && !hoveredShape && !crossLayerPossible) {+ if (!selectedShape && !hoveredShape && !hoveredFlowShape && !crossLayerPossible) {QPointF point = canvas()->snapGuide()->snap(event->point, event->modifiers());m_interactionStrategy.reset(new SvgCreateTextStrategy(this, point));m_dragging = DragMode::Create;@@ -788,6 +789,11 @@ void SvgTextTool::mousePressEvent(KoPointerEvent *event)m_interactionStrategy.reset(new SvgSelectTextStrategy(this, &m_textCursor, event->point));m_dragging = DragMode::Select;event->accept();+ } else if (hoveredFlowShape) {+ QPointF point = canvas()->snapGuide()->snap(event->point, event->modifiers());+ m_interactionStrategy.reset(new SvgCreateTextStrategy(this, point, hoveredFlowShape->cloneShape()));+ m_dragging = DragMode::Create;+ event->accept();} else if (crossLayerPossible) {if (KisToolUtils::selectShapeCrossLayer(canvas(), event->point, KoSvgTextShape_SHAPEID)) {m_interactionStrategy.reset(new SvgSelectTextStrategy(this, &m_textCursor, event->point));@@ -838,7 +844,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)m_interactionStrategy->handleMouseMove(event->point, event->modifiers());if (m_dragging == DragMode::Create) {SvgCreateTextStrategy *c = dynamic_cast<SvgCreateTextStrategy*>(m_interactionStrategy.get());- if (c && c->draggingInlineSize()) {+ if (c && c->draggingInlineSize() && !c->hasWrappingShape()) {if (this->writingMode() == KoSvgText::HorizontalTB) {useCursor(m_text_inline_horizontal);} else {@@ -900,6 +906,7 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)QString shapeType;bool isHorizontal = true;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);if (selectedShape && selectedShape == hoveredShape && m_highlightItem == HighlightItem::None) {cursor = selectedShape->writingMode() == KoSvgText::HorizontalTB? m_ibeam_horizontal: m_ibeam_vertical;@@ -923,6 +930,9 @@ void SvgTextTool::mouseMoveEvent(KoPointerEvent *event)} else {cursor = m_ibeam_vertical;}+ } else if (hoveredFlowShape) {+ m_hoveredShapeHighlightRect.addPath(hoveredFlowShape->absoluteTransformation().map(hoveredFlowShape->outline()));+ cursor = m_text_in_shape;} else if (!hoverPath.isEmpty() && shapeType == KoSvgTextShape_SHAPEID && m_highlightItem == HighlightItem::None) {m_hoveredShapeHighlightRect = hoverPath;if (isHorizontal) {