QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 "qgsrendercontext.h"
23 #include "qgstextformat.h"
24 
25 #include <QPicture>
26 
27 class QgsTextDocument;
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 
325  static void drawBackground( QgsRenderContext &context,
326  Component component,
327  const QgsTextFormat &format,
328  const QgsTextDocument &document,
329  DrawMode mode = Rect );
330 
331  static void drawShadow( QgsRenderContext &context,
332  const Component &component,
333  const QgsTextFormat &format );
334 
335  static void drawMask( QgsRenderContext &context,
336  const Component &component,
337  const QgsTextFormat &format );
338 
339  static void drawText( QgsRenderContext &context,
340  const Component &component,
341  const QgsTextFormat &format );
342 
343  static void drawTextInternal( TextPart drawType,
344  QgsRenderContext &context,
345  const QgsTextFormat &format,
346  const Component &component,
347  const QgsTextDocument &document,
348  const QFontMetricsF *fontMetrics,
349  HAlignment alignment,
350  VAlignment vAlignment,
351  DrawMode mode = Rect );
352 
353  static QgsTextFormat::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
354 
355  static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
356  static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
357 
358  static void drawTextInternalHorizontal( QgsRenderContext &context,
359  const QgsTextFormat &format,
360  TextPart drawType,
361  DrawMode mode,
362  const Component &component,
363  const QgsTextDocument &document,
364  double fontScale,
365  const QFontMetricsF *fontMetrics,
366  HAlignment hAlignment,
367  VAlignment vAlignment,
368  double rotation );
369 
370  static void drawTextInternalVertical( QgsRenderContext &context,
371  const QgsTextFormat &format,
372  TextPart drawType,
373  DrawMode mode,
374  const Component &component,
375  const QgsTextDocument &document,
376  double fontScale,
377  const QFontMetricsF *fontMetrics,
378  HAlignment hAlignment,
379  VAlignment vAlignment,
380  double rotation );
381 
383  friend class QgsLabelPreview;
384 
385  static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
386 
387 };
388 
389 
390 #endif // QGSTEXTRENDERER_H
QgsTextRenderer::TextPart
TextPart
Components of text.
Definition: qgstextrenderer.h:50
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsTextRenderer::Background
@ Background
Background shape.
Definition: qgstextrenderer.h:53
QgsTextRenderer::AlignCenter
@ AlignCenter
Center align.
Definition: qgstextrenderer.h:61
QgsTextRenderer::AlignRight
@ AlignRight
Right align.
Definition: qgstextrenderer.h:62
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsTextRenderer::VAlignment
VAlignment
Vertical alignment.
Definition: qgstextrenderer.h:79
QgsTextRenderer::Shadow
@ Shadow
Drop shadow.
Definition: qgstextrenderer.h:54
QgsVectorLayerLabelProvider
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
Definition: qgsvectorlayerlabelprovider.h:41
QgsTextRenderer::DrawMode
DrawMode
Draw mode to calculate width and height.
Definition: qgstextrenderer.h:42
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsTextRenderer::Point
@ Point
Text at point of origin draw mode.
Definition: qgstextrenderer.h:44
qgis_sip.h
QgsTextRenderer::AlignJustify
@ AlignJustify
Justify align.
Definition: qgstextrenderer.h:63
qgsrendercontext.h
FONT_WORKAROUND_SCALE
#define FONT_WORKAROUND_SCALE
Definition: qgslegendsettings.cpp:113
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:38
QgsTextBlock
Represents a block of text consisting of one or more QgsTextFragment objects.
Definition: qgstextblock.h:36
c
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
Definition: porting_processing.dox:1
QgsTextDocument
Represents a document consisting of one or more QgsTextBlock objects.
Definition: qgstextdocument.h:39
QgsTextRenderer::Label
@ Label
Label-specific draw mode.
Definition: qgstextrenderer.h:45
QgsTextRenderer
Handles rendering text using rich formatting options, including drop shadows, buffers and background ...
Definition: qgstextrenderer.h:37
QgsTextRenderer::AlignVCenter
@ AlignVCenter
Center align.
Definition: qgstextrenderer.h:81
qgstextformat.h
QgsTextRenderer::Buffer
@ Buffer
Buffer component.
Definition: qgstextrenderer.h:52
QgsTextFormat::TextOrientation
TextOrientation
Text orientation.
Definition: qgstextformat.h:45
QgsTextRenderer::AlignBottom
@ AlignBottom
Align to bottom.
Definition: qgstextrenderer.h:82
qgstextblock.h
QgsTextRenderer::HAlignment
HAlignment
Horizontal alignment.
Definition: qgstextrenderer.h:59