Improve justification of Chinese/Japanese text (#542)

This commit is contained in:
Alex Sayers 2023-04-03 22:56:23 +09:00 committed by GitHub
parent 0b4dc6758e
commit 2c735294cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -84,6 +84,7 @@ pub struct BoxElem {
/// outset: (y: 3pt),
/// radius: 2pt,
/// )[rectangle].
/// ```
#[resolve]
#[fold]
pub outset: Sides<Option<Rel<Length>>>,

View File

@ -4,6 +4,7 @@ use std::str::FromStr;
use rustybuzz::{Feature, Tag, UnicodeBuffer};
use typst::font::{Font, FontVariant};
use typst::util::SliceExt;
use unicode_script::{Script, UnicodeScript};
use super::*;
use crate::layout::SpanMapper;
@ -69,11 +70,24 @@ impl ShapedGlyph {
}
/// Whether the glyph is justifiable.
///
/// Typst's basic justification strategy is to stretch all the spaces
/// in a line until the line fills the available width. However, some
/// scripts (notably Chinese and Japanese) don't use spaces.
///
/// In Japanese typography, the convention is to insert space evenly
/// between all glyphs. I assume it's the same in Chinese.
pub fn is_justifiable(&self) -> bool {
self.is_space() || matches!(self.c, '' | '。' | '、')
self.is_space() || is_spaceless(self.c.script())
}
}
/// Does this script separate its words using spaces?
fn is_spaceless(script: Script) -> bool {
use Script::*;
matches!(script, Hiragana | Katakana | Han)
}
/// A side you can go toward.
enum Side {
/// To the left-hand side.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB