QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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  * 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 QGSTEXTRENDERER_H
17 #define QGSTEXTRENDERER_H
18 
19 #include "qgis_sip.h"
20 #include "qgis_core.h"
21 #include "qgstextblock.h"
22 #include "qgstextformat.h"
23 
24 #include <QPicture>
25 
26 class QgsTextDocument;
27 class QgsRenderContext;
28 
36 class CORE_EXPORT QgsTextRenderer
37 {
38  public:
39 
41  enum DrawMode
42  {
43  Rect = 0,
46  };
47 
49  enum TextPart
50  {
51  Text = 0,
55  };
56 
59  {
60  AlignLeft = 0,
64  };
65 
72  static HAlignment convertQtHAlignment( Qt::Alignment alignment );
73 
79  {
80  AlignTop = 0,
83  };
84 
91  static VAlignment convertQtVAlignment( Qt::Alignment alignment );
92 
101  static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
102 
103  // TODO QGIS 4.0 -- remove drawAsOutlines from below methods!
104 
119  static void drawText( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
120  QgsRenderContext &context, const QgsTextFormat &format,
121  bool drawAsOutlines = true, VAlignment vAlignment = AlignTop );
122 
136  static void drawText( QPointF point, double rotation, HAlignment alignment, const QStringList &textLines,
137  QgsRenderContext &context, const QgsTextFormat &format,
138  bool drawAsOutlines = true );
139 
158  Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
159  QgsRenderContext &context, const QgsTextFormat &format,
160  TextPart part, bool drawAsOutlines = true ) SIP_DEPRECATED;
161 
180  Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QStringList &textLines,
181  QgsRenderContext &context, const QgsTextFormat &format,
182  TextPart part, bool drawAsOutlines = true ) SIP_DEPRECATED;
183 
196  static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
197 
205  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
206  QFontMetricsF *fontMetrics = nullptr );
207 
216  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode = Point,
217  QFontMetricsF *fontMetrics = nullptr );
218 
230  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
231 
240  static constexpr double FONT_WORKAROUND_SCALE = 10;
241 
242  private:
243 
244  struct Component
245  {
247  QgsTextBlock block;
249  QPointF origin;
251  bool useOrigin = false;
253  double rotation = 0.0;
255  double rotationOffset = 0.0;
257  QPointF center;
259  QSizeF size;
261  QPointF offset;
263  QPicture picture;
264 
269  double pictureBuffer = 0.0;
271  double dpiRatio = 1.0;
273  HAlignment hAlign = AlignLeft;
274 
276  double extraWordSpacing = 0;
278  double extraLetterSpacing = 0;
279  };
280 
281  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
282  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, DrawMode mode = Point );
283 
299  static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, VAlignment vAlignment, const QgsTextDocument &document,
300  QgsRenderContext &context, const QgsTextFormat &format,
301  TextPart part );
302 
317  static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QgsTextDocument &document,
318  QgsRenderContext &context, const QgsTextFormat &format,
319  TextPart part );
320 
321  static double drawBuffer( QgsRenderContext &context,
322  const Component &component,
323  const QgsTextFormat &format,
324  DrawMode mode );
325 
326  static void drawBackground( QgsRenderContext &context,
327  Component component,
328  const QgsTextFormat &format,
329  const QgsTextDocument &document,
330  DrawMode mode = Rect );
331 
332  static void drawShadow( QgsRenderContext &context,
333  const Component &component,
334  const QgsTextFormat &format );
335 
336  static void drawMask( QgsRenderContext &context,
337  const Component &component,
338  const QgsTextFormat &format,
339  DrawMode mode );
340 
341  static void drawText( QgsRenderContext &context,
342  const Component &component,
343  const QgsTextFormat &format );
344 
345  static void drawTextInternal( TextPart drawType,
346  QgsRenderContext &context,
347  const QgsTextFormat &format,
348  const Component &component,
349  const QgsTextDocument &document,
350  const QFontMetricsF *fontMetrics,
351  HAlignment alignment,
352  VAlignment vAlignment,
353  DrawMode mode = Rect );
354 
355  static QgsTextFormat::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
356 
357  static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
358  static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
359 
360  static void drawTextInternalHorizontal( QgsRenderContext &context,
361  const QgsTextFormat &format,
362  TextPart drawType,
363  DrawMode mode,
364  const Component &component,
365  const QgsTextDocument &document,
366  double fontScale,
367  const QFontMetricsF *fontMetrics,
368  HAlignment hAlignment,
369  VAlignment vAlignment,
370  double rotation );
371 
372  static void drawTextInternalVertical( QgsRenderContext &context,
373  const QgsTextFormat &format,
374  TextPart drawType,
375  DrawMode mode,
376  const Component &component,
377  const QgsTextDocument &document,
378  double fontScale,
379  const QFontMetricsF *fontMetrics,
380  HAlignment hAlignment,
381  VAlignment vAlignment,
382  double rotation );
383 
384  static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
385 
387  friend class QgsLabelPreview;
388 
389  static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
390 
391 };
392 
393 #endif // QGSTEXTRENDERER_H
Struct for storing maximum and minimum scales for measurements in map units.
Contains information about the context of a rendering operation.
Represents a block of text consisting of one or more QgsTextFragment objects.
Definition: qgstextblock.h:36
Represents a document consisting of one or more QgsTextBlock objects.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
TextOrientation
Text orientation.
Definition: qgstextformat.h:46
Handles rendering text using rich formatting options, including drop shadows, buffers and background ...
VAlignment
Vertical alignment.
@ AlignBottom
Align to bottom.
@ AlignVCenter
Center align.
TextPart
Components of text.
@ Shadow
Drop shadow.
@ Buffer
Buffer component.
@ Background
Background shape.
HAlignment
Horizontal alignment.
@ AlignRight
Right align.
@ AlignCenter
Center align.
@ AlignJustify
Justify align.
DrawMode
Draw mode to calculate width and height.
@ Point
Text at point of origin draw mode.
@ Label
Label-specific draw mode.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:168
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
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_DEPRECATED
Definition: qgis_sip.h:106
#define FONT_WORKAROUND_SCALE