58 QStringList graphemes;
59 QVector< QgsTextCharacterFormat > graphemeFormats;
68 for (
const QString &grapheme : fragmentGraphemes )
70 graphemes.append( grapheme );
71 graphemeFormats.append( fragment.characterFormat() );
82 QVector< double > characterWidths( graphemes.count() );
83 QVector< double > characterHeights( graphemes.count() );
84 QVector< double > characterDescents( graphemes.count() );
86 QFont previousNonSuperSubScriptFont;
88 for (
int i = 0; i < graphemes.count(); i++ )
93 double graphemeFirstCharHorizontalAdvanceWithLetterSpacing = 0;
94 double graphemeFirstCharHorizontalAdvance = 0;
95 double graphemeHorizontalAdvance = 0;
96 double characterDescent = 0;
97 double characterHeight = 0;
98 if (
const QgsTextCharacterFormat *graphemeFormat = !graphemeFormats.empty() ? &graphemeFormats[i] :
nullptr )
100 QFont graphemeFont = baseFont;
101 graphemeFormat->updateFontForFormat( graphemeFont, context, 1 );
104 previousNonSuperSubScriptFont = graphemeFont;
106 if ( graphemeFormat->hasVerticalAlignmentSet() )
108 switch ( graphemeFormat->verticalAlignment() )
111 previousNonSuperSubScriptFont = graphemeFont;
117 if ( graphemeFormat->fontPointSize() < 0 )
132 previousNonSuperSubScriptFont = graphemeFont;
135 const QFontMetricsF graphemeFontMetrics( graphemeFont );
136 graphemeFirstCharHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
137 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = graphemeFontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
138 graphemeHorizontalAdvance = graphemeFontMetrics.horizontalAdvance( QString( graphemes[i] ) );
139 characterDescent = graphemeFontMetrics.descent();
140 characterHeight = graphemeFontMetrics.height();
144 graphemeFirstCharHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i].at( 0 ) ) );
145 graphemeFirstCharHorizontalAdvanceWithLetterSpacing = fontMetrics.horizontalAdvance( graphemes[i].at( 0 ) ) + letterSpacing;
146 graphemeHorizontalAdvance = fontMetrics.horizontalAdvance( QString( graphemes[i] ) );
147 characterDescent = fontMetrics.descent();
148 characterHeight = fontMetrics.height();
151 qreal wordSpaceFix = qreal( 0.0 );
152 if ( graphemes[i] == QLatin1String(
" " ) )
156 wordSpaceFix = ( nxt < graphemes.count() && graphemes[nxt] != QLatin1String(
" " ) ) ? wordSpacing : qreal( 0.0 );
161 if ( graphemes[i].length() == 1 &&
162 !
qgsDoubleNear( graphemeFirstCharHorizontalAdvance, graphemeFirstCharHorizontalAdvanceWithLetterSpacing ) )
165 wordSpaceFix -= wordSpacing;
168 const double charWidth = graphemeHorizontalAdvance + wordSpaceFix;
169 characterWidths[i] = mapScale * charWidth;
170 characterHeights[i] = mapScale * characterHeight;
171 characterDescents[i] = mapScale * characterDescent;
174 QgsPrecalculatedTextMetrics res( graphemes, std::move( characterWidths ), std::move( characterHeights ), std::move( characterDescents ) );
@ Normal
Adjacent characters are positioned in the standard way for text in the writing system in use.
@ SubScript
Characters are placed below the base line for normal text.
@ SuperScript
Characters are placed above the base line for normal text.
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.
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.
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.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
constexpr double SUPERSCRIPT_SUBSCRIPT_FONT_SIZE_SCALING_FACTOR