QGIS API Documentation 3.99.0-Master (d270888f95f)
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
18#include "qgsfontutils.h"
19#include "qgsrendercontext.h"
20
21#include <QTextCharFormat>
22
23Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign( const QTextCharFormat &format, bool &set )
24{
25 set = format.hasProperty( QTextFormat::TextVerticalAlignment );
26 switch ( format.verticalAlignment() )
27 {
28 case QTextCharFormat::AlignNormal:
30 case QTextCharFormat::AlignSuperScript:
32 case QTextCharFormat::AlignSubScript:
34
35 // not yet supported
36 case QTextCharFormat::AlignMiddle:
37 case QTextCharFormat::AlignTop:
38 case QTextCharFormat::AlignBottom:
39 case QTextCharFormat::AlignBaseline:
40 set = false;
42 }
44}
45
46QgsTextCharacterFormat::QgsTextCharacterFormat( const QTextCharFormat &format )
47 : mTextColor( format.hasProperty( QTextFormat::ForegroundBrush ) ? format.foreground().color() : QColor() )
48 , mFontWeight( format.hasProperty( QTextFormat::FontWeight ) ? format.fontWeight() : -1 )
49 , mStyleName( format.font().styleName() )
50 , mItalic( format.hasProperty( QTextFormat::FontItalic ) ? ( format.fontItalic() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
51 , mFontPointSize( format.hasProperty( QTextFormat::FontPointSize ) ? format.fontPointSize() : - 1 )
52 , mWordSpacing( format.hasProperty( QTextFormat::FontWordSpacing ) ? format.fontWordSpacing() : std::numeric_limits< double >::quiet_NaN() )
53 , mStrikethrough( format.hasProperty( QTextFormat::FontStrikeOut ) ? ( format.fontStrikeOut() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
54 , mUnderline( format.hasProperty( QTextFormat::FontUnderline ) ? ( format.fontUnderline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
55 , mOverline( format.hasProperty( QTextFormat::FontOverline ) ? ( format.fontOverline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
56 , mBackgroundBrush( format.background() )
57 , mBackgroundPath( format.background().style() == Qt::NoBrush ? format.stringProperty( QTextFormat::BackgroundImageUrl ) : QString() )
58{
59 mVerticalAlign = convertTextCharFormatVAlign( format, mHasVerticalAlignSet );
60
61 if ( format.hasProperty( QTextFormat::FontFamily ) )
62 {
63 mFontFamily = format.fontFamily();
64 }
65 if ( mFontFamily.isEmpty() && format.hasProperty( QTextFormat::FontFamilies ) )
66 {
67 const QStringList families = format.fontFamilies().toStringList();
68 if ( !families.isEmpty() )
69 mFontFamily = families.at( 0 );
70 }
71 if ( format.isImageFormat() )
72 {
73 const QTextImageFormat imageFormat = format.toImageFormat();
74 mImagePath = imageFormat.name();
75 mImageSize = QSizeF( imageFormat.width(), imageFormat.height() );
76 }
77}
78
80{
81 if ( !mTextColor.isValid() && other.mTextColor.isValid() )
82 mTextColor = other.mTextColor;
83 if ( mFontPointSize == -1 && other.mFontPointSize != -1 )
84 mFontPointSize = other.mFontPointSize;
85 if ( mFontPercentageSize == -1 && other.mFontPercentageSize != -1 )
86 mFontPercentageSize = other.mFontPercentageSize;
87 if ( std::isnan( mWordSpacing ) )
88 mWordSpacing = other.mWordSpacing;
89 if ( mFontFamily.isEmpty() && !other.mFontFamily.isEmpty() )
90 mFontFamily = other.mFontFamily;
91 if ( mStrikethrough == BooleanValue::NotSet && other.mStrikethrough != BooleanValue::NotSet )
92 mStrikethrough = other.mStrikethrough;
93 if ( mUnderline == BooleanValue::NotSet && other.mUnderline != BooleanValue::NotSet )
94 mUnderline = other.mUnderline;
95 if ( mOverline == BooleanValue::NotSet && other.mOverline != BooleanValue::NotSet )
96 mOverline = other.mOverline;
97 if ( mItalic == BooleanValue::NotSet && other.mItalic != BooleanValue::NotSet )
98 mItalic = other.mItalic;
99 if ( mFontWeight == -1 && other.mFontWeight != -1 )
100 mFontWeight = other.mFontWeight;
101 if ( mStyleName.isEmpty() && ! other.mStyleName.isEmpty() )
102 mStyleName = other.mStyleName;
103 if ( mHasVerticalAlignSet && other.hasVerticalAlignmentSet() )
104 {
105 mVerticalAlign = other.mVerticalAlign;
106 mHasVerticalAlignSet = true;
107 }
108 if ( mBackgroundBrush.style() == Qt::NoBrush && mBackgroundPath.isEmpty() && other.mBackgroundBrush.style() != Qt::NoBrush )
109 mBackgroundBrush = other.mBackgroundBrush;
110 if ( mBackgroundBrush.style() == Qt::NoBrush && mBackgroundPath.isEmpty() && !other.mBackgroundPath.isEmpty() )
111 mBackgroundPath = other.mBackgroundPath;
112}
113
115{
116 return mTextColor;
117}
118
120{
121 mTextColor = textColor;
122}
123
125{
126 return mFontPointSize;
127}
128
130{
131 mFontPointSize = size;
132}
133
135{
136 return mFontPercentageSize;
137}
138
140{
141 mFontPercentageSize = size;
142}
143
145{
146 return mFontFamily;
147}
148
150{
151 mFontFamily = family;
152}
153
158
160{
161 mStrikethrough = strikethrough;
162}
163
168
173
178
180{
181 mOverline = enabled;
182}
183
185{
186 return mImagePath;
187}
188
189void QgsTextCharacterFormat::setImagePath( const QString &path )
190{
191 mImagePath = path;
192}
193
195{
196 return mImageSize;
197}
198
199void QgsTextCharacterFormat::setImageSize( const QSizeF &size )
200{
201 mImageSize = size;
202}
203
204void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const QgsRenderContext &context, const double scaleFactor ) const
205{
206 // important -- MUST set family first
207 if ( !mFontFamily.isEmpty() )
208 QgsFontUtils::setFontFamily( font, mFontFamily );
209
210 if ( mFontPointSize != -1 )
211 font.setPixelSize( scaleFactor * context.convertToPainterUnits( mFontPointSize, Qgis::RenderUnit::Points ) );
212
213 if ( mFontPercentageSize != -1 )
214 font.setPixelSize( font.pixelSize() * mFontPercentageSize );
215
217 font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::SetTrue );
218
219 if ( mFontWeight != - 1 )
220 {
221 if ( mFontWeight <= 150 )
222 font.setWeight( QFont::Thin );
223 else if ( mFontWeight <= 250 )
224 font.setWeight( QFont::ExtraLight );
225 else if ( mFontWeight <= 350 )
226 font.setWeight( QFont::Light );
227 else if ( mFontWeight <= 450 )
228 font.setWeight( QFont::Normal );
229 else if ( mFontWeight <= 550 )
230 font.setWeight( QFont::Medium );
231 else if ( mFontWeight <= 650 )
232 font.setWeight( QFont::DemiBold );
233 else if ( mFontWeight <= 750 )
234 font.setWeight( QFont::Bold );
235 else if ( mFontWeight <= 850 )
236 font.setWeight( QFont::ExtraBold );
237 else
238 font.setWeight( QFont::Black );
239
240 // depending on the font, platform, and the phase of the moon, we need to both set the font weight AND the style name
241 // in order to get correct rendering!
242 font.setStyleName( mStyleName );
243 }
244
245 if ( mUnderline != BooleanValue::NotSet )
246 font.setUnderline( mUnderline == QgsTextCharacterFormat::BooleanValue::SetTrue );
247 if ( mOverline != BooleanValue::NotSet )
248 font.setOverline( mOverline == QgsTextCharacterFormat::BooleanValue::SetTrue );
249 if ( mStrikethrough != QgsTextCharacterFormat::BooleanValue::NotSet )
250 font.setStrikeOut( mStrikethrough == QgsTextCharacterFormat::BooleanValue::SetTrue );
251
252 if ( !std::isnan( mWordSpacing ) )
253 {
254 font.setWordSpacing( scaleFactor * context.convertToPainterUnits( mWordSpacing, Qgis::RenderUnit::Points ) );
255 }
256}
257
259{
260 return mBackgroundPath;
261}
262
264{
265 mBackgroundPath = path;
266}
267
272
274{
275 mItalic = enabled;
276}
277
279{
280 return mFontWeight;
281}
282
284{
285 mFontWeight = fontWeight;
286}
287
289{
290 return mWordSpacing;
291}
292
294{
295 mWordSpacing = spacing;
296}
297
299{
300 return mBackgroundBrush.style() != Qt::NoBrush || !mBackgroundPath.isEmpty();
301}
302
304{
305 return mBackgroundBrush;
306}
307
309{
310 mBackgroundBrush = brush;
311}
TextCharacterVerticalAlignment
Text vertical alignment for characters.
Definition qgis.h:3035
@ Normal
Adjacent characters are positioned in the standard way for text in the writing system in use.
Definition qgis.h:3036
@ SubScript
Characters are placed below the base line for normal text.
Definition qgis.h:3038
@ SuperScript
Characters are placed above the base line for normal text.
Definition qgis.h:3037
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5260
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).
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.
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:7489
Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign(const QTextCharFormat &format, bool &set)