Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/core/symbology/qgsmarkersymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2997,6 +2997,10 @@ QRectF QgsSvgMarkerSymbolLayer::bounds( QPointF point, QgsSymbolRenderContext &c
const double aspectRatio = calculateAspectRatio( context, scaledWidth, hasDataDefinedAspectRatio );
double scaledHeight = scaledWidth * ( !qgsDoubleNear( aspectRatio, 0.0 ) ? aspectRatio : mDefaultAspectRatio );

QPointF outputOffset;
double angle = 0.0;
calculateOffsetAndRotation( context, scaledWidth, scaledHeight, outputOffset, angle );

scaledWidth = context.renderContext().convertToPainterUnits( scaledWidth, mSizeUnit, mSizeMapUnitScale );
scaledHeight = context.renderContext().convertToPainterUnits( scaledHeight, mSizeUnit, mSizeMapUnitScale );

Expand All @@ -3006,10 +3010,6 @@ QRectF QgsSvgMarkerSymbolLayer::bounds( QPointF point, QgsSymbolRenderContext &c
return QRectF();
}

QPointF outputOffset;
double angle = 0.0;
calculateOffsetAndRotation( context, scaledWidth, scaledHeight, outputOffset, angle );

double strokeWidth = mStrokeWidth;
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::Property::StrokeWidth ) )
{
Expand Down
63 changes: 63 additions & 0 deletions tests/src/core/testqgssvgmarker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <qgssinglesymbolrenderer.h>
#include "qgsmarkersymbollayer.h"
#include "qgsproperty.h"
#include "qgsrendercontext.h"
#include "qgssymbollayerutils.h"
#include "qgsmarkersymbol.h"
#include "qgsfontutils.h"
Expand All @@ -54,6 +55,7 @@ class TestQgsSvgMarkerSymbol : public QgsTest

void svgMarkerSymbol();
void bounds();
void boundsAnchorPoint();
void boundsWidth();
void bench();
void anchor();
Expand Down Expand Up @@ -156,6 +158,67 @@ void TestQgsSvgMarkerSymbol::bounds()
QVERIFY( result );
}

void TestQgsSvgMarkerSymbol::boundsAnchorPoint()
{
// the bounds reported for a non-centered anchor must be equal
// to the center-anchored bounds translated by half the rendered
// symbol size, in pixels.

QgsSvgMarkerSymbolLayer layer( QgsSymbolLayerUtils::svgSymbolNameToPath( u"/backgrounds/background_square.svg"_s, QgsPathResolver() ) );
Comment thread
nyalldawson marked this conversation as resolved.
Outdated
layer.setSize( 24.0 );
layer.setSizeUnit( Qgis::RenderUnit::Millimeters );
layer.setFixedAspectRatio( 1.0 );
layer.setStrokeWidth( 0.0 );
layer.setHorizontalAnchorPoint( Qgis::HorizontalAnchorPoint::Center );
layer.setVerticalAnchorPoint( Qgis::VerticalAnchorPoint::Center );

QgsMapSettings ms;
ms.setExtent( QgsRectangle( 0, 0, 1000, 1000 ) );
ms.setOutputSize( QSize( 500, 500 ) );
ms.setOutputDpi( 96 );
QgsRenderContext rc = QgsRenderContext::fromMapSettings( ms );
QgsSymbolRenderContext src( rc, Qgis::RenderUnit::Unknown, 1.0, false, Qgis::SymbolRenderHints() );

const QPointF point( 250, 250 );

layer.startRender( src );
const QRectF centerBounds = layer.bounds( point, src );
layer.stopRender( src );
QVERIFY( centerBounds.isValid() );

const double halfWidthPx = rc.convertToPainterUnits( layer.size(), layer.sizeUnit(), layer.sizeMapUnitScale() ) / 2.0;
const double halfHeightPx = halfWidthPx; // fixedAspectRatio == 1.0

layer.setVerticalAnchorPoint( Qgis::VerticalAnchorPoint::Bottom );
layer.startRender( src );
const QRectF bottomBounds = layer.bounds( point, src );
layer.stopRender( src );
QGSCOMPARENEAR( bottomBounds.center().x(), centerBounds.center().x(), 0.01 );
QGSCOMPARENEAR( bottomBounds.center().y(), centerBounds.center().y() - halfHeightPx, 0.01 );

layer.setVerticalAnchorPoint( Qgis::VerticalAnchorPoint::Top );
layer.startRender( src );
const QRectF topBounds = layer.bounds( point, src );
layer.stopRender( src );
QGSCOMPARENEAR( topBounds.center().x(), centerBounds.center().x(), 0.01 );
QGSCOMPARENEAR( topBounds.center().y(), centerBounds.center().y() + halfHeightPx, 0.01 );

layer.setVerticalAnchorPoint( Qgis::VerticalAnchorPoint::Center );
layer.setHorizontalAnchorPoint( Qgis::HorizontalAnchorPoint::Left );
layer.startRender( src );
const QRectF leftBounds = layer.bounds( point, src );
layer.stopRender( src );
QGSCOMPARENEAR( leftBounds.center().x(), centerBounds.center().x() + halfWidthPx, 0.01 );
QGSCOMPARENEAR( leftBounds.center().y(), centerBounds.center().y(), 0.01 );

layer.setHorizontalAnchorPoint( Qgis::HorizontalAnchorPoint::Right );
layer.startRender( src );
const QRectF rightBounds = layer.bounds( point, src );
layer.stopRender( src );
QGSCOMPARENEAR( rightBounds.center().x(), centerBounds.center().x() - halfWidthPx, 0.01 );
QGSCOMPARENEAR( rightBounds.center().y(), centerBounds.center().y(), 0.01 );
}

void TestQgsSvgMarkerSymbol::boundsWidth()
{
//use a tall, narrow symbol (non-square to test calculation of height)
Expand Down
Loading