M10: report packed-font failures and verify generated catalogs

This commit is contained in:
mahmoud committed 2026-11-07 13:39:07 +03:30
1 parent 9e3ab8e43d
commit 5bb124a622
7 files changed
+168 -17

No files matched your search

+2 -2
View File
@@ -133,7 +133,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. Additional scripts remain opt-in. Exact Iran Sans appearance still needs the installed font through supported browsers' local font access. Numbers unchanged.
- **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`.
@@ -365,7 +365,7 @@ Ranked; detail and milestones in [`docs/roadmap.md`](docs/roadmap.md).
| Date | Change | Summary |
|---|---|---|
| 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. Additional scripts remain opt-in. Exact Iran Sans appearance still needs the installed font through supported browsers' local font access. 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 |
+22 -9
View File
@@ -29,6 +29,21 @@ fn main() {
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());
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 {
@@ -38,18 +53,12 @@ fn main() {
} else {
String::from("pub static CRAFT_FONTS: &[CraftFont] = &[\n")
};
match input_fonts(dir.as_deref(), wasm, required, embed_cjk, &embed_scripts, &web_fonts, wasm.then_some(out_dir.as_path())) {
Ok(entries) => src.push_str(&entries),
Err(e) => println!("cargo::error={e}"),
}
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()? })).collect()\n});\n");
}
let out = out_dir.join("craft_fonts.rs");
if let Err(e) = std::fs::write(&out, src) {
println!("cargo::error=writing {}: {e}", out.display());
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
@@ -228,6 +237,10 @@ fn pack_font(path: &std::path::Path, out_dir: &std::path::Path) -> Result<(PathB
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;
+50 -5
View File
@@ -13,12 +13,22 @@ pub(crate) struct PackedFont {
}
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() }
}
pub(crate) fn data(&'static self) -> Option<&'static [u8]> {
self.decoded.get_or_init(|| unpack(self.bytes, self.unpacked_len)).as_deref()
/// 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()
}
}
@@ -44,6 +54,7 @@ mod tests {
// 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";
@@ -53,6 +64,7 @@ mod tests {
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());
@@ -67,6 +79,7 @@ mod tests {
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] {
@@ -75,14 +88,46 @@ mod tests {
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().unwrap();
let first = FONT.data("Synthetic", "Regular").unwrap();
assert_eq!(first, b"font");
assert!(std::ptr::eq(first, FONT.data().unwrap()));
assert!(BAD.data().is_none());
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<Vec<String>>);
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);
}
}
+1
View File
@@ -692,6 +692,7 @@ pub fn db() -> &'static RwLock<FontDb> {
raw_db()
}
/// Initialize and return the shared database, loading valid embedded faces exactly once.
fn raw_db() -> &'static RwLock<FontDb> {
DB.get_or_init(|| {
let mut db = FontDb::default();
@@ -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<Option<&'static [u8]>>,
}
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 });
}
"#;
@@ -6,6 +6,8 @@ use super::{craft_fonts, pack_font};
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 {
+1 -1
View File
@@ -150,7 +150,7 @@ Hindi and Arabic.
| Date | Change | Summary |
|---|---|---|
| 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. 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-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). |