void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry ) { if ( !entry ) { return; } QDomDocument svgDoc; if ( !svgDoc.setContent( getImageData( entry->path ) ) ) { return; } //replace fill color, stroke color, stroke with in all nodes QDomElement docElem = svgDoc.documentElement(); QSizeF viewboxSize; double sizeScaleFactor = calcSizeScaleFactor( entry, docElem, viewboxSize ); entry->viewboxSize = viewboxSize; replaceElemParams( docElem, entry->fill, entry->stroke, entry->strokeWidth * sizeScaleFactor ); entry->svgContent = svgDoc.toByteArray( 0 ); // toByteArray screws up tspans inside text by adding new lines before and after each span... this should help, at the // risk of potentially breaking some svgs where the newline is desired entry->svgContent.replace( "\n<tspan", "<tspan" ); entry->svgContent.replace( "</tspan>\n", "</tspan>" ); mTotalSize += entry->svgContent.size(); }
void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry ) { if ( !entry ) { return; } QFile svgFile( entry->file ); if ( !svgFile.open( QIODevice::ReadOnly ) ) { return; } QDomDocument svgDoc; if ( !svgDoc.setContent( &svgFile ) ) { return; } //replace fill color, outline color, outline with in all nodes QDomElement docElem = svgDoc.documentElement(); replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth ); entry->svgContent = svgDoc.toByteArray(); mTotalSize += entry->svgContent.size(); }
void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry ) { if ( !entry ) { return; } QDomDocument svgDoc; if ( !svgDoc.setContent( getImageData( entry->file ) ) ) { return; } //replace fill color, outline color, outline with in all nodes QDomElement docElem = svgDoc.documentElement(); QSizeF viewboxSize; double sizeScaleFactor = calcSizeScaleFactor( entry, docElem, viewboxSize ); entry->viewboxSize = viewboxSize; replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth * sizeScaleFactor ); entry->svgContent = svgDoc.toByteArray(); mTotalSize += entry->svgContent.size(); }
void QgsSvgCache::replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth ) { if ( elem.isNull() ) { return; } //go through attributes QDomNamedNodeMap attributes = elem.attributes(); int nAttributes = attributes.count(); for ( int i = 0; i < nAttributes; ++i ) { QDomAttr attribute = attributes.item( i ).toAttr(); //e.g. style="fill:param(fill);param(stroke)" if ( attribute.name().compare( "style", Qt::CaseInsensitive ) == 0 ) { //entries separated by ';' QString newAttributeString; QStringList entryList = attribute.value().split( ';' ); QStringList::const_iterator entryIt = entryList.constBegin(); for ( ; entryIt != entryList.constEnd(); ++entryIt ) { QStringList keyValueSplit = entryIt->split( ':' ); if ( keyValueSplit.size() < 2 ) { continue; } QString key = keyValueSplit.at( 0 ); QString value = keyValueSplit.at( 1 ); if ( value.startsWith( "param(fill" ) ) { value = fill.name(); } else if ( value.startsWith( "param(outline)" ) ) { value = outline.name(); } else if ( value.startsWith( "param(outline-width)" ) ) { value = QString::number( outlineWidth ); } if ( entryIt != entryList.constBegin() ) { newAttributeString.append( ";" ); } newAttributeString.append( key + ":" + value ); } elem.setAttribute( attribute.name(), newAttributeString ); } else { QString value = attribute.value(); if ( value.startsWith( "param(fill)" ) ) { elem.setAttribute( attribute.name(), fill.name() ); } else if ( value.startsWith( "param(outline)" ) ) { elem.setAttribute( attribute.name(), outline.name() ); } else if ( value.startsWith( "param(outline-width)" ) ) { elem.setAttribute( attribute.name(), QString::number( outlineWidth ) ); } } } QDomNodeList childList = elem.childNodes(); int nChildren = childList.count(); for ( int i = 0; i < nChildren; ++i ) { QDomElement childElem = childList.at( i ).toElement(); replaceElemParams( childElem, fill, outline, outlineWidth ); } }