mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge PR #7152: Type: register craft fonts of every script, not only the Japanese ones
This commit is contained in:
commit
5fd5b7adcb
5 files changed
+120
-19
No files matched your search
@@ -1,9 +1,12 @@
|
||||
//! Fonts from the optional craft-fonts build input (https://github.com/storytold/craft-fonts).
|
||||
//!
|
||||
//! `CRAFT_FONTS` is empty unless the app was built with `CRAFT_FONTS_DIR=<craft-fonts checkout>`
|
||||
//! (see `build.rs`); every user of it must work when it is empty. Today it carries the Japanese
|
||||
//! fonts: BIZ UDPGothic (UI) and Shippori Mincho / BIZ UDMincho (serif document text). The web
|
||||
//! build (wasm32) embeds none of them: they don't fit its size cap (see `build.rs`).
|
||||
//! (see `build.rs`); every user of it must work when it is empty. It carries every font in the
|
||||
//! checkout's `fonts/manifest.txt`, whatever its scripts: the Japanese fonts BIZ UDPGothic (UI) and
|
||||
//! Shippori Mincho / BIZ UDMincho (serif document text) and, in newer checkouts, fonts for other
|
||||
//! scripts such as Noto Sans CJK SC, Noto Sans Arabic and Noto Sans Thai.
|
||||
//! [`crate::fonts::FontDb::new`] registers all of them. The web build (wasm32) embeds none of
|
||||
//! them: they don't fit its size cap (see `build.rs`).
|
||||
|
||||
/// A font from the optional craft-fonts build input (empty unless built with `CRAFT_FONTS_DIR`).
|
||||
pub struct CraftFont {
|
||||
|
||||
@@ -124,7 +124,7 @@ fn collection_isolation_preserves_requested_face() {
|
||||
let blob = Blob::new(Arc::new(bytes));
|
||||
assert!(isolated_face(blob.clone(), 2).is_none());
|
||||
let mut db = FontDb::new();
|
||||
db.register_blob(blob.clone());
|
||||
db.register_blob(blob.clone(), None);
|
||||
let mut style = CharStyle { font_family: "Numeric Test".into(), font_style: "30".into(), ..Default::default() };
|
||||
db.select_named_face(&mut style);
|
||||
assert_eq!(db.faces(&style.font_family)[0].postscript_name.as_deref(), Some("Collection30"));
|
||||
@@ -188,3 +188,66 @@ fn localized_family_names_map_english_families_to_native_names() {
|
||||
assert!(!local.contains_key(DEFAULT_FAMILY));
|
||||
assert!(local.values().all(|v| !v.is_empty()));
|
||||
}
|
||||
|
||||
/// A craft-fonts manifest entry for `bytes`, as `build.rs` writes one per manifest line.
|
||||
fn craft(family: &'static str, scripts: &'static [&'static str], bytes: Vec<u8>) -> crate::CraftFont {
|
||||
crate::CraftFont { family, style: "Regular", scripts, bytes: bytes.leak() }
|
||||
}
|
||||
|
||||
/// A TrueType collection of single-face fonts.
|
||||
fn collection(faces: &[Vec<u8>]) -> Vec<u8> {
|
||||
let mut bytes = b"ttcf\0\x01\0\0".to_vec();
|
||||
bytes.extend((faces.len() as u32).to_be_bytes());
|
||||
let mut start = 12 + 4 * faces.len();
|
||||
for face in faces {
|
||||
bytes.extend((start as u32).to_be_bytes());
|
||||
start += face.len();
|
||||
}
|
||||
for face in faces {
|
||||
let start = bytes.len();
|
||||
bytes.extend(face);
|
||||
let count = u16::from_be_bytes(bytes[start + 4..start + 6].try_into().unwrap()) as usize;
|
||||
for i in 0..count {
|
||||
let r = start + 12 + i * 16 + 8;
|
||||
let offset = u32::from_be_bytes(bytes[r..r + 4].try_into().unwrap()) + start as u32;
|
||||
bytes[r..r + 4].copy_from_slice(&offset.to_be_bytes());
|
||||
}
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
/// Single-face craft fonts of every script register under their manifest family names, whatever
|
||||
/// family their `name` tables give, and the ones the fallback lists name are in the fallback
|
||||
/// stack. The Hans and Arab families are fallback-list names, and no craft-fonts manifest uses
|
||||
/// any of the three, so `FontDb::new` does not register them from the build's own craft fonts.
|
||||
#[test]
|
||||
fn craft_fonts_of_every_script_register_under_their_manifest_families() {
|
||||
let fonts = [
|
||||
craft("WenQuanYi Micro Hei", &["Hans", "Latn"], localized_font(&INTER_REGULAR, "WenQuanYi Micro Hei", "WenQuanYi Micro Hei")),
|
||||
// The `name` tables of these two give the family "Numeric Test".
|
||||
craft("Geeza Pro", &["Arab"], numeric_font(&INTER_REGULAR, "20", "CraftTestArabic")),
|
||||
craft("Craft Test Gothic", &["Jpan", "Latn"], numeric_font(&INTER_SEMIBOLD, "30", "CraftTestGothic")),
|
||||
];
|
||||
let mut db = FontDb::new();
|
||||
assert!(fonts.iter().all(|f| !db.has_family(f.family)));
|
||||
db.register_craft_fonts(&fonts);
|
||||
let families = db.families();
|
||||
for f in &fonts {
|
||||
assert!(families.iter().any(|n| n == f.family), "{} registered under its manifest family: {families:?}", f.family);
|
||||
}
|
||||
assert!(!db.has_family("Numeric Test"), "{families:?}");
|
||||
for family in ["WenQuanYi Micro Hei", "Geeza Pro"] {
|
||||
assert!(db.fallback_stack().any(|f| f == family), "{family} in {:?}", db.fallback_stack().collect::<Vec<_>>());
|
||||
}
|
||||
}
|
||||
|
||||
/// Each face of a craft font collection keeps the family its `name` table gives.
|
||||
#[test]
|
||||
fn craft_font_collections_keep_their_face_families() {
|
||||
let faces = [localized_font(&INTER_REGULAR, "Craft Test A", "クラフト A"), localized_font(&INTER_SEMIBOLD, "Craft Test B", "クラフト B")];
|
||||
let fonts = [craft("Craft Test Collection", &["Jpan", "Latn"], collection(&faces))];
|
||||
let mut db = FontDb::new();
|
||||
db.register_craft_fonts(&fonts);
|
||||
assert!(db.has_family("Craft Test A") && db.has_family("Craft Test B"), "{:?}", db.families());
|
||||
assert!(!db.has_family("Craft Test Collection"));
|
||||
}
|
||||
+20
-10
@@ -1,6 +1,6 @@
|
||||
//! Font database: bundled fonts (always available, also on the web), the optional craft-fonts
|
||||
//! Japanese fonts ([`crate::craft_fonts`], when built with `CRAFT_FONTS_DIR`), optional system
|
||||
//! fonts found by scanning platform directories (plus Fontconfig's file list on Linux),
|
||||
//! fonts of every script ([`crate::craft_fonts`], when built with `CRAFT_FONTS_DIR`), optional
|
||||
//! system fonts found by scanning platform directories (plus Fontconfig's file list on Linux),
|
||||
//! user-registered font data, and PostScript-name lookup for PSD import.
|
||||
|
||||
use std::collections::HashMap;
|
||||
@@ -225,14 +225,22 @@ impl FontDb {
|
||||
db.register_static_font(bytes.as_slice());
|
||||
}
|
||||
// The optional craft-fonts (empty unless built with CRAFT_FONTS_DIR; always empty on
|
||||
// wasm32), before any system font.
|
||||
for f in crate::craft_fonts::CRAFT_FONTS.iter().filter(|f| f.is_japanese()) {
|
||||
db.register_static_font(f.bytes);
|
||||
}
|
||||
// wasm32), every script, before any system font.
|
||||
db.register_craft_fonts(crate::craft_fonts::CRAFT_FONTS);
|
||||
db.refresh_generics();
|
||||
db
|
||||
}
|
||||
|
||||
/// Registers craft-fonts manifest entries of every script. A single-face file registers under
|
||||
/// its manifest family, whatever family its `name` table gives. Each face of a collection
|
||||
/// (TTC/OTC) keeps the family its `name` table gives.
|
||||
fn register_craft_fonts(&mut self, fonts: &[crate::CraftFont]) {
|
||||
for f in fonts {
|
||||
let family = (face_count(f.bytes) == 1).then_some(f.family);
|
||||
self.register_blob(Blob::new(Arc::new(f.bytes)), family);
|
||||
}
|
||||
}
|
||||
|
||||
/// Bundled fonts plus the fonts installed on this machine (no-op on the web).
|
||||
pub fn with_system_fonts() -> Self {
|
||||
let mut db = Self::new();
|
||||
@@ -279,16 +287,18 @@ impl FontDb {
|
||||
|
||||
/// Registers font data (TTF/OTF, or every face of a TTC/OTC). Returns the family names added.
|
||||
pub fn register_font_data(&mut self, bytes: Vec<u8>) -> Vec<String> {
|
||||
self.register_blob(Blob::new(Arc::new(bytes)))
|
||||
self.register_blob(Blob::new(Arc::new(bytes)), None)
|
||||
}
|
||||
|
||||
/// Registers embedded font data without copying it.
|
||||
fn register_static_font(&mut self, bytes: &'static [u8]) -> Vec<String> {
|
||||
self.register_blob(Blob::new(Arc::new(bytes)))
|
||||
self.register_blob(Blob::new(Arc::new(bytes)), None)
|
||||
}
|
||||
|
||||
fn register_blob(&mut self, blob: Blob<u8>) -> Vec<String> {
|
||||
let added = self.fcx.collection.register_fonts(blob, None);
|
||||
/// Registers every face in `blob`, under `family` when given, else under the family each
|
||||
/// face's `name` table gives.
|
||||
fn register_blob(&mut self, blob: Blob<u8>, family: Option<&str>) -> Vec<String> {
|
||||
let added = self.fcx.collection.register_fonts(blob, family.map(|f| FontInfoOverride { family_name: Some(f), ..Default::default() }));
|
||||
let mut names = Vec::new();
|
||||
for (id, _) in added {
|
||||
if let Some(n) = self.fcx.collection.family_name(id)
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
//! web), optional system fonts from platform directory scans plus Fontconfig's Linux file list,
|
||||
//! user font data, TrueType collections, PostScript-name lookup. Builds made with the optional
|
||||
//! craft-fonts input
|
||||
//! (`CRAFT_FONTS_DIR`, see [`craft_fonts`]) also carry its Japanese fonts as fallbacks.
|
||||
//! (`CRAFT_FONTS_DIR`, see [`craft_fonts`]) also carry its fonts of every script (a single-face
|
||||
//! file under its manifest family name); the ones the fallback lists name are fallbacks.
|
||||
//! * [`layout`]: shaping and line layout with [parley] (HarfRust shaping, bidi, line breaking),
|
||||
//! point and paragraph (box) text, per-run styles, Photoshop leading/indent/spacing rules.
|
||||
//! * [`render`]: anti-aliased rasterization (exact-area accumulation, f32 coverage) into a
|
||||
|
||||
@@ -1728,10 +1728,17 @@ fn craft_fonts_cover_japanese_without_system_fonts() {
|
||||
let l = e.layout(&point(text, 24.0), 72.0);
|
||||
assert_eq!(l.glyphs.len(), text.chars().count());
|
||||
assert!(l.glyphs.iter().all(|g| g.id != 0), "no .notdef with craft-fonts");
|
||||
// Sans (Inter) runs fall back to the Gothic UI family.
|
||||
assert_eq!(craft_family_of(&l), vec![crate::craft_fonts::UI_JAPANESE_FAMILY]);
|
||||
// Serif runs fall back to a Mincho face when the build has one (not the web build).
|
||||
if crate::CRAFT_FONTS.iter().any(|f| f.is_mincho()) {
|
||||
// Sans (Inter) runs fall back to the first CJK craft family in the UI locale's script order:
|
||||
// the Gothic UI family when Japanese leads that order or the build has no other CJK craft font.
|
||||
let cjk = |f: &crate::CraftFont| f.scripts.iter().any(|s| matches!(*s, "Jpan" | "Hans" | "Hant" | "Kore"));
|
||||
let first = e.fonts.fallback_stack().find_map(|fam| crate::CRAFT_FONTS.iter().find(|f| f.family == fam && cjk(f))).map(|f| f.family);
|
||||
if !crate::CRAFT_FONTS.iter().any(|f| cjk(f) && !f.is_japanese()) || crate::cjk::ui_script_order()[0] == crate::cjk::CjkScript::Japanese {
|
||||
assert_eq!(first, Some(crate::craft_fonts::UI_JAPANESE_FAMILY));
|
||||
}
|
||||
assert_eq!(craft_family_of(&l), first.into_iter().collect::<Vec<_>>());
|
||||
// Serif runs fall back to a Mincho face when the build has one (not the web build) and the
|
||||
// Japanese craft fonts come first.
|
||||
if first == Some(crate::craft_fonts::UI_JAPANESE_FAMILY) && crate::CRAFT_FONTS.iter().any(|f| f.is_mincho()) {
|
||||
let mut t = point(text, 24.0);
|
||||
t.font_family = "Times New Roman".into();
|
||||
let l = e.layout(&t, 72.0);
|
||||
@@ -1744,6 +1751,23 @@ fn craft_fonts_cover_japanese_without_system_fonts() {
|
||||
assert!(craft_family_of(&l).is_empty());
|
||||
}
|
||||
|
||||
/// Chinese, Arabic and Thai set in Inter fall back to the craft font for their script when the
|
||||
/// build has one (Noto Sans CJK SC, Noto Sans Arabic and Noto Sans Thai in newer craft-fonts).
|
||||
#[test]
|
||||
fn craft_fonts_cover_their_other_scripts_without_system_fonts() {
|
||||
let mut e = TextEngine::new();
|
||||
for (script, text) in [("Hans", "中文图层"), ("Arab", "مرحبا"), ("Thai", "ภาษาไทย")] {
|
||||
if !crate::CRAFT_FONTS.iter().any(|f| f.scripts.contains(&script)) {
|
||||
eprintln!("skipping {script}: built without a craft font for it (set CRAFT_FONTS_DIR to a craft-fonts checkout)");
|
||||
continue;
|
||||
}
|
||||
let l = e.layout(&point(text, 24.0), 72.0);
|
||||
assert!(!l.glyphs.is_empty() && l.glyphs.iter().all(|g| g.id != 0), "{script}: no .notdef with craft-fonts");
|
||||
let fams = craft_family_of(&l);
|
||||
assert!(fams.iter().any(|fam| crate::CRAFT_FONTS.iter().any(|f| f.family == *fam && f.scripts.contains(&script))), "{script}: drawn with {fams:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn works_without_craft_fonts() {
|
||||
// Whatever the build: the bundled fonts load, Latin lays out, and Japanese never panics
|
||||
|
||||
Reference in new issue
Block a user