From d5a08b3b96f405b0fdca42e3f1f6baa32c59c687 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 24 Aug 2022 17:08:07 -0600 Subject: [PATCH] [Snippets/statShape] Handle empty shapes --- Snippets/statShape.py | 47 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/Snippets/statShape.py b/Snippets/statShape.py index ab278973d..95f5b5f9b 100644 --- a/Snippets/statShape.py +++ b/Snippets/statShape.py @@ -57,30 +57,31 @@ with cairo.SVGSurface("example.svg", hhea.advanceWidthMax, hhea.ascent-hhea.desc ellipse_area = math.sqrt(lambda1) * math.sqrt(lambda2) * math.pi/4 * mult * mult - context.save() - context.set_line_cap(cairo.LINE_CAP_ROUND) - context.transform(transform) - context.move_to(0, 0) - context.line_to(0, 0) - context.set_line_width(1) - context.set_source_rgba(1, 0, 0, abs(stats.area / ellipse_area)) - context.stroke() - context.restore() + if stats.area: + context.save() + context.set_line_cap(cairo.LINE_CAP_ROUND) + context.transform(transform) + context.move_to(0, 0) + context.line_to(0, 0) + context.set_line_width(1) + context.set_source_rgba(1, 0, 0, abs(stats.area / ellipse_area)) + context.stroke() + context.restore() - context.save() - context.set_line_cap(cairo.LINE_CAP_ROUND) - context.set_source_rgb(.8, 0, 0) - context.translate(stats.meanX, stats.meanY) + context.save() + context.set_line_cap(cairo.LINE_CAP_ROUND) + context.set_source_rgb(.8, 0, 0) + context.translate(stats.meanX, stats.meanY) - context.move_to(0, 0) - context.line_to(0, 0) - context.set_line_width(15) - context.stroke() + context.move_to(0, 0) + context.line_to(0, 0) + context.set_line_width(15) + context.stroke() - context.transform(cairo.Matrix(1, 0, stats.slant, 1, 0, 0)) - context.move_to(0, -stats.meanY + font['hhea'].ascent) - context.line_to(0, -stats.meanY + font['hhea'].descent) - context.set_line_width(5) - context.stroke() + context.transform(cairo.Matrix(1, 0, stats.slant, 1, 0, 0)) + context.move_to(0, -stats.meanY + font['hhea'].ascent) + context.line_to(0, -stats.meanY + font['hhea'].descent) + context.set_line_width(5) + context.stroke() - context.restore() + context.restore()