Skip to content
Permalink
Browse files

[layouts] Fix data defined page sizes sometimes lead to incorrect

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 2aa2ef48f0cee15287aaa1f9735c8132b06ec211
Showing with 20 additions and 1 deletion.
  1. +10 −0 src/core/layout/qgslayoutexporter.cpp
  2. +10 −1 tests/src/python/test_qgslayoutexporter.py
@@ -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 );
}

@@ -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')

0 comments on commit 2aa2ef4

Please sign in to comment.
You can’t perform that action at this time.