QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 , mBackgroundBrush( format.background() )
56 , mBackgroundPath( format.background().style() == Qt::NoBrush ? format.stringProperty( QTextFormat::BackgroundImageUrl ) : QString() )
57{
58 mVerticalAlign = convertTextCharFormatVAlign( format, mHasVerticalAlignSet );
59
60 if ( format.hasProperty( QTextFormat::FontFamily ) )
61 {
62 mFontFamily = format.fontFamily();
63 }
64 if ( mFontFamily.isEmpty() && format.hasProperty( QTextFormat::FontFamilies ) )
65 {
66 const QStringList families = format.fontFamilies().toStringList();
67 if ( !families.isEmpty() )
68 mFontFamily = families.at( 0 );
69 }
70 if ( format.isImageFormat() )
71 {
72 const QTextImageFormat imageFormat = format.toImageFormat();
73 mImagePath = imageFormat.name();
74 mImageSize = QSizeF( imageFormat.width(), imageFormat.height() );
75 }
76}
77
79{
80 if ( !mTextColor.isValid() && other.mTextColor.isValid() )
81 mTextColor = other.mTextColor;
82 if ( mFontPointSize == -1 && other.mFontPointSize != -1 )
83 mFontPointSize = other.mFontPointSize;
84 if ( mFontPercentageSize == -1 && other.mFontPercentageSize != -1 )
85 mFontPercentageSize = other.mFontPercentageSize;
86 if ( std::isnan( mWordSpacing ) )
87 mWordSpacing = other.mWordSpacing;
88 if ( mFontFamily.isEmpty() && !other.mFontFamily.isEmpty() )
89 mFontFamily = other.mFontFamily;
90 if ( mStrikethrough == BooleanValue::NotSet && other.mStrikethrough != BooleanValue::NotSet )
91 mStrikethrough = other.mStrikethrough;
92 if ( mUnderline == BooleanValue::NotSet && other.mUnderline != BooleanValue::NotSet )
93 mUnderline = other.mUnderline;
94 if ( mOverline == BooleanValue::NotSet && other.mOverline != BooleanValue::NotSet )
95 mOverline = other.mOverline;
96 if ( mItalic == BooleanValue::NotSet && other.mItalic != BooleanValue::NotSet )
97 mItalic = other.mItalic;
98 if ( mFontWeight == -1 && other.mFontWeight != -1 )
99 mFontWeight = other.mFontWeight;
100 if ( mStyleName.isEmpty() && ! other.mStyleName.isEmpty() )
101 mStyleName = other.mStyleName;
102 if ( mHasVerticalAlignSet && other.hasVerticalAlignmentSet() )
103 {
104 mVerticalAlign = other.mVerticalAlign;
105 mHasVerticalAlignSet = true;
106 }
107 if ( mBackgroundBrush.style() == Qt::NoBrush && mBackgroundPath.isEmpty() && other.mBackgroundBrush.style() != Qt::NoBrush )
108 mBackgroundBrush = other.mBackgroundBrush;
109 if ( mBackgroundBrush.style() == Qt::NoBrush && mBackgroundPath.isEmpty() && !other.mBackgroundPath.isEmpty() )
110 mBackgroundPath = other.mBackgroundPath;
111}
112
114{
115 return mTextColor;
116}
117
118void QgsTextCharacterFormat::setTextColor( const QColor &textColor )
119{
120 mTextColor = textColor;
121}
122
124{
125 return mFontPointSize;
126}
127
129{
130 mFontPointSize = size;
131}
132
134{
135 return mFontPercentageSize;
136}
137
139{
140 mFontPercentageSize = size;
141}
142
144{
145 return mFontFamily;
146}
147
148void QgsTextCharacterFormat::setFamily( const QString &family )
149{
150 mFontFamily = family;
151}
152
157
159{
160 mStrikethrough = strikethrough;
161}
162
167
169{
170 mUnderline = underline;
171}
172
177
179{
180 mOverline = enabled;
181}
182
184{
185 return mImagePath;
186}
187
188void QgsTextCharacterFormat::setImagePath( const QString &path )
189{
190 mImagePath = path;
191}
192
194{
195 return mImageSize;
196}
197
198void QgsTextCharacterFormat::setImageSize( const QSizeF &size )
199{
200 mImageSize = size;
201}
202
203void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const QgsRenderContext &context, const double scaleFactor ) const
204{
205 // important -- MUST set family first
206 if ( !mFontFamily.isEmpty() )
207 QgsFontUtils::setFontFamily( font, mFontFamily );
208
209 if ( mFontPointSize != -1 )
210 font.setPixelSize( scaleFactor * context.convertToPainterUnits( mFontPointSize, Qgis::RenderUnit::Points ) );
211
212 if ( mFontPercentageSize != -1 )
213 font.setPixelSize( font.pixelSize() * mFontPercentageSize );
214
216 font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::SetTrue );
217
218 if ( mFontWeight != - 1 )
219 {
220#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
221 font.setWeight( mFontWeight );
222#else
223 if ( mFontWeight <= 150 )
224 font.setWeight( QFont::Thin );
225 else if ( mFontWeight <= 250 )
226 font.setWeight( QFont::ExtraLight );
227 else if ( mFontWeight <= 350 )
228 font.setWeight( QFont::Light );
229 else if ( mFontWeight <= 450 )
230 font.setWeight( QFont::Normal );
231 else if ( mFontWeight <= 550 )
232 font.setWeight( QFont::Medium );
233 else if ( mFontWeight <= 650 )
234 font.setWeight( QFont::DemiBold );
235 else if ( mFontWeight <= 750 )
236 font.setWeight( QFont::Bold );
237 else if ( mFontWeight <= 850 )
238 font.setWeight( QFont::ExtraBold );
239 else
240 font.setWeight( QFont::Black );
241#endif
242
243 // depending on the font, platform, and the phase of the moon, we need to both set the font weight AND the style name
244 // in order to get correct rendering!
245 font.setStyleName( mStyleName );
246 }
247
248 if ( mUnderline != BooleanValue::NotSet )
249 font.setUnderline( mUnderline == QgsTextCharacterFormat::BooleanValue::SetTrue );
250 if ( mOverline != BooleanValue::NotSet )
251 font.setOverline( mOverline == QgsTextCharacterFormat::BooleanValue::SetTrue );
252 if ( mStrikethrough != QgsTextCharacterFormat::BooleanValue::NotSet )
253 font.setStrikeOut( mStrikethrough == QgsTextCharacterFormat::BooleanValue::SetTrue );
254
255 if ( !std::isnan( mWordSpacing ) )
256 {
257 font.setWordSpacing( scaleFactor * context.convertToPainterUnits( mWordSpacing, Qgis::RenderUnit::Points ) );
258 }
259}
260
262{
263 return mBackgroundPath;
264}
265
267{
268 mBackgroundPath = path;
269}
270
275
277{
278 mItalic = enabled;
279}
280
282{
283 return mFontWeight;
284}
285
287{
288 mFontWeight = fontWeight;
289}
290
292{
293 return mWordSpacing;
294}
295
297{
298 mWordSpacing = spacing;
299}
300
302{
303 return mBackgroundBrush.style() != Qt::NoBrush || !mBackgroundPath.isEmpty();
304}
305
307{
308 return mBackgroundBrush;
309}
310
312{
313 mBackgroundBrush = brush;
314}
TextCharacterVerticalAlignment
Text vertical alignment for characters.
Definition qgis.h:2775
@ 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.
void setImageSize(const QSizeF &size)
Sets the image size, if the format applies to a document image fragment.
QSizeF imageSize() const
Returns the image size, if the format applies to a document image fragment.
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.
QString backgroundImagePath() const
Returns the path for the image to be used for rendering the background of the fragment.
QBrush backgroundBrush() const
Returns the brush used for rendering the background of the fragment.
QString imagePath() const
Returns the path to the image to render, if the format applies to a document image fragment.
QgsTextCharacterFormat()=default
void setStrikeOut(BooleanValue enabled)
Sets whether the format has strikethrough enabled.
void setBackgroundImagePath(const QString &path)
Sets the path for the image to be used for rendering the background of the fragment.
void setOverline(BooleanValue enabled)
Sets whether the format has overline enabled.
void setBackgroundBrush(const QBrush &brush)
Sets the brush used for rendering the background of the fragment.
BooleanValue
Status values for boolean format properties.
@ SetTrue
Property is set and true.
void setImagePath(const QString &path)
Sets the path to the image to render, if the format applies to a document image fragment.
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.
bool hasBackground() const
Returns true if the fragment has a background set.
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:6612
Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign(const QTextCharFormat &format, bool &set)