PhotoCraft: An open-source, photo editor written by scratch in pure Rust
UI fonts: draw a variable CJK fallback at Regular, not Thin (#4820) (#4830)
Dan Hiorns33d9cb6ac479
diff --git a/crates/ui-egui/src/cjk_fonts.rs b/crates/ui-egui/src/cjk_fonts.rsindex e5166310e..e4c2739fb 100644--- a/crates/ui-egui/src/cjk_fonts.rs+++ b/crates/ui-egui/src/cjk_fonts.rs@@ -285,37 +285,60 @@ fn missing_char(ctx: &egui::Context, shapes: &[egui::epaint::ClippedShape], fallctx.fonts_mut(|f| chars.into_iter().find(|c| !f.has_glyph(&font, *c)))}-/// (ascent, descent, line gap) of a face in em, from its `hhea` table; `descent` is negative.-fn vertical_metrics(font: &[u8], index: u32) -> Option<(f32, f32, f32)> {- let u16_at = |o: usize| font.get(o..o.checked_add(2)?).map(|b| u16::from_be_bytes([b[0], b[1]]));- let u32_at = |o: usize| font.get(o..o.checked_add(4)?).map(|b| u32::from_be_bytes([b[0], b[1], b[2], b[3]]));- let i16_at = |o: usize| u16_at(o).map(|v| v as i16 as f32);+/// Big-endian readers over a font's bytes; `None` past the end.+fn be_u16(font: &[u8], o: usize) -> Option<u16> {+ font.get(o..o.checked_add(2)?).map(|b| u16::from_be_bytes([b[0], b[1]]))+}++fn be_u32(font: &[u8], o: usize) -> Option<u32> {+ font.get(o..o.checked_add(4)?).map(|b| u32::from_be_bytes([b[0], b[1], b[2], b[3]]))+}++/// The offset of table `tag` in face `index` of `font` (a collection or a single face).+fn table_offset(font: &[u8], index: u32, tag: &[u8; 4]) -> Option<usize> {let dir = if font.get(..4)? == b"ttcf" {// Collection: the offset table of face `index` follows a 12-byte header.- u32_at(12usize.checked_add((index as usize).checked_mul(4)?)?)? as usize+ be_u32(font, 12usize.checked_add((index as usize).checked_mul(4)?)?)? as usize} else {0};- let tables = u16_at(dir.checked_add(4)?)? as usize;- let (mut hhea, mut head) = (None, None);- for i in 0..tables {+ let tables = be_u16(font, dir.checked_add(4)?)? as usize;+ (0..tables).find_map(|i| {let rec = dir.checked_add(12)?.checked_add(i.checked_mul(16)?)?;- let tag = font.get(rec..rec.checked_add(4)?)?;- let off = u32_at(rec.checked_add(8)?)? as usize;- match tag {- b"hhea" => hhea = Some(off),- b"head" => head = Some(off),- _ => {}- }- }- let (hhea, head) = (hhea?, head?);- let upm = u16_at(head.checked_add(18)?)? as f32;+ (font.get(rec..rec.checked_add(4)?)? == tag).then_some(())?;+ Some(be_u32(font, rec.checked_add(8)?)? as usize)+ })+}++/// (ascent, descent, line gap) of a face in em, from its `hhea` table; `descent` is negative.+fn vertical_metrics(font: &[u8], index: u32) -> Option<(f32, f32, f32)> {+ let i16_at = |o: usize| be_u16(font, o).map(|v| v as i16 as f32);+ let (hhea, head) = (table_offset(font, index, b"hhea")?, table_offset(font, index, b"head")?);+ let upm = be_u16(font, head.checked_add(18)?)? as f32;if upm <= 0.0 {return None;}Some((i16_at(hhea.checked_add(4)?)? / upm, i16_at(hhea.checked_add(6)?)? / upm, i16_at(hhea.checked_add(8)?)? / upm))}+/// The weight a variable face should be drawn at: Regular (400) within its `wght` axis, or+/// `None` when it has no weight axis or already defaults to Regular. Noto Sans CJK's variable+/// collection defaults to Thin (100), so the UI's CJK text came out hairline (#4820).+fn regular_weight(font: &[u8], index: u32) -> Option<f32> {+ let fvar = table_offset(font, index, b"fvar")?;+ let axes = fvar.checked_add(be_u16(font, fvar.checked_add(4)?)? as usize)?;+ let count = be_u16(font, fvar.checked_add(8)?)? as usize;+ let size = be_u16(font, fvar.checked_add(10)?)? as usize;+ let fixed = |o: usize| be_u32(font, o).map(|v| v as i32 as f32 / 65536.0);+ (0..count.min(64)).find_map(|i| {+ let rec = axes.checked_add(i.checked_mul(size)?)?;+ (font.get(rec..rec.checked_add(4)?)? == b"wght").then_some(())?;+ let (min, default, max) = (fixed(rec.checked_add(4)?)?, fixed(rec.checked_add(8)?)?, fixed(rec.checked_add(12)?)?);+ let regular = 400f32.clamp(min.min(max), max.max(min));+ (regular.is_finite() && (regular - default).abs() > 0.5).then_some(regular)+ })+}+/// `y_offset_factor` that puts `face`'s baseline where `primary`'s would be. egui centres a/// fallback face's row in the primary font's row, so a face with a big line gap (Hiragino: 0.5em)/// otherwise rides about 0.23em high next to Latin text.@@ -332,6 +355,9 @@ fn add_to_all_families(ctx: &egui::Context, name: String, mut data: FontData) {if let (Some(face), Some(primary)) = (vertical_metrics(&data.font, data.index), primary) {data.tweak.y_offset_factor = baseline_offset(face, primary);}+ if let Some(weight) = regular_weight(&data.font, data.index) {+ data.tweak.coords = egui::epaint::text::VariationCoords::new([(b"wght", weight)]);+ }let mut families: Vec<FontFamily> = ctx.fonts(|f| f.definitions().families.keys().cloned().collect());for f in [FontFamily::Proportional, FontFamily::Monospace] {if !families.contains(&f) {@@ -427,6 +453,68 @@ mod tests {}}+ /// A font holding only an `fvar` table with the given axes (tag, min, default, max), as a+ /// single face or as face 1 of a two-face collection.+ fn fvar_font(axes: &[(&[u8; 4], f32, f32, f32)], collection: bool) -> Vec<u8> {+ let fixed = |v: f32| ((v * 65536.0) as i32).to_be_bytes();+ let mut fvar = Vec::new();+ for x in [1u16, 0, 16, 2, axes.len() as u16, 20, 0, 0] {+ fvar.extend(x.to_be_bytes());+ }+ for (tag, min, def, max) in axes {+ fvar.extend(*tag);+ for v in [*min, *def, *max] {+ fvar.extend(fixed(v));+ }+ fvar.extend([0, 0, 1, 0]);+ }+ let face = |at: u32| {+ let mut d = vec![0, 1, 0, 0, 0, 1, 0, 16, 0, 0, 0, 0];+ d.extend(b"fvar");+ d.extend([0; 4]);+ d.extend(at.to_be_bytes());+ d.extend((fvar.len() as u32).to_be_bytes());+ d+ };+ if !collection {+ let mut f = face(28);+ f.extend(&fvar);+ return f;+ }+ // ttcf header (12 + 2×4), two 28-byte directories, then the shared table.+ let mut f = b"ttcf".to_vec();+ f.extend([0, 1, 0, 0, 0, 0, 0, 2]);+ f.extend(20u32.to_be_bytes());+ f.extend(48u32.to_be_bytes());+ f.extend(face(76));+ f.extend(face(76));+ f.extend(&fvar);+ f+ }++ /// #4820: Noto Sans CJK's variable collection defaults to Thin; the fallback asks for Regular.+ #[test]+ fn variable_fallbacks_are_drawn_at_regular_weight() {+ let thin = fvar_font(&[(b"wght", 100.0, 100.0, 900.0)], true);+ assert_eq!(regular_weight(&thin, 1), Some(400.0), "Noto Sans CJK VF, face 1 of a collection");+ assert_eq!(regular_weight(&fvar_font(&[(b"wdth", 75.0, 100.0, 100.0), (b"wght", 100.0, 100.0, 900.0)], false), 0), Some(400.0));+ assert_eq!(regular_weight(&fvar_font(&[(b"wght", 500.0, 600.0, 900.0)], false), 0), Some(500.0), "Regular clamped into the axis");+ assert_eq!(regular_weight(&fvar_font(&[(b"wght", 100.0, 400.0, 900.0)], false), 0), None, "already Regular");+ assert_eq!(regular_weight(&fvar_font(&[(b"wdth", 75.0, 100.0, 100.0)], false), 0), None, "no weight axis");+ assert_eq!(regular_weight(photocraft_text::fonts::INTER_REGULAR.as_slice(), 0), None, "static font");+ for bad in [&thin[..thin.len() - 9], &thin[..30], &b"ttcf"[..], &[][..]] {+ assert_eq!(regular_weight(bad, 1), None);+ }+ // Registered as a fallback, a static face keeps the default (no variation) coordinates.+ let ctx = egui::Context::default();+ crate::theme::install_fonts(&ctx);+ ctx.run_ui(Default::default(), |_| {}).textures_delta.clear();+ add_to_all_families(&ctx, "test-static".into(), FontData::from_static(photocraft_text::fonts::INTER_REGULAR.as_slice()));+ ctx.run_ui(Default::default(), |_| {}).textures_delta.clear();+ let fonts = ctx.fonts(|f| f.definitions().clone());+ assert_eq!(fonts.font_data.get("test-static").map(|d| d.tweak.coords.clone()), Some(Default::default()));+ }+static DIR: Mutex<Option<PathBuf>> = Mutex::new(None);#[test]