Skip to content

Commit

Permalink
fix issues with align dash pattern (fix qgis#45432)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jan 24, 2024
1 parent 31dd35c commit eb2f052
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/core/symbology/qgslinesymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,15 +777,17 @@ void QgsSimpleLineSymbolLayer::drawPathWithDashPatternTweaks( QPainter *painter,
if ( pen.dashPattern().empty() || points.size() < 2 )
return;

if ( pen.widthF() <= 1.0 )
{
pen.setWidthF( 1.0001 );
}

QVector< qreal > sourcePattern = pen.dashPattern();
const double dashWidthDiv = std::max( 1.0001, pen.widthF() );
const double dashWidthDiv = pen.widthF();
// back to painter units
for ( int i = 0; i < sourcePattern.size(); ++ i )
sourcePattern[i] *= pen.widthF();

if ( pen.widthF() <= 1.0 )
pen.setWidthF( 1.0001 );

QVector< qreal > buffer;
QPolygonF bufferedPoints;
QPolygonF previousSegmentBuffer;
Expand Down Expand Up @@ -960,6 +962,10 @@ void QgsSimpleLineSymbolLayer::drawPathWithDashPatternTweaks( QPainter *painter,
patternIndex++;
currentRemainingDashLength = 0.0;
currentRemainingGapLength = sourcePattern.at( patternIndex );
if ( currentRemainingGapLength == 0.0 )
{
patternIndex++;
}
}
else
{
Expand Down Expand Up @@ -992,8 +998,10 @@ void QgsSimpleLineSymbolLayer::drawPathWithDashPatternTweaks( QPainter *painter,
}
}

if ( patternIndex >= sourcePattern.size() )
if ( patternIndex + 1 >= sourcePattern.size() )
{
patternIndex = 0;
}

const double nextPatternDashLength = sourcePattern.at( patternIndex );
const double nextPatternGapLength = sourcePattern.at( patternIndex + 1 );
Expand Down
12 changes: 11 additions & 1 deletion tests/src/python/test_qgssimplelinesymbollayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ def testTrimDistanceRenderDataDefined(self):
)

def testDashPatternOffset(self):

s = QgsLineSymbol.createSimple({'outline_color': '#ff0000', 'outline_width': '0.6'})

s.symbolLayer(0).setDashPatternOffset(1.2)
Expand Down Expand Up @@ -333,6 +332,17 @@ def testDashCornerTweakDashRender(self):
)
)

def testAlignDashRenderSmallWidth(self):
# rendering test
s = QgsLineSymbol.createSimple({'outline_color': '#ff0000', 'outline_width': '0.1'})

s.symbolLayer(0).setPenStyle(Qt.DashDotDotLine)
s.symbolLayer(0).setAlignDashPattern(True)

g = QgsGeometry.fromWkt('LineString(0 0, 9.2 0, 9.2 10, 1.3 10)')
rendered_image = self.renderGeometry(s, g)
assert self.imageCheck('simpleline_aligndashpattern_small_width', 'simpleline_aligndashpattern_small_width', rendered_image)

def testRingNumberVariable(self):
# test test geometry_ring_num variable
s3 = QgsFillSymbol()
Expand Down
Loading
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 eb2f052

Please sign in to comment.