Branch data Line data Source code
1 : : /***************************************************************************
2 : : qgstextrendererutils.h
3 : : -----------------
4 : : begin : May 2020
5 : : copyright : (C) Nyall Dawson
6 : : email : nyall dot dawson at gmail dot com
7 : : ***************************************************************************
8 : : * *
9 : : * This program is free software; you can redistribute it and/or modify *
10 : : * it under the terms of the GNU General Public License as published by *
11 : : * the Free Software Foundation; either version 2 of the License, or *
12 : : * (at your option) any later version. *
13 : : * *
14 : : ***************************************************************************/
15 : :
16 : : #ifndef QGSTEXTRENDERERUTILS_H
17 : : #define QGSTEXTRENDERERUTILS_H
18 : :
19 : : #include "qgis_sip.h"
20 : : #include "qgis_core.h"
21 : : #include "qgstextbackgroundsettings.h"
22 : : #include "qgstextshadowsettings.h"
23 : : #include "qgstextformat.h"
24 : : #include "qgstextmetrics.h"
25 : :
26 : : /**
27 : : * \class QgsTextRendererUtils
28 : : * \ingroup core
29 : : * \brief Utility functions for text rendering.
30 : : * \since QGIS 3.10
31 : : */
32 : : class CORE_EXPORT QgsTextRendererUtils
33 : : {
34 : : public:
35 : :
36 : : /**
37 : : * Decodes a string representation of a background shape type to a type.
38 : : */
39 : : static QgsTextBackgroundSettings::ShapeType decodeShapeType( const QString &string );
40 : :
41 : : /**
42 : : * Decodes a string representation of a background size type to a type.
43 : : */
44 : : static QgsTextBackgroundSettings::SizeType decodeBackgroundSizeType( const QString &string );
45 : :
46 : : /**
47 : : * Decodes a string representation of a background rotation type to a type.
48 : : */
49 : : static QgsTextBackgroundSettings::RotationType decodeBackgroundRotationType( const QString &string );
50 : :
51 : : /**
52 : : * Decodes a string representation of a shadow placement type to a type.
53 : : */
54 : : static QgsTextShadowSettings::ShadowPlacement decodeShadowPlacementType( const QString &string );
55 : :
56 : : /**
57 : : * Encodes a text \a orientation.
58 : : * \returns encoded string
59 : : * \see decodeTextOrientation()
60 : : */
61 : : static QString encodeTextOrientation( QgsTextFormat::TextOrientation orientation );
62 : :
63 : : /**
64 : : * Attempts to decode a string representation of a text orientation.
65 : : * \param name encoded text orientation name
66 : : * \param ok if specified, will be set to TRUE if the name was successfully decoded
67 : : * \returns decoded text orientation
68 : : * \see encodeTextOrientation()
69 : : */
70 : : static QgsTextFormat::TextOrientation decodeTextOrientation( const QString &name, bool *ok = nullptr );
71 : :
72 : : /**
73 : : * Converts a unit from an old (pre 3.0) label unit.
74 : : *
75 : : * \note Not available in Python bindings.
76 : : * \since QGIS 3.14
77 : : */
78 : : static QgsUnitTypes::RenderUnit convertFromOldLabelUnit( int val ) SIP_SKIP;
79 : :
80 : : /**
81 : : * Converts an encoded color value from a \a layer \a property.
82 : : *
83 : : * \note Not available in Python bindings.
84 : : * \since QGIS 3.14
85 : : */
86 : : static QColor readColor( QgsVectorLayer *layer, const QString &property, const QColor &defaultColor = Qt::black, bool withAlpha = true ) SIP_SKIP;
87 : :
88 : : #ifndef SIP_RUN
89 : :
90 : : /**
91 : : * \class CurvedGraphemePlacement
92 : : * \ingroup core
93 : : * \brief Contains placement information for a single grapheme in a curved text layout.
94 : : * \note Not available in Python bindings
95 : : * \since QGIS 3.20
96 : : */
97 : 0 : class CurvedGraphemePlacement
98 : : {
99 : : public:
100 : :
101 : : //! X coordinate of start of grapheme
102 : 0 : double x = 0;
103 : : //! Y coordinate of start of grapheme
104 : 0 : double y = 0;
105 : : //! Width of grapheme
106 : 0 : double width = 0;
107 : : //! Height of grapheme
108 : 0 : double height = 0;
109 : : //! Angle for grapheme, in radians
110 : 0 : double angle = 0;
111 : : //! Index of corresponding grapheme
112 : 0 : int graphemeIndex = 0;
113 : : };
114 : :
115 : : /**
116 : : * \class CurvePlacementProperties
117 : : * \ingroup core
118 : : * \brief Contains placement information for a curved text layout.
119 : : * \note Not available in Python bindings
120 : : * \since QGIS 3.20
121 : : */
122 : 0 : class CurvePlacementProperties
123 : : {
124 : : public:
125 : :
126 : : //! Placement information for all graphemes in text
127 : : QVector< QgsTextRendererUtils::CurvedGraphemePlacement > graphemePlacement;
128 : : //! Total count of upside down characters
129 : 0 : int upsideDownCharCount = 0;
130 : : //! TRUE if labeled section of line is calculated to be of right-to-left orientation
131 : 0 : bool labeledLineSegmentIsRightToLeft = false;
132 : : //! TRUE if the character placement had to be reversed in order to obtain upright labels on the segment
133 : 0 : bool flippedCharacterPlacementToGetUprightLabels = false;
134 : : };
135 : :
136 : : /**
137 : : * Controls behavior of curved text with respect to line directions
138 : : */
139 : : enum LabelLineDirection
140 : : {
141 : : RespectPainterOrientation, //!< Curved text will be placed respecting the painter orientation, and the actual line direction will be ignored
142 : : FollowLineDirection //!< Curved text placement will respect the line direction and ignore painter orientation
143 : : };
144 : :
145 : : #if 0
146 : : // TODO - refine API when used. We probably want to use QPolygonF here instead of QgsLineString!
147 : : static CurvePlacementProperties *generateCurvedTextPlacement( const QgsPrecalculatedTextMetrics &metrics, const QgsLineString *line, double offsetAlongLine, LabelLineDirection direction = RespectPainterOrientation, double maxConcaveAngle = -1, double maxConvexAngle = -1, bool uprightOnly = true ) SIP_FACTORY;
148 : : #endif
149 : :
150 : : /**
151 : : * Calculates curved text placement properties.
152 : : *
153 : : * \param metrics precalculated text metrics for text to render
154 : : * \param x array of linestring x coordinates
155 : : * \param y array of linestring y coordinates
156 : : * \param numPoints number of points in \a x, \a y arrays
157 : : * \param pathDistances vector of precalculated distances between vertices in \a x, \a y arrays
158 : : * \param offsetAlongLine offset along line at which to start the curved text placement
159 : : * \param direction controls placement of text with respect to painter orientation or line direction
160 : : * \param maxConcaveAngle maximum angle between characters for concave text, or -1 if not set
161 : : * \param maxConvexAngle maximum angle between characters for convex text, or -1 if not set
162 : : * \param uprightOnly set to TRUE if text should be placed in an upright orientation only, or FALSE to allow upside down text placement
163 : : *
164 : : * \returns calculated placement properties, or NULLPTR if placement could not be calculated. Caller takes ownership of the returned placement.
165 : : * \since QGIS 3.20
166 : : */
167 : : static CurvePlacementProperties *generateCurvedTextPlacement( const QgsPrecalculatedTextMetrics &metrics, const double *x, const double *y, int numPoints, const std::vector< double> &pathDistances, double offsetAlongLine, LabelLineDirection direction = RespectPainterOrientation, double maxConcaveAngle = -1, double maxConvexAngle = -1, bool uprightOnly = true ) SIP_SKIP;
168 : : #endif
169 : :
170 : : private:
171 : :
172 : : static CurvePlacementProperties *generateCurvedTextPlacementPrivate( const QgsPrecalculatedTextMetrics &metrics, const double *x, const double *y, int numPoints, const std::vector< double> &pathDistances, double offsetAlongLine, LabelLineDirection direction, double maxConcaveAngle = -1, double maxConvexAngle = -1, bool uprightOnly = true, bool isSecondAttempt = false ) SIP_SKIP;
173 : :
174 : : //! Returns TRUE if the next char position is found. The referenced parameters are updated.
175 : : static bool nextCharPosition( double charWidth, double segmentLength, const double *x, const double *y, int numPoints, int &index, double ¤tDistanceAlongSegment,
176 : : double &characterStartX, double &characterStartY, double &characterEndX, double &characterEndY );
177 : :
178 : : static void findLineCircleIntersection( double cx, double cy, double radius,
179 : : double x1, double y1, double x2, double y2,
180 : : double &xRes, double &yRes );
181 : : };
182 : :
183 : :
184 : : #endif // QGSTEXTRENDERERUTILS_H
|