@@ -101,6 +101,8 @@ QImage QgsSvgCache::svgAsImage( const QString &file, double size, const QColor &
101
101
fitsInCache = true ;
102
102
QgsSvgCacheEntry *currentEntry = cacheEntry ( file, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio );
103
103
104
+ QImage result;
105
+
104
106
// if current entry image is 0: cache image for entry
105
107
// checks to see if image will fit into cache
106
108
// update stats for memory usage
@@ -134,15 +136,23 @@ QImage QgsSvgCache::svgAsImage( const QString &file, double size, const QColor &
134
136
{
135
137
cachePicture ( currentEntry, false );
136
138
}
139
+
140
+ // ...and render cached picture to result image
141
+ result = imageFromCachedPicture ( *currentEntry );
137
142
}
138
143
else
139
144
{
140
145
cacheImage ( currentEntry );
146
+ result = *( currentEntry->image );
141
147
}
142
148
trimToMaximumSize ();
143
149
}
150
+ else
151
+ {
152
+ result = *( currentEntry->image );
153
+ }
144
154
145
- return *( currentEntry-> image ) ;
155
+ return result ;
146
156
}
147
157
148
158
QPicture QgsSvgCache::svgAsPicture ( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
@@ -479,47 +489,25 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry *entry )
479
489
delete entry->image ;
480
490
entry->image = nullptr ;
481
491
482
- bool isFixedAR = entry->fixedAspectRatio > 0 ;
492
+ QSizeF viewBoxSize;
493
+ QSizeF scaledSize;
494
+ QSize imageSize = sizeForImage ( *entry, viewBoxSize, scaledSize );
483
495
484
- QSvgRenderer r ( entry->svgContent );
485
- double hwRatio = 1.0 ;
486
- if ( r.viewBoxF ().width () > 0 )
487
- {
488
- if ( isFixedAR )
489
- {
490
- hwRatio = entry->fixedAspectRatio ;
491
- }
492
- else
493
- {
494
- hwRatio = r.viewBoxF ().height () / r.viewBoxF ().width ();
495
- }
496
- }
497
- double wSize = entry->size ;
498
- int wImgSize = static_cast < int >( wSize );
499
- if ( wImgSize < 1 )
500
- {
501
- wImgSize = 1 ;
502
- }
503
- double hSize = wSize * hwRatio;
504
- int hImgSize = static_cast < int >( hSize );
505
- if ( hImgSize < 1 )
506
- {
507
- hImgSize = 1 ;
508
- }
509
496
// cast double image sizes to int for QImage
510
- QImage *image = new QImage ( wImgSize, hImgSize , QImage::Format_ARGB32_Premultiplied );
497
+ QImage *image = new QImage ( imageSize , QImage::Format_ARGB32_Premultiplied );
511
498
image->fill ( 0 ); // transparent background
512
499
513
500
QPainter p ( image );
514
- if ( qgsDoubleNear ( r.viewBoxF ().width (), r.viewBoxF ().height () ) )
501
+ QSvgRenderer r ( entry->svgContent );
502
+ if ( qgsDoubleNear ( viewBoxSize.width (), viewBoxSize.height () ) )
515
503
{
516
504
r.render ( &p );
517
505
}
518
506
else
519
507
{
520
- QSizeF s ( r. viewBoxF (). size () );
521
- s.scale ( wSize, hSize , Qt::KeepAspectRatio );
522
- QRectF rect ( ( wImgSize - s.width () ) / 2 , ( hImgSize - s.height () ) / 2 , s.width (), s.height () );
508
+ QSizeF s ( viewBoxSize );
509
+ s.scale ( scaledSize. width (), scaledSize. height () , Qt::KeepAspectRatio );
510
+ QRectF rect ( ( imageSize. width () - s.width () ) / 2 , ( imageSize. height () - s.height () ) / 2 , s.width (), s.height () );
523
511
r.render ( &p, rect );
524
512
}
525
513
@@ -906,6 +894,53 @@ void QgsSvgCache::printEntryList()
906
894
}
907
895
}
908
896
897
+ QSize QgsSvgCache::sizeForImage ( const QgsSvgCacheEntry &entry, QSizeF &viewBoxSize, QSizeF &scaledSize ) const
898
+ {
899
+ bool isFixedAR = entry.fixedAspectRatio > 0 ;
900
+
901
+ QSvgRenderer r ( entry.svgContent );
902
+ double hwRatio = 1.0 ;
903
+ viewBoxSize = r.viewBoxF ().size ();
904
+ if ( viewBoxSize.width () > 0 )
905
+ {
906
+ if ( isFixedAR )
907
+ {
908
+ hwRatio = entry.fixedAspectRatio ;
909
+ }
910
+ else
911
+ {
912
+ hwRatio = viewBoxSize.height () / viewBoxSize.width ();
913
+ }
914
+ }
915
+
916
+ // cast double image sizes to int for QImage
917
+ scaledSize.setWidth ( entry.size );
918
+ int wImgSize = static_cast < int >( scaledSize.width () );
919
+ if ( wImgSize < 1 )
920
+ {
921
+ wImgSize = 1 ;
922
+ }
923
+ scaledSize.setHeight ( scaledSize.width () * hwRatio );
924
+ int hImgSize = static_cast < int >( scaledSize.height () );
925
+ if ( hImgSize < 1 )
926
+ {
927
+ hImgSize = 1 ;
928
+ }
929
+ return QSize ( wImgSize, hImgSize );
930
+ }
931
+
932
+ QImage QgsSvgCache::imageFromCachedPicture ( const QgsSvgCacheEntry &entry ) const
933
+ {
934
+ QSizeF viewBoxSize;
935
+ QSizeF scaledSize;
936
+ QImage image ( sizeForImage ( entry, viewBoxSize, scaledSize ), QImage::Format_ARGB32_Premultiplied );
937
+ image.fill ( 0 ); // transparent background
938
+
939
+ QPainter p ( &image );
940
+ p.drawPicture ( QPoint ( 0 , 0 ), *entry.picture );
941
+ return image;
942
+ }
943
+
909
944
void QgsSvgCache::trimToMaximumSize ()
910
945
{
911
946
// only one entry in cache
0 commit comments