typst / typst

A new markup-based typesetting system that is powerful and easy to learn.
https://typst.app
Apache License 2.0
32.21k stars 862 forks source link

PR proposal: Balanced ragged text #4235

Closed egilll closed 1 month ago

egilll commented 3 months ago

Hi. I’d like to submit a PR that alters layout/inline/mod.rs to support balanced ragged text. While Typst does allow for Knuth-Plass to be used for ragged text, the current implementation does not take into account the glue stretchability at the end of the line, and fn commit() does not allow non-justified text to expand slightly.

Here is a comparison, before:

And after:

There are two options that I’d like to add to the parameter #par(linebreaks: ()):

This change can either be implemented by simply adding these glue stretchability values to line.stretchability(), or, if we want to follow Knuth’s paper, by using negative glue, see page 1139.

I’ve implemented the same functionality in JavaScript here if you wish to see how these parameters can be used for different ragged-ness.

Enter-tainer commented 3 months ago

cc @peng1999

laurmaedje commented 1 month ago

Duplicate of https://github.com/typst/typst/issues/2814 I think. I'd be open to support, but I prefer the API in #2814.