@@ -43,11 +43,12 @@ QgsSvgCacheEntry::QgsSvgCacheEntry()
43
43
{
44
44
}
45
45
46
- QgsSvgCacheEntry::QgsSvgCacheEntry ( const QString &p, double s, double ow, double wsf, const QColor &fi, const QColor &ou )
46
+ QgsSvgCacheEntry::QgsSvgCacheEntry ( const QString &p, double s, double ow, double wsf, const QColor &fi, const QColor &ou, double far )
47
47
: path( p )
48
48
, size( s )
49
49
, strokeWidth( ow )
50
50
, widthScaleFactor( wsf )
51
+ , fixedAspectRatio( far )
51
52
, fill( fi )
52
53
, stroke( ou )
53
54
{
@@ -93,12 +94,12 @@ QgsSvgCache::~QgsSvgCache()
93
94
94
95
95
96
QImage QgsSvgCache::svgAsImage ( const QString &file, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
96
- double widthScaleFactor, bool &fitsInCache )
97
+ double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio )
97
98
{
98
99
QMutexLocker locker ( &mMutex );
99
100
100
101
fitsInCache = true ;
101
- QgsSvgCacheEntry *currentEntry = cacheEntry ( file, size, fill, stroke, strokeWidth, widthScaleFactor );
102
+ QgsSvgCacheEntry *currentEntry = cacheEntry ( file, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio );
102
103
103
104
// if current entry image is 0: cache image for entry
104
105
// checks to see if image will fit into cache
@@ -109,7 +110,14 @@ QImage QgsSvgCache::svgAsImage( const QString &file, double size, const QColor &
109
110
double hwRatio = 1.0 ;
110
111
if ( r.viewBoxF ().width () > 0 )
111
112
{
112
- hwRatio = r.viewBoxF ().height () / r.viewBoxF ().width ();
113
+ if ( currentEntry->fixedAspectRatio > 0 )
114
+ {
115
+ hwRatio = currentEntry->fixedAspectRatio ;
116
+ }
117
+ else
118
+ {
119
+ hwRatio = r.viewBoxF ().height () / r.viewBoxF ().width ();
120
+ }
113
121
}
114
122
long cachedDataSize = 0 ;
115
123
cachedDataSize += currentEntry->svgContent .size ();
@@ -138,11 +146,11 @@ QImage QgsSvgCache::svgAsImage( const QString &file, double size, const QColor &
138
146
}
139
147
140
148
QPicture QgsSvgCache::svgAsPicture ( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
141
- double widthScaleFactor, bool forceVectorOutput )
149
+ double widthScaleFactor, bool forceVectorOutput, double fixedAspectRatio )
142
150
{
143
151
QMutexLocker locker ( &mMutex );
144
152
145
- QgsSvgCacheEntry *currentEntry = cacheEntry ( path, size, fill, stroke, strokeWidth, widthScaleFactor );
153
+ QgsSvgCacheEntry *currentEntry = cacheEntry ( path, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio );
146
154
147
155
// if current entry picture is 0: cache picture for entry
148
156
// update stats for memory usage
@@ -156,28 +164,28 @@ QPicture QgsSvgCache::svgAsPicture( const QString &path, double size, const QCol
156
164
}
157
165
158
166
QByteArray QgsSvgCache::svgContent ( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
159
- double widthScaleFactor )
167
+ double widthScaleFactor, double fixedAspectRatio )
160
168
{
161
169
QMutexLocker locker ( &mMutex );
162
170
163
- QgsSvgCacheEntry *currentEntry = cacheEntry ( path, size, fill, stroke, strokeWidth, widthScaleFactor );
171
+ QgsSvgCacheEntry *currentEntry = cacheEntry ( path, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio );
164
172
165
173
return currentEntry->svgContent ;
166
174
}
167
175
168
- QSizeF QgsSvgCache::svgViewboxSize ( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor )
176
+ QSizeF QgsSvgCache::svgViewboxSize ( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio )
169
177
{
170
178
QMutexLocker locker ( &mMutex );
171
179
172
- QgsSvgCacheEntry *currentEntry = cacheEntry ( path, size, fill, stroke, strokeWidth, widthScaleFactor );
180
+ QgsSvgCacheEntry *currentEntry = cacheEntry ( path, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio );
173
181
174
182
return currentEntry->viewboxSize ;
175
183
}
176
184
177
185
QgsSvgCacheEntry *QgsSvgCache::insertSvg ( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
178
- double widthScaleFactor )
186
+ double widthScaleFactor, double fixedAspectRatio )
179
187
{
180
- QgsSvgCacheEntry *entry = new QgsSvgCacheEntry ( path, size, strokeWidth, widthScaleFactor, fill, stroke );
188
+ QgsSvgCacheEntry *entry = new QgsSvgCacheEntry ( path, size, strokeWidth, widthScaleFactor, fill, stroke, fixedAspectRatio );
181
189
182
190
replaceParamsAndCacheSvg ( entry );
183
191
@@ -471,11 +479,20 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry *entry )
471
479
delete entry->image ;
472
480
entry->image = nullptr ;
473
481
482
+ bool isFixedAR = entry->fixedAspectRatio > 0 ;
483
+
474
484
QSvgRenderer r ( entry->svgContent );
475
485
double hwRatio = 1.0 ;
476
486
if ( r.viewBoxF ().width () > 0 )
477
487
{
478
- hwRatio = r.viewBoxF ().height () / r.viewBoxF ().width ();
488
+ if ( isFixedAR )
489
+ {
490
+ hwRatio = entry->fixedAspectRatio ;
491
+ }
492
+ else
493
+ {
494
+ hwRatio = r.viewBoxF ().height () / r.viewBoxF ().width ();
495
+ }
479
496
}
480
497
double wSize = entry->size ;
481
498
int wImgSize = static_cast < int >( wSize );
@@ -521,20 +538,30 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput
521
538
delete entry->picture ;
522
539
entry->picture = nullptr ;
523
540
541
+ bool isFixedAR = entry->fixedAspectRatio > 0 ;
542
+
524
543
// correct QPictures dpi correction
525
544
QPicture *picture = new QPicture ();
526
545
QRectF rect;
527
546
QSvgRenderer r ( entry->svgContent );
528
547
double hwRatio = 1.0 ;
529
548
if ( r.viewBoxF ().width () > 0 )
530
549
{
531
- hwRatio = r.viewBoxF ().height () / r.viewBoxF ().width ();
550
+ if ( isFixedAR )
551
+ {
552
+ hwRatio = entry->fixedAspectRatio ;
553
+ }
554
+ else
555
+ {
556
+ hwRatio = r.viewBoxF ().height () / r.viewBoxF ().width ();
557
+ }
532
558
}
533
559
534
560
double wSize = entry->size ;
535
561
double hSize = wSize * hwRatio;
562
+
536
563
QSizeF s ( r.viewBoxF ().size () );
537
- s.scale ( wSize, hSize, Qt::KeepAspectRatio );
564
+ s.scale ( wSize, hSize, isFixedAR ? Qt::IgnoreAspectRatio : Qt::KeepAspectRatio );
538
565
rect = QRectF ( -s.width () / 2.0 , -s.height () / 2.0 , s.width (), s.height () );
539
566
540
567
QPainter p ( picture );
@@ -544,7 +571,7 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput
544
571
}
545
572
546
573
QgsSvgCacheEntry *QgsSvgCache::cacheEntry ( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
547
- double widthScaleFactor )
574
+ double widthScaleFactor, double fixedAspectRatio )
548
575
{
549
576
// search entries in mEntryLookup
550
577
QgsSvgCacheEntry *currentEntry = nullptr ;
@@ -555,7 +582,8 @@ QgsSvgCacheEntry *QgsSvgCache::cacheEntry( const QString &path, double size, con
555
582
{
556
583
QgsSvgCacheEntry *cacheEntry = *entryIt;
557
584
if ( qgsDoubleNear ( cacheEntry->size , size ) && cacheEntry->fill == fill && cacheEntry->stroke == stroke &&
558
- qgsDoubleNear ( cacheEntry->strokeWidth , strokeWidth ) && qgsDoubleNear ( cacheEntry->widthScaleFactor , widthScaleFactor ) )
585
+ qgsDoubleNear ( cacheEntry->strokeWidth , strokeWidth ) && qgsDoubleNear ( cacheEntry->widthScaleFactor , widthScaleFactor ) &&
586
+ qgsDoubleNear ( cacheEntry->fixedAspectRatio , fixedAspectRatio ))
559
587
{
560
588
currentEntry = cacheEntry;
561
589
break ;
@@ -566,7 +594,7 @@ QgsSvgCacheEntry *QgsSvgCache::cacheEntry( const QString &path, double size, con
566
594
// cache and replace params in svg content
567
595
if ( !currentEntry )
568
596
{
569
- currentEntry = insertSvg ( path, size, fill, stroke, strokeWidth, widthScaleFactor );
597
+ currentEntry = insertSvg ( path, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio );
570
598
}
571
599
else
572
600
{
0 commit comments