QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgstextfragment.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextfragment.cpp
3 -----------------
4 begin : May 2020
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#include "qgstextfragment.h"
17
18#include "qgsstringutils.h"
19
20#include <QFontMetricsF>
21#include <QTextFragment>
22
24 : mText( text != QStringLiteral( "\ufffc" ) ? text : QString() )
25 , mIsImage( text == QStringLiteral( "\ufffc" ) )
26 , mCharFormat( format )
27{}
28
29QgsTextFragment::QgsTextFragment( const QTextFragment &fragment )
30 : mText( fragment.text() != QStringLiteral( "\ufffc" ) ? fragment.text() : QString() )
31 , mIsImage( fragment.text() == QStringLiteral( "\ufffc" ) )
32 , mCharFormat( fragment.charFormat() )
33{
34}
35
36QString QgsTextFragment::text() const
37{
38 return mText;
39}
40
41void QgsTextFragment::setText( const QString &text )
42{
43 mText = text;
44}
45
47{
48 mCharFormat = charFormat;
49}
50
52{
53 return mIsImage;
54}
55
56double QgsTextFragment::horizontalAdvance( const QFont &font, const QgsRenderContext &context, bool fontHasBeenUpdatedForFragment, double scaleFactor ) const
57{
58 if ( fontHasBeenUpdatedForFragment )
59 {
60 const QFontMetricsF fm( font );
61 return fm.horizontalAdvance( mText );
62 }
63 else
64 {
65 QFont updatedFont = font;
66 mCharFormat.updateFontForFormat( updatedFont, context, scaleFactor );
67 const QFontMetricsF fm( updatedFont );
68 return fm.horizontalAdvance( mText );
69 }
70}
71
73{
74 mText = QgsStringUtils::capitalize( mText, capitalization );
75}
76
Capitalization
String capitalization options.
Definition qgis.h:3389
Contains information about the context of a rendering operation.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Stores information relating to individual character formatting.
void setText(const QString &text)
Sets the text content of the fragment.
QString text() const
Returns the text content of the fragment.
void setCharacterFormat(const QgsTextCharacterFormat &format)
Sets the character format for the fragment.
bool isImage() const
Returns true if the fragment represents an image.
double horizontalAdvance(const QFont &font, const QgsRenderContext &context, bool fontHasBeenUpdatedForFragment=false, double scaleFactor=1.0) const
Returns the horizontal advance associated with this fragment, when rendered using the specified base ...
QgsTextFragment(const QString &text=QString(), const QgsTextCharacterFormat &format=QgsTextCharacterFormat())
Constructor for QgsTextFragment, with the specified text and optional character format.
void applyCapitalization(Qgis::Capitalization capitalization)
Applies a capitalization style to the fragment's text.