Skip to content

Commit

Permalink
Fix replacement of fill/stroke opacity in parameterized svgs with opa…
Browse files Browse the repository at this point in the history
…city in style attributes
  • Loading branch information
nyalldawson committed Aug 15, 2019
1 parent d7426b2 commit 26a9c0d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/core/symbology/qgssvgcache.cpp
Expand Up @@ -675,40 +675,43 @@ void QgsSvgCache::replaceElemParams( QDomElement &elem, const QColor &fill, cons
{
continue;
}
QString key = keyValueSplit.at( 0 );
const QString key = keyValueSplit.at( 0 );
QString value = keyValueSplit.at( 1 );
QString newValue = value;
value = value.trimmed().toLower();

if ( value.startsWith( QLatin1String( "param(fill)" ) ) )
{
value = fill.name();
newValue = fill.name();
}
else if ( value.startsWith( QLatin1String( "param(fill-opacity)" ) ) )
{
value = fill.alphaF();
newValue = QString::number( fill.alphaF() );
}
else if ( value.startsWith( QLatin1String( "param(outline)" ) ) )
{
value = stroke.name();
newValue = stroke.name();
}
else if ( value.startsWith( QLatin1String( "param(outline-opacity)" ) ) )
{
value = stroke.alphaF();
newValue = QString::number( stroke.alphaF() );
}
else if ( value.startsWith( QLatin1String( "param(outline-width)" ) ) )
{
value = QString::number( strokeWidth );
newValue = QString::number( strokeWidth );
}

if ( entryIt != entryList.constBegin() )
{
newAttributeString.append( ';' );
}
newAttributeString.append( key + ':' + value );
newAttributeString.append( key + ':' + newValue );
}
elem.setAttribute( attribute.name(), newAttributeString );
}
else
{
QString value = attribute.value();
const QString value = attribute.value().trimmed().toLower();
if ( value.startsWith( QLatin1String( "param(fill)" ) ) )
{
elem.setAttribute( attribute.name(), fill.name() );
Expand Down
61 changes: 61 additions & 0 deletions tests/src/core/testqgssvgcache.cpp
Expand Up @@ -52,6 +52,7 @@ class TestQgsSvgCache : public QObject
void threadSafeImage();
void changeImage(); //check that cache is updated if svg source file changes
void base64();
void replaceParams();

};

Expand Down Expand Up @@ -258,6 +259,66 @@ void TestQgsSvgCache::base64()

}

void TestQgsSvgCache::replaceParams()
{
QDomDocument doc;
QDomElement elem = doc.createElement( QStringLiteral( "svg" ) );
elem.setAttribute( QStringLiteral( "not_style" ), QStringLiteral( "val" ) );
elem.setAttribute( QStringLiteral( "style" ), QStringLiteral( "font-weight:bold; font-size:12px" ) );

QgsSvgCache cache;
cache.replaceElemParams( elem, QColor( 255, 0, 0, 150 ), QColor( 0, 255, 0, 100 ), 0.6 );

// params in styles
QCOMPARE( elem.attribute( QStringLiteral( "not_style" ) ), QStringLiteral( "val" ) );
QCOMPARE( elem.attribute( QStringLiteral( "style" ) ), QStringLiteral( "font-weight:bold; font-size:12px" ) );

// with fill color
elem.setAttribute( QStringLiteral( "style" ), QStringLiteral( "font-weight:bold; fill: param(Fill); font-size:12px" ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 150 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "style" ) ), QStringLiteral( "font-weight:bold; fill:#ff0000; font-size:12px" ) );
// with fill opacity
elem.setAttribute( QStringLiteral( "style" ), QStringLiteral( "font-weight:bold; fill: param(Fill);fill-opacity:param(fill-opacity);font-size:12px" ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "style" ) ), QStringLiteral( "font-weight:bold; fill:#ff0000;fill-opacity:0.0980392;font-size:12px" ) );
// with stroke color
elem.setAttribute( QStringLiteral( "style" ), QStringLiteral( "font-weight:bold; outline: param(Outline);font-size:12px" ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "style" ) ), QStringLiteral( "font-weight:bold; outline:#00ff00;font-size:12px" ) );
// with stroke opacity
elem.setAttribute( QStringLiteral( "style" ), QStringLiteral( "font-weight:bold; outline: param(Outline);outline-opacity:param(outline-opacity);font-size:12px" ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "style" ) ), QStringLiteral( "font-weight:bold; outline:#00ff00;outline-opacity:0.392157;font-size:12px" ) );
// with stroke size
elem.setAttribute( QStringLiteral( "style" ), QStringLiteral( "font-weight:bold; outline: param(Outline);outline-opacity:param(outline-opacity);stroke-width: param(outline-width) ;font-size:12px" ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "style" ) ), QStringLiteral( "font-weight:bold; outline:#00ff00;outline-opacity:0.392157;stroke-width:0.6;font-size:12px" ) );

// params in attributes

// with fill color
elem.setAttribute( QStringLiteral( "fill" ), QStringLiteral( " param(Fill) " ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 150 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "fill" ) ), QStringLiteral( "#ff0000" ) );
// with fill opacity
elem.setAttribute( QStringLiteral( "fill-opacity" ), QStringLiteral( "param(fill-opacity)" ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "fill-opacity" ) ).left( 6 ), QStringLiteral( "0.0980" ) );
// with stroke color
elem.setAttribute( QStringLiteral( "stroke" ), QStringLiteral( " param(Outline) " ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "stroke" ) ), QStringLiteral( "#00ff00" ) );
// with stroke opacity
elem.setAttribute( QStringLiteral( "stroke-opacity" ), QStringLiteral( "param(outline-opacity)" ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "stroke-opacity" ) ).left( 6 ), QStringLiteral( "0.3921" ) );
// with stroke size
elem.setAttribute( QStringLiteral( "stroke-size" ), QStringLiteral( "param(outline-width) " ) );
cache.replaceElemParams( elem, QColor( 255, 0, 0, 25 ), QColor( 0, 255, 0, 100 ), 0.6 );
QCOMPARE( elem.attribute( QStringLiteral( "stroke-size" ) ), QStringLiteral( "0.6" ) );

}

bool TestQgsSvgCache::imageCheck( const QString &testName, QImage &image, int mismatchCount )
{
//draw background
Expand Down

0 comments on commit 26a9c0d

Please sign in to comment.