From 29d31810a63e060fc6f157025d5be04be343b286 Mon Sep 17 00:00:00 2001 From: theDigitalGuy Date: Thu, 14 Aug 2025 20:26:31 +0200 Subject: [PATCH] Attempt of making the backend clip/ignore curves outside the xlim/ylim. --- mpl_typst/backend.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mpl_typst/backend.py b/mpl_typst/backend.py index e3f8622..cc36370 100644 --- a/mpl_typst/backend.py +++ b/mpl_typst/backend.py @@ -241,11 +241,18 @@ def normalize(coords) -> tuple[float, ...]: else: stroke.kwargs.update({'dash': bounds}) + # Get clipping rectangle from graphics context + bbox = gc.get_clip_rectangle() + clip = None + if bbox: + p1, p2 = bbox.get_points() + clip = (p1[0], p1[1], p2[0], p2[1]) + # Since Typst v0.13.0, path drawing API (aka curve) is more coherent to # Matplotlib's Path object. subpath: list[Call] superpath: list[list[Call]] = [] - for points, code in path.iter_segments(transform): + for points, code in path.iter_segments(transform, clip=clip): points = normalize(points) scalars = tuple([Scalar(p, 'in') for p in points]) match code: