QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
22#include <QTextFragment>
23
24using namespace Qt::StringLiterals;
25
27 : mText( text != u"\ufffc"_s ? text : QString() )
28 , mIsImage( text == u"\ufffc"_s )
29 , mCharFormat( format )
30{}
31
32QgsTextFragment::QgsTextFragment( const QTextFragment &fragment )
33 : mText( fragment.text() != u"\ufffc"_s ? fragment.text() : QString() )
34 , mIsImage( fragment.text() == u"\ufffc"_s )
35 , mCharFormat( fragment.charFormat() )
36{
37}
38
39QString QgsTextFragment::text() const
40{
41 return mText;
42}
43
44void QgsTextFragment::setText( const QString &text )
45{
46 mText = text;
47}
48
50{
51 mCharFormat = charFormat;
52}
53
55{
56 return mIsImage;
57}
58
59double QgsTextFragment::horizontalAdvance( const QFont &font, const QgsRenderContext &context, bool fontHasBeenUpdatedForFragment, double scaleFactor ) const
60{
61 if ( fontHasBeenUpdatedForFragment )
62 {
63 const QFontMetricsF fm( font );
64 return fm.horizontalAdvance( mText );
65 }
66 else
67 {
68 QFont updatedFont = font;
69 mCharFormat.updateFontForFormat( updatedFont, context, scaleFactor );
70 const QFontMetricsF fm( updatedFont );
71 return fm.horizontalAdvance( mText );
72 }
73}
74
76{
77 mText = QgsStringUtils::capitalize( mText, capitalization );
78}
79
Capitalization
String capitalization options.
Definition qgis.h:3448
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.