diff options
| author | Alex Sayers <alex@asayers.com> | 2023-04-03 22:56:23 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-03 15:56:23 +0200 |
| commit | 2c735294cd5e47f1f1eb6402a3b8c500dccd047b (patch) | |
| tree | 285aeb52c530fcee7361384128905791bf51fe2e /library/src | |
| parent | 0b4dc6758ec165be9ac78893c2cca03089255a3b (diff) | |
Improve justification of Chinese/Japanese text (#542)
Diffstat (limited to 'library/src')
| -rw-r--r-- | library/src/layout/container.rs | 1 | ||||
| -rw-r--r-- | library/src/text/shaping.rs | 16 |
2 files changed, 16 insertions, 1 deletions
diff --git a/library/src/layout/container.rs b/library/src/layout/container.rs index 0f81a1df..166d6c09 100644 --- a/library/src/layout/container.rs +++ b/library/src/layout/container.rs @@ -84,6 +84,7 @@ pub struct BoxElem { /// outset: (y: 3pt), /// radius: 2pt, /// )[rectangle]. + /// ``` #[resolve] #[fold] pub outset: Sides<Option<Rel<Length>>>, diff --git a/library/src/text/shaping.rs b/library/src/text/shaping.rs index b895b6f9..2dd0cd6d 100644 --- a/library/src/text/shaping.rs +++ b/library/src/text/shaping.rs @@ -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. |
