Skip to content

Commit 936b650

Browse files
committed
Add method to QgsSvgCache to return SVG viewbox size
1 parent 709b47e commit 936b650

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

python/core/symbology-ng/qgssvgcache.sip

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class QgsSvgCacheEntry
2727
double outlineWidth;
2828
double widthScaleFactor;
2929
double rasterScaleFactor;
30+
31+
/** SVG viewbox size.
32+
* @note added in QGIS 2.14
33+
*/
34+
QSizeF viewboxSize;
35+
3036
QColor fill;
3137
QColor outline;
3238
QImage* image;
@@ -83,6 +89,20 @@ class QgsSvgCache : QObject
8389
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
8490
double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
8591

92+
/** Calculates the viewbox size of a (possibly cached) SVG file.
93+
* @param file Absolute or relative path to SVG file.
94+
* @param size size of cached image
95+
* @param fill color of fill
96+
* @param outline color of outline
97+
* @param outlineWidth width of outline
98+
* @param widthScaleFactor width scale factor
99+
* @param rasterScaleFactor raster scale factor
100+
* @returns viewbox size set in SVG file
101+
* @note added in QGIS 2.14
102+
*/
103+
QSizeF svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
104+
double widthScaleFactor, double rasterScaleFactor );
105+
86106
/** Tests if an svg file contains parameters for fill, outline color, outline width. If yes, possible default values are returned. If there are several
87107
default values in the svg file, only the first one is considered*/
88108
void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,

src/core/symbology-ng/qgssvgcache.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ const QByteArray& QgsSvgCache::svgContent( const QString& file, double size, con
192192
return currentEntry->svgContent;
193193
}
194194

195+
QSizeF QgsSvgCache::svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth, double widthScaleFactor, double rasterScaleFactor )
196+
{
197+
QMutexLocker locker( &mMutex );
198+
199+
QgsSvgCacheEntry *currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
200+
201+
return currentEntry->viewboxSize;
202+
}
203+
195204
QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
196205
double widthScaleFactor, double rasterScaleFactor )
197206
{
@@ -280,14 +289,16 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
280289
//replace fill color, outline color, outline with in all nodes
281290
QDomElement docElem = svgDoc.documentElement();
282291

283-
double sizeScaleFactor = calcSizeScaleFactor( entry, docElem );
292+
QSizeF viewboxSize;
293+
double sizeScaleFactor = calcSizeScaleFactor( entry, docElem, viewboxSize );
294+
entry->viewboxSize = viewboxSize;
284295
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth * sizeScaleFactor );
285296

286297
entry->svgContent = svgDoc.toByteArray();
287298
mTotalSize += entry->svgContent.size();
288299
}
289300

290-
double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem ) const
301+
double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem, QSizeF& viewboxSize ) const
291302
{
292303
QString viewBox;
293304

@@ -319,11 +330,16 @@ double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElem
319330
if ( parts.count() != 4 )
320331
return 1.0;
321332

333+
bool heightOk = false;
334+
double height = parts.at( 3 ).toDouble( &heightOk );
335+
322336
bool widthOk = false;
323337
double width = parts.at( 2 ).toDouble( &widthOk );
324338
if ( widthOk )
325339
{
326-
return width / entry->size ;
340+
if ( heightOk )
341+
viewboxSize = QSizeF( width, height );
342+
return width / entry->size;
327343
}
328344

329345
return 1.0;

src/core/symbology-ng/qgssvgcache.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QString>
2626
#include <QUrl>
2727
#include <QObject>
28+
#include <QSizeF>
2829

2930
class QDomElement;
3031
class QImage;
@@ -55,6 +56,12 @@ class CORE_EXPORT QgsSvgCacheEntry
5556
double outlineWidth;
5657
double widthScaleFactor;
5758
double rasterScaleFactor;
59+
60+
/** SVG viewbox size.
61+
* @note added in QGIS 2.14
62+
*/
63+
QSizeF viewboxSize;
64+
5865
QColor fill;
5966
QColor outline;
6067
QImage* image;
@@ -109,6 +116,20 @@ class CORE_EXPORT QgsSvgCache : public QObject
109116
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
110117
double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );
111118

119+
/** Calculates the viewbox size of a (possibly cached) SVG file.
120+
* @param file Absolute or relative path to SVG file.
121+
* @param size size of cached image
122+
* @param fill color of fill
123+
* @param outline color of outline
124+
* @param outlineWidth width of outline
125+
* @param widthScaleFactor width scale factor
126+
* @param rasterScaleFactor raster scale factor
127+
* @returns viewbox size set in SVG file
128+
* @note added in QGIS 2.14
129+
*/
130+
QSizeF svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
131+
double widthScaleFactor, double rasterScaleFactor );
132+
112133
/** Tests if an svg file contains parameters for fill, outline color, outline width. If yes, possible default values are returned. If there are several
113134
default values in the svg file, only the first one is considered*/
114135
void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
@@ -200,7 +221,7 @@ class CORE_EXPORT QgsSvgCache : public QObject
200221
bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth ) const;
201222

202223
/** Calculates scaling for rendered image sizes to SVG logical sizes*/
203-
double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem ) const;
224+
double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem, QSizeF& viewboxSize ) const;
204225

205226
/** Release memory and remove cache entry from mEntryLookup*/
206227
void removeCacheEntry( const QString& s, QgsSvgCacheEntry* entry );

0 commit comments

Comments
 (0)