Skip to content
Permalink
Browse files

Add method to QgsSvgCache to return SVG viewbox size

  • Loading branch information
nyalldawson committed Nov 15, 2015
1 parent 709b47e commit 936b6506ecd07d68db9a78b7754801ac88c3a2df
@@ -27,6 +27,12 @@ class QgsSvgCacheEntry
double outlineWidth;
double widthScaleFactor;
double rasterScaleFactor;

/** SVG viewbox size.
* @note added in QGIS 2.14
*/
QSizeF viewboxSize;

QColor fill;
QColor outline;
QImage* image;
@@ -83,6 +89,20 @@ class QgsSvgCache : QObject
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );

/** Calculates the viewbox size of a (possibly cached) SVG file.
* @param file Absolute or relative path to SVG file.
* @param size size of cached image
* @param fill color of fill
* @param outline color of outline
* @param outlineWidth width of outline
* @param widthScaleFactor width scale factor
* @param rasterScaleFactor raster scale factor
* @returns viewbox size set in SVG file
* @note added in QGIS 2.14
*/
QSizeF svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );

/** Tests if an svg file contains parameters for fill, outline color, outline width. If yes, possible default values are returned. If there are several
default values in the svg file, only the first one is considered*/
void containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor, bool& hasOutlineWidthParam,
@@ -192,6 +192,15 @@ const QByteArray& QgsSvgCache::svgContent( const QString& file, double size, con
return currentEntry->svgContent;
}

QSizeF QgsSvgCache::svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth, double widthScaleFactor, double rasterScaleFactor )
{
QMutexLocker locker( &mMutex );

QgsSvgCacheEntry *currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );

return currentEntry->viewboxSize;
}

QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
@@ -280,14 +289,16 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
//replace fill color, outline color, outline with in all nodes
QDomElement docElem = svgDoc.documentElement();

double sizeScaleFactor = calcSizeScaleFactor( entry, docElem );
QSizeF viewboxSize;
double sizeScaleFactor = calcSizeScaleFactor( entry, docElem, viewboxSize );
entry->viewboxSize = viewboxSize;
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth * sizeScaleFactor );

entry->svgContent = svgDoc.toByteArray();
mTotalSize += entry->svgContent.size();
}

double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem ) const
double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem, QSizeF& viewboxSize ) const
{
QString viewBox;

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

bool heightOk = false;
double height = parts.at( 3 ).toDouble( &heightOk );

bool widthOk = false;
double width = parts.at( 2 ).toDouble( &widthOk );
if ( widthOk )
{
return width / entry->size ;
if ( heightOk )
viewboxSize = QSizeF( width, height );
return width / entry->size;
}

return 1.0;
@@ -25,6 +25,7 @@
#include <QString>
#include <QUrl>
#include <QObject>
#include <QSizeF>

class QDomElement;
class QImage;
@@ -55,6 +56,12 @@ class CORE_EXPORT QgsSvgCacheEntry
double outlineWidth;
double widthScaleFactor;
double rasterScaleFactor;

/** SVG viewbox size.
* @note added in QGIS 2.14
*/
QSizeF viewboxSize;

QColor fill;
QColor outline;
QImage* image;
@@ -109,6 +116,20 @@ class CORE_EXPORT QgsSvgCache : public QObject
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor, bool forceVectorOutput = false );

/** Calculates the viewbox size of a (possibly cached) SVG file.
* @param file Absolute or relative path to SVG file.
* @param size size of cached image
* @param fill color of fill
* @param outline color of outline
* @param outlineWidth width of outline
* @param widthScaleFactor width scale factor
* @param rasterScaleFactor raster scale factor
* @returns viewbox size set in SVG file
* @note added in QGIS 2.14
*/
QSizeF svgViewboxSize( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );

/** Tests if an svg file contains parameters for fill, outline color, outline width. If yes, possible default values are returned. If there are several
default values in the svg file, only the first one is considered*/
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
bool& hasOutlineWidthParam, bool& hasDefaultOutlineWidth, double& defaultOutlineWidth ) const;

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

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

0 comments on commit 936b650

Please sign in to comment.
You can’t perform that action at this time.