QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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 "qgsconfig.h"
17#include "qgstextformat.h"
18
19#include "qgsapplication.h"
20#include "qgscolorutils.h"
21#include "qgsfontmanager.h"
22#include "qgsfontutils.h"
23#include "qgspainting.h"
24#include "qgspallabeling.h"
25#include "qgssymbollayerutils.h"
26#include "qgstextrenderer.h"
27#include "qgstextrenderer_p.h"
29#include "qgsunittypes.h"
30#include "qgsvectorlayer.h"
31
32#include <QFontDatabase>
33#include <QMimeData>
34#include <QScreen>
35#include <QString>
36#include <QWidget>
37
38using namespace Qt::StringLiterals;
39
41{
42 d = new QgsTextSettingsPrivate();
43}
44
46 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
47 : mBufferSettings( other.mBufferSettings )
48 , mBackgroundSettings( other.mBackgroundSettings )
49 , mShadowSettings( other.mShadowSettings )
50 , mMaskSettings( other.mMaskSettings )
51 , mTextFontFamily( other.mTextFontFamily )
52 , mTextFontFound( other.mTextFontFound )
53 , d( other.d )
54//****** IMPORTANT! editing this? make sure you update the move constructor too! *****
55{}
56
58 : mBufferSettings( std::move( other.mBufferSettings ) )
59 , mBackgroundSettings( std::move( other.mBackgroundSettings ) )
60 , mShadowSettings( std::move( other.mShadowSettings ) )
61 , mMaskSettings( std::move( other.mMaskSettings ) )
62 , mTextFontFamily( std::move( other.mTextFontFamily ) )
63 , mTextFontFound( other.mTextFontFound )
64 , d( std::move( other.d ) )
65{}
66
68{
69 if ( &other == this )
70 return *this;
71
72 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
73 d = other.d;
74 mBufferSettings = other.mBufferSettings;
75 mBackgroundSettings = other.mBackgroundSettings;
76 mShadowSettings = other.mShadowSettings;
77 mMaskSettings = other.mMaskSettings;
78 mTextFontFamily = other.mTextFontFamily;
79 mTextFontFound = other.mTextFontFound;
80 return *this;
81 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
82}
83
85{
86 if ( &other == this )
87 return *this;
88
89 d = std::move( other.d );
90 mBufferSettings = std::move( other.mBufferSettings );
91 mBackgroundSettings = std::move( other.mBackgroundSettings );
92 mShadowSettings = std::move( other.mShadowSettings );
93 mMaskSettings = std::move( other.mMaskSettings );
94 mTextFontFamily = std::move( other.mTextFontFamily );
95 mTextFontFound = other.mTextFontFound;
96 return *this;
97}
98
101
102bool QgsTextFormat::operator==( const QgsTextFormat &other ) const
103{
104 if ( d->isValid != other.isValid()
105 || d->textFont != other.font()
106 || namedStyle() != other.namedStyle()
107 || d->fontSizeUnits != other.sizeUnit()
108 || d->fontSizeMapUnitScale != other.sizeMapUnitScale()
109 || d->fontSize != other.size()
110 || d->textColor != other.color()
111 || d->opacity != other.opacity()
112 || d->blendMode != other.blendMode()
113 || d->multilineHeight != other.lineHeight()
114 || d->multilineHeightUnits != other.lineHeightUnit()
115 || d->orientation != other.orientation()
116 || d->previewBackgroundColor != other.previewBackgroundColor()
117 || d->allowHtmlFormatting != other.allowHtmlFormatting()
118 || d->forcedBold != other.forcedBold()
119 || d->forcedItalic != other.forcedItalic()
120 || d->capitalization != other.capitalization()
121 || d->tabStopDistance != other.tabStopDistance()
122 || d->tabPositions != other.tabPositions()
123 || d->tabStopDistanceUnits != other.tabStopDistanceUnit()
124 || d->tabStopDistanceMapUnitScale != other.tabStopDistanceMapUnitScale()
125 || mBufferSettings != other.mBufferSettings
126 || mBackgroundSettings != other.mBackgroundSettings
127 || mShadowSettings != other.mShadowSettings
128 || mMaskSettings != other.mMaskSettings
129 || d->families != other.families()
130 || d->mDataDefinedProperties != other.dataDefinedProperties() )
131 return false;
132
133 return true;
134}
135
136bool QgsTextFormat::operator!=( const QgsTextFormat &other ) const
137{
138 return !( *this == other );
139}
140
142{
143 return d->isValid;
144}
145
147{
148 d->isValid = true;
149}
150
152{
153 d->isValid = true;
154 return mBufferSettings;
155}
156
158{
159 d->isValid = true;
160 mBufferSettings = bufferSettings;
161}
162
164{
165 d->isValid = true;
166 return mBackgroundSettings;
167}
168
170{
171 d->isValid = true;
172 mBackgroundSettings = backgroundSettings;
173}
174
176{
177 d->isValid = true;
178 return mShadowSettings;
179}
180
182{
183 d->isValid = true;
184 mShadowSettings = shadowSettings;
185}
186
188{
189 d->isValid = true;
190 return mMaskSettings;
191}
192
194{
195 d->isValid = true;
196 mMaskSettings = maskSettings;
197}
198
200{
201 return d->textFont;
202}
203
204QFont QgsTextFormat::scaledFont( const QgsRenderContext &context, double scaleFactor, bool *isZeroSize ) const
205{
206 if ( isZeroSize )
207 *isZeroSize = false;
208
209 QFont font = d->textFont;
210 if ( scaleFactor == 1 )
211 {
212 int fontPixelSize = QgsTextRenderer::sizeToPixel( d->fontSize, context, d->fontSizeUnits, d->fontSizeMapUnitScale );
213 if ( fontPixelSize == 0 )
214 {
215 if ( isZeroSize )
216 *isZeroSize = true;
217 return QFont();
218 }
219
220 font.setPixelSize( fontPixelSize );
221 }
222 else
223 {
224 double fontPixelSize = context.convertToPainterUnits( d->fontSize, d->fontSizeUnits, d->fontSizeMapUnitScale );
225 if ( qgsDoubleNear( fontPixelSize, 0 ) )
226 {
227 if ( isZeroSize )
228 *isZeroSize = true;
229 return QFont();
230 }
231 const int roundedPixelSize = static_cast< int >( std::round( scaleFactor * fontPixelSize + 0.5 ) );
232 font.setPixelSize( roundedPixelSize );
233 }
234
235 font.setLetterSpacing( QFont::AbsoluteSpacing, context.convertToPainterUnits( d->textFont.letterSpacing(), d->fontSizeUnits, d->fontSizeMapUnitScale ) * scaleFactor );
236 font.setWordSpacing( context.convertToPainterUnits( d->textFont.wordSpacing(), d->fontSizeUnits, d->fontSizeMapUnitScale ) * scaleFactor * scaleFactor );
237
238 if ( d->capitalization == Qgis::Capitalization::SmallCaps || d->capitalization == Qgis::Capitalization::AllSmallCaps )
239 font.setCapitalization( QFont::SmallCaps );
240
241 return font;
242}
243
244void QgsTextFormat::setFont( const QFont &font )
245{
246 d->isValid = true;
247 d->textFont = font;
248 d->originalFontFamily.clear();
249}
250
252{
253 if ( !d->textNamedStyle.isEmpty() )
254 return d->textNamedStyle;
255
256 QFontDatabase db;
257 return db.styleString( d->textFont );
258}
259
260void QgsTextFormat::setNamedStyle( const QString &style )
261{
262 d->isValid = true;
263 QgsFontUtils::updateFontViaStyle( d->textFont, style );
264 d->textNamedStyle = style;
265}
266
268{
269 return d->forcedBold;
270}
271
273{
274 d->isValid = true;
275 d->textFont.setBold( forced );
276 d->forcedBold = true;
277}
278
280{
281 return d->forcedItalic;
282}
283
285{
286 d->isValid = true;
287 d->textFont.setItalic( forced );
288 d->forcedItalic = true;
289}
290
291QStringList QgsTextFormat::families() const
292{
293 return d->families;
294}
295
296void QgsTextFormat::setFamilies( const QStringList &families )
297{
298 d->isValid = true;
299 d->families = families;
300}
301
303{
304 return d->fontSizeUnits;
305}
306
308{
309 d->isValid = true;
310 d->fontSizeUnits = unit;
311}
312
314{
315 return d->fontSizeMapUnitScale;
316}
317
319{
320 d->isValid = true;
321 d->fontSizeMapUnitScale = scale;
322}
323
325{
326 return d->fontSize;
327}
328
330{
331 d->isValid = true;
332 d->fontSize = size;
333}
334
336{
337 return d->textColor;
338}
339
340void QgsTextFormat::setColor( const QColor &color )
341{
342 d->isValid = true;
343 d->textColor = color;
344}
345
347{
348 return d->opacity;
349}
350
351void QgsTextFormat::multiplyOpacity( double opacityFactor )
352{
353 if ( qgsDoubleNear( opacityFactor, 1.0 ) )
354 return;
355 d->opacity *= opacityFactor;
356 mBufferSettings.setOpacity( mBufferSettings.opacity() * opacityFactor );
357 mShadowSettings.setOpacity( mShadowSettings.opacity() * opacityFactor );
358 mBackgroundSettings.setOpacity( mBackgroundSettings.opacity() * opacityFactor );
359 mMaskSettings.setOpacity( mMaskSettings.opacity() * opacityFactor );
360}
361
363{
364 d->isValid = true;
365 d->opacity = opacity;
366}
367
369{
370 return d->textFont.stretch() > 0 ? d->textFont.stretch() : 100;
371}
372
374{
375 d->isValid = true;
376 d->textFont.setStretch( factor );
377}
378
379QPainter::CompositionMode QgsTextFormat::blendMode() const
380{
381 return d->blendMode;
382}
383
384void QgsTextFormat::setBlendMode( QPainter::CompositionMode mode )
385{
386 d->isValid = true;
387 d->blendMode = mode;
388}
389
391{
392 return d->multilineHeight;
393}
394
395void QgsTextFormat::setLineHeight( double height )
396{
397 d->isValid = true;
398 d->multilineHeight = height;
399}
400
402{
403 return d->multilineHeightUnits;
404}
405
407{
408 d->isValid = true;
409 d->multilineHeightUnits = unit;
410}
411
413{
414 return d->tabStopDistance;
415}
416
418{
419 d->isValid = true;
420 d->tabStopDistance = distance;
421}
422
423QList<QgsTextFormat::Tab> QgsTextFormat::tabPositions() const
424{
425 return d->tabPositions;
426}
427
428void QgsTextFormat::setTabPositions( const QList<Tab> &positions )
429{
430 d->isValid = true;
431 d->tabPositions = positions;
432}
433
435{
436 return d->tabStopDistanceUnits;
437}
438
440{
441 d->isValid = true;
442 d->tabStopDistanceUnits = unit;
443}
444
446{
447 return d->tabStopDistanceMapUnitScale;
448}
449
451{
452 d->isValid = true;
453 d->tabStopDistanceMapUnitScale = scale;
454}
455
457{
458 return d->orientation;
459}
460
462{
463 d->isValid = true;
464 d->orientation = orientation;
465}
466
468{
469 // bit of complexity here to maintain API..
470 return d->capitalization == Qgis::Capitalization::MixedCase && d->textFont.capitalization() != QFont::MixedCase ? static_cast< Qgis::Capitalization >( d->textFont.capitalization() )
471 : d->capitalization;
472}
473
475{
476 d->isValid = true;
477 d->capitalization = capitalization;
478 d->textFont.setCapitalization( capitalization == Qgis::Capitalization::SmallCaps || capitalization == Qgis::Capitalization::AllSmallCaps ? QFont::SmallCaps : QFont::MixedCase );
479}
480
482{
483 return d->allowHtmlFormatting;
484}
485
487{
488 d->isValid = true;
489 d->allowHtmlFormatting = allow;
490}
491
493{
494 return d->previewBackgroundColor;
495}
496
498{
499 d->isValid = true;
500 d->previewBackgroundColor = color;
501}
502
504{
505 d->isValid = true;
506 QFont appFont = QApplication::font();
507 d->originalFontFamily = QgsApplication::fontManager()->processFontFamilyName( layer->customProperty( u"labeling/fontFamily"_s, QVariant( appFont.family() ) ).toString() );
508 mTextFontFamily = d->originalFontFamily;
509 QString fontFamily = mTextFontFamily;
510 if ( mTextFontFamily != appFont.family() && !QgsFontUtils::fontFamilyMatchOnSystem( mTextFontFamily ) )
511 {
512 // trigger to notify about font family substitution
513 mTextFontFound = false;
514
515 // TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented
516 // currently only defaults to matching algorithm for resolving [foundry], if a font of similar family is found (default for QFont)
517
518 // for now, do not use matching algorithm for substitution if family not found, substitute default instead
519 fontFamily = appFont.family();
520 }
521 else
522 {
523 mTextFontFound = true;
524 }
525
526 if ( !layer->customProperty( u"labeling/fontSize"_s ).isValid() )
527 {
528 d->fontSize = appFont.pointSizeF();
529 }
530 else
531 {
532 d->fontSize = layer->customProperty( u"labeling/fontSize"_s ).toDouble();
533 }
534
535 if ( layer->customProperty( u"labeling/fontSizeUnit"_s ).toString().isEmpty() )
536 {
537 d->fontSizeUnits = layer->customProperty( u"labeling/fontSizeInMapUnits"_s, QVariant( false ) ).toBool() ? Qgis::RenderUnit::MapUnits : Qgis::RenderUnit::Points;
538 }
539 else
540 {
541 bool ok = false;
542 d->fontSizeUnits = QgsUnitTypes::decodeRenderUnit( layer->customProperty( u"labeling/fontSizeUnit"_s ).toString(), &ok );
543 if ( !ok )
544 d->fontSizeUnits = Qgis::RenderUnit::Points;
545 }
546 if ( layer->customProperty( u"labeling/fontSizeMapUnitScale"_s ).toString().isEmpty() )
547 {
548 //fallback to older property
549 double oldMin = layer->customProperty( u"labeling/fontSizeMapUnitMinScale"_s, 0.0 ).toDouble();
550 d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
551 double oldMax = layer->customProperty( u"labeling/fontSizeMapUnitMaxScale"_s, 0.0 ).toDouble();
552 d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
553 }
554 else
555 {
556 d->fontSizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( layer->customProperty( u"labeling/fontSizeMapUnitScale"_s ).toString() );
557 }
558 int fontWeight = layer->customProperty( u"labeling/fontWeight"_s ).toInt();
559 bool fontItalic = layer->customProperty( u"labeling/fontItalic"_s ).toBool();
560 d->textFont = QgsFontUtils::createFont( fontFamily, d->fontSize, fontWeight, fontItalic );
561 d->textNamedStyle = QgsFontUtils::translateNamedStyle( layer->customProperty( u"labeling/namedStyle"_s, QVariant( "" ) ).toString() );
562 QgsFontUtils::updateFontViaStyle( d->textFont, d->textNamedStyle ); // must come after textFont.setPointSizeF()
563 d->capitalization = static_cast< Qgis::Capitalization >( layer->customProperty( u"labeling/fontCapitals"_s, QVariant( 0 ) ).toUInt() );
564 d->textFont.setUnderline( layer->customProperty( u"labeling/fontUnderline"_s ).toBool() );
565 d->textFont.setStrikeOut( layer->customProperty( u"labeling/fontStrikeout"_s ).toBool() );
566 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, layer->customProperty( u"labeling/fontLetterSpacing"_s, QVariant( 0.0 ) ).toDouble() );
567 d->textFont.setWordSpacing( layer->customProperty( u"labeling/fontWordSpacing"_s, QVariant( 0.0 ) ).toDouble() );
568 d->textColor = QgsTextRendererUtils::readColor( layer, u"labeling/textColor"_s, Qt::black, false );
569 if ( layer->customProperty( u"labeling/textOpacity"_s ).toString().isEmpty() )
570 {
571 d->opacity = ( 1 - layer->customProperty( u"labeling/textTransp"_s ).toInt() / 100.0 ); //0 -100
572 }
573 else
574 {
575 d->opacity = ( layer->customProperty( u"labeling/textOpacity"_s ).toDouble() );
576 }
577 d->blendMode = QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( layer->customProperty( u"labeling/blendMode"_s, QVariant( static_cast< int >( Qgis::BlendMode::Normal ) ) ).toUInt() ) );
578 d->multilineHeight = layer->customProperty( u"labeling/multilineHeight"_s, QVariant( 1.0 ) ).toDouble();
579 d->previewBackgroundColor = QgsTextRendererUtils::readColor( layer, u"labeling/previewBkgrdColor"_s, QColor( 255, 255, 255 ), false );
580
581 mBufferSettings.readFromLayer( layer );
582 mShadowSettings.readFromLayer( layer );
583 mBackgroundSettings.readFromLayer( layer );
584}
585
586void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
587{
588 d->isValid = true;
589 QDomElement textStyleElem;
590 if ( elem.nodeName() == "text-style"_L1 )
591 textStyleElem = elem;
592 else
593 textStyleElem = elem.firstChildElement( u"text-style"_s );
594 QFont appFont = QApplication::font();
595 d->originalFontFamily = QgsApplication::fontManager()->processFontFamilyName( textStyleElem.attribute( u"fontFamily"_s, appFont.family() ) );
596 mTextFontFamily = d->originalFontFamily;
597 QString fontFamily = mTextFontFamily;
598
599 const QDomElement familiesElem = textStyleElem.firstChildElement( u"families"_s );
600 const QDomNodeList familyNodes = familiesElem.childNodes();
601 QStringList families;
602 families.reserve( familyNodes.size() );
603 for ( int i = 0; i < familyNodes.count(); ++i )
604 {
605 const QDomElement familyElem = familyNodes.at( i ).toElement();
606 families << familyElem.attribute( u"name"_s );
607 }
608 d->families = families;
609
610 mTextFontFound = false;
611 QString matched;
612 if ( mTextFontFamily != appFont.family() && !QgsFontUtils::fontFamilyMatchOnSystem( mTextFontFamily ) )
613 {
614 if ( QgsApplication::fontManager()->tryToDownloadFontFamily( mTextFontFamily, matched ) )
615 {
616 mTextFontFound = true;
617 }
618 else
619 {
620 for ( const QString &family : std::as_const( families ) )
621 {
622 const QString processedFamily = QgsApplication::fontManager()->processFontFamilyName( family );
623 if ( QgsFontUtils::fontFamilyMatchOnSystem( processedFamily ) || QgsApplication::fontManager()->tryToDownloadFontFamily( processedFamily, matched ) )
624 {
625 mTextFontFound = true;
626 fontFamily = processedFamily;
627 break;
628 }
629 }
630
631 if ( !mTextFontFound )
632 {
633 // couldn't even find a matching font in the backup list -- substitute default instead
634 fontFamily = appFont.family();
635 }
636 }
637 }
638 else
639 {
640 mTextFontFound = true;
641 }
642
643 if ( !mTextFontFound )
644 {
645 context.pushMessage( QObject::tr( "Font “%1” not available on system" ).arg( mTextFontFamily ) );
646 }
647
648 if ( textStyleElem.hasAttribute( u"fontSize"_s ) )
649 {
650 d->fontSize = textStyleElem.attribute( u"fontSize"_s ).toDouble();
651 }
652 else
653 {
654 d->fontSize = appFont.pointSizeF();
655 }
656
657 if ( !textStyleElem.hasAttribute( u"fontSizeUnit"_s ) )
658 {
659 d->fontSizeUnits = textStyleElem.attribute( u"fontSizeInMapUnits"_s ).toUInt() == 0 ? Qgis::RenderUnit::Points : Qgis::RenderUnit::MapUnits;
660 }
661 else
662 {
663 d->fontSizeUnits = QgsUnitTypes::decodeRenderUnit( textStyleElem.attribute( u"fontSizeUnit"_s ) );
664 }
665
666 if ( !textStyleElem.hasAttribute( u"fontSizeMapUnitScale"_s ) )
667 {
668 //fallback to older property
669 double oldMin = textStyleElem.attribute( u"fontSizeMapUnitMinScale"_s, u"0"_s ).toDouble();
670 d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
671 double oldMax = textStyleElem.attribute( u"fontSizeMapUnitMaxScale"_s, u"0"_s ).toDouble();
672 d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
673 }
674 else
675 {
676 d->fontSizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textStyleElem.attribute( u"fontSizeMapUnitScale"_s ) );
677 }
678 int fontWeight = textStyleElem.attribute( u"fontWeight"_s ).toInt();
679 bool fontItalic = textStyleElem.attribute( u"fontItalic"_s ).toInt();
680 d->textFont = QgsFontUtils::createFont( fontFamily, d->fontSize, fontWeight, fontItalic );
681 d->textFont.setPointSizeF( d->fontSize ); //double precision needed because of map units
682 d->textNamedStyle = QgsFontUtils::translateNamedStyle( textStyleElem.attribute( u"namedStyle"_s ) );
683 QgsFontUtils::updateFontViaStyle( d->textFont, d->textNamedStyle ); // must come after textFont.setPointSizeF()
684 d->forcedBold = textStyleElem.attribute( u"forcedBold"_s ).toInt();
685 d->forcedItalic = textStyleElem.attribute( u"forcedItalic"_s ).toInt();
686 d->textFont.setUnderline( textStyleElem.attribute( u"fontUnderline"_s ).toInt() );
687 d->textFont.setStrikeOut( textStyleElem.attribute( u"fontStrikeout"_s ).toInt() );
688 d->textFont.setKerning( textStyleElem.attribute( u"fontKerning"_s, u"1"_s ).toInt() );
689 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, textStyleElem.attribute( u"fontLetterSpacing"_s, u"0"_s ).toDouble() );
690 d->textFont.setWordSpacing( textStyleElem.attribute( u"fontWordSpacing"_s, u"0"_s ).toDouble() );
691 d->textColor = QgsColorUtils::colorFromString( textStyleElem.attribute( u"textColor"_s, QgsColorUtils::colorToString( Qt::black ) ) );
692 if ( !textStyleElem.hasAttribute( u"textOpacity"_s ) )
693 {
694 d->opacity = ( 1 - textStyleElem.attribute( u"textTransp"_s ).toInt() / 100.0 ); //0 -100
695 }
696 else
697 {
698 d->opacity = ( textStyleElem.attribute( u"textOpacity"_s ).toDouble() );
699 }
700 if ( textStyleElem.hasAttribute( u"stretchFactor"_s ) )
701 {
702 d->textFont.setStretch( textStyleElem.attribute( u"stretchFactor"_s, u"100"_s ).toInt() );
703 }
704 d->orientation = QgsTextRendererUtils::decodeTextOrientation( textStyleElem.attribute( u"textOrientation"_s ) );
705 d->previewBackgroundColor = QgsColorUtils::colorFromString( textStyleElem.attribute( u"previewBkgrdColor"_s, QgsColorUtils::colorToString( Qt::white ) ) );
706
707 d->blendMode = QgsPainting::getCompositionMode( static_cast< Qgis::BlendMode >( textStyleElem.attribute( u"blendMode"_s, QString::number( static_cast< int >( Qgis::BlendMode::Normal ) ) ).toUInt() ) );
708
709 if ( !textStyleElem.hasAttribute( u"multilineHeight"_s ) )
710 {
711 QDomElement textFormatElem = elem.firstChildElement( u"text-format"_s );
712 d->multilineHeight = textFormatElem.attribute( u"multilineHeight"_s, u"1"_s ).toDouble();
713 }
714 else
715 {
716 d->multilineHeight = textStyleElem.attribute( u"multilineHeight"_s, u"1"_s ).toDouble();
717 }
718 bool ok = false;
719 d->multilineHeightUnits = QgsUnitTypes::decodeRenderUnit( textStyleElem.attribute( u"multilineHeightUnit"_s, u"percent"_s ), &ok );
720
721 d->tabStopDistance = textStyleElem.attribute( u"tabStopDistance"_s, u"80"_s ).toDouble();
722 d->tabStopDistanceUnits = QgsUnitTypes::decodeRenderUnit( textStyleElem.attribute( u"tabStopDistanceUnit"_s, u"Point"_s ), &ok );
723 d->tabStopDistanceMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textStyleElem.attribute( u"tabStopDistanceMapUnitScale"_s ) );
724
725 QList< Tab > tabPositions;
726 {
727 const QDomElement tabPositionsElem = textStyleElem.firstChildElement( u"tabPositions"_s );
728 const QDomNodeList tabNodes = tabPositionsElem.childNodes();
729 tabPositions.reserve( tabNodes.size() );
730 for ( int i = 0; i < tabNodes.count(); ++i )
731 {
732 const QDomElement tabElem = tabNodes.at( i ).toElement();
733 tabPositions << Tab( tabElem.attribute( u"position"_s ).toDouble() );
734 }
735 }
736 d->tabPositions = tabPositions;
737
738 if ( textStyleElem.hasAttribute( u"capitalization"_s ) )
739 d->capitalization = static_cast< Qgis::Capitalization >( textStyleElem.attribute( u"capitalization"_s, QString::number( static_cast< int >( Qgis::Capitalization::MixedCase ) ) ).toInt() );
740 else
741 d->capitalization = static_cast< Qgis::Capitalization >( textStyleElem.attribute( u"fontCapitals"_s, u"0"_s ).toUInt() );
742
743 if ( d->capitalization == Qgis::Capitalization::SmallCaps || d->capitalization == Qgis::Capitalization::AllSmallCaps )
744 d->textFont.setCapitalization( QFont::SmallCaps );
745
746 d->allowHtmlFormatting = textStyleElem.attribute( u"allowHtml"_s, u"0"_s ).toInt();
747
748 if ( textStyleElem.firstChildElement( u"text-buffer"_s ).isNull() )
749 {
750 mBufferSettings.readXml( elem );
751 }
752 else
753 {
754 mBufferSettings.readXml( textStyleElem );
755 }
756 if ( textStyleElem.firstChildElement( u"text-mask"_s ).isNull() )
757 {
758 mMaskSettings.readXml( elem );
759 }
760 else
761 {
762 mMaskSettings.readXml( textStyleElem );
763 }
764 if ( textStyleElem.firstChildElement( u"shadow"_s ).isNull() )
765 {
766 mShadowSettings.readXml( elem );
767 }
768 else
769 {
770 mShadowSettings.readXml( textStyleElem );
771 }
772 if ( textStyleElem.firstChildElement( u"background"_s ).isNull() )
773 {
774 mBackgroundSettings.readXml( elem, context );
775 }
776 else
777 {
778 mBackgroundSettings.readXml( textStyleElem, context );
779 }
780
781 QDomElement ddElem = textStyleElem.firstChildElement( u"dd_properties"_s );
782 if ( ddElem.isNull() )
783 {
784 ddElem = elem.firstChildElement( u"dd_properties"_s );
785 }
786 if ( !ddElem.isNull() )
787 {
788 d->mDataDefinedProperties.readXml( ddElem, QgsPalLayerSettings::propertyDefinitions() );
789 mBackgroundSettings.upgradeDataDefinedProperties( d->mDataDefinedProperties );
790 }
791 else
792 {
793 d->mDataDefinedProperties.clear();
794 }
795}
796
797QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
798{
799 // text style
800 QDomElement textStyleElem = doc.createElement( u"text-style"_s );
801 textStyleElem.setAttribute( u"fontFamily"_s, !d->originalFontFamily.isEmpty() ? d->originalFontFamily : d->textFont.family() );
802
803 QDomElement familiesElem = doc.createElement( u"families"_s );
804 for ( const QString &family : std::as_const( d->families ) )
805 {
806 QDomElement familyElem = doc.createElement( u"family"_s );
807 familyElem.setAttribute( u"name"_s, family );
808 familiesElem.appendChild( familyElem );
809 }
810 textStyleElem.appendChild( familiesElem );
811
812 textStyleElem.setAttribute( u"namedStyle"_s, QgsFontUtils::untranslateNamedStyle( d->textNamedStyle ) );
813 textStyleElem.setAttribute( u"fontSize"_s, d->fontSize );
814 textStyleElem.setAttribute( u"fontSizeUnit"_s, QgsUnitTypes::encodeUnit( d->fontSizeUnits ) );
815 textStyleElem.setAttribute( u"fontSizeMapUnitScale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( d->fontSizeMapUnitScale ) );
816 textStyleElem.setAttribute( u"fontWeight"_s, d->textFont.weight() );
817 textStyleElem.setAttribute( u"fontItalic"_s, d->textFont.italic() );
818 textStyleElem.setAttribute( u"fontStrikeout"_s, d->textFont.strikeOut() );
819 textStyleElem.setAttribute( u"fontUnderline"_s, d->textFont.underline() );
820 textStyleElem.setAttribute( u"forcedBold"_s, d->forcedBold );
821 textStyleElem.setAttribute( u"forcedItalic"_s, d->forcedItalic );
822 textStyleElem.setAttribute( u"textColor"_s, QgsColorUtils::colorToString( d->textColor ) );
823 textStyleElem.setAttribute( u"previewBkgrdColor"_s, QgsColorUtils::colorToString( d->previewBackgroundColor ) );
824 textStyleElem.setAttribute( u"fontLetterSpacing"_s, d->textFont.letterSpacing() );
825 textStyleElem.setAttribute( u"fontWordSpacing"_s, d->textFont.wordSpacing() );
826 textStyleElem.setAttribute( u"fontKerning"_s, d->textFont.kerning() );
827 textStyleElem.setAttribute( u"textOpacity"_s, d->opacity );
828 if ( d->textFont.stretch() > 0 )
829 textStyleElem.setAttribute( u"stretchFactor"_s, d->textFont.stretch() );
830 textStyleElem.setAttribute( u"textOrientation"_s, QgsTextRendererUtils::encodeTextOrientation( d->orientation ) );
831 textStyleElem.setAttribute( u"blendMode"_s, static_cast< int >( QgsPainting::getBlendModeEnum( d->blendMode ) ) );
832 textStyleElem.setAttribute( u"multilineHeight"_s, d->multilineHeight );
833 textStyleElem.setAttribute( u"multilineHeightUnit"_s, QgsUnitTypes::encodeUnit( d->multilineHeightUnits ) );
834
835 textStyleElem.setAttribute( u"tabStopDistance"_s, d->tabStopDistance );
836 textStyleElem.setAttribute( u"tabStopDistanceUnit"_s, QgsUnitTypes::encodeUnit( d->tabStopDistanceUnits ) );
837 textStyleElem.setAttribute( u"tabStopDistanceMapUnitScale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( d->tabStopDistanceMapUnitScale ) );
838
839 if ( !d->tabPositions.empty() )
840 {
841 QDomElement tabPositionsElem = doc.createElement( u"tabPositions"_s );
842 for ( const Tab &tab : std::as_const( d->tabPositions ) )
843 {
844 QDomElement tabElem = doc.createElement( u"tab"_s );
845 tabElem.setAttribute( u"position"_s, tab.position() );
846 tabPositionsElem.appendChild( tabElem );
847 }
848 textStyleElem.appendChild( tabPositionsElem );
849 }
850
851 textStyleElem.setAttribute( u"allowHtml"_s, d->allowHtmlFormatting ? u"1"_s : u"0"_s );
852 textStyleElem.setAttribute( u"capitalization"_s, QString::number( static_cast< int >( d->capitalization ) ) );
853
854 QDomElement ddElem = doc.createElement( u"dd_properties"_s );
855 d->mDataDefinedProperties.writeXml( ddElem, QgsPalLayerSettings::propertyDefinitions() );
856
857 textStyleElem.appendChild( mBufferSettings.writeXml( doc ) );
858 textStyleElem.appendChild( mMaskSettings.writeXml( doc ) );
859 textStyleElem.appendChild( mBackgroundSettings.writeXml( doc, context ) );
860 textStyleElem.appendChild( mShadowSettings.writeXml( doc ) );
861 textStyleElem.appendChild( ddElem );
862
863 return textStyleElem;
864}
865
867{
868 //set both the mime color data, and the text (format settings).
869
870 QMimeData *mimeData = new QMimeData;
871 mimeData->setColorData( QVariant( color() ) );
872
873 QgsReadWriteContext rwContext;
874 QDomDocument textDoc;
875 QDomElement textElem = writeXml( textDoc, rwContext );
876 textDoc.appendChild( textElem );
877 mimeData->setText( textDoc.toString() );
878
879 return mimeData;
880}
881
883{
884 QgsTextFormat format;
885 format.setFont( font );
886 if ( font.pointSizeF() > 0 )
887 {
888 format.setSize( font.pointSizeF() );
890 }
891 else if ( font.pixelSize() > 0 )
892 {
893 format.setSize( font.pixelSize() );
895 }
896
897 return format;
898}
899
901{
902 QFont f = font();
903 switch ( sizeUnit() )
904 {
906 f.setPointSizeF( size() );
907 break;
908
910 f.setPointSizeF( size() * 2.83464567 );
911 break;
912
914 f.setPointSizeF( size() * 72 );
915 break;
916
918 f.setPixelSize( static_cast< int >( std::round( size() ) ) );
919 break;
920
925 // no meaning here
926 break;
927 }
928 return f;
929}
930
931QgsTextFormat QgsTextFormat::fromMimeData( const QMimeData *data, bool *ok )
932{
933 if ( ok )
934 *ok = false;
935 QgsTextFormat format;
936 if ( !data )
937 return format;
938
939 QString text = data->text();
940 if ( !text.isEmpty() )
941 {
942 QDomDocument doc;
943 QDomElement elem;
944 QgsReadWriteContext rwContext;
945
946 if ( doc.setContent( text ) )
947 {
948 elem = doc.documentElement();
949
950 format.readXml( elem, rwContext );
951 if ( ok )
952 *ok = true;
953 return format;
954 }
955 }
956 return format;
957}
958
960{
961 if ( d->blendMode != QPainter::CompositionMode_SourceOver )
962 return true;
963
964 if ( mBufferSettings.enabled() && mBufferSettings.blendMode() != QPainter::CompositionMode_SourceOver )
965 return true;
966
967 if ( mBackgroundSettings.enabled() && mBackgroundSettings.blendMode() != QPainter::CompositionMode_SourceOver )
968 return true;
969
970 if ( mShadowSettings.enabled() && mShadowSettings.blendMode() != QPainter::CompositionMode_SourceOver )
971 return true;
972
973 return false;
974}
975
977{
978 if ( d->blendMode != QPainter::CompositionMode_SourceOver )
979 return true;
980
981 if ( mBufferSettings.enabled() && mBufferSettings.blendMode() != QPainter::CompositionMode_SourceOver )
982 return true;
983
984 if ( mBackgroundSettings.enabled() && mBackgroundSettings.blendMode() != QPainter::CompositionMode_SourceOver )
985 return true;
986
987 if ( mShadowSettings.enabled() && mShadowSettings.blendMode() != QPainter::CompositionMode_SourceOver )
988 return true;
989
990 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::FontBlendMode )
991 || d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::ShadowBlendMode )
992 || d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::BufferBlendMode )
993 || d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::ShapeBlendMode ) )
994 return true;
995
996 return false;
997}
998
1000{
1001 d->isValid = true;
1002 return d->mDataDefinedProperties;
1003}
1004
1006{
1007 return d->mDataDefinedProperties;
1008}
1009
1010QSet<QString> QgsTextFormat::referencedFields( const QgsRenderContext &context ) const
1011{
1012 QSet< QString > fields = d->mDataDefinedProperties.referencedFields( context.expressionContext(), true );
1013 fields.unite( mBufferSettings.referencedFields( context ) );
1014 fields.unite( mBackgroundSettings.referencedFields( context ) );
1015 fields.unite( mShadowSettings.referencedFields( context ) );
1016 fields.unite( mMaskSettings.referencedFields( context ) );
1017 return fields;
1018}
1019
1021{
1022 d->isValid = true;
1023 d->mDataDefinedProperties = collection;
1024}
1025
1027{
1028 d->isValid = true;
1029 if ( !d->mDataDefinedProperties.hasActiveProperties() )
1030 return;
1031
1032 QString ddFontFamily;
1033 context.expressionContext().setOriginalValueVariable( d->textFont.family() );
1034 QVariant exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::Family, context.expressionContext() );
1035 if ( !QgsVariantUtils::isNull( exprVal ) )
1036 {
1037 QString family = exprVal.toString().trimmed();
1039 if ( d->textFont.family() != family )
1040 {
1041 // testing for ddFontFamily in QFontDatabase.families() may be slow to do for every feature
1042 // (i.e. don't use QgsFontUtils::fontFamilyMatchOnSystem( family ) here)
1043 if ( QgsFontUtils::fontFamilyOnSystem( family ) )
1044 {
1045 ddFontFamily = family;
1046 }
1047 }
1048 }
1049
1050 // data defined named font style?
1051 QString ddFontStyle;
1052 context.expressionContext().setOriginalValueVariable( d->textNamedStyle );
1053 exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::FontStyle, context.expressionContext() );
1054 if ( !QgsVariantUtils::isNull( exprVal ) )
1055 {
1056 QString fontstyle = exprVal.toString().trimmed();
1057 ddFontStyle = fontstyle;
1058 }
1059
1060 bool ddBold = false;
1061 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::Bold ) )
1062 {
1063 context.expressionContext().setOriginalValueVariable( d->textFont.bold() );
1064 ddBold = d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Property::Bold, context.expressionContext(), false );
1065 }
1066
1067 bool ddItalic = false;
1068 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::Italic ) )
1069 {
1070 context.expressionContext().setOriginalValueVariable( d->textFont.italic() );
1071 ddItalic = d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Property::Italic, context.expressionContext(), false );
1072 }
1073
1074 // TODO: update when pref for how to resolve missing family (use matching algorithm or just default font) is implemented
1075 // (currently defaults to what has been read in from layer settings)
1076 QFont newFont;
1077 QFontDatabase fontDb;
1078 QFont appFont = QApplication::font();
1079 bool newFontBuilt = false;
1080 if ( ddBold || ddItalic )
1081 {
1082 // new font needs built, since existing style needs removed
1083 newFont = QgsFontUtils::createFont( !ddFontFamily.isEmpty() ? ddFontFamily : d->textFont.family() );
1084 newFontBuilt = true;
1085 newFont.setBold( ddBold );
1086 newFont.setItalic( ddItalic );
1087 }
1088 else if ( !ddFontStyle.isEmpty() && ddFontStyle.compare( "Ignore"_L1, Qt::CaseInsensitive ) != 0 )
1089 {
1090 if ( !ddFontFamily.isEmpty() )
1091 {
1092 // both family and style are different, build font from database
1093 QFont styledfont = fontDb.font( ddFontFamily, ddFontStyle, appFont.pointSize() );
1094 if ( appFont != styledfont )
1095 {
1096 newFont = styledfont;
1097 newFontBuilt = true;
1098 }
1099 }
1100
1101 // update the font face style
1102 QgsFontUtils::updateFontViaStyle( newFontBuilt ? newFont : d->textFont, ddFontStyle );
1103 }
1104 else if ( !ddFontFamily.isEmpty() )
1105 {
1106 if ( ddFontStyle.compare( "Ignore"_L1, Qt::CaseInsensitive ) != 0 )
1107 {
1108 // just family is different, build font from database
1109 QFont styledfont = fontDb.font( ddFontFamily, d->textNamedStyle, appFont.pointSize() );
1110 if ( appFont != styledfont )
1111 {
1112 newFont = styledfont;
1113 newFontBuilt = true;
1114 }
1115 }
1116 else
1117 {
1118 newFont = QgsFontUtils::createFont( ddFontFamily );
1119 newFontBuilt = true;
1120 }
1121 }
1122
1123 if ( newFontBuilt )
1124 {
1125 // copy over existing font settings
1126 newFont.setUnderline( d->textFont.underline() );
1127 newFont.setStrikeOut( d->textFont.strikeOut() );
1128 newFont.setWordSpacing( d->textFont.wordSpacing() );
1129 newFont.setLetterSpacing( QFont::AbsoluteSpacing, d->textFont.letterSpacing() );
1130 d->textFont = newFont;
1131 }
1132
1133 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::Underline ) )
1134 {
1135 context.expressionContext().setOriginalValueVariable( d->textFont.underline() );
1136 d->textFont.setUnderline( d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Property::Underline, context.expressionContext(), d->textFont.underline() ) );
1137 }
1138
1139 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::Strikeout ) )
1140 {
1141 context.expressionContext().setOriginalValueVariable( d->textFont.strikeOut() );
1142 d->textFont.setStrikeOut( d->mDataDefinedProperties.valueAsBool( QgsPalLayerSettings::Property::Strikeout, context.expressionContext(), d->textFont.strikeOut() ) );
1143 }
1144
1145 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::Color ) )
1146 {
1148 d->textColor = d->mDataDefinedProperties.valueAsColor( QgsPalLayerSettings::Property::Color, context.expressionContext(), d->textColor );
1149 }
1150
1151 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::Size ) )
1152 {
1154 d->fontSize = d->mDataDefinedProperties.valueAsDouble( QgsPalLayerSettings::Property::Size, context.expressionContext(), d->fontSize );
1155 }
1156
1157 exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::FontSizeUnit, context.expressionContext() );
1158 if ( !QgsVariantUtils::isNull( exprVal ) )
1159 {
1160 QString units = exprVal.toString();
1161 if ( !units.isEmpty() )
1162 {
1163 bool ok;
1165 if ( ok )
1166 d->fontSizeUnits = res;
1167 }
1168 }
1169
1170 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::FontOpacity ) )
1171 {
1172 context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
1173 const QVariant val = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::FontOpacity, context.expressionContext(), d->opacity * 100 );
1174 if ( !QgsVariantUtils::isNull( val ) )
1175 {
1176 d->opacity = val.toDouble() / 100.0;
1177 }
1178 }
1179
1180 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::FontStretchFactor ) )
1181 {
1182 context.expressionContext().setOriginalValueVariable( d->textFont.stretch() );
1183 const QVariant val = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::FontStretchFactor, context.expressionContext(), d->textFont.stretch() );
1184 if ( !QgsVariantUtils::isNull( val ) )
1185 {
1186 d->textFont.setStretch( val.toInt() );
1187 }
1188 }
1189
1190 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::TextOrientation ) )
1191 {
1192 const QString encoded = QgsTextRendererUtils::encodeTextOrientation( d->orientation );
1193 context.expressionContext().setOriginalValueVariable( encoded );
1194 d->orientation = QgsTextRendererUtils::decodeTextOrientation( d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::TextOrientation, context.expressionContext(), encoded ).toString() );
1195 }
1196
1197 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::FontLetterSpacing ) )
1198 {
1199 context.expressionContext().setOriginalValueVariable( d->textFont.letterSpacing() );
1200 const QVariant val = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::FontLetterSpacing, context.expressionContext(), d->textFont.letterSpacing() );
1201 if ( !QgsVariantUtils::isNull( val ) )
1202 {
1203 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, val.toDouble() );
1204 }
1205 }
1206
1207 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::FontWordSpacing ) )
1208 {
1209 context.expressionContext().setOriginalValueVariable( d->textFont.wordSpacing() );
1210 const QVariant val = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::FontWordSpacing, context.expressionContext(), d->textFont.wordSpacing() );
1211 if ( !QgsVariantUtils::isNull( val ) )
1212 {
1213 d->textFont.setWordSpacing( val.toDouble() );
1214 }
1215 }
1216
1217 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::TabStopDistance ) )
1218 {
1219 context.expressionContext().setOriginalValueVariable( d->tabStopDistance );
1220 const QVariant val = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::TabStopDistance, context.expressionContext(), d->tabStopDistance );
1221 if ( !QgsVariantUtils::isNull( val ) )
1222 {
1223 if ( val.userType() == QMetaType::Type::QVariantList )
1224 {
1225 const QVariantList parts = val.toList();
1226 d->tabPositions.clear();
1227 d->tabPositions.reserve( parts.size() );
1228 for ( const QVariant &part : parts )
1229 {
1230 d->tabPositions.append( Tab( part.toDouble() ) );
1231 }
1232 }
1233 else if ( val.userType() == QMetaType::Type::QStringList )
1234 {
1235 const QStringList parts = val.toStringList();
1236 d->tabPositions.clear();
1237 d->tabPositions.reserve( parts.size() );
1238 for ( const QString &part : parts )
1239 {
1240 d->tabPositions.append( Tab( part.toDouble() ) );
1241 }
1242 }
1243 else
1244 {
1245 d->tabPositions.clear();
1246 d->tabStopDistance = val.toDouble();
1247 }
1248 }
1249 }
1250
1251 if ( d->mDataDefinedProperties.isActive( QgsPalLayerSettings::Property::FontBlendMode ) )
1252 {
1253 exprVal = d->mDataDefinedProperties.value( QgsPalLayerSettings::Property::FontBlendMode, context.expressionContext() );
1254 QString blendstr = exprVal.toString().trimmed();
1255 if ( !blendstr.isEmpty() )
1256 d->blendMode = QgsSymbolLayerUtils::decodeBlendMode( blendstr );
1257 }
1258
1259 mShadowSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1260 mBackgroundSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1261 mBufferSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1262 mMaskSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1263}
1264
1265QPixmap QgsTextFormat::textFormatPreviewPixmap( const QgsTextFormat &format, QSize size, const QString &previewText, int padding, const QgsScreenProperties &screen )
1266{
1267 const double devicePixelRatio = screen.isValid() ? screen.devicePixelRatio() : 1;
1268 QgsTextFormat tempFormat = format;
1269 QPixmap pixmap( size * devicePixelRatio );
1270 pixmap.fill( Qt::transparent );
1271 pixmap.setDevicePixelRatio( devicePixelRatio );
1272
1273 QPainter painter;
1274 painter.begin( &pixmap );
1275
1276 painter.setRenderHint( QPainter::Antialiasing );
1277
1278 const QRectF rect( 0, 0, size.width(), size.height() );
1279
1280 // shameless eye candy - use a subtle gradient when drawing background
1281 painter.setPen( Qt::NoPen );
1282 QColor background1 = tempFormat.previewBackgroundColor();
1283 if ( ( background1.lightnessF() < 0.7 ) )
1284 {
1285 background1 = background1.darker( 125 );
1286 }
1287 else
1288 {
1289 background1 = background1.lighter( 125 );
1290 }
1291 QColor background2 = tempFormat.previewBackgroundColor();
1292 QLinearGradient linearGrad( QPointF( 0, 0 ), QPointF( 0, rect.height() ) );
1293 linearGrad.setColorAt( 0, background1 );
1294 linearGrad.setColorAt( 1, background2 );
1295 painter.setBrush( QBrush( linearGrad ) );
1296 if ( size.width() > 30 )
1297 {
1298 painter.drawRoundedRect( rect, 6, 6 );
1299 }
1300 else
1301 {
1302 // don't use rounded rect for small previews
1303 painter.drawRect( rect );
1304 }
1305 painter.setBrush( Qt::NoBrush );
1306 painter.setPen( Qt::NoPen );
1307 padding += 1; // move text away from background border
1308
1309 QgsRenderContext context;
1310 QgsMapToPixel newCoordXForm;
1311 newCoordXForm.setParameters( 1, 0, 0, 0, 0, 0 );
1312 context.setMapToPixel( newCoordXForm );
1313
1314 if ( screen.isValid() )
1315 {
1316 screen.updateRenderContextForScreen( context );
1317 }
1318 else
1319 {
1320 QWidget *activeWindow = QApplication::activeWindow();
1321 if ( QScreen *screen = activeWindow ? activeWindow->screen() : nullptr )
1322 {
1323 context.setScaleFactor( screen->physicalDotsPerInch() / 25.4 );
1324 context.setDevicePixelRatio( screen->devicePixelRatio() );
1325 }
1326 else
1327 {
1328 context.setScaleFactor( 96.0 / 25.4 );
1329 context.setDevicePixelRatio( 1.0 );
1330 }
1331 }
1332
1335 context.setPainter( &painter );
1337
1338 // slightly inset text to account for buffer/background
1339 const double fontSize = context.convertToPainterUnits( tempFormat.size(), tempFormat.sizeUnit(), tempFormat.sizeMapUnitScale() );
1340 double xtrans = 0;
1341 if ( tempFormat.buffer().enabled() )
1342 xtrans = tempFormat.buffer().sizeUnit() == Qgis::RenderUnit::Percentage
1343 ? fontSize * tempFormat.buffer().size() / 100
1344 : context.convertToPainterUnits( tempFormat.buffer().size(), tempFormat.buffer().sizeUnit(), tempFormat.buffer().sizeMapUnitScale() );
1345 if ( tempFormat.background().enabled() && tempFormat.background().sizeType() != QgsTextBackgroundSettings::SizeFixed )
1346 xtrans = std::max( xtrans, context.convertToPainterUnits( tempFormat.background().size().width(), tempFormat.background().sizeUnit(), tempFormat.background().sizeMapUnitScale() ) );
1347
1348 double ytrans = 0.0;
1349 if ( tempFormat.buffer().enabled() )
1350 ytrans = std::max(
1351 ytrans,
1352 tempFormat.buffer().sizeUnit() == Qgis::RenderUnit::Percentage ? fontSize * tempFormat.buffer().size() / 100
1353 : context.convertToPainterUnits( tempFormat.buffer().size(), tempFormat.buffer().sizeUnit(), tempFormat.buffer().sizeMapUnitScale() )
1354 );
1355 if ( tempFormat.background().enabled() )
1356 ytrans = std::max( ytrans, context.convertToPainterUnits( tempFormat.background().size().height(), tempFormat.background().sizeUnit(), tempFormat.background().sizeMapUnitScale() ) );
1357
1358 const QStringList text = QStringList() << ( previewText.isEmpty() ? QObject::tr( "Aa" ) : previewText );
1359 const double textHeight = QgsTextRenderer::textHeight( context, tempFormat, text, Qgis::TextLayoutMode::Rectangle );
1360 QRectF textRect = rect;
1361 textRect.setLeft( xtrans + padding );
1362 textRect.setWidth( rect.width() - xtrans - 2 * padding );
1363
1364 if ( textRect.width() > 2000 )
1365 textRect.setWidth( 2000 - 2 * padding );
1366
1367 const double bottom = textRect.height() / 2 + textHeight / 2;
1368 textRect.setTop( bottom - textHeight );
1369 textRect.setBottom( bottom );
1370
1371 QgsTextRenderer::drawText( textRect, 0, Qgis::TextHorizontalAlignment::Center, text, context, tempFormat );
1372
1373 // draw border on top of text
1374 painter.setBrush( Qt::NoBrush );
1375 painter.setPen( QPen( tempFormat.previewBackgroundColor().darker( 150 ), 0 ) );
1376 if ( size.width() > 30 )
1377 {
1378 painter.drawRoundedRect( rect, 6, 6 );
1379 }
1380 else
1381 {
1382 // don't use rounded rect for small previews
1383 painter.drawRect( rect );
1384 }
1385 painter.end();
1386 return pixmap;
1387}
1388
1389QString QgsTextFormat::asCSS( double pointToPixelMultiplier ) const
1390{
1391 QString css;
1392
1393 switch ( lineHeightUnit() )
1394 {
1396 css += u"line-height: %1%;"_s.arg( lineHeight() * 100 );
1397 break;
1399 css += u"line-height: %1px;"_s.arg( lineHeight() );
1400 break;
1402 // While the Qt documentation states pt unit type is supported, it's ignored, convert to px
1403 css += u"line-height: %1px;"_s.arg( lineHeight() * pointToPixelMultiplier );
1404 break;
1406 // While the Qt documentation states cm unit type is supported, it's ignored, convert to px
1407 css += u"line-height: %1px;"_s.arg( lineHeight() * 2.83464567 * pointToPixelMultiplier );
1408 break;
1413 break;
1414 }
1415 css += u"color: rgba(%1,%2,%3,%4);"_s.arg( color().red() ).arg( color().green() ).arg( color().blue() ).arg( QString::number( color().alphaF(), 'f', 4 ) );
1416 QFont f = toQFont();
1418 {
1419 f.setPointSizeF( size() / 0.352778 );
1420 }
1421 css += QgsFontUtils::asCSS( toQFont(), pointToPixelMultiplier );
1422
1423 return css;
1424}
1425
1427 : mPosition( position )
1428{}
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
Definition qgis.h:2799
@ Rectangle
Text within rectangle layout mode.
Definition qgis.h:3003
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Definition qgis.h:5087
@ Normal
Normal.
Definition qgis.h:5088
Capitalization
String capitalization options.
Definition qgis.h:3503
@ AllSmallCaps
Force all characters to small caps.
Definition qgis.h:3511
@ MixedCase
Mixed case, ie no change.
Definition qgis.h:3504
@ SmallCaps
Mixed case small caps.
Definition qgis.h:3508
TextOrientation
Text orientations.
Definition qgis.h:2987
RenderUnit
Rendering size units.
Definition qgis.h:5340
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
Definition qgis.h:5344
@ Millimeters
Millimeters.
Definition qgis.h:5341
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5345
@ Unknown
Mixed or unknown units.
Definition qgis.h:5347
@ MapUnits
Map units.
Definition qgis.h:5342
@ Pixels
Pixels.
Definition qgis.h:5343
@ Inches
Inches.
Definition qgis.h:5346
@ MetersInMapUnits
Meters value as Map units.
Definition qgis.h:5348
@ Antialiasing
Use antialiasing while drawing.
Definition qgis.h:2853
@ Center
Center align.
Definition qgis.h:3045
static QgsFontManager * fontManager()
Returns the application font manager, which manages available fonts and font installation for the QGI...
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
QString processFontFamilyName(const QString &name) const
Processes a font family name, applying any matching fontFamilyReplacements() to the name.
static QString asCSS(const QFont &font, double pointToPixelMultiplier=1.0)
Returns a CSS string representing the specified font as closely as possible.
static QString translateNamedStyle(const QString &namedStyle)
Returns the localized named style of a font, if such a translation is available.
static QString untranslateNamedStyle(const QString &namedStyle)
Returns the english named style of a font, if possible.
static QFont createFont(const QString &family, int pointSize=-1, int weight=-1, bool italic=false)
Creates a font with the specified family.
static bool fontFamilyMatchOnSystem(const QString &family, QString *chosen=nullptr, bool *match=nullptr)
Check whether font family is on system.
static bool fontFamilyOnSystem(const QString &family)
Check whether font family is on system in a quick manner, which does not compare [foundry].
static bool updateFontViaStyle(QFont &f, const QString &fontstyle, bool fallback=false)
Updates font with named style and retain all font properties.
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
Perform transforms between map coordinates and device coordinates.
void setParameters(double mapUnitsPerPixel, double centerX, double centerY, int widthPixels, int heightPixels, double rotation)
Sets parameters for use in transforming coordinates.
Struct for storing maximum and minimum scales for measurements in map units.
static Qgis::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a Qgis::BlendMode corresponding to a QPainter::CompositionMode.
static QPainter::CompositionMode getCompositionMode(Qgis::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a Qgis::BlendMode.
@ FontStretchFactor
Font stretch factor, since QGIS 3.24.
@ TabStopDistance
Tab stop distance, since QGIS 3.38.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the labeling property definitions.
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
QSet< QString > referencedFields(const QgsExpressionContext &context=QgsExpressionContext(), bool ignoreContext=false) const final
Returns the set of any fields referenced by the active properties from the collection.
A container for the context for various read/write operations on objects.
void pushMessage(const QString &message, Qgis::MessageLevel level=Qgis::MessageLevel::Warning) const
Append a message to the context.
Contains information about the context of a rendering operation.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
void setDevicePixelRatio(float ratio)
Sets the device pixel ratio.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QgsExpressionContext & expressionContext()
Gets the expression context.
void setRasterizedRenderingPolicy(Qgis::RasterizedRenderingPolicy policy)
Sets the policy controlling when rasterisation of content during renders is permitted.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Stores properties relating to a screen.
double devicePixelRatio() const
Returns the ratio between physical pixels and device-independent pixels for the screen.
bool isValid() const
Returns true if the properties are valid.
void updateRenderContextForScreen(QgsRenderContext &context) const
Updates the settings in a render context to match the screen settings.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static QPainter::CompositionMode decodeBlendMode(const QString &s)
static QString encodeColor(const QColor &color)
Container for settings relating to a text background object.
QSizeF size() const
Returns the size of the background shape.
bool enabled() const
Returns whether the background is enabled.
SizeType sizeType() const
Returns the method used to determine the size of the background shape (e.g., fixed size or buffer aro...
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the shape size.
Qgis::RenderUnit sizeUnit() const
Returns the units used for the shape's size.
Container for settings relating to a text buffer.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
bool enabled() const
Returns whether the buffer is enabled.
Defines a tab position for a text format.
double position() const
Returns the tab position.
Tab(double position)
Constructor for a Tab at the specified position.
QgsTextFormat()
Default constructor for QgsTextFormat.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setBlendMode(QPainter::CompositionMode mode)
Sets the blending mode used for drawing the text.
void setSize(double size)
Sets the size for rendered text.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the size.
void setCapitalization(Qgis::Capitalization capitalization)
Sets the text capitalization style.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the format's property collection, used for data defined overrides.
QStringList families() const
Returns the list of font families to use when restoring the text format, in order of precedence.
void setOrientation(Qgis::TextOrientation orientation)
Sets the orientation for the text.
void setFont(const QFont &font)
Sets the font used for rendering text.
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
QList< QgsTextFormat::Tab > tabPositions() const
Returns the list of tab positions for tab stops.
void setFamilies(const QStringList &families)
Sets a list of font families to use for the text format, in order of precedence.
void setForcedItalic(bool forced)
Sets whether the format is set to force an italic style.
static QgsTextFormat fromMimeData(const QMimeData *data, bool *ok=nullptr)
Attempts to parse the provided mime data as a QgsTextFormat.
double lineHeight() const
Returns the line height for text.
double tabStopDistance() const
Returns the distance for tab stops.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
int stretchFactor() const
Returns the text's stretch factor.
void updateDataDefinedProperties(QgsRenderContext &context)
Updates the format by evaluating current values of data defined properties.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the format's property collection, used for data defined overrides.
void setStretchFactor(int factor)
Sets the text's stretch factor.
void setShadow(const QgsTextShadowSettings &shadowSettings)
Sets the text's drop shadow settings.
void setMask(const QgsTextMaskSettings &maskSettings)
Sets the text's masking settings.
void setPreviewBackgroundColor(const QColor &color)
Sets the background color that text will be rendered on for previews.
QFont scaledFont(const QgsRenderContext &context, double scaleFactor=1.0, bool *isZeroSize=nullptr) const
Returns a font with the size scaled to match the format's size settings (including units and map unit...
void setTabStopDistanceUnit(Qgis::RenderUnit unit)
Sets the unit used for the tab stop distance.
void multiplyOpacity(double opacityFactor)
Multiply opacity by opacityFactor.
void setOpacity(double opacity)
Sets the text's opacity.
QPainter::CompositionMode blendMode() const
Returns the blending mode used for drawing the text.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
bool operator==(const QgsTextFormat &other) const
void setAllowHtmlFormatting(bool allow)
Sets whether text should be treated as a HTML document and HTML tags should be used for formatting th...
void setLineHeightUnit(Qgis::RenderUnit unit)
Sets the unit for the line height for text.
Qgis::RenderUnit lineHeightUnit() const
Returns the units for the line height for text.
Qgis::Capitalization capitalization() const
Returns the text capitalization style.
bool forcedItalic() const
Returns true if the format is set to force an italic style.
QgsTextMaskSettings & mask()
Returns a reference to the masking settings.
void setForcedBold(bool forced)
Sets whether the format is set to force a bold style.
bool isValid() const
Returns true if the format is valid.
void setTabStopDistanceMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the tab stop distance.
bool hasNonDefaultCompositionMode() const
Returns true if any component of the font format requires a non-default composition mode.
void setBuffer(const QgsTextBufferSettings &bufferSettings)
Sets the text's buffer settings.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
Qgis::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
static QgsTextFormat fromQFont(const QFont &font)
Returns a text format matching the settings from an input font.
void setValid()
Sets the format to a valid state, without changing any of the default format settings.
bool allowHtmlFormatting() const
Returns true if text should be treated as a HTML document and HTML tags should be used for formatting...
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
bool operator!=(const QgsTextFormat &other) const
double opacity() const
Returns the text's opacity.
void setTabStopDistance(double distance)
Sets the distance for tab stops.
Qgis::TextOrientation orientation() const
Returns the orientation of the text.
QString namedStyle() const
Returns the named style for the font used for rendering text (e.g., "bold").
double size() const
Returns the size for rendered text.
QgsTextShadowSettings & shadow()
Returns a reference to the text drop shadow settings.
QgsTextFormat & operator=(const QgsTextFormat &other)
void setTabPositions(const QList< QgsTextFormat::Tab > &positions)
Sets the list of tab positions for tab stops.
static QPixmap textFormatPreviewPixmap(const QgsTextFormat &format, QSize size, const QString &previewText=QString(), int padding=0, const QgsScreenProperties &screen=QgsScreenProperties())
Returns a pixmap preview for a text format.
Qgis::RenderUnit tabStopDistanceUnit() const
Returns the units for the tab stop distance.
bool forcedBold() const
Returns true if the format is set to force a bold style.
void setBackground(const QgsTextBackgroundSettings &backgroundSettings)
Sets the text's background settings.q.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
QString asCSS(double pointToPixelMultiplier=1.0) const
Returns a CSS string representing the specified text format as closely as possible.
QMimeData * toMimeData() const
Returns new mime data representing the text format settings.
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the size.
void setNamedStyle(const QString &style)
Sets the named style for the font used for rendering text.
QColor color() const
Returns the color that text will be rendered in.
QFont font() const
Returns the font used for rendering text.
void readFromLayer(QgsVectorLayer *layer)
Reads settings from a layer's custom properties (for QGIS 2.x projects).
QgsMapUnitScale tabStopDistanceMapUnitScale() const
Returns the map unit scale object for the tab stop distance.
QColor previewBackgroundColor() const
Returns the background color for text previews.
bool containsAdvancedEffects() const
Returns true if any component of the font format requires advanced effects such as blend modes,...
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
void setLineHeight(double height)
Sets the line height for text.
Container for settings relating to a selective masking around a text.
static Qgis::TextOrientation decodeTextOrientation(const QString &name, bool *ok=nullptr)
Attempts to decode a string representation of a text orientation.
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.
static QString encodeTextOrientation(Qgis::TextOrientation orientation)
Encodes a text orientation.
static int sizeToPixel(double size, const QgsRenderContext &c, Qgis::RenderUnit unit, const QgsMapUnitScale &mapUnitScale=QgsMapUnitScale())
Calculates pixel size (considering output size should be in pixel or map units, scale factors and opt...
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
Container for settings relating to a text shadow.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based dataset.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975