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
573 bool singleNode() const;
574
576 struct KRITAFLAKE_EXPORT TextCursorChangeListener : public ShapeChangeListener {
577 void notifyShapeChanged(ChangeType type, KoShape *shape) override;
578 virtual void notifyCursorPosChanged(int pos, int anchor) = 0;
579 virtual void notifyMarkupChanged() = 0;
580 };
582 void notifyCursorPosChanged(int pos, int anchor);
584 void notifyMarkupChanged();
585
586 /*--------------- Converter functions ---------------*/
587
594 void convertCharTransformsToPreformatted(bool makeInlineSize = false);
595
601 void setCharacterTransformsFromLayout();
602
603 /*--------------- Loading / Saving ------------------*/
604
606 bool saveSvg(SvgSavingContext &context) override;
607 // Used by the html writer.
608 bool saveHtml(HtmlSavingContext &context);
609
611 void enterNodeSubtree();
613 void leaveNodeSubtree();
614
616 KoSvgTextShapeMementoSP getMemento();
617
619 void setMemento(const KoSvgTextShapeMementoSP memento);
620
622 void setMemento(const KoSvgTextShapeMementoSP memento, int pos, int anchor);
623
625 void debugParsing();
626
627 /***
628 * This blocks the shape from automatically calling relayout
629 * when the text or properties change. Relayout needs to be called
630 * in this mode.
631 * Used in the SVGTextLabel.
632 */
633 void setRelayoutBlocked(const bool disable);
634
640 bool relayoutIsBlocked() const;
641
650 void setFontMatchingDisabled(const bool disable);
651
656 bool fontMatchingDisabled() const;
657
667 static QList<QPainterPath> generateTextAreas(const QList<KoShape*> shapesInside, const QList<KoShape*> shapesSubtract, const KoSvgTextProperties &props);
668
669 void startBulkAction() override;
670 QRectF endBulkAction() override;
671
672protected:
673
674 void shapeChanged(ChangeType type, KoShape *shape) override;
675private:
676 friend class TestSvgText;
677 friend class KoSvgTextLoader;
684 int nextPos(int pos, bool visual);
685
692 int previousPos(int pos, bool visual);
693
699 QPainterPath defaultCursorShape();
700
701 void setMementoImpl(const KoSvgTextShapeMementoSP memento);
702
703 class Private;
704 QScopedPointer<Private> d;
705};
706
707Q_DECLARE_OPERATORS_FOR_FLAGS(KoSvgTextShape::DebugElements)
708
709class KRITAFLAKE_EXPORT KoSvgTextShapeMemento {
710public:
713private:
714 friend class KoSvgTextShape;
715};
716
726class KRITAFLAKE_EXPORT KoSvgTextNodeIndex {
727public:
730
736 KoSvgTextProperties *properties();
737
743 KoSvgText::TextOnPathInfo *textPathInfo();
744
749 KoShape *textPath();
750
751private:
752 // for internal factory use only
754
755private:
756 friend class KoSvgTextShape;
757 struct Private;
758 QScopedPointer<Private> d;
759
760};
761
772 QPointF finalPos;
773 QPointF advance;
774 qreal rotateDeg; // Rotation in radians.
775 int visualIndex = -1;
776 int logicalIndex = -1;
777 bool rtl = false;
778 bool middle = false;
780
781 // For use with std::sort
783 bool compare = false;
784 if (a.visualIndex >= 0
785 && b.visualIndex >= 0) {
786 compare = a.visualIndex < b.visualIndex;
787 } else {
788 compare = a.logicalIndex < b.logicalIndex;
789 }
790 return compare;
791 }
793 return a.logicalIndex < b.logicalIndex;
794 }
795};
796
798{
799
800public:
804
805 KoShape *createDefaultShape(KoDocumentResourceManager *documentResources = 0) const override;
806
807 KoShape *createShape(const KoProperties *params, KoDocumentResourceManager *documentResources = 0) const override;
809 bool supports(const QDomElement &e, KoShapeLoadingContext &context) const override;
810};
811
812
813
814#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:669
virtual void paintStroke(QPainter &painter) const
paintStroke paints the shape's stroked outline
Definition KoShape.cpp:195
virtual QSizeF size() const
Get the size of the shape in pt.
Definition KoShape.cpp:740
virtual QRectF outlineRect() const
Definition KoShape.cpp:566
virtual QPainterPath outline() const
Definition KoShape.cpp:559
virtual QVector< PaintOrder > paintOrder() const
paintOrder
Definition KoShape.cpp:693
virtual KoShapeStrokeModelSP stroke() const
Definition KoShape.cpp:890
virtual void setResolution(qreal xRes, qreal yRes)
Definition KoShape.cpp:1213
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:299
virtual void shapeChanged(ChangeType type, KoShape *shape=0)
Definition KoShape.cpp:1059
virtual void setStroke(KoShapeStrokeModelSP stroke)
Definition KoShape.cpp:904
virtual void setBackground(QSharedPointer< KoShapeBackground > background)
Definition KoShape.cpp:751
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:172
virtual QSharedPointer< KoShapeBackground > background() const
Definition KoShape.cpp:759
virtual void setSize(const QSizeF &size)
Resize the shape.
Definition KoShape.cpp:248
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
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.