Repository navigation
Refine linebreak algorithm for better Chinese justification - #701
Conversation
- Updated cost function to be more similar to Knuth's paper. - Limits shrinking to a predefined amount. - Stretch justification consists of two steps: first stretch on preferred characters, then stretch all stretchable characters if necessary.
|
I do have a suggestion. Because every script has their own justification considerations, it might be a good idea to separate the bits that are specific to CJK from the main generic function. Like detect if the main script being used is CJK. It will allow for the function to be more extendable in the future for different script while still being readable instead of being jumbled into one big function. Edit: The reason I am suggesting separating it by script is that I am writing out thoughts, at the behest of the Typst Discord, on how to implement Arabic justification practically. It is still in draft form (and I will open an issue once it is finished) but you can look at it here. https://asibahi.github.io/Thoughts/Practical%20Arabic%20Justification.html |
I definitely agree with you. But while I'm keeping CJK typography in mind, this PR do not contains any sophisticated CJK-specific algorithms, and |
| let mut justification_ratio = 0.0; | ||
| let mut extra_justification = Abs::zero(); |
There was a problem hiding this comment.
I don't really understand the purpose of extra_justification.
There was a problem hiding this comment.
We can insert spaces between Chinese characters to stretch the line, but that is not preferred and should be last resort. (See Procedures for Inter-Character Space Expansion from Chinese Layout Requirements)
Let's say in a Chinese and Western mixed texts, we need to stretch a line with 3 space character and 3 Chinese character (not on line end). Every space character has stretchability of 1/6em and Chinese characters have stretchability of 0.
If the remaining space is less than 1/2em, we will not insert spaces between characters. Instead, we will stretch the 3 spaces to fill the remaining space.
On the other hand, if the remaining space is greater than 1/2em, stretching the 3 spaces by 1/6em will still leave us with some leftover length. In this case, we will add (remain - 1/2em) / 6 spaces to each adjustable character, including Chinese characters and spaces. (remain - 1/2em) / 6 is the extra justification.
There was a problem hiding this comment.
Would be great to have this explained with a comment in the source!
| let mut justification_ratio = 0.0; | ||
| let mut extra_justification = Abs::zero(); |
There was a problem hiding this comment.
Would be great to have this explained with a comment in the source!
|
Looking good in general! I dug a bit deeper into the cost and ratio calculations now and added some remarks. |
|
Thanks for these improvements! |
Overview comparison
(The upper part of the text are cropped for the ease of reading.)
Improvements on Chinese linebreaking
Internal changes
Added a shrinkability and stretchability mechanism
This mechanism is the same as in Donald Knuth and Michael Plass’s 1981 paper.
Every character has a shrinkability and strechability, which means the preferred adjust amount. If that character is not justifiable, both value of it should be zero.
The shrinkability and strechability of a glyph can be acquired by
ShapedGlyph::stretch.Updated cost function to be more similar to Knuth's paper.
Change to the justification ratio
Originally the ratio is defined as$$r = \frac{l-L}{w}$$ where $l$ is target line width, $L$ is ideal line width, and $w$ is accumulate of all space's original length. $r\in[-0.15,\infty)$ .
The new ratio is defined as$$r = \frac{l-L}{Y}\text{ or }\frac{l-L}Z$$ where $Y$ is total shrinkability and $Z$ is total strechability of the line. $r\in[-1,\infty)$ , but ideally $r$ should be within $[-1,1]$ , which means the adjust amount is in the preferred bound.
Change to the cost function
The original cost function is$$\delta = |r|^3 + p + a,$$ and now it becomes $$\delta = (0.01 + |r|^3 + p)^2 + a$$ where $p$ is hyphen penalty, $a$ is consecutive hyphen penalty.
This change is not necessary, but according to Knuth's paper, it has some good property which I believe will benefit both English and CJK typography:
New method to insert spaces to stretch the lines.
Stretch justification now consists of two steps:
This change has no effect to English typography. Because all spaces have the same stretchabliity. But it is essential to CJK typography.
Epilogue
Typography is hard, CJK typography is even harder! We are not going to achieve good typography without investing a great deal of time and energy, and this PR is still far from being complete. However, I believe that things should be improved on a step-by-step basis and I hope that this will be a good starting point.
Related #276