QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
19#include <QTextCharFormat>
20
21Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign( const QTextCharFormat &format, bool &set )
22{
23 set = format.hasProperty( QTextFormat::TextVerticalAlignment );
24 switch ( format.verticalAlignment() )
25 {
26 case QTextCharFormat::AlignNormal:
28 case QTextCharFormat::AlignSuperScript:
30 case QTextCharFormat::AlignSubScript:
32
33 // not yet supported
34 case QTextCharFormat::AlignMiddle:
35 case QTextCharFormat::AlignTop:
36 case QTextCharFormat::AlignBottom:
37 case QTextCharFormat::AlignBaseline:
38 set = false;
40 }
42}
43
44QgsTextCharacterFormat::QgsTextCharacterFormat( const QTextCharFormat &format )
45 : mTextColor( format.hasProperty( QTextFormat::ForegroundBrush ) ? format.foreground().color() : QColor() )
46 , mFontWeight( format.hasProperty( QTextFormat::FontWeight ) ? format.fontWeight() : -1 )
47 , mStyleName( format.font().styleName() )
48 , mItalic( format.hasProperty( QTextFormat::FontItalic ) ? ( format.fontItalic() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
49 , mFontPointSize( format.hasProperty( QTextFormat::FontPointSize ) ? format.fontPointSize() : - 1 )
50 , mFontFamily( format.hasProperty( QTextFormat::FontFamily ) ? format.fontFamily() : QString() )
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
59{
60 return mTextColor;
61}
62
63void QgsTextCharacterFormat::setTextColor( const QColor &textColor )
64{
65 mTextColor = textColor;
66}
67
69{
70 return mFontPointSize;
71}
72
74{
75 mFontPointSize = size;
76}
77
79{
80 return mFontFamily;
81}
82
83void QgsTextCharacterFormat::setFamily( const QString &family )
84{
85 mFontFamily = family;
86}
87
89{
90 return mStrikethrough;
91}
92
94{
95 mStrikethrough = strikethrough;
96}
97
99{
100 return mUnderline;
101}
102
104{
105 mUnderline = underline;
106}
107
109{
110 return mOverline;
111}
112
114{
115 mOverline = enabled;
116}
117
118void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const QgsRenderContext &context, const double scaleFactor ) const
119{
120 // important -- MUST set family first
121 if ( !mFontFamily.isEmpty() )
122 font.setFamily( mFontFamily );
123
124 if ( mFontPointSize != -1 )
125 font.setPixelSize( scaleFactor * context.convertToPainterUnits( mFontPointSize, Qgis::RenderUnit::Points ) );
126
128 font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::SetTrue );
129
130 if ( mFontWeight != - 1 )
131 {
132#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
133 font.setWeight( mFontWeight );
134#else
135 if ( mFontWeight <= 150 )
136 font.setWeight( QFont::Thin );
137 else if ( mFontWeight <= 250 )
138 font.setWeight( QFont::ExtraLight );
139 else if ( mFontWeight <= 350 )
140 font.setWeight( QFont::Light );
141 else if ( mFontWeight <= 450 )
142 font.setWeight( QFont::Normal );
143 else if ( mFontWeight <= 550 )
144 font.setWeight( QFont::Medium );
145 else if ( mFontWeight <= 650 )
146 font.setWeight( QFont::DemiBold );
147 else if ( mFontWeight <= 750 )
148 font.setWeight( QFont::Bold );
149 else if ( mFontWeight <= 850 )
150 font.setWeight( QFont::ExtraBold );
151 else
152 font.setWeight( QFont::Black );
153#endif
154
155 // depending on the font, platform, and the phase of the moon, we need to both set the font weight AND the style name
156 // in order to get correct rendering!
157 font.setStyleName( mStyleName );
158 }
159
160 if ( mUnderline != BooleanValue::NotSet )
161 font.setUnderline( mUnderline == QgsTextCharacterFormat::BooleanValue::SetTrue );
162 if ( mOverline != BooleanValue::NotSet )
163 font.setOverline( mOverline == QgsTextCharacterFormat::BooleanValue::SetTrue );
164 if ( mStrikethrough != QgsTextCharacterFormat::BooleanValue::NotSet )
165 font.setStrikeOut( mStrikethrough == QgsTextCharacterFormat::BooleanValue::SetTrue );
166}
167
169{
170 return mItalic;
171}
172
174{
175 mItalic = enabled;
176}
177
179{
180 return mFontWeight;
181}
182
184{
185 mFontWeight = fontWeight;
186}
TextCharacterVerticalAlignment
Text vertical alignment for characters.
Definition: qgis.h:1869
@ 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.
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 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.
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:4180
Qgis::TextCharacterVerticalAlignment convertTextCharFormatVAlign(const QTextCharFormat &format, bool &set)