Krita Source Code Documentation
Loading...
Searching...
No Matches
KoSvgTextShape.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef KOSVGTEXTSHAPE_H
8#define KOSVGTEXTSHAPE_H
9
10#include "kritaflake_export.h"
11#include <KoSvgTextProperties.h>
12
13#include <KoShapeFactoryBase.h>
14#include <SvgShape.h>
15#include <KoShape.h>
17#include <KoSvgText.h>
19#include <QFlags>
20
24class KoPathShape;
26
27#define KoSvgTextShape_SHAPEID "KoSvgTextShapeID"
28#define KoSvgTextShape_TEXTCONTOURGROUP "krita:textContourGroup"
32class KRITAFLAKE_EXPORT KoSvgTextShape : public KoShape, public SvgShape, public KoShapeBulkActionInterface
33{
34public:
37 ~KoSvgTextShape() override;
38
39 static const QString &defaultPlaceholderText();
40
41 KoShape* cloneShape() const override;
42
43 void paint(QPainter &painter) const override;
44 void paintStroke(QPainter &painter) const override;
45
46 QPainterPath outline() const override;
47 QRectF outlineRect() const override;
48 QRectF boundingRect() const override;
49
50 QSizeF size() const override;
51 void setSize(const QSizeF &size) override;
52
53 enum class DebugElement {
54 CharBbox = 1 << 0,
55 LineBox = 1 << 1,
56 };
58 void paintDebug(QPainter &painter, DebugElements elements) const;
59
65 void relayout() const;
66
74 KoShape * textOutline() const;
75
76 enum TextType {
77 PrePositionedText = 0,
79 PreformattedText,
82 TextInShape
83 };
94 TextType textType() const;
95
100 void setShapesInside(QList<KoShape*> shapesInside);
101
110 void addShapeContours(QList<KoShape*> shapes, const bool inside = true);
116 bool shapeInContours(KoShape *shape);
117
125 void removeShapesFromContours(QList<KoShape*> shapes, bool callUpdate = true, bool cleanup = true);
126
136 void moveShapeInsideToIndex(KoShape* shapeInside, const int index);
137
147 bool setTextPathOnRange(KoShape *textPath, const int startPos = -1, const int endPos = -1);
148
157 QList<KoShape*> textPathsAtRange(const int startPos = -1, const int endPos = -1);
158
164 void addTextPathAtEnd(KoShape *textPath);
165
171
176 void setShapesSubtract(QList<KoShape*> shapesSubtract);
177
188 QList<QPainterPath> textWrappingAreas() const;
189
195
200 KoShapeManager *internalShapeManager() const;
201
202 QMap<QString, QString> shapeTypeSpecificStyles(SvgSavingContext &context) const;
203
204 void setResolution(qreal xRes, qreal yRes) override;
205
206
207
210 int posLeft(int pos, bool visual = false);
213 int posRight(int pos, bool visual = false);
215 int posUp(int pos, bool visual = false);
217 int posDown(int pos, bool visual = false);
218
226 int lineStart(int pos);
234 int lineEnd(int pos);
235
243 int wordLeft(int pos, bool visual = false);
251 int wordRight(int pos, bool visual = false);
252
261 int nextIndex(int pos);
262
269 int previousIndex(int pos);
270
277 int nextLine(int pos);
284 int previousLine(int pos);
285
292 int wordEnd(int pos);
299 int wordStart(int pos);
300
308 QPainterPath cursorForPos(int pos, QLineF &caret, QColor &color, double bidiFlagSize = 1.0);
309
318 QPainterPath selectionBoxes(int pos, int anchor);
319
320 QPainterPath underlines(int pos, int anchor,
321 KoSvgText::TextDecorations decor,
323 qreal minimum, bool thick);
324
334 int posForPoint(QPointF point, int start = -1, int end = -1, bool *overlaps = nullptr);
335
350 int posForPointLineSensitive(QPointF point);
359 int posForIndex(int index, bool firstIndex = false, bool skipSynthetic = false) const;
366 int indexForPos(int pos) const;
367
374 QPointF initialTextPosition() const;
375
376 /*--------------- Edit text ---------------*/
384 bool insertText(int pos, QString text);
385
401 bool removeText(int &index, int &length);
402
404 KoSvgTextProperties propertiesForPos(const int pos, bool inherited = false) const;
405
413 QList<KoSvgTextProperties> propertiesForRange(const int startPos, const int endPos, bool inherited = false) const;
414
421 void setPropertiesAtPos(int pos, KoSvgTextProperties properties);
422
438 void mergePropertiesIntoRange(const int startPos,
439 const int endPos,
440 const KoSvgTextProperties properties,
441 const QSet<KoSvgTextProperties::PropertyId> removeProperties = QSet<KoSvgTextProperties::PropertyId>());
442
443
451 std::unique_ptr<KoSvgTextShape> copyRange(int index, int length) const;
452
461 bool insertRichText(int pos, const KoSvgTextShape *richText, bool inheritPropertiesIfPossible = false);
462
464 void cleanUp();
465
477 bool setCharacterTransformsOnRange(const int startPos, const int endPos, const QVector<QPointF> positions, const QVector<qreal> rotateDegrees, const bool deltaPosition = true);
478
486 QList<KoSvgTextCharacterInfo> getPositionsAndRotationsForRange(const int startPos, const int endPos) const;
487
491 void removeTransformsFromRange(const int startPos, const int endPos);
492
493/*****************************************************************************
494 * KoSvgTextNodeIndex functions.
495 *
496 * SVG Text is internally a tree of nodes, with each node having text
497 * properties, and the child nodes furthest from the root having text content.
498 * While we can access the latter by using ranges, and we can easily access
499 * the root node by using -1, there's no way to access the nodes that may
500 * be inbetween. KoSvgTextNodeIndex allows accessing these nodes.
501 *
502 ****************************************************************************/
503
509 KoSvgTextNodeIndex findNodeIndexForPropertyId(KoSvgTextProperties::PropertyId propertyId);
510
518 QPair<int, int> findRangeForNodeIndex(const KoSvgTextNodeIndex &node) const;
519
528 KoSvgTextNodeIndex topLevelNodeForPos(int pos) const;
529
539 KoSvgTextNodeIndex nodeForTextPath(KoShape *textPath) const;
540
541 /*--------------- Properties ---------------*/
542
543 KoSvgTextProperties textProperties() const;
545 void setBackground(QSharedPointer<KoShapeBackground> background) override;
546 KoShapeStrokeModelSP stroke() const override;
547 void setStroke(KoShapeStrokeModelSP stroke) override;
548 QVector<PaintOrder> paintOrder() const override;
549 void setPaintOrder(PaintOrder first, PaintOrder second) override;
550
551
558 QString plainText();
559
565 KoSvgText::WritingMode writingMode() const;
566
572 KoSvgText::Direction direction() const;
573
580 bool singleNode() const;
581
583 struct KRITAFLAKE_EXPORT TextCursorChangeListener : public ShapeChangeListener {
584 void notifyShapeChanged(ChangeType type, KoShape *shape) override;
585 virtual void notifyCursorPosChanged(int pos, int anchor) = 0;
586 virtual void notifyMarkupChanged() = 0;
587 };
589 void notifyCursorPosChanged(int pos, int anchor);
591 void notifyMarkupChanged();
592
593 /*--------------- Converter functions ---------------*/
594
601 void convertCharTransformsToPreformatted(bool makeInlineSize = false);
602
608 void setCharacterTransformsFromLayout();
609
610 /*--------------- Loading / Saving ------------------*/
611
613 bool saveSvg(SvgSavingContext &context) override;
614 // Used by the html writer.
615 bool saveHtml(HtmlSavingContext &context);
616
618 void enterNodeSubtree();
620 void leaveNodeSubtree();
621
623 KoSvgTextShapeMementoSP getMemento();
624
626 void setMemento(const KoSvgTextShapeMementoSP memento);
627
629 void setMemento(const KoSvgTextShapeMementoSP memento, int pos, int anchor);
630
632 void debugParsing();
633
634 /***
635 * This blocks the shape from automatically calling relayout
636 * when the text or properties change. Relayout needs to be called
637 * in this mode.
638 * Used in the SVGTextLabel.
639 */
640 void setRelayoutBlocked(const bool disable);
641
647 bool relayoutIsBlocked() const;
648
657 void setFontMatchingDisabled(const bool disable);
658
663 bool fontMatchingDisabled() const;
664
674 static QList<QPainterPath> generateTextAreas(const QList<KoShape*> shapesInside, const QList<KoShape*> shapesSubtract, const KoSvgTextProperties &props);
675
676 void startBulkAction() override;
677 QRectF endBulkAction() override;
678
679protected:
680
681 void shapeChanged(ChangeType type, KoShape *shape) override;
682private:
683 friend class TestSvgText;
684 friend class KoSvgTextLoader;
691 int nextPos(int pos, bool visual);
692
699 int previousPos(int pos, bool visual);
700
706 QPainterPath defaultCursorShape();
707
708 void setMementoImpl(const KoSvgTextShapeMementoSP memento);
709
710 class Private;
711 QScopedPointer<Private> d;
712};
713
714Q_DECLARE_OPERATORS_FOR_FLAGS(KoSvgTextShape::DebugElements)
715
716class KRITAFLAKE_EXPORT KoSvgTextShapeMemento {
717public:
720private:
721 friend class KoSvgTextShape;
722};
723
733class KRITAFLAKE_EXPORT KoSvgTextNodeIndex {
734public:
737
743 KoSvgTextProperties *properties();
744
750 KoSvgText::TextOnPathInfo *textPathInfo();
751
756 KoShape *textPath();
757
758private:
759 // for internal factory use only
761
762private:
763 friend class KoSvgTextShape;
764 struct Private;
765 QScopedPointer<Private> d;
766
767};
768
779 QPointF finalPos;
780 QPointF advance;
781 qreal rotateDeg; // Rotation in radians.
782 int visualIndex = -1;
783 int logicalIndex = -1;
784 bool rtl = false;
785 bool middle = false;
787
788 // For use with std::sort
790 bool compare = false;
791 if (a.visualIndex >= 0
792 && b.visualIndex >= 0) {
793 compare = a.visualIndex < b.visualIndex;
794 } else {
795 compare = a.logicalIndex < b.logicalIndex;
796 }
797 return compare;
798 }
800 return a.logicalIndex < b.logicalIndex;
801 }
802};
803
805{
806
807public:
811
812 KoShape *createDefaultShape(KoDocumentResourceManager *documentResources = 0) const override;
813
814 KoShape *createShape(const KoProperties *params, KoDocumentResourceManager *documentResources = 0) const override;
816 bool supports(const QDomElement &e, KoShapeLoadingContext &context) const override;
817};
818
819
820
821#endif // KOSVGTEXTSHAPE_H
qreal length(const QPointF &vec)
Definition Ellipse.cc:82
QSharedPointer< KoSvgTextShapeMemento > KoSvgTextShapeMementoSP
The HtmlSavingContext class provides context for saving a flake-based document to html.
The position of a path point within a path shape.
Definition KoPathShape.h:63
virtual void setPaintOrder(PaintOrder first, PaintOrder second)
setPaintOrder set the paint order. As there's only three entries in any given paintorder,...
Definition KoShape.cpp:664
virtual void paintStroke(QPainter &painter) const
paintStroke paints the shape's stroked outline
Definition KoShape.cpp:196
virtual QSizeF size() const
Get the size of the shape in pt.
Definition KoShape.cpp:735
virtual QRectF outlineRect() const
Definition KoShape.cpp:561
virtual QPainterPath outline() const
Definition KoShape.cpp:554
virtual QVector< PaintOrder > paintOrder() const
paintOrder
Definition KoShape.cpp:688
virtual KoShapeStrokeModelSP stroke() const
Definition KoShape.cpp:885
virtual void setResolution(qreal xRes, qreal yRes)
Definition KoShape.cpp:1208
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:300
virtual void shapeChanged(ChangeType type, KoShape *shape=0)
Definition KoShape.cpp:1054
virtual void setStroke(KoShapeStrokeModelSP stroke)
Definition KoShape.cpp:899
virtual void setBackground(QSharedPointer< KoShapeBackground > background)
Definition KoShape.cpp:746
virtual void paint(QPainter &painter) const =0
Paint the shape fill The class extending this one is responsible for painting itself....
ChangeType
Used by shapeChanged() to select which change was made.
Definition KoShape.h:92
virtual KoShape * cloneShape() const
creates a deep copy of the shape or shape's subtree
Definition KoShape.cpp:173
virtual QSharedPointer< KoShapeBackground > background() const
Definition KoShape.cpp:754
virtual void setSize(const QSizeF &size)
Resize the shape.
Definition KoShape.cpp:249
The KoSvgTextNodeIndex class.
QScopedPointer< Private > d
KoSvgTextShapeFactory()
constructor
bool supports(const QDomElement &e, KoShapeLoadingContext &context) const override
Reimplemented.
KoShape * createDefaultShape(KoDocumentResourceManager *documentResources=0) const override
KoShape * createShape(const KoProperties *params, KoDocumentResourceManager *documentResources=0) const override
QString plainText()
plainText plain text of all text inside this text shape, without the bidi controls or any transforms.
@ InlineWrap
Uses inline size to wrap and preserves spaces.
QPointF initialTextPosition() const
initialTextPosition Returns the initial text position as per SVG algorithm. The eventual result of th...
QScopedPointer< Private > d
Q_DECLARE_FLAGS(DebugElements, DebugElement)
QList< KoShape * > shapesSubtract() const
shapesSubtract
QList< KoShape * > shapesInside() const
shapesInside
Context for saving svg files.
An interface providing svg loading and saving routines.
Definition SvgShape.h:18
virtual bool saveSvg(SvgSavingContext &context)
Saves data utilizing specified svg saving context.
Definition SvgShape.cpp:14
Q_DECLARE_OPERATORS_FOR_FLAGS(KisBaseRectsWalker::SubtreeVisitFlags)
TextDecorationStyle
Style of the text-decoration.
Definition KoSvgText.h:265
Direction
Base direction used by Bidi algorithm.
Definition KoSvgText.h:48
Interface for bulk actions on shapes.
virtual QRectF endBulkAction()=0
virtual void startBulkAction()=0
The KoSvgTextCharacterInfo class This is a small struct to convey information about character positio...
KoSvgText::FontMetrics metrics
<Whether the character is in the middle of a cluster.
static bool logicalLessThan(const KoSvgTextCharacterInfo &a, const KoSvgTextCharacterInfo &b)
static bool visualLessThan(const KoSvgTextCharacterInfo &a, const KoSvgTextCharacterInfo &b)
ShapeChangeListener so we can inform any text cursors that the cursor needs updating.
virtual void notifyCursorPosChanged(int pos, int anchor)=0
The FontMetrics class A class to keep track of a variety of font metrics. Note that values are in Fre...
Definition KoSvgText.h:327
The LineBox struct.