diff --git a/crates/engine/src/cmd/retype.rs b/crates/engine/src/cmd/retype.rs index 0e31d5632a..5d6c8ab9c4 100644 --- a/crates/engine/src/cmd/retype.rs +++ b/crates/engine/src/cmd/retype.rs @@ -611,6 +611,9 @@ fn first_mismatch(t: &TextObject, matches: &[GlyphMatch], size: f64) -> Option, system_layout: bool, unmatched: &mut Vec) // leave glyphs unchecked; past it they keep no tracking. Each measure takes the glyphs to // rebuild the rest and the characters it lays out (see [`Line::tracked`]); the budget covers // one measure of each whole line, as `retype` gives the first, and the glyph checks' budget. - // The glyphs measured again after splits are limited apart from it, at six times each rest's - // glyphs from half as much again as the glyph checks' budget. + // The glyphs measured again after splits are limited apart from it, at six times the glyphs + // each measure reads from half as much again as the glyph checks' budget. let mut remeasured = budget.saturating_add(budget / 2); let mut track_budget = lines.iter().map(|l| l.text.chars().count().saturating_add(l.pieces.len().saturating_mul(394))).fold(budget, usize::saturating_add); @@ -671,20 +674,47 @@ fn verified(lines: Vec, system_layout: bool, unmatched: &mut Vec) // rest takes the tracking its own glyphs are spaced by, while the budget for // measuring it lasts and when type draws its first glyph as it was, followed by the // next two (else that glyph splits off first, and the tracking is measured from the - // next one); past it, an untracked line's rest keeps its text as it is. + // next one); past it, an untracked line's rest keeps its text as it is. The tracking + // is measured from the rest's first [`MEASURE_SPAN`] glyphs at most, and the glyphs + // after them follow at that tracking, so each split costs the same however long the + // rest is, and splits at irregular gaps early in a line leave the budget for a long + // evenly tracked run after them. Some(k) => { let k = k.max(1); - // A measure takes at least six times the rest's glyphs from each budget. - let measurable = n - k >= 3 && remeasured >= (n - k).saturating_mul(6) && track_budget >= (n - k).saturating_mul(6); + let span = (n - k).min(MEASURE_SPAN); + // A measure takes at least six times the glyphs it reads from each budget. + let measurable = n - k >= 3 && remeasured >= span.saturating_mul(6) && track_budget >= span.saturating_mul(6); let first_drawn = measurable && { budget = budget.saturating_sub(3); l.respaced(k, k + 3, l.tracking).is_some_and(|p| p.first_mismatch(&p.probe(system_layout)) != Some(0)) }; let rest = if first_drawn { - // Rebuilding the rest for the measure takes its glyphs from the budget. - remeasured = remeasured.saturating_sub((n - k).saturating_mul(6)); - track_budget = track_budget.saturating_sub(n - k); - l.respaced(k, n, 0.0).map(|r| r.tracked(system_layout, &mut track_budget)) + // Rebuilding the glyphs measured takes them from the budget. + remeasured = remeasured.saturating_sub(span.saturating_mul(6)); + track_budget = track_budget.saturating_sub(span); + l.respaced(k, k + span, 0.0).map(|r| { + let mut r = r.tracked(system_layout, &mut track_budget); + // Tracking that leaves a space under half its advance: the gaps alone + // do not show the spaces past the glyphs measured (only the kerning a + // space breaks may), so the whole rest is measured while the budget + // lasts. + let more = n - k - span; + let space = space_advance(&r.face) * r.scale; + if more > 0 + && (space + r.tracking_pt()).abs() < space / 2.0 + && remeasured >= more.saturating_mul(6) + && track_budget >= more.saturating_mul(6) + && let Some(whole) = l.respaced(k, n, 0.0) + { + remeasured = remeasured.saturating_sub(more.saturating_mul(6)); + track_budget = track_budget.saturating_sub(more); + return whole.tracked(system_layout, &mut track_budget); + } + for (id, m) in l.pieces.iter().zip(&l.matches).skip(k + span) { + r.push(*id, m); + } + r + }) } else if l.tracking == 0.0 { l.part(k, n) } else { diff --git a/crates/engine/src/cmd/retype_tests.rs b/crates/engine/src/cmd/retype_tests.rs index 80361f1e0a..627878a3c2 100644 --- a/crates/engine/src/cmd/retype_tests.rs +++ b/crates/engine/src/cmd/retype_tests.rs @@ -1002,6 +1002,59 @@ fn glyphs_type_cannot_draw_do_not_use_up_the_tracking_measure() { assert_eq!((last.pieces.len(), last.tracking), (95, 300.0), "{:?}", out.iter().map(|l| (l.pieces.len(), l.tracking)).collect::>()); } +#[test] +fn irregular_gaps_before_a_long_tracked_run_do_not_split_the_run() { + // The first eight gaps each take another tracking, and the 92 glyphs after them are tracked + // 300: measuring the rest after each irregular gap must not use up the budget the run needs, + // so the run is one object with its tracking. + let mut s = session(); + let id = s.execute("text.create", &json!({"x": 10, "y": 100, "text": "i".repeat(100), "size": 6})).unwrap()["id"].as_u64().unwrap(); + s.execute("text.setRangeStyle", &json!({"id": id, "tracking": 300})).unwrap(); + for (i, t) in [50, 170, 90, 230, 130, 410, 20, 520].into_iter().enumerate() { + s.execute("text.setRangeStyle", &json!({"id": id, "start": i, "end": i + 1, "tracking": t})).unwrap(); + } + s.execute("select.set", &json!({"ids": [id]})).unwrap(); + let g = s.execute("type.createOutlines", &json!({})).unwrap()["ids"][0].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [g]})).unwrap(); + let r = s.execute("type.retype", &json!({})).unwrap(); + let texts: Vec<&str> = r["texts"].as_array().unwrap().iter().map(|t| t.as_str().unwrap()).collect(); + assert_eq!(texts.concat(), "i".repeat(100), "{r}"); + assert_eq!(r["unmatched"], json!([])); + assert!(texts.len() <= 9, "at most one object per irregular gap, then the run: {texts:?}"); + let run = text_of(&s, r["ids"].as_array().unwrap().last().unwrap().as_u64().unwrap()); + assert_eq!(run.plain_text().chars().count(), 92, "{texts:?}"); + assert!(run.runs.iter().all(|r| r.style.tracking == 300.0), "{texts:?}"); +} + +#[test] +fn a_long_tightly_tracked_rest_keeps_its_word_spaces_after_a_split() { + // Tracking as tight as a space is wide leaves the space no advance, and only the kerning it + // breaks shows it is there: the word space past the glyphs the rest's tracking is measured + // from is read by that measure too. + let mut s = session(); + let text = format!("{} {}", "AV".repeat(20), "AV".repeat(5)); + let id = s.execute("text.create", &json!({"x": 10, "y": 100, "text": text, "size": 10})).unwrap()["id"].as_u64().unwrap(); + let face = text_of(&s, id).runs[0].style.font_family.clone(); + let probe = s.execute("text.create", &json!({"x": 10, "y": 200, "text": "AV", "size": 10})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [probe]})).unwrap(); + let pg = s.execute("type.createOutlines", &json!({})).unwrap()["ids"][0].as_u64().unwrap(); + let pl = lines_in(&s, pg).into_iter().next().unwrap(); + assert_eq!(pl.face.family, face); + let tracking = -(super::space_advance(&pl.face) / pl.face.units_per_em() * 1000.0).round(); + s.execute("text.setRangeStyle", &json!({"id": id, "tracking": tracking})).unwrap(); + s.execute("select.set", &json!({"ids": [id]})).unwrap(); + let g = s.execute("type.createOutlines", &json!({})).unwrap()["ids"][0].as_u64().unwrap(); + let mut budget = 100_000; + let mut l = lines_in(&s, g).into_iter().next().unwrap().tracked(false, &mut budget); + assert_eq!((l.text.as_str(), l.tracking), (text.as_str(), tracking)); + l.matches[0].gid += 1; + let mut unmatched = vec![]; + let out = super::verified(vec![l], false, &mut unmatched); + assert_eq!(unmatched.len(), 1); + let texts: Vec<(&str, f64)> = out.iter().map(|l| (l.text.as_str(), l.tracking)).collect(); + assert_eq!(texts, vec![(&text[1..], tracking)]); +} + #[test] fn tightly_tracked_type_keeps_its_tracking() { // Tracking tighter than a space: the space moves the next glyph back.