From 4aa448a5a37889d80fbf6ef006c5db1dd7c55f39 Mon Sep 17 00:00:00 2001
From: jaseg <git@jaseg.de>
Date: Sun, 16 Apr 2023 20:12:59 +0200
Subject: svg-flatten: Fix pattern clipping for rotated/skewed transforms

---
 svg-flatten/src/svg_pattern.cpp | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

(limited to 'svg-flatten/src')

diff --git a/svg-flatten/src/svg_pattern.cpp b/svg-flatten/src/svg_pattern.cpp
index e8fb851..db740cf 100644
--- a/svg-flatten/src/svg_pattern.cpp
+++ b/svg-flatten/src/svg_pattern.cpp
@@ -60,8 +60,8 @@ void gerbolyze::Pattern::tile (gerbolyze::RenderContext &ctx) {
      * patternTransform. This is necessary so we iterate over the correct bounds when tiling below */
     d2p pos_xf = ctx.mat().doc2phys(d2p{x, y});
     double inst_x = pos_xf[0], inst_y = pos_xf[1];
-    double inst_w = ctx.mat().doc2phys_dist(w);
-    double inst_h = ctx.mat().doc2phys_dist(h);
+    double inst_w = w;
+    double inst_h = h;
 
     ClipperLib::IntRect clip_bounds = get_paths_bounds(ctx.clip());
     double bx = clip_bounds.left / clipper_scale;
@@ -70,12 +70,14 @@ void gerbolyze::Pattern::tile (gerbolyze::RenderContext &ctx) {
     double bh = (clip_bounds.bottom - clip_bounds.top) / clipper_scale;
 
     d2p clip_p0 = patternTransform_inv.doc2phys(d2p{bx, by});
-    d2p clip_p1 = patternTransform_inv.doc2phys(d2p{bx+bw, by+bh});
+    d2p clip_p1 = patternTransform_inv.doc2phys(d2p{bx+bw, by});
+    d2p clip_p2 = patternTransform_inv.doc2phys(d2p{bx+bw, by+bh});
+    d2p clip_p3 = patternTransform_inv.doc2phys(d2p{bx, by+bh});
 
-    bx = fmin(clip_p0[0], clip_p1[0]);
-    by = fmin(clip_p0[1], clip_p1[1]);
-    bw = fmax(clip_p0[0], clip_p1[0]) - bx;
-    bh = fmax(clip_p0[1], clip_p1[1]) - by;
+    bx = fmin(fmin(clip_p0[0], clip_p1[0]), fmin(clip_p2[0], clip_p3[0]));
+    by = fmin(fmin(clip_p0[1], clip_p1[1]), fmin(clip_p2[1], clip_p3[1]));
+    bw = fmax(fmax(clip_p0[0], clip_p1[0]), fmax(clip_p2[0], clip_p3[0])) - bx;
+    bh = fmax(fmax(clip_p0[1], clip_p1[1]), fmax(clip_p2[1], clip_p3[1])) - by;
 
     if (patternUnits == SVG_ObjectBoundingBox) {
         inst_x *= bw;
@@ -102,11 +104,9 @@ void gerbolyze::Pattern::tile (gerbolyze::RenderContext &ctx) {
     for (double inst_off_x = fmod(inst_x, inst_w) - 2*inst_w;
             inst_off_x < bx + bw + 2*inst_w;
             inst_off_x += inst_w) {
-
         for (double inst_off_y = fmod(inst_y, inst_h) - 2*inst_h;
                 inst_off_y < by + bh + 2*inst_h;
                 inst_off_y += inst_h) {
-
             xform2d elem_xf;
             /* Change into this individual tile's coordinate system */
             elem_xf.translate(inst_off_x, inst_off_y);
-- 
cgit