Skip to content

Commit 482140b

Browse files
committed
Fix issues rendering SVG with text (fix #14644, #14794)
(cherry-picked from 2265115)
1 parent db9a95e commit 482140b

File tree

5 files changed

+228
-3
lines changed

5 files changed

+228
-3
lines changed

src/core/symbology-ng/qgssvgcache.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,13 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
328328
entry->viewboxSize = viewboxSize;
329329
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth * sizeScaleFactor );
330330

331-
entry->svgContent = svgDoc.toByteArray();
331+
entry->svgContent = svgDoc.toByteArray( 0 );
332+
333+
// toByteArray screws up tspans inside text by adding new lines before and after each span... this should help, at the
334+
// risk of potentially breaking some svgs where the newline is desired
335+
entry->svgContent.replace( "\n<tspan", "<tspan" );
336+
entry->svgContent.replace( "</tspan>\n", "</tspan>" );
337+
332338
mTotalSize += entry->svgContent.size();
333339
}
334340

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

343349
//find svg viewbox attribute
344350
//first check if docElem is svg element
345-
if ( docElem.tagName() == "svg" )
351+
if ( docElem.tagName() == "svg" && docElem.hasAttribute( "viewBox" ) )
346352
{
347353
viewBox = docElem.attribute( "viewBox", QString() );
348354
}
355+
else if ( docElem.tagName() == "svg" && docElem.hasAttribute( "viewbox" ) )
356+
{
357+
viewBox = docElem.attribute( "viewbox", QString() );
358+
}
349359
else
350360
{
351361
QDomElement svgElem = docElem.firstChildElement( "svg" ) ;
352362
if ( !svgElem.isNull() )
353363
{
354-
viewBox = svgElem.attribute( "viewBox", QString() );
364+
if ( svgElem.hasAttribute( "viewBox" ) )
365+
viewBox = svgElem.attribute( "viewBox", QString() );
366+
else if ( svgElem.hasAttribute( "viewbox" ) )
367+
viewBox = svgElem.attribute( "viewbox", QString() );
355368
}
356369
}
357370

tests/src/core/testqgscomposerpicture.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ class TestQgsComposerPicture : public QObject
5757
void pictureSvgFrameToImage();
5858

5959
void svgParameters();
60+
void issue_14644();
6061

6162
void pictureExpression();
6263
void pictureInvalidExpression();
6364

65+
6466
private:
6567
QgsComposition* mComposition;
6668
QgsComposerPicture* mComposerPicture;
@@ -387,6 +389,22 @@ void TestQgsComposerPicture::svgParameters()
387389
mComposerPicture->setPicturePath( mPngImage );
388390
}
389391

392+
void TestQgsComposerPicture::issue_14644()
393+
{
394+
//test rendering SVG file with text
395+
mComposition->addComposerPicture( mComposerPicture );
396+
mComposerPicture->setResizeMode( QgsComposerPicture::Zoom );
397+
mComposerPicture->setPicturePath( QString( TEST_DATA_DIR ) + "/svg/issue_14644.svg" );
398+
399+
QgsCompositionChecker checker( "composerpicture_issue_14644", mComposition );
400+
checker.setControlPathPrefix( "composer_picture" );
401+
QVERIFY( checker.testComposition( mReport, 0, 0 ) );
402+
403+
mComposition->removeItem( mComposerPicture );
404+
mComposerPicture->setSceneRect( QRectF( 70, 70, 100, 100 ) );
405+
mComposerPicture->setPicturePath( mPngImage );
406+
}
407+
390408
void TestQgsComposerPicture::pictureExpression()
391409
{
392410
//test picture source via expression

tests/testdata/svg/issue_14644.svg

+194
Loading

0 commit comments

Comments
 (0)