Skip to content

Commit

Permalink
Don’t crash when the font size is really small
Browse files Browse the repository at this point in the history
Fix #1499.
  • Loading branch information
liZe committed Nov 29, 2021
1 parent 5ebb9db commit 80b88db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
17 changes: 17 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ def test_text_font_size_zero():
assert paragraph.height == 0


@assert_no_logs
def test_text_font_size_very_small():
# Test regression: https://github.com/Kozea/WeasyPrint/issues/1499
page, = render_pages('''
<style>
p { font-size: 0.00000001px }
</style>
<p>test font size zero</p>
''')
html, = page.children
body, = html.children
paragraph, = body.children
line, = paragraph.children
assert line.height < 0.001
assert paragraph.height < 0.001


@assert_no_logs
def test_text_spaced_inlines():
page, = render_pages('''
Expand Down
15 changes: 9 additions & 6 deletions weasyprint/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ def __init__(self, font_hash, file_content, pango_font, index):
b'/' + self.hash.encode('ascii') + b'+' +
self.family.replace(b' ', b''))
self.italic_angle = 0 # TODO: this should be different
self.ascent = int(
pango.pango_font_metrics_get_ascent(pango_metrics) /
font_size * 1000)
self.descent = -int(
pango.pango_font_metrics_get_descent(pango_metrics) /
font_size * 1000)
if font_size:
self.ascent = int(
pango.pango_font_metrics_get_ascent(pango_metrics) /
font_size * 1000)
self.descent = -int(
pango.pango_font_metrics_get_descent(pango_metrics) /
font_size * 1000)
else:
self.ascent = self.descent = 0
self.upem = harfbuzz.hb_face_get_upem(hb_face)
self.png = harfbuzz.hb_ot_color_has_png(hb_face)
self.svg = harfbuzz.hb_ot_color_has_svg(hb_face)
Expand Down

0 comments on commit 80b88db

Please sign in to comment.