From ff2acf193a92aaf0411d2cadccdaba868b93f2d7 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Thu, 30 May 2024 13:19:38 +0200 Subject: [PATCH] Ensure that gradient size is positive to please some PDF readers This was broken with Chrome and macOS Preview for example. --- weasyprint/svg/defs.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/weasyprint/svg/defs.py b/weasyprint/svg/defs.py index 6641e368a..b3572e6d3 100644 --- a/weasyprint/svg/defs.py +++ b/weasyprint/svg/defs.py @@ -188,6 +188,14 @@ def draw_gradient(svg, node, gradient, font_size, opacity, stroke): bx2, by2 = transform_matrix.invert.transform_point(width, height) width, height = bx2 - bx1, by2 - by1 + # Ensure that width and height are positive to please some PDF readers + if bx1 > bx2: + width = -width + bx1, bx2 = bx2, bx1 + if by1 > by2: + height = -height + by1, by2 = by2, by1 + pattern = svg.stream.add_pattern( bx1, by1, width, height, width, height, matrix @ svg.stream.ctm) group = pattern.add_group(bx1, by1, width, height)