diff --git a/crates/doc/src/text.rs b/crates/doc/src/text.rs index 75a31230e8..fc586e3c2c 100644 --- a/crates/doc/src/text.rs +++ b/crates/doc/src/text.rs @@ -18,9 +18,9 @@ pub enum Kerning { /// Font kerning (`kern` feature). #[default] Metrics, - /// Photoshop's optical kerning; rendered like `Metrics` for now. + /// Photoshop's optical kerning: pair spacing computed from the glyph outlines. Optical, - /// No kerning. + /// No automatic kerning (Photoshop's "0" / manual kerning). Off, } @@ -73,7 +73,11 @@ pub struct CharStyle { pub strikethrough: bool, pub faux_bold: bool, pub faux_italic: bool, + /// Automatic (pair) kerning mode. pub kerning: Kerning, + /// Manual kerning in 1/1000 em between this character and the next one (Photoshop stores it + /// per character run as `Kerning`). Added to the automatic kerning of the pair. + pub kern: f32, pub caps: Caps, /// Standard ligatures (`liga`/`clig`). pub ligatures: bool, @@ -111,6 +115,7 @@ impl Default for CharStyle { faux_bold: false, faux_italic: false, kerning: Kerning::Metrics, + kern: 0.0, caps: Caps::Normal, ligatures: true, discretionary_ligatures: false, diff --git a/crates/engine/src/type_cmds.rs b/crates/engine/src/type_cmds.rs index 779b75f1d5..036990b5c0 100644 --- a/crates/engine/src/type_cmds.rs +++ b/crates/engine/src/type_cmds.rs @@ -82,6 +82,24 @@ fn range_param(text: &str, p: &Value) -> (usize, usize) { } } +/// Photoshop's manual kerning range (1/1000 em). +pub const KERN_MIN: f64 = -1000.0; +pub const KERN_MAX: f64 = 10_000.0; + +/// Validates the `kerning` key of character params: a number in [`KERN_MIN`]..=[`KERN_MAX`] or +/// `"metrics"`, `"optical"`, `"off"` (`"none"`, `"0"`). +pub fn check_kerning(p: &Value) -> std::result::Result<(), String> { + match p.get("kerning") { + None => Ok(()), + Some(Value::Number(n)) => match n.as_f64() { + Some(k) if (KERN_MIN..=KERN_MAX).contains(&k) => Ok(()), + _ => Err(format!("kerning must be between {KERN_MIN} and {KERN_MAX} (1/1000 em)")), + }, + Some(Value::String(v)) if matches!(v.to_ascii_lowercase().as_str(), "metrics" | "optical" | "off" | "none" | "0") => Ok(()), + Some(_) => Err("kerning must be a number (1/1000 em) or \"metrics\", \"optical\" or \"off\"".into()), + } +} + /// Applies character-style keys from JSON. Returns true if any key was present. pub fn apply_char_props(s: &mut CharStyle, p: &Value) -> bool { let mut any = false; @@ -157,13 +175,25 @@ pub fn apply_char_props(s: &mut CharStyle, p: &Value) -> bool { hit(true); } } - if let Some(v) = p.get("kerning").and_then(Value::as_str) { - s.kerning = match v { - "optical" => Kerning::Optical, - "none" | "off" | "0" => Kerning::Off, - _ => Kerning::Metrics, - }; - hit(true); + match p.get("kerning") { + // A number is Photoshop's manual kerning: no automatic pair kerning plus the value. + Some(v) if v.is_number() => { + if let Some(k) = v.as_f64().filter(|k| k.is_finite()) { + s.kerning = Kerning::Off; + s.kern = k.clamp(KERN_MIN, KERN_MAX) as f32; + hit(true); + } + } + Some(Value::String(v)) => { + s.kerning = match v.to_ascii_lowercase().as_str() { + "optical" => Kerning::Optical, + "none" | "off" | "0" => Kerning::Off, + _ => Kerning::Metrics, + }; + s.kern = 0.0; + hit(true); + } + _ => {} } if let Some(v) = p.get("caps").and_then(Value::as_str) { s.caps = match v { @@ -435,7 +465,7 @@ fn info(s: &Session, p: &Value) -> Result { })) } -const CHAR_PARAMS: &str = r##""font":str,"fontStyle":str,"weight":100..900,"italic":bool,"size":pt,"color":"#rrggbb"|[r,g,b,a],"tracking":1/1000em,"leading":pt|"auto","baselineShift":pt,"horizontalScale":%,"verticalScale":%,"underline":bool,"strikethrough":bool,"fauxBold":bool,"fauxItalic":bool,"kerning":"metrics|optical|none","caps":"normal|small|all","ligatures":bool,"discretionaryLigatures":bool,"features":{"ss01":1},"variations":{"wght":650},"language":str"##; +const CHAR_PARAMS: &str = r##""font":str,"fontStyle":str,"weight":100..900,"italic":bool,"size":pt,"color":"#rrggbb"|[r,g,b,a],"tracking":1/1000em,"leading":pt|"auto","baselineShift":pt,"horizontalScale":%,"verticalScale":%,"underline":bool,"strikethrough":bool,"fauxBold":bool,"fauxItalic":bool,"kerning":1/1000em (manual, after each character)|"metrics"|"optical"|"off","caps":"normal|small|all","ligatures":bool,"discretionaryLigatures":bool,"features":{"ss01":1},"variations":{"wght":650},"language":str"##; pub fn specs() -> Vec { vec![ @@ -491,13 +521,58 @@ pub fn specs() -> Vec { label: "Edit Type", menu: &[], shortcut: None, - params: r##"{"layer":id?,"text":str? (replace all, styles kept),"replace":{"start":char,"end":char,"text":str}?,"runs":[{"start":char,"end":char,…character keys}]?,"box":[x,y,w,h]? (to paragraph text),"point":[x,y]? (to point text),"move":[dx,dy]?,"transform":[a,b,c,d,e,f]?,"antialias":"none|sharp|crisp|strong|smooth"?,"name":str?}"##, + params: r##"{"layer":id?,"text":str? (replace all, styles kept),"replace":{"start":char,"end":char,"text":str}?,"runs":[{"start":char,"end":char,…character keys}]?,"box":[x,y,w,h]? (to paragraph text),"point":[x,y]? (to point text),"move":[dx,dy]?,"transform":[a,b,c,d,e,f]?,"antialias":"none|sharp|crisp|strong|smooth"?,"name":str?,"kerning":1/1000em|"metrics"|"optical"|"off"? (with "range":[startChar,endChar]?, default all),"kernPair":{"at":caretChar,"by":1/1000em}? (Photoshop Alt+←/→: the pair before the caret becomes manual, its current kerning + by)}"##, enabled: has_doc, journal: true, run: |s, p| { let id = layer_id(s, p)?; let name = p.get("name").and_then(Value::as_str).map(str::to_string); - with_text_layer(s, p, "Edit Type", |t, _| { + check_kerning(p).map_err(|m| bad("type.edit", m))?; + if let Some(Value::Array(runs)) = p.get("runs") { + for r in runs { + check_kerning(r).map_err(|m| bad("type.edit", m))?; + } + } + let kern_pair = match p.get("kernPair") { + None => None, + Some(k) => { + let at = k.get("at").and_then(Value::as_u64).ok_or_else(|| bad("type.edit", "kernPair.at must be a caret position (character index)"))?; + let by = k + .get("by") + .and_then(Value::as_f64) + .filter(|v| v.is_finite() && v.abs() <= KERN_MAX) + .ok_or_else(|| bad("type.edit", "kernPair.by must be a number of 1/1000 em"))?; + Some((at as usize, by)) + } + }; + let label = if kern_pair.is_some() { "Kerning" } else { "Edit Type" }; + with_text_layer(s, p, label, |t, doc| { + if let Some((at, by)) = kern_pair { + // Photoshop's Alt+←/→: the pair before the caret becomes manually kerned, + // starting from what it shows now (its metrics/optical or manual value). + let n = t.text.chars().count(); + if at == 0 || at >= n { + return Err(bad("type.edit", "kernPair needs a caret between two characters")); + } + let a = byte_at(&t.text, at - 1); + let b = byte_at(&t.text, at); + let now = { + let mut eng = photocraft_text::shared().lock().unwrap_or_else(|e| e.into_inner()); + eng.pair_kerning(t, doc.resolution_dpi, a) + } + .ok_or_else(|| bad("type.edit", "no kerning pair at the caret (line break or line end)"))?; + let value = (f64::from(now.round()) + by).clamp(KERN_MIN, KERN_MAX) as f32; + style_range(t, a, b, &|st| { + st.kerning = Kerning::Off; + st.kern = value; + }); + } + if p.get("kerning").is_some() { + let (a, b) = range_param(&t.text, p); + style_range(t, a, b, &|st| { + apply_char_props(st, &json!({ "kerning": p.get("kerning") })); + }); + } if let Some(v) = p.get("text").and_then(Value::as_str) { let n = t.text.len(); replace_text(t, 0, n, v); @@ -569,6 +644,7 @@ pub fn specs() -> Vec { journal: true, run: |s, p| { let id = layer_id(s, p)?; + check_kerning(p).map_err(|m| bad("type.setStyle", m))?; with_text_layer(s, p, "Set Type Style", |t, _| { let (a, b) = range_param(&t.text, p); let mut probe = CharStyle::default(); @@ -793,4 +869,129 @@ mod tests { assert_eq!(t.runs[0].len, 3); assert_eq!(t.runs[0].style.size_pt, 40.0); } + + fn kerning_of(s: &Session, id: u64) -> Vec<(Kerning, f32)> { + let t = text_layer(s, id); + let mut out = Vec::new(); + let mut at = 0; + for r in t.char_runs() { + out.extend(std::iter::repeat_n((r.style.kerning, r.style.kern), t.text[at..at + r.len].chars().count())); + at += r.len; + } + out + } + + /// #206: `type.edit {"kerning"}` over a range takes a number (manual, 1/1000 em) or a mode. + #[test] + fn edit_sets_kerning_over_a_range() { + let mut s = session(); + let id = s.execute("type.create", json!({"x": 5, "y": 50, "text": "AVAT", "size": 30})).unwrap()["layer"].as_u64().unwrap(); + s.execute("type.edit", json!({"layer": id, "range": [0, 1], "kerning": 100})).unwrap(); + s.execute("type.edit", json!({"layer": id, "range": [2, 4], "kerning": "optical"})).unwrap(); + assert_eq!(kerning_of(&s, id), vec![(Kerning::Off, 100.0), (Kerning::Metrics, 0.0), (Kerning::Optical, 0.0), (Kerning::Optical, 0.0)]); + s.execute("type.edit", json!({"layer": id, "kerning": "metrics"})).unwrap(); + assert!(kerning_of(&s, id).iter().all(|k| *k == (Kerning::Metrics, 0.0))); + // Runs and setStyle take the same values; type.info reports them. + s.execute("type.edit", json!({"layer": id, "runs": [{"start": 1, "end": 2, "kerning": -50}]})).unwrap(); + s.execute("type.setStyle", json!({"layer": id, "range": [3, 4], "kerning": "off"})).unwrap(); + assert_eq!(kerning_of(&s, id)[1], (Kerning::Off, -50.0)); + assert_eq!(kerning_of(&s, id)[3], (Kerning::Off, 0.0)); + let info = s.execute("type.info", json!({"layer": id})).unwrap(); + assert_eq!(info["runs"][1]["style"]["kern"], json!(-50.0)); + } + + /// Bad kerning params are errors, never panics, and leave the layer alone. + #[test] + fn kerning_params_fail_gracefully() { + let mut s = session(); + let id = s.execute("type.create", json!({"x": 5, "y": 50, "text": "AV\nT", "size": 30})).unwrap()["layer"].as_u64().unwrap(); + let before = text_layer(&s, id); + for p in [ + json!({"kerning": "tight"}), + json!({"kerning": 1e9}), + json!({"kerning": -5000}), + json!({"kerning": true}), + json!({"kerning": [1]}), + json!({"runs": [{"start": 0, "end": 1, "kerning": "x"}]}), + json!({"kernPair": 3}), + json!({"kernPair": {"by": 20}}), + json!({"kernPair": {"at": 1}}), + json!({"kernPair": {"at": 1, "by": "20"}}), + json!({"kernPair": {"at": 1, "by": 1e12}}), + json!({"kernPair": {"at": 0, "by": 20}}), + json!({"kernPair": {"at": 4, "by": 20}}), + json!({"kernPair": {"at": 99, "by": 20}}), + json!({"kernPair": {"at": 2, "by": 20}}), + json!({"kernPair": {"at": 3, "by": 20}}), + json!({"kernPair": {"at": -1, "by": 20}}), + ] { + let mut p = p; + p["layer"] = json!(id); + assert!(s.execute("type.edit", p.clone()).is_err(), "{p}"); + } + assert!(s.execute("type.setStyle", json!({"layer": id, "kerning": "tight"})).is_err()); + assert_eq!(text_layer(&s, id).runs, before.runs); + } + + /// Cost of one Alt+←/→ press (`kernPair`: two measuring layouts + the edit's re-render) on + /// a headline and on a 2000-character paragraph. Release timings for the dev log: + /// `cargo test --release -p photocraft-engine kern_pair_cost -- --ignored --nocapture`. + #[test] + #[ignore] + fn kern_pair_cost() { + let para = "The quick brown fox jumps over the lazy dog. ".repeat(45); + for (name, text, size, bx, kerning) in [ + ("headline", "AVATAR Wave", 72, false, "metrics"), + ("paragraph", para.as_str(), 12, true, "metrics"), + ("optical paragraph", para.as_str(), 12, true, "optical"), + ] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 2000, "height": 1500})).unwrap(); + let mut p = json!({"x": 20, "y": 100, "text": text, "size": size, "font": "Inter", "kerning": kerning}); + if bx { + p["box"] = json!([20, 20, 900, 1400]); + } + let id = s.execute("type.create", p).unwrap()["layer"].as_u64().unwrap(); + let n = 20; + let t0 = std::time::Instant::now(); + for i in 0..n { + s.execute("type.edit", json!({"layer": id, "kernPair": {"at": 1 + i % 3, "by": 20}})).unwrap(); + } + let per = t0.elapsed().as_secs_f64() * 1000.0 / f64::from(n); + let t1 = std::time::Instant::now(); + for i in 0..n { + s.execute("type.setStyle", json!({"layer": id, "range": [1 + i % 3, 2 + i % 3], "tracking": i})).unwrap(); + } + let base = t1.elapsed().as_secs_f64() * 1000.0 / f64::from(n); + eprintln!("{name} ({} chars): kernPair {per:.2} ms/press; a plain style edit {base:.2} ms", text.chars().count()); + } + } + + /// Photoshop's Alt+←/→: the pair before the caret becomes manual, starting from the kerning + /// it shows (the font's pair kerning for "AV"), ±20 per press, one history step each. + #[test] + fn kern_pair_steps_from_the_shown_kerning() { + let mut s = session(); + let id = s.execute("type.create", json!({"x": 5, "y": 50, "text": "AVA", "size": 40, "font": "Inter"})).unwrap()["layer"].as_u64().unwrap(); + let metric = { + let t = text_layer(&s, id); + photocraft_text::shared().lock().unwrap().pair_kerning(&t, 72.0, 0).unwrap() + }; + assert!(metric < -10.0, "Inter kerns AV: {metric}"); + let steps = s.active().unwrap().history.entries().len(); + s.execute("type.edit", json!({"layer": id, "kernPair": {"at": 1, "by": 20}})).unwrap(); + let k0 = kerning_of(&s, id)[0]; + assert_eq!(k0, (Kerning::Off, metric.round() + 20.0)); + s.execute("type.edit", json!({"layer": id, "kernPair": {"at": 1, "by": 100}})).unwrap(); + assert_eq!(kerning_of(&s, id)[0].1, metric.round() + 120.0); + s.execute("type.edit", json!({"layer": id, "kernPair": {"at": 1, "by": -20}})).unwrap(); + assert_eq!(kerning_of(&s, id)[0].1, metric.round() + 100.0); + // Other characters are untouched; each press is its own undoable step. + assert_eq!(&kerning_of(&s, id)[1..], &[(Kerning::Metrics, 0.0), (Kerning::Metrics, 0.0)]); + assert_eq!(s.active().unwrap().history.entries().len(), steps + 3); + assert!(s.undo()); + assert_eq!(kerning_of(&s, id)[0].1, metric.round() + 120.0); + assert!(s.undo() && s.undo()); + assert_eq!(kerning_of(&s, id)[0], (Kerning::Metrics, 0.0)); + } } diff --git a/crates/engine/src/type_styles_cmds.rs b/crates/engine/src/type_styles_cmds.rs index d677df50f0..f54116c99f 100644 --- a/crates/engine/src/type_styles_cmds.rs +++ b/crates/engine/src/type_styles_cmds.rs @@ -99,6 +99,7 @@ fn perturbed_char() -> CharStyle { faux_bold: true, faux_italic: true, kerning: Kerning::Off, + kern: -12345.0, caps: Caps::AllCaps, ligatures: false, discretionary_ligatures: true, diff --git a/crates/format/tests/common/mod.rs b/crates/format/tests/common/mod.rs index 27d6f28669..77c2273c44 100644 --- a/crates/format/tests/common/mod.rs +++ b/crates/format/tests/common/mod.rs @@ -123,7 +123,11 @@ pub fn rich_doc(mode: ColorMode, depth: SampleType) -> Document { transform: Affine::translate(10.5, 20.25), cache: Some(cache.clone()), psd_raw: Some(blob(9, 100)), - runs: vec![TextRun { len: 6, style: CharStyle::default() }, TextRun { len: 9, style: CharStyle::default() }], + runs: vec![ + TextRun { len: 1, style: CharStyle { kerning: text::Kerning::Off, kern: 120.0, ..CharStyle::default() } }, + TextRun { len: 5, style: CharStyle::default() }, + TextRun { len: 9, style: CharStyle { kerning: text::Kerning::Optical, ..CharStyle::default() } }, + ], paragraphs: vec![ParagraphRun { len: 15, style: ParagraphStyle::default() }], shape: TextShape::Box { x: 1.0, y: 2.0, width: 100.0, height: 50.0 }, orientation: text::Orientation::Vertical, diff --git a/crates/format/tests/roundtrip.rs b/crates/format/tests/roundtrip.rs index f52094bf9b..998bf33480 100644 --- a/crates/format/tests/roundtrip.rs +++ b/crates/format/tests/roundtrip.rs @@ -231,6 +231,45 @@ fn vector_fields_roundtrip_and_default_when_absent() { assert!(old.layers.iter().all(|l| l.vector_mask.is_none())); } +/// Type kerning (#206): the auto mode and manual kerning survive .pcraft; manifests written +/// before `kern` existed load with no manual kerning. +#[test] +fn text_kerning_roundtrips_and_defaults() { + use photocraft_doc::LayerContent; + use photocraft_doc::text::Kerning; + let doc = rich_doc(ColorMode::Rgb, SampleType::U8); + let bytes = save_to_bytes(&doc, &SaveOptions::default()).unwrap(); + let back = load_from_bytes(&bytes).unwrap(); + let runs = |d: &Document| { + d.walk() + .into_iter() + .find_map(|(_, _, l)| match &l.content { + LayerContent::Text(t) => Some(t.runs.iter().map(|r| (r.len, r.style.kerning, r.style.kern)).collect::>()), + _ => None, + }) + .unwrap() + }; + assert_eq!(runs(&back), vec![(1, Kerning::Off, 120.0), (5, Kerning::Metrics, 0.0), (9, Kerning::Optical, 0.0)]); + assert_eq!(runs(&back), runs(&doc)); + let m = read_manifest(&bytes).unwrap(); + let mut v = serde_json::to_value(&m.document).unwrap(); + fn strip(v: &mut serde_json::Value) { + match v { + serde_json::Value::Object(o) => { + o.remove("kern"); + o.values_mut().for_each(strip); + } + serde_json::Value::Array(a) => a.iter_mut().for_each(strip), + _ => {} + } + } + strip(&mut v); + assert!(!v.to_string().contains("\"kern\"")); + let old: photocraft_format::manifest::DocM = serde_json::from_value(v).unwrap(); + let s = serde_json::to_string(&old).unwrap(); + assert!(s.contains("\"kern\":0.0"), "kern defaults to 0"); +} + #[test] fn channel_restrictions_and_bevel_elements_roundtrip() { use photocraft_doc::{Bevel, BevelContour, BevelTexture, Contour, Effect}; diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index 9ca71b4940..ecfdd21724 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -51,6 +51,8 @@ pub(crate) struct Ctx<'a> { pub warnings: Vec, /// Document resolution (type sizes are converted to points with it). pub dpi: f32, + /// The parsed `Txt2` block (type settings EngineData lacks, e.g. optical kerning). + pub txt2: Option, } fn doc_mode(m: PsdMode) -> Option { @@ -251,6 +253,9 @@ impl Ctx<'_> { let (text, transform) = blocks::parse_tysh(&data).unwrap_or_default(); TextLayer { text, transform, ..Default::default() } }); + if let Some(txt2) = &self.txt2 { + photocraft_text::psd::apply_txt2(&mut t, &data, txt2); + } t.cache = Some(self.record_surface(rec, &name)); t.psd_raw = principal(b"TySh"); LayerContent::Text(t) @@ -490,6 +495,7 @@ pub fn psd_to_document(file: &PsdFile) -> (Document, Vec) { cmyk: fmt.mode == ColorMode::Cmyk, warnings, dpi: doc.resolution_dpi, + txt2: file.global_blocks.iter().find(|b| &b.key == b"Txt2").and_then(|b| photocraft_text::psd::parse_txt2(&b.data)), }; let (w, hh) = (h.width as usize, h.height as usize); diff --git a/crates/io/src/text_styles_map.rs b/crates/io/src/text_styles_map.rs index c14d28c5ec..8919114ab3 100644 --- a/crates/io/src/text_styles_map.rs +++ b/crates/io/src/text_styles_map.rs @@ -45,13 +45,31 @@ pub fn import(doc: &mut Document) { }); pmap.insert(*ix, id); } - link(t, &sh.run_char, &cmap, &sh.run_para, &pmap); + link(t, &sh.run_char, &sh.run_char_lens, &cmap, &sh.run_para, &pmap); } doc.text_styles = styles; } -fn link(t: &mut TextLayer, run_char: &[Option], cmap: &HashMap, run_para: &[Option], pmap: &HashMap) { - if t.runs.len() == run_char.len() { +fn link( + t: &mut TextLayer, + run_char: &[Option], + run_char_lens: &[usize], + cmap: &HashMap, + run_para: &[Option], + pmap: &HashMap, +) { + if run_char_lens.len() == run_char.len() && !run_char.is_empty() { + // Each model run takes the sheet of the `StyleRun` entry its first character is in. + let (mut off, mut i, mut end) = (0usize, 0usize, run_char_lens.first().copied().unwrap_or(0)); + for r in t.runs.iter_mut() { + while off >= end && i + 1 < run_char_lens.len() { + i += 1; + end = end.saturating_add(run_char_lens.get(i).copied().unwrap_or(0)); + } + r.style.style_sheet = run_char.get(i).copied().flatten().and_then(|i| cmap.get(&i).copied()); + off = off.saturating_add(r.len); + } + } else if t.runs.len() == run_char.len() { for (r, ix) in t.runs.iter_mut().zip(run_char) { r.style.style_sheet = ix.and_then(|i| cmap.get(&i).copied()); } diff --git a/crates/io/tests/psd_kerning.rs b/crates/io/tests/psd_kerning.rs new file mode 100644 index 0000000000..73cec791b9 --- /dev/null +++ b/crates/io/tests/psd_kerning.rs @@ -0,0 +1,189 @@ +//! Kerning of PSD type layers (#206): manual kerning in EngineData (`AutoKerning`/`Kerning` +//! style-run fields), the automatic mode in `Txt2`, and the layout against Photoshop's own +//! glyph positions. +//! +//! The Photoshop-authored oracles (`corpus/photoshop/text/kerning-*.psd`) run under the +//! `corpus` feature (`cargo xtask test-corpus`). + +#[cfg(feature = "corpus")] +use std::path::Path; + +use photocraft_doc::text::{CharStyle, Kerning, TextRun}; +use photocraft_doc::{Document, Layer, LayerContent, TextLayer}; +use photocraft_text::engine_data::Value as E; + +/// `(AutoKerning, Kerning)` of every UTF-16 unit of the engine text of a `TySh` block. +fn kerning_fields(tysh: &[u8]) -> Vec<(bool, i64)> { + let t = photocraft_text::psd::parse_tysh(tysh).unwrap(); + let e = photocraft_text::psd::engine_data(&t.text).unwrap(); + let runs = e.path(&["EngineDict", "StyleRun", "RunArray"]).and_then(E::as_array).unwrap(); + let lens = e.path(&["EngineDict", "StyleRun", "RunLengthArray"]).and_then(E::as_array).unwrap(); + let mut out = Vec::new(); + for (r, n) in runs.iter().zip(lens) { + let d = r.path(&["StyleSheet", "StyleSheetData"]).unwrap(); + let f = (d.get("AutoKerning").and_then(E::as_bool).unwrap_or(true), d.get("Kerning").and_then(E::as_i64).unwrap_or(0)); + out.extend(std::iter::repeat_n(f, n.as_i64().unwrap() as usize)); + } + out +} + +/// Per character: (mode, manual kern). +fn chars(t: &TextLayer) -> Vec<(Kerning, f32)> { + let mut out = Vec::new(); + let mut at = 0; + for r in t.char_runs() { + let n = t.text.get(at..at + r.len).map_or(0, |s| s.chars().count()); + out.extend(std::iter::repeat_n((r.style.kerning, r.style.kern), n)); + at += r.len; + } + out +} + +fn layer(text: &str, runs: &[(usize, Kerning, f32)]) -> TextLayer { + let base = CharStyle { font_family: "Inter".into(), size_pt: 24.0, ..Default::default() }; + let mut t = TextLayer { + text: text.into(), + runs: runs.iter().map(|&(len, kerning, kern)| TextRun { len, style: CharStyle { kerning, kern, ..base.clone() } }).collect(), + ..Default::default() + }; + t.sync_summary(); + t +} + +/// The model survives a PSD round trip, and the EngineData fields follow Photoshop's form: a +/// manual kern after character c sits on c + 1; "no automatic kerning" on c is +/// `AutoKerning false` on c + 2 (how Photoshop reads it). +#[test] +fn manual_and_off_kerning_round_trip_through_psd() { + use Kerning::{Metrics as M, Off as O}; + // "AVATAR Wave": +100 after A, -50 after T, characters 6..8 without automatic kerning. + let t = layer("AVATAR Wave", &[(1, O, 100.0), (2, M, 0.0), (1, O, -50.0), (2, M, 0.0), (3, O, 0.0), (2, M, 0.0)]); + let mut doc = Document::new("k", photocraft_geom::Size::new(64, 32), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); + let mut t2 = t.clone(); + photocraft_text::TextEngine::new().render_layer(&mut t2, doc.resolution_dpi, doc.pixel_format()); + doc.layers.push(Layer::new("kerned", LayerContent::Text(t2))); + let bytes = photocraft_io::export(&doc, "k.psd", &Default::default()).unwrap().bytes; + let back = photocraft_io::import("k.psd", &bytes).unwrap().document; + let LayerContent::Text(b) = &back.layers[0].content else { panic!("not text") }; + assert_eq!(chars(b), chars(&t)); + + let tysh = photocraft_text::psd::build_tysh(&t, 72.0, None); + let f = kerning_fields(&tysh); + // 11 characters + the trailing paragraph break. + assert_eq!(f.len(), 12); + assert_eq!(f[1], (false, 100), "kern after A on V, Photoshop's form"); + assert_eq!(f[4], (true, -50), "kern after T on A; character 2 isn't manual"); + for j in [8, 9, 10] { + assert!(!f[j].0, "character {} without automatic kerning", j - 2); + } + assert!(f.iter().enumerate().filter(|(j, _)| ![1, 4, 8, 9, 10].contains(j)).all(|(_, x)| *x == (true, 0)), "{f:?}"); +} + +/// Kerning that can't be represented (non-finite) and bad PSD fields never panic and stay sane. +#[test] +fn hostile_kerning_values() { + let t = layer("AV", &[(1, Kerning::Off, f32::NAN), (1, Kerning::Metrics, f32::INFINITY)]); + let tysh = photocraft_text::psd::build_tysh(&t, 72.0, None); + assert!(kerning_fields(&tysh).iter().all(|f| f.1 == 0)); + let back = photocraft_text::psd::text_layer_from_tysh(&tysh, 72.0).unwrap(); + assert!(chars(&back).iter().all(|c| c.1 == 0.0)); + // Huge values clamp. + let t = layer("AV", &[(1, Kerning::Off, 1e30), (1, Kerning::Metrics, 0.0)]); + let f = kerning_fields(&photocraft_text::psd::build_tysh(&t, 72.0, None)); + assert_eq!(f[1].1, 10_000); +} + +/// Glyph pen positions Photoshop stored for text object `index` in `Txt2` (`/21 /1`). +#[cfg(feature = "corpus")] +fn photoshop_positions(txt2: &E, index: usize) -> Option> { + fn find(v: &E, depth: usize) -> Option> { + if depth > 100 { + return None; + } + match v { + E::Dict(items) => { + if let Some(p) = v.get("21").and_then(|d| d.get("1")).and_then(E::as_array) { + return Some(p.iter().filter_map(E::as_f64).collect()); + } + items.iter().find_map(|(_, x)| find(x, depth + 1)) + } + E::Array(a) => a.iter().find_map(|x| find(x, depth + 1)), + _ => None, + } + } + find(txt2.path(&["1", "1"])?.as_array()?.get(index)?, 0) +} + +/// Photoshop-authored kerning oracles (`corpus/photoshop/text/kerning-*.psd`, made from scratch +/// by photocraft-corpus's generator): the automatic mode read from `Txt2` per character, the +/// EngineData kerning fields after export, and our layout against Photoshop's glyph positions. +#[cfg(feature = "corpus")] +#[test] +fn photoshop_kerning_oracles() { + use Kerning::{Metrics as M, Off as O, Optical as P}; + let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop/text"); + assert!(dir.is_dir(), "{} is missing: run `cargo xtask corpus --all`", dir.display()); + let mut engine = photocraft_text::TextEngine::with_system_fonts(); + let arial = engine.fonts.has_family("Arial"); + // "AVATAR Wavy To. LT" (18 characters). + let all = |m: Kerning| vec![(m, 0.0f32); 18]; + let partial: Vec<(Kerning, f32)> = (0..18).map(|i| (if (1..=3).contains(&i) { O } else { M }, 0.0)).collect(); + // (file, expected modes, EngineData fields as Photoshop wrote them survive our export, + // worst / mean advance error allowed in px). + type Case = (&'static str, Vec<(Kerning, f32)>, bool, f64, f64); + let cases: [Case; 5] = [ + ("kerning-metrics.psd", all(M), true, 0.02, 0.02), + ("kerning-off.psd", all(O), false, 0.02, 0.02), + ("kerning-off-partial.psd", partial, false, 0.02, 0.02), + // The fitted optical model: within ~30/1000 em per pair, ~12 on average. + ("kerning-optical.psd", all(P), true, 1.0, 0.4), + ("kerning-optical-24pt.psd", all(P), true, 0.75, 0.3), + ]; + for (name, want, same_fields, worst_max, mean_max) in cases { + let bytes = std::fs::read(dir.join(name)).unwrap_or_else(|e| panic!("{name}: {e}")); + let file = photocraft_psd::PsdFile::from_bytes(&bytes).unwrap(); + let txt2 = file.global_blocks.iter().find(|b| &b.key == b"Txt2").and_then(|b| photocraft_text::psd::parse_txt2(&b.data)).unwrap(); + let doc = photocraft_io::import(name, &bytes).unwrap().document; + let t = doc + .layers + .iter() + .find_map(|l| match &l.content { + LayerContent::Text(t) => Some(t), + _ => None, + }) + .unwrap(); + assert_eq!(chars(t), want, "{name}"); + + let original = t.psd_raw.as_ref().map(|r| r.to_vec()).unwrap(); + let tysh = photocraft_text::psd::parse_tysh(&original).unwrap(); + let rebuilt = photocraft_text::psd::build_tysh(t, doc.resolution_dpi, None); + if same_fields { + assert_eq!(kerning_fields(&rebuilt), kerning_fields(&original), "{name}"); + } + // Re-import (with the document's Txt2, as an exported file keeps it): same model. + let mut back = photocraft_text::psd::text_layer_from_tysh(&rebuilt, doc.resolution_dpi).unwrap(); + photocraft_text::psd::apply_txt2(&mut back, &rebuilt, &txt2); + assert_eq!(chars(&back), want, "{name} after export"); + + // Layout against Photoshop's glyph positions (needs Arial, as Photoshop used). + if !arial { + eprintln!("{name}: Arial not installed, layout comparison skipped"); + continue; + } + let index = match &tysh.text.get("TextIndex") { + Some(photocraft_psd::descriptor::Value::Integer(i)) => *i as usize, + _ => panic!("{name}: no TextIndex"), + }; + let ps = photoshop_positions(&txt2, index).unwrap(); + let x: Vec = engine.layout(t, doc.resolution_dpi).glyphs.iter().map(|g| f64::from(g.x)).collect(); + let (mut worst, mut sum) = (0.0f64, 0.0f64); + for i in 0..x.len() - 1 { + let d = (x[i + 1] - x[i]) - (ps[i + 1] - ps[i]); + worst = worst.max(d.abs()); + sum += d.abs(); + } + let mean = sum / (x.len() - 1) as f64; + eprintln!("{name}: advance error vs Photoshop: mean {mean:.3} px, worst {worst:.3} px"); + assert!(worst <= worst_max && mean <= mean_max, "{name}: mean {mean}, worst {worst}"); + } +} diff --git a/crates/io/tests/text_corpus.rs b/crates/io/tests/text_corpus.rs index ce36e12c92..efa8c775a2 100644 --- a/crates/io/tests/text_corpus.rs +++ b/crates/io/tests/text_corpus.rs @@ -68,6 +68,9 @@ fn corpus_text_layers() { continue; }; let doc = imp.document; + // The document's Txt2 (kept verbatim on export) carries what EngineData can't, e.g. + // optical kerning; re-reads apply it like an import does. + let txt2 = doc.metadata.psd_global_blocks.iter().find(|b| &b.1 == b"Txt2").and_then(|b| photocraft_text::psd::parse_txt2(&b.2)); let mut all = Vec::new(); walk(&doc.layers, &mut all); for l in all { @@ -82,7 +85,10 @@ fn corpus_text_layers() { assert!(st.size_pt > 0.0 && st.postscript_name.is_some(), "{name}/{}: {st:?}", l.name); // Round trip of the model through our own TySh writer. let rebuilt = photocraft_text::psd::build_tysh(t, doc.resolution_dpi, None); - let back = photocraft_text::psd::text_layer_from_tysh(&rebuilt, doc.resolution_dpi).unwrap(); + let mut back = photocraft_text::psd::text_layer_from_tysh(&rebuilt, doc.resolution_dpi).unwrap(); + if let Some(txt2) = &txt2 { + photocraft_text::psd::apply_txt2(&mut back, &rebuilt, txt2); + } assert_eq!(back.text, t.text); assert_eq!(back.char_runs(), t.char_runs(), "{name}/{}", l.name); assert_eq!(back.paragraph_runs(), t.paragraph_runs(), "{name}/{}", l.name); diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rs index 7d0928ef5e..348b7091ae 100644 --- a/crates/text/src/layout.rs +++ b/crates/text/src/layout.rs @@ -277,11 +277,29 @@ const RLM: &str = "\u{200F}"; pub(crate) struct Layouter { lcx: LayoutContext, + optical: crate::optical::Cache, +} + +/// A cluster of a line in visual order, for kerning. +struct KernSlot { + /// Index of the run in the paragraph layout. + run: usize, + /// Byte offset in the paragraph text. + start: usize, + first: Option, + last: Option, + font: FontData, + coords: Vec, + size: f32, + rtl: bool, + blank: bool, + /// Vertical type, upright glyph (its outline doesn't run along the column). + upright: bool, } impl Layouter { pub fn new() -> Self { - Self { lcx: LayoutContext::new() } + Self { lcx: LayoutContext::new(), optical: crate::optical::Cache::default() } } pub fn layout(&mut self, fonts: &mut FontDb, t: &TextLayer, dpi: f32) -> TextLayout { @@ -484,9 +502,109 @@ impl Layouter { prev_baseline = Some(baseline); let last_line = li + 1 == nlines; let adv = m.advance - m.trailing_whitespace; + let map = |o: usize| (prange.start + o.saturating_sub(prefix.len())).min(content_end); + + // Kerning after each cluster (px), per run in visual cluster order: manual + // kerning plus optical pair kerning. Applied after shaping (like horizontal + // scale) in line space, so it moves the following glyphs, carets and the line + // extent; in vertical type that is along the column. Optical kerning measures + // horizontal outlines, so it applies to rotated (Latin) glyphs only there. + let mut slots: Vec = Vec::new(); + let mut line_runs: Vec = Vec::new(); + for item in line.items() { + let PositionedLayoutItem::GlyphRun(gr) = item else { + continue; + }; + let run = gr.run(); + if line_runs.contains(&run.index()) { + continue; + } + line_runs.push(run.index()); + for c in run.visual_clusters() { + let mut gl = c.glyphs(); + let first = gl.next().map(|g| g.id); + let last = gl.last().map(|g| g.id).or(first); + slots.push(KernSlot { + run: run.index(), + start: c.text_range().start, + first, + last, + font: run.font().clone(), + coords: run.normalized_coords().to_vec(), + size: run.font_size(), + rtl: c.is_rtl(), + blank: first.is_none() || c.is_space_or_nbsp() || c.text_range().end <= prefix.len(), + upright: vertical && c.first_style().brush.1 != VClass::Rotate as u8, + }); + } + } + let mut kern_px: Vec = vec![0.0; slots.len()]; + for j in 0..slots.len().saturating_sub(1) { + let (a, b) = (&slots[j], &slots[j + 1]); + if a.start < prefix.len() { + continue; + } + let st = &out.styles[style_at(map(a.start))]; + let next = &out.styles[style_at(map(b.start))]; + let mut units = if st.kern.is_finite() { st.kern } else { 0.0 }; + // Optical pairs: both characters optical (a mode change splits shaping + // runs, which ends automatic kerning, as with Metrics). + if st.kerning == Kerning::Optical + && next.kerning == Kerning::Optical + && st.kern == 0.0 + && next.kern == 0.0 + && !a.rtl + && !b.rtl + && !a.blank + && !b.blank + && !a.upright + && !b.upright + && a.font.data.id() == b.font.data.id() + && a.font.index == b.font.index + && a.coords == b.coords + && (a.size - b.size).abs() < 1e-3 + && let (Some(l), Some(r)) = (a.last, b.first) + { + let coords: Vec = + a.coords.iter().map(|&c| skrifa::instance::NormalizedCoord::from_bits(c)).collect(); + if let Some(k) = self.optical.pair(a.font.data.id(), a.font.data.as_ref(), a.font.index, &coords, l, r) { + units += k + crate::optical::size_adjust(st.size_pt); + } + } + kern_px[j] = units / 1000.0 * a.size; + } + let line_kern: f32 = kern_px.iter().sum(); + // Per run on the line: (run index, glyph → slot, glyphs emitted, first slot whose + // kerning isn't applied yet). + let mut cursors: Vec<(usize, Vec, usize, usize)> = Vec::new(); + for item in line.items() { + let PositionedLayoutItem::GlyphRun(gr) = item else { + continue; + }; + let run = gr.run(); + if cursors.iter().any(|c| c.0 == run.index()) { + continue; + } + let base = slots.iter().position(|s| s.run == run.index()).unwrap_or(0); + let mut g2s = Vec::new(); + for (ci, c) in run.visual_clusters().enumerate() { + g2s.extend(c.glyphs().map(|_| base + ci)); + } + cursors.push((run.index(), g2s, 0, base)); + } + // Parley aligned box lines without the kerning. + let kern_align = if is_box { + match ps.align { + TextAlign::Center => line_kern / 2.0, + TextAlign::Right => line_kern, + _ => 0.0, + } + } else { + 0.0 + }; let dx = if is_box { - let base = line_origin + indent_start; - let slack = avail.unwrap_or(0.0) - adv; + let base = line_origin + indent_start - kern_align; + let slack = avail.unwrap_or(0.0) - adv - line_kern; base + if last_line { match ps.align { TextAlign::JustifyCenter => slack * 0.5 - m.offset, @@ -498,20 +616,19 @@ impl Layouter { } } else { let target = match ps.align { - TextAlign::Center | TextAlign::JustifyCenter => -adv / 2.0, - TextAlign::Right | TextAlign::JustifyRight => -adv, + TextAlign::Center | TextAlign::JustifyCenter => -(adv + line_kern) / 2.0, + TextAlign::Right | TextAlign::JustifyRight => -(adv + line_kern), _ => indent_start, }; target - m.offset }; let justify_all = is_box && last_line && ps.align == TextAlign::JustifyAll; let line_index = out.lines.len(); - let map = |o: usize| (prange.start + o.saturating_sub(prefix.len())).min(content_end); let lr = line.text_range(); let g0 = out.glyphs.len(); let c0 = out.clusters.len(); let mut vinfo: Vec = Vec::new(); - let mut extra = 0.0f32; // horizontal-scale growth along the line + let mut extra = 0.0f32; // horizontal-scale growth and kerning along the line let mut seen_runs: Vec = Vec::new(); for item in line.items() { let PositionedLayoutItem::GlyphRun(gr) = item else { @@ -534,8 +651,9 @@ impl Layouter { if !seen_runs.contains(&run.index()) { seen_runs.push(run.index()); let mut cx = run_x0; - for c in run.visual_clusters() { - let a = c.advance() * hs; + let base = slots.iter().position(|s| s.run == run.index()).unwrap_or(0); + for (ci, c) in run.visual_clusters().enumerate() { + let a = c.advance() * hs + kern_px.get(base + ci).copied().unwrap_or(0.0); let r = c.text_range(); if r.end > prefix.len() || prefix.is_empty() { out.clusters.push(ClusterInfo { range: map(r.start)..map(r.end), x: cx, advance: a, line: line_index, rtl: c.is_rtl() }); @@ -552,7 +670,18 @@ impl Layouter { _ => Vec::new(), }; let mut pen = gr.offset(); + let mut cursor = cursors.iter_mut().find(|c| c.0 == run.index()); for g in gr.glyphs() { + // Kerning of the clusters before this glyph's cluster. + if let Some(c) = cursor.as_deref_mut() { + if let Some(&slot) = c.1.get(c.2) { + while c.3 < slot { + extra += kern_px.get(c.3).copied().unwrap_or(0.0); + c.3 += 1; + } + } + c.2 += 1; + } out.glyphs.push(PlacedGlyph { face, id: g.id, @@ -575,6 +704,16 @@ impl Layouter { extra += g.advance * (hs - 1.0); pen += g.advance; } + // The run's last glyph: its remaining clusters' kerning follows it. + if let Some(c) = cursor + && c.2 >= c.1.len() + { + let end = slots.iter().rposition(|s| s.run == c.0).map_or(c.3, |p| p + 1); + while c.3 < end { + extra += kern_px.get(c.3).copied().unwrap_or(0.0); + c.3 += 1; + } + } let run_x1 = dx + gr.offset() + gr.advance() + extra; let shift = st.baseline_shift_pt * k; if vertical { @@ -785,7 +924,9 @@ impl skrifa::outline::OutlinePen for YMax { /// OpenType feature settings of a character style (CSS `font-feature-settings` items). fn feature_list(st: &CharStyle) -> Vec { let mut feats: Vec = Vec::new(); - if st.kerning == Kerning::Off { + // Optical and manual kerning replace the font's kerning table; a character with a manual + // kern is manually kerned whatever its mode (as in Photoshop). + if st.kerning != Kerning::Metrics || st.kern != 0.0 { feats.push("\"kern\" 0".into()); } if !st.ligatures { diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index c5d4480f3f..97fdd781a5 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -19,6 +19,7 @@ pub mod engine_data; pub mod fonts; pub mod glyphs; pub mod layout; +pub mod optical; pub mod psd; pub mod psd_styles; pub mod raster; @@ -61,6 +62,58 @@ impl TextEngine { self.layouter.layout(&mut self.fonts, layer, dpi) } + /// Kerning (1/1000 em) between the character starting at byte `at` and the next one, as + /// laid out: the manual kern, or the automatic (metrics / optical) kerning of the pair. + /// `None` when there is no such pair on one line. + pub fn pair_kerning(&mut self, layer: &TextLayer, dpi: f32, at: usize) -> Option { + let runs = layer.char_runs(); + let ch = layer.text.get(at..)?.chars().next()?; + let next = at + ch.len_utf8(); + if ch == '\n' || ch == '\r' || layer.text.get(next..)?.chars().next().is_none_or(|c| c == '\n' || c == '\r') { + return None; + } + let gap = |l: &TextLayout| { + let a = l.clusters.iter().find(|c| c.range.start == at)?; + let b = l.clusters.iter().find(|c| c.range.start == next)?; + (a.line == b.line && !a.rtl).then_some(b.x - a.x) + }; + let with = gap(&self.layout(layer, dpi))?; + // Em of the pair's first character (px). + let mut acc = 0; + let style_run = runs.iter().find(|r| { + acc += r.len; + at < acc + })?; + let em = style_run.style.size_pt * if dpi > 0.0 { dpi / 72.0 } else { 1.0 }; + if em.is_nan() || em <= 0.0 { + return None; + } + // The same layer with the pair's first character unkerned. + let mut plain = layer.clone(); + plain.runs = Vec::with_capacity(runs.len() + 2); + let mut start = 0; + for r in runs { + let end = start + r.len; + let mut push = |s: usize, e: usize, unkern: bool| { + if e > s { + let mut style = r.style.clone(); + if unkern { + style.kerning = photocraft_doc::text::Kerning::Off; + style.kern = 0.0; + } + plain.runs.push(photocraft_doc::text::TextRun { len: e - s, style }); + } + }; + push(start, at.clamp(start, end), false); + push(at.clamp(start, end), next.clamp(start, end), true); + push(next.clamp(start, end), end, false); + start = end; + } + let without = gap(&self.layout(&plain, dpi))?; + let k = (with - without) / em * 1000.0; + k.is_finite().then_some(k) + } + /// Lays out and rasterizes a text layer into document space. pub fn render(&mut self, layer: &TextLayer, dpi: f32, format: PixelFormat) -> (TextLayout, Rendered) { let l = self.layout(layer, dpi); diff --git a/crates/text/src/optical.rs b/crates/text/src/optical.rs new file mode 100644 index 0000000000..1fce3b8abe --- /dev/null +++ b/crates/text/src/optical.rs @@ -0,0 +1,322 @@ +//! Optical kerning (Photoshop's Character panel › Kerning › Optical): pair spacing computed from +//! the glyph outlines instead of the font's kerning table. +//! +//! Every glyph gets a horizontal *profile*: at [`SAMPLES`] heights across the em, the distance +//! from the start of its advance to its leftmost ink and from its rightmost ink to the end of its +//! advance (em units, size independent, cached per face and glyph). For a pair, the gap at each +//! height is the right distance of the first glyph plus the left distance of the second. Two +//! features describe a pair: its closest approach and the mean white space beyond it (capped, +//! so open counters and overhangs count only so far). The kerning is a linear function of how +//! those differ from the font's own "oo" pair, plus a size term (Photoshop spaces small type +//! looser). The coefficients were fitted (least squares) to Photoshop 2026's optical kerning of +//! 94 pairs in each of Arial, Georgia, Times New Roman and Verdana at 100 pt, and the size term to +//! 12/24/48 pt; the residual is about 8/1000 em RMS for the sans and Georgia, 17 for Times (see +//! the tests). Clean-room: only Photoshop's output positions were observed. + +use std::collections::HashMap; + +use skrifa::instance::{LocationRef, NormalizedCoord, Size}; +use skrifa::outline::{DrawSettings, OutlinePen}; +use skrifa::{GlyphId, MetadataProvider}; + +/// Number of sampled heights per glyph. +pub const SAMPLES: usize = 48; +/// Sampled band, em units above the baseline. +const Y_LO: f32 = -0.25; +const Y_HI: f32 = 1.0; + +/// Left/right side distances at each sampled height (em); `None` where the glyph has no ink. +#[derive(Clone, Debug, PartialEq)] +pub struct Profile { + pub left: [Option; SAMPLES], + pub right: [Option; SAMPLES], + /// Advance width (em). + pub advance: f32, +} + +impl Profile { + /// Whether the glyph has any ink (spaces don't). + pub fn is_empty(&self) -> bool { + self.left.iter().all(Option::is_none) + } +} + +/// Height of sample `i` (em). +pub fn sample_y(i: usize) -> f32 { + Y_LO + (Y_HI - Y_LO) * (i as f32 + 0.5) / SAMPLES as f32 +} + +/// Line segments of a flattened outline (font units). +#[derive(Default)] +struct Flatten { + segs: Vec<[f32; 4]>, + start: (f32, f32), + at: (f32, f32), +} + +impl Flatten { + fn line(&mut self, x: f32, y: f32) { + self.segs.push([self.at.0, self.at.1, x, y]); + self.at = (x, y); + } +} + +impl OutlinePen for Flatten { + fn move_to(&mut self, x: f32, y: f32) { + self.start = (x, y); + self.at = (x, y); + } + fn line_to(&mut self, x: f32, y: f32) { + self.line(x, y); + } + fn quad_to(&mut self, cx: f32, cy: f32, x: f32, y: f32) { + let (x0, y0) = self.at; + for i in 1..=8 { + let t = i as f32 / 8.0; + let u = 1.0 - t; + self.line(u * u * x0 + 2.0 * u * t * cx + t * t * x, u * u * y0 + 2.0 * u * t * cy + t * t * y); + } + } + fn curve_to(&mut self, c0x: f32, c0y: f32, c1x: f32, c1y: f32, x: f32, y: f32) { + let (x0, y0) = self.at; + for i in 1..=12 { + let t = i as f32 / 12.0; + let u = 1.0 - t; + let (a, b, c, d) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t); + self.line(a * x0 + b * c0x + c * c1x + d * x, a * y0 + b * c0y + c * c1y + d * y); + } + } + fn close(&mut self) { + let (x, y) = self.start; + if (x, y) != self.at { + self.line(x, y); + } + } +} + +/// Computes the profile of glyph `gid` (`None` if the font or glyph can't be read). +pub fn profile(font: &skrifa::FontRef<'_>, coords: &[NormalizedCoord], gid: u32) -> Option { + let loc = LocationRef::new(coords); + let upem = f32::from(font.metrics(Size::unscaled(), loc).units_per_em); + if upem.is_nan() || upem <= 0.0 { + return None; + } + let advance = font.glyph_metrics(Size::unscaled(), loc).advance_width(GlyphId::new(gid)).unwrap_or(0.0) / upem; + let mut pen = Flatten::default(); + let outline = font.outline_glyphs().get(GlyphId::new(gid))?; + outline.draw(DrawSettings::unhinted(Size::unscaled(), loc), &mut pen).ok()?; + let mut left = [None; SAMPLES]; + let mut right = [None; SAMPLES]; + for i in 0..SAMPLES { + let y = sample_y(i) * upem; + let (mut lo, mut hi) = (f32::INFINITY, f32::NEG_INFINITY); + for &[x0, y0, x1, y1] in &pen.segs { + if (y0 <= y && y < y1) || (y1 <= y && y < y0) { + let x = x0 + (x1 - x0) * (y - y0) / (y1 - y0); + lo = lo.min(x); + hi = hi.max(x); + } + } + if lo.is_finite() && hi.is_finite() { + left[i] = Some(lo / upem); + right[i] = Some(advance - hi / upem); + } + } + Some(Profile { left, right, advance }) +} + +/// Band of heights used for pair gaps (em above the baseline). +const BAND: (f32, f32) = (-0.1, 1.0); +/// How far (em) beyond the closest approach a gap still counts as white space. +const DEPTH: f32 = 0.04; +/// Closest approaches above this (em) stop opening the pair further. +const NEAR: f32 = 0.1; +/// Fitted coefficients (see the module docs): intercept, closest approach, white space beyond +/// it, saturated closest approach. +const C0: f32 = -0.0033; +const C_MIN: f32 = 0.5384; +const C_EXCESS: f32 = 0.5526; +const C_NEAR: f32 = -0.1612; +/// Optical kerning never moves a pair further than this (1/1000 em). +const LIMIT: f32 = 400.0; + +/// Shape features of a pair: closest approach and mean white space beyond it (capped at +/// [`DEPTH`]), em. `None` when the glyphs share no inked height inside the band. +pub fn pair_features(l: &Profile, r: &Profile) -> Option<(f32, f32)> { + let mut gaps = [0.0f32; SAMPLES]; + let mut n = 0; + for i in 0..SAMPLES { + let y = sample_y(i); + if y < BAND.0 || y > BAND.1 { + continue; + } + if let (Some(a), Some(b)) = (l.right[i], r.left[i]) { + gaps[n] = a + b; + n += 1; + } + } + let gaps = gaps.get(..n).filter(|g| !g.is_empty())?; + let min = gaps.iter().copied().fold(f32::INFINITY, f32::min); + let excess = gaps.iter().map(|g| (g - min).min(DEPTH)).sum::() / n as f32; + Some((min, excess)) +} + +/// Photoshop's optical kerning is looser at small sizes: pairs open by about +/// 11 × ln(95 / size) thousandths of an em below 95 pt (measured at 12, 24, 48, 96 and 100 pt; +/// flat below 15 pt here, where the measurements level off). +pub fn size_adjust(size_pt: f32) -> f32 { + if !size_pt.is_finite() || size_pt <= 0.0 { + return 0.0; + } + 11.0 * (95.0 / size_pt.clamp(15.0, 95.0)).ln() +} + +/// Optical kerning (1/1000 em) of a pair with features `f`, for a font whose "oo" pair has +/// features `reference`, at 95 pt and above (see [`size_adjust`]). +pub fn kern_from_features(f: (f32, f32), reference: (f32, f32)) -> f32 { + let k = C0 + C_MIN * (reference.0 - f.0) + C_EXCESS * (reference.1 - f.1) + C_NEAR * (reference.0.min(NEAR) - f.0.min(NEAR)); + let k = k * 1000.0; + if k.is_finite() { k.clamp(-LIMIT, LIMIT) } else { 0.0 } +} + +/// Per-font cache of glyph profiles and reference features. Keyed by the caller's font id +/// (parley's blob id plus face index) and variation coordinates. +#[derive(Default)] +pub struct Cache { + profiles: HashMap<(u64, u32), Option>, + reference: HashMap>, +} + +impl Cache { + /// Entries kept before the cache is cleared (bounds memory with many fonts). + const MAX_PROFILES: usize = 50_000; + + fn font_key(font_id: u64, index: u32, coords: &[NormalizedCoord]) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + (font_id, index).hash(&mut h); + for c in coords { + c.to_bits().hash(&mut h); + } + h.finish() + } + + fn get(&mut self, fk: u64, font: &skrifa::FontRef<'_>, coords: &[NormalizedCoord], gid: u32) -> Option { + if self.profiles.len() > Self::MAX_PROFILES { + self.profiles.clear(); + } + self.profiles.entry((fk, gid)).or_insert_with(|| profile(font, coords, gid)).clone() + } + + /// Features of the font's "oo" pair (its own idea of round-to-round spacing). + fn reference(&mut self, fk: u64, font: &skrifa::FontRef<'_>, coords: &[NormalizedCoord]) -> Option<(f32, f32)> { + if let Some(r) = self.reference.get(&fk) { + return *r; + } + let r = ['o', 'O', 'n', 'H'].iter().find_map(|&c| { + let g = font.charmap().map(c)?; + let p = self.get(fk, font, coords, g.to_u32())?; + pair_features(&p, &p) + }); + self.reference.insert(fk, r); + r + } + + /// Optical kerning of glyphs `left`, `right` of one face in 1/1000 em, or `None` when it + /// doesn't apply (spaces, glyphs that share no inked height, unreadable fonts). + pub fn pair(&mut self, font_id: u64, data: &[u8], index: u32, coords: &[NormalizedCoord], left: u32, right: u32) -> Option { + let font = skrifa::FontRef::from_index(data, index).ok()?; + let fk = Self::font_key(font_id, index, coords); + let l = self.get(fk, &font, coords, left)?; + let r = self.get(fk, &font, coords, right)?; + let f = pair_features(&l, &r)?; + Some(kern_from_features(f, self.reference(fk, &font, coords)?)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Text set in Photoshop 2026 at 100 pt with Kerning › Optical; `PHOTOSHOP` holds the + /// kerning Photoshop applied after each character (1/1000 em: optical positions minus the + /// unkerned ones, read from the saved PSDs). + const SAMPLE: &str = "HHOOHnnoonvAVTAToTyLTWaYoPAFAVaxyrnilHIOHnHoxoxcdbpkkwwAAVVff1471LYTeKOnHeHaHsHtHgHzHkHxHvHwrst"; + #[rustfmt::skip] + const PHOTOSHOP: [(&str, &str, &[i32]); 4] = [ + ("ArialMT", "/System/Library/Fonts/Supplemental/Arial.ttf", &[-16, -19, -20, -17, -20, -27, -22, -17, -20, -44, -52, -84, 15, -108, -110, -118, -122, -123, -18, -130, 14, -50, -111, -117, -15, -84, -12, -86, -84, -73, -6, 1, -19, -7, -27, -24, -23, -24, -22, -17, -20, -23, -15, -44, -42, -44, -45, -6, -28, -23, -23, -17, 7, 16, -40, 34, -84, 26, -23, 15, -34, -76, -74, -88, -69, -141, 15, -120, -16, -67, -13, -23, -17, -19, -18, -22, -19, -21, -20, -20, -16, -21, -28, -29, -21, -19, -22, -22, -24, -24, -19, -18, -11, -28]), + ("Georgia", "/System/Library/Fonts/Supplemental/Georgia.ttf", &[-28, -51, -18, -53, -24, -12, -38, -17, -37, -36, -79, -110, 33, -44, -42, -115, -115, -113, -46, -61, 22, -84, -62, -102, -50, -109, 15, -91, -110, -95, 11, 16, 13, 2, -12, -6, -17, -23, -53, -53, -24, -16, -50, -41, -40, -41, -37, -10, -28, -38, -37, 12, 7, 31, -63, 49, -110, 47, -39, 13, -14, -6, -58, -10, -20, -59, 36, -110, -40, -51, -19, -16, -50, -39, -33, -10, -26, -40, -58, -10, -37, -15, -18, -18, -18, 2, -7, -6, -49, -46, -53, 13, -11, -19]), + ("TimesNewRomanPSMT", "/System/Library/Fonts/Supplemental/Times New Roman.ttf", &[3, -38, -17, -38, 4, 11, -26, -16, -24, -36, -96, -159, -3, -70, -74, -114, -117, -122, -44, -105, 0, -93, -114, -137, -36, -120, 8, -117, -159, -104, -5, -14, 10, 22, -1, -13, 2, -1, -40, -38, 4, 10, -38, -47, -49, -47, -46, -15, -17, -16, -29, 20, 8, 12, -91, 13, -159, 12, -79, -2, 14, -98, -65, -71, -55, -133, -4, -116, -29, -67, -3, 10, -36, -29, -22, -4, -16, -28, -42, -5, -35, -1, 0, -3, 7, 16, 2, 6, -46, -44, -44, 8, -7, -22]), + ("Verdana", "/System/Library/Fonts/Supplemental/Verdana.ttf", &[-33, -26, -21, -27, -35, -35, -27, -20, -28, -52, -66, -82, 33, -93, -93, -120, -120, -121, -32, -102, 16, -67, -103, -104, -26, -72, -20, -70, -82, -59, -27, 4, -30, -12, -37, -37, -35, -47, -56, -27, -35, -33, -26, -53, -53, -53, -57, -16, -37, -28, -30, -19, 0, -10, -49, 26, -82, 26, -20, 17, -38, -98, -87, -69, -72, -102, 38, -120, -22, -78, -24, -33, -28, -20, -33, -33, -23, -22, -29, -27, -28, -35, -41, -37, -37, -19, -32, -32, -32, -32, -38, -37, 5, -15]), + ]; + + /// The fitted model against Photoshop's optical kerning (macOS system fonts; skipped where + /// they aren't installed). Reports the error per font; fails if it regresses. + #[test] + fn optical_matches_photoshop() { + let chars: Vec = SAMPLE.chars().collect(); + let (mut sum, mut n) = (0.0f64, 0usize); + for (name, path, ps) in PHOTOSHOP { + let Ok(data) = std::fs::read(path) else { continue }; + let mut cache = Cache::default(); + let font = skrifa::FontRef::from_index(&data, 0).unwrap(); + let (mut fs, mut fneg, mut fpos) = (0.0f64, 0usize, 0usize); + for (i, w) in chars.windows(2).enumerate() { + let g = |c: char| font.charmap().map(c).unwrap().to_u32(); + let ours = cache.pair(1, &data, 0, &[], g(w[0]), g(w[1])).unwrap_or(0.0); + let theirs = ps[i] as f32; + fs += f64::from((ours - theirs).powi(2)); + // Direction agreement on clear cases. + if theirs < -40.0 && ours > 0.0 { + fneg += 1; + } + if theirs > 15.0 && ours < -15.0 { + fpos += 1; + } + } + let rms = (fs / ps.len() as f64).sqrt(); + eprintln!("{name}: rms {rms:.1}/1000 em over {} pairs", ps.len()); + assert!(rms < 20.0, "{name}: rms {rms}"); + // Times' serif stems (kH, kk) are opened by Photoshop and tightened slightly here. + assert!(fneg + fpos <= 2, "{name}: {} pairs kerned the wrong way", fneg + fpos); + sum += fs; + n += ps.len(); + } + if n > 0 { + let rms = (sum / n as f64).sqrt(); + eprintln!("all: rms {rms:.1}/1000 em"); + assert!(rms < 12.5, "rms {rms}"); + } + } + + /// Writes glyph profiles of macOS system fonts as JSON for fitting [`PARAMS`] + /// (`OPTICAL_DUMP= cargo test -p photocraft-text optical_dump -- --ignored`). + #[test] + #[ignore] + fn optical_dump() { + let Some(out) = std::env::var_os("OPTICAL_DUMP") else { return }; + let chars = "HOnovAVTyLWaYPFxrilIcdbpkwf1475eKsgzt"; + let mut json = String::from("{"); + for (name, path) in [ + ("ArialMT", "/System/Library/Fonts/Supplemental/Arial.ttf"), + ("Georgia", "/System/Library/Fonts/Supplemental/Georgia.ttf"), + ("TimesNewRomanPSMT", "/System/Library/Fonts/Supplemental/Times New Roman.ttf"), + ("Verdana", "/System/Library/Fonts/Supplemental/Verdana.ttf"), + ] { + let data = std::fs::read(path).unwrap(); + let font = skrifa::FontRef::from_index(&data, 0).unwrap(); + json.push_str(&format!("\"{name}\":{{")); + for c in chars.chars() { + let g = font.charmap().map(c).unwrap().to_u32(); + let p = profile(&font, &[], g).unwrap(); + let f = |v: &[Option]| v.iter().map(|x| x.map_or("null".to_string(), |x| format!("{x:.5}"))).collect::>().join(","); + json.push_str(&format!("\"{c}\":{{\"adv\":{},\"l\":[{}],\"r\":[{}]}},", p.advance, f(&p.left), f(&p.right))); + } + json.pop(); + json.push_str("},"); + } + json.pop(); + json.push('}'); + std::fs::write(out, json).unwrap(); + } +} diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rs index 8a188c550f..1f32fbefa8 100644 --- a/crates/text/src/psd.rs +++ b/crates/text/src/psd.rs @@ -104,6 +104,83 @@ pub fn engine_data(text: &Descriptor) -> Option { } } +/// Parses the document's `Txt2` block (Photoshop's text engine data for all type layers). +pub fn parse_txt2(data: &[u8]) -> Option { + // Txt2 is a bare sequence of `/key value` pairs (no enclosing `<< >>`). + let mut wrapped = Vec::with_capacity(data.len() + 4); + wrapped.extend_from_slice(b"<<"); + wrapped.extend_from_slice(data); + wrapped.extend_from_slice(b">>"); + ed::parse(&wrapped).ok().filter(|v| matches!(v, E::Dict(_))) +} + +/// Applies the automatic kerning modes that Photoshop keeps only in `Txt2` (EngineData writes +/// Optical and "0" alike as `AutoKerning true`) to `layer`, read from `tysh`. Only when the +/// layer's `TextIndex` entry still holds the layer's text (an edit elsewhere leaves `Txt2` +/// stale). +/// +/// Observed layout: `Txt2 /1` is the list of text objects; an object's `/0` is its text model +/// with `/0` the text (`\r` breaks, trailing `\r`) and `/6 /0` its style runs, each +/// `<< /0 << /0 << … /6 <