Jollywatt / typst-fletcher

Typst package for drawing diagrams with arrows, built on top of CeTZ.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for setting label stroke

LordBaryhobal opened this issue · comments

It would be great to be able to set the stroke on edge labels, especially the color

I've not used this package for very long, but as I understand it, the labels are drawn in the following function:

#let draw-edge-label(edge, label-pos, debug: 0) = {
cetz.draw.content(
label-pos,
box(
// cetz seems to sometimes squash the content, causing a line-
// break, when padding is present...
fill: edge.label-fill,
stroke: if debug >= 2 { DEBUG_COLOR + 0.25pt },
radius: .2em,
pad(.2em)[#edge.label],
),
padding: .2em,
anchor: if edge.label-anchor != auto { edge.label-anchor },
)
if debug >= 2 {
cetz.draw.circle(
label-pos,
radius: 0.75pt,
stroke: none,
fill: DEBUG_COLOR,
)
}
}

Simply adding a label-stroke to the edge dictionary and changing the function like so should do the trick I believe

#let draw-edge-label(edge, label-pos, debug: 0) = {
	cetz.draw.content(
		label-pos,
		box(
			// cetz seems to sometimes squash the content, causing a line-
			// break, when padding is present...
			fill: edge.label-fill,
			stroke: if debug >= 2 { DEBUG_COLOR + 0.25pt } else { edge.label-stroke },
			radius: .2em,
			pad(.2em)[#edge.label],
		),
		padding: .2em,
		anchor: if edge.label-anchor != auto { edge.label-anchor },
	)

	if debug >= 2 {
		cetz.draw.circle(
			label-pos,
			radius: 0.75pt,
			stroke: none,
			fill: DEBUG_COLOR,
		)
	}
}

It could also default to the edge's stroke

That's a good point; everything about the label style except the background fill (indent, rounded corners) is still hard coded.

In the mean time you can probably get what you want by wrapping the label in a box() with the desired stroke style, and in pad() to adjust the indent.

But I agree, we should add this

There is now a label-wrapper option to control this.