Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(titles): captions and titles in Thai, Arabic, Hindi, Bengali, Korean and the rest draw their words, not boxes

Jareeref76b4fff5c7
diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md
index 1dad0f52..74e01f6f 100644
--- a/THIRD_PARTY_NOTICES.md
+++ b/THIRD_PARTY_NOTICES.md
@@ -110,21 +110,22 @@ file's licence is beside it in `src/crates/concat-text/fonts/`, as
- **Mystery Quest**: The Mystery Quest Project Authors. OFL. See `fonts/LICENSE-MysteryQuest.txt`.
- **Zen Tokyo Zoo**: The Zen Tokyo Zoo Project Authors. OFL. See `fonts/LICENSE-ZenTokyoZoo.txt`.
-The Noto faces in `src/crates/concat/fonts/` are the window's fallback for
-the scripts its own face does not draw, compiled into the binary
-(`src/crates/concat/src/fonts.rs`). Each is the Regular face from the
-notofonts project's own release, cut down with fonttools' `pyftsubset`;
-the release, the SHA-256 of the archive it came in and the command that
-cut it are recorded beside each face in `fonts.rs`. A subset is a Modified
+The Noto faces in `src/crates/concat-text/fonts/` are the fallback for the
+scripts the other faces do not draw, in the interface and in titles and
+captions alike, compiled into the binary once
+(`src/crates/concat-text/src/lib.rs`, `SCRIPT_FALLBACKS`). Each is the
+Regular face from the notofonts project's own release, cut down with
+fonttools' `pyftsubset`; the release, the SHA-256 of the archive it came
+in and the command that cut it are recorded beside each face there. A subset is a Modified
Version under the OFL, and keeps the family name the release gave it, as
the OFL allows for Noto, which reserves no font name.
-- **Noto Sans CJK SC** (a subset: the Chinese, Korean and kana characters the interface falls back on): Copyright 2014-2021 Adobe (http://www.adobe.com/), with Reserved Font Name 'Source'. OFL. See `src/crates/concat/fonts/LICENSE-NotoSansCJK.txt`.
-- **Noto Sans Arabic UI** 2.011 (a subset: the Arabic block and the characters the Arabic, Persian and Urdu interface uses): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/arabic). OFL. See `src/crates/concat/fonts/LICENSE-NotoSansArabicUI.txt`.
-- **Noto Sans Hebrew** 3.001 (a subset: the Hebrew block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/hebrew). OFL. See `src/crates/concat/fonts/LICENSE-NotoSansHebrew.txt`.
-- **Noto Sans Devanagari** 2.007 (a subset: the Devanagari block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/devanagari). OFL. See `src/crates/concat/fonts/LICENSE-NotoSansDevanagari.txt`.
-- **Noto Sans Bengali** 3.011 (a subset: the Bengali block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/bengali). OFL. See `src/crates/concat/fonts/LICENSE-NotoSansBengali.txt`.
-- **Noto Sans Thai** 2.002 (a subset: the Thai block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/thai). OFL. See `src/crates/concat/fonts/LICENSE-NotoSansThai.txt`.
+- **Noto Sans CJK SC** (a subset: GB 2312, Big5 level 1, JIS X 0208, the Hangul of KS X 1001 and the kana): Copyright 2014-2021 Adobe (http://www.adobe.com/), with Reserved Font Name 'Source'. OFL. See `src/crates/concat-text/fonts/LICENSE-NotoSansCJK.txt`.
+- **Noto Sans Arabic UI** 2.011 (a subset: the Arabic block and the characters the Arabic, Persian and Urdu interface uses): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/arabic). OFL. See `src/crates/concat-text/fonts/LICENSE-NotoSansArabicUI.txt`.
+- **Noto Sans Hebrew** 3.001 (a subset: the Hebrew block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/hebrew). OFL. See `src/crates/concat-text/fonts/LICENSE-NotoSansHebrew.txt`.
+- **Noto Sans Devanagari** 2.007 (a subset: the Devanagari block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/devanagari). OFL. See `src/crates/concat-text/fonts/LICENSE-NotoSansDevanagari.txt`.
+- **Noto Sans Bengali** 3.011 (a subset: the Bengali block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/bengali). OFL. See `src/crates/concat-text/fonts/LICENSE-NotoSansBengali.txt`.
+- **Noto Sans Thai** 2.002 (a subset: the Thai block): Copyright 2022 The Noto Project Authors (https://github.com/notofonts/thai). OFL. See `src/crates/concat-text/fonts/LICENSE-NotoSansThai.txt`.
The licence does not permit selling the font on its own; shipping the
`fonts/` directory as it stands satisfies it.
diff --git a/src/crates/concat/fonts/LICENSE-NotoSansArabicUI.txt b/src/crates/concat-text/fonts/LICENSE-NotoSansArabicUI.txt
similarity index 100%
rename from src/crates/concat/fonts/LICENSE-NotoSansArabicUI.txt
rename to src/crates/concat-text/fonts/LICENSE-NotoSansArabicUI.txt
diff --git a/src/crates/concat/fonts/LICENSE-NotoSansBengali.txt b/src/crates/concat-text/fonts/LICENSE-NotoSansBengali.txt
similarity index 100%
rename from src/crates/concat/fonts/LICENSE-NotoSansBengali.txt
rename to src/crates/concat-text/fonts/LICENSE-NotoSansBengali.txt
diff --git a/src/crates/concat/fonts/LICENSE-NotoSansCJK.txt b/src/crates/concat-text/fonts/LICENSE-NotoSansCJK.txt
similarity index 100%
rename from src/crates/concat/fonts/LICENSE-NotoSansCJK.txt
rename to src/crates/concat-text/fonts/LICENSE-NotoSansCJK.txt
diff --git a/src/crates/concat/fonts/LICENSE-NotoSansDevanagari.txt b/src/crates/concat-text/fonts/LICENSE-NotoSansDevanagari.txt
similarity index 100%
rename from src/crates/concat/fonts/LICENSE-NotoSansDevanagari.txt
rename to src/crates/concat-text/fonts/LICENSE-NotoSansDevanagari.txt
diff --git a/src/crates/concat/fonts/LICENSE-NotoSansHebrew.txt b/src/crates/concat-text/fonts/LICENSE-NotoSansHebrew.txt
similarity index 100%
rename from src/crates/concat/fonts/LICENSE-NotoSansHebrew.txt
rename to src/crates/concat-text/fonts/LICENSE-NotoSansHebrew.txt
diff --git a/src/crates/concat/fonts/LICENSE-NotoSansThai.txt b/src/crates/concat-text/fonts/LICENSE-NotoSansThai.txt
similarity index 100%
rename from src/crates/concat/fonts/LICENSE-NotoSansThai.txt
rename to src/crates/concat-text/fonts/LICENSE-NotoSansThai.txt
diff --git a/src/crates/concat/fonts/NotoSansArabicUI-Concat.ttf b/src/crates/concat-text/fonts/NotoSansArabicUI-Concat.ttf
similarity index 100%
rename from src/crates/concat/fonts/NotoSansArabicUI-Concat.ttf
rename to src/crates/concat-text/fonts/NotoSansArabicUI-Concat.ttf
diff --git a/src/crates/concat/fonts/NotoSansBengali-Concat.ttf b/src/crates/concat-text/fonts/NotoSansBengali-Concat.ttf
similarity index 100%
rename from src/crates/concat/fonts/NotoSansBengali-Concat.ttf
rename to src/crates/concat-text/fonts/NotoSansBengali-Concat.ttf
diff --git a/src/crates/concat/fonts/NotoSansCJKsc-Concat.otf b/src/crates/concat-text/fonts/NotoSansCJKsc-Concat.otf
similarity index 100%
rename from src/crates/concat/fonts/NotoSansCJKsc-Concat.otf
rename to src/crates/concat-text/fonts/NotoSansCJKsc-Concat.otf
diff --git a/src/crates/concat/fonts/NotoSansDevanagari-Concat.ttf b/src/crates/concat-text/fonts/NotoSansDevanagari-Concat.ttf
similarity index 100%
rename from src/crates/concat/fonts/NotoSansDevanagari-Concat.ttf
rename to src/crates/concat-text/fonts/NotoSansDevanagari-Concat.ttf
diff --git a/src/crates/concat/fonts/NotoSansHebrew-Concat.ttf b/src/crates/concat-text/fonts/NotoSansHebrew-Concat.ttf
similarity index 100%
rename from src/crates/concat/fonts/NotoSansHebrew-Concat.ttf
rename to src/crates/concat-text/fonts/NotoSansHebrew-Concat.ttf
diff --git a/src/crates/concat/fonts/NotoSansThai-Concat.ttf b/src/crates/concat-text/fonts/NotoSansThai-Concat.ttf
similarity index 100%
rename from src/crates/concat/fonts/NotoSansThai-Concat.ttf
rename to src/crates/concat-text/fonts/NotoSansThai-Concat.ttf
diff --git a/src/crates/concat-text/src/lib.rs b/src/crates/concat-text/src/lib.rs
index e245ad51..95b24eff 100644
--- a/src/crates/concat-text/src/lib.rs
+++ b/src/crates/concat-text/src/lib.rs
@@ -310,19 +310,104 @@ pub const BASE_FONTS: [&[u8]; 41] = [
include_bytes!("../fonts/ZenTokyoZoo-Regular.ttf"),
];
+/// Noto Sans CJK SC, cut to GB 2312, Big5 level 1, JIS X 0208, KS X 1001's
+/// Hangul, the kana, the CJK punctuation and fullwidth forms, and every
+/// character in the shipped locales. Under three megabytes rather than
+/// sixteen. Japanese and Traditional Chinese read in the Simplified face's
+/// forms where the regions differ; a machine's own font for them comes
+/// first.
+pub const NOTO_SANS_CJK_SC: &[u8] = include_bytes!("../fonts/NotoSansCJKsc-Concat.otf");
+
+/// Noto Sans Arabic UI 2.011, for Arabic, Persian and Urdu: the UI cut,
+/// whose ascent and descent fit a line of Latin, where Noto Sans Arabic's
+/// would stand taller than the rows it is put in. Urdu reads in its
+/// Naskh-like forms, not Nastaliq; every Urdu letter is there. From
+/// https://github.com/notofonts/arabic/releases/download/NotoSansArabicUI-v2.011/NotoSansArabicUI-v2.011.zip
+/// (SHA-256 f775ee259557195721466e99ec0316bbcd897f0429e25955f5d9582ca1202ce7).
+pub const NOTO_SANS_ARABIC_UI: &[u8] = include_bytes!("../fonts/NotoSansArabicUI-Concat.ttf");
+
+/// Noto Sans Hebrew 3.001. From
+/// https://github.com/notofonts/hebrew/releases/download/NotoSansHebrew-v3.001/NotoSansHebrew-v3.001.zip
+/// (SHA-256 df0a71814b4e63644cf40fcc4529111b61266b7a2dafbe95068b29a7520cc3cb).
+pub const NOTO_SANS_HEBREW: &[u8] = include_bytes!("../fonts/NotoSansHebrew-Concat.ttf");
+
+/// Noto Sans Devanagari 2.007, for Hindi. From
+/// https://github.com/notofonts/devanagari/releases/download/NotoSansDevanagari-v2.007/NotoSansDevanagari-v2.007.zip
+/// (SHA-256 820c7da45b1e63562cb41c0a8cac5d9a4202312043a3a040ed1325857ef469b1).
+pub const NOTO_SANS_DEVANAGARI: &[u8] = include_bytes!("../fonts/NotoSansDevanagari-Concat.ttf");
+
+/// Noto Sans Bengali 3.011. From
+/// https://github.com/notofonts/bengali/releases/download/NotoSansBengali-v3.011/NotoSansBengali-v3.011.zip
+/// (SHA-256 2aca24bf71665e66c32e14862ee58857dd8b8b07b6321ec7c96f3121ad6683b1).
+pub const NOTO_SANS_BENGALI: &[u8] = include_bytes!("../fonts/NotoSansBengali-Concat.ttf");
+
+/// Noto Sans Thai 2.002, the loopless face Thai interfaces are set in.
+/// From https://github.com/notofonts/thai/releases/download/NotoSansThai-v2.002/NotoSansThai-v2.002.zip
+/// (SHA-256 af889cc673fc714060ce5e4e088fbad32aa4c0571a19958efeaff128a22da485).
+pub const NOTO_SANS_THAI: &[u8] = include_bytes!("../fonts/NotoSansThai-Concat.ttf");
+
+/// A face for each script the base fonts have no glyphs for, which every
+/// title falls back on after the machine's own fonts: a caption in Thai,
+/// Arabic or Korean draws its words wherever it is painted, not boxes on a
+/// phone with no font of its own to lend. Each is the static Regular
+/// instance from the notofonts releases, unhinted, cut with fonttools'
+/// `pyftsubset` to the script's whole basic block, Latin-1, the general
+/// punctuation, U+25CC (the dotted circle the shaper puts under a mark
+/// with nothing to sit on) and every character the shipped locales use,
+/// with every layout feature kept so the joining and conjuncts still shape:
+///
+/// ```text
+/// pyftsubset <face>-Regular.ttf --unicodes=<block>,U+0020-007E,U+00A0-00FF,U+2000-206F,U+25CC \
+/// --text-file=<every locale's characters> --layout-features='*' --no-hinting \
+/// --notdef-outline --name-IDs=0,1,2,3,4,5,6,13,14 --output-file=<face>-Concat.ttf
+/// ```
+///
+/// The licences are `fonts/LICENSE-<face>.txt`. The window registers the
+/// same bytes as its interface's fallbacks (concat's fonts.rs).
+pub const SCRIPT_FALLBACKS: [&[u8]; 6] = [
+ NOTO_SANS_CJK_SC,
+ NOTO_SANS_ARABIC_UI,
+ NOTO_SANS_HEBREW,
+ NOTO_SANS_DEVANAGARI,
+ NOTO_SANS_BENGALI,
+ NOTO_SANS_THAI,
+];
+
/// Faces that used to be bundled and no longer are: a document that names
/// one is painted in the bundled face, not in whatever the system offers
/// for a name it does not know.
const RETIRED: [&str; 2] = ["Helvetica Neue", "Synonym"];
impl Fonts {
- /// The bundled face and the system's fonts.
+ /// The bundled faces, the system's fonts, and a face for every script
+ /// the bundled ones lack.
pub fn new() -> Fonts {
+ Fonts::with_system(true)
+ }
+
+ /// The faces titles can use, the machine's own among them or not. The
+ /// machine's come first, so where it has a face for a script, at the
+ /// title's weight, that face draws it; the script fallbacks come last.
+ fn with_system(system: bool) -> Fonts {
let mut db = fontdb::Database::new();
- db.load_system_fonts();
+ if system {
+ db.load_system_fonts();
+ // fontdb knows a desk's font folders and not a phone's, so on a
+ // phone the system had lent titles nothing: every script the
+ // bundled faces lack was boxes.
+ #[cfg(target_os = "android")]
+ for dir in ["/system/fonts", "/product/fonts"] {
+ db.load_fonts_dir(dir);
+ }
+ #[cfg(target_os = "ios")]
+ db.load_fonts_dir("/System/Library/Fonts");
+ }
for face in BUNDLED.iter().chain(BASE_FONTS.iter()) {
db.load_font_data(face.to_vec());
}
+ for face in SCRIPT_FALLBACKS {
+ db.load_font_source(fontdb::Source::Binary(Arc::new(face)));
+ }
Fonts {
db,
cache: Mutex::default(),
@@ -2165,6 +2250,56 @@ mod tests {
assert!(opaque_pixels(&out.png) > 100);
}
+ /// A caption in any language the app speaks draws its words with the
+ /// bundled faces alone, as on a phone whose fonts lend nothing: every
+ /// character lands on a face that has a glyph for it, never on a box.
+ #[test]
+ fn every_language_the_app_speaks_draws_without_the_machines_fonts() {
+ let fonts = Fonts::with_system(false);
+ let lines = [
+ ("Thai", "สวัสดีครับ ยินดีต้อนรับสู่คำบรรยาย"),
+ ("Arabic", "مرحبا بك في الترجمة"),
+ ("Persian", "سلام، به زیرنویس خوش آمدید"),
+ ("Urdu", "خوش آمدید، یہ سب ٹائٹل ہے"),
+ ("Hebrew", "שלום, ברוכים הבאים לכתוביות"),
+ ("Hindi", "नमस्ते, उपशीर्षक में आपका स्वागत है"),
+ ("Bengali", "হ্যালো, সাবটাইটেলে স্বাগতম"),
+ ("Korean", "안녕하세요, 자막에 오신 것을 환영합니다"),
+ ("Simplified Chinese", "你好,欢迎使用字幕"),
+ ("Traditional Chinese", "你好,歡迎使用字幕"),
+ ("Japanese", "こんにちは、字幕へようこそ。カタカナ"),
+ ("Russian", "Привет, добро пожаловать в субтитры"),
+ ("Ukrainian", "Привіт, ласкаво просимо до субтитрів ґ є ї"),
+ ("Vietnamese", "Xin chào, chào mừng đến với phụ đề ữ ỵ ẫ"),
+ ("Turkish", "Merhaba, altyazılara hoş geldiniz İ ğ ş"),
+ ("Polish", "Cześć, witaj w napisach ą ę ł ż ź"),
+ ("Croatian", "Bok, dobrodošli u titlove č ć đ š ž"),
+ ("German", "Grüß dich, willkommen bei den Untertiteln ß"),
+ ("Greek", "Γεια σας, καλώς ήρθατε στους υπότιτλους"),
+ ];
+ let mut boxes = Vec::new();
+ for (language, line) in lines {
+ let casting = fonts.cast(&style(line)).expect("casts");
+ let faces: Vec<_> = casting
+ .faces
+ .iter()
+ .map(|(data, index)| {
+ ttf_parser::Face::parse((**data).as_ref(), *index).expect("parses")
+ })
+ .collect();
+ for (at, ch) in line.char_indices() {
+ if ch.is_whitespace() || joins(ch) {
+ continue;
+ }
+ let face = &faces[casting.paragraphs[0][at]];
+ if face.glyph_index(ch).is_none() {
+ boxes.push(format!("{language}: {ch:?} (U+{:04X})", ch as u32));
+ }
+ }
+ }
+ assert!(boxes.is_empty(), "drawn as boxes:\n{}", boxes.join("\n"));
+ }
+
/// Left-aligned words start at the anchor and run right; right-aligned
/// ones end there; centred ones straddle it. Growing the words moves
/// only the far edge.
diff --git a/src/crates/concat/src/fonts.rs b/src/crates/concat/src/fonts.rs
index b7c0cd1e..a29f158a 100644
--- a/src/crates/concat/src/fonts.rs
+++ b/src/crates/concat/src/fonts.rs
@@ -21,63 +21,20 @@
//! interface needs - and Inter, which the title painter carries anyway,
//! stands behind the machine's own for Cyrillic and Greek.
//!
-//! The Noto faces are the static Regular instances from the notofonts
-//! releases, unhinted, cut with fonttools' `pyftsubset` to the script's
-//! basic block, Latin-1 and the general punctuation, U+25CC (the dotted
-//! circle the shaper puts under a mark with nothing to sit on) and every
-//! character the shipped locales use, with every layout feature kept so
-//! the joining and conjuncts still shape:
-//!
-//! ```text
-//! pyftsubset <face>-Regular.ttf --unicodes=<block>,U+0020-007E,U+00A0-00FF,U+2000-206F,U+25CC \
-//! --text-file=<every locale's characters> --layout-features='*' --no-hinting \
-//! --notdef-outline --name-IDs=0,1,2,3,4,5,6,13,14 --output-file=<face>-Concat.ttf
-//! ```
-//!
-//! Each face's release and the SHA-256 of its zip are on its constant
-//! below; the licence is `fonts/LICENSE-<face>.txt`, and
-//! THIRD_PARTY_NOTICES.md lists them.
+//! The Noto faces live in concat-text, which falls back on them for titles
+//! too; their sources, the way they were cut and their licences are on
+//! `concat_text::SCRIPT_FALLBACKS`, and THIRD_PARTY_NOTICES.md lists them.
use std::sync::Arc;
use slint::fontique_011::fontique::{self, FallbackKey, FamilyId, Language, Script};
-/// Noto Sans CJK SC, cut to the characters the interface falls back on:
-/// GB 2312, Big5 level 1, JIS X 0208, KS X 1001's Hangul, the kana, the
-/// CJK punctuation and fullwidth forms, and every character in the
-/// shipped locales, which the test below holds it to. Under three
-/// megabytes rather than sixteen. Japanese and Traditional Chinese read
-/// in the Simplified face's forms where the regions differ; on a machine
-/// with a font of its own for them, that font comes first.
-const CJK_FALLBACK: &[u8] = include_bytes!("../fonts/NotoSansCJKsc-Concat.otf");
-
-/// Noto Sans Arabic UI 2.011, for Arabic, Persian and Urdu: the UI cut,
-/// whose ascent and descent fit a line of Latin, where Noto Sans Arabic's
-/// would stand taller than the rows it is put in. Urdu reads in its
-/// Naskh-like forms, not Nastaliq; every Urdu letter is there. From
-/// https://github.com/notofonts/arabic/releases/download/NotoSansArabicUI-v2.011/NotoSansArabicUI-v2.011.zip
-/// (SHA-256 f775ee259557195721466e99ec0316bbcd897f0429e25955f5d9582ca1202ce7).
-const ARABIC_FALLBACK: &[u8] = include_bytes!("../fonts/NotoSansArabicUI-Concat.ttf");
-
-/// Noto Sans Hebrew 3.001. From
-/// https://github.com/notofonts/hebrew/releases/download/NotoSansHebrew-v3.001/NotoSansHebrew-v3.001.zip
-/// (SHA-256 df0a71814b4e63644cf40fcc4529111b61266b7a2dafbe95068b29a7520cc3cb).
-const HEBREW_FALLBACK: &[u8] = include_bytes!("../fonts/NotoSansHebrew-Concat.ttf");
-
-/// Noto Sans Devanagari 2.007, for Hindi. From
-/// https://github.com/notofonts/devanagari/releases/download/NotoSansDevanagari-v2.007/NotoSansDevanagari-v2.007.zip
-/// (SHA-256 820c7da45b1e63562cb41c0a8cac5d9a4202312043a3a040ed1325857ef469b1).
-const DEVANAGARI_FALLBACK: &[u8] = include_bytes!("../fonts/NotoSansDevanagari-Concat.ttf");
-
-/// Noto Sans Bengali 3.011. From
-/// https://github.com/notofonts/bengali/releases/download/NotoSansBengali-v3.011/NotoSansBengali-v3.011.zip
-/// (SHA-256 2aca24bf71665e66c32e14862ee58857dd8b8b07b6321ec7c96f3121ad6683b1).
-const BENGALI_FALLBACK: &[u8] = include_bytes!("../fonts/NotoSansBengali-Concat.ttf");
-
-/// Noto Sans Thai 2.002, the loopless face Thai interfaces are set in.
-/// From https://github.com/notofonts/thai/releases/download/NotoSansThai-v2.002/NotoSansThai-v2.002.zip
-/// (SHA-256 af889cc673fc714060ce5e4e088fbad32aa4c0571a19958efeaff128a22da485).
-const THAI_FALLBACK: &[u8] = include_bytes!("../fonts/NotoSansThai-Concat.ttf");
+const CJK_FALLBACK: &[u8] = concat_text::NOTO_SANS_CJK_SC;
+const ARABIC_FALLBACK: &[u8] = concat_text::NOTO_SANS_ARABIC_UI;
+const HEBREW_FALLBACK: &[u8] = concat_text::NOTO_SANS_HEBREW;
+const DEVANAGARI_FALLBACK: &[u8] = concat_text::NOTO_SANS_DEVANAGARI;
+const BENGALI_FALLBACK: &[u8] = concat_text::NOTO_SANS_BENGALI;
+const THAI_FALLBACK: &[u8] = concat_text::NOTO_SANS_THAI;
/// A face the window falls back on, and the fallback lists it goes on: a
/// script, and the locales fontique keeps a list of its own for under it