Skip to content

Commit

Permalink
Add method to QgsSvgCache to return SVG viewbox size
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 15, 2015
1 parent 709b47e commit 936b650
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
20 changes: 20 additions & 0 deletions python/core/symbology-ng/qgssvgcache.sip
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 19 additions & 3 deletions src/core/symbology-ng/qgssvgcache.cpp
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
23 changes: 22 additions & 1 deletion src/core/symbology-ng/qgssvgcache.h
Expand Up @@ -25,6 +25,7 @@
#include <QString>
#include <QUrl>
#include <QObject>
#include <QSizeF>

class QDomElement;
class QImage;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 936b650

Please sign in to comment.