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