QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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,
93 Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(),
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,
127 Qgis::TextRendererFlags flags = Qgis::TextRendererFlags() );
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
158 static void drawTextOnLine( const QPolygonF &line, const QString &text,
159 QgsRenderContext &context, const QgsTextFormat &format,
160 double offsetAlongLine = 0, double offsetFromLine = 0 );
161
174 static void drawDocumentOnLine( const QPolygonF &line,
175 const QgsTextFormat &format,
176 const QgsTextDocument &document,
177 QgsRenderContext &context,
178 double offsetAlongLine = 0,
179 double offsetFromLine = 0 );
180
199 Q_DECL_DEPRECATED static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
200 QgsRenderContext &context, const QgsTextFormat &format,
201 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
202
221 Q_DECL_DEPRECATED static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines,
222 QgsRenderContext &context, const QgsTextFormat &format,
223 Qgis::TextComponent part, bool drawAsOutlines = true ) SIP_DEPRECATED;
224
237 static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor = 1.0 );
238
246 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
247 QFontMetricsF *fontMetrics = nullptr );
248
259 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point,
260 QFontMetricsF *fontMetrics = nullptr, Qgis::TextRendererFlags flags = Qgis::TextRendererFlags(), double maxLineWidth = 0 );
261
273 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects = false );
274
281 static bool textRequiresWrapping( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
282
289 static QStringList wrappedText( const QgsRenderContext &context, const QString &text, double width, const QgsTextFormat &format );
290
299 static constexpr double FONT_WORKAROUND_SCALE = 10;
300
301 // to match QTextEngine handling of superscript/subscript font sizes
302
308 static constexpr double SUPERSCRIPT_SUBSCRIPT_FONT_SIZE_SCALING_FACTOR = 2.0 / 3.0;
309
310 private:
311
312 struct Component
313 {
315 QgsTextBlock block;
316
318 int blockIndex = 0;
319
321 int firstFragmentIndex = 0;
322
324 QPointF origin;
326 bool useOrigin = false;
328 double rotation = 0.0;
330 double rotationOffset = 0.0;
332 QPointF center;
334 QSizeF size;
336 QPointF offset;
338 QPicture picture;
339
344 double pictureBuffer = 0.0;
346 double dpiRatio = 1.0;
349
351 double extraWordSpacing = 0;
353 double extraLetterSpacing = 0;
354 };
355
356 static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
357 static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, Qgis::TextLayoutMode mode = Qgis::TextLayoutMode::Point );
358
376 static void drawPart( const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, Qgis::TextVerticalAlignment vAlignment, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics,
377 QgsRenderContext &context, const QgsTextFormat &format,
379
396 static void drawPart( QPointF origin, double rotation, Qgis::TextHorizontalAlignment alignment, const QgsTextDocument &document,
397 const QgsTextDocumentMetrics &metrics,
398 QgsRenderContext &context, const QgsTextFormat &format,
401
402 static double drawBuffer( QgsRenderContext &context,
403 const Component &component,
404 const QgsTextFormat &format,
405 const QgsTextDocumentMetrics &metrics,
407
408 static void drawBackground( QgsRenderContext &context,
409 Component component,
410 const QgsTextFormat &format,
411 const QgsTextDocumentMetrics &metrics,
413
414 static void drawShadow( QgsRenderContext &context,
415 const Component &component,
416 const QgsTextFormat &format );
417
418 static void drawMask( QgsRenderContext &context,
419 const Component &component,
420 const QgsTextFormat &format,
421 const QgsTextDocumentMetrics &metrics,
423
424 static void drawText( QgsRenderContext &context,
425 const Component &component,
426 const QgsTextFormat &format );
427
428 static void drawTextInternal( Qgis::TextComponent drawType,
429 QgsRenderContext &context,
430 const QgsTextFormat &format,
431 const Component &component,
432 const QgsTextDocument &document,
433 const QgsTextDocumentMetrics &metrics,
437
438 static Qgis::TextOrientation calculateRotationAndOrientationForComponent( const QgsTextFormat &format, const Component &component, double &rotation );
439
440 static void calculateExtraSpacingForLineJustification( double spaceToDistribute, const QgsTextBlock &block, double &extraWordSpace, double &extraLetterSpace );
441 static void applyExtraSpacingForLineJustification( QFont &font, double extraWordSpace, double extraLetterSpace );
442
443 static void drawTextInternalHorizontal( QgsRenderContext &context,
444 const QgsTextFormat &format,
445 Qgis::TextComponent drawType,
447 const Component &component,
448 const QgsTextDocument &document,
449 const QgsTextDocumentMetrics &metrics,
450 double fontScale,
453 double rotation );
454
455 static void drawTextInternalVertical( QgsRenderContext &context,
456 const QgsTextFormat &format,
457 Qgis::TextComponent drawType,
459 const Component &component,
460 const QgsTextDocument &document,
461 const QgsTextDocumentMetrics &metrics,
462 double fontScale,
465 double rotation );
466
467 static double calculateScaleFactorForFormat( const QgsRenderContext &context, const QgsTextFormat &format );
468
470 friend class QgsLabelPreview;
471
472 static QgsTextFormat updateShadowPosition( const QgsTextFormat &format );
473
474};
475
476#endif // QGSTEXTRENDERER_H
TextLayoutMode
Text layout modes.
Definition qgis.h:2361
@ Point
Text at point of origin layout mode.
@ Rectangle
Text within rectangle layout mode.
TextOrientation
Text orientations.
Definition qgis.h:2346
RenderUnit
Rendering size units.
Definition qgis.h:4193
TextVerticalAlignment
Text vertical alignment.
Definition qgis.h:2413
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:2394
TextComponent
Text components.
Definition qgis.h:2378
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