56 QStringList graphemes;
57 QVector< QgsTextCharacterFormat > graphemeFormats;
66 for (
const QString &grapheme : fragmentGraphemes )
68 graphemes.append( grapheme );
69 graphemeFormats.append( fragment.characterFormat() );
80 QVector< double > characterWidths( graphemes.count() );
81 QVector< double > characterHeights( graphemes.count() );
82 QVector< double > characterDescents( graphemes.count() );
84 QFont previousNonSuperSubScriptFont;
86 for (
int i = 0; i < graphemes.count(); i++ )
91 double graphemeFirstCharHorizontalAdvanceWithLetterSpacing = 0;
92 double graphemeFirstCharHorizontalAdvance = 0;
93 double graphemeHorizontalAdvance = 0;
94 double characterDescent = 0;
95 double characterHeight = 0;
96 if (
const QgsTextCharacterFormat *graphemeFormat = !graphemeFormats.empty() ? &graphemeFormats[i] : nullptr )
98 QFont graphemeFont = baseFont;
99 graphemeFormat->updateFontForFormat( graphemeFont, context, 1 );
102 previousNonSuperSubScriptFont = graphemeFont;
104 if ( graphemeFormat->hasVerticalAlignmentSet() )
106 switch ( graphemeFormat->verticalAlignment() )
109 previousNonSuperSubScriptFont = graphemeFont;
115 if ( graphemeFormat->fontPointSize() < 0 )
130 previousNonSuperSubScriptFont = graphemeFont;
133 const QFontMetricsF graphemeFontMetrics( graphemeFont );
134 graphemeFirstCharHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
135 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = graphemeFontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
136 graphemeHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i] ) );
137 characterDescent = graphemeFontMetrics.descent();
138 characterHeight = graphemeFontMetrics.height();
142 graphemeFirstCharHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
143 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = fontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
144 graphemeHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i] ) );
145 characterDescent = fontMetrics.descent();
146 characterHeight = fontMetrics.height();
149 qreal wordSpaceFix = qreal( 0.0 );
150 if ( graphemes[i] == QLatin1String(
" " ) )
154 wordSpaceFix = ( nxt < graphemes.count() && graphemes[nxt] != QLatin1String(
" " ) ) ? wordSpacing : qreal( 0.0 );
159 if ( graphemes[i].length() == 1 &&
160 !
qgsDoubleNear( graphemeFirstCharHorizontalAdvance, graphemeFirstCharHorizontalAdvanceWithLetterSpacing ) )
163 wordSpaceFix -= wordSpacing;
166 const double charWidth = graphemeHorizontalAdvance + wordSpaceFix;
167 characterWidths[i] = mapScale * charWidth;
168 characterHeights[i] = mapScale * characterHeight;
169 characterDescents[i] = mapScale * characterDescent;
172 QgsPrecalculatedTextMetrics res( graphemes, std::move( characterWidths ), std::move( characterHeights ), std::move( characterDescents ) );
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.
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.
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.
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.