Skip to content

Commit

Permalink
Fix #49: Division by 0 on dashed or dotted border smaller than one do…
Browse files Browse the repository at this point in the history
…t/dash.
  • Loading branch information
SimonSapin committed Mar 18, 2013
1 parent 4c04bcd commit 408b7d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions weasyprint/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,12 @@ def draw_border_segment(context, enable_hinting, style, width, color, side,
if context.user_to_device_distance(width, 0)[0] > 3:
# Round so that dash is a divisor of length,
# but not in the dots are too small.
dash = length / round(length / dash)
dash = length / max(1, round(length / dash))
context.set_line_cap(cairo.LINE_CAP_ROUND)
context.set_dash([0, dash])
else: # dashed
# Round so that 2*dash is a divisor of length
dash = length / (2 * round(length / (2 * dash)))
dash = length / (2 * max(1, round(length / (2 * dash))))
context.set_dash([dash])
# Stroke along the line in === above, as wide as the border
context.move_to(x1, y1)
Expand Down
20 changes: 20 additions & 0 deletions weasyprint/tests/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,26 @@ def test_outlines():
return test_borders(margin='20px', prop='outline')


@assert_no_logs
def test_small_borders():
# Regression test for ZeroDivisionError on dashed or dotted borders
# smaller than a dash/dot.
# https://github.com/Kozea/WeasyPrint/issues/49

# Do not test the exact rendering of earch border style but at least
# check that they do not do the same.
assert_different_renderings(50, 50, [('small_border_%s' % border_style, '''
<style>
@page { size: 50px 50px }
html { background: #fff }
body { margin: 5px; height: 0; border: 10px %s blue }
</style>
<body>''' % border_style)
for border_style in ['none', 'solid', 'dashed', 'dotted']
])



@assert_no_logs
def test_margin_boxes():
"""Test the rendering of margin boxes"""
Expand Down

0 comments on commit 408b7d6

Please sign in to comment.