QGIS API Documentation 4.3.0-Master (59e977eef4b)
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.h"
20#include "qgis_core.h"
21#include "qgis_sip.h"
22#include "qgsmapunitscale.h"
23#include "qgstextblock.h"
24
25#include <QPainterPath>
26#include <QPicture>
27
28class QgsTextDocument;
30class QgsTextFormat;
32
33class QFontMetricsF;
34
41class CORE_EXPORT QgsTextRenderer
42{
43 public:
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 5.0 -- remove drawAsOutlines from below methods!
71
90 static void drawText(
91 const QRectF &rect,
92 double rotation,
94 const QStringList &textLines,
95 QgsRenderContext &context,
96 const QgsTextFormat &format,
97 bool drawAsOutlines = true,
101 );
102
125 static void drawDocument(
126 const QRectF &rect,
127 const QgsTextFormat &format,
128 const QgsTextDocument &document,
129 const QgsTextDocumentMetrics &metrics,
130 QgsRenderContext &context,
133 double rotation = 0,
136 );
137
151 static void drawText(
152 QPointF point, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines = true
153 );
154
175 static void drawDocument(
176 QPointF point,
177 const QgsTextFormat &format,
178 const QgsTextDocument &document,
179 const QgsTextDocumentMetrics &metrics,
180 QgsRenderContext &context,
182 double rotation,
184 );
185
201 static void drawTextOnLine(
202 const QPolygonF &line,
203 const QString &text,
204 QgsRenderContext &context,
205 const QgsTextFormat &format,
206 double offsetAlongLine = 0,
207 double offsetFromLine = 0,
210 );
211
227 static void drawDocumentOnLine(
228 const QPolygonF &line,
229 const QgsTextFormat &format,
230 const QgsTextDocument &document,
231 QgsRenderContext &context,
232 double offsetAlongLine = 0,
233 double offsetFromLine = 0,
236 );
237
256 Q_DECL_DEPRECATED static void drawPart(
257 const QRectF &rect,
258 double rotation,
260 const QStringList &textLines,
261 QgsRenderContext &context,
262 const QgsTextFormat &format,
264 bool drawAsOutlines = true
266
285 Q_DECL_DEPRECATED static void drawPart(
286 QPointF origin,
287 double rotation,
289 const QStringList &textLines,
290 QgsRenderContext &context,
291 const QgsTextFormat &format,
293 bool drawAsOutlines = true
295
308 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
309
317 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics = nullptr );
318
329 static double textHeight(
330 const QgsRenderContext &context,
331 const QgsTextFormat &format,
332 const QStringList &textLines,
334 QFontMetricsF *fontMetrics = nullptr,
336 double maxLineWidth = 0
337 );
338
350 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
351
358 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
359
366 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
367
378 static constexpr double FONT_WORKAROUND_SCALE = 10;
379
388 static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
389
390 // to match QTextEngine handling of superscript/subscript font sizes
391
397 static constexpr double SUPERSCRIPT_SUBSCRIPT_FONT_SIZE_SCALING_FACTOR = 2.0 / 3.0;
398
399 private:
400 struct Component
401 {
403 QgsTextBlock block;
404
406 int blockIndex = 0;
407
409 int firstFragmentIndex = 0;
410
412 QPointF origin;
414 bool useOrigin = false;
416 double rotation = 0.0;
418 double rotationOffset = 0.0;
420 QPointF center;
422 QSizeF size;
424 QPointF offset;
426 QPicture picture;
427
432 double pictureBuffer = 0.0;
434 double dpiRatio = 1.0;
437
439 double extraWordSpacing = 0;
441 double extraLetterSpacing = 0;
442 };
443
444 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
445 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
446
464 static void drawParts(
465 const QRectF &rect,
466 double rotation,
469 const QgsTextDocument &document,
470 const QgsTextDocumentMetrics &metrics,
471 QgsRenderContext &context,
472 const QgsTextFormat &format,
475 );
476
493 static void drawParts(
494 QPointF origin,
495 double rotation,
497 const QgsTextDocument &document,
498 const QgsTextDocumentMetrics &metrics,
499 QgsRenderContext &context,
500 const QgsTextFormat &format,
503 );
504
505 static double drawBuffer( QgsRenderContext &context, const Component &component, const QgsTextFormat &format, const QgsTextDocumentMetrics &metrics, Qgis::TextLayoutMode mode );
506
507 static void drawBackground(
508 QgsRenderContext &context, const Component &component, const QgsTextFormat &format, const QgsTextDocumentMetrics &metrics, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Rectangle
509 );
510
511 static void drawShadow( QgsRenderContext &context, const Component &component, const QgsTextFormat &format );
512
513 static void drawMask( QgsRenderContext &context, const Component &component, const QgsTextFormat &format, const QgsTextDocumentMetrics &metrics, Qgis::TextLayoutMode mode );
514
515 static void drawText( QgsRenderContext &context, const Component &component, const QgsTextFormat &format );
516
517 static void drawTextInternal(
518 Qgis::TextComponents components,
519 QgsRenderContext &context,
520 const QgsTextFormat &format,
521 const Component &component,
522 const QgsTextDocument &document,
523 const QgsTextDocumentMetrics &metrics,
527 );
528
529 static Qgis::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
530
531 static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
532 static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
533
534 static void drawTextInternalHorizontal(
535 QgsRenderContext &context,
536 const QgsTextFormat &format,
537 Qgis::TextComponents components,
539 const Component &component,
540 const QgsTextDocument &document,
541 const QgsTextDocumentMetrics &metrics,
542 double fontScale,
545 double rotation
546 );
547
548 static void drawTextInternalVertical(
549 QgsRenderContext &context,
550 const QgsTextFormat &format,
551 Qgis::TextComponents components,
553 const Component &component,
554 const QgsTextDocument &document,
555 const QgsTextDocumentMetrics &metrics,
556 double fontScale,
559 double rotation
560 );
561
562 struct DeferredRenderFragment
563 {
564 // mandatory
565 QColor color;
566 QPointF point;
567 // optional
568 QPainterPath path;
569 // optional
570 QFont font;
571 QString text;
572 };
573
574 struct BlockMetrics
575 {
576 double xOffset = 0;
577 double backgroundXOffset = 0;
578 double width = 0;
579 double backgroundWidth = 0;
580 double extraWordSpace = 0;
581 double extraLetterSpace = 0;
582 };
583
584 static QVector< QgsTextRenderer::BlockMetrics > calculateBlockMetrics(
585 const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics, Qgis::TextLayoutMode mode, double targetWidth, const Qgis::TextHorizontalAlignment hAlignment
586 );
587
588 struct DeferredRenderBlock
589 {
590 QPointF origin;
591 Component component;
592 QVector< DeferredRenderFragment > fragments;
593 };
594
595 static QBrush createBrushForPath( QgsRenderContext &context, const QString &path );
596
597 static void renderBlockHorizontal(
598 const QgsTextBlock &block,
599 int blockIndex,
600 const QgsTextDocumentMetrics &metrics,
601 QgsRenderContext &context,
602 const QgsTextFormat &format,
603 QPainter *painter,
604 bool forceRenderAsPaths,
605 double fontScale,
606 double extraWordSpace,
607 double extraLetterSpace,
609 DeferredRenderBlock *deferredRenderBlock
610 );
611 static void renderDocumentBackgrounds(
612 QgsRenderContext &context,
613 const QgsTextDocument &document,
614 const QgsTextDocumentMetrics &metrics,
615 const Component &component,
616 const QVector< QgsTextRenderer::BlockMetrics > &blockMetrics,
618 double verticalAlignOffset,
619 double rotation
620 );
621 static void renderDeferredBlocks(
622 QgsRenderContext &context,
623 const QgsTextFormat &format,
624 Qgis::TextComponents components,
625 const std::vector<DeferredRenderBlock> &deferredBlocks,
626 bool usePathsForText,
627 double fontScale,
628 const Component &component,
629 double rotation
630 );
631 static void renderDeferredBuffer(
632 QgsRenderContext &context, const QgsTextFormat &format, Qgis::TextComponents components, const std::vector<DeferredRenderBlock> &deferredBlocks, double fontScale, const Component &component, double rotation
633 );
634 static void renderDeferredShadowForText(
635 QgsRenderContext &context, const QgsTextFormat &format, const std::vector<DeferredRenderBlock> &deferredBlocks, double fontScale, const Component &component, double rotation
636 );
637 static void renderDeferredText( QgsRenderContext &context, const std::vector<DeferredRenderBlock> &deferredBlocks, bool usePathsForText, double fontScale, const Component &component, double rotation );
638
640 friend class QgsLabelPreview;
641
642 static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
643
647 static bool usePathsToRender( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
648
652 static bool usePictureToRender( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
653};
654
655#endif // QGSTEXTRENDERER_H
TextAnchorPoint
Anchor point of label text.
Definition qgis.h:1475
@ StartOfText
Anchor using start of text.
Definition qgis.h:1476
TextLayoutMode
Text layout modes.
Definition qgis.h:3099
@ Point
Text at point of origin layout mode.
Definition qgis.h:3101
@ Rectangle
Text within rectangle layout mode.
Definition qgis.h:3100
QFlags< TextRendererFlag > TextRendererFlags
Definition qgis.h:3622
TextOrientation
Text orientations.
Definition qgis.h:3084
RenderUnit
Rendering size units.
Definition qgis.h:5650
TextVerticalAlignment
Text vertical alignment.
Definition qgis.h:3159
@ Top
Align to top.
Definition qgis.h:3160
@ TruncateStringWhenLineIsTooShort
When a string is too long for the line, truncate characters instead of aborting the placement.
Definition qgis.h:3189
@ UseBaselinePlacement
Generate placement based on the character baselines instead of centers.
Definition qgis.h:3190
QFlags< TextComponent > TextComponents
Text components.
Definition qgis.h:3129
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:3140
QFlags< CurvedTextFlag > CurvedTextFlags
Flags controlling behavior of curved text generation.
Definition qgis.h:3201
TextComponent
Text components.
Definition qgis.h:3116
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 ...
static Qgis::TextVerticalAlignment convertQtVAlignment(Qt::Alignment alignment)
Converts a Qt vertical alignment flag to a Qgis::TextVerticalAlignment value.
static double textWidth(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics=nullptr)
Returns the width of a text based on a given format.
static void drawDocument(const QRectF &rect, const QgsTextFormat &format, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics, QgsRenderContext &context, Qgis::TextHorizontalAlignment horizontalAlignment=Qgis::TextHorizontalAlignment::Left, Qgis::TextVerticalAlignment verticalAlignment=Qgis::TextVerticalAlignment::Top, double rotation=0, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags())
Draws a text document within a rectangle using the specified settings.
static int sizeToPixel(double size, const QgsRenderContext &c, Qgis::RenderUnit unit, const QgsMapUnitScale &mapUnitScale=QgsMapUnitScale())
Calculates pixel size (considering output size should be in pixel or map units, scale factors and opt...
static Q_DECL_DEPRECATED void drawPart(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, Qgis::TextComponent part, bool drawAsOutlines=true)
Draws a single component of rendered text using the specified settings.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static bool textRequiresWrapping(const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format)
Returns true if the specified text requires line wrapping in order to fit within the specified width ...
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor=1.0)
Returns the font metrics for the given text format, when rendered in the specified render context.
static double calculateScaleFactorForFormat(const QgsRenderContext &context, const QgsTextFormat &format)
Returns the scale factor used for upscaling font sizes and downscaling destination painter devices.
static QStringList wrappedText(const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format)
Wraps a text string to multiple lines, such that each individual line will fit within the specified w...
friend class QgsLabelPreview
static void drawTextOnLine(const QPolygonF &line, const QString &text, QgsRenderContext &context, const QgsTextFormat &format, double offsetAlongLine=0, double offsetFromLine=0, Qgis::CurvedTextFlags flags=Qgis::CurvedTextFlag::UseBaselinePlacement|Qgis::CurvedTextFlag::TruncateStringWhenLineIsTooShort, Qgis::TextAnchorPoint textAnchor=Qgis::TextAnchorPoint::StartOfText)
Draws text along a line using the specified settings.
friend class QgsVectorLayerLabelProvider
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
static constexpr double SUPERSCRIPT_SUBSCRIPT_FONT_SIZE_SCALING_FACTOR
Scale factor to use for super or subscript text which doesn't have an explicit font size set.
static constexpr double FONT_WORKAROUND_SCALE
Scale factor for upscaling font sizes and downscaling destination painter devices.
static Qgis::TextHorizontalAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a Qgis::TextHorizontalAlignment value.
static void drawDocumentOnLine(const QPolygonF &line, const QgsTextFormat &format, const QgsTextDocument &document, QgsRenderContext &context, double offsetAlongLine=0, double offsetFromLine=0, Qgis::CurvedTextFlags flags=Qgis::CurvedTextFlag::UseBaselinePlacement|Qgis::CurvedTextFlag::TruncateStringWhenLineIsTooShort, Qgis::TextAnchorPoint textAnchor=Qgis::TextAnchorPoint::StartOfText)
Draws a text document along a line using the specified settings.
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:113