Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

CJK 1.11: Chinese language names in their common spellings

aiongga08198e13935
diff --git a/ROADMAP.md b/ROADMAP.md
index fe69906c1..f195feb27 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -87,7 +87,7 @@ DesignCraft aims at full Adobe InDesign parity — and to be better: faster, ope
- Edit ▸ Menus (`window.hideMenuItem`): hide menu items; menus with hidden items end in Show All Menu Items.
- View ▸ Separations Preview (`view.separations`): one process plate as ink density, or areas over an ink limit in red (a GCR preview of the rendered view; spot plates and the flattener preview aren't there).
- Separations Preview plates now come from each object's own colour (CMYK as authored; RGB and images through the colour settings, images via a cached 17³ table), so pure black shows only on the black plate.
-- Language (Character panel ▸ Language, `type.char {language}`): text in other languages isn't hyphenated or spell-checked with the English dictionaries, typographer's quotes follow the language (German „…“, French and Russian «…», Swiss, Spanish, Dutch, Nordic, Japanese and Traditional Chinese 「…」, Simplified Chinese “…”), the font's localized forms (OpenType `locl`: Turkish i, Bulgarian Cyrillic, Japanese / Chinese / Korean glyph variants) follow it too, and so does the fallback for CJK characters the font lacks: Japanese, Simplified Chinese, Traditional Chinese and Korean each try their own installed system fonts (Japanese starts with the craft-fonts Japanese faces, Mincho first, when built with them; Hangul, kana and Bopomofo pick their language's fonts whatever the language). Hyphenation dictionaries for other languages aren't bundled yet.
+- Language (Character panel ▸ Language, `type.char {language}`): text in other languages isn't hyphenated or spell-checked with the English dictionaries, typographer's quotes follow the language (German „…“, French and Russian «…», Swiss, Spanish, Dutch, Nordic, Japanese and Traditional Chinese 「…」, Simplified Chinese “…”), the font's localized forms (OpenType `locl`: Turkish i, Bulgarian Cyrillic, Japanese / Chinese / Korean glyph variants) follow it too, and so does the fallback for CJK characters the font lacks: Japanese, Simplified Chinese, Traditional Chinese and Korean each try their own installed system fonts (Japanese starts with the craft-fonts Japanese faces, Mincho first, when built with them; Hangul, kana and Bopomofo pick their language's fonts whatever the language). Language names from IDML keep their spelling; Chinese, Japanese and Korean are recognised in their common spellings and locale codes ("Simplified Chinese", "Chinese (Traditional)", `zh_CN`, `zh-Hant`, `ja_JP`, `ko-KR`). Hyphenation dictionaries for other languages aren't bundled yet.
- Books (`book.new/open/add/remove/list/styleSource/paginate/syncStyles/exportPdf`, Book panel): `.dcbook` files, continuous page numbering, style sync from a style source, one merged PDF.
- Track Changes (`changes.track/list/acceptAll/rejectAll`, Type ▸ Track Changes): typing is marked added (highlighted on screen), deletions are kept but hidden from layout and exports until accepted or rejected. Per-change accept/reject and authors aren't there yet.
- Track Changes: per-change Accept / Reject (`changes.accept`, `changes.reject`) and a Track Changes panel listing every change.
diff --git a/crates/doc/src/attrs.rs b/crates/doc/src/attrs.rs
index ff21dd7a9..24aac7558 100644
--- a/crates/doc/src/attrs.rs
+++ b/crates/doc/src/attrs.rs
@@ -193,25 +193,43 @@ impl Digits {
/// The BCP 47 tag of a language name as InDesign and IDML write it ("English: USA", "Chinese:
/// Simplified"), for the shaper's localized forms (`locl`). A variant we don't know falls back to
/// its language ("English: Australian" → `en`); an unknown language (or "[No Language]") has none.
+/// Case and punctuation don't matter, and Chinese, Japanese and Korean also go by their other
+/// common spellings and locale codes ("Simplified Chinese", "Chinese (Traditional)", "zh_CN",
+/// "zh-Hant", "ja_JP", "ko-KR").
pub fn language_tag(language: &str) -> Option<&'static str> {
- let l = language.trim().to_ascii_lowercase();
- let variant = match l.as_str() {
- "english: usa" => Some("en-US"),
- "english: uk" => Some("en-GB"),
- "english: canadian" => Some("en-CA"),
- "german: swiss 2006 reform" | "german: swiss" => Some("de-CH"),
- "french: canadian" => Some("fr-CA"),
- "portuguese: brazilian" => Some("pt-BR"),
- "norwegian: bokmål" | "norwegian: bokmal" => Some("nb"),
- "norwegian: nynorsk" => Some("nn"),
- "chinese: simplified" => Some("zh-Hans"),
- "chinese: traditional" => Some("zh-Hant"),
+ let l = language.to_lowercase();
+ let words: Vec<&str> = l.split(|c: char| !c.is_alphanumeric()).filter(|w| !w.is_empty()).collect();
+ let first = words.first().copied().unwrap_or("");
+ let has = |any: &[&str]| words.iter().any(|w| any.contains(w));
+ if first == "zh" || has(&["chinese"]) {
+ // The script decides; then the name; then the region.
+ return Some(if has(&["hans"]) {
+ "zh-Hans"
+ } else if has(&["hant"]) {
+ "zh-Hant"
+ } else if has(&["simplified", "cn", "sg"]) {
+ "zh-Hans"
+ } else if has(&["traditional", "tw", "hk", "mo"]) {
+ "zh-Hant"
+ } else {
+ "zh"
+ });
+ }
+ let variant = match words.join(" ").as_str() {
+ "english usa" => Some("en-US"),
+ "english uk" => Some("en-GB"),
+ "english canadian" => Some("en-CA"),
+ "german swiss 2006 reform" | "german swiss" => Some("de-CH"),
+ "french canadian" => Some("fr-CA"),
+ "portuguese brazilian" => Some("pt-BR"),
+ "norwegian bokmål" | "norwegian bokmal" => Some("nb"),
+ "norwegian nynorsk" => Some("nn"),
_ => None,
};
if variant.is_some() {
return variant;
}
- Some(match l.split(':').next().unwrap_or("").trim() {
+ Some(match first {
"english" => "en",
"german" => "de",
"french" => "fr",
@@ -238,9 +256,8 @@ pub fn language_tag(language: &str) -> Option<&'static str> {
"serbian" => "sr",
"macedonian" => "mk",
"turkish" => "tr",
- "japanese" => "ja",
- "korean" => "ko",
- "chinese" => "zh",
+ "japanese" | "ja" => "ja",
+ "korean" | "ko" => "ko",
"arabic" => "ar",
"persian" | "farsi" => "fa",
"urdu" => "ur",
@@ -783,6 +800,45 @@ mod tests {
assert_eq!(language_tag(""), None);
}
+ #[test]
+ fn chinese_japanese_and_korean_names_map_in_their_common_spellings() {
+ for (name, tag) in [
+ ("Chinese: Simplified", Some("zh-Hans")),
+ ("Simplified Chinese", Some("zh-Hans")),
+ ("Chinese Simplified", Some("zh-Hans")),
+ ("Chinese (Simplified)", Some("zh-Hans")),
+ (" chinese: SIMPLIFIED ", Some("zh-Hans")),
+ ("zh_CN", Some("zh-Hans")),
+ ("zh-CN", Some("zh-Hans")),
+ ("zh-Hans", Some("zh-Hans")),
+ ("zh-Hans-CN", Some("zh-Hans")),
+ ("Chinese: Traditional", Some("zh-Hant")),
+ ("Traditional Chinese", Some("zh-Hant")),
+ ("Chinese Traditional", Some("zh-Hant")),
+ ("Chinese (Traditional)", Some("zh-Hant")),
+ ("zh_TW", Some("zh-Hant")),
+ ("zh-TW", Some("zh-Hant")),
+ ("zh_HK", Some("zh-Hant")),
+ ("zh-HK", Some("zh-Hant")),
+ ("zh-Hant", Some("zh-Hant")),
+ ("Chinese", Some("zh")),
+ ("zh", Some("zh")),
+ ("Japanese", Some("ja")),
+ ("ja", Some("ja")),
+ ("ja_JP", Some("ja")),
+ ("ja-JP", Some("ja")),
+ ("Korean", Some("ko")),
+ ("ko_KR", Some("ko")),
+ ("ko-KR", Some("ko")),
+ ("English: USA", Some("en-US")),
+ ("German: Swiss 2006 Reform", Some("de-CH")),
+ ("Norwegian: Bokmål", Some("nb")),
+ ("[No Language]", None),
+ ] {
+ assert_eq!(language_tag(name), tag, "{name}");
+ }
+ }
+
#[test]
fn merge_and_resolve() {
let mut a = CharAttrs { size: Some(10.0), ..Default::default() };
diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs
index d81e1699a..dd88b7b58 100644
--- a/crates/engine/src/cmd/text.rs
+++ b/crates/engine/src/cmd/text.rs
@@ -480,6 +480,12 @@ fn smart_quotes(s: &Session, text: &str) -> String {
/// Typographer's quotes of a language: [double open, double close, single open, single close].
pub fn quote_marks(language: &str) -> [char; 4] {
+ // Chinese and Japanese go by the tag, which accepts every spelling of their names.
+ match designcraft_doc::language_tag(language) {
+ Some("zh-Hans") => return ['\u{201C}', '\u{201D}', '\u{2018}', '\u{2019}'],
+ Some("zh" | "zh-Hant" | "ja") => return ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}'],
+ _ => {}
+ }
let l = language.to_ascii_lowercase();
if l.starts_with("german: swiss") || l.contains("swiss") {
['\u{00AB}', '\u{00BB}', '\u{2039}', '\u{203A}']
@@ -508,10 +514,6 @@ pub fn quote_marks(language: &str) -> [char; 4] {
['\u{201E}', '\u{201D}', '\u{201A}', '\u{2019}']
} else if l.starts_with("swedish") || l.starts_with("finnish") {
['\u{201D}', '\u{201D}', '\u{2019}', '\u{2019}']
- } else if l.starts_with("chinese: simplified") {
- ['\u{201C}', '\u{201D}', '\u{2018}', '\u{2019}']
- } else if l.starts_with("japanese") || l.starts_with("chinese") {
- ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}']
} else if l.starts_with("danish") {
['\u{00BB}', '\u{00AB}', '\u{203A}', '\u{2039}']
} else {
@@ -1334,10 +1336,49 @@ mod language_tests {
assert_eq!(quote_marks("Chinese: Traditional"), ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}']);
assert_eq!(quote_marks("Chinese"), ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}']);
assert_eq!(quote_marks("Japanese"), ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}']);
+ // Other spellings of the language names take the same quotes.
+ assert_eq!(quote_marks("Simplified Chinese"), ['\u{201C}', '\u{201D}', '\u{2018}', '\u{2019}']);
+ assert_eq!(quote_marks("zh_CN"), ['\u{201C}', '\u{201D}', '\u{2018}', '\u{2019}']);
+ assert_eq!(quote_marks("Chinese (Traditional)"), ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}']);
+ assert_eq!(quote_marks("zh-TW"), ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}']);
+ assert_eq!(quote_marks("ja_JP"), ['\u{300C}', '\u{300D}', '\u{300E}', '\u{300F}']);
for l in ["Japanese", "Korean", "Chinese", "Chinese: Simplified", "Chinese: Traditional"] {
assert!(!designcraft_compose::is_english(l), "{l}: no English hyphenation or spelling");
}
}
+
+ #[test]
+ fn idml_language_names_keep_their_spelling_and_pick_the_language() {
+ use std::io::Read;
+ // A stand-in for Songti SC (first in the Simplified Chinese chain; the real one when installed).
+ designcraft_fonts::FontDb::global().add_font(designcraft_fonts::testing::font_with("Songti SC", &['直']).unwrap());
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [72, 72, 300, 200], "content": "text", "text": "直"})).unwrap();
+ s.execute("text.select", &json!({"story": r["story"], "anchor": 0, "focus": 3})).unwrap();
+ s.execute("type.char", &json!({"attrs": {"language": "Simplified Chinese"}})).unwrap();
+ let idml = designcraft_idml::export_idml(&s.doc().unwrap().doc);
+ let mut z = zip::ZipArchive::new(std::io::Cursor::new(idml.clone())).unwrap();
+ let stories: String = (0..z.len())
+ .map(|i| {
+ let mut f = z.by_index(i).unwrap();
+ let mut x = String::new();
+ if f.name().starts_with("Stories/") {
+ f.read_to_string(&mut x).unwrap();
+ }
+ x
+ })
+ .collect();
+ assert!(stories.contains(r#"AppliedLanguage="$ID/Simplified Chinese""#), "{stories}");
+ // The name comes back as written and sets the text as Simplified Chinese.
+ let back = designcraft_idml::import_idml(&idml).unwrap();
+ let st = back.stories.values().find(|st| st.text.contains('直')).unwrap();
+ let (_, base) = back.styles.resolve_para(&st.paras[0]);
+ assert_eq!(back.styles.resolve_char(&base, st.char_format_at(0)).language, "Simplified Chinese");
+ let cs = designcraft_compose::compose_story(&back, st.id, &Default::default());
+ let face = cs.frames[0].lines[0].glyphs.iter().find(|g| g.byte == 0).unwrap().face;
+ assert_eq!(face.family, "Songti SC");
+ }
}
#[cfg(test)]
diff --git a/docs/cjk.md b/docs/cjk.md
index 72c46c8ed..ccb5a03dd 100644
--- a/docs/cjk.md
+++ b/docs/cjk.md
@@ -42,6 +42,7 @@ presets or tables are copied. Our default mojikumi and kinsoku tables are built
| Ruby | group ruby only, fixed 50 % size, no options, doesn't affect leading |
| Kenten | the IDML kinds and a custom character, drawn as characters; no position, size, alignment, colour or font |
| Composite fonts | done: model, `style.compositeFont.*`, shaping, IDML, font list, replace and Preflight; no dialog |
+| Languages | Japanese, Korean, Simplified and Traditional Chinese reach the shaper (`locl`), font fallback, line breaking and typographer's quotes; IDML language names are kept as written and recognised in their common spellings and locale codes ("Simplified Chinese", "Chinese (Traditional)", `zh_CN`, `zh-Hant`, `ja_JP`, `ko-KR`) |
| CJK fonts | Japanese faces (Shippori Mincho first) from the optional craft-fonts build input; fallback by language (Japanese: the craft-fonts faces, then system fonts; Simplified and Traditional Chinese, Korean: system fonts); document fonts: the fonts of a `Document Fonts` folder beside an opened file belong to that document (composition, export, font menus), ahead of installed fonts of the same name, and are forgotten when it closes; Package copies every font file that draws text, fallback fonts included (licence permitting). Font files stay in memory for the session (reopening a document reuses them) and aren't read on the web |
| Everything else below | missing |