QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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 "qgsmapunitscale.h"
23#include "qgis.h"
24
25#include <QPicture>
26
27class QgsTextDocument;
29class QgsTextFormat;
31
32class QFontMetricsF;
33
41class CORE_EXPORT QgsTextRenderer
42{
43 public:
44
51 static Qgis::TextHorizontalAlignment convertQtHAlignment( Qt::Alignment alignment );
52
59 static Qgis::TextVerticalAlignment convertQtVAlignment( Qt::Alignment alignment );
60
69 static int sizeToPixel( double size, const QgsRenderContext &c, Qgis::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
70
71 // TODO QGIS 4.0 -- remove drawAsOutlines from below methods!
72
91 static void drawText( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
92 QgsRenderContext &context, const QgsTextFormat &format,
93 bool drawAsOutlines = true, Qgis::TextVerticalAlignment vAlignment = Qgis::TextVerticalAlignment::Top,
94 Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(),
95 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
96
119 static void drawDocument( const QRectF &rect,
120 const QgsTextFormat &format,
121 const QgsTextDocument &document,
122 const QgsTextDocumentMetrics &metrics,
123 QgsRenderContext &context,
124 Qgis::TextHorizontalAlignment horizontalAlignment = Qgis::TextHorizontalAlignment::Left,
125 Qgis::TextVerticalAlignment verticalAlignment = Qgis::TextVerticalAlignment::Top,
126 double rotation = 0,
127 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle,
128 Qgis::TextRendererFlags flags = Qgis::TextRendererFlags() );
129
143 static void drawText( QPointF point, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
144 QgsRenderContext &context, const QgsTextFormat &format,
145 bool drawAsOutlines = true );
146
165 Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
166 QgsRenderContext &context, const QgsTextFormat &format,
167 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
168
187 Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
188 QgsRenderContext &context, const QgsTextFormat &format,
189 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
190
203 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
204
212 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
213 QFontMetricsF *fontMetrics = nullptr );
214
225 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point,
226 QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
227
239 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
240
247 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
248
255 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
256
265 static constexpr double FONT_WORKAROUND_SCALE = 10;
266
267 private:
268
269 struct Component
270 {
272 QgsTextBlock block;
273
275 int blockIndex = 0;
276
278 int firstFragmentIndex = 0;
279
281 QPointF origin;
283 bool useOrigin = false;
285 double rotation = 0.0;
287 double rotationOffset = 0.0;
289 QPointF center;
291 QSizeF size;
293 QPointF offset;
295 QPicture picture;
296
301 double pictureBuffer = 0.0;
303 double dpiRatio = 1.0;
305 Qgis::TextHorizontalAlignment hAlign = Qgis::TextHorizontalAlignment::Left;
306
308 double extraWordSpacing = 0;
310 double extraLetterSpacing = 0;
311 };
312
313 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
314 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
315
333 static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, Qgis::TextVerticalAlignment vAlignment, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics,
334 QgsRenderContext &context, const QgsTextFormat &format,
336
353 static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QgsTextDocument &document,
354 const QgsTextDocumentMetrics &metrics,
355 QgsRenderContext &context, const QgsTextFormat &format,
358
359 static double drawBuffer( QgsRenderContext &context,
360 const Component &component,
361 const QgsTextFormat &format,
362 const QgsTextDocumentMetrics &metrics,
364
365 static void drawBackground( QgsRenderContext &context,
366 Component component,
367 const QgsTextFormat &format,
368 const QgsTextDocumentMetrics &metrics,
369 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
370
371 static void drawShadow( QgsRenderContext &context,
372 const Component &component,
373 const QgsTextFormat &format );
374
375 static void drawMask( QgsRenderContext &context,
376 const Component &component,
377 const QgsTextFormat &format,
378 const QgsTextDocumentMetrics &metrics,
380
381 static void drawText( QgsRenderContext &context,
382 const Component &component,
383 const QgsTextFormat &format );
384
385 static void drawTextInternal( Qgis::TextComponent drawType,
386 QgsRenderContext &context,
387 const QgsTextFormat &format,
388 const Component &component,
389 const QgsTextDocument &document,
390 const QgsTextDocumentMetrics &metrics,
393 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
394
395 static Qgis::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
396
397 static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
398 static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
399
400 static void drawTextInternalHorizontal( QgsRenderContext &context,
401 const QgsTextFormat &format,
402 Qgis::TextComponent drawType,
404 const Component &component,
405 const QgsTextDocument &document,
406 const QgsTextDocumentMetrics &metrics,
407 double fontScale,
410 double rotation );
411
412 static void drawTextInternalVertical( QgsRenderContext &context,
413 const QgsTextFormat &format,
414 Qgis::TextComponent drawType,
416 const Component &component,
417 const QgsTextDocument &document,
418 const QgsTextDocumentMetrics &metrics,
419 double fontScale,
422 double rotation );
423
424 static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
425
427 friend class QgsLabelPreview;
428
429 static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
430
431};
432
433#endif // QGSTEXTRENDERER_H
TextLayoutMode
Text layout modes.
Definition: qgis.h:1801
@ Point
Text at point of origin layout mode.
TextOrientation
Text orientations.
Definition: qgis.h:1786
RenderUnit
Rendering size units.
Definition: qgis.h:3176
TextVerticalAlignment
Text vertical alignment.
Definition: qgis.h:1853
TextHorizontalAlignment
Text horizontal alignment.
Definition: qgis.h:1834
TextComponent
Text components.
Definition: qgis.h:1818
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 ...
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