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 32 of file SvgCreateTextStrategy.cpp.

34 , m_dragStart(clicked)
35 , m_dragEnd(clicked)
36 , m_flowShape(shape)
37{
38 KoSvgTextProperties properties = tool->propertiesForNewText();
40 const KoSvgText::FontMetrics fontMetrics = properties.metrics(true);
41 const qreal ftMultiplier = properties.fontSize().value / fontMetrics.fontSize;
42 const double lineHeight = (fontMetrics.lineGap+fontMetrics.ascender+fontMetrics.descender)*ftMultiplier;
43 m_minSizeInline = {lineHeight, lineHeight};
44}
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()
KoSvgText::CssLengthPercentage fontSize() const
void inheritFrom(const KoSvgTextProperties &parentProperties, bool resolve=false)
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 194 of file SvgCreateTextStrategy.cpp.

195{
196 tool()->canvas()->snapGuide()->reset();
197 const QRectF updateRect = QRectF(m_dragStart, m_dragEnd).normalized();
198 tool()->canvas()->updateCanvas(updateRect);
199}
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 65 of file SvgCreateTextStrategy.cpp.

66{
67 SvgTextTool *const tool = qobject_cast<SvgTextTool *>(this->tool());
68
69 QRectF rectangle = QRectF(m_dragStart, m_dragEnd).normalized();
70
71 KoSvgTextProperties properties = tool->propertiesForNewText();
72 KoSvgTextProperties resolvedProperties = properties;
73 resolvedProperties.inheritFrom(KoSvgTextProperties::defaultProperties(), true);
74
75 const KoSvgText::FontMetrics fontMetrics = properties.metrics(true);
76 const qreal ftMultiplier = resolvedProperties.fontSize().value / fontMetrics.fontSize;
77 double ascender = fontMetrics.ascender;
78 ascender += fontMetrics.lineGap/2;
79 ascender *= ftMultiplier;
80 const double lineHeight = m_minSizeInline.width();
82
83 bool unwrappedText = m_modifiers.testFlag(Qt::ControlModifier);
84 if (rectangle.width() < m_minSizeInline.width() && rectangle.height() < m_minSizeInline.height()) {
85 unwrappedText = true;
86 }
87 if (!unwrappedText) {
89 val.isAuto = false;
90 val.customValue = writingMode == KoSvgText::HorizontalTB? rectangle.width(): rectangle.height();
91 properties.setProperty(KoSvgTextProperties::InlineSizeId, QVariant::fromValue(val));
92 }
93 if (writingMode != KoSvgText::HorizontalTB) {
95 }
96 // Ensure white space is set to pre-wrap if unspecified.
99 }
102 }
103
104 KoShapeFactoryBase *factory = KoShapeRegistry::instance()->value("KoSvgTextShapeID");
105 KoProperties *params = new KoProperties();//Fill these with "svgText", "defs" and "shapeRect"
106 params->setProperty("defs", QVariant(tool->generateDefs(properties)));
107
108 QPointF origin = rectangle.topLeft();
109
110 {
113
114 if (writingMode == KoSvgText::HorizontalTB) {
115 origin.setY(rectangle.top() + ascender);
116 if (halign == KoSvgText::AnchorMiddle) {
117 origin.setX(rectangle.center().x());
118 } else if ((halign == KoSvgText::AnchorEnd && !isRtl) || (halign == KoSvgText::AnchorStart && isRtl)) {
119 origin.setX(rectangle.right());
120 }
121 } else {
122 if (writingMode == KoSvgText::VerticalRL) {
123 origin.setX(rectangle.right() - (lineHeight*0.5));
124 } else {
125 origin.setX(rectangle.left() + (lineHeight*0.5));
126 }
127
128 if (halign == KoSvgText::AnchorMiddle) {
129 origin.setY(rectangle.center().y());
130 } else if (halign == KoSvgText::AnchorEnd) {
131 origin.setY(rectangle.bottom());
132 }
133 }
134 }
135 if (!rectangle.contains(origin) && unwrappedText) {
136 origin = writingMode == KoSvgText::HorizontalTB? QPointF(origin.x(), rectangle.bottom()): QPointF(rectangle.center().x(), origin.y());
137 }
138 params->setProperty("shapeRect", QVariant(rectangle));
139 params->setProperty("origin", QVariant(origin));
140
141 KoSvgTextShape *textShape = dynamic_cast<KoSvgTextShape *>(factory->createShape( params, tool->canvas()->shapeController()->resourceManager()));
142
145 textShape->setBackground(properties.background());
146 }
148 textShape->setStroke(properties.stroke());
149 }
150
151 KUndo2Command *parentCommand = new KUndo2Command();
152
153 new KoKeepShapesSelectedCommand(tool->koSelection()->selectedShapes(), {}, tool->canvas()->selectedShapesProxy(), false, parentCommand);
154
155 KUndo2Command *cmd = tool->canvas()->shapeController()->addShape(textShape, 0, parentCommand);
156 parentCommand->setText(cmd->text());
157
158 if (m_flowShape) {
160
161 KoPathShape *path = dynamic_cast<KoPathShape*>(m_flowShape);
162 KoPathSegment segment;
163 if(path) {
164 segment = path->segmentAtPoint(m_dragStart, tool->handleGrabRect(m_dragStart));
165 }
166 if (segment.isValid()) {
167 int pos = textShape->posForIndex(textShape->plainText().size());
168 new KoSvgTextSetTextPathOnRangeCommand(textShape, m_flowShape, 0, pos, parentCommand);
169
171 const qreal grab = tool->grabSensitivityInPt();
172 QList<KoPathSegment> segments = path->segmentsAt(path->outlineRect().adjusted(-grab, -grab, grab, grab));
173 Q_FOREACH(KoPathSegment s, segments) {
174 if (s == segment) {
175 info.startOffset += (segment.nearestPoint(path->documentToShape(m_dragStart))*segment.length());
176 break;
177 }
178 info.startOffset += s.length();
179 qDebug() << info.startOffset << s.length();
180 }
181 qDebug() << "setting path at..." << info.startOffset << segments.size();
182 new SvgTextPathInfoChangeCommand(textShape, 2, info, parentCommand);
183 } else {
184 new KoSvgTextAddShapeCommand(textShape, m_flowShape, true, parentCommand);
185 }
186 }
187
188 new KoKeepShapesSelectedCommand({}, {textShape}, tool->canvas()->selectedShapesProxy(), true, parentCommand);
189 tool->canvas()->snapGuide()->reset();
190
191 return parentCommand;
192}
void setText(const KUndo2MagicString &text)
KUndo2MagicString text() const
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)
virtual KoShape * createShape(const KoProperties *params, KoDocumentResourceManager *documentResources=0) const
static KoShapeRegistry * instance()
QPointF absolutePosition(KoFlake::AnchorPosition anchor=KoFlake::Center) const
Definition KoShape.cpp:573
virtual void setPosition(const QPointF &position)
Set the position of the shape in pt.
Definition KoShape.cpp:267
@ 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
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
@ 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(), KoSnapGuide::reset(), 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 206 of file SvgCreateTextStrategy.cpp.

207{
208 QRectF rectangle = QRectF(m_dragStart, m_dragEnd).normalized();
209 return (rectangle.width() >= m_minSizeInline.width() || rectangle.height() >= m_minSizeInline.height()) && !m_modifiers.testFlag(Qt::ControlModifier);
210}

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 201 of file SvgCreateTextStrategy.cpp.

202{
203 m_modifiers = modifiers;
204}

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 57 of file SvgCreateTextStrategy.cpp.

58{
59 m_dragEnd = this->tool()->canvas()->snapGuide()->snap(mouseLocation, modifiers);
60 m_modifiers = modifiers;
61 const QRectF updateRect = QRectF(m_dragStart, m_dragEnd).normalized();
62 tool()->canvas()->updateCanvas(kisGrowRect(updateRect, 100));
63}
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 212 of file SvgCreateTextStrategy.cpp.

213{
214 return (m_flowShape)? true: false;
215}

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 46 of file SvgCreateTextStrategy.cpp.

47{
48 const QTransform originalPainterTransform = painter.transform();
49 painter.setTransform(converter.documentToView(), true);
50 KisHandlePainterHelper handlePainter(&painter, originalPainterTransform, 0.0, decorationThickness());
51
52 const QPolygonF poly(QRectF(m_dragStart, m_dragEnd));
53 handlePainter.setHandleStyle(KisHandleStyle::primarySelection());
54 handlePainter.drawRubberLine(poly);
55}
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: