Skip to content

Refine linebreak algorithm for better Chinese justification - #701

Merged
laurmaedje merged 15 commits into
typst:mainfrom
peng1999:justification
Apr 13, 2023
Merged

laurmaedje merged 15 commits into
typst:mainfrom
peng1999:justification

Conversation

@peng1999

@peng1999 peng1999 commented Apr 10, 2023 •

Copy link
Copy Markdown
Contributor

Overview comparison

(The upper part of the text are cropped for the ease of reading.)

Original version Refined version
Original version Refined version

Improvements on Chinese linebreaking

  • Fixed problems that Chinese character not aligned on line end. (“则” and “及” in above text)
  • Fixed problems that compress Chinese character. (“近年来” in above text) Chinese characters only allows expand and do not allow compress.
  • Added a subset of Chinese punctuation compression.

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

This change is required for Chinese typography.

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:

  • Minimizing the sum of squares of badnesses not only tends to minimize the maximum badness per line, it also provides secondary optimization; for example, when one particularly bad line is inevitable, the other line breaks will also be optimized.
  • Adding 0.01 to $|r|^3$ instead of using the cost by itself will minimize the total number of lines in cases where there are breaks with approximately zero badness.

New method to insert spaces to stretch the lines.

Stretch justification now consists of two steps:

  • Add space to all stretchable character, proportional to their stretchability. The added space should not be exceed the stretchability limit.
  • If that is not enough, as a last resort, add more space to all justifiable characters equally. Chinese character has no stretchability but is justifiable. They are not adjusted in the first step, but will be stretched in the second step.

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

- 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.
@asibahi

asibahi commented Apr 10, 2023 •

Copy link
Copy Markdown
Contributor

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

@peng1999

peng1999 commented Apr 11, 2023 •

Copy link
Copy Markdown
Contributor Author

it might be a good idea to separate the bits that are specific to CJK from the main generic function.

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
mainly contains general changes which also apply to Latin script. I think it's not too late to add a second PR to implement language-aware algorithms.

Comment thread library/src/layout/par.rs Resolved
Comment thread tests/ref/text/linebreak.png Resolved
Comment thread tests/typ/layout/par-justify-cjk.typ Resolved
Comment thread library/src/layout/par.rs Resolved
Comment thread library/src/layout/par.rs Resolved
Comment thread library/src/text/shaping.rs Resolved
Comment thread library/src/layout/par.rs Resolved
Comment thread library/src/layout/par.rs
Comment on lines +1275 to +1276
let mut justification_ratio = 0.0;
let mut extra_justification = Abs::zero();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand the purpose of extra_justification.

@peng1999 peng1999 Apr 12, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to have this explained with a comment in the source!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments added in 587cf04

@peng1999
peng1999 requested a review from laurmaedje April 12, 2023 05:57
Comment thread library/src/text/shaping.rs Resolved
Comment thread library/src/text/shaping.rs Resolved
Comment thread library/src/text/shaping.rs Resolved
Comment thread library/src/layout/par.rs Resolved
Comment thread library/src/layout/par.rs
Comment on lines +1275 to +1276
let mut justification_ratio = 0.0;
let mut extra_justification = Abs::zero();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to have this explained with a comment in the source!

Comment thread library/src/layout/par.rs Resolved
Comment thread library/src/layout/par.rs Resolved
Comment thread library/src/layout/par.rs Resolved
@laurmaedje

Copy link
Copy Markdown
Member

Looking good in general! I dug a bit deeper into the cost and ratio calculations now and added some remarks.

@peng1999
peng1999 requested a review from laurmaedje April 13, 2023 05:32
@laurmaedje
laurmaedje merged commit ff16f3f into typst:main Apr 13, 2023
@laurmaedje

Copy link
Copy Markdown
Member

Thanks for these improvements!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants