QGIS API Documentation  3.27.0-Master (bef583a8ef)
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 
120  static void drawText( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
121  QgsRenderContext &context, const QgsTextFormat &format,
122  bool drawAsOutlines = true, VAlignment vAlignment = AlignTop,
123  Qgis::TextRendererFlags flags = Qgis::TextRendererFlags() );
124 
138  static void drawText( QPointF point, double rotation, HAlignment alignment, const QStringList &textLines,
139  QgsRenderContext &context, const QgsTextFormat &format,
140  bool drawAsOutlines = true );
141 
160  Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines,
161  QgsRenderContext &context, const QgsTextFormat &format,
162  TextPart part, bool drawAsOutlines = true ) SIP_DEPRECATED;
163 
182  Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QStringList &textLines,
183  QgsRenderContext &context, const QgsTextFormat &format,
184  TextPart part, bool drawAsOutlines = true ) SIP_DEPRECATED;
185 
198  static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
199 
207  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
208  QFontMetricsF *fontMetrics = nullptr );
209 
220  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode = Point,
221  QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
222 
234  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
235 
242  static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
243 
250  static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
251 
260  static constexpr double FONT_WORKAROUND_SCALE = 10;
261 
262  private:
263 
264  struct Component
265  {
267  QgsTextBlock block;
269  QPointF origin;
271  bool useOrigin = false;
273  double rotation = 0.0;
275  double rotationOffset = 0.0;
277  QPointF center;
279  QSizeF size;
281  QPointF offset;
283  QPicture picture;
284 
289  double pictureBuffer = 0.0;
291  double dpiRatio = 1.0;
293  HAlignment hAlign = AlignLeft;
294 
296  double extraWordSpacing = 0;
298  double extraLetterSpacing = 0;
299  };
300 
301  static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
302  static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, DrawMode mode = Point );
303 
319  static void drawPart( const QRectF &rect, double rotation, HAlignment alignment, VAlignment vAlignment, const QgsTextDocument &document,
320  QgsRenderContext &context, const QgsTextFormat &format,
321  TextPart part );
322 
337  static void drawPart( QPointF origin, double rotation, HAlignment alignment, const QgsTextDocument &document,
338  QgsRenderContext &context, const QgsTextFormat &format,
339  TextPart part );
340 
341  static double drawBuffer( QgsRenderContext &context,
342  const Component &component,
343  const QgsTextFormat &format,
344  DrawMode mode );
345 
346  static void drawBackground( QgsRenderContext &context,
347  Component component,
348  const QgsTextFormat &format,
349  const QgsTextDocument &document,
350  DrawMode mode = Rect );
351 
352  static void drawShadow( QgsRenderContext &context,
353  const Component &component,
354  const QgsTextFormat &format );
355 
356  static void drawMask( QgsRenderContext &context,
357  const Component &component,
358  const QgsTextFormat &format,
359  DrawMode mode );
360 
361  static void drawText( QgsRenderContext &context,
362  const Component &component,
363  const QgsTextFormat &format );
364 
365  static void drawTextInternal( TextPart drawType,
366  QgsRenderContext &context,
367  const QgsTextFormat &format,
368  const Component &component,
369  const QgsTextDocument &document,
370  const QFontMetricsF *fontMetrics,
371  HAlignment alignment,
372  VAlignment vAlignment,
373  DrawMode mode = Rect );
374 
375  static QgsTextFormat::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
376 
377  static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
378  static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
379 
380  static void drawTextInternalHorizontal( QgsRenderContext &context,
381  const QgsTextFormat &format,
382  TextPart drawType,
383  DrawMode mode,
384  const Component &component,
385  const QgsTextDocument &document,
386  double fontScale,
387  const QFontMetricsF *fontMetrics,
388  HAlignment hAlignment,
389  VAlignment vAlignment,
390  double rotation );
391 
392  static void drawTextInternalVertical( QgsRenderContext &context,
393  const QgsTextFormat &format,
394  TextPart drawType,
395  DrawMode mode,
396  const Component &component,
397  const QgsTextDocument &document,
398  double fontScale,
399  const QFontMetricsF *fontMetrics,
400  HAlignment hAlignment,
401  VAlignment vAlignment,
402  double rotation );
403 
404  static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
405 
407  friend class QgsLabelPreview;
408 
409  static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
410 
411 };
412 
413 #endif // QGSTEXTRENDERER_H
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:72
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