QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgstextlabelfeature.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextlabelfeature.cpp
3 ---------------------
4 begin : December 2015
5 copyright : (C) 2015 by Martin Dobias
6 email : wonder dot sk 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#include "qgstextlabelfeature.h"
17
18#include "qgspallabeling.h"
19#include "qgsmaptopixel.h"
21#include "qgstextfragment.h"
22#include "qgstextblock.h"
23
25 : QgsLabelFeature( id, std::move( geometry ), size )
26{
27 mDefinedFont = QFont();
28}
29
31
32QString QgsTextLabelFeature::text( int partId ) const
33{
34 if ( partId == -1 )
35 return mLabelText;
36 else
37 return mTextMetrics->grapheme( partId );
38}
39
41{
42 return mTextMetrics.has_value() ? mTextMetrics->graphemeFormat( partId ) : QgsTextCharacterFormat();
43}
44
46{
47 return mTextMetrics.has_value() && partId < mTextMetrics->graphemeFormatCount();
48}
49
50QgsPrecalculatedTextMetrics QgsTextLabelFeature::calculateTextMetrics( const QgsMapToPixel *xform, const QgsRenderContext &context, const QFont &baseFont, const QFontMetricsF &fontMetrics, double letterSpacing, double wordSpacing, const QString &text, QgsTextDocument *document, QgsTextDocumentMetrics * )
51{
52 // create label info!
53 const double mapScale = xform->mapUnitsPerPixel();
54 QStringList graphemes;
55 QVector< QgsTextCharacterFormat > graphemeFormats;
56
57 if ( document )
58 {
59 for ( const QgsTextBlock &block : std::as_const( *document ) )
60 {
61 for ( const QgsTextFragment &fragment : block )
62 {
63 const QStringList fragmentGraphemes = QgsPalLabeling::splitToGraphemes( fragment.text() );
64 for ( const QString &grapheme : fragmentGraphemes )
65 {
66 graphemes.append( grapheme );
67 graphemeFormats.append( fragment.characterFormat() );
68 }
69 }
70 }
71 }
72 else
73 {
74 //split string by valid grapheme boundaries - required for certain scripts (see #6883)
76 }
77
78 QVector< double > characterWidths( graphemes.count() );
79 QVector< double > characterHeights( graphemes.count() );
80 QVector< double > characterDescents( graphemes.count() );
81 for ( int i = 0; i < graphemes.count(); i++ )
82 {
83 // reconstruct how Qt creates word spacing, then adjust per individual stored character
84 // this will allow PAL to create each candidate width = character width + correct spacing
85
86 double graphemeFirstCharHorizontalAdvanceWithLetterSpacing = 0;
87 double graphemeFirstCharHorizontalAdvance = 0;
88 double graphemeHorizontalAdvance = 0;
89 double characterDescent = 0;
90 double characterHeight = 0;
91 if ( const QgsTextCharacterFormat *graphemeFormat = !graphemeFormats.empty() ? &graphemeFormats[i] : nullptr )
92 {
93 QFont graphemeFont = baseFont;
94 graphemeFormat->updateFontForFormat( graphemeFont, context, 1 );
95 const QFontMetricsF graphemeFontMetrics( graphemeFont );
96 graphemeFirstCharHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
97 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = graphemeFontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
98 graphemeHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i] ) );
99 characterDescent = graphemeFontMetrics.descent();
100 characterHeight = graphemeFontMetrics.height();
101 }
102 else
103 {
104 graphemeFirstCharHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
105 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = fontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
106 graphemeHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i] ) );
107 characterDescent = fontMetrics.descent();
108 characterHeight = fontMetrics.height();
109 }
110
111 qreal wordSpaceFix = qreal( 0.0 );
112 if ( graphemes[i] == QLatin1String( " " ) )
113 {
114 // word spacing only gets added once at end of consecutive run of spaces, see QTextEngine::shapeText()
115 int nxt = i + 1;
116 wordSpaceFix = ( nxt < graphemes.count() && graphemes[nxt] != QLatin1String( " " ) ) ? wordSpacing : qreal( 0.0 );
117 }
118
119 // this workaround only works for clusters with a single character. Not sure how it should be handled
120 // with multi-character clusters.
121 if ( graphemes[i].length() == 1 &&
122 !qgsDoubleNear( graphemeFirstCharHorizontalAdvance, graphemeFirstCharHorizontalAdvanceWithLetterSpacing ) )
123 {
124 // word spacing applied when it shouldn't be
125 wordSpaceFix -= wordSpacing;
126 }
127
128 const double charWidth = graphemeHorizontalAdvance + wordSpaceFix;
129 characterWidths[i] = mapScale * charWidth;
130 characterHeights[i] = mapScale * characterHeight;
131 characterDescents[i] = mapScale * characterDescent;
132 }
133
134 QgsPrecalculatedTextMetrics res( graphemes, std::move( characterWidths ), std::move( characterHeights ), std::move( characterDescents ) );
135 res.setGraphemeFormats( graphemeFormats );
136 return res;
137}
138
140{
141 return mDocument;
142}
143
145{
146 return mDocumentMetrics;
147}
148
150{
152 mDocumentMetrics = metrics;
153}
The QgsLabelFeature class describes a feature that should be used within the labeling engine.
QString mLabelText
text of the label
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
double mapUnitsPerPixel() const
Returns the current map units per pixel.
static QStringList splitToGraphemes(const QString &text)
Splits a text string to a list of graphemes, which are the smallest allowable character divisions in ...
Contains precalculated properties regarding text metrics for text to be renderered at a later stage.
void setGraphemeFormats(const QVector< QgsTextCharacterFormat > &formats)
Sets the character formats associated with the text graphemes().
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
Stores information relating to individual character formatting.
Contains pre-calculated metrics of a QgsTextDocument.
Represents a document consisting of one or more QgsTextBlock objects.
Stores a fragment of text along with formatting overrides to be used when rendering the fragment.
QgsTextLabelFeature(QgsFeatureId id, geos::unique_ptr geometry, QSizeF size)
Construct text label feature.
QgsTextDocument document() const
Returns the document for the label.
static QgsPrecalculatedTextMetrics calculateTextMetrics(const QgsMapToPixel *xform, const QgsRenderContext &context, const QFont &baseFont, const QFontMetricsF &fontMetrics, double letterSpacing, double wordSpacing, const QString &text=QString(), QgsTextDocument *document=nullptr, QgsTextDocumentMetrics *metrics=nullptr)
Calculate text metrics for later retrieval via textMetrics().
QgsTextDocument mDocument
~QgsTextLabelFeature() override
Clean up.
QgsTextCharacterFormat characterFormat(int partId) const
Returns the character format corresponding to the specified label part.
QFont mDefinedFont
Font for rendering.
QgsTextDocumentMetrics mDocumentMetrics
QgsTextDocumentMetrics documentMetrics() const
Returns the document metrics for the label.
QString text(int partId) const
Returns the text component corresponding to a specified label part.
bool hasCharacterFormat(int partId) const
Returns true if the feature contains specific character formatting for the part with matching ID.
std::optional< QgsPrecalculatedTextMetrics > mTextMetrics
void setDocument(const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics)
Sets the document and document metrics for the label.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:74
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2527
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28