QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
qgstextcharacterformat.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextcharacterformat.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
17#include "qgsrendercontext.h"
18#include "qgsfontutils.h"
19
20#include <QTextCharFormat>
21
22Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign( const QTextCharFormat &format, bool &set )
23{
24 set = format.hasProperty( QTextFormat::TextVerticalAlignment );
25 switch ( format.verticalAlignment() )
26 {
27 case QTextCharFormat::AlignNormal:
29 case QTextCharFormat::AlignSuperScript:
31 case QTextCharFormat::AlignSubScript:
33
34 // not yet supported
35 case QTextCharFormat::AlignMiddle:
36 case QTextCharFormat::AlignTop:
37 case QTextCharFormat::AlignBottom:
38 case QTextCharFormat::AlignBaseline:
39 set = false;
41 }
43}
44
45QgsTextCharacterFormat::QgsTextCharacterFormat( const QTextCharFormat &format )
46 : mTextColor( format.hasProperty( QTextFormat::ForegroundBrush ) ? format.foreground().color() : QColor() )
47 , mFontWeight( format.hasProperty( QTextFormat::FontWeight ) ? format.fontWeight() : -1 )
48 , mStyleName( format.font().styleName() )
49 , mItalic( format.hasProperty( QTextFormat::FontItalic ) ? ( format.fontItalic() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
50 , mFontPointSize( format.hasProperty( QTextFormat::FontPointSize ) ? format.fontPointSize() : - 1 )
51 , mWordSpacing( format.hasProperty( QTextFormat::FontWordSpacing ) ? format.fontWordSpacing() : std::numeric_limits< double >::quiet_NaN() )
52 , mStrikethrough( format.hasProperty( QTextFormat::FontStrikeOut ) ? ( format.fontStrikeOut() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
53 , mUnderline( format.hasProperty( QTextFormat::FontUnderline ) ? ( format.fontUnderline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
54 , mOverline( format.hasProperty( QTextFormat::FontOverline ) ? ( format.fontOverline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
55{
56 mVerticalAlign = convertTextCharFormatVAlign( format, mHasVerticalAlignSet );
57
58 if ( format.hasProperty( QTextFormat::FontFamily ) )
59 {
60 mFontFamily = format.fontFamily();
61 }
62 if ( mFontFamily.isEmpty() && format.hasProperty( QTextFormat::FontFamilies ) )
63 {
64 const QStringList families = format.fontFamilies().toStringList();
65 if ( !families.isEmpty() )
66 mFontFamily = families.at( 0 );
67 }
68}
69
71{
72 if ( !mTextColor.isValid() && other.mTextColor.isValid() )
73 mTextColor = other.mTextColor;
74 if ( mFontPointSize == -1 && other.mFontPointSize != -1 )
75 mFontPointSize = other.mFontPointSize;
76 if ( mFontPercentageSize == -1 && other.mFontPercentageSize != -1 )
77 mFontPercentageSize = other.mFontPercentageSize;
78 if ( std::isnan( mWordSpacing ) )
79 mWordSpacing = other.mWordSpacing;
80 if ( mFontFamily.isEmpty() && !other.mFontFamily.isEmpty() )
81 mFontFamily = other.mFontFamily;
82 if ( mStrikethrough == BooleanValue::NotSet && other.mStrikethrough != BooleanValue::NotSet )
83 mStrikethrough = other.mStrikethrough;
84 if ( mUnderline == BooleanValue::NotSet && other.mUnderline != BooleanValue::NotSet )
85 mUnderline = other.mUnderline;
86 if ( mOverline == BooleanValue::NotSet && other.mOverline != BooleanValue::NotSet )
87 mOverline = other.mOverline;
88 if ( mItalic == BooleanValue::NotSet && other.mItalic != BooleanValue::NotSet )
89 mItalic = other.mItalic;
90 if ( mFontWeight == -1 && other.mFontWeight != -1 )
91 mFontWeight = other.mFontWeight;
92 if ( mStyleName.isEmpty() && ! other.mStyleName.isEmpty() )
93 mStyleName = other.mStyleName;
94 if ( mHasVerticalAlignSet && other.hasVerticalAlignmentSet() )
95 {
96 mVerticalAlign = other.mVerticalAlign;
97 mHasVerticalAlignSet = true;
98 }
99}
100
102{
103 return mTextColor;
104}
105
106void QgsTextCharacterFormat::setTextColor( const QColor &textColor )
107{
108 mTextColor = textColor;
109}
110
112{
113 return mFontPointSize;
114}
115
117{
118 mFontPointSize = size;
119}
120
122{
123 return mFontPercentageSize;
124}
125
127{
128 mFontPercentageSize = size;
129}
130
132{
133 return mFontFamily;
134}
135
136void QgsTextCharacterFormat::setFamily( const QString &family )
137{
138 mFontFamily = family;
139}
140
145
147{
148 mStrikethrough = strikethrough;
149}
150
155
157{
158 mUnderline = underline;
159}
160
165
167{
168 mOverline = enabled;
169}
170
171void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const QgsRenderContext &context, const double scaleFactor ) const
172{
173 // important -- MUST set family first
174 if ( !mFontFamily.isEmpty() )
175 QgsFontUtils::setFontFamily( font, mFontFamily );
176
177 if ( mFontPointSize != -1 )
178 font.setPixelSize( scaleFactor * context.convertToPainterUnits( mFontPointSize, Qgis::RenderUnit::Points ) );
179
180 if ( mFontPercentageSize != -1 )
181 font.setPixelSize( font.pixelSize() * mFontPercentageSize );
182
184 font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::SetTrue );
185
186 if ( mFontWeight != - 1 )
187 {
188#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
189 font.setWeight( mFontWeight );
190#else
191 if ( mFontWeight <= 150 )
192 font.setWeight( QFont::Thin );
193 else if ( mFontWeight <= 250 )
194 font.setWeight( QFont::ExtraLight );
195 else if ( mFontWeight <= 350 )
196 font.setWeight( QFont::Light );
197 else if ( mFontWeight <= 450 )
198 font.setWeight( QFont::Normal );
199 else if ( mFontWeight <= 550 )
200 font.setWeight( QFont::Medium );
201 else if ( mFontWeight <= 650 )
202 font.setWeight( QFont::DemiBold );
203 else if ( mFontWeight <= 750 )
204 font.setWeight( QFont::Bold );
205 else if ( mFontWeight <= 850 )
206 font.setWeight( QFont::ExtraBold );
207 else
208 font.setWeight( QFont::Black );
209#endif
210
211 // depending on the font, platform, and the phase of the moon, we need to both set the font weight AND the style name
212 // in order to get correct rendering!
213 font.setStyleName( mStyleName );
214 }
215
216 if ( mUnderline != BooleanValue::NotSet )
217 font.setUnderline( mUnderline == QgsTextCharacterFormat::BooleanValue::SetTrue );
218 if ( mOverline != BooleanValue::NotSet )
219 font.setOverline( mOverline == QgsTextCharacterFormat::BooleanValue::SetTrue );
220 if ( mStrikethrough != QgsTextCharacterFormat::BooleanValue::NotSet )
221 font.setStrikeOut( mStrikethrough == QgsTextCharacterFormat::BooleanValue::SetTrue );
222
223 if ( !std::isnan( mWordSpacing ) )
224 {
225 font.setWordSpacing( scaleFactor * context.convertToPainterUnits( mWordSpacing, Qgis::RenderUnit::Points ) );
226 }
227}
228
233
235{
236 mItalic = enabled;
237}
238
240{
241 return mFontWeight;
242}
243
245{
246 mFontWeight = fontWeight;
247}
248
250{
251 return mWordSpacing;
252}
253
255{
256 mWordSpacing = spacing;
257}
TextCharacterVerticalAlignment
Text vertical alignment for characters.
Definition qgis.h:2765
@ 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.
@ Points
Points (e.g., for font sizes)
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
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).
Stores information relating to individual character formatting.
void setFamily(const QString &family)
Sets the font family name.
void overrideWith(const QgsTextCharacterFormat &other)
Override all the default/unset properties of the current character format with the settings from anot...
void updateFontForFormat(QFont &font, const QgsRenderContext &context, double scaleFactor=1.0) const
Updates the specified font in place, applying character formatting options which are applicable on a ...
void setFontWeight(int fontWeight)
Sets the font weight.
QColor textColor() const
Returns the character's text color, or an invalid color if no color override is set and the default f...
BooleanValue italic() const
Returns whether the format has italic enabled.
QgsTextCharacterFormat()=default
void setStrikeOut(BooleanValue enabled)
Sets whether the format has strikethrough enabled.
void setOverline(BooleanValue enabled)
Sets whether the format has overline enabled.
BooleanValue
Status values for boolean format properties.
@ SetTrue
Property is set and true.
int fontWeight() const
Returns the font weight, or -1 if the font weight is not set and should be inherited.
bool hasVerticalAlignmentSet() const
Returns true if the format has an explicit vertical alignment set.
BooleanValue strikeOut() const
Returns whether the format has strikethrough enabled.
double wordSpacing() const
Returns the font word spacing, in points, or NaN if word spacing is not set and should be inherited.
double fontPointSize() const
Returns the font point size, or -1 if the font size is not set and should be inherited.
QString family() const
Returns the font family name, or an empty string if the family is not set and should be inherited.
BooleanValue underline() const
Returns whether the format has underline enabled.
void setWordSpacing(double spacing)
Sets the font word spacing, in points, or NaN if word spacing is not set and should be inherited.
void setTextColor(const QColor &textColor)
Sets the character's text color.
void setUnderline(BooleanValue enabled)
Sets whether the format has underline enabled.
void setItalic(BooleanValue enabled)
Sets whether the format has italic enabled.
BooleanValue overline() const
Returns whether the format has overline enabled.
void setFontPointSize(double size)
Sets the font point size.
void setFontPercentageSize(double size)
Sets the font percentage size (as fraction of inherited font size).
double fontPercentageSize() const
Returns the font percentage size (as fraction of inherited font size), or -1 if the font size percent...
#define BUILTIN_UNREACHABLE
Definition qgis.h:6511
Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign(const QTextCharFormat &format, bool &set)