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
928 stars 38 forks source link

Fit shape to content #740

Closed Ownezx closed 3 weeks ago

Ownezx commented 3 weeks ago

Hi, I'm currently wanting to size the size of shapes to content much like the it does in default typst : https://typst.app/docs/reference/visualize/circle/

#circle[
  #set align(center + horizon)
  Automatically \
  sized to fit.
]

For instance, for the circle I would expect to be able to use something in the lines of:

#import "@preview/cetz:0.3.1": canvas, draw, tree

#set page(width: auto, height: auto, margin: .5cm)

#canvas(
  length: 1cm,
  {
    import draw: *

    circle((0, 0))
    content((), [Circle that fits the content])
    circle((0, 0), radius: auto)
    content((), [Circle that fits the content])
    circle((0, 3), radius: (auto, auto))
    content((), [Ellipse that fits the content])
  },
)

Ideally the auto fitting should be customizeable with a padding value?

johannes-wolf commented 3 weeks ago

Fitting exists for rectangles and circles: The frame: parameter of content supports both "rect" and "circle" values and supports padding via the padding: parameter.

Support for other shapes should be easy to implement, have a look at the content function in src/draw/shapes.typ (search for "rect").