diff --git a/src/core/symbology/qgssvgcache.cpp b/src/core/symbology/qgssvgcache.cpp index 1fae9f692138..f1ffaffecec1 100644 --- a/src/core/symbology/qgssvgcache.cpp +++ b/src/core/symbology/qgssvgcache.cpp @@ -503,40 +503,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() ); diff --git a/tests/src/core/testqgssvgcache.cpp b/tests/src/core/testqgssvgcache.cpp index ae5a978ca2d1..589c648f3f27 100644 --- a/tests/src/core/testqgssvgcache.cpp +++ b/tests/src/core/testqgssvgcache.cpp @@ -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(); }; @@ -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