diff --git a/Cargo.lock b/Cargo.lock index 68ac647da..bb1435d19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2225,6 +2225,7 @@ dependencies = [ "craft-font-catalog", "figcraft-doc", "figcraft-geom", + "flate2", "harfrust 0.13.3", "icu_segmenter", "log", diff --git a/ROADMAP.md b/ROADMAP.md index 5536c5b08..ad2ae9f69 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~26 points and ~162–292 h away (one Opus 5.5 agent; ~33–67 h wall-clock with 5 agents) -> **Last reviewed:** 2026-11-15 · **Last updated:** 2026-11-18 · **Change:** minor (the browser build uses team libraries and plugin directories on a server; CLI post-parse errors escape terminal controls in paths and failure text; M6 `.fig` export writes text on a path (#2323), list spacing, hanging punctuation and lists, vertical trim (#2305), vector region paints (#2302), image adjustments (#2297) and a page's prototype background (#2326); M8 Tidy up keeps a row's or column's order and sizes and evens the gaps, rounds the gap, tidies overlapping rows and grids along their longer axis, and raises a negative gap so no layer reaches the one two before it, as measured in Figma (#2319); a polygon's or star's Count handle and a double-click on a layer's icon (zoom, range anchor) follow Figma; Tab while renaming moves only the rename and wraps from the last layer; Align is unavailable for a layer in an auto layout frame's flow; the Scale tool pivots on the opposite handle and starts its origin at the centre; Paste over selection puts the copies just above the selected layer; Crop from the image fill's menu crops with a drag (#2307); M10 code export writes every colour and gradient fill in paint order for CSS, SwiftUI, Compose and Flutter (#2271), CSS as one layered background; 24 pt icon buttons draw 16 pt glyphs (#2227, #2238); M5 marquee selection follows Figma's measured rules and Select layer lists locked layers with a padlock (#1560); M7 the Design panel shows the page's variable modes with nothing selected (#2215); M12 mind maps in a section or frame without a place centre on the view, stickies take a Small–Huge text-size menu, and M9 `.jam` shapes with text import as native board shapes (#1808); M4 a boolean group's corner radius rounds its result (#2222), Flatten bakes a point's corner radius into its path (#2195) and loops close through a joining segment (#1951); library links survive moving or cloning the project folder (#2132); renaming a variable group to a path with `/` nests it (#2353); SVG import, widget images, batches and grids take bounded work, and a host's server key is never sent over plain `ws://` to another machine (#1923); CI prefetches dependencies for Sonar coverage and runs Harden-Runner on every job; releases FigCraft v0.18.0 and v0.18.1 (Flatpak update fix); readiness estimates unchanged) · **Target:** Figma Design (desktop app 126.10.7) +> **Last reviewed:** 2026-11-15 · **Last updated:** 2026-11-18 · **Change:** minor (web builds with craft-fonts embed Noto Sans Arabic by default so Persian and Arabic text draws without extra build flags (#1902); the browser build uses team libraries and plugin directories on a server; CLI post-parse errors escape terminal controls in paths and failure text; M6 `.fig` export writes text on a path (#2323), list spacing, hanging punctuation and lists, vertical trim (#2305), vector region paints (#2302), image adjustments (#2297) and a page's prototype background (#2326); M8 Tidy up keeps a row's or column's order and sizes and evens the gaps, rounds the gap, tidies overlapping rows and grids along their longer axis, and raises a negative gap so no layer reaches the one two before it, as measured in Figma (#2319); a polygon's or star's Count handle and a double-click on a layer's icon (zoom, range anchor) follow Figma; Tab while renaming moves only the rename and wraps from the last layer; Align is unavailable for a layer in an auto layout frame's flow; the Scale tool pivots on the opposite handle and starts its origin at the centre; Paste over selection puts the copies just above the selected layer; Crop from the image fill's menu crops with a drag (#2307); M10 code export writes every colour and gradient fill in paint order for CSS, SwiftUI, Compose and Flutter (#2271), CSS as one layered background; 24 pt icon buttons draw 16 pt glyphs (#2227, #2238); M5 marquee selection follows Figma's measured rules and Select layer lists locked layers with a padlock (#1560); M7 the Design panel shows the page's variable modes with nothing selected (#2215); M12 mind maps in a section or frame without a place centre on the view, stickies take a Small–Huge text-size menu, and M9 `.jam` shapes with text import as native board shapes (#1808); M4 a boolean group's corner radius rounds its result (#2222), Flatten bakes a point's corner radius into its path (#2195) and loops close through a joining segment (#1951); library links survive moving or cloning the project folder (#2132); renaming a variable group to a path with `/` nests it (#2353); SVG import, widget images, batches and grids take bounded work, and a host's server key is never sent over plain `ws://` to another machine (#1923); CI prefetches dependencies for Sonar coverage and runs Harden-Runner on every job; releases FigCraft v0.18.0 and v0.18.1 (Flatpak update fix); readiness estimates unchanged) · **Target:** Figma Design (desktop app 126.10.7) FigCraft is a clean-room, open-source, pure-Rust reimplementation of the Figma design workflow, native on macOS, Windows, Linux and FreeBSD and in the browser, fully drivable by agents through @@ -105,7 +105,7 @@ native speakers' reviews of the nine language drafts need people. | Traditional Chinese (Taiwan) | zh-Hant | 2,647 / 2,647 entries (100%, counted 2026-11-03) | draft (needs native review) | | Spanish | es, es-419 | 100% (es-419: an overlay of 101 entries) | draft (needs native review) | | Hindi | hi | 0% | none | -| Arabic | ar | 2,508 / 2,508 entries (100%, counted 2026-10-28) | draft; the window mirrors (panels swap sides); native review and mirrored panel rows pending | +| Arabic | ar | 2,508 / 2,508 entries (100%, counted 2026-10-28) | draft; web builds with craft-fonts offer Arabic by default; the window mirrors (panels swap sides); native review and mirrored panel rows pending | | French | fr | 95% | draft (needs native review) | | Portuguese (Brazil) | pt-BR | 95% | draft (needs native review) | | Indonesian | id | 99% | draft (needs native review) | @@ -134,6 +134,7 @@ Ranked; detail and milestones in [`docs/roadmap.md`](docs/roadmap.md). ## Progress log +- **2026-11-07** · M10, #1902: web builds with craft-fonts embed Noto Sans Arabic Regular by default, so Persian and Arabic text has a fallback without `FIGCRAFT_EMBED_SCRIPTS=Arab`; required web builds reject catalogs without that face. Web faces are compressed losslessly at build time and restored once at runtime to keep the default catalog within the WASM size budget. A decode failure logs the unavailable face once; a compiler regression checks generated catalog initialization. Additional scripts remain opt-in. Exact Iran Sans appearance still needs the installed font through supported browsers' local font access. Numbers unchanged. - **2026-11-05** · M2, #1823: Suggest auto layout is its own command. `layout.suggest` used to run `layout.add` and made one row of everything. It now splits a frame's layers into rows or columns by the gaps between them, wraps each band of several layers in a hugging frame without fill and repeats inside it, so the design gets as many auto layout frames as it needs and keeps its placement where the gaps are even (the frame keeps its size). Uneven gaps, bands that start at different offsets across the flow (lined up at the smallest) and layers that overlap along both axes are reported as `warnings`, and instances and layers inside them are refused. `layout.add` is unchanged. Tests in `layout_suggest_tests.rs`. Numbers unchanged. - **2026-11-05** · M3, #2002: the Local variables window selects several variables with a click, Shift+click (range) and Cmd/Ctrl+click (toggle), cleared by Escape or a click on the empty space; delete, move to a group and scope act on the selected local variables as one undo step (`command.batch`); inherited variables can be selected but are left out, since they change in their own collection, and the bulk menu needs two or more local variables selected (otherwise the row shows its own menu). Rename and value edits stay single-row. - **2026-11-05** · M10, #2014: the desktop UI takes a system face for Ethiopic, Devanagari, Gurmukhi, Gujarati, Tamil, Telugu, Kannada and Malayalam, Sinhala, Lao, Myanmar, Khmer, Armenian and Georgian as it did for Hebrew and Arabic (`bidi::script_fallbacks`: one sample letter per script, the system's families for each; Bengali and Thai keep `crate::cjk`'s choice), so a layer name or a String variable's Amharic value draws in the panels instead of boxes. Test: `bidi::tests::other_scripts_take_an_installed_system_face`. @@ -370,6 +371,7 @@ Ranked; detail and milestones in [`docs/roadmap.md`](docs/roadmap.md). | 2026-11-15 | patch | CLI usage and operation failures escape terminal controls in paths and error text, using the same printable-text rule as info and script diagnostics; exit codes and ordinary quotes and paths stay unchanged. | | 2026-11-10 | minor | M4, #2222: a boolean group takes a corner radius that rounds every sharp corner of its result. Numbers unchanged | | 2026-11-10 | minor | M12, #2226 (step 1 of 4): mind maps in the document and engine. `board.mindMap` places a first node with three children on curved branches and `board.mindMapAdd` adds a child or a sibling; the normalize pass lays a touched map out again from its first node (88 between columns, 48 between siblings, stacks centred on their parents). No toolbar entry yet; numbers unchanged. | +| 2026-11-07 | patch | M10, #1902: web builds with craft-fonts embed Noto Sans Arabic Regular by default, so Persian and Arabic text has a fallback without `FIGCRAFT_EMBED_SCRIPTS=Arab`; required web builds reject catalogs without that face. Web faces are compressed losslessly at build time and restored once at runtime to keep the default catalog within the WASM size budget. A decode failure logs the unavailable face once; a compiler regression checks generated catalog initialization. Additional scripts remain opt-in. Exact Iran Sans appearance still needs the installed font through supported browsers' local font access. Numbers unchanged. | | 2026-11-03 | minor | Traditional Chinese (Taiwan) (`zh-Hant`, 繁體中文(台灣)) draft catalog: every UI string (1,975), command label (641) and menu wording (31), translated clean-room from the English source; `zh-TW`, `zh-HK`, `zh-MO` and `zh-Hant` read it while an explicit Hans stays Simplified; the desktop UI prefers an installed Noto Sans CJK TC, then Microsoft JhengHei, PingFang TC and Heiti TC; no font is bundled; native review pending. Numbers unchanged | | 2026-11-06 | patch | M1, #2028: Edit ▸ Set default properties gives new rectangles, ellipses, polygons and stars drawn with the tools (and, set from a text layer, new text) the selected layer's properties; it only copied them for Paste properties. Tests: `default_props_tests` (3). Numbers unchanged | | 2026-11-06 | patch | M5, #2027: `proto.settings` refuses an unknown device name with the list of devices (it saved any string, and the presenter showed no device), takes known names in any case, and answers the page's prototype settings, so `proto.settings {}` reads them back over MCP. Test: `proto_present_tests::unknown_devices_are_refused_and_settings_read_back`. Numbers unchanged | diff --git a/crates/text/Cargo.toml b/crates/text/Cargo.toml index aea3d3be5..23b658e9d 100644 --- a/crates/text/Cargo.toml +++ b/crates/text/Cargo.toml @@ -8,6 +8,12 @@ description = "FigCraft text: font database (embedded craft-fonts + system fonts [lints] workspace = true +[build-dependencies] +flate2 = { workspace = true } + +[dev-dependencies] +flate2 = { workspace = true } + [dependencies] figcraft-geom = { workspace = true } figcraft-doc = { workspace = true } @@ -34,6 +40,7 @@ craft-font-catalog = { workspace = true } # wasm) instead of its dictionaries (~1.8 MB, which would take the .wasm past the 24 MiB gate in # packaging/web/package.sh). [target.'cfg(target_arch = "wasm32")'.dependencies] +flate2 = { workspace = true } icu_segmenter = { workspace = true, features = ["lstm"] } [target.'cfg(target_os = "macos")'.dependencies] diff --git a/crates/text/build.rs b/crates/text/build.rs index 796079968..1a30ee4da 100644 --- a/crates/text/build.rs +++ b/crates/text/build.rs @@ -1,11 +1,13 @@ //! Embeds the optional craft-fonts build input (`CRAFT_FONTS_DIR`): both `fonts/manifest.txt` and the //! opt-in Latin presentation fonts in `fonts/latin-manifest.txt` (Inter, Roboto, …). Unset or missing, //! `CRAFT_FONTS` is empty and native apps use system fonts. Required web releases enforce Inter -//! Regular/SemiBold and Noto Sans Georgian Regular after filtering. Recipe: craft-fonts `docs/integration.md`. +//! Regular/SemiBold, Noto Sans Georgian Regular and Noto Sans Arabic Regular after filtering. +//! Recipe: craft-fonts `docs/integration.md`. //! Noto Sans CJK only with `FIGCRAFT_EMBED_CJK` set (the web build's only way to show CJK UI text). -//! The other scripts' faces (Noto Sans Arabic, Hebrew, Thai, Devanagari…) stay out of web builds unless -//! `FIGCRAFT_EMBED_SCRIPTS` names their script tags (`Arab,Hebr,Thai,Deva`: one tag per script wanted, -//! as the manifest's scripts column lists them; #1902). +//! Noto Sans Arabic is included by default so Persian and Arabic remain readable without local fonts +//! (#1902). Other scripts' faces (Hebrew, Thai, Devanagari…) stay out of web builds unless +//! `FIGCRAFT_EMBED_SCRIPTS` names their script tags (`Hebr,Thai,Deva`: one tag per script wanted, +//! as the manifest's scripts column lists them). //! `FIGCRAFT_WEB_FONTS` (comma-separated families) adds manifest families to the web build. use std::fmt::Write as _; use std::path::PathBuf; @@ -20,24 +22,45 @@ fn main() { println!("cargo::rerun-if-env-changed=FIGCRAFT_EMBED_CJK"); println!("cargo::rerun-if-env-changed=FIGCRAFT_EMBED_SCRIPTS"); println!("cargo::rerun-if-env-changed=FIGCRAFT_WEB_FONTS"); - let mut src = String::from("pub static CRAFT_FONTS: &[CraftFont] = &[\n"); let dir = std::env::var_os("CRAFT_FONTS_DIR").map(PathBuf::from); let required = std::env::var_os("CRAFT_FONTS_REQUIRED").is_some(); let wasm = std::env::var("CARGO_CFG_TARGET_ARCH").is_ok_and(|a| a == "wasm32"); let embed_cjk = std::env::var_os("FIGCRAFT_EMBED_CJK").is_some(); let embed_scripts = scripts_from(std::env::var("FIGCRAFT_EMBED_SCRIPTS").ok().as_deref()); let web_fonts = web_fonts(&std::env::var("FIGCRAFT_WEB_FONTS").unwrap_or_default()); - match input_fonts(dir.as_deref(), wasm, required, embed_cjk, &embed_scripts, &web_fonts) { - Ok(entries) => src.push_str(&entries), - Err(e) => println!("cargo::error={e}"), - } - src.push_str("];\n"); - let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap_or_default()).join("craft_fonts.rs"); - if let Err(e) = std::fs::write(&out, src) { + let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap_or_default()); + let entries = match input_fonts(dir.as_deref(), wasm, required, embed_cjk, &embed_scripts, &web_fonts, wasm.then_some(out_dir.as_path())) { + Ok(entries) => entries, + Err(e) => { + println!("cargo::error={e}"); + String::new() + } + }; + let out = out_dir.join("craft_fonts.rs"); + if let Err(e) = std::fs::write(&out, catalog_source(&entries, wasm)) { println!("cargo::error=writing {}: {e}", out.display()); } } +/// Wrap generated face entries in the native static slice or lazily decoded web catalog. +fn catalog_source(entries: &str, wasm: bool) -> String { + // Compress the complete web catalog so adding the Arabic fallback stays within the WASM + // size budget. Each face is restored once, byte-for-byte, before the existing font consumers. + let mut src = if wasm { + String::from( + "pub static CRAFT_FONTS: std::sync::LazyLock> = std::sync::LazyLock::new(|| {\n static PACKED: &[(&str, &str, &crate::embedded::PackedFont)] = &[\n", + ) + } else { + String::from("pub static CRAFT_FONTS: &[CraftFont] = &[\n") + }; + src.push_str(entries); + src.push_str("];\n"); + if wasm { + src.push_str(" PACKED.iter().filter_map(|(family, style, font)| Some(CraftFont { family, style, bytes: font.data(family, style)? })).collect()\n});\n"); + } + src +} + /// The script tags `FIGCRAFT_EMBED_SCRIPTS` names (comma- or space-separated), trimmed; none when /// it is unset. fn scripts_from(var: Option<&str>) -> Vec { @@ -64,6 +87,7 @@ fn input_fonts( embed_cjk: bool, embed_scripts: &[String], web_fonts: &[String], + packed_dir: Option<&std::path::Path>, ) -> Result { let Some(dir) = dir else { for family in web_fonts { @@ -80,7 +104,7 @@ fn input_fonts( watch(&path); continue; } - match craft_fonts(dir, manifest, wasm, embed_cjk, embed_scripts, web_fonts) { + match craft_fonts(dir, manifest, wasm, embed_cjk, embed_scripts, web_fonts, packed_dir) { Ok(catalog) => { loaded = true; out.push_str(&catalog.entries); @@ -96,7 +120,7 @@ fn input_fonts( println!("cargo::warning=FIGCRAFT_WEB_FONTS: {family:?} isn't embedded: {why}"); } if required && wasm { - for (family, style) in [("Inter", "Regular"), ("Inter", "SemiBold"), ("Noto Sans Georgian", "Regular")] { + for (family, style) in [("Inter", "Regular"), ("Inter", "SemiBold"), ("Noto Sans Georgian", "Regular"), ("Noto Sans Arabic", "Regular")] { if !faces.iter().any(|(f, s)| f == family && s == style) { return Err(format!("required web font missing: {family} {style}; CRAFT_FONTS_DIR must include both Latin and core release faces")); } @@ -135,6 +159,7 @@ fn craft_fonts( embed_cjk: bool, embed_scripts: &[String], web_fonts: &[String], + packed_dir: Option<&std::path::Path>, ) -> Result { let manifest = dir.join(manifest_rel); watch(&manifest); @@ -155,25 +180,71 @@ fn craft_fonts( if cjk && !embed_cjk { continue; } - // Web builds have a size budget: Inter, Noto Sans Georgian (small, for the Georgian UI), only the - // SemiBold wordmark, optionally Noto Sans CJK (every CJK script, for CJK UI text), the faces - // of the scripts `FIGCRAFT_EMBED_SCRIPTS` names (an Arabic or Persian document needs Noto Sans - // Arabic, +0.8 MB; the browser has no system fonts to fall back on, #1902), and the + // Web builds have a size budget: Inter, Noto Sans Georgian (small, for the Georgian UI), + // Noto Sans Arabic (Persian/Arabic would otherwise show boxes without local fonts, #1902), + // only the SemiBold wordmark, optionally Noto Sans CJK (every CJK script, for CJK + // UI text), the faces of the scripts `FIGCRAFT_EMBED_SCRIPTS` names, and the // families a distribution names in `FIGCRAFT_WEB_FONTS` (a Thai face for Thai text, say). let wordmark = *family == "Archivo" && *style == "SemiBold"; let asked = scripts.split(',').map(str::trim).any(|s| embed_scripts.iter().any(|e| e.eq_ignore_ascii_case(s))); let added = web_fonts.iter().any(|f| f == family); - if wasm && !matches!(*family, "Inter" | "Noto Sans Georgian") && !wordmark && !cjk && !asked && !added { + if wasm && !matches!(*family, "Inter" | "Noto Sans Georgian" | "Noto Sans Arabic") && !wordmark && !cjk && !asked && !added { continue; } let path = dir.join(file).canonicalize().map_err(|e| format!("{file}: {e}"))?; println!("cargo::rerun-if-changed={}", path.display()); faces.push(((*family).to_owned(), (*style).to_owned())); - let _ = writeln!(out, " CraftFont {{ family: {family:?}, style: {style:?}, bytes: include_bytes!({:?}) }},", path.display().to_string()); + if let Some(packed_dir) = packed_dir.filter(|_| wasm) { + let (packed, len) = pack_font(&path, packed_dir)?; + let _ = writeln!( + out, + " ({family:?}, {style:?}, {{ static FONT: crate::embedded::PackedFont = crate::embedded::PackedFont::new(include_bytes!({:?}), {len}); &FONT }}),", + packed.display().to_string() + ); + } else { + let _ = + writeln!(out, " CraftFont {{ family: {family:?}, style: {style:?}, bytes: include_bytes!({:?}) }},", path.display().to_string()); + } } Ok(Catalog { entries: out, faces }) } +/// Write a lossless zlib copy under OUT_DIR, never into the font checkout. A path hash avoids +/// collisions between manifest entries without using their relative paths as output paths. +fn pack_font(path: &std::path::Path, out_dir: &std::path::Path) -> Result<(PathBuf, usize), String> { + use std::hash::{Hash, Hasher}; + use std::io::{Read as _, Write as _}; + // Keep this in step with embedded::unpack: a required face must not build successfully + // only to be discarded by the runtime decoder. Limit reads too, if the input changes. + const MAX_FONT_BYTES: u64 = 256 * 1024 * 1024; + let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; + let len = file.metadata().map_err(|e| format!("{}: {e}", path.display()))?.len(); + if len == 0 || len > MAX_FONT_BYTES { + return Err(format!("{}: embedded font size must be 1..={MAX_FONT_BYTES} bytes", path.display())); + } + let mut bytes = Vec::new(); + file.take(MAX_FONT_BYTES + 1).read_to_end(&mut bytes).map_err(|e| format!("{}: {e}", path.display()))?; + if bytes.is_empty() || bytes.len() as u64 > MAX_FONT_BYTES { + return Err(format!("{}: embedded font size changed outside the supported range", path.display())); + } + let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); + encoder.write_all(&bytes).map_err(|e| format!("compressing {}: {e}", path.display()))?; + let packed = encoder.finish().map_err(|e| format!("compressing {}: {e}", path.display()))?; + let mut hash = std::collections::hash_map::DefaultHasher::new(); + path.hash(&mut hash); + let output = out_dir.join(format!("font-{:016x}.zlib", hash.finish())); + std::fs::write(&output, packed).map_err(|e| format!("{}: {e}", output.display()))?; + Ok((output, bytes.len())) +} + +#[cfg(test)] +#[path = "tests/support/generated_catalog.rs"] +mod generated_catalog; + +#[cfg(test)] +#[path = "tests/support/web_fallback.rs"] +mod web_fallback; + #[cfg(test)] mod tests { use super::*; @@ -181,18 +252,18 @@ mod tests { static NEXT: AtomicU64 = AtomicU64::new(0); - struct Fixture(PathBuf); + pub(super) struct Fixture(pub(super) PathBuf); impl Fixture { /// Test fixture: create a unique temporary directory, write a synthetic face file and - /// install the default Georgian manifest. - fn new() -> Self { + /// install the default Georgian and Arabic manifest. + pub(super) fn new() -> Self { let dir = std::env::temp_dir().join(format!("fig-font-input-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))); std::fs::create_dir_all(dir.join("fonts")).unwrap(); // The build script validates manifest/file presence; font bytes are checked centrally. std::fs::write(dir.join("fonts/face.ttf"), b"synthetic build-input fixture").unwrap(); let fixture = Self(dir); - fixture.manifest("manifest.txt", &[("Noto Sans Georgian", "Regular")]); + fixture.manifest("manifest.txt", &[("Noto Sans Georgian", "Regular"), ("Noto Sans Arabic", "Regular")]); fixture } @@ -210,6 +281,32 @@ mod tests { } } + /// Fail at build time for sizes the runtime decoder cannot accept. + #[test] + fn web_font_compression_rejects_empty_and_oversized_inputs() { + let f = Fixture::new(); + let path = f.0.join("invalid.ttf"); + let file = std::fs::File::create(&path).unwrap(); + assert!(pack_font(&path, &f.0).unwrap_err().contains("font size")); + file.set_len(256 * 1024 * 1024 + 1).unwrap(); + assert!(pack_font(&path, &f.0).unwrap_err().contains("font size")); + } + + /// Web catalog entries embed lossless packed files; native entries still reference raw fonts. + #[test] + fn web_catalog_packs_fonts_and_native_keeps_original_bytes() { + let f = Fixture::new(); + let web = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &[], &[], Some(&f.0)).unwrap(); + assert_eq!(web.entries.matches("PackedFont::new").count(), 2); + let path = f.0.join("fonts/face.ttf").canonicalize().unwrap(); + let (packed, len) = pack_font(&path, &f.0).unwrap(); + assert!(web.entries.contains(&packed.display().to_string())); + assert_eq!(len, std::fs::read(&path).unwrap().len()); + let native = craft_fonts(&f.0, "fonts/manifest.txt", false, false, &[], &[], Some(&f.0)).unwrap(); + assert!(native.entries.contains(&path.display().to_string())); + assert!(!native.entries.contains("PackedFont")); + } + /// Asserts the native Latin catalog keeps its 43 permitted presentation faces, the web /// catalog keeps only the four Inter faces, Adobe-authored families are absent, and /// permitted native faces remain. @@ -217,9 +314,9 @@ mod tests { fn restored_latin_catalog_preserves_permitted_native_faces_and_web_budget() { let f = Fixture::new(); std::fs::write(f.0.join("fonts/latin-manifest.txt"), include_str!("tests/fixtures/latin-families.txt")).unwrap(); - let native = craft_fonts(&f.0, "fonts/latin-manifest.txt", false, false, &[], &[]).unwrap(); + let native = craft_fonts(&f.0, "fonts/latin-manifest.txt", false, false, &[], &[], None).unwrap(); assert_eq!(native.faces.len(), 43, "all prior permitted presentation faces remain available"); - let web = craft_fonts(&f.0, "fonts/latin-manifest.txt", true, false, &[], &[]).unwrap(); + let web = craft_fonts(&f.0, "fonts/latin-manifest.txt", true, false, &[], &[], None).unwrap(); assert_eq!(web.faces.len(), 4, "the web catalog keeps only the four Inter faces"); for catalog in [&native, &web] { assert!(!catalog.entries.contains("Source Sans 3")); @@ -227,30 +324,31 @@ mod tests { } assert!(native.faces.iter().any(|(family, _)| family == "Carlito")); assert!(native.faces.iter().any(|(family, _)| family == "Liberation Serif")); - assert!(input_fonts(Some(&f.0), true, true, false, &[], &[]).is_ok()); + assert!(input_fonts(Some(&f.0), true, true, false, &[], &[], None).is_ok()); } /// Asserts required web input rejects a catalog missing Inter Regular or Inter SemiBold. #[test] fn required_web_rejects_core_only_and_partial_latin_catalogs() { let f = Fixture::new(); - assert!(input_fonts(Some(&f.0), true, true, false, &[], &[]).unwrap_err().contains("Inter Regular")); + assert!(input_fonts(Some(&f.0), true, true, false, &[], &[], None).unwrap_err().contains("Inter Regular")); f.manifest("latin-manifest.txt", &[("Inter", "Regular"), ("Unrelated", "SemiBold")]); - assert!(input_fonts(Some(&f.0), true, true, false, &[], &[]).unwrap_err().contains("Inter SemiBold")); + assert!(input_fonts(Some(&f.0), true, true, false, &[], &[], None).unwrap_err().contains("Inter SemiBold")); } - /// Asserts required web input embeds the Inter and Georgian catalogs (three entries) and + /// Asserts required web input embeds the Inter, Georgian and Arabic catalogs (four entries) and /// rejects a manifest without Noto Sans Georgian Regular. #[test] fn required_web_embeds_both_catalogs_and_requires_georgian() { let f = Fixture::new(); f.manifest("latin-manifest.txt", &[("Inter", "Regular"), ("Inter", "SemiBold")]); - let entries = input_fonts(Some(&f.0), true, true, false, &[], &[]).unwrap(); - assert_eq!(entries.matches("CraftFont {").count(), 3); + let entries = input_fonts(Some(&f.0), true, true, false, &[], &[], None).unwrap(); + assert_eq!(entries.matches("CraftFont {").count(), 4); assert!(entries.contains("family: \"Inter\"")); assert!(entries.contains("family: \"Noto Sans Georgian\"")); + assert!(entries.contains("family: \"Noto Sans Arabic\"")); f.manifest("manifest.txt", &[("Other", "Regular")]); - assert!(input_fonts(Some(&f.0), true, true, false, &[], &[]).unwrap_err().contains("Noto Sans Georgian Regular")); + assert!(input_fonts(Some(&f.0), true, true, false, &[], &[], None).unwrap_err().contains("Noto Sans Georgian Regular")); } /// Asserts absent font input is empty when optional and an error when required, while present @@ -258,10 +356,10 @@ mod tests { #[test] fn optional_and_native_inputs_keep_their_existing_policy() { let f = Fixture::new(); - assert!(input_fonts(None, true, false, false, &[], &[]).unwrap().is_empty()); - assert!(input_fonts(None, true, true, false, &[], &[]).unwrap_err().contains("CRAFT_FONTS_DIR is missing")); - assert!(input_fonts(Some(&f.0), true, false, false, &[], &[]).unwrap().contains("Noto Sans Georgian")); - assert!(input_fonts(Some(&f.0), false, true, false, &[], &[]).unwrap().contains("Noto Sans Georgian")); + assert!(input_fonts(None, true, false, false, &[], &[], None).unwrap().is_empty()); + assert!(input_fonts(None, true, true, false, &[], &[], None).unwrap_err().contains("CRAFT_FONTS_DIR is missing")); + assert!(input_fonts(Some(&f.0), true, false, false, &[], &[], None).unwrap().contains("Noto Sans Georgian")); + assert!(input_fonts(Some(&f.0), false, true, false, &[], &[], None).unwrap().contains("Noto Sans Georgian")); } /// Asserts required web input rejects a manifest whose face file is missing, even when every @@ -271,12 +369,38 @@ mod tests { let f = Fixture::new(); f.manifest("latin-manifest.txt", &[("Inter", "Regular"), ("Inter", "SemiBold")]); std::fs::remove_file(f.0.join("fonts/face.ttf")).unwrap(); - assert!(input_fonts(Some(&f.0), true, true, false, &[], &[]).unwrap_err().contains("fonts/face.ttf")); + assert!(input_fonts(Some(&f.0), true, true, false, &[], &[], None).unwrap_err().contains("fonts/face.ttf")); } - /// #1902: a web build leaves the other scripts' faces out (an Arabic or Persian document shows - /// missing-glyph boxes) unless `FIGCRAFT_EMBED_SCRIPTS` names their script tags; native builds - /// keep every face, as before. + /// #1902: the default web catalog needs an Arabic fallback without local-font permission + /// or an extra distribution setting; otherwise Persian and Arabic text becomes boxes. + #[test] + fn default_web_catalog_keeps_arabic_without_opt_in() { + let f = Fixture::new(); + f.manifest("manifest.txt", &[("Noto Sans Georgian", "Regular"), ("Noto Sans Arabic", "Regular"), ("Noto Sans Hebrew", "Regular")]); + let entries = input_fonts(Some(&f.0), true, false, false, &[], &[], None).unwrap(); + assert!(entries.contains("family: \"Noto Sans Arabic\""), "default web text must have an Arabic/Persian fallback"); + assert!(!entries.contains("Noto Sans Hebrew"), "other scripts remain opt-in"); + } + + /// A required web release must not silently publish without its Arabic fallback. + #[test] + fn required_web_rejects_missing_arabic_regular() { + let f = Fixture::new(); + f.manifest("latin-manifest.txt", &[("Inter", "Regular"), ("Inter", "SemiBold")]); + for arabic in [None, Some("Bold")] { + let mut faces = vec![("Noto Sans Georgian", "Regular")]; + if let Some(style) = arabic { + faces.push(("Noto Sans Arabic", style)); + } + f.manifest("manifest.txt", &faces); + let result = input_fonts(Some(&f.0), true, true, false, &[], &[], None); + assert!(result.is_err_and(|e| e.contains("Noto Sans Arabic Regular")), "missing Arabic Regular must fail the release build"); + } + } + + /// Arabic is a default fallback; other scripts stay opt-in through `FIGCRAFT_EMBED_SCRIPTS`. + /// Native builds keep every face, as before. #[test] fn web_embeds_other_scripts_faces_only_when_asked() { let f = Fixture::new(); @@ -286,18 +410,18 @@ mod tests { ) .unwrap(); let families = |c: &Catalog| c.faces.iter().map(|(f, _)| f.clone()).collect::>(); - let web = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &[], &[]).unwrap(); - assert_eq!(families(&web), ["Noto Sans Georgian"], "the web budget: Georgian only"); - let arabic = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &["Arab".to_string()], &[]).unwrap(); + let web = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &[], &[], None).unwrap(); + assert_eq!(families(&web), ["Noto Sans Georgian", "Noto Sans Arabic"], "the default web script fallbacks"); + let arabic = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &["Arab".to_string()], &[], None).unwrap(); assert_eq!(families(&arabic), ["Noto Sans Georgian", "Noto Sans Arabic"]); // Several tags, any case, spaces, and a tag among a face's several scripts. - let more = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &scripts_from(Some(" hebr, LATN ")), &[]).unwrap(); - assert_eq!(families(&more), ["Noto Sans Georgian", "Noto Sans Hebrew", "Noto Sans Symbols"]); + let more = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &scripts_from(Some(" hebr, LATN ")), &[], None).unwrap(); + assert_eq!(families(&more), ["Noto Sans Georgian", "Noto Sans Arabic", "Noto Sans Hebrew", "Noto Sans Symbols"]); // Native builds keep every face whatever the variable says. - assert_eq!(craft_fonts(&f.0, "fonts/manifest.txt", false, false, &[], &[]).unwrap().faces.len(), 4); + assert_eq!(craft_fonts(&f.0, "fonts/manifest.txt", false, false, &[], &[], None).unwrap().faces.len(), 4); // The variable reaches the catalog through `input_fonts`. - assert!(input_fonts(Some(&f.0), true, false, false, &["Arab".into()], &[]).unwrap().contains("Noto Sans Arabic")); - assert!(!input_fonts(Some(&f.0), true, false, false, &[], &[]).unwrap().contains("Noto Sans Arabic")); + assert!(input_fonts(Some(&f.0), true, false, false, &["Arab".into()], &[], None).unwrap().contains("Noto Sans Arabic")); + assert!(input_fonts(Some(&f.0), true, false, false, &[], &[], None).unwrap().contains("Noto Sans Arabic")); assert!(scripts_from(None).is_empty()); } @@ -311,11 +435,15 @@ mod tests { let named = web_fonts(" Noto Sans Thai, ,Noto Sans Thia "); assert_eq!(named, ["Noto Sans Thai", "Noto Sans Thia"]); let families = |c: &Catalog| c.faces.iter().map(|(family, _)| family.clone()).collect::>(); - let web = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &[], &named).unwrap(); + let web = craft_fonts(&f.0, "fonts/manifest.txt", true, false, &[], &named, None).unwrap(); assert_eq!(families(&web), ["Noto Sans Georgian", "Noto Sans Thai"], "the named family joins; the others stay out"); - assert_eq!(families(&craft_fonts(&f.0, "fonts/manifest.txt", true, false, &[], &[]).unwrap()), ["Noto Sans Georgian"], "unset: as before"); - assert_eq!(craft_fonts(&f.0, "fonts/manifest.txt", false, false, &[], &[]).unwrap().faces.len(), 3, "native builds embed every family"); + assert_eq!( + families(&craft_fonts(&f.0, "fonts/manifest.txt", true, false, &[], &[], None).unwrap()), + ["Noto Sans Georgian"], + "unset: as before" + ); + assert_eq!(craft_fonts(&f.0, "fonts/manifest.txt", false, false, &[], &[], None).unwrap().faces.len(), 3, "native builds embed every family"); assert_eq!(not_embedded(&named, &web.faces), ["Noto Sans Thia"], "a misspelt family is reported"); - assert!(input_fonts(Some(&f.0), true, false, false, &[], &named).unwrap().contains("family: \"Noto Sans Thai\"")); + assert!(input_fonts(Some(&f.0), true, false, false, &[], &named, None).unwrap().contains("family: \"Noto Sans Thai\"")); } } diff --git a/crates/text/src/embedded.rs b/crates/text/src/embedded.rs new file mode 100644 index 000000000..fd8e2d7b5 --- /dev/null +++ b/crates/text/src/embedded.rs @@ -0,0 +1,133 @@ +//! Lossless decoding of web fonts, shared by the document renderer and UI through static bytes. + +use std::sync::OnceLock; + +/// Matches the font-file limit: corrupted metadata must not request an unbounded allocation. +const MAX_UNPACKED_FONT_BYTES: usize = 256 * 1024 * 1024; + +/// One embedded zlib stream, decoded at most once; invalid streams stay unavailable. +pub(crate) struct PackedFont { + bytes: &'static [u8], + unpacked_len: usize, + decoded: OnceLock>>, +} + +impl PackedFont { + /// Describe an embedded stream without decoding or allocating until it is first used. + pub(crate) const fn new(bytes: &'static [u8], unpacked_len: usize) -> Self { + Self { bytes, unpacked_len, decoded: OnceLock::new() } + } + + /// Restore static font bytes once, or report the unavailable face once and cache the failure. + pub(crate) fn data(&'static self, family: &str, style: &str) -> Option<&'static [u8]> { + self.decoded + .get_or_init(|| { + let bytes = unpack(self.bytes, self.unpacked_len); + if bytes.is_none() { + log::warn!("embedded font {family} {style} could not be decoded"); + } + bytes + }) + .as_deref() + } +} + +/// Decode exactly the declared size, refusing malformed, incomplete or trailing data. +pub(crate) fn unpack(bytes: &[u8], unpacked_len: usize) -> Option> { + if unpacked_len == 0 || unpacked_len > MAX_UNPACKED_FONT_BYTES { + return None; + } + // A fixed output buffer prevents even a valid compressed stream expanding past its budget. + let mut decoded = Vec::new(); + decoded.try_reserve_exact(unpacked_len).ok()?; + decoded.resize(unpacked_len, 0); + let mut decoder = flate2::Decompress::new(true); + let status = decoder.decompress(bytes, &mut decoded, flate2::FlushDecompress::Finish).ok()?; + (status == flate2::Status::StreamEnd && decoder.total_out() == unpacked_len as u64 && decoder.total_in() == bytes.len() as u64).then_some(decoded) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write as _; + + // A zlib stream of the synthetic bytes "font"; no font asset is needed by these tests. + const PACKED: &[u8] = &[120, 218, 75, 203, 207, 43, 1, 0, 4, 57, 1, 184]; + + /// Binary font data survives compression without losing any byte values. + #[test] + fn compressed_bytes_round_trip_exactly() { + let original = b"synthetic font bytes\0\xff\x80\0synthetic font bytes"; + let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); + encoder.write_all(original).unwrap(); + let packed = encoder.finish().unwrap(); + assert_eq!(unpack(&packed, original.len()).unwrap(), original); + } + + /// Invalid checksums, unfinished streams and appended data never become usable font bytes. + #[test] + fn corrupt_truncated_and_trailing_streams_are_refused() { + assert!(unpack(b"not a zlib stream", 4).is_none()); + for end in 0..PACKED.len() { + assert!(unpack(&PACKED[..end], 4).is_none(), "truncated at byte {end}"); + } + let mut corrupt = PACKED.to_vec(); + corrupt[11] ^= 1; + assert!(unpack(&corrupt, 4).is_none(), "checksum mismatch"); + let mut trailing = PACKED.to_vec(); + trailing.push(0); + assert!(unpack(&trailing, 4).is_none(), "trailing bytes"); + } + + /// The declared size is both an exact output length and a bounded allocation request. + #[test] + fn declared_size_is_exact_and_bounded() { + for len in [0, 3, 5, MAX_UNPACKED_FONT_BYTES + 1, usize::MAX] { + assert!(unpack(PACKED, len).is_none(), "invalid unpacked size {len}"); + } + assert_eq!(unpack(PACKED, 4).unwrap(), b"font"); + } + + /// Repeated requests reuse the same decoded allocation, including a cached decode failure. + #[test] + fn decoded_bytes_and_failures_are_cached() { + static FONT: PackedFont = PackedFont::new(PACKED, 4); + static BAD: PackedFont = PackedFont::new(PACKED, 3); + let first = FONT.data("Synthetic", "Regular").unwrap(); + assert_eq!(first, b"font"); + assert!(std::ptr::eq(first, FONT.data("Synthetic", "Regular").unwrap())); + assert!(BAD.data("Synthetic", "Invalid").is_none()); + assert!(BAD.decoded.get().is_some_and(Option::is_none)); + } + + /// An omitted face names its family and style in a single warning, even after repeated use. + #[test] + fn unavailable_font_is_reported_once() { + struct Warnings(std::sync::Mutex>); + impl log::Log for Warnings { + /// Capture only warnings emitted by the decoder. + fn enabled(&self, metadata: &log::Metadata<'_>) -> bool { + metadata.level() == log::Level::Warn + } + + /// Retain messages so the test can count warnings for its own face. + fn log(&self, record: &log::Record<'_>) { + if self.enabled(record.metadata()) { + self.0.lock().unwrap().push(record.args().to_string()); + } + } + + /// Messages are already stored synchronously in memory. + fn flush(&self) {} + } + static WARNINGS: Warnings = Warnings(std::sync::Mutex::new(Vec::new())); + static BAD: PackedFont = PackedFont::new(b"broken stream", 4); + log::set_logger(&WARNINGS).unwrap(); + log::set_max_level(log::LevelFilter::Warn); + assert!(BAD.data("Unavailable Test Face", "SemiBold").is_none()); + assert!(BAD.data("Unavailable Test Face", "SemiBold").is_none()); + let messages = WARNINGS.0.lock().unwrap(); + let expected = "embedded font Unavailable Test Face SemiBold could not be decoded"; + assert_eq!(messages.iter().filter(|message| message.as_str() == expected).count(), 1); + } +} diff --git a/crates/text/src/fontdb.rs b/crates/text/src/fontdb.rs index f5774307e..fa29e2130 100644 --- a/crates/text/src/fontdb.rs +++ b/crates/text/src/fontdb.rs @@ -700,10 +700,11 @@ pub fn db() -> &'static RwLock { raw_db() } +/// Initialize and return the shared database, loading valid embedded faces exactly once. fn raw_db() -> &'static RwLock { DB.get_or_init(|| { let mut db = FontDb::default(); - for f in CRAFT_FONTS { + for f in CRAFT_FONTS.iter() { if let Some(face) = Face::from_bytes(FontBytes::Static(f.bytes), 0, Some(f.family), Some(f.style)) { db.add(face); } diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index b6d87d669..37f603464 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -9,6 +9,8 @@ mod breaks; pub mod color; pub mod display; +#[cfg(target_arch = "wasm32")] +mod embedded; pub mod fontdb; mod layout; mod onpath; diff --git a/crates/text/tests/support/generated_catalog.rs b/crates/text/tests/support/generated_catalog.rs new file mode 100644 index 000000000..77b108fe4 --- /dev/null +++ b/crates/text/tests/support/generated_catalog.rs @@ -0,0 +1,90 @@ +//! Compile and execute the generated web catalog independently of Cargo and optional font assets. + +use super::{catalog_source, craft_fonts, pack_font, tests::Fixture}; +use std::fmt::Write as _; + +/// Generated Rust must preserve each face's inputs, skip rejected faces, and initialize only once. +#[test] +fn generated_web_catalog_compiles_and_initializes() { + let fixture = Fixture::new(); + let mut manifest = String::new(); + let mut expected = String::from("static EXPECTED: &[(&str, &str, &[u8], usize, Option<&[u8]>)] = &[\n"); + for (index, (family, style, bytes, accepted)) in [ + ("Inter", "SemiBold", b"first synthetic font".as_slice(), true), + ("Noto Sans Arabic", "Regular", b"second synthetic font with different bytes".as_slice(), true), + ("Noto Sans Georgian", "Regular", b"rejected synthetic font".as_slice(), false), + ] + .into_iter() + .enumerate() + { + let relative = format!("fonts/face-{index}.ttf"); + std::fs::write(fixture.0.join(&relative), bytes).unwrap(); + let source = fixture.0.join(&relative).canonicalize().unwrap(); + let (packed, _) = pack_font(&source, &fixture.0).unwrap(); + writeln!(manifest, "{family} | {style} | {relative} | Latn").unwrap(); + let decoded = if accepted { format!("Some(include_bytes!({:?}).as_slice())", source) } else { "None".into() }; + writeln!(expected, "({family:?}, {style:?}, include_bytes!({packed:?}), {}, {decoded}),", bytes.len()).unwrap(); + } + expected.push_str("];\n"); + std::fs::write(fixture.0.join("fonts/manifest.txt"), manifest).unwrap(); + let entries = craft_fonts(&fixture.0, "fonts/manifest.txt", true, false, &[], &[], Some(&fixture.0)).unwrap().entries; + for populated in [true, false] { + let catalog = catalog_source(if populated { &entries } else { "" }, true); + compile_and_run(&fixture.0, &format!("const POPULATED: bool = {populated};\n{expected}{catalog}{HARNESS}")); + } +} + +/// Compile a std-only contract harness, then execute its assertions without discovering rlibs. +fn compile_and_run(dir: &std::path::Path, source: &str) { + let input = dir.join("generated_catalog.rs"); + let executable = dir.join(format!("generated_catalog{}", std::env::consts::EXE_SUFFIX)); + std::fs::write(&input, source).unwrap(); + let compiler = std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()); + let built = std::process::Command::new(compiler).arg("--edition=2024").arg(&input).arg("-o").arg(&executable).output().unwrap(); + assert!(built.status.success(), "generated catalog did not compile:\n{}", String::from_utf8_lossy(&built.stderr)); + let ran = std::process::Command::new(&executable).output().unwrap(); + assert!(ran.status.success(), "generated catalog failed:\n{}", String::from_utf8_lossy(&ran.stderr)); +} + +// This stub tests code-generation wiring; the real decoder has separate byte-for-byte tests. +// OnceLock deliberately preserves the real type's interior mutability, catching invalid static +// promotions (E0492) that a plain byte-slice stub would allow. +const HARNESS: &str = r#" +pub struct CraftFont { pub family: &'static str, pub style: &'static str, pub bytes: &'static [u8] } +static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +mod embedded { + pub struct PackedFont { + bytes: &'static [u8], len: usize, + decoded: std::sync::OnceLock>, + } + impl PackedFont { + /// Mirror the production constructor's const/static contract. + pub const fn new(bytes: &'static [u8], len: usize) -> Self { + Self { bytes, len, decoded: std::sync::OnceLock::new() } + } + /// Validate all generated inputs and return a distinct marker or a rejected face. + pub fn data(&'static self, family: &str, style: &str) -> Option<&'static [u8]> { + crate::CALLS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + *self.decoded.get_or_init(|| { + let expected = crate::EXPECTED.iter().find(|row| row.0 == family && row.1 == style).unwrap(); + assert_eq!(self.bytes, expected.2); + assert_eq!(self.len, expected.3); + expected.4 + }) + } + } +} +/// Observe the generated public catalog twice; initialization must happen once, on first access. +fn main() { + use std::sync::atomic::Ordering::SeqCst; + assert_eq!(CALLS.load(SeqCst), 0); + let expected: Vec<_> = EXPECTED.iter().filter(|row| POPULATED && row.4.is_some()).collect(); + for _ in 0..2 { + assert_eq!(CRAFT_FONTS.len(), expected.len()); + for (font, row) in CRAFT_FONTS.iter().zip(&expected) { + assert_eq!((font.family, font.style, font.bytes), (row.0, row.1, row.4.unwrap())); + } + } + assert_eq!(CALLS.load(SeqCst), if POPULATED { EXPECTED.len() } else { 0 }); +} +"#; diff --git a/crates/text/tests/support/web_fallback.rs b/crates/text/tests/support/web_fallback.rs new file mode 100644 index 000000000..f9fac6178 --- /dev/null +++ b/crates/text/tests/support/web_fallback.rs @@ -0,0 +1,84 @@ +//! Exercise shaping with the actual default web catalog, without desktop font fallbacks. + +use super::{craft_fonts, pack_font}; + +#[path = "../../src/embedded.rs"] +mod embedded; +use figcraft_text::fontdb::{Face, FontBytes}; + +/// The default web faces render Persian and Arabic with joined letters and bidi ordering even +/// when the requested font is unavailable and system-font discovery is disabled. +#[test] +fn default_web_fonts_render_persian_and_arabic_without_system_fonts() { + let Some(dir) = std::env::var_os("CRAFT_FONTS_DIR").map(std::path::PathBuf::from).filter(|p| p.join("fonts/manifest.txt").is_file()) else { + eprintln!("craft-fonts is absent; build-policy fixtures still run, real-font shaping skipped"); + return; + }; + // Check source availability independently of the web filter under test. Otherwise removing + // Arabic from that filter would skip this regression instead of reproducing the boxes. + if [("Inter", "Regular"), ("Inter", "SemiBold"), ("Noto Sans Georgian", "Regular"), ("Noto Sans Arabic", "Regular")] + .iter() + .any(|(family, style)| !figcraft_text::CRAFT_FONTS.iter().any(|font| font.family == *family && font.style == *style)) + { + eprintln!("complete release font catalog is absent; build-policy fixtures still run, real-font shaping skipped"); + return; + } + let packed_dir = super::tests::Fixture::new(); + let mut faces = Vec::new(); + for manifest in ["fonts/latin-manifest.txt", "fonts/manifest.txt"] { + if dir.join(manifest).is_file() { + faces.extend(craft_fonts(&dir, manifest, true, false, &[], &[], None).unwrap().faces); + } + } + + // This integration-test process owns its font database. Native embedding includes more + // families than the web, so retain only the web build's faces and disable system discovery. + figcraft_text::opt_out_of_system_fonts(); + let has_all_inter_styles = ["Regular", "Italic", "SemiBold", "Bold"].iter().all(|style| faces.iter().any(|(f, s)| f == "Inter" && s == style)); + let mut prior_raw_bytes = 0; + let mut packed_bytes = 0; + { + let mut db = figcraft_text::db().write().unwrap(); + *db = figcraft_text::FontDb::default(); + for (family, style) in faces { + let font = figcraft_text::CRAFT_FONTS.iter().find(|f| f.family == family && f.style == style).expect("manifest face is embedded"); + // Run the build-time compressor and the exact WASM decoder before shaping. + let source = packed_dir.0.join("face.ttf"); + std::fs::write(&source, font.bytes).unwrap(); + let (packed, len) = pack_font(&source, &packed_dir.0).unwrap(); + let packed = std::fs::read(packed).unwrap(); + packed_bytes += packed.len(); + if family != "Noto Sans Arabic" { + prior_raw_bytes += font.bytes.len(); + } + let decoded = embedded::unpack(&packed, len).unwrap(); + assert_eq!(decoded, font.bytes); + db.add(Face::from_bytes(FontBytes::Shared(std::sync::Arc::new(decoded)), 0, Some(font.family), Some(font.style)).unwrap()); + } + } + + if has_all_inter_styles { + assert!(packed_bytes < prior_raw_bytes, "including Arabic must not grow the embedded web font payload"); + } + eprintln!("web font payload: {packed_bytes} compressed bytes including Arabic; {prior_raw_bytes} raw bytes before Arabic"); + + for family in ["Iran Sans", "Inter"] { + for characters in ["۴۵,۰۰۰ تومان به کیف پول بومرنگ شما واریز شد.", "مرحبا بالعالم ١٢٣"] { + let mut text = figcraft_doc::TextData::new(characters); + text.style.font_family = family.into(); + let layout = figcraft_text::layout(&text, None, None); + assert!(!layout.glyphs.is_empty()); + assert!(layout.glyphs.iter().all(|g| g.gid != 0), "missing glyphs for {family}: {characters}"); + assert!(layout.glyphs.iter().any(|g| g.face.family == "Noto Sans Arabic")); + assert!(layout.lines.first().unwrap().rtl, "Arabic/Persian paragraphs run right to left"); + if characters.starts_with('۴') { + let x = |at| layout.glyphs.iter().find(|g| g.char_start == at).unwrap().x; + assert!(x(0) < x(5), "Persian digits keep their left-to-right order inside the RTL paragraph"); + } + } + let mut joined = figcraft_doc::TextData::new("بب"); + joined.style.font_family = family.into(); + let layout = figcraft_text::layout(&joined, None, None); + assert!(layout.glyphs.iter().all(|g| g.gid != g.face.glyph_for('ب')), "adjacent Arabic letters use joined forms"); + } +} diff --git a/docs/localization-parity.md b/docs/localization-parity.md index e74c0df7f..29e4a9a50 100644 --- a/docs/localization-parity.md +++ b/docs/localization-parity.md @@ -1,6 +1,6 @@ # Localization parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-05 · **Change:** minor (a right-to-left UI language (Arabic) mirrors the window: the icon rail and layers panel on the right, the Design panel on the left, popovers beside it over the canvas, egui's scroll bars, submenus and checkboxes mirrored; `FIGCRAFT_EMBED_SCRIPTS` embeds other scripts' craft-fonts faces in a web build, so Arabic and Persian documents draw there (#1902); documents: a paragraph's base direction can be forced with Text ▸ Text direction, as Add right-to-left text says (#1830); Bosnian draft catalog, one/few/other plurals; German terminology corrections (M10): gradient stops, OpenType stylistic sets and the Home-screen preference; partial review, catalog remains a draft; Arabic draft catalog, count-neutral plurals and web font gate; Thai draft catalog and Thai UI fonts; a missing translation fails no check: `cargo xtask i18n-report` lists them; Vietnamese draft catalog; string counts re-measured: 2,352 entries; Russian and Turkish catalogs reviewed by native speakers, Russian one/few/many plurals; Czech and Indonesian drafts; the Japanese draft completed; counts re-measured; Windows UI language from the first preferred language; draft catalogs catch up: every draft translates every string again; Simplified Chinese draft catalog; macOS finds its downloadable CJK system fonts (#406); string counts re-measured: 2,272 entries, the drafts at 95–99%; Swedish command labels and menu wordings; Russian, Swedish, Turkish, Czech and Indonesian listed; a duplicated paragraph removed) · **Target:** Figma Design (desktop app 126.10.7) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-07 · **Change:** minor (a right-to-left UI language (Arabic) mirrors the window: the icon rail and layers panel on the right, the Design panel on the left, popovers beside it over the canvas, egui's scroll bars, submenus and checkboxes mirrored; web builds with craft-fonts embed Noto Sans Arabic Regular by default, and required web builds reject catalogs without it, so Persian and Arabic documents draw without extra build flags (#1902); other scripts remain opt-in with `FIGCRAFT_EMBED_SCRIPTS`; documents: a paragraph's base direction can be forced with Text ▸ Text direction, as Add right-to-left text says (#1830); Bosnian draft catalog, one/few/other plurals; German terminology corrections (M10): gradient stops, OpenType stylistic sets and the Home-screen preference; partial review, catalog remains a draft; Arabic draft catalog, count-neutral plurals and web font gate; Thai draft catalog and Thai UI fonts; a missing translation fails no check: `cargo xtask i18n-report` lists them; Vietnamese draft catalog; string counts re-measured: 2,352 entries; Russian and Turkish catalogs reviewed by native speakers, Russian one/few/many plurals; Czech and Indonesian drafts; the Japanese draft completed; counts re-measured; Windows UI language from the first preferred language; draft catalogs catch up: every draft translates every string again; Simplified Chinese draft catalog; macOS finds its downloadable CJK system fonts (#406); string counts re-measured: 2,272 entries, the drafts at 95–99%; Swedish command labels and menu wordings; Russian, Swedish, Turkish, Czech and Indonesian listed; a duplicated paragraph removed) · **Target:** Figma Design (desktop app 126.10.7) FigCraft's interface has a localization layer with an English catalog, a Russian catalog reviewed by a native speaker, and Spanish (with a Latin American overlay), French, German, Brazilian @@ -96,11 +96,14 @@ supported by both apps (CJK, Arabic and Devanagari shaping, IME); Figma has no v embeds Noto Sans CJK SC (+16 MB) and offers Japanese, Korean and Simplified Chinese; without a Noto Sans CJK TC face built in it leaves Traditional Chinese out too; otherwise the language picker and System leave them out. No font file is in this repo. - embeds Noto Sans CJK SC (+16 MB) and offers Japanese, Korean and Simplified Chinese; otherwise the language picker and - System leave them out. `FIGCRAFT_EMBED_SCRIPTS=Arab,Hebr,Thai,Deva` (one tag per script wanted, as - the craft-fonts manifest lists them) embeds those scripts' faces as well, so an Arabic or Persian - document draws in Noto Sans Arabic (+0.8 MB) instead of missing-glyph boxes (#1902), and with `Arab` - the picker offers the Arabic UI catalog too, as on the desktop. No font file is in this repo. +- **Arabic and Persian on the web:** builds with craft-fonts embed Noto Sans Arabic Regular by + default, so documents have an Arabic-script fallback and the picker offers the Arabic UI without + extra build flags (#1902). Required web builds reject a catalog without that face. Web fonts are packed + losslessly at build time and restored once at runtime to preserve the WASM size budget; other + scripts remain optional through + `FIGCRAFT_EMBED_SCRIPTS=Hebr,Thai,Deva` (one tag per script wanted, as the manifest lists them). + Noto Sans Arabic is a fallback: matching Iran Sans exactly still needs that installed font via + local font access in a browser that supports it. No font file is added to this repository. ## Languages @@ -111,7 +114,7 @@ supported by both apps (CJK, Arabic and Devanagari shaping, IME); Figma has no v | Traditional Chinese (Taiwan) | zh-Hant | 2,647 / 2,647 (100%: 1,975 UI strings, 641 command labels, 31 menu wordings; draft, counted 2026-11-03) | yes | CJK text and IME in documents; UI text in an installed Noto Sans CJK TC, then Microsoft JhengHei, PingFang TC or Heiti TC (web: only with a built-in TC face); zh-TW, zh-HK, zh-MO and zh-Hant read it, an explicit Hans stays Simplified | no | draft | 1–2 (after native review) | | Spanish | es, es-419 | 2,352 / 2,352 (100%, draft); es-419 overlays 104 Latin American wordings (draft) | yes | Latin | no | draft | 0–1 (after native review) | | Hindi | hi | 0 (0%) | no | Devanagari shaping works in documents; the desktop UI takes an installed Devanagari face (Nirmala UI, Kohinoor Devanagari, Noto Sans Devanagari, Lohit) for names and values (#2014); no UI catalog | no | none | 4–7 | -| Arabic | ar | 1,857 / 1,857 UI strings, 620 command labels, 31 menu wordings (draft, counted 2026-10-28) | catalog complete; native review pending | Existing bidi/shaping and Arabic system fallback; web: only a build with `FIGCRAFT_EMBED_SCRIPTS=Arab`, or with `FIGCRAFT_WEB_FONTS` naming Noto Sans Arabic, embeds it (the font budget leaves it out otherwise, #1902); documents draw in it and the picker offers the Arabic UI then; the window mirrors (panels swap sides), rows inside the panels do not | no | draft | review + mirrored panel rows (not re-estimated) | +| Arabic | ar | 1,857 / 1,857 UI strings, 620 command labels, 31 menu wordings (draft, counted 2026-10-28) | catalog complete; native review pending | Existing bidi/shaping and Arabic system fallback; web builds with craft-fonts embed Noto Sans Arabic by default for documents and the Arabic UI (#1902); the window mirrors (panels swap sides), rows inside the panels do not | no | draft | review + mirrored panel rows (not re-estimated) | | French | fr | 2,328 / 2,352 (99.0%, draft) | yes | Latin | no | draft | 1–2 (after native review) | | Portuguese | pt-BR | 2,331 / 2,352 (99.1%, draft) | yes | Latin | no | draft | 1–2 (after native review) | | Indonesian | id | 2,292 / 2,352 (97.4%, draft) | yes | Latin | no | draft | 1–2 (after native review) | @@ -147,6 +150,7 @@ Hindi and Arabic. | Date | Change | Summary | |---|---|---| | 2026-11-13 | patch | Rows that recent merges had duplicated or garbled are merged back into one row each (no behaviour change): Hindi, Arabic. | +| 2026-11-07 | patch | M10, #1902: Noto Sans Arabic Regular joins the default web font catalog, so Persian and Arabic documents no longer need `FIGCRAFT_EMBED_SCRIPTS=Arab`. Required web builds fail if the face is missing; web fonts are packed losslessly and restored once at runtime to preserve the WASM size budget. Failed decoding reports the face once; generated catalog initialization is covered by a compiler regression. Other script fonts stay optional. Exact Iran Sans rendering requires that installed font through supported browsers' local font access. Existing duplicate Arabic rows and the repeated web-font paragraph are consolidated, preserving the mirrored window and pending panel-row mirroring. Grades and estimates unchanged. | | 2026-11-03 | minor | Traditional Chinese (Taiwan) (`zh-Hant`, 繁體中文(台灣)) draft catalog: every UI string (1,975), command label (641) and menu wording (31), translated clean-room from the English source and marked as a draft awaiting native review. `zh-TW`, `zh-HK`, `zh-MO` and `zh-Hant` read it; an explicit Hans still reads Simplified Chinese. The desktop UI prefers an installed Noto Sans CJK TC, then Microsoft JhengHei, PingFang TC and Heiti TC; no font is bundled. No plural forms. Localization grade and estimates unchanged | | 2026-11-03 | patch | M10, #1902: the web build drew Persian and Arabic text as missing-glyph boxes. The browser has no system fonts, and `crates/text/build.rs` keeps the web catalog to Inter, Noto Sans Georgian, the wordmark and (with `FIGCRAFT_EMBED_CJK`) Noto Sans CJK, so craft-fonts' Noto Sans Arabic never reached it. `FIGCRAFT_EMBED_SCRIPTS=Arab,Hebr,…` (script tags as the manifest's scripts column lists them) now embeds those scripts' faces in a web build; the default build and its size are unchanged, and with `Arab` embedded the picker offers the Arabic UI as well (`cjk::can_show`, as on the desktop). Noto Sans Hebrew, Devanagari and Georgian join Arabic and Thai among the document fallbacks, so an embedded face draws its script in an Inter document. Tests: `web_embeds_other_scripts_faces_only_when_asked` in the build script, `every_embeddable_script_face_is_a_document_fallback`. | | 2026-11-05 | patch | #2014: the panels drew a String variable's Amharic value as boxes: the UI took a system face for Hebrew and Arabic only. `bidi::script_fallbacks` now samples one letter of each of 16 scripts (Ethiopic, Devanagari, Gurmukhi, Gujarati, Tamil, Telugu, Kannada and Malayalam, Sinhala, Lao, Myanmar, Khmer, Armenian and Georgian as well as Hebrew and Arabic; Bengali and Thai keep `crate::cjk`'s faces) against the system's families for each (Windows, macOS, the common Linux fonts), the first installed one that draws the letter taken once. Desktop only: the web build has no system fonts. Test: `bidi::tests::other_scripts_take_an_installed_system_face` (skips scripts the machine has no face for). | diff --git a/docs/translating.md b/docs/translating.md index 8bad40f0f..10dc2785b 100644 --- a/docs/translating.md +++ b/docs/translating.md @@ -93,16 +93,17 @@ Traditional Chinese is left out unless that build has a Noto Sans CJK TC face. A Chinese UI puts its Chinese face first so Han takes Simplified Chinese shapes, and a Traditional Chinese UI puts an installed Noto Sans CJK TC first, then the system's Taiwan faces (Microsoft JhengHei, PingFang TC, Heiti TC), so Han takes Traditional shapes. No font is added to this -repository. Georgian is drawn in -`FIGCRAFT_EMBED_CJK=1` (Noto Sans CJK SC, +16 MB); otherwise they are left out of the picker. The other -scripts' faces (Noto Sans Arabic, Hebrew, Thai, Devanagari) join a web build only with -`FIGCRAFT_EMBED_SCRIPTS=Arab,Hebr,Thai,Deva` (one tag per script wanted, as the manifest lists them), for -documents in those scripts; with `Arab` embedded the picker offers Arabic too (#1902). A -Chinese UI puts its Chinese face first so Han takes Simplified Chinese shapes. Georgian is drawn in -craft-fonts' Noto Sans Georgian, one of the built-in faces in `theme::install_fonts`, or else in an -installed font on the desktop. Web builds keep it only when the craft-fonts they're built with has -it (`crates/text/build.rs`); the commit releases pin doesn't yet (`docs/releasing.md`), so the web -picker leaves Georgian out. A new CJK or other non-Latin language also needs its script in +repository. + +Web builds with craft-fonts embed Noto Sans Arabic Regular by default for Persian and Arabic +documents, and the picker offers Arabic without extra build flags (#1902). Required web builds +reject a catalog without that face (`crates/text/build.rs`). Web faces are compressed losslessly +at build time and restored once at runtime to preserve the WASM size budget. Additional script +faces (Noto Sans Hebrew, Thai, Devanagari) join a web build with +`FIGCRAFT_EMBED_SCRIPTS=Hebr,Thai,Deva` (one tag per script wanted, as the manifest lists them). +Georgian is drawn in craft-fonts' Noto Sans Georgian, one of the built-in faces in +`theme::install_fonts`, or else in an installed font on the desktop. Required web builds also +require Noto Sans Georgian Regular. A new CJK or other non-Latin language needs its script in `cjk::language`, or a built-in craft-fonts face. ## Arabic draft @@ -118,10 +119,12 @@ This is deliberate: the current plural `Forms` have no zero/two fields. It is no of Arabic's six CLDR categories; wording remains valid for 0, 1, 2 and larger integer counts. Arabic text uses the existing bidi/shaping and Arabic system-font fallback on desktop. Web builds -require an embedded Noto Sans Arabic face to offer Arabic. The current web font budget filters -that face out, so Arabic is not offered on the web yet. No font is added to -this repository. The interface's panels are not mirrored; this catalog does not claim a complete -right-to-left interface. Native review and representative in-app checks remain required. +with craft-fonts include Noto Sans Arabic by default and offer Arabic. This fallback makes Persian +and Arabic text readable; matching Iran Sans exactly still requires that installed font. In a +browser that supports local font access, choose “Use fonts installed on this computer…” in the +Design panel's font menu and grant the browser's permission. No font is added to this repository. +The window mirrors (the panels swap sides), but rows inside the panels keep their left-to-right +order. Native review and representative in-app checks remain required. ## Tamil draft