QGIS API Documentation 3.29.0-Master (d0a9f12494)
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 "qgsunittypes.h"
23#include "qgsmapunitscale.h"
24#include "qgis.h"
25
26#include <QPicture>
27
28class QgsTextDocument;
30class QgsTextFormat;
32
33class QFontMetricsF;
34
42class CORE_EXPORT QgsTextRenderer
43{
44 public:
45
52 static Qgis::TextHorizontalAlignment convertQtHAlignment( Qt::Alignment alignment );
53
60 static Qgis::TextVerticalAlignment convertQtVAlignment( Qt::Alignment alignment );
61
70 static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
71
72 // TODO QGIS 4.0 -- remove drawAsOutlines from below methods!
73
92 static void drawText( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
93 QgsRenderContext &context, const QgsTextFormat &format,
94 bool drawAsOutlines = true, Qgis::TextVerticalAlignment vAlignment = Qgis::TextVerticalAlignment::Top,
95 Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(),
96 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
97
120 static void drawDocument( const QRectF &rect,
121 const QgsTextFormat &format,
122 const QgsTextDocument &document,
123 const QgsTextDocumentMetrics &metrics,
124 QgsRenderContext &context,
125 Qgis::TextHorizontalAlignment horizontalAlignment = Qgis::TextHorizontalAlignment::Left,
126 Qgis::TextVerticalAlignment verticalAlignment = Qgis::TextVerticalAlignment::Top,
127 double rotation = 0,
128 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle,
129 Qgis::TextRendererFlags flags = Qgis::TextRendererFlags() );
130
144 static void drawText( QPointF point, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
145 QgsRenderContext &context, const QgsTextFormat &format,
146 bool drawAsOutlines = true );
147
166 Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
167 QgsRenderContext &context, const QgsTextFormat &format,
168 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
169
188 Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
189 QgsRenderContext &context, const QgsTextFormat &format,
190 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
191
204 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
205
213 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
214 QFontMetricsF *fontMetrics = nullptr );
215
226 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point,
227 QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
228
240 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
241
248 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
249
256 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
257
266 static constexpr double FONT_WORKAROUND_SCALE = 10;
267
268 private:
269
270 struct Component
271 {
273 QgsTextBlock block;
274
276 int blockIndex = 0;
277
279 int firstFragmentIndex = 0;
280
282 QPointF origin;
284 bool useOrigin = false;
286 double rotation = 0.0;
288 double rotationOffset = 0.0;
290 QPointF center;
292 QSizeF size;
294 QPointF offset;
296 QPicture picture;
297
302 double pictureBuffer = 0.0;
304 double dpiRatio = 1.0;
306 Qgis::TextHorizontalAlignment hAlign = Qgis::TextHorizontalAlignment::Left;
307
309 double extraWordSpacing = 0;
311 double extraLetterSpacing = 0;
312 };
313
314 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
315 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
316
334 static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, Qgis::TextVerticalAlignment vAlignment, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics,
335 QgsRenderContext &context, const QgsTextFormat &format,
337
354 static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QgsTextDocument &document,
355 const QgsTextDocumentMetrics &metrics,
356 QgsRenderContext &context, const QgsTextFormat &format,
359
360 static double drawBuffer( QgsRenderContext &context,
361 const Component &component,
362 const QgsTextFormat &format,
363 const QgsTextDocumentMetrics &metrics,
365
366 static void drawBackground( QgsRenderContext &context,
367 Component component,
368 const QgsTextFormat &format,
369 const QgsTextDocumentMetrics &metrics,
370 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
371
372 static void drawShadow( QgsRenderContext &context,
373 const Component &component,
374 const QgsTextFormat &format );
375
376 static void drawMask( QgsRenderContext &context,
377 const Component &component,
378 const QgsTextFormat &format,
379 const QgsTextDocumentMetrics &metrics,
381
382 static void drawText( QgsRenderContext &context,
383 const Component &component,
384 const QgsTextFormat &format );
385
386 static void drawTextInternal( Qgis::TextComponent drawType,
387 QgsRenderContext &context,
388 const QgsTextFormat &format,
389 const Component &component,
390 const QgsTextDocument &document,
391 const QgsTextDocumentMetrics &metrics,
394 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
395
396 static Qgis::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
397
398 static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
399 static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
400
401 static void drawTextInternalHorizontal( QgsRenderContext &context,
402 const QgsTextFormat &format,
403 Qgis::TextComponent drawType,
405 const Component &component,
406 const QgsTextDocument &document,
407 const QgsTextDocumentMetrics &metrics,
408 double fontScale,
411 double rotation );
412
413 static void drawTextInternalVertical( QgsRenderContext &context,
414 const QgsTextFormat &format,
415 Qgis::TextComponent drawType,
417 const Component &component,
418 const QgsTextDocument &document,
419 const QgsTextDocumentMetrics &metrics,
420 double fontScale,
423 double rotation );
424
425 static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
426
428 friend class QgsLabelPreview;
429
430 static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
431
432};
433
434#endif // QGSTEXTRENDERER_H
TextLayoutMode
Text layout modes.
Definition: qgis.h:1663
@ Point
Text at point of origin layout mode.
TextOrientation
Text orientations.
Definition: qgis.h:1648
TextVerticalAlignment
Text vertical alignment.
Definition: qgis.h:1715
TextHorizontalAlignment
Text horizontal alignment.
Definition: qgis.h:1696
TextComponent
Text components.
Definition: qgis.h:1680
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
Contains pre-calculated metrics of a QgsTextDocument.
Represents a document consisting of one or more QgsTextBlock objects.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
Handles rendering text using rich formatting options, including drop shadows, buffers and background ...
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