32#include <QFontDatabase>
38using namespace Qt::StringLiterals;
42 d =
new QgsTextSettingsPrivate();
47 : mBufferSettings( other.mBufferSettings )
48 , mBackgroundSettings( other.mBackgroundSettings )
49 , mShadowSettings( other.mShadowSettings )
50 , mMaskSettings( other.mMaskSettings )
51 , mTextFontFamily( other.mTextFontFamily )
52 , mTextFontFound( other.mTextFontFound )
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 ) )
74 mBufferSettings = other.mBufferSettings;
75 mBackgroundSettings = other.mBackgroundSettings;
76 mShadowSettings = other.mShadowSettings;
77 mMaskSettings = other.mMaskSettings;
78 mTextFontFamily = other.mTextFontFamily;
79 mTextFontFound = other.mTextFontFound;
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;
104 if ( d->isValid != other.
isValid()
105 || d->textFont != other.
font()
107 || d->fontSizeUnits != other.
sizeUnit()
109 || d->fontSize != other.
size()
110 || d->textColor != other.
color()
111 || d->opacity != other.
opacity()
125 || mBufferSettings != other.mBufferSettings
126 || mBackgroundSettings != other.mBackgroundSettings
127 || mShadowSettings != other.mShadowSettings
128 || mMaskSettings != other.mMaskSettings
138 return !( *
this == other );
154 return mBufferSettings;
160 mBufferSettings = bufferSettings;
166 return mBackgroundSettings;
172 mBackgroundSettings = backgroundSettings;
178 return mShadowSettings;
184 mShadowSettings = shadowSettings;
190 return mMaskSettings;
196 mMaskSettings = maskSettings;
209 QFont
font = d->textFont;
210 if ( scaleFactor == 1 )
213 if ( fontPixelSize == 0 )
220 font.setPixelSize( fontPixelSize );
224 double fontPixelSize = context.
convertToPainterUnits( d->fontSize, d->fontSizeUnits, d->fontSizeMapUnitScale );
231 const int roundedPixelSize =
static_cast< int >( std::round( scaleFactor * fontPixelSize + 0.5 ) );
232 font.setPixelSize( roundedPixelSize );
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 );
239 font.setCapitalization( QFont::SmallCaps );
248 d->originalFontFamily.clear();
253 if ( !d->textNamedStyle.isEmpty() )
254 return d->textNamedStyle;
257 return db.styleString( d->textFont );
264 d->textNamedStyle = style;
269 return d->forcedBold;
275 d->textFont.setBold( forced );
276 d->forcedBold =
true;
281 return d->forcedItalic;
287 d->textFont.setItalic( forced );
288 d->forcedItalic =
true;
304 return d->fontSizeUnits;
310 d->fontSizeUnits = unit;
315 return d->fontSizeMapUnitScale;
321 d->fontSizeMapUnitScale = scale;
343 d->textColor =
color;
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 );
370 return d->textFont.stretch() > 0 ? d->textFont.stretch() : 100;
376 d->textFont.setStretch( factor );
392 return d->multilineHeight;
398 d->multilineHeight = height;
403 return d->multilineHeightUnits;
409 d->multilineHeightUnits = unit;
414 return d->tabStopDistance;
420 d->tabStopDistance = distance;
425 return d->tabPositions;
431 d->tabPositions = positions;
436 return d->tabStopDistanceUnits;
442 d->tabStopDistanceUnits = unit;
447 return d->tabStopDistanceMapUnitScale;
453 d->tabStopDistanceMapUnitScale = scale;
458 return d->orientation;
483 return d->allowHtmlFormatting;
489 d->allowHtmlFormatting = allow;
494 return d->previewBackgroundColor;
500 d->previewBackgroundColor =
color;
506 QFont appFont = QApplication::font();
508 mTextFontFamily = d->originalFontFamily;
509 QString fontFamily = mTextFontFamily;
513 mTextFontFound =
false;
519 fontFamily = appFont.family();
523 mTextFontFound =
true;
528 d->fontSize = appFont.pointSizeF();
532 d->fontSize = layer->
customProperty( u
"labeling/fontSize"_s ).toDouble();
535 if ( layer->
customProperty( u
"labeling/fontSizeUnit"_s ).toString().isEmpty() )
546 if ( layer->
customProperty( u
"labeling/fontSizeMapUnitScale"_s ).toString().isEmpty() )
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;
558 int fontWeight = layer->
customProperty( u
"labeling/fontWeight"_s ).toInt();
559 bool fontItalic = layer->
customProperty( u
"labeling/fontItalic"_s ).toBool();
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() );
569 if ( layer->
customProperty( u
"labeling/textOpacity"_s ).toString().isEmpty() )
571 d->opacity = ( 1 - layer->
customProperty( u
"labeling/textTransp"_s ).toInt() / 100.0 );
575 d->opacity = ( layer->
customProperty( u
"labeling/textOpacity"_s ).toDouble() );
578 d->multilineHeight = layer->
customProperty( u
"labeling/multilineHeight"_s, QVariant( 1.0 ) ).toDouble();
581 mBufferSettings.readFromLayer( layer );
582 mShadowSettings.readFromLayer( layer );
583 mBackgroundSettings.readFromLayer( layer );
589 QDomElement textStyleElem;
590 if ( elem.nodeName() ==
"text-style"_L1 )
591 textStyleElem = elem;
593 textStyleElem = elem.firstChildElement( u
"text-style"_s );
594 QFont appFont = QApplication::font();
596 mTextFontFamily = d->originalFontFamily;
597 QString fontFamily = mTextFontFamily;
599 const QDomElement familiesElem = textStyleElem.firstChildElement( u
"families"_s );
600 const QDomNodeList familyNodes = familiesElem.childNodes();
602 families.reserve( familyNodes.size() );
603 for (
int i = 0; i < familyNodes.count(); ++i )
605 const QDomElement familyElem = familyNodes.at( i ).toElement();
606 families << familyElem.attribute( u
"name"_s );
610 mTextFontFound =
false;
616 mTextFontFound =
true;
620 for (
const QString &family : std::as_const(
families ) )
625 mTextFontFound =
true;
626 fontFamily = processedFamily;
631 if ( !mTextFontFound )
634 fontFamily = appFont.family();
640 mTextFontFound =
true;
643 if ( !mTextFontFound )
647 if ( mTextFontFamily.compare(
"MS Shell Dlg"_L1, Qt::CaseInsensitive ) != 0 && mTextFontFamily.compare(
"MS Shell Dlg 2"_L1, Qt::CaseInsensitive ) != 0 )
649 context.
pushMessage( QObject::tr(
"Font “%1” not available on system" ).arg( mTextFontFamily ) );
653 if ( textStyleElem.hasAttribute( u
"fontSize"_s ) )
655 d->fontSize = textStyleElem.attribute( u
"fontSize"_s ).toDouble();
659 d->fontSize = appFont.pointSizeF();
662 if ( !textStyleElem.hasAttribute( u
"fontSizeUnit"_s ) )
671 if ( !textStyleElem.hasAttribute( u
"fontSizeMapUnitScale"_s ) )
674 double oldMin = textStyleElem.attribute( u
"fontSizeMapUnitMinScale"_s, u
"0"_s ).toDouble();
675 d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
676 double oldMax = textStyleElem.attribute( u
"fontSizeMapUnitMaxScale"_s, u
"0"_s ).toDouble();
677 d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
683 int fontWeight = textStyleElem.attribute( u
"fontWeight"_s ).toInt();
684 bool fontItalic = textStyleElem.attribute( u
"fontItalic"_s ).toInt();
686 d->textFont.setPointSizeF( d->fontSize );
689 d->forcedBold = textStyleElem.attribute( u
"forcedBold"_s ).toInt();
690 d->forcedItalic = textStyleElem.attribute( u
"forcedItalic"_s ).toInt();
691 d->textFont.setUnderline( textStyleElem.attribute( u
"fontUnderline"_s ).toInt() );
692 d->textFont.setStrikeOut( textStyleElem.attribute( u
"fontStrikeout"_s ).toInt() );
693 d->textFont.setKerning( textStyleElem.attribute( u
"fontKerning"_s, u
"1"_s ).toInt() );
694 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, textStyleElem.attribute( u
"fontLetterSpacing"_s, u
"0"_s ).toDouble() );
695 d->textFont.setWordSpacing( textStyleElem.attribute( u
"fontWordSpacing"_s, u
"0"_s ).toDouble() );
697 if ( !textStyleElem.hasAttribute( u
"textOpacity"_s ) )
699 d->opacity = ( 1 - textStyleElem.attribute( u
"textTransp"_s ).toInt() / 100.0 );
703 d->opacity = ( textStyleElem.attribute( u
"textOpacity"_s ).toDouble() );
705 if ( textStyleElem.hasAttribute( u
"stretchFactor"_s ) )
707 d->textFont.setStretch( textStyleElem.attribute( u
"stretchFactor"_s, u
"100"_s ).toInt() );
714 if ( !textStyleElem.hasAttribute( u
"multilineHeight"_s ) )
716 QDomElement textFormatElem = elem.firstChildElement( u
"text-format"_s );
717 d->multilineHeight = textFormatElem.attribute( u
"multilineHeight"_s, u
"1"_s ).toDouble();
721 d->multilineHeight = textStyleElem.attribute( u
"multilineHeight"_s, u
"1"_s ).toDouble();
726 d->tabStopDistance = textStyleElem.attribute( u
"tabStopDistance"_s, u
"80"_s ).toDouble();
732 const QDomElement tabPositionsElem = textStyleElem.firstChildElement( u
"tabPositions"_s );
733 const QDomNodeList tabNodes = tabPositionsElem.childNodes();
735 for (
int i = 0; i < tabNodes.count(); ++i )
737 const QDomElement tabElem = tabNodes.at( i ).toElement();
743 if ( textStyleElem.hasAttribute( u
"capitalization"_s ) )
746 d->capitalization =
static_cast< Qgis::Capitalization >( textStyleElem.attribute( u
"fontCapitals"_s, u
"0"_s ).toUInt() );
749 d->textFont.setCapitalization( QFont::SmallCaps );
751 d->allowHtmlFormatting = textStyleElem.attribute( u
"allowHtml"_s, u
"0"_s ).toInt();
753 if ( textStyleElem.firstChildElement( u
"text-buffer"_s ).isNull() )
755 mBufferSettings.readXml( elem );
759 mBufferSettings.readXml( textStyleElem );
761 if ( textStyleElem.firstChildElement( u
"text-mask"_s ).isNull() )
763 mMaskSettings.readXml( elem );
767 mMaskSettings.readXml( textStyleElem );
769 if ( textStyleElem.firstChildElement( u
"shadow"_s ).isNull() )
771 mShadowSettings.readXml( elem );
775 mShadowSettings.readXml( textStyleElem );
777 if ( textStyleElem.firstChildElement( u
"background"_s ).isNull() )
779 mBackgroundSettings.readXml( elem, context );
783 mBackgroundSettings.readXml( textStyleElem, context );
786 QDomElement ddElem = textStyleElem.firstChildElement( u
"dd_properties"_s );
787 if ( ddElem.isNull() )
789 ddElem = elem.firstChildElement( u
"dd_properties"_s );
791 if ( !ddElem.isNull() )
794 mBackgroundSettings.upgradeDataDefinedProperties( d->mDataDefinedProperties );
798 d->mDataDefinedProperties.clear();
805 QDomElement textStyleElem = doc.createElement( u
"text-style"_s );
806 textStyleElem.setAttribute( u
"fontFamily"_s, !d->originalFontFamily.isEmpty() ? d->originalFontFamily : d->textFont.family() );
808 QDomElement familiesElem = doc.createElement( u
"families"_s );
809 for (
const QString &family : std::as_const( d->families ) )
811 QDomElement familyElem = doc.createElement( u
"family"_s );
812 familyElem.setAttribute( u
"name"_s, family );
813 familiesElem.appendChild( familyElem );
815 textStyleElem.appendChild( familiesElem );
818 textStyleElem.setAttribute( u
"fontSize"_s, d->fontSize );
821 textStyleElem.setAttribute( u
"fontWeight"_s, d->textFont.weight() );
822 textStyleElem.setAttribute( u
"fontItalic"_s, d->textFont.italic() );
823 textStyleElem.setAttribute( u
"fontStrikeout"_s, d->textFont.strikeOut() );
824 textStyleElem.setAttribute( u
"fontUnderline"_s, d->textFont.underline() );
825 textStyleElem.setAttribute( u
"forcedBold"_s, d->forcedBold );
826 textStyleElem.setAttribute( u
"forcedItalic"_s, d->forcedItalic );
829 textStyleElem.setAttribute( u
"fontLetterSpacing"_s, d->textFont.letterSpacing() );
830 textStyleElem.setAttribute( u
"fontWordSpacing"_s, d->textFont.wordSpacing() );
831 textStyleElem.setAttribute( u
"fontKerning"_s, d->textFont.kerning() );
832 textStyleElem.setAttribute( u
"textOpacity"_s, d->opacity );
833 if ( d->textFont.stretch() > 0 )
834 textStyleElem.setAttribute( u
"stretchFactor"_s, d->textFont.stretch() );
837 textStyleElem.setAttribute( u
"multilineHeight"_s, d->multilineHeight );
840 textStyleElem.setAttribute( u
"tabStopDistance"_s, d->tabStopDistance );
844 if ( !d->tabPositions.empty() )
846 QDomElement tabPositionsElem = doc.createElement( u
"tabPositions"_s );
847 for (
const Tab &tab : std::as_const( d->tabPositions ) )
849 QDomElement tabElem = doc.createElement( u
"tab"_s );
850 tabElem.setAttribute( u
"position"_s, tab.position() );
851 tabPositionsElem.appendChild( tabElem );
853 textStyleElem.appendChild( tabPositionsElem );
856 textStyleElem.setAttribute( u
"allowHtml"_s, d->allowHtmlFormatting ? u
"1"_s : u
"0"_s );
857 textStyleElem.setAttribute( u
"capitalization"_s, QString::number(
static_cast< int >( d->capitalization ) ) );
859 QDomElement ddElem = doc.createElement( u
"dd_properties"_s );
862 textStyleElem.appendChild( mBufferSettings.writeXml( doc ) );
863 textStyleElem.appendChild( mMaskSettings.writeXml( doc ) );
864 textStyleElem.appendChild( mBackgroundSettings.writeXml( doc, context ) );
865 textStyleElem.appendChild( mShadowSettings.writeXml( doc ) );
866 textStyleElem.appendChild( ddElem );
868 return textStyleElem;
875 QMimeData *mimeData =
new QMimeData;
876 mimeData->setColorData( QVariant(
color() ) );
879 QDomDocument textDoc;
880 QDomElement textElem =
writeXml( textDoc, rwContext );
881 textDoc.appendChild( textElem );
882 mimeData->setText( textDoc.toString() );
891 if (
font.pointSizeF() > 0 )
896 else if (
font.pixelSize() > 0 )
911 f.setPointSizeF(
size() );
915 f.setPointSizeF(
size() * 2.83464567 );
919 f.setPointSizeF(
size() * 72 );
923 f.setPixelSize(
static_cast< int >( std::round(
size() ) ) );
944 QString text = data->text();
945 if ( !text.isEmpty() )
951 if ( doc.setContent( text ) )
953 elem = doc.documentElement();
955 format.
readXml( elem, rwContext );
966 if ( d->blendMode != QPainter::CompositionMode_SourceOver )
969 if ( mBufferSettings.enabled() && mBufferSettings.blendMode() != QPainter::CompositionMode_SourceOver )
972 if ( mBackgroundSettings.enabled() && mBackgroundSettings.blendMode() != QPainter::CompositionMode_SourceOver )
975 if ( mShadowSettings.enabled() && mShadowSettings.blendMode() != QPainter::CompositionMode_SourceOver )
983 if ( d->blendMode != QPainter::CompositionMode_SourceOver )
986 if ( mBufferSettings.enabled() && mBufferSettings.blendMode() != QPainter::CompositionMode_SourceOver )
989 if ( mBackgroundSettings.enabled() && mBackgroundSettings.blendMode() != QPainter::CompositionMode_SourceOver )
992 if ( mShadowSettings.enabled() && mShadowSettings.blendMode() != QPainter::CompositionMode_SourceOver )
1007 return d->mDataDefinedProperties;
1012 return d->mDataDefinedProperties;
1018 fields.unite( mBufferSettings.referencedFields( context ) );
1019 fields.unite( mBackgroundSettings.referencedFields( context ) );
1020 fields.unite( mShadowSettings.referencedFields( context ) );
1021 fields.unite( mMaskSettings.referencedFields( context ) );
1028 d->mDataDefinedProperties = collection;
1034 if ( !d->mDataDefinedProperties.hasActiveProperties() )
1037 QString ddFontFamily;
1042 QString family = exprVal.toString().trimmed();
1044 if ( d->textFont.family() != family )
1050 ddFontFamily = family;
1056 QString ddFontStyle;
1061 QString fontstyle = exprVal.toString().trimmed();
1062 ddFontStyle = fontstyle;
1065 bool ddBold =
false;
1072 bool ddItalic =
false;
1082 QFontDatabase fontDb;
1083 QFont appFont = QApplication::font();
1084 bool newFontBuilt =
false;
1085 if ( ddBold || ddItalic )
1089 newFontBuilt =
true;
1090 newFont.setBold( ddBold );
1091 newFont.setItalic( ddItalic );
1093 else if ( !ddFontStyle.isEmpty() && ddFontStyle.compare(
"Ignore"_L1, Qt::CaseInsensitive ) != 0 )
1095 if ( !ddFontFamily.isEmpty() )
1098 QFont styledfont = fontDb.font( ddFontFamily, ddFontStyle, appFont.pointSize() );
1099 if ( appFont != styledfont )
1101 newFont = styledfont;
1102 newFontBuilt =
true;
1109 else if ( !ddFontFamily.isEmpty() )
1111 if ( ddFontStyle.compare(
"Ignore"_L1, Qt::CaseInsensitive ) != 0 )
1114 QFont styledfont = fontDb.font( ddFontFamily, d->textNamedStyle, appFont.pointSize() );
1115 if ( appFont != styledfont )
1117 newFont = styledfont;
1118 newFontBuilt =
true;
1124 newFontBuilt =
true;
1131 newFont.setUnderline( d->textFont.underline() );
1132 newFont.setStrikeOut( d->textFont.strikeOut() );
1133 newFont.setWordSpacing( d->textFont.wordSpacing() );
1134 newFont.setLetterSpacing( QFont::AbsoluteSpacing, d->textFont.letterSpacing() );
1135 d->textFont = newFont;
1165 QString units = exprVal.toString();
1166 if ( !units.isEmpty() )
1171 d->fontSizeUnits = res;
1181 d->opacity = val.toDouble() / 100.0;
1191 d->textFont.setStretch( val.toInt() );
1208 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, val.toDouble() );
1218 d->textFont.setWordSpacing( val.toDouble() );
1228 if ( val.userType() == QMetaType::Type::QVariantList )
1230 const QVariantList parts = val.toList();
1231 d->tabPositions.clear();
1232 d->tabPositions.reserve( parts.size() );
1233 for (
const QVariant &part : parts )
1235 d->tabPositions.append(
Tab( part.toDouble() ) );
1238 else if ( val.userType() == QMetaType::Type::QStringList )
1240 const QStringList parts = val.toStringList();
1241 d->tabPositions.clear();
1242 d->tabPositions.reserve( parts.size() );
1243 for (
const QString &part : parts )
1245 d->tabPositions.append(
Tab( part.toDouble() ) );
1250 d->tabPositions.clear();
1251 d->tabStopDistance = val.toDouble();
1259 QString blendstr = exprVal.toString().trimmed();
1260 if ( !blendstr.isEmpty() )
1264 mShadowSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1265 mBackgroundSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1266 mBufferSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1267 mMaskSettings.updateDataDefinedProperties( context, d->mDataDefinedProperties );
1274 QPixmap pixmap(
size * devicePixelRatio );
1275 pixmap.fill( Qt::transparent );
1276 pixmap.setDevicePixelRatio( devicePixelRatio );
1279 painter.begin( &pixmap );
1281 painter.setRenderHint( QPainter::Antialiasing );
1283 const QRectF rect( 0, 0,
size.width(),
size.height() );
1286 painter.setPen( Qt::NoPen );
1288 if ( ( background1.lightnessF() < 0.7 ) )
1290 background1 = background1.darker( 125 );
1294 background1 = background1.lighter( 125 );
1297 QLinearGradient linearGrad( QPointF( 0, 0 ), QPointF( 0, rect.height() ) );
1298 linearGrad.setColorAt( 0, background1 );
1299 linearGrad.setColorAt( 1, background2 );
1300 painter.setBrush( QBrush( linearGrad ) );
1301 if (
size.width() > 30 )
1303 painter.drawRoundedRect( rect, 6, 6 );
1308 painter.drawRect( rect );
1310 painter.setBrush( Qt::NoBrush );
1311 painter.setPen( Qt::NoPen );
1325 QWidget *activeWindow = QApplication::activeWindow();
1326 if ( QScreen *screen = activeWindow ? activeWindow->screen() :
nullptr )
1348 ? fontSize * tempFormat.
buffer().
size() / 100
1353 double ytrans = 0.0;
1363 const QStringList text = QStringList() << ( previewText.isEmpty() ? QObject::tr(
"Aa" ) : previewText );
1365 QRectF textRect = rect;
1366 textRect.setLeft( xtrans + padding );
1367 textRect.setWidth( rect.width() - xtrans - 2 * padding );
1369 if ( textRect.width() > 2000 )
1370 textRect.setWidth( 2000 - 2 * padding );
1372 const double bottom = textRect.height() / 2 + textHeight / 2;
1373 textRect.setTop( bottom - textHeight );
1374 textRect.setBottom( bottom );
1379 painter.setBrush( Qt::NoBrush );
1381 if (
size.width() > 30 )
1383 painter.drawRoundedRect( rect, 6, 6 );
1388 painter.drawRect( rect );
1401 css += u
"line-height: %1%;"_s.arg(
lineHeight() * 100 );
1404 css += u
"line-height: %1px;"_s.arg(
lineHeight() );
1408 css += u
"line-height: %1px;"_s.arg(
lineHeight() * pointToPixelMultiplier );
1412 css += u
"line-height: %1px;"_s.arg(
lineHeight() * 2.83464567 * pointToPixelMultiplier );
1420 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 ) );
1424 f.setPointSizeF(
size() / 0.352778 );
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
@ Rectangle
Text within rectangle layout mode.
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Capitalization
String capitalization options.
@ AllSmallCaps
Force all characters to small caps.
@ MixedCase
Mixed case, ie no change.
@ SmallCaps
Mixed case small caps.
TextOrientation
Text orientations.
RenderUnit
Rendering size units.
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes).
@ Unknown
Mixed or unknown units.
@ MetersInMapUnits
Meters value as Map units.
@ Antialiasing
Use antialiasing while drawing.
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.
@ FontSizeUnit
Font size units.
@ FontStyle
Font style name.
@ Italic
Use italic style.
@ FontStretchFactor
Font stretch factor, since QGIS 3.24.
@ Strikeout
Use strikeout.
@ Underline
Use underline.
@ FontBlendMode
Text blend mode.
@ FontOpacity
Text opacity.
@ FontWordSpacing
Word spacing.
@ FontLetterSpacing
Letter spacing.
@ 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).