Skip to content

Commit

Permalink
Fix flexible aspect ratio is ignored when rendering marker svg symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and nyalldawson committed Jul 21, 2020
1 parent a79f0e5 commit 9484aff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/symbology/qgssvgcache.cpp
Expand Up @@ -407,6 +407,8 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry *entry )
std::unique_ptr< QImage > image = qgis::make_unique< QImage >( imageSize, QImage::Format_ARGB32_Premultiplied );
image->fill( 0 ); // transparent background

const bool isFixedAR = entry->fixedAspectRatio > 0;

QPainter p( image.get() );
QSvgRenderer r( entry->svgContent );
if ( qgsDoubleNear( viewBoxSize.width(), viewBoxSize.height() ) )
Expand All @@ -416,7 +418,7 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry *entry )
else
{
QSizeF s( viewBoxSize );
s.scale( scaledSize.width(), scaledSize.height(), Qt::KeepAspectRatio );
s.scale( scaledSize.width(), scaledSize.height(), isFixedAR ? Qt::IgnoreAspectRatio : Qt::KeepAspectRatio );
QRectF rect( ( imageSize.width() - s.width() ) / 2, ( imageSize.height() - s.height() ) / 2, s.width(), s.height() );
r.render( &p, rect );
}
Expand Down
13 changes: 13 additions & 0 deletions tests/src/core/testqgssvgcache.cpp
Expand Up @@ -54,6 +54,7 @@ class TestQgsSvgCache : public QObject
void changeImage(); //check that cache is updated if svg source file changes
void base64();
void replaceParams();
void aspectRatio();

};

Expand Down Expand Up @@ -332,6 +333,18 @@ void TestQgsSvgCache::replaceParams()

}

void TestQgsSvgCache::aspectRatio()
{
// test rendering SVGs with manual aspect ratio
QgsSvgCache cache;
bool inCache = false;

const QString originalImage = TEST_DATA_DIR + QStringLiteral( "/test_symbol_svg.svg" );
QImage img = cache.svgAsImage( originalImage, 200, QColor( 0, 0, 0 ), QColor( 0, 0, 0 ), 1.0,
1.0, inCache, 0.5 );
QVERIFY( imageCheck( QStringLiteral( "svgcache_aspect_ratio" ), img, 30 ) );
}

bool TestQgsSvgCache::imageCheck( const QString &testName, QImage &image, int mismatchCount )
{
//draw background
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9484aff

Please sign in to comment.