Skip to content

Commit

Permalink
Fix some problems with svg cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jul 4, 2011
1 parent 55a1778 commit ff83436
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 56 deletions.
10 changes: 3 additions & 7 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -555,24 +555,20 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
outputOffset = _rotatedOffset( outputOffset, mAngle );
p->translate( point + outputOffset );

if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale )
{
double s = mSize / mOrigSize;
p->scale( s, s );
}
int size = (int)( context.outputLineWidth( mSize ) );

if ( mAngle != 0 )
p->rotate( mAngle );

if ( doubleNear( context.renderContext().rasterScaleFactor(), 1.0, 0.1 ) )
{
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, mSize, mFillColor, mOutlineColor, mOutlineWidth,
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, size, mFillColor, mOutlineColor, mOutlineWidth,
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
p->drawImage( -img.width() / 2.0, -img.width() / 2.0, img );
}
else
{
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, mFillColor, mOutlineColor, mOutlineWidth,
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, size, mFillColor, mOutlineColor, mOutlineWidth,
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
p->drawPicture( 0, 0, pct );
}
Expand Down
62 changes: 19 additions & 43 deletions src/core/symbology-ng/qgssvgcache.cpp
Expand Up @@ -95,7 +95,7 @@ QgsSvgCache::~QgsSvgCache()
}


const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
const QImage& QgsSvgCache::svgAsImage( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
QgsSvgCacheEntry* currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
Expand All @@ -111,7 +111,7 @@ const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const Q
return *( currentEntry->image );
}

const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
const QPicture& QgsSvgCache::svgAsPicture( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
QgsSvgCacheEntry* currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
Expand All @@ -127,7 +127,7 @@ const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, con
return *( currentEntry->picture );
}

QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
QgsSvgCacheEntry* entry = new QgsSvgCacheEntry( file, size, outlineWidth, widthScaleFactor, rasterScaleFactor, fill, outline );
Expand Down Expand Up @@ -159,37 +159,6 @@ QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, cons
void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor,
bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const
{
/*hasFillParam = false;
hasOutlineParam = false;
hasOutlineWidthParam = false;
QFile svgFile( path );
if ( !svgFile.open( QIODevice::ReadOnly ) )
{
return;
}
QDomDocument svgDoc;
if ( !svgDoc.setContent( &svgFile ) )
{
return;
}
//there are surely faster ways to get this information
QString content = svgDoc.toString();
if ( content.contains( "param(fill" ) )
{
hasFillParam = true;
}
if ( content.contains( "param(outline" ) )
{
hasOutlineParam = true;
}
if ( content.contains( "param(outline-width)" ) )
{
hasOutlineWidthParam = true;
}*/

defaultFillColor = QColor( Qt::black );
defaultOutlineColor = QColor( Qt::black );
defaultOutlineWidth = 1.0;
Expand Down Expand Up @@ -247,7 +216,7 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
delete entry->image;
entry->image = 0;

double imageSize = entry->size * entry->widthScaleFactor * entry->rasterScaleFactor;
int imageSize = entry->size;
QImage* image = new QImage( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
image->fill( 0 ); // transparent background

Expand All @@ -271,8 +240,7 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )

//correct QPictures dpi correction
QPicture* picture = new QPicture();
double dpi = entry->widthScaleFactor * 25.4 * entry->rasterScaleFactor;
double pictureSize = entry->size * entry->widthScaleFactor / dpi * picture->logicalDpiX();
double pictureSize = entry->size / 25.4 / entry->rasterScaleFactor * picture->logicalDpiX();
QRectF rect( QPointF( -pictureSize / 2.0, -pictureSize / 2.0 ), QSizeF( pictureSize, pictureSize ) );


Expand All @@ -283,7 +251,7 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
mTotalSize += entry->picture->size();
}

QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
//search entries in mEntryLookup
Expand All @@ -302,7 +270,6 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con
}
}


//if not found: create new entry
//cache and replace params in svg content
if ( !currentEntry )
Expand All @@ -312,10 +279,18 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con
else
{
takeEntryFromList( currentEntry );
mMostRecentEntry->nextEntry = currentEntry;
currentEntry->previousEntry = mMostRecentEntry;
currentEntry->nextEntry = 0;
mMostRecentEntry = currentEntry;
if( !mMostRecentEntry ) //list is empty
{
mMostRecentEntry = currentEntry;
mLeastRecentEntry = currentEntry;
}
else
{
mMostRecentEntry->nextEntry = currentEntry;
currentEntry->previousEntry = mMostRecentEntry;
currentEntry->nextEntry = 0;
mMostRecentEntry = currentEntry;
}
}

//debugging
Expand Down Expand Up @@ -537,6 +512,7 @@ void QgsSvgCache::trimToMaximumSize()
entry = entry->nextEntry;

takeEntryFromList( bkEntry );
mEntryLookup.remove( bkEntry->file, bkEntry );
mTotalSize -= bkEntry->dataSize();
delete bkEntry;
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/symbology-ng/qgssvgcache.h
Expand Up @@ -34,7 +34,7 @@ struct CORE_EXPORT QgsSvgCacheEntry
~QgsSvgCacheEntry();

QString file;
double size;
int size; //size in pixel
double outlineWidth;
double widthScaleFactor;
double rasterScaleFactor;
Expand Down Expand Up @@ -65,9 +65,9 @@ class CORE_EXPORT QgsSvgCache
static QgsSvgCache* instance();
~QgsSvgCache();

const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
const QImage& svgAsImage( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
const QPicture& svgAsPicture( const QString& file, int 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
Expand All @@ -79,14 +79,14 @@ class CORE_EXPORT QgsSvgCache
QgsSvgCache();

/**Creates new cache entry and returns pointer to it*/
QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
QgsSvgCacheEntry* insertSVG( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );

void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
void cacheImage( QgsSvgCacheEntry* entry );
void cachePicture( QgsSvgCacheEntry* entry );
/**Returns entry from cache or creates a new entry if it does not exist already*/
QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
QgsSvgCacheEntry* cacheEntry( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );

/**Removes the least used items until the maximum size is under the limit*/
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Expand Up @@ -547,7 +547,7 @@ class QgsSvgListModel : public QAbstractListModel
bool fillParam, outlineParam, outlineWidthParam;
QgsSvgCache::instance()->containsParams( entry, fillParam, fill, outlineParam, outline, outlineWidthParam, outlineWidth );

const QImage& img = QgsSvgCache::instance()->svgAsImage( entry, 8, fill, outline, outlineWidth, 3.5 /*appr. 88 dpi*/, 1.0 );
const QImage& img = QgsSvgCache::instance()->svgAsImage( entry, 30, fill, outline, outlineWidth, 3.5 /*appr. 88 dpi*/, 1.0 );
pixmap = QPixmap::fromImage( img );
QPixmapCache::insert( entry, pixmap );
}
Expand Down

0 comments on commit ff83436

Please sign in to comment.