62 const QList< QgsTextFormat::Tab > tabPositions = format.
tabPositions();
63 QList< double > tabStopDistancesPainterUnits;
64 tabStopDistancesPainterUnits.reserve( tabPositions.size() );
67 tabStopDistancesPainterUnits.append(
69 ? tab.position() * baseFont.pixelSize()
77 QStringList graphemes;
78 QVector< QgsTextCharacterFormat > graphemeFormats;
85 for (
const QString &grapheme : fragmentGraphemes )
87 graphemes.append( grapheme );
88 graphemeFormats.append( fragment.characterFormat() );
93 QVector< double > characterWidths( graphemes.count() );
94 QVector< double > characterHeights( graphemes.count() );
95 QVector< double > characterDescents( graphemes.count() );
97 QFont previousNonSuperSubScriptFont;
99 double currentWidth = 0;
100 for (
int i = 0; i < graphemes.count(); i++ )
105 double graphemeFirstCharHorizontalAdvanceWithLetterSpacing = 0;
106 double graphemeFirstCharHorizontalAdvance = 0;
107 double graphemeHorizontalAdvance = 0;
108 double characterDescent = 0;
109 double characterHeight = 0;
110 if ( graphemes[i] ==
'\t' )
112 double nextTabStop = 0;
113 if ( !tabStopDistancesPainterUnits.empty() )
116 nextTabStop = currentWidth;
117 for (
const double tabStop : std::as_const( tabStopDistancesPainterUnits ) )
119 if ( tabStop >= currentWidth )
121 nextTabStop = tabStop;
128 nextTabStop = ( std::floor( currentWidth / tabStopDistancePainterUnits ) + 1 ) * tabStopDistancePainterUnits;
131 const double thisTabWidth = nextTabStop - currentWidth;
133 graphemeFirstCharHorizontalAdvance = thisTabWidth;
134 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = thisTabWidth;
135 graphemeHorizontalAdvance = thisTabWidth;
136 characterDescent = fontMetrics.descent();
137 characterHeight = fontMetrics.height();
139 else if (
const QgsTextCharacterFormat *graphemeFormat = !graphemeFormats.empty() ? &graphemeFormats[i] :
nullptr )
141 QFont graphemeFont = baseFont;
142 graphemeFormat->updateFontForFormat( graphemeFont, context, 1 );
145 previousNonSuperSubScriptFont = graphemeFont;
147 if ( graphemeFormat->hasVerticalAlignmentSet() )
149 switch ( graphemeFormat->verticalAlignment() )
152 previousNonSuperSubScriptFont = graphemeFont;
158 if ( graphemeFormat->fontPointSize() < 0 )
173 previousNonSuperSubScriptFont = graphemeFont;
176 const QFontMetricsF graphemeFontMetrics( graphemeFont );
177 graphemeFirstCharHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
178 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = graphemeFontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
179 graphemeHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i] ) );
180 characterDescent = graphemeFontMetrics.descent();
181 characterHeight = graphemeFontMetrics.height();
185 graphemeFirstCharHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
186 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = fontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
187 graphemeHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i] ) );
188 characterDescent = fontMetrics.descent();
189 characterHeight = fontMetrics.height();
192 qreal wordSpaceFix = qreal( 0.0 );
193 if ( graphemes[i] ==
" "_L1 )
197 wordSpaceFix = ( nxt < graphemes.count() && graphemes[nxt] !=
" "_L1 ) ? wordSpacing : qreal( 0.0 );
202 if ( graphemes[i].length() == 1 &&
203 !
qgsDoubleNear( graphemeFirstCharHorizontalAdvance, graphemeFirstCharHorizontalAdvanceWithLetterSpacing ) )
206 wordSpaceFix -= wordSpacing;
209 const double charWidth = graphemeHorizontalAdvance + wordSpaceFix;
210 characterWidths[i] = mapScale * charWidth;
211 characterHeights[i] = mapScale * characterHeight;
212 characterDescents[i] = mapScale * characterDescent;
214 currentWidth += charWidth;
217 QgsPrecalculatedTextMetrics res( graphemes, std::move( characterWidths ), std::move( characterHeights ), std::move( characterDescents ) );
Contains precalculated properties regarding text metrics for text to be rendered 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.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
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 document along with formatting overrides to be used when rendering the fragment.
const QgsTextDocument & document() const
Returns the document for the label.
QgsTextCharacterFormat characterFormat(int partId) const
Returns the character format corresponding to the specified label part.
QgsTextLabelFeature(QgsFeatureId id, geos::unique_ptr geometry, QSizeF size, int subPartId=0)
Construct text label feature.
static QgsPrecalculatedTextMetrics calculateTextMetrics(const QgsMapToPixel *xform, const QgsRenderContext &context, const QgsTextFormat &format, const QFont &baseFont, const QFontMetricsF &fontMetrics, double letterSpacing, double wordSpacing, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics)
Calculate text metrics for later retrieval via textMetrics().
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.