QGIS API Documentation 3.41.0-Master (181b2f43d8e)
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#include <QPainterPath>
27
28class QgsTextDocument;
30class QgsTextFormat;
32
33class QFontMetricsF;
34
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,
96
119 static void drawDocument( const QRectF &rect,
120 const QgsTextFormat &format,
121 const QgsTextDocument &document,
122 const QgsTextDocumentMetrics &metrics,
123 QgsRenderContext &context,
126 double rotation = 0,
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
166 static void drawDocument( QPointF point,
167 const QgsTextFormat &format,
168 const QgsTextDocument &document,
169 const QgsTextDocumentMetrics &metrics,
170 QgsRenderContext &context,
172 double rotation );
173
186 static void drawTextOnLine( const QPolygonF &line, const QString &text,
187 QgsRenderContext &context, const QgsTextFormat &format,
188 double offsetAlongLine = 0, double offsetFromLine = 0 );
189
202 static void drawDocumentOnLine( const QPolygonF &line,
203 const QgsTextFormat &format,
204 const QgsTextDocument &document,
205 QgsRenderContext &context,
206 double offsetAlongLine = 0,
207 double offsetFromLine = 0 );
208
227 Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
228 QgsRenderContext &context, const QgsTextFormat &format,
229 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
230
249 Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
250 QgsRenderContext &context, const QgsTextFormat &format,
251 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
252
265 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
266
274 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
275 QFontMetricsF *fontMetrics = nullptr );
276
287 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point,
288 QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
289
301 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
302
309 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
310
317 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
318
329 static constexpr double FONT_WORKAROUND_SCALE = 10;
330
339 static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
340
341 // to match QTextEngine handling of superscript/subscript font sizes
342
348 static constexpr double SUPERSCRIPT_SUBSCRIPT_FONT_SIZE_SCALING_FACTOR = 2.0 / 3.0;
349
350 private:
351
352 struct Component
353 {
355 QgsTextBlock block;
356
358 int blockIndex = 0;
359
361 int firstFragmentIndex = 0;
362
364 QPointF origin;
366 bool useOrigin = false;
368 double rotation = 0.0;
370 double rotationOffset = 0.0;
372 QPointF center;
374 QSizeF size;
376 QPointF offset;
378 QPicture picture;
379
384 double pictureBuffer = 0.0;
386 double dpiRatio = 1.0;
389
391 double extraWordSpacing = 0;
393 double extraLetterSpacing = 0;
394 };
395
396 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
397 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
398
416 static void drawParts( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, Qgis::TextVerticalAlignment vAlignment, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics,
417 QgsRenderContext &context, const QgsTextFormat &format,
419
436 static void drawParts( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QgsTextDocument &document,
437 const QgsTextDocumentMetrics &metrics,
438 QgsRenderContext &context, const QgsTextFormat &format,
441
442 static double drawBuffer( QgsRenderContext &context,
443 const Component &component,
444 const QgsTextFormat &format,
445 const QgsTextDocumentMetrics &metrics,
447
448 static void drawBackground( QgsRenderContext &context,
449 const Component &component,
450 const QgsTextFormat &format,
451 const QgsTextDocumentMetrics &metrics,
453
454 static void drawShadow( QgsRenderContext &context,
455 const Component &component,
456 const QgsTextFormat &format );
457
458 static void drawMask( QgsRenderContext &context,
459 const Component &component,
460 const QgsTextFormat &format,
461 const QgsTextDocumentMetrics &metrics,
463
464 static void drawText( QgsRenderContext &context,
465 const Component &component,
466 const QgsTextFormat &format );
467
468 static void drawTextInternal( Qgis::TextComponents components,
469 QgsRenderContext &context,
470 const QgsTextFormat &format,
471 const Component &component,
472 const QgsTextDocument &document,
473 const QgsTextDocumentMetrics &metrics,
477
478 static Qgis::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
479
480 static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
481 static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
482
483 static void drawTextInternalHorizontal( QgsRenderContext &context,
484 const QgsTextFormat &format,
485 Qgis::TextComponents components,
487 const Component &component,
488 const QgsTextDocument &document,
489 const QgsTextDocumentMetrics &metrics,
490 double fontScale,
493 double rotation );
494
495 static void drawTextInternalVertical( QgsRenderContext &context,
496 const QgsTextFormat &format,
497 Qgis::TextComponents components,
499 const Component &component,
500 const QgsTextDocument &document,
501 const QgsTextDocumentMetrics &metrics,
502 double fontScale,
505 double rotation );
506
507 struct DeferredRenderFragment
508 {
509 // mandatory
510 QColor color;
511 QPointF point;
512 // optional
513 QPainterPath path;
514 // optional
515 QFont font;
516 QString text;
517 };
518
519 struct BlockMetrics
520 {
521 double xOffset = 0;
522 double backgroundXOffset = 0;
523 double width = 0;
524 double backgroundWidth = 0;
525 double extraWordSpace = 0;
526 double extraLetterSpace = 0;
527 };
528
529 static QVector< QgsTextRenderer::BlockMetrics > calculateBlockMetrics( const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics, Qgis::TextLayoutMode mode, double targetWidth, const Qgis::TextHorizontalAlignment hAlignment );
530
531 struct DeferredRenderBlock
532 {
533 QPointF origin;
534 Component component;
535 QVector< DeferredRenderFragment > fragments;
536 };
537
538 static QBrush createBrushForPath( QgsRenderContext &context, const QString &path );
539
540 static void renderBlockHorizontal( const QgsTextBlock &block, int blockIndex,
541 const QgsTextDocumentMetrics &metrics, QgsRenderContext &context,
542 const QgsTextFormat &format,
543 QPainter *painter, bool forceRenderAsPaths,
544 double fontScale, double extraWordSpace, double extraLetterSpace,
546 DeferredRenderBlock *deferredRenderBlock );
547 static void renderDocumentBackgrounds( QgsRenderContext &context, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics, const Component &component, const QVector< QgsTextRenderer::BlockMetrics > &blockMetrics, Qgis::TextLayoutMode mode, double verticalAlignOffset, double rotation );
548 static void renderDeferredBlocks( QgsRenderContext &context, const QgsTextFormat &format, Qgis::TextComponents components, const std::vector<DeferredRenderBlock> &deferredBlocks, bool usePathsForText, double fontScale, const Component &component, double rotation );
549 static void renderDeferredBuffer( QgsRenderContext &context, const QgsTextFormat &format, Qgis::TextComponents components, const std::vector<DeferredRenderBlock> &deferredBlocks, double fontScale, const Component &component, double rotation );
550 static void renderDeferredShadowForText( QgsRenderContext &context, const QgsTextFormat &format, const std::vector<DeferredRenderBlock> &deferredBlocks, double fontScale, const Component &component, double rotation );
551 static void renderDeferredText( QgsRenderContext &context, const std::vector<DeferredRenderBlock> &deferredBlocks, bool usePathsForText, double fontScale, const Component &component, double rotation );
552
554 friend class QgsLabelPreview;
555
556 static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
557
561 static bool usePathsToRender( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
562
566 static bool usePictureToRender( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
567
568};
569
570#endif // QGSTEXTRENDERER_H
TextLayoutMode
Text layout modes.
Definition qgis.h:2744
@ Point
Text at point of origin layout mode.
@ Rectangle
Text within rectangle layout mode.
QFlags< TextRendererFlag > TextRendererFlags
Definition qgis.h:3207
TextOrientation
Text orientations.
Definition qgis.h:2729
RenderUnit
Rendering size units.
Definition qgis.h:4892
TextVerticalAlignment
Text vertical alignment.
Definition qgis.h:2804
QFlags< TextComponent > TextComponents
Text components.
Definition qgis.h:2774
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:2785
TextComponent
Text components.
Definition qgis.h:2761
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