QGIS API Documentation 3.38.0-Grenoble (exported)
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 , mStrikethrough( format.hasProperty( QTextFormat::FontStrikeOut ) ? ( format.fontStrikeOut() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
52 , mUnderline( format.hasProperty( QTextFormat::FontUnderline ) ? ( format.fontUnderline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
53 , mOverline( format.hasProperty( QTextFormat::FontOverline ) ? ( format.fontOverline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
54{
55 mVerticalAlign = convertTextCharFormatVAlign( format, mHasVerticalAlignSet );
56
57 if ( format.hasProperty( QTextFormat::FontFamily ) )
58 {
59 mFontFamily = format.fontFamily();
60 }
61 if ( mFontFamily.isEmpty() && format.hasProperty( QTextFormat::FontFamilies ) )
62 {
63 const QStringList families = format.fontFamilies().toStringList();
64 if ( !families.isEmpty() )
65 mFontFamily = families.at( 0 );
66 }
67}
68
70{
71 if ( !mTextColor.isValid() && other.mTextColor.isValid() )
72 mTextColor = other.mTextColor;
73 if ( mFontPointSize == -1 && other.mFontPointSize != -1 )
74 mFontPointSize = other.mFontPointSize;
75 if ( mFontFamily.isEmpty() && !other.mFontFamily.isEmpty() )
76 mFontFamily = other.mFontFamily;
77 if ( mStrikethrough == BooleanValue::NotSet && other.mStrikethrough != BooleanValue::NotSet )
78 mStrikethrough = other.mStrikethrough;
79 if ( mUnderline == BooleanValue::NotSet && other.mUnderline != BooleanValue::NotSet )
80 mUnderline = other.mUnderline;
81 if ( mOverline == BooleanValue::NotSet && other.mOverline != BooleanValue::NotSet )
82 mOverline = other.mOverline;
83 if ( mItalic == BooleanValue::NotSet && other.mItalic != BooleanValue::NotSet )
84 mItalic = other.mItalic;
85 if ( mFontWeight == -1 && other.mFontWeight != -1 )
86 mFontWeight = other.mFontWeight;
87 if ( mStyleName.isEmpty() && ! other.mStyleName.isEmpty() )
88 mStyleName = other.mStyleName;
89 if ( mHasVerticalAlignSet && other.hasVerticalAlignmentSet() )
90 {
91 mVerticalAlign = other.mVerticalAlign;
92 mHasVerticalAlignSet = true;
93 }
94}
95
97{
98 return mTextColor;
99}
100
101void QgsTextCharacterFormat::setTextColor( const QColor &textColor )
102{
103 mTextColor = textColor;
104}
105
107{
108 return mFontPointSize;
109}
110
112{
113 mFontPointSize = size;
114}
115
117{
118 return mFontFamily;
119}
120
121void QgsTextCharacterFormat::setFamily( const QString &family )
122{
123 mFontFamily = family;
124}
125
130
132{
133 mStrikethrough = strikethrough;
134}
135
140
142{
143 mUnderline = underline;
144}
145
150
152{
153 mOverline = enabled;
154}
155
156void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const QgsRenderContext &context, const double scaleFactor ) const
157{
158 // important -- MUST set family first
159 if ( !mFontFamily.isEmpty() )
160 QgsFontUtils::setFontFamily( font, mFontFamily );
161
162 if ( mFontPointSize != -1 )
163 font.setPixelSize( scaleFactor * context.convertToPainterUnits( mFontPointSize, Qgis::RenderUnit::Points ) );
164
166 font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::SetTrue );
167
168 if ( mFontWeight != - 1 )
169 {
170#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
171 font.setWeight( mFontWeight );
172#else
173 if ( mFontWeight <= 150 )
174 font.setWeight( QFont::Thin );
175 else if ( mFontWeight <= 250 )
176 font.setWeight( QFont::ExtraLight );
177 else if ( mFontWeight <= 350 )
178 font.setWeight( QFont::Light );
179 else if ( mFontWeight <= 450 )
180 font.setWeight( QFont::Normal );
181 else if ( mFontWeight <= 550 )
182 font.setWeight( QFont::Medium );
183 else if ( mFontWeight <= 650 )
184 font.setWeight( QFont::DemiBold );
185 else if ( mFontWeight <= 750 )
186 font.setWeight( QFont::Bold );
187 else if ( mFontWeight <= 850 )
188 font.setWeight( QFont::ExtraBold );
189 else
190 font.setWeight( QFont::Black );
191#endif
192
193 // depending on the font, platform, and the phase of the moon, we need to both set the font weight AND the style name
194 // in order to get correct rendering!
195 font.setStyleName( mStyleName );
196 }
197
198 if ( mUnderline != BooleanValue::NotSet )
199 font.setUnderline( mUnderline == QgsTextCharacterFormat::BooleanValue::SetTrue );
200 if ( mOverline != BooleanValue::NotSet )
201 font.setOverline( mOverline == QgsTextCharacterFormat::BooleanValue::SetTrue );
202 if ( mStrikethrough != QgsTextCharacterFormat::BooleanValue::NotSet )
203 font.setStrikeOut( mStrikethrough == QgsTextCharacterFormat::BooleanValue::SetTrue );
204}
205
210
212{
213 mItalic = enabled;
214}
215
217{
218 return mFontWeight;
219}
220
222{
223 mFontWeight = fontWeight;
224}
TextCharacterVerticalAlignment
Text vertical alignment for characters.
Definition qgis.h:2546
@ 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
Constructor for QgsTextCharacterFormat.
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 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 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.
#define BUILTIN_UNREACHABLE
Definition qgis.h:6099
Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign(const QTextCharFormat &format, bool &set)