QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
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
40class CORE_EXPORT QgsTextRenderer
41{
42 public:
43
50 static Qgis::TextHorizontalAlignment convertQtHAlignment( Qt::Alignment alignment );
51
58 static Qgis::TextVerticalAlignment convertQtVAlignment( Qt::Alignment alignment );
59
68 static int sizeToPixel( double size, const QgsRenderContext &c, Qgis::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
69
70 // TODO QGIS 4.0 -- remove drawAsOutlines from below methods!
71
90 static void drawText( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
91 QgsRenderContext &context, const QgsTextFormat &format,
92 bool drawAsOutlines = true, Qgis::TextVerticalAlignment vAlignment = Qgis::TextVerticalAlignment::Top,
95
118 static void drawDocument( const QRectF &rect,
119 const QgsTextFormat &format,
120 const QgsTextDocument &document,
121 const QgsTextDocumentMetrics &metrics,
122 QgsRenderContext &context,
125 double rotation = 0,
128
142 static void drawText( QPointF point, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
143 QgsRenderContext &context, const QgsTextFormat &format,
144 bool drawAsOutlines = true );
145
165 static void drawDocument( QPointF point,
166 const QgsTextFormat &format,
167 const QgsTextDocument &document,
168 const QgsTextDocumentMetrics &metrics,
169 QgsRenderContext &context,
171 double rotation );
172
185 static void drawTextOnLine( const QPolygonF &line, const QString &text,
186 QgsRenderContext &context, const QgsTextFormat &format,
187 double offsetAlongLine = 0, double offsetFromLine = 0 );
188
201 static void drawDocumentOnLine( const QPolygonF &line,
202 const QgsTextFormat &format,
203 const QgsTextDocument &document,
204 QgsRenderContext &context,
205 double offsetAlongLine = 0,
206 double offsetFromLine = 0 );
207
226 Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
227 QgsRenderContext &context, const QgsTextFormat &format,
228 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
229
248 Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
249 QgsRenderContext &context, const QgsTextFormat &format,
250 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
251
264 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
265
273 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
274 QFontMetricsF *fontMetrics = nullptr );
275
286 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point,
287 QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
288
300 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
301
308 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
309
316 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
317
328 static constexpr double FONT_WORKAROUND_SCALE = 10;
329
338 static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
339
340 // to match QTextEngine handling of superscript/subscript font sizes
341
347 static constexpr double SUPERSCRIPT_SUBSCRIPT_FONT_SIZE_SCALING_FACTOR = 2.0 / 3.0;
348
349 private:
350
351 struct Component
352 {
354 QgsTextBlock block;
355
357 int blockIndex = 0;
358
360 int firstFragmentIndex = 0;
361
363 QPointF origin;
365 bool useOrigin = false;
367 double rotation = 0.0;
369 double rotationOffset = 0.0;
371 QPointF center;
373 QSizeF size;
375 QPointF offset;
377 QPicture picture;
378
383 double pictureBuffer = 0.0;
385 double dpiRatio = 1.0;
388
390 double extraWordSpacing = 0;
392 double extraLetterSpacing = 0;
393 };
394
395 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
396 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
397
415 static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, Qgis::TextVerticalAlignment vAlignment, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics,
416 QgsRenderContext &context, const QgsTextFormat &format,
418
435 static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QgsTextDocument &document,
436 const QgsTextDocumentMetrics &metrics,
437 QgsRenderContext &context, const QgsTextFormat &format,
440
441 static double drawBuffer( QgsRenderContext &context,
442 const Component &component,
443 const QgsTextFormat &format,
444 const QgsTextDocumentMetrics &metrics,
446
447 static void drawBackground( QgsRenderContext &context,
448 Component component,
449 const QgsTextFormat &format,
450 const QgsTextDocumentMetrics &metrics,
452
453 static void drawShadow( QgsRenderContext &context,
454 const Component &component,
455 const QgsTextFormat &format );
456
457 static void drawMask( QgsRenderContext &context,
458 const Component &component,
459 const QgsTextFormat &format,
460 const QgsTextDocumentMetrics &metrics,
462
463 static void drawText( QgsRenderContext &context,
464 const Component &component,
465 const QgsTextFormat &format );
466
467 static void drawTextInternal( Qgis::TextComponent drawType,
468 QgsRenderContext &context,
469 const QgsTextFormat &format,
470 const Component &component,
471 const QgsTextDocument &document,
472 const QgsTextDocumentMetrics &metrics,
476
477 static Qgis::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
478
479 static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
480 static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
481
482 static void drawTextInternalHorizontal( QgsRenderContext &context,
483 const QgsTextFormat &format,
484 Qgis::TextComponent drawType,
486 const Component &component,
487 const QgsTextDocument &document,
488 const QgsTextDocumentMetrics &metrics,
489 double fontScale,
492 double rotation );
493
494 static void drawTextInternalVertical( QgsRenderContext &context,
495 const QgsTextFormat &format,
496 Qgis::TextComponent drawType,
498 const Component &component,
499 const QgsTextDocument &document,
500 const QgsTextDocumentMetrics &metrics,
501 double fontScale,
504 double rotation );
505
506 static void renderBlockHorizontal( const QgsTextBlock &block, int blockIndex,
507 const QgsTextDocumentMetrics &metrics, QgsRenderContext &context,
508 const QgsTextFormat &format,
509 QPainter *painter, bool usePaths,
510 double fontScale, double extraWordSpace, double extraLetterSpace,
512
514 friend class QgsLabelPreview;
515
516 static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
517
521 static bool usePathsToRender( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
522};
523
524#endif // QGSTEXTRENDERER_H
TextLayoutMode
Text layout modes.
Definition qgis.h:2699
@ Point
Text at point of origin layout mode.
@ Rectangle
Text within rectangle layout mode.
QFlags< TextRendererFlag > TextRendererFlags
Definition qgis.h:3154
TextOrientation
Text orientations.
Definition qgis.h:2684
RenderUnit
Rendering size units.
Definition qgis.h:4839
TextVerticalAlignment
Text vertical alignment.
Definition qgis.h:2751
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:2732
TextComponent
Text components.
Definition qgis.h:2716
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.
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.
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