From 2c735294cd5e47f1f1eb6402a3b8c500dccd047b Mon Sep 17 00:00:00 2001 From: Alex Sayers Date: Mon, 3 Apr 2023 22:56:23 +0900 Subject: Improve justification of Chinese/Japanese text (#542) --- library/src/text/shaping.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'library/src/text') 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. -- cgit v1.2.3