Skip to content

Commit 16f700a

Browse files
committedOct 10, 2015
Fix svg outline widths are incorrectly scaled (fix #11522)
SVG outline width sizes were not correctly calculated, and were set to a constant value regardless of the rendered size of the svg image. This meant that: 1. the rendered outline width was effectively randomly scaled 2. the width would change as the symbol size was modified This change has some large flow on effects, eg: - the large outline widths required to render an outline in <2.12 will now be drawn in their correct sizes, eg massive outlines. Projects will need to be updated to reflect this. - the default outline width set for the provided svg symbols (1 mm) is much too large, so the symbols look bad with the default width. This size needs to be modified in all the (~300 svg marker images) - On the plus side, the long standing issue where most of the svg symbols were rendered with white fill on white background and an almost invisible black border is now fixed (refs #10908, #8538)
1 parent a721752 commit 16f700a

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed
 

‎src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ QgsSvgMarkerSymbolLayerV2::QgsSvgMarkerSymbolLayerV2( const QString& name, doubl
10391039
mAngle = angle;
10401040
mOffset = QPointF( 0, 0 );
10411041
mScaleMethod = scaleMethod;
1042-
mOutlineWidth = 1.0;
1042+
mOutlineWidth = 0.2;
10431043
mOutlineWidthUnit = QgsSymbolV2::MM;
10441044
mColor = QColor( Qt::black );
10451045
mOutlineColor = QColor( Qt::black );

‎src/core/symbology-ng/qgssvgcache.cpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, QColo
229229
{
230230
defaultFillColor = QColor( Qt::black );
231231
defaultOutlineColor = QColor( Qt::black );
232-
defaultOutlineWidth = 1.0;
232+
defaultOutlineWidth = 0.2;
233233

234234
QDomDocument svgDoc;
235235
if ( !svgDoc.setContent( getImageData( path ) ) )
@@ -256,12 +256,56 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
256256

257257
//replace fill color, outline color, outline with in all nodes
258258
QDomElement docElem = svgDoc.documentElement();
259-
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth );
259+
260+
double sizeScaleFactor = calcSizeScaleFactor( entry, docElem );
261+
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth * sizeScaleFactor );
260262

261263
entry->svgContent = svgDoc.toByteArray();
262264
mTotalSize += entry->svgContent.size();
263265
}
264266

267+
double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem ) const
268+
{
269+
QString viewBox;
270+
271+
//bad size
272+
if ( !entry || entry->size == 0 )
273+
return 1.0;
274+
275+
//find svg viewbox attribute
276+
//first check if docElem is svg element
277+
if ( docElem.tagName() == "svg" )
278+
{
279+
viewBox = docElem.attribute( "viewBox", QString() );
280+
}
281+
else
282+
{
283+
QDomElement svgElem = docElem.firstChildElement( "svg" ) ;
284+
if ( !svgElem.isNull() )
285+
{
286+
viewBox = svgElem.attribute( "viewBox", QString() );
287+
}
288+
}
289+
290+
//could not find valid viewbox attribute
291+
if ( viewBox.isEmpty() )
292+
return 1.0;
293+
294+
//width should be 3rd element in a 4 part space delimited string
295+
QStringList parts = viewBox.split( " " );
296+
if ( parts.count() != 4 )
297+
return 1.0;
298+
299+
bool widthOk = false;
300+
double width = parts.at( 2 ).toDouble( &widthOk );
301+
if ( widthOk )
302+
{
303+
return width / entry->size ;
304+
}
305+
306+
return 1.0;
307+
}
308+
265309
QByteArray QgsSvgCache::getImageData( const QString &path ) const
266310
{
267311
// is it a path to local file?

‎src/core/symbology-ng/qgssvgcache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ class CORE_EXPORT QgsSvgCache : public QObject
178178
void containsElemParams( const QDomElement& elem, bool& hasFillParam, QColor& defaultFill, bool& hasOutlineParam, QColor& defaultOutline,
179179
bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const;
180180

181+
/** Calculates scaling for rendered image sizes to SVG logical sizes*/
182+
double calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElement& docElem ) const;
183+
181184
/** Release memory and remove cache entry from mEntryLookup*/
182185
void removeCacheEntry( const QString& s, QgsSvgCacheEntry* entry );
183186

@@ -189,6 +192,7 @@ class CORE_EXPORT QgsSvgCache : public QObject
189192

190193
//! Mutex to prevent concurrent access to the class from multiple threads at once (may corrupt the entries otherwise).
191194
QMutex mMutex;
195+
192196
};
193197

194198
#endif // QGSSVGCACHE_H

‎src/ui/symbollayer/widget_svgmarker.ui

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<property name="singleStep">
3636
<double>0.200000000000000</double>
3737
</property>
38+
<property name="value">
39+
<double>0.200000000000000</double>
40+
</property>
3841
<property name="showClearButton" stdset="0">
3942
<bool>false</bool>
4043
</property>

0 commit comments

Comments
 (0)
Please sign in to comment.