Retype: a long run after a split keeps its tracking fraction and its spaces

The tracking measured over the first glyphs of a rest is read as a whole number, or as none under one unit, only when the difference moves the last glyph of the rest by no more than the tolerance. A rest whose tracking gives a space a negative advance is measured whole, as one with a small advance is. The whole-rest measure takes the budget for every glyph it reads.
This commit is contained in:
dotCooCoo
2026-11-19 17:43:38 -08:00
parent 1f5cee040e
commit 3e8e29891e
2 changed files with 100 additions and 8 deletions
+26 -8
View File
@@ -400,6 +400,14 @@ impl Line {
/// its characters, spaces included, from `budget` before it runs; without enough left, the
/// line stays as it is, or becomes the best tracked reading laid out before the budget ran out.
fn tracked(self, system_layout: bool, budget: &mut usize) -> Line {
let reach = self.pieces.len();
self.tracked_reaching(system_layout, budget, reach)
}
/// [`Line::tracked`] for a line whose tracking is applied to `reach` glyphs: a measured tracking
/// within 0.05 of a whole number is read as that number only when the difference moves the
/// glyph `reach` places on by no more than the tolerance [`first_mismatch`] allows.
fn tracked_reaching(self, system_layout: bool, budget: &mut usize, reach: usize) -> Line {
let size = self.size();
let n = self.pieces.len();
if n < 3 || size <= 0.0 {
@@ -506,7 +514,12 @@ impl Line {
let mut tries = vec![];
for seed in [lower, median(extra, true), spaced_seed].into_iter().flatten() {
let Some(measured) = measure(seed) else { continue };
for t in [round_tracking(measured), measured] {
let rounded = round_tracking(measured);
let drift = (rounded - measured).abs() / 1000.0 * size * reach.saturating_sub(1) as f64;
for t in [rounded, measured] {
if t != measured && drift > (size * 0.01).max(0.01) {
continue;
}
if !tries.contains(&t) {
tries.push(t);
}
@@ -554,7 +567,12 @@ impl Line {
match (tracked.first_mismatch(&tracked.probe(system_layout)), plain_miss) {
// Under a unit of tracking, or a word space the tracked reading drops, where
// untracked type draws the line: no tracking.
(None, None) if t.abs() < 1.0 || drops_a_word_space => return self,
(None, None)
if (t.abs() < 1.0 && t.abs() / 1000.0 * size * reach.saturating_sub(1) as f64 <= (size * 0.01).max(0.01))
|| drops_a_word_space =>
{
return self;
}
(None, _) => return tracked,
(Some(k), Some(p)) if k > p && best.as_ref().is_none_or(|(b, _)| k > *b) => best = Some((k, tracked)),
_ => {}
@@ -693,7 +711,7 @@ fn verified(lines: Vec<Line>, system_layout: bool, unmatched: &mut Vec<NodeId>)
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);
let mut r = r.tracked_reaching(system_layout, &mut track_budget, n - k);
// 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
@@ -701,13 +719,13 @@ fn verified(lines: Vec<Line>, system_layout: bool, unmatched: &mut Vec<NodeId>)
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)
&& space + r.tracking_pt() < space / 2.0
&& remeasured >= (n - k).saturating_mul(6)
&& track_budget >= (n - k).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);
remeasured = remeasured.saturating_sub((n - k).saturating_mul(6));
track_budget = track_budget.saturating_sub(n - k);
return whole.tracked(system_layout, &mut track_budget);
}
for (id, m) in l.pieces.iter().zip(&l.matches).skip(k + span) {
+74
View File
@@ -1026,6 +1026,80 @@ fn irregular_gaps_before_a_long_tracked_run_do_not_split_the_run() {
assert!(run.runs.iter().all(|r| r.style.tracking == 300.0), "{texts:?}");
}
#[test]
fn a_long_run_after_a_split_keeps_the_fraction_of_its_tracking() {
// Tracking 300.04 reads as 300 over the 32 glyphs measured, but 400 glyphs apart the two
// differ by more than the tolerance: the run after the irregular gaps keeps 300.04 and stays
// one object.
let mut s = session();
let id = s.execute("text.create", &json!({"x": 10, "y": 100, "text": "i".repeat(400), "size": 6})).unwrap()["id"].as_u64().unwrap();
s.execute("text.setRangeStyle", &json!({"id": id, "tracking": 300.04})).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(400), "{r}");
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(), 392, "{texts:?}");
assert!(run.runs.iter().all(|r| near(r.style.tracking, 300.04, 1e-3)), "{texts:?}");
}
#[test]
fn a_long_run_after_a_split_keeps_a_tracking_under_one_unit_that_adds_up() {
// Tracking 0.2 moves none of the 32 glyphs measured by more than the tolerance, but moves the
// glyphs 399 places on: the rest after a split keeps it and stays one object.
let mut s = session();
let id = s.execute("text.create", &json!({"x": 10, "y": 100, "text": "M".repeat(400), "size": 6})).unwrap()["id"].as_u64().unwrap();
s.execute("text.setRangeStyle", &json!({"id": id, "tracking": 0.2})).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!(near(l.tracking, 0.2, 0.01), "{}", l.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 last = out.last().unwrap();
assert!(last.pieces.len() == 399 && near(last.tracking, 0.2, 0.01), "{:?}", out.iter().map(|l| (l.pieces.len(), l.tracking)).collect::<Vec<_>>());
}
#[test]
fn a_long_rest_tracked_past_a_negative_space_advance_keeps_its_word_spaces_after_a_split() {
// Tracking tighter than the space is wide, by one half to one and a half spaces more, gives the
// space a negative advance; the word space past the glyphs the tracking is measured from is
// still read.
for (unit, times) in
[("AV", 1.5), ("AV", 1.7), ("TA", 1.5), ("TA", 1.7), ("To", 1.5), ("To", 1.7), ("MM", 1.5), ("MM", 1.7), ("Wo", 1.6), ("Wo", 1.8)]
{
let mut s = session();
let text = format!("{} {}", unit.repeat(20), unit.repeat(5));
let id = s.execute("text.create", &json!({"x": 10, "y": 100, "text": text, "size": 10})).unwrap()["id"].as_u64().unwrap();
let probe = s.execute("text.create", &json!({"x": 10, "y": 200, "text": unit, "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();
let tracking = -(super::space_advance(&pl.face) / pl.face.units_per_em() * 1000.0 * times).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), "{times}");
l.matches[0].gid += 1;
let mut unmatched = vec![];
let out = super::verified(vec![l], false, &mut unmatched);
assert_eq!(unmatched.len(), 1, "{times}");
let texts: Vec<(&str, f64)> = out.iter().map(|l| (l.text.as_str(), l.tracking)).collect();
assert_eq!(texts, vec![(&text[1..], tracking)], "{times}");
}
}
#[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