QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgstextformat.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextformat.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 
16 #include "qgstextformat.h"
17 #include "qgstextrenderer_p.h"
18 #include "qgstextrenderer.h"
19 #include "qgsvectorlayer.h"
20 #include "qgsfontutils.h"
21 #include "qgssymbollayerutils.h"
22 #include "qgspainting.h"
23 #include "qgstextrendererutils.h"
24 #include "qgspallabeling.h"
25 #include <QFontDatabase>
26 #include <QDesktopWidget>
27 
29 {
30  d = new QgsTextSettingsPrivate();
31 }
32 
34  : mBufferSettings( other.mBufferSettings )
35  , mBackgroundSettings( other.mBackgroundSettings )
36  , mShadowSettings( other.mShadowSettings )
37  , mMaskSettings( other.mMaskSettings )
38  , mTextFontFamily( other.mTextFontFamily )
39  , mTextFontFound( other.mTextFontFound )
40  , d( other.d )
41 {
42 
43 }
44 
46 {
47  d = other.d;
48  mBufferSettings = other.mBufferSettings;
49  mBackgroundSettings = other.mBackgroundSettings;
50  mShadowSettings = other.mShadowSettings;
51  mMaskSettings = other.mMaskSettings;
52  mTextFontFamily = other.mTextFontFamily;
53  mTextFontFound = other.mTextFontFound;
54  return *this;
55 }
56 
58 {
59 
60 }
61 
62 bool QgsTextFormat::operator==( const QgsTextFormat &other ) const
63 {
64  if ( d->isValid != other.isValid()
65  || d->textFont != other.font()
66  || namedStyle() != other.namedStyle()
67  || d->fontSizeUnits != other.sizeUnit()
68  || d->fontSizeMapUnitScale != other.sizeMapUnitScale()
69  || d->fontSize != other.size()
70  || d->textColor != other.color()
71  || d->opacity != other.opacity()
72  || d->blendMode != other.blendMode()
73  || d->multilineHeight != other.lineHeight()
74  || d->orientation != other.orientation()
75  || d->previewBackgroundColor != other.previewBackgroundColor()
76  || d->allowHtmlFormatting != other.allowHtmlFormatting()
77  || d->capitalization != other.capitalization()
78  || mBufferSettings != other.mBufferSettings
79  || mBackgroundSettings != other.mBackgroundSettings
80  || mShadowSettings != other.mShadowSettings
81  || mMaskSettings != other.mMaskSettings
82  || d->mDataDefinedProperties != other.dataDefinedProperties() )
83  return false;
84 
85  return true;
86 }
87 
88 bool QgsTextFormat::operator!=( const QgsTextFormat &other ) const
89 {
90  return !( *this == other );
91 }
92 
94 {
95  return d->isValid;
96 }
97 
99 {
100  d->isValid = true;
101 }
102 
104 {
105  d->isValid = true;
106  return mBufferSettings;
107 }
108 
109 void QgsTextFormat::setBuffer( const QgsTextBufferSettings &bufferSettings )
110 {
111  d->isValid = true;
112  mBufferSettings = bufferSettings;
113 }
114 
116 {
117  d->isValid = true;
118  return mBackgroundSettings;
119 }
120 
122 {
123  d->isValid = true;
124  mBackgroundSettings = backgroundSettings;
125 }
126 
128 {
129  d->isValid = true;
130  return mShadowSettings;
131 }
132 
133 void QgsTextFormat::setShadow( const QgsTextShadowSettings &shadowSettings )
134 {
135  d->isValid = true;
136  mShadowSettings = shadowSettings;
137 }
138 
140 {
141  d->isValid = true;
142  return mMaskSettings;
143 }
144 
145 void QgsTextFormat::setMask( const QgsTextMaskSettings &maskSettings )
146 {
147  d->isValid = true;
148  mMaskSettings = maskSettings;
149 }
150 
151 QFont QgsTextFormat::font() const
152 {
153  return d->textFont;
154 }
155 
156 QFont QgsTextFormat::scaledFont( const QgsRenderContext &context, double scaleFactor ) const
157 {
158  QFont font = d->textFont;
159  if ( scaleFactor == 1 )
160  {
161  int fontPixelSize = QgsTextRenderer::sizeToPixel( d->fontSize, context, d->fontSizeUnits,
162  d->fontSizeMapUnitScale );
163  font.setPixelSize( fontPixelSize );
164  }
165  else
166  {
167  double fontPixelSize = context.convertToPainterUnits( d->fontSize, d->fontSizeUnits, d->fontSizeMapUnitScale );
168  font.setPixelSize( std::round( scaleFactor * fontPixelSize + 0.5 ) );
169  }
170 
171  font.setLetterSpacing( QFont::AbsoluteSpacing, context.convertToPainterUnits( d->textFont.letterSpacing(), d->fontSizeUnits, d->fontSizeMapUnitScale ) * scaleFactor );
172  font.setWordSpacing( context.convertToPainterUnits( d->textFont.wordSpacing(), d->fontSizeUnits, d->fontSizeMapUnitScale ) * scaleFactor * scaleFactor );
173 
174  return font;
175 }
176 
177 void QgsTextFormat::setFont( const QFont &font )
178 {
179  d->isValid = true;
180  d->textFont = font;
181 }
182 
184 {
185  if ( !d->textNamedStyle.isEmpty() )
186  return d->textNamedStyle;
187 
188  QFontDatabase db;
189  return db.styleString( d->textFont );
190 }
191 
192 void QgsTextFormat::setNamedStyle( const QString &style )
193 {
194  d->isValid = true;
195  QgsFontUtils::updateFontViaStyle( d->textFont, style );
196  d->textNamedStyle = style;
197 }
198 
200 {
201  return d->fontSizeUnits;
202 }
203 
205 {
206  d->isValid = true;
207  d->fontSizeUnits = unit;
208 }
209 
211 {
212  return d->fontSizeMapUnitScale;
213 }
214 
216 {
217  d->isValid = true;
218  d->fontSizeMapUnitScale = scale;
219 }
220 
221 double QgsTextFormat::size() const
222 {
223  return d->fontSize;
224 }
225 
226 void QgsTextFormat::setSize( double size )
227 {
228  d->isValid = true;
229  d->fontSize = size;
230 }
231 
232 QColor QgsTextFormat::color() const
233 {
234  return d->textColor;
235 }
236 
237 void QgsTextFormat::setColor( const QColor &color )
238 {
239  d->isValid = true;
240  d->textColor = color;
241 }
242 
244 {
245  return d->opacity;
246 }
247 
248 void QgsTextFormat::setOpacity( double opacity )
249 {
250  d->isValid = true;
251  d->opacity = opacity;
252 }
253 
254 QPainter::CompositionMode QgsTextFormat::blendMode() const
255 {
256  return d->blendMode;
257 }
258 
259 void QgsTextFormat::setBlendMode( QPainter::CompositionMode mode )
260 {
261  d->isValid = true;
262  d->blendMode = mode;
263 }
264 
266 {
267  return d->multilineHeight;
268 }
269 
270 void QgsTextFormat::setLineHeight( double height )
271 {
272  d->isValid = true;
273  d->multilineHeight = height;
274 }
275 
277 {
278  return d->orientation;
279 }
280 
282 {
283  d->isValid = true;
284  d->orientation = orientation;
285 }
286 
288 {
289  // bit of complexity here to maintain API..
290  return d->capitalization == QgsStringUtils::MixedCase && d->textFont.capitalization() != QFont::MixedCase ? static_cast< QgsStringUtils::Capitalization >( d->textFont.capitalization() ) : d->capitalization ;
291 }
292 
294 {
295  d->isValid = true;
296  d->capitalization = capitalization;
297  d->textFont.setCapitalization( QFont::MixedCase );
298 }
299 
301 {
302  return d->allowHtmlFormatting;
303 }
304 
306 {
307  d->isValid = true;
308  d->allowHtmlFormatting = allow;
309 }
310 
312 {
313  return d->previewBackgroundColor;
314 }
315 
316 void QgsTextFormat::setPreviewBackgroundColor( const QColor &color )
317 {
318  d->isValid = true;
319  d->previewBackgroundColor = color;
320 }
321 
323 {
324  d->isValid = true;
325  QFont appFont = QApplication::font();
326  mTextFontFamily = layer->customProperty( QStringLiteral( "labeling/fontFamily" ), QVariant( appFont.family() ) ).toString();
327  QString fontFamily = mTextFontFamily;
328  if ( mTextFontFamily != appFont.family() && !QgsFontUtils::fontFamilyMatchOnSystem( mTextFontFamily ) )
329  {
330  // trigger to notify about font family substitution
331  mTextFontFound = false;
332 
333  // TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented
334  // currently only defaults to matching algorithm for resolving [foundry], if a font of similar family is found (default for QFont)
335 
336  // for now, do not use matching algorithm for substitution if family not found, substitute default instead
337  fontFamily = appFont.family();
338  }
339  else
340  {
341  mTextFontFound = true;
342  }
343 
344  if ( !layer->customProperty( QStringLiteral( "labeling/fontSize" ) ).isValid() )
345  {
346  d->fontSize = appFont.pointSizeF();
347  }
348  else
349  {
350  d->fontSize = layer->customProperty( QStringLiteral( "labeling/fontSize" ) ).toDouble();
351  }
352 
353  if ( layer->customProperty( QStringLiteral( "labeling/fontSizeUnit" ) ).toString().isEmpty() )
354  {
355  d->fontSizeUnits = layer->customProperty( QStringLiteral( "labeling/fontSizeInMapUnits" ), QVariant( false ) ).toBool() ?
357  }
358  else
359  {
360  bool ok = false;
361  d->fontSizeUnits = QgsUnitTypes::decodeRenderUnit( layer->customProperty( QStringLiteral( "labeling/fontSizeUnit" ) ).toString(), &ok );
362  if ( !ok )
363  d->fontSizeUnits = QgsUnitTypes::RenderPoints;
364  }
365  if ( layer->customProperty( QStringLiteral( "labeling/fontSizeMapUnitScale" ) ).toString().isEmpty() )
366  {
367  //fallback to older property
368  double oldMin = layer->customProperty( QStringLiteral( "labeling/fontSizeMapUnitMinScale" ), 0.0 ).toDouble();
369  d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
370  double oldMax = layer->customProperty( QStringLiteral( "labeling/fontSizeMapUnitMaxScale" ), 0.0 ).toDouble();
371  d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
372  }
373  else
374  {
375  d->fontSizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( layer->customProperty( QStringLiteral( "labeling/fontSizeMapUnitScale" ) ).toString() );
376  }
377  int fontWeight = layer->customProperty( QStringLiteral( "labeling/fontWeight" ) ).toInt();
378  bool fontItalic = layer->customProperty( QStringLiteral( "labeling/fontItalic" ) ).toBool();
379  d->textFont = QFont( fontFamily, d->fontSize, fontWeight, fontItalic );
380  d->textNamedStyle = QgsFontUtils::translateNamedStyle( layer->customProperty( QStringLiteral( "labeling/namedStyle" ), QVariant( "" ) ).toString() );
381  QgsFontUtils::updateFontViaStyle( d->textFont, d->textNamedStyle ); // must come after textFont.setPointSizeF()
382  d->capitalization = static_cast< QgsStringUtils::Capitalization >( layer->customProperty( QStringLiteral( "labeling/fontCapitals" ), QVariant( 0 ) ).toUInt() );
383  d->textFont.setUnderline( layer->customProperty( QStringLiteral( "labeling/fontUnderline" ) ).toBool() );
384  d->textFont.setStrikeOut( layer->customProperty( QStringLiteral( "labeling/fontStrikeout" ) ).toBool() );
385  d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, layer->customProperty( QStringLiteral( "labeling/fontLetterSpacing" ), QVariant( 0.0 ) ).toDouble() );
386  d->textFont.setWordSpacing( layer->customProperty( QStringLiteral( "labeling/fontWordSpacing" ), QVariant( 0.0 ) ).toDouble() );
387  d->textColor = QgsTextRendererUtils::readColor( layer, QStringLiteral( "labeling/textColor" ), Qt::black, false );
388  if ( layer->customProperty( QStringLiteral( "labeling/textOpacity" ) ).toString().isEmpty() )
389  {
390  d->opacity = ( 1 - layer->customProperty( QStringLiteral( "labeling/textTransp" ) ).toInt() / 100.0 ); //0 -100
391  }
392  else
393  {
394  d->opacity = ( layer->customProperty( QStringLiteral( "labeling/textOpacity" ) ).toDouble() );
395  }
396  d->blendMode = QgsPainting::getCompositionMode(
397  static_cast< QgsPainting::BlendMode >( layer->customProperty( QStringLiteral( "labeling/blendMode" ), QVariant( QgsPainting::BlendNormal ) ).toUInt() ) );
398  d->multilineHeight = layer->customProperty( QStringLiteral( "labeling/multilineHeight" ), QVariant( 1.0 ) ).toDouble();
399  d->previewBackgroundColor = QgsTextRendererUtils::readColor( layer, QStringLiteral( "labeling/previewBkgrdColor" ), QColor( 255, 255, 255 ), false );
400 
401  mBufferSettings.readFromLayer( layer );
402  mShadowSettings.readFromLayer( layer );
403  mBackgroundSettings.readFromLayer( layer );
404 }
405 
406 void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
407 {
408  d->isValid = true;
409  QDomElement textStyleElem;
410  if ( elem.nodeName() == QLatin1String( "text-style" ) )
411  textStyleElem = elem;
412  else
413  textStyleElem = elem.firstChildElement( QStringLiteral( "text-style" ) );
414  QFont appFont = QApplication::font();
415  mTextFontFamily = textStyleElem.attribute( QStringLiteral( "fontFamily" ), appFont.family() );
416  QString fontFamily = mTextFontFamily;
417  if ( mTextFontFamily != appFont.family() && !QgsFontUtils::fontFamilyMatchOnSystem( mTextFontFamily ) )
418  {
419  // trigger to notify user about font family substitution (signal emitted in QgsVectorLayer::prepareLabelingAndDiagrams)
420  mTextFontFound = false;
421 
422  // TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented
423  // currently only defaults to matching algorithm for resolving [foundry], if a font of similar family is found (default for QFont)
424 
425  // for now, do not use matching algorithm for substitution if family not found, substitute default instead
426  fontFamily = appFont.family();
427  }
428  else
429  {
430  mTextFontFound = true;
431  }
432 
433  if ( textStyleElem.hasAttribute( QStringLiteral( "fontSize" ) ) )
434  {
435  d->fontSize = textStyleElem.attribute( QStringLiteral( "fontSize" ) ).toDouble();
436  }
437  else
438  {
439  d->fontSize = appFont.pointSizeF();
440  }
441 
442  if ( !textStyleElem.hasAttribute( QStringLiteral( "fontSizeUnit" ) ) )
443  {
444  d->fontSizeUnits = textStyleElem.attribute( QStringLiteral( "fontSizeInMapUnits" ) ).toUInt() == 0 ? QgsUnitTypes::RenderPoints
446  }
447  else
448  {
449  d->fontSizeUnits = QgsUnitTypes::decodeRenderUnit( textStyleElem.attribute( QStringLiteral( "fontSizeUnit" ) ) );
450  }
451 
452  if ( !textStyleElem.hasAttribute( QStringLiteral( "fontSizeMapUnitScale" ) ) )
453  {
454  //fallback to older property
455  double oldMin = textStyleElem.attribute( QStringLiteral( "fontSizeMapUnitMinScale" ), QStringLiteral( "0" ) ).toDouble();
456  d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
457  double oldMax = textStyleElem.attribute( QStringLiteral( "fontSizeMapUnitMaxScale" ), QStringLiteral( "0" ) ).toDouble();
458  d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
459  }
460  else
461  {
462  d->fontSizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textStyleElem.attribute( QStringLiteral( "fontSizeMapUnitScale" ) ) );
463  }
464  int fontWeight = textStyleElem.attribute( QStringLiteral( "fontWeight" ) ).toInt();
465  bool fontItalic = textStyleElem.attribute( QStringLiteral( "fontItalic" ) ).toInt();
466  d->textFont = QFont( fontFamily, d->fontSize, fontWeight, fontItalic );
467  d->textFont.setPointSizeF( d->fontSize ); //double precision needed because of map units
468  d->textNamedStyle = QgsFontUtils::translateNamedStyle( textStyleElem.attribute( QStringLiteral( "namedStyle" ) ) );
469  QgsFontUtils::updateFontViaStyle( d->textFont, d->textNamedStyle ); // must come after textFont.setPointSizeF()
470  d->textFont.setUnderline( textStyleElem.attribute( QStringLiteral( "fontUnderline" ) ).toInt() );
471  d->textFont.setStrikeOut( textStyleElem.attribute( QStringLiteral( "fontStrikeout" ) ).toInt() );
472  d->textFont.setKerning( textStyleElem.attribute( QStringLiteral( "fontKerning" ), QStringLiteral( "1" ) ).toInt() );
473  d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, textStyleElem.attribute( QStringLiteral( "fontLetterSpacing" ), QStringLiteral( "0" ) ).toDouble() );
474  d->textFont.setWordSpacing( textStyleElem.attribute( QStringLiteral( "fontWordSpacing" ), QStringLiteral( "0" ) ).toDouble() );
475  d->textColor = QgsSymbolLayerUtils::decodeColor( textStyleElem.attribute( QStringLiteral( "textColor" ), QgsSymbolLayerUtils::encodeColor( Qt::black ) ) );
476  if ( !textStyleElem.hasAttribute( QStringLiteral( "textOpacity" ) ) )
477  {
478  d->opacity = ( 1 - textStyleElem.attribute( QStringLiteral( "textTransp" ) ).toInt() / 100.0 ); //0 -100
479  }
480  else
481  {
482  d->opacity = ( textStyleElem.attribute( QStringLiteral( "textOpacity" ) ).toDouble() );
483  }
484  d->orientation = QgsTextRendererUtils::decodeTextOrientation( textStyleElem.attribute( QStringLiteral( "textOrientation" ) ) );
485  d->previewBackgroundColor = QgsSymbolLayerUtils::decodeColor( textStyleElem.attribute( QStringLiteral( "previewBkgrdColor" ), QgsSymbolLayerUtils::encodeColor( Qt::white ) ) );
486 
487  d->blendMode = QgsPainting::getCompositionMode(
488  static_cast< QgsPainting::BlendMode >( textStyleElem.attribute( QStringLiteral( "blendMode" ), QString::number( QgsPainting::BlendNormal ) ).toUInt() ) );
489 
490  if ( !textStyleElem.hasAttribute( QStringLiteral( "multilineHeight" ) ) )
491  {
492  QDomElement textFormatElem = elem.firstChildElement( QStringLiteral( "text-format" ) );
493  d->multilineHeight = textFormatElem.attribute( QStringLiteral( "multilineHeight" ), QStringLiteral( "1" ) ).toDouble();
494  }
495  else
496  {
497  d->multilineHeight = textStyleElem.attribute( QStringLiteral( "multilineHeight" ), QStringLiteral( "1" ) ).toDouble();
498  }
499 
500  if ( textStyleElem.hasAttribute( QStringLiteral( "capitalization" ) ) )
501  d->capitalization = static_cast< QgsStringUtils::Capitalization >( textStyleElem.attribute( QStringLiteral( "capitalization" ), QString::number( QgsStringUtils::MixedCase ) ).toInt() );
502  else
503  d->capitalization = static_cast< QgsStringUtils::Capitalization >( textStyleElem.attribute( QStringLiteral( "fontCapitals" ), QStringLiteral( "0" ) ).toUInt() );
504 
505  d->allowHtmlFormatting = textStyleElem.attribute( QStringLiteral( "allowHtml" ), QStringLiteral( "0" ) ).toInt();
506 
507  if ( textStyleElem.firstChildElement( QStringLiteral( "text-buffer" ) ).isNull() )
508  {
509  mBufferSettings.readXml( elem );
510  }
511  else
512  {
513  mBufferSettings.readXml( textStyleElem );
514  }
515  if ( textStyleElem.firstChildElement( QStringLiteral( "text-mask" ) ).isNull() )
516  {
517  mMaskSettings.readXml( elem );
518  }
519  else
520  {
521  mMaskSettings.readXml( textStyleElem );
522  }
523  if ( textStyleElem.firstChildElement( QStringLiteral( "shadow" ) ).isNull() )
524  {
525  mShadowSettings.readXml( elem );
526  }
527  else
528  {
529  mShadowSettings.readXml( textStyleElem );
530  }
531  if ( textStyleElem.firstChildElement( QStringLiteral( "background" ) ).isNull() )
532  {
533  mBackgroundSettings.readXml( elem, context );
534  }
535  else
536  {
537  mBackgroundSettings.readXml( textStyleElem, context );
538  }
539 
540  QDomElement ddElem = textStyleElem.firstChildElement( QStringLiteral( "dd_properties" ) );
541  if ( ddElem.isNull() )
542  {
543  ddElem = elem.firstChildElement( QStringLiteral( "dd_properties" ) );
544  }
545  if ( !ddElem.isNull() )
546  {
547  d->mDataDefinedProperties.readXml( ddElem, QgsPalLayerSettings::propertyDefinitions() );
548  }
549  else
550  {
551  d->mDataDefinedProperties.clear();
552  }
553 }
554 
555 QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
556 {
557  // text style
558  QDomElement textStyleElem = doc.createElement( QStringLiteral( "text-style" ) );
559  textStyleElem.setAttribute( QStringLiteral( "fontFamily" ), d->textFont.family() );
560  textStyleElem.setAttribute( QStringLiteral( "namedStyle" ), QgsFontUtils::untranslateNamedStyle( d->textNamedStyle ) );
561  textStyleElem.setAttribute( QStringLiteral( "fontSize" ), d->fontSize );
562  textStyleElem.setAttribute( QStringLiteral( "fontSizeUnit" ), QgsUnitTypes::encodeUnit( d->fontSizeUnits ) );
563  textStyleElem.setAttribute( QStringLiteral( "fontSizeMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( d->fontSizeMapUnitScale ) );
564  textStyleElem.setAttribute( QStringLiteral( "fontWeight" ), d->textFont.weight() );
565  textStyleElem.setAttribute( QStringLiteral( "fontItalic" ), d->textFont.italic() );
566  textStyleElem.setAttribute( QStringLiteral( "fontStrikeout" ), d->textFont.strikeOut() );
567  textStyleElem.setAttribute( QStringLiteral( "fontUnderline" ), d->textFont.underline() );
568  textStyleElem.setAttribute( QStringLiteral( "textColor" ), QgsSymbolLayerUtils::encodeColor( d->textColor ) );
569  textStyleElem.setAttribute( QStringLiteral( "previewBkgrdColor" ), QgsSymbolLayerUtils::encodeColor( d->previewBackgroundColor ) );
570  textStyleElem.setAttribute( QStringLiteral( "fontLetterSpacing" ), d->textFont.letterSpacing() );
571  textStyleElem.setAttribute( QStringLiteral( "fontWordSpacing" ), d->textFont.wordSpacing() );
572  textStyleElem.setAttribute( QStringLiteral( "fontKerning" ), d->textFont.kerning() );
573  textStyleElem.setAttribute( QStringLiteral( "textOpacity" ), d->opacity );
574  textStyleElem.setAttribute( QStringLiteral( "textOrientation" ), QgsTextRendererUtils::encodeTextOrientation( d->orientation ) );
575  textStyleElem.setAttribute( QStringLiteral( "blendMode" ), QgsPainting::getBlendModeEnum( d->blendMode ) );
576  textStyleElem.setAttribute( QStringLiteral( "multilineHeight" ), d->multilineHeight );
577  textStyleElem.setAttribute( QStringLiteral( "allowHtml" ), d->allowHtmlFormatting ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
578  textStyleElem.setAttribute( QStringLiteral( "capitalization" ), QString::number( static_cast< int >( d->capitalization ) ) );
579 
580  QDomElement ddElem = doc.createElement( QStringLiteral( "dd_properties" ) );
581  d->mDataDefinedProperties.writeXml( ddElem, QgsPalLayerSettings::propertyDefinitions() );
582 
583  textStyleElem.appendChild( mBufferSettings.writeXml( doc ) );
584  textStyleElem.appendChild( mMaskSettings.writeXml( doc ) );
585  textStyleElem.appendChild( mBackgroundSettings.writeXml( doc, context ) );
586  textStyleElem.appendChild( mShadowSettings.writeXml( doc ) );
587  textStyleElem.appendChild( ddElem );
588 
589  return textStyleElem;
590 }
591 
592 QMimeData *QgsTextFormat::toMimeData() const
593 {
594  //set both the mime color data, and the text (format settings).
595  QMimeData *mimeData = new QMimeData;
596  mimeData->setColorData( QVariant( color() ) );
597 
598  QgsReadWriteContext rwContext;
599  QDomDocument textDoc;
600  QDomElement textElem = writeXml( textDoc, rwContext );
601  textDoc.appendChild( textElem );
602  mimeData->setText( textDoc.toString() );
603 
604  return mimeData;
605 }
606 
608 {
609  QgsTextFormat format;
610  format.setFont( font );
611  if ( font.pointSizeF() > 0 )
612  {
613  format.setSize( font.pointSizeF() );
615  }
616  else if ( font.pixelSize() > 0 )
617  {
618  format.setSize( font.pixelSize() );
620  }
621 
622  return format;
623 }
624 
626 {
627  QFont f = font();
628  switch ( sizeUnit() )
629  {
631  f.setPointSizeF( size() );
632  break;
633 
635  f.setPointSizeF( size() * 2.83464567 );
636  break;
637 
639  f.setPointSizeF( size() * 72 );
640  break;
641 
643  f.setPixelSize( static_cast< int >( std::round( size() ) ) );
644  break;
645 
650  // no meaning here
651  break;
652  }
653  return f;
654 }
655 
656 QgsTextFormat QgsTextFormat::fromMimeData( const QMimeData *data, bool *ok )
657 {
658  if ( ok )
659  *ok = false;
660  QgsTextFormat format;
661  if ( !data )
662  return format;
663 
664  QString text = data->text();
665  if ( !text.isEmpty() )
666  {
667  QDomDocument doc;
668  QDomElement elem;
669  QgsReadWriteContext rwContext;
670 
671  if ( doc.setContent( text ) )
672  {
673  elem = doc.documentElement();
674 
675  format.readXml( elem, rwContext );
676  if ( ok )
677  *ok = true;
678  return format;
679  }
680  }
681  return format;
682 }
683 
685 {
686  if ( d->blendMode != QPainter::CompositionMode_SourceOver )
687  return true;
688 
689  if ( mBufferSettings.enabled() && mBufferSettings.blendMode() != QPainter::CompositionMode_SourceOver )
690  return true;
691 
692  if ( mBackgroundSettings.enabled() && mBackgroundSettings.blendMode() != QPainter::CompositionMode_SourceOver )
693  return true;
694 
695  if ( mShadowSettings.enabled() && mShadowSettings.blendMode() != QPainter::CompositionMode_SourceOver )
696  return true;
697 
698  return false;
699 }
700 
702 {
703  d->isValid = true;
704  return d->mDataDefinedProperties;
705 }
706 
708 {
709  return d->mDataDefinedProperties;
710 }
711 
712 QSet<QString> QgsTextFormat::referencedFields( const QgsRenderContext &context ) const
713 {
714  QSet< QString > fields = d->mDataDefinedProperties.referencedFields( context.expressionContext(), true );
715  fields.unite( mBufferSettings.referencedFields( context ) );
716  fields.unite( mBackgroundSettings.referencedFields( context ) );
717  fields.unite( mShadowSettings.referencedFields( context ) );
718  fields.unite( mMaskSettings.referencedFields( context ) );
719  return fields;
720 }
721 
723 {
724  d->isValid = true;
725  d->mDataDefinedProperties = collection;
726 }
727 
729 {
730  d->isValid = true;
731  if ( !d->mDataDefinedProperties.hasActiveProperties() )
732  return;
733 
734  QString ddFontFamily;
735  context.expressionContext().setOriginalValueVariable( d->textFont.family() );
736  QVariant exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::Family, context.expressionContext() );
737  if ( exprVal.isValid() )
738  {
739  QString family = exprVal.toString().trimmed();
740  if ( d->textFont.family() != family )
741  {
742  // testing for ddFontFamily in QFontDatabase.families() may be slow to do for every feature
743  // (i.e. don't use QgsFontUtils::fontFamilyMatchOnSystem( family ) here)
744  if ( QgsFontUtils::fontFamilyOnSystem( family ) )
745  {
746  ddFontFamily = family;
747  }
748  }
749  }
750 
751  // data defined named font style?
752  QString ddFontStyle;
753  context.expressionContext().setOriginalValueVariable( d->textNamedStyle );
754  exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::FontStyle, context.expressionContext() );
755  if ( exprVal.isValid() )
756  {
757  QString fontstyle = exprVal.toString().trimmed();
758  ddFontStyle = fontstyle;
759  }
760 
761  bool ddBold = false;
762  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Bold ) )
763  {
764  context.expressionContext().setOriginalValueVariable( d->textFont.bold() );
765  ddBold = d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Bold, context.expressionContext(), false ) ;
766  }
767 
768  bool ddItalic = false;
769  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Italic ) )
770  {
771  context.expressionContext().setOriginalValueVariable( d->textFont.italic() );
772  ddItalic = d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Italic, context.expressionContext(), false );
773  }
774 
775  // TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented
776  // (currently defaults to what has been read in from layer settings)
777  QFont newFont;
778  QFontDatabase fontDb;
779  QFont appFont = QApplication::font();
780  bool newFontBuilt = false;
781  if ( ddBold || ddItalic )
782  {
783  // new font needs built, since existing style needs removed
784  newFont = QFont( !ddFontFamily.isEmpty() ? ddFontFamily : d->textFont.family() );
785  newFontBuilt = true;
786  newFont.setBold( ddBold );
787  newFont.setItalic( ddItalic );
788  }
789  else if ( !ddFontStyle.isEmpty()
790  && ddFontStyle.compare( QLatin1String( "Ignore" ), Qt::CaseInsensitive ) != 0 )
791  {
792  if ( !ddFontFamily.isEmpty() )
793  {
794  // both family and style are different, build font from database
795  QFont styledfont = fontDb.font( ddFontFamily, ddFontStyle, appFont.pointSize() );
796  if ( appFont != styledfont )
797  {
798  newFont = styledfont;
799  newFontBuilt = true;
800  }
801  }
802 
803  // update the font face style
804  QgsFontUtils::updateFontViaStyle( newFontBuilt ? newFont : d->textFont, ddFontStyle );
805  }
806  else if ( !ddFontFamily.isEmpty() )
807  {
808  if ( ddFontStyle.compare( QLatin1String( "Ignore" ), Qt::CaseInsensitive ) != 0 )
809  {
810  // just family is different, build font from database
811  QFont styledfont = fontDb.font( ddFontFamily, d->textNamedStyle, appFont.pointSize() );
812  if ( appFont != styledfont )
813  {
814  newFont = styledfont;
815  newFontBuilt = true;
816  }
817  }
818  else
819  {
820  newFont = QFont( ddFontFamily );
821  newFontBuilt = true;
822  }
823  }
824 
825  if ( newFontBuilt )
826  {
827  // copy over existing font settings
828  newFont.setUnderline( d->textFont.underline() );
829  newFont.setStrikeOut( d->textFont.strikeOut() );
830  newFont.setWordSpacing( d->textFont.wordSpacing() );
831  newFont.setLetterSpacing( QFont::AbsoluteSpacing, d->textFont.letterSpacing() );
832  d->textFont = newFont;
833  }
834 
835  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Underline ) )
836  {
837  context.expressionContext().setOriginalValueVariable( d->textFont.underline() );
838  d->textFont.setUnderline( d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Underline, context.expressionContext(), d->textFont.underline() ) );
839  }
840 
841  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Strikeout ) )
842  {
843  context.expressionContext().setOriginalValueVariable( d->textFont.strikeOut() );
844  d->textFont.setStrikeOut( d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Strikeout, context.expressionContext(), d->textFont.strikeOut() ) );
845  }
846 
847  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Color ) )
848  {
850  d->textColor = d->mDataDefinedProperties.valueAsColor( QgsPalLayerSettings::Color, context.expressionContext(), d->textColor );
851  }
852 
853  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Size ) )
854  {
856  d->fontSize = d->mDataDefinedProperties.valueAsDouble( QgsPalLayerSettings::Size, context.expressionContext(), d->fontSize );
857  }
858 
859  exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::FontSizeUnit, context.expressionContext() );
860  if ( exprVal.isValid() )
861  {
862  QString units = exprVal.toString();
863  if ( !units.isEmpty() )
864  {
865  bool ok;
867  if ( ok )
868  d->fontSizeUnits = res;
869  }
870  }
871 
872  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::FontOpacity ) )
873  {
874  context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
875  d->opacity = d->mDataDefinedProperties.value( QgsPalLayerSettings::FontOpacity, context.expressionContext(), d->opacity * 100 ).toDouble() / 100.0;
876  }
877 
878  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::TextOrientation ) )
879  {
880  const QString encoded = QgsTextRendererUtils::encodeTextOrientation( d->orientation );
881  context.expressionContext().setOriginalValueVariable( encoded );
882  d->orientation = QgsTextRendererUtils::decodeTextOrientation( d->mDataDefinedProperties.value( QgsPalLayerSettings::TextOrientation, context.expressionContext(), encoded ).toString() );
883  }
884 
885  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::FontLetterSpacing ) )
886  {
887  context.expressionContext().setOriginalValueVariable( d->textFont.letterSpacing() );
888  d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, d->mDataDefinedProperties.value( QgsPalLayerSettings::FontLetterSpacing, context.expressionContext(), d->textFont.letterSpacing() ).toDouble() );
889  }
890 
891  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::FontWordSpacing ) )
892  {
893  context.expressionContext().setOriginalValueVariable( d->textFont.wordSpacing() );
894  d->textFont.setWordSpacing( d->mDataDefinedProperties.value( QgsPalLayerSettings::FontWordSpacing, context.expressionContext(), d->textFont.wordSpacing() ).toDouble() );
895  }
896 
897  if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::FontBlendMode ) )
898  {
899  exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::FontBlendMode, context.expressionContext() );
900  QString blendstr = exprVal.toString().trimmed();
901  if ( !blendstr.isEmpty() )
902  d->blendMode = QgsSymbolLayerUtils::decodeBlendMode( blendstr );
903  }
904 
905  mShadowSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
906  mBackgroundSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
907  mBufferSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
908  mMaskSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
909 }
910 
911 QPixmap QgsTextFormat::textFormatPreviewPixmap( const QgsTextFormat &format, QSize size, const QString &previewText, int padding )
912 {
913  QgsTextFormat tempFormat = format;
914  QPixmap pixmap( size );
915  pixmap.fill( Qt::transparent );
916  QPainter painter;
917  painter.begin( &pixmap );
918 
919  painter.setRenderHint( QPainter::Antialiasing );
920 
921  QRect rect( 0, 0, size.width(), size.height() );
922 
923  // shameless eye candy - use a subtle gradient when drawing background
924  painter.setPen( Qt::NoPen );
925  QColor background1 = tempFormat.previewBackgroundColor();
926  if ( ( background1.lightnessF() < 0.7 ) )
927  {
928  background1 = background1.darker( 125 );
929  }
930  else
931  {
932  background1 = background1.lighter( 125 );
933  }
934  QColor background2 = tempFormat.previewBackgroundColor();
935  QLinearGradient linearGrad( QPointF( 0, 0 ), QPointF( 0, rect.height() ) );
936  linearGrad.setColorAt( 0, background1 );
937  linearGrad.setColorAt( 1, background2 );
938  painter.setBrush( QBrush( linearGrad ) );
939  if ( size.width() > 30 )
940  {
941  painter.drawRoundedRect( rect, 6, 6 );
942  }
943  else
944  {
945  // don't use rounded rect for small previews
946  painter.drawRect( rect );
947  }
948  painter.setBrush( Qt::NoBrush );
949  painter.setPen( Qt::NoPen );
950  padding += 1; // move text away from background border
951 
952  QgsRenderContext context;
953  QgsMapToPixel newCoordXForm;
954  newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 );
955  context.setMapToPixel( newCoordXForm );
956 
957  context.setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
958  context.setUseAdvancedEffects( true );
959  context.setFlag( QgsRenderContext::Antialiasing, true );
960  context.setPainter( &painter );
961  context.setFlag( QgsRenderContext::Antialiasing, true );
962 
963  // slightly inset text to account for buffer/background
964  double xtrans = 0;
965  if ( tempFormat.buffer().enabled() )
966  xtrans = context.convertToPainterUnits( tempFormat.buffer().size(), tempFormat.buffer().sizeUnit(), tempFormat.buffer().sizeMapUnitScale() );
967  if ( tempFormat.background().enabled() && tempFormat.background().sizeType() != QgsTextBackgroundSettings::SizeFixed )
968  xtrans = std::max( xtrans, context.convertToPainterUnits( tempFormat.background().size().width(), tempFormat.background().sizeUnit(), tempFormat.background().sizeMapUnitScale() ) );
969 
970  double ytrans = 0.0;
971  if ( tempFormat.buffer().enabled() )
972  ytrans = std::max( ytrans, context.convertToPainterUnits( tempFormat.buffer().size(), tempFormat.buffer().sizeUnit(), tempFormat.buffer().sizeMapUnitScale() ) );
973  if ( tempFormat.background().enabled() )
974  ytrans = std::max( ytrans, context.convertToPainterUnits( tempFormat.background().size().height(), tempFormat.background().sizeUnit(), tempFormat.background().sizeMapUnitScale() ) );
975 
976  const QStringList text = QStringList() << ( previewText.isEmpty() ? QObject::tr( "Aa" ) : previewText );
977  const double textHeight = QgsTextRenderer::textHeight( context, tempFormat, text, QgsTextRenderer::Rect );
978  QRectF textRect = rect;
979  textRect.setLeft( xtrans + padding );
980  textRect.setWidth( rect.width() - xtrans - 2 * padding );
981 
982  if ( textRect.width() > 2000 )
983  textRect.setWidth( 2000 - 2 * padding );
984 
985  const double bottom = textRect.height() / 2 + textHeight / 2;
986  textRect.setTop( bottom - textHeight );
987  textRect.setBottom( bottom );
988 
989  QgsTextRenderer::drawText( textRect, 0, QgsTextRenderer::AlignCenter, text, context, tempFormat );
990 
991  // draw border on top of text
992  painter.setBrush( Qt::NoBrush );
993  painter.setPen( QPen( tempFormat.previewBackgroundColor().darker( 150 ), 0 ) );
994  if ( size.width() > 30 )
995  {
996  painter.drawRoundedRect( rect, 6, 6 );
997  }
998  else
999  {
1000  // don't use rounded rect for small previews
1001  painter.drawRect( rect );
1002  }
1003  painter.end();
1004  return pixmap;
1005 }
QgsPainting::BlendNormal
@ BlendNormal
Definition: qgspainting.h:38
QgsUnitTypes::RenderInches
@ RenderInches
Inches.
Definition: qgsunittypes.h:173
QgsTextShadowSettings::readFromLayer
void readFromLayer(QgsVectorLayer *layer)
Reads settings from a layer's custom properties (for QGIS 2.x projects).
Definition: qgstextshadowsettings.cpp:223
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:52
qgspallabeling.h
QgsSymbolLayerUtils::decodeBlendMode
static QPainter::CompositionMode decodeBlendMode(const QString &s)
Definition: qgssymbollayerutils.cpp:745
QgsTextFormat::namedStyle
QString namedStyle() const
Returns the named style for the font used for rendering text (e.g., "bold").
Definition: qgstextformat.cpp:183
QgsRenderContext::convertToPainterUnits
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
Definition: qgsrendercontext.cpp:318
QgsTextFormat::scaledFont
QFont scaledFont(const QgsRenderContext &context, double scaleFactor=1.0) const
Returns a font with the size scaled to match the format's size settings (including units and map unit...
Definition: qgstextformat.cpp:156
QgsTextFormat::setFont
void setFont(const QFont &font)
Sets the font used for rendering text.
Definition: qgstextformat.cpp:177
QgsTextBufferSettings::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
Definition: qgstextbuffersettings.cpp:102
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsRenderContext::expressionContext
QgsExpressionContext & expressionContext()
Gets the expression context.
Definition: qgsrendercontext.h:596
QgsPainting::BlendMode
BlendMode
Blending modes enum defining the available composition modes that can be used when rendering a layer.
Definition: qgspainting.h:37
QgsTextRenderer::drawText
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, VAlignment vAlignment=AlignTop)
Draws text within a rectangle using the specified settings.
Definition: qgstextrenderer.cpp:75
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsTextBackgroundSettings::enabled
bool enabled() const
Returns whether the background is enabled.
Definition: qgstextbackgroundsettings.cpp:90
QgsTextFormat::shadow
QgsTextShadowSettings & shadow()
Returns a reference to the text drop shadow settings.
Definition: qgstextformat.cpp:127
QgsTextFormat::previewBackgroundColor
QColor previewBackgroundColor() const
Returns the background color for text previews.
Definition: qgstextformat.cpp:311
QgsSymbolLayerUtils::encodeMapUnitScale
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
Definition: qgssymbollayerutils.cpp:558
qgstextrenderer.h
QgsPalLayerSettings::Color
@ Color
Text color.
Definition: qgspallabeling.h:351
QgsMapToPixel::setParameters
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Set parameters for use in transforming coordinates.
Definition: qgsmaptopixel.cpp:163
qgstextrenderer_p.h
QgsTextFormat::orientation
TextOrientation orientation() const
Returns the orientation of the text.
Definition: qgstextformat.cpp:276
QgsPalLayerSettings::FontOpacity
@ FontOpacity
Text opacity.
Definition: qgspallabeling.h:357
QgsTextFormat::buffer
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
Definition: qgstextformat.cpp:103
QgsTextRenderer::AlignCenter
@ AlignCenter
Center align.
Definition: qgstextrenderer.h:61
QgsRenderContext::setFlag
void setFlag(Flag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
Definition: qgsrendercontext.cpp:179
QgsRenderContext::setPainter
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Definition: qgsrendercontext.h:491
QgsUnitTypes::RenderPoints
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:172
QgsExpressionContext::setOriginalValueVariable
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
Definition: qgsexpressioncontext.cpp:566
qgssymbollayerutils.h
QgsTextBufferSettings::updateDataDefinedProperties
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
Definition: qgstextbuffersettings.cpp:172
QgsTextBackgroundSettings::referencedFields
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Definition: qgstextbackgroundsettings.cpp:814
QgsTextFormat::background
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
Definition: qgstextformat.cpp:115
QgsTextFormat::setValid
void setValid()
Sets the format to a valid state, without changing any of the default format settings.
Definition: qgstextformat.cpp:98
QgsPalLayerSettings::Strikeout
@ Strikeout
Use strikeout.
Definition: qgspallabeling.h:352
QgsTextShadowSettings::readXml
void readXml(const QDomElement &elem)
Read settings from a DOM element.
Definition: qgstextshadowsettings.cpp:289
QgsTextFormat::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
Definition: qgstextformat.cpp:199
QgsTextBackgroundSettings
Container for settings relating to a text background object.
Definition: qgstextbackgroundsettings.h:46
QgsUnitTypes::RenderPercentage
@ RenderPercentage
Percentage of another measurement (e.g., canvas size, feature size)
Definition: qgsunittypes.h:171
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsTextBackgroundSettings::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
Definition: qgstextbackgroundsettings.cpp:599
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
QgsTextRendererUtils::decodeTextOrientation
static QgsTextFormat::TextOrientation decodeTextOrientation(const QString &name, bool *ok=nullptr)
Attempts to decode a string representation of a text orientation.
Definition: qgstextrendererutils.cpp:112
QgsTextRendererUtils::readColor
static QColor readColor(QgsVectorLayer *layer, const QString &property, const QColor &defaultColor=Qt::black, bool withAlpha=true)
Converts an encoded color value from a layer property.
Definition: qgstextrendererutils.cpp:145
QgsTextBackgroundSettings::readFromLayer
void readFromLayer(QgsVectorLayer *layer)
Reads settings from a layer's custom properties (for QGIS 2.x projects).
Definition: qgstextbackgroundsettings.cpp:340
QgsTextRendererUtils::encodeTextOrientation
static QString encodeTextOrientation(QgsTextFormat::TextOrientation orientation)
Encodes a text orientation.
Definition: qgstextrendererutils.cpp:98
QgsTextFormat::operator=
QgsTextFormat & operator=(const QgsTextFormat &other)
Definition: qgstextformat.cpp:45
QgsStringUtils::Capitalization
Capitalization
Capitalization options.
Definition: qgsstringutils.h:189
QgsTextFormat::setPreviewBackgroundColor
void setPreviewBackgroundColor(const QColor &color)
Sets the background color that text will be rendered on for previews.
Definition: qgstextformat.cpp:316
qgsfontutils.h
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:57
QgsTextBackgroundSettings::sizeType
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
Definition: qgstextbackgroundsettings.cpp:130
QgsTextBackgroundSettings::blendMode
QPainter::CompositionMode blendMode() const
Returns the blending mode used for drawing the background shape.
Definition: qgstextbackgroundsettings.cpp:260
QgsTextShadowSettings::enabled
bool enabled() const
Returns whether the shadow is enabled.
Definition: qgstextshadowsettings.cpp:73
QgsTextFormat::color
QColor color() const
Returns the color that text will be rendered in.
Definition: qgstextformat.cpp:232
QgsTextShadowSettings::writeXml
QDomElement writeXml(QDomDocument &doc) const
Write settings into a DOM element.
Definition: qgstextshadowsettings.cpp:357
QgsTextRenderer::Rect
@ Rect
Text within rectangle draw mode.
Definition: qgstextrenderer.h:43
QgsUnitTypes::decodeRenderUnit
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
Definition: qgsunittypes.cpp:2900
QgsTextMaskSettings::updateDataDefinedProperties
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
Definition: qgstextmasksettings.cpp:145
QgsPainting::getBlendModeEnum
static QgsPainting::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a BlendMode corresponding to a QPainter::CompositionMode.
Definition: qgspainting.cpp:80
QgsPalLayerSettings::FontWordSpacing
@ FontWordSpacing
Word spacing.
Definition: qgspallabeling.h:360
QgsTextMaskSettings::readXml
void readXml(const QDomElement &elem)
Read settings from a DOM element.
Definition: qgstextmasksettings.cpp:197
QgsTextFormat::setBackground
void setBackground(const QgsTextBackgroundSettings &backgroundSettings)
Sets the text's background settings.q.
Definition: qgstextformat.cpp:121
QgsTextBackgroundSettings::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
Definition: qgstextbackgroundsettings.cpp:160
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsTextFormat::setColor
void setColor(const QColor &color)
Sets the color that text will be rendered in.
Definition: qgstextformat.cpp:237
QgsTextBackgroundSettings::size
QSizeF size() const
Returns the size of the background shape.
Definition: qgstextbackgroundsettings.cpp:140
QgsTextFormat::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the size.
Definition: qgstextformat.cpp:210
qgstextrendererutils.h
QgsTextShadowSettings::blendMode
QPainter::CompositionMode blendMode() const
Returns the blending mode used for drawing the drop shadow.
Definition: qgstextshadowsettings.cpp:213
QgsPalLayerSettings::FontLetterSpacing
@ FontLetterSpacing
Letter spacing.
Definition: qgspallabeling.h:359
QgsTextShadowSettings::updateDataDefinedProperties
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
Definition: qgstextshadowsettings.cpp:378
QgsUnitTypes::encodeUnit
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
Definition: qgsunittypes.cpp:122
QgsStringUtils::MixedCase
@ MixedCase
Mixed case, ie no change.
Definition: qgsstringutils.h:190
QgsTextFormat::~QgsTextFormat
~QgsTextFormat()
Definition: qgstextformat.cpp:57
QgsPalLayerSettings::Bold
@ Bold
Use bold style.
Definition: qgspallabeling.h:348
QgsTextBufferSettings::writeXml
QDomElement writeXml(QDomDocument &doc) const
Write settings into a DOM element.
Definition: qgstextbuffersettings.cpp:373
QgsTextFormat::updateDataDefinedProperties
void updateDataDefinedProperties(QgsRenderContext &context)
Updates the format by evaluating current values of data defined properties.
Definition: qgstextformat.cpp:728
QgsTextMaskSettings
Container for settings relating to a selective masking around a text.
Definition: qgstextmasksettings.h:42
QgsTextFormat::fromMimeData
static QgsTextFormat fromMimeData(const QMimeData *data, bool *ok=nullptr)
Attempts to parse the provided mime data as a QgsTextFormat.
Definition: qgstextformat.cpp:656
QgsTextBufferSettings::referencedFields
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Definition: qgstextbuffersettings.cpp:230
QgsFontUtils::translateNamedStyle
static QString translateNamedStyle(const QString &namedStyle)
Returns the localized named style of a font, if such a translation is available.
Definition: qgsfontutils.cpp:432
QgsPalLayerSettings::TextOrientation
@ TextOrientation
Definition: qgspallabeling.h:368
QgsPainting::getCompositionMode
static QPainter::CompositionMode getCompositionMode(QgsPainting::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a BlendMode.
Definition: qgspainting.cpp:20
QgsPalLayerSettings::FontBlendMode
@ FontBlendMode
Text blend mode.
Definition: qgspallabeling.h:361
QgsTextBackgroundSettings::updateDataDefinedProperties
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
Definition: qgstextbackgroundsettings.cpp:637
QgsTextFormat::allowHtmlFormatting
bool allowHtmlFormatting() const
Returns true if text should be treated as a HTML document and HTML tags should be used for formatting...
Definition: qgstextformat.cpp:300
QgsTextFormat::setBlendMode
void setBlendMode(QPainter::CompositionMode mode)
Sets the blending mode used for drawing the text.
Definition: qgstextformat.cpp:259
qgspainting.h
QgsRenderContext::setMapToPixel
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
Definition: qgsrendercontext.h:420
QgsTextFormat::lineHeight
double lineHeight() const
Returns the line height for text.
Definition: qgstextformat.cpp:265
QgsFontUtils::fontFamilyMatchOnSystem
static bool fontFamilyMatchOnSystem(const QString &family, QString *chosen=nullptr, bool *match=nullptr)
Check whether font family is on system.
Definition: qgsfontutils.cpp:69
QgsTextFormat::opacity
double opacity() const
Returns the text's opacity.
Definition: qgstextformat.cpp:243
QgsTextRenderer::textHeight
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode=Point, QFontMetricsF *fontMetrics=nullptr)
Returns the height of a text based on a given format.
Definition: qgstextrenderer.cpp:567
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:38
QgsPalLayerSettings::propertyDefinitions
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the labeling property definitions.
Definition: qgspallabeling.cpp:573
QgsTextFormat::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
Definition: qgstextformat.cpp:406
QgsTextFormat::capitalization
QgsStringUtils::Capitalization capitalization() const
Returns the text capitalization style.
Definition: qgstextformat.cpp:287
QgsTextBackgroundSettings::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
Definition: qgstextbackgroundsettings.cpp:468
QgsTextFormat::fromQFont
static QgsTextFormat fromQFont(const QFont &font)
Returns a text format matching the settings from an input font.
Definition: qgstextformat.cpp:607
QgsTextFormat::toQFont
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
Definition: qgstextformat.cpp:625
QgsTextFormat::setOrientation
void setOrientation(TextOrientation orientation)
Sets the orientation for the text.
Definition: qgstextformat.cpp:281
QgsTextFormat::isValid
bool isValid() const
Returns true if the format is valid.
Definition: qgstextformat.cpp:93
QgsTextBufferSettings
Container for settings relating to a text buffer.
Definition: qgstextbuffersettings.h:43
QgsTextFormat::textFormatPreviewPixmap
static QPixmap textFormatPreviewPixmap(const QgsTextFormat &format, QSize size, const QString &previewText=QString(), int padding=0)
Returns a pixmap preview for a text format.
Definition: qgstextformat.cpp:911
QgsTextBufferSettings::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
Definition: qgstextbuffersettings.cpp:92
QgsFontUtils::fontFamilyOnSystem
static bool fontFamilyOnSystem(const QString &family)
Check whether font family is on system in a quick manner, which does not compare [foundry].
Definition: qgsfontutils.cpp:38
QgsTextBufferSettings::readXml
void readXml(const QDomElement &elem)
Read settings from a DOM element.
Definition: qgstextbuffersettings.cpp:306
QgsTextFormat::setNamedStyle
void setNamedStyle(const QString &style)
Sets the named style for the font used for rendering text.
Definition: qgstextformat.cpp:192
QgsTextBufferSettings::size
double size() const
Returns the size of the buffer.
Definition: qgstextbuffersettings.cpp:82
QgsUnitTypes::RenderPixels
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:170
qgsvectorlayer.h
QgsTextFormat::mask
QgsTextMaskSettings & mask()
Returns a reference to the masking settings.
Definition: qgstextformat.cpp:139
QgsTextFormat::size
double size() const
Returns the size for rendered text.
Definition: qgstextformat.cpp:221
QgsTextFormat::setSizeMapUnitScale
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the size.
Definition: qgstextformat.cpp:215
QgsTextFormat::operator!=
bool operator!=(const QgsTextFormat &other) const
Definition: qgstextformat.cpp:88
QgsTextFormat::setAllowHtmlFormatting
void setAllowHtmlFormatting(bool allow)
Sets whether text should be treated as a HTML document and HTML tags should be used for formatting th...
Definition: qgstextformat.cpp:305
QgsPropertyCollection
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
Definition: qgspropertycollection.h:319
QgsPalLayerSettings::Size
@ Size
Label size.
Definition: qgspallabeling.h:347
QgsTextRenderer::sizeToPixel
static int sizeToPixel(double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale=QgsMapUnitScale())
Calculates pixel size (considering output size should be in pixel or map units, scale factors and opt...
Definition: qgstextrenderer.cpp:70
QgsTextFormat::readFromLayer
void readFromLayer(QgsVectorLayer *layer)
Reads settings from a layer's custom properties (for QGIS 2.x projects).
Definition: qgstextformat.cpp:322
QgsTextShadowSettings
Container for settings relating to a text shadow.
Definition: qgstextshadowsettings.h:38
QgsTextBufferSettings::enabled
bool enabled() const
Returns whether the buffer is enabled.
Definition: qgstextbuffersettings.cpp:72
QgsTextFormat::toMimeData
QMimeData * toMimeData() const
Returns new mime data representing the text format settings.
Definition: qgstextformat.cpp:592
QgsTextFormat::setBuffer
void setBuffer(const QgsTextBufferSettings &bufferSettings)
Sets the text's buffer settings.
Definition: qgstextformat.cpp:109
QgsMapLayer::customProperty
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
Definition: qgsmaplayer.cpp:1723
QgsPalLayerSettings::Italic
@ Italic
Use italic style.
Definition: qgspallabeling.h:349
QgsTextFormat::setDataDefinedProperties
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the format's property collection, used for data defined overrides.
Definition: qgstextformat.cpp:722
QgsTextFormat::setShadow
void setShadow(const QgsTextShadowSettings &shadowSettings)
Sets the text's drop shadow settings.
Definition: qgstextformat.cpp:133
QgsTextFormat::dataDefinedProperties
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the format's property collection, used for data defined overrides.
Definition: qgstextformat.cpp:701
QgsMapToPixel
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:38
QgsTextFormat::setLineHeight
void setLineHeight(double height)
Sets the line height for text.
Definition: qgstextformat.cpp:270
QgsTextFormat::setCapitalization
void setCapitalization(QgsStringUtils::Capitalization capitalization)
Sets the text capitalization style.
Definition: qgstextformat.cpp:293
QgsTextBackgroundSettings::SizeFixed
@ SizeFixed
Fixed size.
Definition: qgstextbackgroundsettings.h:68
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsPalLayerSettings::Family
@ Family
Font family.
Definition: qgspallabeling.h:353
QgsTextFormat::setOpacity
void setOpacity(double opacity)
Sets the text's opacity.
Definition: qgstextformat.cpp:248
QgsTextFormat::setMask
void setMask(const QgsTextMaskSettings &maskSettings)
Sets the text's masking settings.
Definition: qgstextformat.cpp:145
QgsTextFormat::QgsTextFormat
QgsTextFormat()
Default constructor for QgsTextFormat.
Definition: qgstextformat.cpp:28
QgsPalLayerSettings::FontStyle
@ FontStyle
Font style name.
Definition: qgspallabeling.h:354
QgsTextShadowSettings::referencedFields
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Definition: qgstextshadowsettings.cpp:467
QgsUnitTypes::RenderMetersInMapUnits
@ RenderMetersInMapUnits
Meters value as Map units.
Definition: qgsunittypes.h:175
QgsUnitTypes::RenderUnknownUnit
@ RenderUnknownUnit
Mixed or unknown units.
Definition: qgsunittypes.h:174
QgsTextFormat::font
QFont font() const
Returns the font used for rendering text.
Definition: qgstextformat.cpp:151
QgsRenderContext::Antialiasing
@ Antialiasing
Use antialiasing while drawing.
Definition: qgsrendercontext.h:79
QgsTextMaskSettings::referencedFields
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Definition: qgstextmasksettings.cpp:192
QgsTextFormat::setSizeUnit
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
Definition: qgstextformat.cpp:204
QgsTextFormat::referencedFields
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Definition: qgstextformat.cpp:712
QgsTextBackgroundSettings::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
Definition: qgstextbackgroundsettings.cpp:150
QgsTextMaskSettings::writeXml
QDomElement writeXml(QDomDocument &doc) const
Write settings into a DOM element.
Definition: qgstextmasksettings.cpp:215
QgsRenderContext::setUseAdvancedEffects
void setUseAdvancedEffects(bool enabled)
Used to enable or disable advanced effects such as blend modes.
Definition: qgsrendercontext.cpp:256
QgsTextBufferSettings::readFromLayer
void readFromLayer(QgsVectorLayer *layer)
Reads settings from a layer's custom properties (for QGIS 2.x projects).
Definition: qgstextbuffersettings.cpp:235
qgstextformat.h
QgsPalLayerSettings::Underline
@ Underline
Use underline.
Definition: qgspallabeling.h:350
QgsTextFormat::blendMode
QPainter::CompositionMode blendMode() const
Returns the blending mode used for drawing the text.
Definition: qgstextformat.cpp:254
QgsFontUtils::untranslateNamedStyle
static QString untranslateNamedStyle(const QString &namedStyle)
Returns the english named style of a font, if possible.
Definition: qgsfontutils.cpp:446
QgsTextFormat::setSize
void setSize(double size)
Sets the size for rendered text.
Definition: qgstextformat.cpp:226
QgsPalLayerSettings::FontSizeUnit
@ FontSizeUnit
Font size units.
Definition: qgspallabeling.h:355
QgsTextBufferSettings::blendMode
QPainter::CompositionMode blendMode() const
Returns the blending mode used for drawing the buffer.
Definition: qgstextbuffersettings.cpp:152
QgsTextFormat::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
Definition: qgstextformat.cpp:555
QgsUnitTypes::RenderMapUnits
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:169
QgsTextFormat::operator==
bool operator==(const QgsTextFormat &other) const
Definition: qgstextformat.cpp:62
QgsTextFormat::TextOrientation
TextOrientation
Text orientation.
Definition: qgstextformat.h:45
QgsSymbolLayerUtils::decodeMapUnitScale
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
Definition: qgssymbollayerutils.cpp:568
QgsRenderContext::setScaleFactor
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
Definition: qgsrendercontext.h:476
QgsFontUtils::updateFontViaStyle
static bool updateFontViaStyle(QFont &f, const QString &fontstyle, bool fallback=false)
Updates font with named style and retain all font properties.
Definition: qgsfontutils.cpp:122
QgsTextFormat::containsAdvancedEffects
bool containsAdvancedEffects() const
Returns true if any component of the font format requires advanced effects such as blend modes,...
Definition: qgstextformat.cpp:684