cetz-package / cetz

CeTZ: ein Typst Zeichenpaket - A library for drawing stuff with Typst.
https://cetz-package.github.io
GNU Lesser General Public License v3.0
731 stars 34 forks source link

Implement clipping region for groups #618

Open hooyuser opened 1 month ago

hooyuser commented 1 month ago

Hi, I was wondering if there is a possibility to implement clipping regions for groups, similar to how it's handled in TikZ. In TikZ, we can define a clipping path within a scope to constrain the visible area of contained elements, as demonstrated below:

\begin{scope} 
    \clip (-2, 0) rectangle (2, 2);     
\end{scope}

Would it be possible to introduce a similar feature for groups in CeTZ, allowing users to define clipping regions that limit rendering to specified boundaries? This feature would be incredibly helpful for complex diagrams where elements outside a certain area need to be hidden.

johannes-wolf commented 1 month ago

This is planned, but doing this for non-rectangular regions is not trivial.

johannes-wolf commented 1 month ago

I guess you mean path-clipping (boolean operations on paths)?

hooyuser commented 1 month ago

Thank you for your quick response!

Yes, I was referring to path-clipping similar to what is shown in the examples on this TikZ page. However, even just implementing clipping for rectangular regions will help a lot for my current needs.

Could you please elaborate on whether it's possible to set up a rectangular clipping region for groups with the current version of CeTZ? Any guidance or workarounds would also be greatly appreciated.

johannes-wolf commented 1 month ago

With the current version of cetz it is possible but not implemented. You have to implement some form of path clipping. There is a closed PR #535 that implements this by using a wasm library.

hooyuser commented 1 month ago

Given the complexities of general clipping, I'm curious to learn how to implement rectangular clipping first. Could you provide some guidance or hints?

It’s a pity about the licensing issues that led to the closure of PR #535, especially considering the effort that must have gone into it. While I haven’t looked into this extensively, a preliminary search suggested that there may be some other suitable path-clipping libraries like Clipper2 or Graphite compatible with LGPL. I thought this might be worth mentioning for future considerations.