Skip to content

Commit

Permalink
Handle hairline stroke sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 9, 2024
1 parent 6929ee9 commit 7115ebb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
60 changes: 44 additions & 16 deletions felt/core/fsl_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,25 @@ def convert_to_pixels(
)
return round(res) if round_size else res

@staticmethod
def convert_stroke_to_pixels(
size,
size_unit: QgsUnitTypes.RenderUnit,
context: ConversionContext
):
"""
Converts a stroke size to pixels
"""
if size == 0:
# handle hairline sizes
return 1

res = FslConverter.convert_to_pixels(
size, size_unit, context, True
)
# round up to at least 1 pixel
return max(res, 1)

@staticmethod
def convert_cap_style(style: Qt.PenCapStyle) -> str:
"""
Expand Down Expand Up @@ -477,9 +496,10 @@ def simple_line_to_fsl(
color_str = FslConverter.color_to_fsl(
layer.color(), context
)
stroke_width = FslConverter.convert_to_pixels(layer.width(),
layer.widthUnit(),
context)
stroke_width = FslConverter.convert_stroke_to_pixels(
layer.width(),
layer.widthUnit(),
context)

res = {
'color': color_str,
Expand Down Expand Up @@ -685,7 +705,7 @@ def simple_fill_to_fsl(
layer.strokeColor().alphaF() > 0):
res['strokeColor'] = FslConverter.color_to_fsl(layer.strokeColor(),
context)
res['strokeWidth'] = FslConverter.convert_to_pixels(
res['strokeWidth'] = FslConverter.convert_stroke_to_pixels(
layer.strokeWidth(), layer.strokeWidthUnit(), context)
res['lineJoin'] = FslConverter.convert_join_style(
layer.penJoinStyle())
Expand Down Expand Up @@ -720,9 +740,11 @@ def simple_marker_to_fsl(
size = FslConverter.convert_to_pixels(layer.size(), layer.sizeUnit(),
context)

stroke_width = FslConverter.convert_to_pixels(layer.strokeWidth(),
layer.strokeWidthUnit(),
context)
stroke_width = FslConverter.convert_stroke_to_pixels(
layer.strokeWidth(),
layer.strokeWidthUnit(),
context
)

res = {
'color': color_str,
Expand Down Expand Up @@ -767,9 +789,11 @@ def ellipse_marker_to_fsl(
layer.symbolWidthUnit(),
context))

stroke_width = FslConverter.convert_to_pixels(layer.strokeWidth(),
layer.strokeWidthUnit(),
context)
stroke_width = FslConverter.convert_stroke_to_pixels(
layer.strokeWidth(),
layer.strokeWidthUnit(),
context
)

res = {
'color': color_str,
Expand Down Expand Up @@ -814,9 +838,11 @@ def svg_marker_to_fsl(
size = FslConverter.convert_to_pixels(layer.size(), layer.sizeUnit(),
context)

stroke_width = FslConverter.convert_to_pixels(layer.strokeWidth(),
layer.strokeWidthUnit(),
context)
stroke_width = FslConverter.convert_stroke_to_pixels(
layer.strokeWidth(),
layer.strokeWidthUnit(),
context
)

res = {
'color': color_str,
Expand Down Expand Up @@ -860,9 +886,11 @@ def font_marker_to_fsl(
size = FslConverter.convert_to_pixels(layer.size(), layer.sizeUnit(),
context)

stroke_width = FslConverter.convert_to_pixels(layer.strokeWidth(),
layer.strokeWidthUnit(),
context)
stroke_width = FslConverter.convert_stroke_to_pixels(
layer.strokeWidth(),
layer.strokeWidthUnit(),
context
)

res = {
'color': color_str,
Expand Down
25 changes: 19 additions & 6 deletions felt/test/test_fsl_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ def test_simple_line_to_fsl(self):
'lineJoin': 'miter',
'size': 3.0}]
)
line.setUseCustomDashPattern(False)

# hairline
line.setWidth(0)
self.assertEqual(
FslConverter.simple_line_to_fsl(line, conversion_context),
[{
'color': 'rgb(0, 255, 0)',
'size': 1,
'lineCap': 'round',
'lineJoin': 'miter',
}]
)

def test_simple_fill_to_fsl(self):
"""
Expand Down Expand Up @@ -583,7 +596,7 @@ def test_simple_marker_to_fsl(self):
[{'color': 'rgb(120, 130, 140)',
'size': 19,
'strokeColor': 'rgba(0, 0, 0, 0)',
'strokeWidth': 0.0}]
'strokeWidth': 1}]
)

self.assertEqual(
Expand All @@ -593,7 +606,7 @@ def test_simple_marker_to_fsl(self):
'size': 19,
'opacity': 0.5,
'strokeColor': 'rgba(0, 0, 0, 0)',
'strokeWidth': 0.0}]
'strokeWidth': 1}]
)

# with stroke
Expand Down Expand Up @@ -649,7 +662,7 @@ def test_ellipse_marker_to_fsl(self):
[{'color': 'rgb(120, 130, 140)',
'size': 19,
'strokeColor': 'rgba(0, 0, 0, 0)',
'strokeWidth': 0.0}]
'strokeWidth': 1}]
)

self.assertEqual(
Expand All @@ -659,7 +672,7 @@ def test_ellipse_marker_to_fsl(self):
'size': 19,
'opacity': 0.5,
'strokeColor': 'rgba(0, 0, 0, 0)',
'strokeWidth': 0.0}]
'strokeWidth': 1}]
)

# with stroke
Expand Down Expand Up @@ -779,7 +792,7 @@ def test_font_marker_to_fsl(self):
[{'color': 'rgb(120, 130, 140)',
'size': 19,
'strokeColor': 'rgba(0, 0, 0, 0)',
'strokeWidth': 0}]
'strokeWidth': 1}]
)

self.assertEqual(
Expand All @@ -789,7 +802,7 @@ def test_font_marker_to_fsl(self):
'size': 19,
'opacity': 0.5,
'strokeColor': 'rgba(0, 0, 0, 0)',
'strokeWidth': 0}]
'strokeWidth': 1}]
)

# with stroke
Expand Down

0 comments on commit 7115ebb

Please sign in to comment.