QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
159 static void drawTextOnLine( const QPolygonF &line, const QString &text,
160 QgsRenderContext &context, const QgsTextFormat &format,
161 double offsetAlongLine = 0, double offsetFromLine = 0 );
162
175 static void drawDocumentOnLine( const QPolygonF &line,
176 const QgsTextFormat &format,
177 const QgsTextDocument &document,
178 QgsRenderContext &context,
179 double offsetAlongLine = 0,
180 double offsetFromLine = 0 );
181
200 Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
201 QgsRenderContext &context, const QgsTextFormat &format,
202 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
203
222 Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
223 QgsRenderContext &context, const QgsTextFormat &format,
224 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
225
238 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
239
247 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
248 QFontMetricsF *fontMetrics = nullptr );
249
260 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point,
261 QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
262
274 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
275
282 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
283
290 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
291
300 static constexpr double FONT_WORKAROUND_SCALE = 10;
301
302 // to match QTextEngine handling of superscript/subscript font sizes
303
309 static constexpr double SUPERSCRIPT_SUBSCRIPT_FONT_SIZE_SCALING_FACTOR = 2.0 / 3.0;
310
311 private:
312
313 struct Component
314 {
316 QgsTextBlock block;
317
319 int blockIndex = 0;
320
322 int firstFragmentIndex = 0;
323
325 QPointF origin;
327 bool useOrigin = false;
329 double rotation = 0.0;
331 double rotationOffset = 0.0;
333 QPointF center;
335 QSizeF size;
337 QPointF offset;
339 QPicture picture;
340
345 double pictureBuffer = 0.0;
347 double dpiRatio = 1.0;
349 Qgis::TextHorizontalAlignment hAlign = Qgis::TextHorizontalAlignment::Left;
350
352 double extraWordSpacing = 0;
354 double extraLetterSpacing = 0;
355 };
356
357 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
358 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
359
377 static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, Qgis::TextVerticalAlignment vAlignment, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics,
378 QgsRenderContext &context, const QgsTextFormat &format,
380
397 static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QgsTextDocument &document,
398 const QgsTextDocumentMetrics &metrics,
399 QgsRenderContext &context, const QgsTextFormat &format,
402
403 static double drawBuffer( QgsRenderContext &context,
404 const Component &component,
405 const QgsTextFormat &format,
406 const QgsTextDocumentMetrics &metrics,
408
409 static void drawBackground( QgsRenderContext &context,
410 Component component,
411 const QgsTextFormat &format,
412 const QgsTextDocumentMetrics &metrics,
413 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
414
415 static void drawShadow( QgsRenderContext &context,
416 const Component &component,
417 const QgsTextFormat &format );
418
419 static void drawMask( QgsRenderContext &context,
420 const Component &component,
421 const QgsTextFormat &format,
422 const QgsTextDocumentMetrics &metrics,
424
425 static void drawText( QgsRenderContext &context,
426 const Component &component,
427 const QgsTextFormat &format );
428
429 static void drawTextInternal( Qgis::TextComponent drawType,
430 QgsRenderContext &context,
431 const QgsTextFormat &format,
432 const Component &component,
433 const QgsTextDocument &document,
434 const QgsTextDocumentMetrics &metrics,
437 Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle );
438
439 static Qgis::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
440
441 static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
442 static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
443
444 static void drawTextInternalHorizontal( QgsRenderContext &context,
445 const QgsTextFormat &format,
446 Qgis::TextComponent drawType,
448 const Component &component,
449 const QgsTextDocument &document,
450 const QgsTextDocumentMetrics &metrics,
451 double fontScale,
454 double rotation );
455
456 static void drawTextInternalVertical( QgsRenderContext &context,
457 const QgsTextFormat &format,
458 Qgis::TextComponent drawType,
460 const Component &component,
461 const QgsTextDocument &document,
462 const QgsTextDocumentMetrics &metrics,
463 double fontScale,
466 double rotation );
467
468 static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
469
471 friend class QgsLabelPreview;
472
473 static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
474
475};
476
477#endif // QGSTEXTRENDERER_H
TextLayoutMode
Text layout modes.
Definition: qgis.h:1959
@ Point
Text at point of origin layout mode.
TextOrientation
Text orientations.
Definition: qgis.h:1944
RenderUnit
Rendering size units.
Definition: qgis.h:3441
TextVerticalAlignment
Text vertical alignment.
Definition: qgis.h:2011
TextHorizontalAlignment
Text horizontal alignment.
Definition: qgis.h:1992
TextComponent
Text components.
Definition: qgis.h:1976
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:42
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