Skip to content
Permalink
Browse files
Fixes #44426 : when masking, set dpi for new allocated image
  • Loading branch information
troopa81 authored and nyalldawson committed Dec 21, 2021
1 parent ca5c83a commit 964bccfd35509b81e5f5b49ed3bcf467013c2f00
@@ -374,6 +374,8 @@ QImage *QgsMapRendererJob::allocateImage( QString layerId )
QImage *image = new QImage( mSettings.deviceOutputSize(),
mSettings.outputImageFormat() );
image->setDevicePixelRatio( static_cast<qreal>( mSettings.devicePixelRatio() ) );
image->setDotsPerMeterX( 1000 * mSettings.outputDpi() / 25.4 );
image->setDotsPerMeterY( 1000 * mSettings.outputDpi() / 25.4 );
if ( image->isNull() )
{
mErrors.append( Error( layerId, tr( "Insufficient memory for image %1x%2" ).arg( mSettings.outputSize().width() ).arg( mSettings.outputSize().height() ) ) );
@@ -203,10 +203,11 @@ QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer *layer, QgsRender
mRasterViewPort->mHeight = static_cast<qgssize>( std::abs( mRasterViewPort->mBottomRightPoint.y() - mRasterViewPort->mTopLeftPoint.y() ) );


const double dpi = 25.4 * rendererContext.scaleFactor();
if ( mProviderCapabilities & QgsRasterDataProvider::DpiDependentData
&& rendererContext.dpiTarget() >= 0.0 )
{
const double dpiScaleFactor = rendererContext.dpiTarget() / rendererContext.painter()->device()->logicalDpiX();
const double dpiScaleFactor = rendererContext.dpiTarget() / dpi;
mRasterViewPort->mWidth *= dpiScaleFactor;
mRasterViewPort->mHeight *= dpiScaleFactor;
}
@@ -240,8 +241,7 @@ QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer *layer, QgsRender
// TODO R->mLastViewPort = *mRasterViewPort;

// TODO: is it necessary? Probably WMS only?
layer->dataProvider()->setDpi( 25.4 * rendererContext.scaleFactor() );

layer->dataProvider()->setDpi( dpi );

// copy the whole raster pipe!
mPipe = new QgsRasterPipe( *layer->pipe() );
@@ -372,4 +372,3 @@ bool QgsRasterLayerRenderer::forceRasterRender() const
// preview of intermediate raster rendering results requires a temporary output image
return renderContext()->testFlag( Qgis::RenderContextFlag::RenderPartialOutput );
}

@@ -124,6 +124,8 @@ def setUp(self):
# polygon layer with a rule based labeling
self.polys_layer2 = QgsProject.instance().mapLayersByName('polys2')[0]

self.raster_layer = QgsProject.instance().mapLayersByName('raster_layer')[0]

# try to fix the font for where labels are defined
# in order to have more stable image comparison tests
for layer in [self.polys_layer, self.lines_with_labels, self.polys_layer2]:
@@ -755,6 +757,27 @@ def test_layout_exports(self):
self.report += self.checker.report()
self.assertTrue(res)

def test_different_dpi_target(self):
"""Test with raster layer and a target dpi"""

# modify labeling settings
label_settings = self.polys_layer.labeling().settings()
fmt = label_settings.format()
# enable a mask
fmt.mask().setEnabled(True)
fmt.mask().setSize(4.0)
# and mask other symbol layers underneath
fmt.mask().setMaskedSymbolLayers([
# the black part of roads
QgsSymbolLayerReference(self.lines_layer.id(), QgsSymbolLayerId("", 0))])

label_settings.setFormat(fmt)
self.polys_layer.labeling().setSettings(label_settings)

self.map_settings.setLayers([self.lines_layer, self.polys_layer, self.raster_layer])
self.map_settings.setDpiTarget(300)
self.check_renderings(self.map_settings, "different_dpi_target")


if __name__ == '__main__':
start_app()
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 964bccf

Please sign in to comment.