diff --git a/crates/text/src/shape.rs b/crates/text/src/shape.rs index 07fd89380..1e1454024 100644 --- a/crates/text/src/shape.rs +++ b/crates/text/src/shape.rs @@ -1,7 +1,7 @@ use super::autonum; use super::run::{RunEffects, TextShadow}; use super::style::{Cell, Opts, ParaShaped, Style, face_chain, make_style}; -use super::symbol::{covers_symbol, is_symbol_font, map_symbol_bullet, symbol_font_char, symbol_map_char}; +use super::symbol::{covers_symbol, is_symbol_font, map_symbol_bullet, symbol_fallback_char, symbol_font_char, symbol_map_char}; use super::{ Arc, Bullet, Caps, Ctx, Fill, FontDb, FontFace, FontSlot, HashMap, ParaProps, RunKind, RunProps, ScriptTag, Shape, ShapeParams, TextBody, UnicodeSegmentation, bidi, resolve, script, @@ -201,6 +201,15 @@ fn shape_cells( .filter(|f| db.has_family(f)) .map(|f| db.face(f, st.style_name)) .filter(|f| covers_symbol(f, cluster.first().copied().unwrap_or(' '))); + // Without that font, the code draws as the Unicode look-alike (not a missing-glyph box). + let mapped: Vec; + let cluster = match cluster.first().and_then(|c| symbol_fallback_char(*c, st.sym.as_deref())).filter(|_| sym_face.is_none()) { + Some(m) => { + mapped = std::iter::once(m).chain(cluster.iter().skip(1).copied()).collect(); + mapped.as_slice() + } + None => cluster, + }; let face = match (sym_face, prev.as_ref()) { (Some(f), _) => f, (None, Some(p)) @@ -246,12 +255,13 @@ fn shape_cells( } } } - // Items: maximal ranges of one level, face, script and shaping style (case mapping, language). + // Items: maximal ranges of one level, face, script and shaping style (case mapping, language, a:sym symbol font). let upper = |i: usize| styles.get(char_style.get(i).copied().unwrap_or(0)).is_some_and(|st| st.caps != Caps::None); let lang = |i: usize| styles.get(char_style.get(i).copied().unwrap_or(0)).and_then(|st| st.lang.as_deref()); let kern = |i: usize| styles.get(char_style.get(i).copied().unwrap_or(0)).is_some_and(|st| st.kern); let up = |i: usize| cells.get(i).is_some_and(|c| c.upright); - let key = |i: usize| (levels.get(i).copied(), char_face.get(i).copied(), scripts.get(i).map(|s| s.0), upper(i), lang(i), kern(i), up(i)); + let sym = |i: usize| styles.get(char_style.get(i).copied().unwrap_or(0)).and_then(|st| st.sym.as_deref()); + let key = |i: usize| (levels.get(i).copied(), char_face.get(i).copied(), scripts.get(i).map(|s| s.0), upper(i), lang(i), kern(i), up(i), sym(i)); let mut items: Vec<(usize, usize)> = Vec::new(); let mut a = 0; while a < n { @@ -391,7 +401,8 @@ fn shape_item( let sym = styles.get(first.style).and_then(|st| st.sym.as_deref()); let glyphs = deckcraft_fonts::shape_range(face, text, b0..b1, ¶ms, |ch| { let ch = if upper { caps_char(ch) } else { ch }; - symbol_map_char(face, ch, sym).unwrap_or(ch) + let own_font = sym.is_some_and(|s| face.family.eq_ignore_ascii_case(s)); + symbol_map_char(face, ch, sym).or_else(|| symbol_fallback_char(ch, sym).filter(|_| !own_font)).unwrap_or(ch) }); let upem = face.upem.max(1.0); let scale_of = |cell: &Cell| { diff --git a/crates/text/src/symbol.rs b/crates/text/src/symbol.rs index 6b6a8819b..523b4a1d8 100644 --- a/crates/text/src/symbol.rs +++ b/crates/text/src/symbol.rs @@ -30,6 +30,29 @@ pub(crate) fn symbol_map_char(face: &FontFace, ch: char, sym: Option<&str>) -> O symbol_font_draw(face, code) } +/// The Unicode look-alike that draws `ch`, a U+F0xx private-use code of the `a:sym` symbol font +/// `sym`, when that font is not installed (or does not draw the code): the characters bullets use +/// for these fonts. `None` when `ch` is not such a code, `sym` is not a symbol font, or a Symbol-font code +/// has no standard Unicode character (its fragment glyphs); Wingdings and Webdings codes without a look-alike draw as a bullet. +pub(crate) fn symbol_fallback_char(ch: char, sym: Option<&str>) -> Option { + let code = ch as u32; + if !(0xF000..=0xF0FF).contains(&code) { + return None; + } + let sym = sym?; + if !is_symbol_font(sym) { + return None; + } + let low = code & 0xFF; + if low == 0x20 { + return Some(' '); + } + if sym.eq_ignore_ascii_case("symbol") { + return symbol_unicode(low).or_else(|| (0x21..=0x7E).contains(&low).then_some(low)).and_then(char::from_u32); + } + map_symbol_bullet(&ch.to_string(), Some(sym)).chars().next() +} + /// The real-Unicode character the Symbol font's code `code` (0x00–0xFF) maps to, in the Adobe /// Symbol encoding (Unicode, Inc.'s `MAPPINGS/VENDORS/ADOBE/symbol.txt`; Apple's Mac OS Symbol /// shares this layout, with a few alternative Unicode choices — see `symbol_unicode_alts`): the @@ -306,6 +329,7 @@ pub fn map_symbol_bullet(c: &str, font: Option<&str>) -> String { let m = match code { 0xa7 => '▪', 0xd8 => '➢', + 0xe8 => '➔', 0xfc => '✓', 0x76 => '❖', 0x71 => '❑', diff --git a/crates/text/tests/autocraft_regression_1916.rs b/crates/text/tests/autocraft_regression_1916.rs new file mode 100644 index 000000000..9ea567925 --- /dev/null +++ b/crates/text/tests/autocraft_regression_1916.rs @@ -0,0 +1,55 @@ +use deckcraft_fonts::FontDb; +use deckcraft_geom::Rect; +use deckcraft_model::defaults; +use deckcraft_model::resolve::Ctx; +use deckcraft_model::text::{Bullet, TextBody}; +use deckcraft_model::{LayoutType, Presentation, Xfrm}; +use deckcraft_text::{GlyphRun, NoFields, Opts, layout}; + +fn lay_sym(text: &str, sym: &str) -> Vec { + let mut p = Presentation::default(); + let lay = defaults::layout_of_kind(&p, LayoutType::TitleAndContent).expect("layout"); + let mut s = defaults::new_slide(&mut p, lay).expect("slide"); + let mut body = s.shapes.remove(1); + body.xfrm = Some(Xfrm::new(0.0, 0.0, 600.0, 300.0)); + body.text = Some(TextBody::from_text(text)); + let para = &mut body.text.as_mut().expect("text").paragraphs[0]; + para.props.bullet = Some(Bullet::None); + para.runs[0].props.font = Some("Arial".into()); + para.runs[0].props.font_sym = Some(sym.into()); + s.shapes = vec![body.clone()]; + p.slides = vec![std::sync::Arc::new(s)]; + let ctx = Ctx::for_slide(&p, &p.slides[0]).expect("ctx"); + let rect = Rect::new(0.0, 0.0, 600.0, 300.0); + layout(&ctx, &body, body.text.as_ref().expect("text"), &Opts { rect, fields: &NoFields, prompt_color: None, no_shrink: false }).runs +} + +#[test] +fn wingdings_arrow_without_the_sym_font_draws_the_unicode_arrow() { + let db = FontDb::global(); + if db.has_family("Wingdings") { + eprintln!("skipped: Wingdings is installed"); + return; + } + let arrow = '\u{2794}'; + let has_arrow = ["DejaVu Sans", "Noto Sans Symbols", "Noto Sans Symbols2", "Apple Symbols", "Segoe UI Symbol"] + .iter() + .any(|f| db.has_family(f) && db.face(f, "Regular").covers(arrow)); + if !has_arrow { + eprintln!("skipped: no installed font draws U+2794"); + return; + } + // U+F0E8 is Wingdings' right arrow (the issue's "F0 E8" box). + let runs = lay_sym("\u{F0E8} x", "Wingdings"); + let (run, i) = runs.iter().find_map(|r| r.cells.iter().position(|c| c.0 == 0).map(|i| (r, i))).expect("the symbol is drawn"); + let gid = run.glyphs.get(i).map(|g| g.0).expect("a glyph"); + assert_ne!(gid, 0, "the private-use code drew a missing-glyph box"); + assert_eq!(gid, run.face.glyph_for(arrow), "the code draws the arrow, not another character"); + // The Symbol font's bullet code (U+F0B7) draws its bullet, U+2022. + let runs = lay_sym("\u{F0B7} x", "Symbol"); + if let Some((run, i)) = runs.iter().find_map(|r| r.cells.iter().position(|c| c.0 == 0).map(|i| (r, i))) + && !db.has_family("Symbol") + { + assert_eq!(run.glyphs.get(i).map(|g| g.0), Some(run.face.glyph_for('\u{2022}'))); + } +}