Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgCreateTextStrategy Class Reference

#include <SvgCreateTextStrategy.h>

+ Inheritance diagram for SvgCreateTextStrategy:

Public Member Functions

void cancelInteraction () override
 
KUndo2CommandcreateCommand () override
 
bool draggingInlineSize ()
 
void finishInteraction (Qt::KeyboardModifiers modifiers) override
 
void handleMouseMove (const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers) override
 
bool hasWrappingShape ()
 
void paint (QPainter &painter, const KoViewConverter &converter) override
 
 SvgCreateTextStrategy (SvgTextTool *tool, const QPointF &clicked, KoShape *shape=nullptr)
 
 ~SvgCreateTextStrategy () override=default
 
- Public Member Functions inherited from KoInteractionStrategy
 KoInteractionStrategy (KoToolBase *parent)
 constructor
 
KoToolBasetool () const
 
virtual ~KoInteractionStrategy ()
 Destructor.
 

Private Attributes

QPointF m_dragEnd
 
QPointF m_dragStart
 
KoShapem_flowShape
 
QSizeF m_minSizeInline
 
Qt::KeyboardModifiers m_modifiers
 

Additional Inherited Members

- Protected Member Functions inherited from KoInteractionStrategy
uint decorationThickness () const
 
uint grabSensitivity () const
 Convenience function to get the global grab sensitivity.
 
uint handleRadius () const
 Convenience function to get the global handle radius.
 
 KoInteractionStrategy (KoInteractionStrategyPrivate &)
 constructor
 
- Protected Attributes inherited from KoInteractionStrategy
KoInteractionStrategyPrivated_ptr
 

Detailed Description

Definition at line 20 of file SvgCreateTextStrategy.h.

Constructor & Destructor Documentation

◆ SvgCreateTextStrategy()

SvgCreateTextStrategy::SvgCreateTextStrategy ( SvgTextTool * tool,
const QPointF & clicked,
KoShape * shape = nullptr )

Definition at line 34 of file SvgCreateTextStrategy.cpp.

36 , m_dragStart(clicked)
37 , m_dragEnd(clicked)
38 , m_flowShape(shape)
39{
40 KoSvgTextProperties properties = tool->propertiesForNewText();
42 const KoSvgText::FontMetrics fontMetrics = properties.metrics(true);
43 const qreal ftMultiplier = properties.fontSize().value / fontMetrics.fontSize;
44 const double lineHeight = (fontMetrics.lineGap+fontMetrics.ascender+fontMetrics.descender)*ftMultiplier;
45 m_minSizeInline = {lineHeight, lineHeight};
46}
KoInteractionStrategy(KoToolBase *parent)
constructor
KoSvgText::FontMetrics metrics(const bool withResolvedLineHeight=true, const bool offsetByBaseline=false) const
metrics Return the metrics of the first available font.
static const KoSvgTextProperties & defaultProperties()
void inheritFrom(const KoSvgTextProperties &parentProperties, bool resolve=false, bool onlyFontAndLineHeight=false)
KoSvgText::CssLengthPercentage fontSize() const
The FontMetrics class A class to keep track of a variety of font metrics. Note that values are in Fre...
Definition KoSvgText.h:327
qint32 lineGap
additional linegap between consecutive lines.
Definition KoSvgText.h:341
qint32 fontSize
Currently set size, CSS unit 'em'.
Definition KoSvgText.h:329
qint32 descender
distance for origin to bottom.
Definition KoSvgText.h:340
qint32 ascender
distance from origin to top.
Definition KoSvgText.h:339

References KoSvgText::FontMetrics::ascender, KoSvgTextProperties::defaultProperties(), KoSvgText::FontMetrics::descender, KoSvgText::FontMetrics::fontSize, KoSvgTextProperties::fontSize(), KoSvgTextProperties::inheritFrom(), KoSvgText::FontMetrics::lineGap, m_minSizeInline, KoSvgTextProperties::metrics(), KoInteractionStrategy::tool(), and KoSvgText::CssLengthPercentage::value.

◆ ~SvgCreateTextStrategy()

SvgCreateTextStrategy::~SvgCreateTextStrategy ( )
overridedefault

Member Function Documentation

◆ cancelInteraction()

void SvgCreateTextStrategy::cancelInteraction ( )
overridevirtual

This method will undo frames based interactions by calling createCommand() and unexecuting that.

Reimplemented from KoInteractionStrategy.

Definition at line 210 of file SvgCreateTextStrategy.cpp.

211{
212 tool()->canvas()->snapGuide()->reset();
213 const QRectF updateRect = QRectF(m_dragStart, m_dragEnd).normalized();
214 tool()->canvas()->updateCanvas(updateRect);
215}
KoSnapGuide * snapGuide
virtual void updateCanvas(const QRectF &rc)=0
void reset()
Resets the snap guide.
KoCanvasBase * canvas() const
Returns the canvas the tool is working on.

References KoToolBase::canvas(), m_dragEnd, m_dragStart, KoSnapGuide::reset(), KoCanvasBase::snapGuide, KoInteractionStrategy::tool(), and KoCanvasBase::updateCanvas().

◆ createCommand()

KUndo2Command * SvgCreateTextStrategy::createCommand ( )
overridevirtual

For interactions that are undo-able this method should be implemented to return such a command. Implementations should return 0 otherwise.

Returns
a command, or 0.

Properties don't write their fills/strokes to svg, so we need to set those manually.

Implements KoInteractionStrategy.

Definition at line 67 of file SvgCreateTextStrategy.cpp.

68{
69 SvgTextTool *const tool = qobject_cast<SvgTextTool *>(this->tool());
70
71 QRectF rectangle = QRectF(m_dragStart, m_dragEnd).normalized();
72
73 KoSvgTextProperties properties = tool->propertiesForNewText();
74 KoSvgTextProperties resolvedProperties = properties;
75 resolvedProperties.inheritFrom(KoSvgTextProperties::defaultProperties(), true);
76
77 const KoSvgText::FontMetrics fontMetrics = properties.metrics(true);
78 const qreal ftMultiplier = resolvedProperties.fontSize().value / fontMetrics.fontSize;
79 double ascender = fontMetrics.ascender;
80 ascender += fontMetrics.lineGap/2;
81 ascender *= ftMultiplier;
82 const double lineHeight = m_minSizeInline.width();
84
85 bool unwrappedText = m_modifiers.testFlag(Qt::ControlModifier) || m_flowShape;
86 if (rectangle.width() < m_minSizeInline.width() && rectangle.height() < m_minSizeInline.height()) {
87 unwrappedText = true;
88 }
89 if (!unwrappedText) {
91 val.isAuto = false;
92 val.customValue = writingMode == KoSvgText::HorizontalTB? rectangle.width(): rectangle.height();
93 properties.setProperty(KoSvgTextProperties::InlineSizeId, QVariant::fromValue(val));
94 } else {
95 // We explicitely remove the inline size, because it could've been inside the properties by a different method.
97 }
98 if (writingMode != KoSvgText::HorizontalTB) {
100 }
101 // Ensure white space is set to pre-wrap if unspecified.
104 }
107 }
108
109 KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value("KoSvgTextShapeID");
110 KoProperties *params = new KoProperties();//Fill these with "svgText", "defs" and "shapeRect"
111 params->setProperty("defs", QVariant(tool->generateDefs(properties)));
112
113 QPointF origin = rectangle.topLeft();
114
115 {
118
119 if (writingMode == KoSvgText::HorizontalTB) {
120 origin.setY(rectangle.top() + ascender);
121 if (halign == KoSvgText::AnchorMiddle) {
122 origin.setX(rectangle.center().x());
123 } else if ((halign == KoSvgText::AnchorEnd && !isRtl) || (halign == KoSvgText::AnchorStart && isRtl)) {
124 origin.setX(rectangle.right());
125 }
126 } else {
127 if (writingMode == KoSvgText::VerticalRL) {
128 origin.setX(rectangle.right() - (lineHeight*0.5));
129 } else {
130 origin.setX(rectangle.left() + (lineHeight*0.5));
131 }
132
133 if (halign == KoSvgText::AnchorMiddle) {
134 origin.setY(rectangle.center().y());
135 } else if (halign == KoSvgText::AnchorEnd) {
136 origin.setY(rectangle.bottom());
137 }
138 }
139 }
140 if (!rectangle.contains(origin) && unwrappedText) {
141 origin = writingMode == KoSvgText::HorizontalTB? QPointF(origin.x(), rectangle.bottom()): QPointF(rectangle.center().x(), origin.y());
142 }
143 params->setProperty("shapeRect", QVariant(rectangle));
144 params->setProperty("origin", QVariant(origin));
145
146 KoSvgTextShape *textShape = dynamic_cast<KoSvgTextShape *>(factory->createShape( params, tool->canvas()->shapeController()->resourceManager()));
147
150 textShape->setBackground(properties.background());
151 }
153 textShape->setStroke(properties.stroke());
154 }
155
156 KUndo2Command *parentCommand = new KUndo2Command();
157
158 new KoKeepShapesSelectedCommand(tool->koSelection()->selectedShapes(), {}, tool->canvas()->selectedShapesProxy(), false, parentCommand);
159
160 KoShapeContainer* parent = tool->canvas()->shapeController()->documentBase()->createParentForShapes({textShape}, false, parentCommand);
161 KUndo2Command *cmd = tool->canvas()->shapeController()->addShape(textShape, parent, parentCommand);
162
163 parentCommand->setText(cmd->text());
164
165 if (m_flowShape) {
167
168 KoPathShape *path = dynamic_cast<KoPathShape*>(m_flowShape);
169 KoPathSegment segment;
170 if(path) {
171 segment = path->segmentAtPoint(m_dragStart, tool->handleGrabRect(m_dragStart));
172 }
173 if (segment.isValid()) {
174 int pos = textShape->posForIndex(textShape->plainText().size());
175 new KoSvgTextSetTextPathOnRangeCommand(textShape, m_flowShape, 0, pos, parentCommand);
176
178 const qreal grab = tool->grabSensitivityInPt();
179 QList<KoPathSegment> segments = path->segmentsAt(path->outlineRect().adjusted(-grab, -grab, grab, grab));
180 Q_FOREACH(KoPathSegment s, segments) {
181 if (s == segment) {
182 info.startOffset += (segment.nearestPoint(path->documentToShape(m_dragStart))*segment.length());
183 break;
184 }
185 info.startOffset += s.length();
186 qDebug() << info.startOffset << s.length();
187 }
188 new KoSvgTextPathInfoChangeCommand(textShape, 2, info, parentCommand);
189 } else {
190 new KoSvgTextAddShapeCommand(textShape, m_flowShape, true, parentCommand);
191 }
192 }
193
194 {
195 KoSelectedShapesProxy *finalSelectionProxy = tool->canvas()->selectedShapesProxy();
196 if (parent) {
197 KisShapeLayer *layer = dynamic_cast<KisShapeLayer *>(parent);
198 if (layer) {
199 finalSelectionProxy = layer->selectedShapesProxy();
200 }
201 }
202 new KoKeepShapesSelectedCommand({}, {textShape}, finalSelectionProxy, true, parentCommand);
203 }
204
205 tool->canvas()->snapGuide()->reset();
206
207 return parentCommand;
208}
void setText(const KUndo2MagicString &text)
KUndo2MagicString text() const
KoSelectedShapesProxy * selectedShapesProxy()
selectedShapesProxy
QPointer< KoShapeController > shapeController
virtual KoSelectedShapesProxy * selectedShapesProxy() const =0
selectedShapesProxy() is a special interface for keeping a persistent connections to selectionChanged...
const T value(const QString &id) const
A KoPathSegment consist of two neighboring KoPathPoints.
qreal length(qreal error=0.005) const
qreal nearestPoint(const QPointF &point) const
bool isValid() const
Returns if segment is valid, e.g. has two valid points.
The position of a path point within a path shape.
Definition KoPathShape.h:63
void setProperty(const QString &name, const QVariant &value)
The KoSelectedShapesProxy class is a special interface of KoCanvasBase to have a stable connection to...
virtual KoShape * createShape(const KoProperties *params, KoDocumentResourceManager *documentResources=0) const
static KoShapeRegistry * instance()
QPointF absolutePosition(KoFlake::AnchorPosition anchor=KoFlake::Center) const
Definition KoShape.cpp:568
virtual void setPosition(const QPointF &position)
Set the position of the shape in pt.
Definition KoShape.cpp:268
@ TextAnchorId
KoSvgText::TextAnchor.
@ InlineSizeId
KoSvgText::AutoValue.
@ TextOrientationId
KoSvgText::TextOrientation.
@ TextCollapseId
KoSvgText::TextSpaceCollapse.
@ StrokeId
KoSvgText::StrokeProperty.
@ FillId
KoSvgText::BackgroundProperty.
@ WritingModeId
KoSvgText::WritingMode.
@ DirectionId
KoSvgText::Direction.
@ TextWrapId
KoSvgText::TextWrap.
QSharedPointer< KoShapeBackground > background() const
void removeProperty(PropertyId id)
KoShapeStrokeModelSP stroke() const
bool hasProperty(PropertyId id) const
void setProperty(PropertyId id, const QVariant &value)
QVariant propertyOrDefault(PropertyId id) const
void setBackground(QSharedPointer< KoShapeBackground > background) override
int posForIndex(int index, bool firstIndex=false, bool skipSynthetic=false) const
posForIndex Get the cursor position for a given index in a string.
void setStroke(KoShapeStrokeModelSP stroke) override
QRectF handleGrabRect(const QPointF &position) const
Qt::KeyboardModifiers m_modifiers
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327
@ TopLeft
Definition KoFlake.h:86
TextAnchor
Where the text is anchored for SVG 1.1 text and 'inline-size'.
Definition KoSvgText.h:79
@ AnchorEnd
Anchor right for LTR, left for RTL.
Definition KoSvgText.h:82
@ AnchorStart
Anchor left for LTR, right for RTL.
Definition KoSvgText.h:80
@ AnchorMiddle
Anchor to the middle.
Definition KoSvgText.h:81
Direction
Base direction used by Bidi algorithm.
Definition KoSvgText.h:48
@ DirectionRightToLeft
Definition KoSvgText.h:50
@ HorizontalTB
Definition KoSvgText.h:38
@ OrientationUpright
Set all characters upright.
Definition KoSvgText.h:73
@ Preserve
Do not collapse any space.
Definition KoSvgText.h:99

References KoShape::absolutePosition(), KoSvgText::AnchorEnd, KoSvgText::AnchorMiddle, KoSvgText::AnchorStart, KoSvgText::FontMetrics::ascender, KoSvgTextProperties::background(), KoToolBase::canvas(), KoShapeFactoryBase::createShape(), KoSvgText::AutoValue::customValue, KoSvgTextProperties::defaultProperties(), KoSvgTextProperties::DirectionId, KoSvgText::DirectionRightToLeft, KoSvgTextProperties::FillId, KoSvgText::FontMetrics::fontSize, KoSvgTextProperties::fontSize(), KoToolBase::handleGrabRect(), KoSvgTextProperties::hasProperty(), KoSvgText::HorizontalTB, KoSvgTextProperties::inheritFrom(), KoSvgTextProperties::InlineSizeId, KoShapeRegistry::instance(), KoSvgText::AutoValue::isAuto, KoPathSegment::isValid(), KoPathSegment::length(), KoSvgText::FontMetrics::lineGap, m_dragEnd, m_dragStart, m_flowShape, m_minSizeInline, m_modifiers, KoSvgTextProperties::metrics(), KoPathSegment::nearestPoint(), KoSvgText::OrientationUpright, KoSvgTextShape::plainText, KoSvgTextShape::posForIndex(), KoSvgText::Preserve, KoSvgTextProperties::propertyOrDefault(), KoSvgTextProperties::removeProperty(), KoSnapGuide::reset(), KisShapeLayer::selectedShapesProxy(), KoCanvasBase::selectedShapesProxy(), KoSvgTextShape::setBackground(), KoShape::setPosition(), KoProperties::setProperty(), KoSvgTextProperties::setProperty(), KoSvgTextShape::setStroke(), KUndo2Command::setText(), KoCanvasBase::shapeController, KoCanvasBase::snapGuide, KoSvgText::TextOnPathInfo::startOffset, KoSvgTextProperties::stroke(), KoSvgTextProperties::StrokeId, KUndo2Command::text(), KoSvgTextProperties::TextAnchorId, KoSvgTextProperties::TextCollapseId, KoSvgTextProperties::TextOrientationId, KoSvgTextProperties::TextWrapId, KoInteractionStrategy::tool(), KoFlake::TopLeft, KoSvgText::CssLengthPercentage::value, KoGenericRegistry< T >::value(), KoSvgText::VerticalRL, KoSvgText::Wrap, and KoSvgTextProperties::WritingModeId.

◆ draggingInlineSize()

bool SvgCreateTextStrategy::draggingInlineSize ( )

Definition at line 222 of file SvgCreateTextStrategy.cpp.

223{
224 QRectF rectangle = QRectF(m_dragStart, m_dragEnd).normalized();
225 return (rectangle.width() >= m_minSizeInline.width() || rectangle.height() >= m_minSizeInline.height()) && !m_modifiers.testFlag(Qt::ControlModifier);
226}

References m_dragEnd, m_dragStart, m_minSizeInline, and m_modifiers.

◆ finishInteraction()

void SvgCreateTextStrategy::finishInteraction ( Qt::KeyboardModifiers modifiers)
overridevirtual

Override to make final changes to the data on the end of an interaction.

Implements KoInteractionStrategy.

Definition at line 217 of file SvgCreateTextStrategy.cpp.

218{
219 m_modifiers = modifiers;
220}

References m_modifiers.

◆ handleMouseMove()

void SvgCreateTextStrategy::handleMouseMove ( const QPointF & mouseLocation,
Qt::KeyboardModifiers modifiers )
overridevirtual

Extending classes should implement this method to update the selectedShapes based on the new mouse position.

Parameters
mouseLocationthe new location in pt
modifiersOR-ed set of keys pressed.

Implements KoInteractionStrategy.

Definition at line 59 of file SvgCreateTextStrategy.cpp.

60{
61 m_dragEnd = this->tool()->canvas()->snapGuide()->snap(mouseLocation, modifiers);
62 m_modifiers = modifiers;
63 const QRectF updateRect = QRectF(m_dragStart, m_dragEnd).normalized();
64 tool()->canvas()->updateCanvas(kisGrowRect(updateRect, 100));
65}
QPointF snap(const QPointF &mousePosition, Qt::KeyboardModifiers modifiers)
snaps the mouse position, returns if mouse was snapped
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186

References KoToolBase::canvas(), kisGrowRect(), m_dragEnd, m_dragStart, m_modifiers, KoSnapGuide::snap(), KoCanvasBase::snapGuide, KoInteractionStrategy::tool(), and KoCanvasBase::updateCanvas().

◆ hasWrappingShape()

bool SvgCreateTextStrategy::hasWrappingShape ( )

Definition at line 228 of file SvgCreateTextStrategy.cpp.

229{
230 return (m_flowShape)? true: false;
231}

References m_flowShape.

◆ paint()

void SvgCreateTextStrategy::paint ( QPainter & painter,
const KoViewConverter & converter )
overridevirtual

Reimplement this if the action needs to draw a "blob" on the canvas; that is, a transient decoration like a rubber band.

Reimplemented from KoInteractionStrategy.

Definition at line 48 of file SvgCreateTextStrategy.cpp.

49{
50 const QTransform originalPainterTransform = painter.transform();
51 painter.setTransform(converter.documentToView(), true);
52 KisHandlePainterHelper handlePainter(&painter, originalPainterTransform, 0.0, decorationThickness());
53
54 const QPolygonF poly(QRectF(m_dragStart, m_dragEnd));
55 handlePainter.setHandleStyle(KisHandleStyle::primarySelection());
56 handlePainter.drawRubberLine(poly);
57}
The KisHandlePainterHelper class is a special helper for painting handles around objects....
static KisHandleStyle & primarySelection()
virtual QPointF documentToView(const QPointF &documentPoint) const

References KoInteractionStrategy::decorationThickness(), KoViewConverter::documentToView(), KisHandlePainterHelper::drawRubberLine(), m_dragEnd, m_dragStart, KisHandleStyle::primarySelection(), and KisHandlePainterHelper::setHandleStyle().

Member Data Documentation

◆ m_dragEnd

QPointF SvgCreateTextStrategy::m_dragEnd
private

Definition at line 37 of file SvgCreateTextStrategy.h.

◆ m_dragStart

QPointF SvgCreateTextStrategy::m_dragStart
private

Definition at line 36 of file SvgCreateTextStrategy.h.

◆ m_flowShape

KoShape* SvgCreateTextStrategy::m_flowShape
private

Definition at line 39 of file SvgCreateTextStrategy.h.

◆ m_minSizeInline

QSizeF SvgCreateTextStrategy::m_minSizeInline
private

Definition at line 38 of file SvgCreateTextStrategy.h.

◆ m_modifiers

Qt::KeyboardModifiers SvgCreateTextStrategy::m_modifiers
private

Definition at line 40 of file SvgCreateTextStrategy.h.


The documentation for this class was generated from the following files: