QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
89 static void drawText( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
90 QgsRenderContext &context, const QgsTextFormat &format,
91 bool drawAsOutlines = true, Qgis::TextVerticalAlignment vAlignment = Qgis::TextVerticalAlignment::Top,
92 Qgis::TextRendererFlags flags = Qgis::TextRendererFlags() );
93
107 static void drawText( QPointF point, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
108 QgsRenderContext &context, const QgsTextFormat &format,
109 bool drawAsOutlines = true );
110
129 Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
130 QgsRenderContext &context, const QgsTextFormat &format,
131 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
132
151 Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
152 QgsRenderContext &context, const QgsTextFormat &format,
153 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
154
167 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
168
176 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
177 QFontMetricsF *fontMetrics = nullptr );
178
189 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point,
190 QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
191
203 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
204
211 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
212
219 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
220
229 static constexpr double FONT_WORKAROUND_SCALE = 10;
230
231 private:
232
233 struct Component
234 {
236 QgsTextBlock block;
237
239 int blockIndex = 0;
240
242 QPointF origin;
244 bool useOrigin = false;
246 double rotation = 0.0;
248 double rotationOffset = 0.0;
250 QPointF center;
252 QSizeF size;
254 QPointF offset;
256 QPicture picture;
257
262 double pictureBuffer = 0.0;
264 double dpiRatio = 1.0;
266 Qgis::TextHorizontalAlignment hAlign = Qgis::TextHorizontalAlignment::Left;
267
269 double extraWordSpacing = 0;
271 double extraLetterSpacing = 0;
272 };
273
274 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
275 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
276
294 static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, Qgis::TextVerticalAlignment vAlignment, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics,
295 QgsRenderContext &context, const QgsTextFormat &format,
297
314 static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QgsTextDocument &document,
315 const QgsTextDocumentMetrics &metrics,
316 QgsRenderContext &context, const QgsTextFormat &format,
319
320 static double drawBuffer( QgsRenderContext &context,
321 const Component &component,
322 const QgsTextFormat &format,
323 const QgsTextDocumentMetrics &metrics,
325
326 static void drawBackground( QgsRenderContext &context,
327 Component component,
328 const QgsTextFormat &format,
329 const QgsTextDocumentMetrics &metrics,
330 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
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,
340
341 static void drawText( QgsRenderContext &context,
342 const Component &component,
343 const QgsTextFormat &format );
344
345 static void drawTextInternal( Qgis::TextComponent drawType,
346 QgsRenderContext &context,
347 const QgsTextFormat &format,
348 const Component &component,
349 const QgsTextDocument &document,
350 const QgsTextDocumentMetrics &metrics,
353 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
354
355 static Qgis::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 Qgis::TextComponent drawType,
364 const Component &component,
365 const QgsTextDocument &document,
366 const QgsTextDocumentMetrics &metrics,
367 double fontScale,
370 double rotation );
371
372 static void drawTextInternalVertical( QgsRenderContext &context,
373 const QgsTextFormat &format,
374 Qgis::TextComponent drawType,
376 const Component &component,
377 const QgsTextDocument &document,
378 const QgsTextDocumentMetrics &metrics,
379 double fontScale,
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
TextLayoutMode
Text layout modes.
Definition: qgis.h:1445
@ Point
Text at point of origin layout mode.
TextOrientation
Text orientations.
Definition: qgis.h:1430
TextVerticalAlignment
Text vertical alignment.
Definition: qgis.h:1492
TextHorizontalAlignment
Text horizontal alignment.
Definition: qgis.h:1476
TextComponent
Text components.
Definition: qgis.h:1460
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