@@ -2649,17 +2649,19 @@ void QgsTextRenderer::drawText( const QRectF &rect, double rotation, QgsTextRend
2649
2649
tmpFormat.updateDataDefinedProperties ( context );
2650
2650
tmpFormat = updateShadowPosition ( tmpFormat );
2651
2651
2652
+ const QgsTextDocument document = format.allowHtmlFormatting () ? QgsTextDocument::fromHtml ( textLines ) : QgsTextDocument::fromPlainText ( textLines );
2653
+
2652
2654
if ( tmpFormat.background ().enabled () )
2653
2655
{
2654
- drawPart ( rect, rotation, alignment, textLines , context, tmpFormat, Background );
2656
+ drawPart ( rect, rotation, alignment, document , context, tmpFormat, Background );
2655
2657
}
2656
2658
2657
2659
if ( tmpFormat.buffer ().enabled () )
2658
2660
{
2659
- drawPart ( rect, rotation, alignment, textLines , context, tmpFormat, Buffer );
2661
+ drawPart ( rect, rotation, alignment, document , context, tmpFormat, Buffer );
2660
2662
}
2661
2663
2662
- drawPart ( rect, rotation, alignment, textLines , context, tmpFormat, Text );
2664
+ drawPart ( rect, rotation, alignment, document , context, tmpFormat, Text );
2663
2665
}
2664
2666
2665
2667
void QgsTextRenderer::drawText ( QPointF point, double rotation, QgsTextRenderer::HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool )
@@ -2669,17 +2671,19 @@ void QgsTextRenderer::drawText( QPointF point, double rotation, QgsTextRenderer:
2669
2671
tmpFormat.updateDataDefinedProperties ( context );
2670
2672
tmpFormat = updateShadowPosition ( tmpFormat );
2671
2673
2674
+ const QgsTextDocument document = format.allowHtmlFormatting () ? QgsTextDocument::fromHtml ( textLines ) : QgsTextDocument::fromPlainText ( textLines );
2675
+
2672
2676
if ( tmpFormat.background ().enabled () )
2673
2677
{
2674
- drawPart ( point, rotation, alignment, textLines , context, tmpFormat, Background );
2678
+ drawPart ( point, rotation, alignment, document , context, tmpFormat, Background );
2675
2679
}
2676
2680
2677
2681
if ( tmpFormat.buffer ().enabled () )
2678
2682
{
2679
- drawPart ( point, rotation, alignment, textLines , context, tmpFormat, Buffer );
2683
+ drawPart ( point, rotation, alignment, document , context, tmpFormat, Buffer );
2680
2684
}
2681
2685
2682
- drawPart ( point, rotation, alignment, textLines , context, tmpFormat, Text );
2686
+ drawPart ( point, rotation, alignment, document , context, tmpFormat, Text );
2683
2687
}
2684
2688
2685
2689
QgsTextFormat QgsTextRenderer::updateShadowPosition ( const QgsTextFormat &format )
@@ -2706,7 +2710,9 @@ QgsTextFormat QgsTextRenderer::updateShadowPosition( const QgsTextFormat &format
2706
2710
void QgsTextRenderer::drawPart ( const QRectF &rect, double rotation, HAlignment alignment,
2707
2711
const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, QgsTextRenderer::TextPart part, bool )
2708
2712
{
2709
- drawPart ( rect, rotation, alignment, QgsTextDocument ( textLines ), context, format, part );
2713
+ const QgsTextDocument document = format.allowHtmlFormatting () ? QgsTextDocument::fromHtml ( textLines ) : QgsTextDocument::fromPlainText ( textLines );
2714
+
2715
+ drawPart ( rect, rotation, alignment, document, context, format, part );
2710
2716
}
2711
2717
2712
2718
void QgsTextRenderer::drawPart ( const QRectF &rect, double rotation, QgsTextRenderer::HAlignment alignment, const QgsTextDocument &document, QgsRenderContext &context, const QgsTextFormat &format, QgsTextRenderer::TextPart part )
@@ -2775,7 +2781,8 @@ void QgsTextRenderer::drawPart( const QRectF &rect, double rotation, QgsTextRend
2775
2781
2776
2782
void QgsTextRenderer::drawPart ( QPointF origin, double rotation, QgsTextRenderer::HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, QgsTextRenderer::TextPart part, bool )
2777
2783
{
2778
- drawPart ( origin, rotation, alignment, QgsTextDocument ( textLines ), context, format, part );
2784
+ const QgsTextDocument document = format.allowHtmlFormatting () ? QgsTextDocument::fromHtml ( textLines ) : QgsTextDocument::fromPlainText ( textLines );
2785
+ drawPart ( origin, rotation, alignment, document, context, format, part );
2779
2786
}
2780
2787
2781
2788
void QgsTextRenderer::drawPart ( QPointF origin, double rotation, QgsTextRenderer::HAlignment alignment, const QgsTextDocument &document, QgsRenderContext &context, const QgsTextFormat &format, QgsTextRenderer::TextPart part )
@@ -3795,25 +3802,30 @@ void QgsTextRenderer::drawTextInternal( TextPart drawType,
3795
3802
QPainter textp;
3796
3803
textp.begin ( &textPict );
3797
3804
textp.setPen ( Qt::NoPen );
3798
- QFont font = format.scaledFont ( context );
3805
+ const QFont font = format.scaledFont ( context );
3799
3806
3800
3807
double xOffset = 0 ;
3801
3808
for ( const QgsTextFragment &fragment : block )
3802
3809
{
3803
3810
// draw text, QPainterPath method
3804
3811
QPainterPath path;
3805
3812
path.setFillRule ( Qt::WindingFill );
3806
- path.addText ( xOffset, 0 , font, fragment.text () );
3813
+
3814
+ QFont fragmentFont = font;
3815
+ fragment.characterFormat ().updateFontForFormat ( fragmentFont );
3816
+ QFontMetricsF fragmentMetrics = QFontMetricsF ( fragmentFont );
3817
+
3818
+ path.addText ( xOffset, 0 , fragmentFont, fragment.text () );
3807
3819
3808
3820
QColor textColor = fragment.characterFormat ().textColor ().isValid () ? fragment.characterFormat ().textColor () : format.color ();
3809
3821
textColor.setAlphaF ( format.opacity () );
3810
3822
textp.setBrush ( textColor );
3811
3823
textp.drawPath ( path );
3812
3824
3813
3825
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
3814
- xOffset += fontMetrics-> width ( fragment.text () );
3826
+ xOffset += fragmentMetrics. width ( fragment.text () );
3815
3827
#else
3816
- xOffset += fontMetrics-> horizontalAdvance ( fragment.text () );
3828
+ xOffset += fragmentMetrics. horizontalAdvance ( fragment.text () );
3817
3829
#endif
3818
3830
// TODO: why are some font settings lost on drawPicture() when using drawText() inside QPicture?
3819
3831
// e.g. some capitalization options, but not others
@@ -4201,6 +4213,13 @@ QgsTextFormat::TextOrientation QgsTextRendererUtils::decodeTextOrientation( cons
4201
4213
4202
4214
QgsTextCharacterFormat::QgsTextCharacterFormat ( const QTextCharFormat &format )
4203
4215
: mTextColor( format.hasProperty( QTextFormat::ForegroundBrush ) ? format.foreground().color() : QColor() )
4216
+ #if 0 // settings which affect font metrics are disabled for now
4217
+ , mFontWeight( format.hasProperty( QTextFormat::FontWeight ) ? format.fontWeight() : -1 )
4218
+ , mItalic( format.hasProperty( QTextFormat::FontItalic ) ? ( format.fontItalic() ? BooleanValue::True : BooleanValue::False ) : BooleanValue::NotSet )
4219
+ #endif
4220
+ , mStrikethrough ( format.hasProperty( QTextFormat::FontStrikeOut ) ? ( format.fontStrikeOut() ? BooleanValue::True : BooleanValue::False ) : BooleanValue::NotSet )
4221
+ , mUnderline ( format.hasProperty( QTextFormat::FontUnderline ) ? ( format.fontUnderline() ? BooleanValue::True : BooleanValue::False ) : BooleanValue::NotSet )
4222
+ , mOverline ( format.hasProperty( QTextFormat::FontOverline ) ? ( format.fontOverline() ? BooleanValue::True : BooleanValue::False ) : BooleanValue::NotSet )
4204
4223
{
4205
4224
4206
4225
}
@@ -4215,6 +4234,75 @@ void QgsTextCharacterFormat::setTextColor( const QColor &textColor )
4215
4234
mTextColor = textColor;
4216
4235
}
4217
4236
4237
+ QgsTextCharacterFormat::BooleanValue QgsTextCharacterFormat::strikeOut () const
4238
+ {
4239
+ return mStrikethrough ;
4240
+ }
4241
+
4242
+ void QgsTextCharacterFormat::setStrikeOut ( BooleanValue strikethrough )
4243
+ {
4244
+ mStrikethrough = strikethrough;
4245
+ }
4246
+
4247
+ QgsTextCharacterFormat::BooleanValue QgsTextCharacterFormat::underline () const
4248
+ {
4249
+ return mUnderline ;
4250
+ }
4251
+
4252
+ void QgsTextCharacterFormat::setUnderline ( BooleanValue underline )
4253
+ {
4254
+ mUnderline = underline;
4255
+ }
4256
+
4257
+ QgsTextCharacterFormat::BooleanValue QgsTextCharacterFormat::overline () const
4258
+ {
4259
+ return mOverline ;
4260
+ }
4261
+
4262
+ void QgsTextCharacterFormat::setOverline ( QgsTextCharacterFormat::BooleanValue enabled )
4263
+ {
4264
+ mOverline = enabled;
4265
+ }
4266
+
4267
+ void QgsTextCharacterFormat::updateFontForFormat ( QFont &font ) const
4268
+ {
4269
+ if ( mUnderline != BooleanValue::NotSet )
4270
+ font.setUnderline ( mUnderline == QgsTextCharacterFormat::BooleanValue::True );
4271
+ if ( mOverline != BooleanValue::NotSet )
4272
+ font.setOverline ( mOverline == QgsTextCharacterFormat::BooleanValue::True );
4273
+ if ( mStrikethrough != QgsTextCharacterFormat::BooleanValue::NotSet )
4274
+ font.setStrikeOut ( mStrikethrough == QgsTextCharacterFormat::BooleanValue::True );
4275
+
4276
+ #if 0 // settings which affect font metrics are disabled for now
4277
+ if ( mItalic != QgsTextCharacterFormat::BooleanValue::NotSet )
4278
+ font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::True );
4279
+ if ( mFontWeight != -1 )
4280
+ font.setWeight( mFontWeight );
4281
+ #endif
4282
+ }
4283
+
4284
+ #if 0 // settings which affect font metrics are disabled for now
4285
+ QgsTextCharacterFormat::BooleanValue QgsTextCharacterFormat::italic() const
4286
+ {
4287
+ return mItalic;
4288
+ }
4289
+
4290
+ void QgsTextCharacterFormat::setItalic( QgsTextCharacterFormat::BooleanValue enabled )
4291
+ {
4292
+ mItalic = enabled;
4293
+ }
4294
+
4295
+ int QgsTextCharacterFormat::fontWeight() const
4296
+ {
4297
+ return mFontWeight;
4298
+ }
4299
+
4300
+ void QgsTextCharacterFormat::setFontWeight( int fontWeight )
4301
+ {
4302
+ mFontWeight = fontWeight;
4303
+ }
4304
+ #endif
4305
+
4218
4306
//
4219
4307
// QgsTextFragment
4220
4308
//
@@ -4223,6 +4311,13 @@ QgsTextFragment::QgsTextFragment( const QString &text, const QgsTextCharacterFor
4223
4311
, mCharFormat( format )
4224
4312
{}
4225
4313
4314
+ QgsTextFragment::QgsTextFragment ( const QTextFragment &fragment )
4315
+ : mText( fragment.text() )
4316
+ , mCharFormat( QgsTextCharacterFormat( fragment.charFormat() ) )
4317
+ {
4318
+
4319
+ }
4320
+
4226
4321
QString QgsTextFragment::text () const
4227
4322
{
4228
4323
return mText ;
@@ -4259,11 +4354,48 @@ QgsTextDocument::QgsTextDocument( const QgsTextFragment &fragment )
4259
4354
append ( QgsTextBlock ( fragment ) );
4260
4355
}
4261
4356
4262
- QgsTextDocument::QgsTextDocument ( const QStringList &lines )
4357
+ QgsTextDocument QgsTextDocument::fromPlainText ( const QStringList &lines )
4263
4358
{
4264
- reserve ( lines.size () );
4359
+ QgsTextDocument document;
4360
+ document.reserve ( lines.size () );
4265
4361
for ( const QString &line : lines )
4266
- append ( QgsTextBlock ( QgsTextFragment ( line ) ) );
4362
+ document.append ( QgsTextBlock ( QgsTextFragment ( line ) ) );
4363
+ return document;
4364
+ }
4365
+
4366
+ QgsTextDocument QgsTextDocument::fromHtml ( const QStringList &lines )
4367
+ {
4368
+ QgsTextDocument document;
4369
+
4370
+ document.reserve ( lines.size () );
4371
+ for ( const QString &line : lines )
4372
+ {
4373
+ QTextDocument sourceDoc;
4374
+ sourceDoc.setHtml ( line );
4375
+
4376
+ QTextBlock sourceBlock = sourceDoc.firstBlock ();
4377
+ while ( true )
4378
+ {
4379
+ auto it = sourceBlock.begin ();
4380
+ QgsTextBlock block;
4381
+ while ( !it.atEnd () )
4382
+ {
4383
+ const QTextFragment fragment = it.fragment ();
4384
+ if ( fragment.isValid () )
4385
+ {
4386
+ block.append ( QgsTextFragment ( fragment ) );
4387
+ }
4388
+ it++;
4389
+ }
4390
+ if ( !block.isEmpty () )
4391
+ document << block;
4392
+
4393
+ sourceBlock = sourceBlock.next ();
4394
+ if ( !sourceBlock.isValid () )
4395
+ break ;
4396
+ }
4397
+ }
4398
+ return document;
4267
4399
}
4268
4400
4269
4401
QStringList QgsTextDocument::toPlainText () const
0 commit comments