How can I achieve snake justification?

XKCD 1676 shows snake justification, where every line has a snake to the right margin. How can this be done in Typst? I don’t care about snakes, but I intend to fill the right side of the text by some specified content, like a line.

My idea is to redefine par.line by a show rule, but it seems that par.line ignores show rules because the documentation suggests that, and all such rules which I tried did nothing. But, if it worked, it would be so:

#show par.line: l => layout(size => {
    let remain = size.width - measure(l).width
    l.body
    line(length: remain)
} )

Is there a way to make this work?

1 Like

I think this does it:

#show par: it => {
    block({
        it.body
        // Append your desired text here.
        box(width: 1fr, fill: green, " ")
    })
}

#lorem(5)

#lorem(10)

#lorem(30)

No. This justified only the last line of every paragraph, and other lines are unaffected. It is supposed to insert the filter to every line of text.

I don’t think Typst’s justification algorithms are configurable that way, so what remains is that you’d need to reimplement justification yourself in typst, which is probably not impossible for snake justification.

I was wondering if maybe using meander would help - using its code as a starting point. It’s a very advanced package but it’s already implemented many of the tools you need, like selecting how much text to fit into the next line. (This function looks very useful in meander: internals.fill-box.)

Introducing…

The code is here (permalink), and it’s using meander to segment the text. We’ll need to find a higher resolution snake graphic, segment it better (so that it can repeat without glitches) and then it’s ready to print…

11 Likes