Skip to content

Commit

Permalink
[layouts] Fix data defined page sizes sometimes lead to incorrect
Browse files Browse the repository at this point in the history
created atlas image sizes

Because the exporter prefers to use the image size when it's
specified (as is done in QGIS app), we need to ignore this
when the aspect ratio of that size doesn't match the aspect
ratio of the page to render.

Fixes #18534
  • Loading branch information
nyalldawson committed Jun 19, 2018
1 parent 543c7a7 commit 2aa2ef4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/core/layout/qgslayoutexporter.cpp
Expand Up @@ -169,6 +169,16 @@ QImage QgsLayoutExporter::renderPageToImage( int page, QSize imageSize, double d
( void )restorer;

QRectF paperRect = QRectF( pageItem->pos().x(), pageItem->pos().y(), pageItem->rect().width(), pageItem->rect().height() );

if ( imageSize.isValid() && ( !qgsDoubleNear( static_cast< double >( imageSize.width() ) / imageSize.height(),
paperRect.width() / paperRect.height(), 0.008 ) ) )
{
// specified image size is wrong aspect ratio for paper rect - so ignore it and just use dpi
// this can happen e.g. as a result of data defined page sizes
// see https://issues.qgis.org/issues/18534
imageSize = QSize();
}

return renderRegionToImage( paperRect, imageSize, dpi );
}

Expand Down
11 changes: 10 additions & 1 deletion tests/src/python/test_qgslayoutexporter.py
Expand Up @@ -378,13 +378,22 @@ def testExportToImage(self):

# image size
settings.imageSize = QSize(600, 851)

rendered_file_path = os.path.join(self.basetestpath, 'test_exporttoimagesize.png')
self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success)
self.assertFalse(os.path.exists(rendered_file_path))
page2_path = os.path.join(self.basetestpath, 'test_exporttoimagesize_2.png')
self.assertTrue(self.checkImage('exporttoimagesize_page2', 'exporttoimagesize_page2', page2_path))

# image size with incorrect aspect ratio
# this can happen as a result of data defined page sizes
settings.imageSize = QSize(851, 600)
rendered_file_path = os.path.join(self.basetestpath, 'test_exporttoimagesizebadaspect.png')
self.assertEqual(exporter.exportToImage(rendered_file_path, settings), QgsLayoutExporter.Success)

page2_path = os.path.join(self.basetestpath, 'test_exporttoimagesizebadaspect_2.png')
im = QImage(page2_path)
self.assertTrue(self.checkImage('exporttoimagesize_badaspect', 'exporttoimagedpi_page2', page2_path), '{}x{}'.format(im.width(), im.height()))

def testExportToPdf(self):
md = QgsProject.instance().metadata()
md.setTitle('proj title')
Expand Down

0 comments on commit 2aa2ef4

Please sign in to comment.