chalk-diagrams / chalk

A declarative drawing API in Python
MIT License
276 stars 13 forks source link

"Document Size" for multiple images #125

Open srush opened 1 year ago

srush commented 1 year ago

Since people are posting issues again, let me add one :)

I am having a lot of trouble keeping my images for my course at a standard size. The problem is that when I use SVG the images get rescaled in strange ways in different parts of the lecture.

For example the thickness of lines is set relative to the image size, but if I have 6 images all of different sizes the lines get resized differently. I can set a fixed height, put then sometimes the diagrams are really long and clip off the page.

I have some workarounds for this, but I feel like I am doing something incorrect, and I end up having to do hacks like "with_envelope" on each diagram to get it to look good.

danoneata commented 1 year ago

Thanks for the feature suggestion, Sasha! 🙂 These semantics would correspond to those of output units in diagrams, right?

srush commented 1 year ago

Ah, yup we should just copy their structure here. It is a bit annoying that you need to know about this when drawing (lwO). However if they did it that way, there is probably not a better solution.

danoneata commented 1 year ago

On a closer look it seems that our line_width_local already behaves like diagram's "output units" (so we should probably update this name?). For example, the diagrams generated by the following example result in the same line width (as far as I could tell):

from chalk import *

sq = square(1).line_width_local(1.5)
dia = hcat([sq.scale(s) for s in [0.5, 0.8, 1, 1.5, 2]], sep=0.2)
dia.render_svg("/tmp/out-1.svg", height=128)
dia.render_svg("/tmp/out-2.svg", height=512)

Apart from line width, I guess we also want to control the arrowhead size and font size in a similar manner, right?

It is a bit annoying that you need to know about this when drawing (lwO)

I think that this attribute can be specified at the end (just before rendering)? For example:

dia = hcat([square(1).scale(s) for s in [0.5, 0.8, 1, 1.5, 2]], sep=0.2)
dia.line_width_local(1.5).render_svg("/tmp/out.svg", height=128)