QGIS API Documentation  3.12.1-București (121cc00ff0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgstextrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextrenderer.h
3  -----------------
4  begin : September 2015
5  copyright : (C) Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSTEXTRENDERER_H
18 #define QGSTEXTRENDERER_H
19 
20 #include "qgis_sip.h"
21 #include "qgis_core.h"
22 #include "qgsmapunitscale.h"
23 #include "qgsunittypes.h"
25 
26 #include <QSharedData>
27 #include <QPainter>
28 #include <QPicture>
29 #include <QDomElement>
30 
32 class QgsTextBufferSettingsPrivate;
33 class QgsTextBackgroundSettingsPrivate;
34 class QgsTextShadowSettingsPrivate;
35 class QgsTextMaskSettingsPrivate;
36 class QgsTextSettingsPrivate;
37 class QgsVectorLayer;
38 class QgsPaintEffect;
39 class QgsMarkerSymbol;
41 
49 class CORE_EXPORT QgsTextBufferSettings
50 {
51  public:
52 
54 
60 
65  QgsTextBufferSettings &operator=( const QgsTextBufferSettings &other );
66 
68 
73  bool enabled() const;
74 
80  void setEnabled( bool enabled );
81 
87  double size() const;
88 
95  void setSize( double size );
96 
102  QgsUnitTypes::RenderUnit sizeUnit() const;
103 
110  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
111 
118  QgsMapUnitScale sizeMapUnitScale() const;
119 
127  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
128 
133  QColor color() const;
134 
140  void setColor( const QColor &color );
141 
148  bool fillBufferInterior() const;
149 
156  void setFillBufferInterior( bool fill );
157 
163  double opacity() const;
164 
171  void setOpacity( double opacity );
172 
177  Qt::PenJoinStyle joinStyle() const;
178 
184  void setJoinStyle( Qt::PenJoinStyle style );
185 
190  QPainter::CompositionMode blendMode() const;
191 
197  void setBlendMode( QPainter::CompositionMode mode );
198 
203  void readFromLayer( QgsVectorLayer *layer );
204 
209  void readXml( const QDomElement &elem );
210 
215  QDomElement writeXml( QDomDocument &doc ) const;
216 
222  QgsPaintEffect *paintEffect() const;
223 
229  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
230 
235  void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );
236 
237  private:
238 
239  QSharedDataPointer<QgsTextBufferSettingsPrivate> d;
240 
241 };
242 
251 class CORE_EXPORT QgsTextBackgroundSettings
252 {
253  public:
254 
259  {
260  ShapeRectangle = 0,
266  };
267 
271  enum SizeType
272  {
273  SizeBuffer = 0,
275  SizePercent
276  };
277 
282  {
283  RotationSync = 0,
285  RotationFixed
286  };
287 
289 
295 
296  QgsTextBackgroundSettings &operator=( const QgsTextBackgroundSettings &other );
297 
299 
304  bool enabled() const;
305 
311  void setEnabled( bool enabled );
312 
317  ShapeType type() const;
318 
324  void setType( ShapeType type );
325 
330  QString svgFile() const;
331 
338  void setSvgFile( const QString &file );
339 
347  QgsMarkerSymbol *markerSymbol() const;
348 
356  void setMarkerSymbol( QgsMarkerSymbol *symbol SIP_TRANSFER );
357 
364  SizeType sizeType() const;
365 
373  void setSizeType( SizeType type );
374 
383  QSizeF size() const;
384 
394  void setSize( QSizeF size );
395 
403  QgsUnitTypes::RenderUnit sizeUnit() const;
404 
413  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
414 
421  QgsMapUnitScale sizeMapUnitScale() const;
422 
430  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
431 
437  RotationType rotationType() const;
438 
445  void setRotationType( RotationType type );
446 
452  double rotation() const;
453 
459  void setRotation( double rotation );
460 
467  QPointF offset() const;
468 
476  void setOffset( QPointF offset );
477 
483  QgsUnitTypes::RenderUnit offsetUnit() const;
484 
491  void setOffsetUnit( QgsUnitTypes::RenderUnit units );
492 
499  QgsMapUnitScale offsetMapUnitScale() const;
500 
508  void setOffsetMapUnitScale( const QgsMapUnitScale &scale );
509 
516  QSizeF radii() const;
517 
526  void setRadii( QSizeF radii );
527 
533  QgsUnitTypes::RenderUnit radiiUnit() const;
534 
541  void setRadiiUnit( QgsUnitTypes::RenderUnit units );
542 
549  QgsMapUnitScale radiiMapUnitScale() const;
550 
558  void setRadiiMapUnitScale( const QgsMapUnitScale &scale );
559 
565  double opacity() const;
566 
573  void setOpacity( double opacity );
574 
579  QPainter::CompositionMode blendMode() const;
580 
586  void setBlendMode( QPainter::CompositionMode mode );
587 
593  QColor fillColor() const;
594 
601  void setFillColor( const QColor &color );
602 
608  QColor strokeColor() const;
609 
616  void setStrokeColor( const QColor &color );
617 
624  double strokeWidth() const;
625 
632  void setStrokeWidth( double width );
633 
639  QgsUnitTypes::RenderUnit strokeWidthUnit() const;
640 
647  void setStrokeWidthUnit( QgsUnitTypes::RenderUnit units );
648 
655  QgsMapUnitScale strokeWidthMapUnitScale() const;
656 
664  void setStrokeWidthMapUnitScale( const QgsMapUnitScale &scale );
665 
670  Qt::PenJoinStyle joinStyle() const;
671 
677  void setJoinStyle( Qt::PenJoinStyle style );
678 
684  QgsPaintEffect *paintEffect() const;
685 
691  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
692 
697  void readFromLayer( QgsVectorLayer *layer );
698 
703  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
704 
709  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
710 
715  void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );
716 
717  private:
718 
719  QSharedDataPointer<QgsTextBackgroundSettingsPrivate> d;
720 
721 };
722 
731 class CORE_EXPORT QgsTextShadowSettings
732 {
733  public:
734 
739  {
740  ShadowLowest = 0,
743  ShadowShape
744  };
745 
747 
753 
754  QgsTextShadowSettings &operator=( const QgsTextShadowSettings &other );
755 
757 
762  bool enabled() const;
763 
769  void setEnabled( bool enabled );
770 
777  QgsTextShadowSettings::ShadowPlacement shadowPlacement() const;
778 
786  void setShadowPlacement( QgsTextShadowSettings::ShadowPlacement placement );
787 
793  int offsetAngle() const;
794 
801  void setOffsetAngle( int angle );
802 
809  double offsetDistance() const;
810 
818  void setOffsetDistance( double distance );
819 
825  QgsUnitTypes::RenderUnit offsetUnit() const;
826 
833  void setOffsetUnit( QgsUnitTypes::RenderUnit units );
834 
841  QgsMapUnitScale offsetMapUnitScale() const;
842 
850  void setOffsetMapUnitScale( const QgsMapUnitScale &scale );
851 
856  bool offsetGlobal() const;
857 
862  void setOffsetGlobal( bool global );
863 
869  double blurRadius() const;
870 
877  void setBlurRadius( double blurRadius );
878 
884  QgsUnitTypes::RenderUnit blurRadiusUnit() const;
885 
892  void setBlurRadiusUnit( QgsUnitTypes::RenderUnit units );
893 
900  QgsMapUnitScale blurRadiusMapUnitScale() const;
901 
909  void setBlurRadiusMapUnitScale( const QgsMapUnitScale &scale );
910 
915  bool blurAlphaOnly() const;
916 
923  void setBlurAlphaOnly( bool alphaOnly );
924 
930  double opacity() const;
931 
938  void setOpacity( double opacity );
939 
944  int scale() const;
945 
951  void setScale( int scale );
952 
957  QColor color() const;
958 
964  void setColor( const QColor &color );
965 
970  QPainter::CompositionMode blendMode() const;
971 
977  void setBlendMode( QPainter::CompositionMode mode );
978 
983  void readFromLayer( QgsVectorLayer *layer );
984 
989  void readXml( const QDomElement &elem );
990 
995  QDomElement writeXml( QDomDocument &doc ) const;
996 
1001  void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );
1002 
1003  private:
1004 
1005  QSharedDataPointer<QgsTextShadowSettingsPrivate> d;
1006 
1007 };
1008 
1009 
1019 class CORE_EXPORT QgsTextMaskSettings
1020 {
1021  public:
1022 
1027  {
1028  MaskBuffer = 0
1029  };
1030 
1032 
1037  QgsTextMaskSettings( const QgsTextMaskSettings &other );
1038 
1043  QgsTextMaskSettings &operator=( const QgsTextMaskSettings &other );
1044 
1046 
1050  bool enabled() const;
1051 
1055  void setEnabled( bool );
1056 
1061  MaskType type() const;
1062 
1068  void setType( MaskType type );
1069 
1075  double size() const;
1076 
1083  void setSize( double size );
1084 
1090  QgsUnitTypes::RenderUnit sizeUnit() const;
1091 
1098  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
1099 
1106  QgsMapUnitScale sizeMapUnitScale() const;
1107 
1115  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
1116 
1121  Qt::PenJoinStyle joinStyle() const;
1122 
1128  void setJoinStyle( Qt::PenJoinStyle style );
1129 
1135  double opacity() const;
1136 
1143  void setOpacity( double opacity );
1144 
1150  QgsPaintEffect *paintEffect() const;
1151 
1157  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
1158 
1163  void readXml( const QDomElement &elem );
1164 
1169  QDomElement writeXml( QDomDocument &doc ) const;
1170 
1176  QgsSymbolLayerReferenceList maskedSymbolLayers() const;
1177 
1183  void setMaskedSymbolLayers( QgsSymbolLayerReferenceList maskedLayers );
1184 
1188  void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );
1189 
1190  private:
1191 
1192  QSharedDataPointer<QgsTextMaskSettingsPrivate> d;
1193 };
1194 
1203 class CORE_EXPORT QgsTextFormat
1204 {
1205  public:
1206 
1209  {
1213  };
1214 
1215  QgsTextFormat();
1216 
1221  QgsTextFormat( const QgsTextFormat &other );
1222 
1223  QgsTextFormat &operator=( const QgsTextFormat &other );
1224 
1225  ~QgsTextFormat();
1226 
1231  QgsTextBufferSettings &buffer() { return mBufferSettings; }
1232 
1237  SIP_SKIP QgsTextBufferSettings buffer() const { return mBufferSettings; }
1238 
1244  void setBuffer( const QgsTextBufferSettings &bufferSettings ) { mBufferSettings = bufferSettings; }
1245 
1250  QgsTextBackgroundSettings &background() { return mBackgroundSettings; }
1251 
1256  SIP_SKIP QgsTextBackgroundSettings background() const { return mBackgroundSettings; }
1257 
1263  void setBackground( const QgsTextBackgroundSettings &backgroundSettings ) { mBackgroundSettings = backgroundSettings; }
1264 
1269  QgsTextShadowSettings &shadow() { return mShadowSettings; }
1270 
1275  SIP_SKIP QgsTextShadowSettings shadow() const { return mShadowSettings; }
1276 
1282  void setShadow( const QgsTextShadowSettings &shadowSettings ) { mShadowSettings = shadowSettings; }
1283 
1288  QgsTextMaskSettings &mask() { return mMaskSettings; }
1289 
1296  SIP_SKIP QgsTextMaskSettings mask() const { return mMaskSettings; }
1297 
1305  void setMask( const QgsTextMaskSettings &maskSettings ) { mMaskSettings = maskSettings; }
1306 
1316  QFont font() const;
1317 
1326  QFont scaledFont( const QgsRenderContext &context ) const;
1327 
1337  void setFont( const QFont &font );
1338 
1344  QString namedStyle() const;
1345 
1352  void setNamedStyle( const QString &style );
1353 
1359  double size() const;
1360 
1367  void setSize( double size );
1368 
1375  QgsUnitTypes::RenderUnit sizeUnit() const;
1376 
1384  void setSizeUnit( QgsUnitTypes::RenderUnit unit );
1385 
1392  QgsMapUnitScale sizeMapUnitScale() const;
1393 
1400  void setSizeMapUnitScale( const QgsMapUnitScale &scale );
1401 
1406  QColor color() const;
1407 
1413  void setColor( const QColor &color );
1414 
1420  double opacity() const;
1421 
1428  void setOpacity( double opacity );
1429 
1434  QPainter::CompositionMode blendMode() const;
1435 
1441  void setBlendMode( QPainter::CompositionMode mode );
1442 
1449  double lineHeight() const;
1450 
1458  void setLineHeight( double height );
1459 
1465  TextOrientation orientation() const;
1466 
1472  void setOrientation( TextOrientation orientation );
1473 
1479  QColor previewBackgroundColor() const;
1480 
1486  void setPreviewBackgroundColor( const QColor &color );
1487 
1492  void readFromLayer( QgsVectorLayer *layer );
1493 
1498  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
1499 
1504  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
1505 
1511  QMimeData *toMimeData() const SIP_FACTORY;
1512 
1520  static QgsTextFormat fromQFont( const QFont &font );
1521 
1529  QFont toQFont() const;
1530 
1536  static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );
1537 
1542  bool containsAdvancedEffects() const;
1543 
1549  bool fontFound() const { return mTextFontFound; }
1550 
1557  QString resolvedFontFamily() const { return mTextFontFamily; }
1558 
1564  QgsPropertyCollection &dataDefinedProperties();
1565 
1572  const QgsPropertyCollection &dataDefinedProperties() const SIP_SKIP;
1573 
1580  void setDataDefinedProperties( const QgsPropertyCollection &collection );
1581 
1586  void updateDataDefinedProperties( QgsRenderContext &context );
1587 
1596  static QPixmap textFormatPreviewPixmap( const QgsTextFormat &format, QSize size, const QString &previewText = QString(), int padding = 0 );
1597 
1598  private:
1599 
1600  QgsTextBufferSettings mBufferSettings;
1601  QgsTextBackgroundSettings mBackgroundSettings;
1602  QgsTextShadowSettings mShadowSettings;
1603  QgsTextMaskSettings mMaskSettings;
1604 
1605  QString mTextFontFamily;
1606  bool mTextFontFound = true;
1607 
1608  QSharedDataPointer<QgsTextSettingsPrivate> d;
1609 
1610 };
1611 
1620 class CORE_EXPORT QgsTextRenderer
1621 {
1622  public:
1623 
1626  {
1627  Rect = 0,
1630  };
1631 
1634  {
1635  Text = 0,
1639  };
1640 
1643  {
1644  AlignLeft = 0,
1647  };
1648 
1657  static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
1658 
1659  // TODO QGIS 4.0 -- remove drawAsOutlines from below methods!
1660 
1674  static void drawText( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1675  QgsRenderContext &context, const QgsTextFormat &format,
1676  bool drawAsOutlines = true );
1677 
1691  static void drawText( QPointF point, double rotation, HAlignment alignment, const QStringList &textLines,
1692  QgsRenderContext &context, const QgsTextFormat &format,
1693  bool drawAsOutlines = true );
1694 
1711  static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
1712  QgsRenderContext &context, const QgsTextFormat &format,
1713  TextPart part, bool drawAsOutlines = true );
1714 
1731  static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QStringList &textLines,
1732  QgsRenderContext &context, const QgsTextFormat &format,
1733  TextPart part, bool drawAsOutlines = true );
1734 
1741  static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format );
1742 
1750  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
1751  QFontMetricsF *fontMetrics = nullptr );
1752 
1761  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode,
1762  QFontMetricsF *fontMetrics = nullptr );
1763 
1764  private:
1765 
1766  struct Component
1767  {
1769  QString text;
1771  QPointF origin;
1773  bool useOrigin = false;
1775  double rotation = 0.0;
1777  double rotationOffset = 0.0;
1779  QPointF center;
1781  QSizeF size;
1783  QPointF offset;
1785  QPicture picture;
1786 
1791  double pictureBuffer = 0.0;
1793  double dpiRatio = 1.0;
1795  HAlignment hAlign = AlignLeft;
1796  };
1797 
1798  static void drawBuffer( QgsRenderContext &context,
1799  const Component &component,
1800  const QgsTextFormat &format,
1801  const QFontMetricsF *fontMetrics );
1802 
1803  static void drawBackground( QgsRenderContext &context,
1804  Component component,
1805  const QgsTextFormat &format,
1806  const QStringList &textLines,
1807  DrawMode mode = Rect );
1808 
1809  static void drawShadow( QgsRenderContext &context,
1810  const Component &component,
1811  const QgsTextFormat &format );
1812 
1813  static void drawMask( QgsRenderContext &context,
1814  const Component &component,
1815  const QgsTextFormat &format );
1816 
1817  static void drawText( QgsRenderContext &context,
1818  const Component &component,
1819  const QgsTextFormat &format );
1820 
1821  static void drawTextInternal( TextPart drawType,
1822  QgsRenderContext &context,
1823  const QgsTextFormat &format,
1824  const Component &component,
1825  const QStringList &textLines,
1826  const QFontMetricsF *fontMetrics,
1827  HAlignment alignment,
1828  DrawMode mode = Rect );
1829 
1831  friend class QgsLabelPreview;
1832 
1833  static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
1834 
1835 
1836 };
1837 
1844 class CORE_EXPORT QgsTextRendererUtils
1845 {
1846  public:
1847 
1851  static QgsTextBackgroundSettings::ShapeType decodeShapeType( const QString &string );
1852 
1856  static QgsTextBackgroundSettings::SizeType decodeBackgroundSizeType( const QString &string );
1857 
1861  static QgsTextBackgroundSettings::RotationType decodeBackgroundRotationType( const QString &string );
1862 
1866  static QgsTextShadowSettings::ShadowPlacement decodeShadowPlacementType( const QString &string );
1867 
1873  static QString encodeTextOrientation( QgsTextFormat::TextOrientation orientation );
1874 
1882  static QgsTextFormat::TextOrientation decodeTextOrientation( const QString &name, bool *ok = nullptr );
1883 };
1884 
1885 
1886 #endif // QGSTEXTRENDERER_H
TextOrientation
Text orientation.
The class is used as a container of context for various read/write operations on other objects...
RotationType
Methods for determining the rotation of the background shape.
Horizontally or vertically oriented text based on rotation (only available for map labeling) ...
void setMask(const QgsTextMaskSettings &maskSettings)
Sets the text&#39;s masking settings.
MaskType
Mask shape types.
Text at point of origin draw mode.
bool fontFound() const
Returns true if the specified font was found on the system, or false if the font was not found and a ...
Utility functions for text rendering.
QgsTextBackgroundSettings background() const
Returns a reference to the text background settings.
Draw shadow under buffer.
ShadowPlacement
Placement positions for text shadow.
QgsTextShadowSettings & shadow()
Returns a reference to the text drop shadow settings.
HAlignment
Horizontal alignment.
Base class for visual effects which can be applied to QPicture drawings.
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
void setBackground(const QgsTextBackgroundSettings &backgroundSettings)
Sets the text&#39;s background settings.q.
Label-specific draw mode.
Container for settings relating to a text background object.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
A marker symbol type, for rendering Point and MultiPoint geometries.
Definition: qgssymbol.h:895
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_SKIP
Definition: qgis_sip.h:126
Buffer component.
QList< QgsSymbolLayerReference > QgsSymbolLayerReferenceList
#define SIP_TRANSFER
Definition: qgis_sip.h:36
Draw shadow under text.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
SizeType
Methods for determining the background shape size.
QgsTextMaskSettings & mask()
Returns a reference to the masking settings.
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsTextShadowSettings shadow() const
Returns a reference to the text drop shadow settings.
Handles rendering text using rich formatting options, including drop shadows, buffers and background ...
ShapeType
Background shape types.
TextPart
Components of text.
QgsTextMaskSettings mask() const
Returns a reference to the masking settings.
Contains information about the context of a rendering operation.
Shape rotation is offset from text rotation.
Struct for storing maximum and minimum scales for measurements in map units.
Container for settings relating to a text shadow.
Container for settings relating to a text buffer.
#define SIP_OUT
Definition: qgis_sip.h:58
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
void setBuffer(const QgsTextBufferSettings &bufferSettings)
Sets the text&#39;s buffer settings.
void setShadow(const QgsTextShadowSettings &shadowSettings)
Sets the text&#39;s drop shadow settings.
Container for all settings relating to text rendering.
DrawMode
Draw mode to calculate width and height.
Represents a vector layer which manages a vector based data sets.
Square - buffered sizes only.
QgsTextBufferSettings buffer() const
Returns a reference to the text buffer settings.
Container for settings relating to a selective masking around a text.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:145
Horizontally oriented text.
QString resolvedFontFamily() const
Returns the family for the resolved font, ie if the specified font was not found on the system this w...