Skip to content

Commit

Permalink
Fix issues rendering SVG with text (fix #14644, #14794)
Browse files Browse the repository at this point in the history
(cherry-picked from 2265115)
  • Loading branch information
nyalldawson committed Jun 30, 2016
1 parent db9a95e commit 482140b
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/core/symbology-ng/qgssvgcache.cpp
Expand Up @@ -328,7 +328,13 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
entry->viewboxSize = viewboxSize;
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth * sizeScaleFactor );

entry->svgContent = svgDoc.toByteArray();
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();
}

Expand All @@ -342,16 +348,23 @@ double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry* entry, const QDomElem

//find svg viewbox attribute
//first check if docElem is svg element
if ( docElem.tagName() == "svg" )
if ( docElem.tagName() == "svg" && docElem.hasAttribute( "viewBox" ) )
{
viewBox = docElem.attribute( "viewBox", QString() );
}
else if ( docElem.tagName() == "svg" && docElem.hasAttribute( "viewbox" ) )
{
viewBox = docElem.attribute( "viewbox", QString() );
}
else
{
QDomElement svgElem = docElem.firstChildElement( "svg" ) ;
if ( !svgElem.isNull() )
{
viewBox = svgElem.attribute( "viewBox", QString() );
if ( svgElem.hasAttribute( "viewBox" ) )
viewBox = svgElem.attribute( "viewBox", QString() );
else if ( svgElem.hasAttribute( "viewbox" ) )
viewBox = svgElem.attribute( "viewbox", QString() );
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/src/core/testqgscomposerpicture.cpp
Expand Up @@ -57,10 +57,12 @@ class TestQgsComposerPicture : public QObject
void pictureSvgFrameToImage();

void svgParameters();
void issue_14644();

void pictureExpression();
void pictureInvalidExpression();


private:
QgsComposition* mComposition;
QgsComposerPicture* mComposerPicture;
Expand Down Expand Up @@ -387,6 +389,22 @@ void TestQgsComposerPicture::svgParameters()
mComposerPicture->setPicturePath( mPngImage );
}

void TestQgsComposerPicture::issue_14644()
{
//test rendering SVG file with text
mComposition->addComposerPicture( mComposerPicture );
mComposerPicture->setResizeMode( QgsComposerPicture::Zoom );
mComposerPicture->setPicturePath( QString( TEST_DATA_DIR ) + "/svg/issue_14644.svg" );

QgsCompositionChecker checker( "composerpicture_issue_14644", mComposition );
checker.setControlPathPrefix( "composer_picture" );
QVERIFY( checker.testComposition( mReport, 0, 0 ) );

mComposition->removeItem( mComposerPicture );
mComposerPicture->setSceneRect( QRectF( 70, 70, 100, 100 ) );
mComposerPicture->setPicturePath( mPngImage );
}

void TestQgsComposerPicture::pictureExpression()
{
//test picture source via expression
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.
194 changes: 194 additions & 0 deletions tests/testdata/svg/issue_14644.svg
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 482140b

Please sign in to comment.