@@ -2905,13 +2905,92 @@ fn placeholder_groups(glyphs: &[&Glyph], body: f32) -> Vec<Vec<u32>> {
29052905 absorb_math_accents(glyphs, body, &mut groups);
29062906 fuse_script_groups(glyphs, body, &mut groups);
29072907 union_shared_members(&mut groups);
2908+ absorb_interposed_digits(glyphs, body, &mut groups);
29082909 // Every caller sees the same holes. The unholed-math check used to see
29092910 // `g_t(` without its `ℓ)` and kept the sentence in English.
29102911 let mut groups = close_open_brackets(glyphs, groups, body);
29112912 sort_placeholder_groups(glyphs, body, &mut groups);
29122913 groups
29132914}
29142915
2916+ /// `−6/ + 12`: `−` and `/` are math glyphs and `6` is a prose digit set
2917+ /// between them. A hole of only `−…/` drew the `/` before the `6` and the
2918+ /// `6` lost its sign. Digits on the hole's baseline between two members
2919+ /// belong to the hole.
2920+ fn absorb_interposed_digits(glyphs: &[&Glyph], body: f32, groups: &mut [Vec<u32>]) {
2921+ let by_id = glyph_index_refs(glyphs);
2922+ let members: HashSet<u32> = groups.iter().flatten().copied().collect();
2923+ for group in groups.iter_mut() {
2924+ let mut items: Vec<&Glyph> = group
2925+ .iter()
2926+ .filter_map(|id| by_id.get(id).map(|index| glyphs[*index]))
2927+ .collect();
2928+ if items.len() < 2 {
2929+ continue;
2930+ }
2931+ let y = items[0].matrix[5];
2932+ if !items
2933+ .iter()
2934+ .all(|glyph| (glyph.matrix[5] - y).abs() < body * 0.1)
2935+ {
2936+ continue;
2937+ }
2938+ items.sort_by(|a, b| a.matrix[4].total_cmp(&b.matrix[4]));
2939+ let mut add = Vec::new();
2940+ for pair in items.windows(2) {
2941+ let (left, right) = (glyph_ink_right(pair[0]), pair[1].matrix[4]);
2942+ let between: Vec<&Glyph> = glyphs
2943+ .iter()
2944+ .copied()
2945+ .filter(|glyph| {
2946+ !members.contains(&glyph.id)
2947+ && !glyph.unicode.trim().is_empty()
2948+ && (glyph.matrix[5] - y).abs() < body * 0.1
2949+ && glyph.matrix[4] >= left - 0.3
2950+ && glyph_ink_right(glyph) <= right + 0.3
2951+ })
2952+ .collect();
2953+ let digits = !between.is_empty()
2954+ && between.iter().all(|glyph| {
2955+ glyph
2956+ .unicode
2957+ .trim()
2958+ .chars()
2959+ .all(|ch| ch.is_ascii_digit() || ch == '.')
2960+ })
2961+ && between
2962+ .iter()
2963+ .any(|glyph| glyph.unicode.chars().any(|ch| ch.is_ascii_digit()));
2964+ if digits {
2965+ add.extend(between.iter().map(|glyph| glyph.id));
2966+ }
2967+ }
2968+ // `−1.377`: the decimal point is the last member. The digits that
2969+ // touch it finish the number.
2970+ let last = items[items.len() - 1];
2971+ if matches!(last.unicode.trim(), "." | "," | "−" | "+" | "/") {
2972+ let mut edge = glyph_ink_right(last);
2973+ loop {
2974+ let next = glyphs.iter().copied().find(|glyph| {
2975+ !members.contains(&glyph.id)
2976+ && !add.contains(&glyph.id)
2977+ && (glyph.matrix[5] - y).abs() < body * 0.1
2978+ && glyph.matrix[4] >= edge - 0.5
2979+ && glyph.matrix[4] <= edge + body * 0.15
2980+ && glyph.unicode.trim().chars().count() == 1
2981+ && glyph.unicode.trim().chars().all(|ch| ch.is_ascii_digit())
2982+ });
2983+ let Some(next) = next else {
2984+ break;
2985+ };
2986+ edge = glyph_ink_right(next);
2987+ add.push(next.id);
2988+ }
2989+ }
2990+ group.extend(add);
2991+ }
2992+ }
2993+
29152994/// A superscript that formed its own hole (`W` then `Q`) is drawn beside the
29162995/// base instead of on it. Fold a script-only group into the base it sits on.
29172996fn fuse_script_groups(glyphs: &[&Glyph], body: f32, groups: &mut Vec<Vec<u32>>) {
@@ -7916,10 +7995,16 @@ fn clear_seated_formula_overlaps(
79167995 continue;
79177996 };
79187997 let body = lines[line_index].size.max(member_size).max(1.0);
7919- if let Some(hole_x) = nearest_hole_origin(&lines[line_index], ink_left, body) {
7998+ let hole = nearest_hole_origin(&lines[line_index], ink_left, body)
7999+ .or_else(|| trailing_hole_origin(&lines[line_index], ink_left, ink_right, body));
8000+ if let Some(hole_x) = hole {
79208001 let key = (shifts[index].page, (hole_x * 4.0).round() as i32);
79218002 if claimed.insert(key) {
7922- let original_left = ink_left - shifts[index].dx;
8003+ // Seat the formula's own ink. A tail picked up beside it
8004+ // must not drag the snap off the hole.
8005+ let seated_left =
8006+ seated_ink_span(&shifts[index], extraction).map_or(ink_left, |(left, _)| left);
8007+ let original_left = seated_left - shifts[index].dx;
79238008 let new_dx = hole_x - original_left;
79248009 if (new_dx - shifts[index].dx).abs() > 0.5 {
79258010 shifts[index].dx = new_dx;
@@ -10189,6 +10274,28 @@ fn text_runs_through_ink(line: &Drawn, ink_left: f32, ink_right: f32) -> bool {
1018910274 false
1019010275}
1019110276
10277+ /// `对上的 −6/`: the hole sat at the end of the slice and reflow left only
10278+ /// the word space. The formula belongs one space after the last character,
10279+ /// not on top of it.
10280+ fn trailing_hole_origin(line: &Drawn, ink_left: f32, ink_right: f32, body: f32) -> Option<f32> {
10281+ let chars: Vec<char> = line.text.chars().collect();
10282+ if line.widths.len() != chars.len() || !chars.last().is_some_and(|c| c.is_whitespace()) {
10283+ return None;
10284+ }
10285+ let last = chars.iter().rposition(|c| !c.is_whitespace())?;
10286+ if !is_han_char(chars[last]) {
10287+ return None;
10288+ }
10289+ let mut end = line.x;
10290+ for index in 0..=last {
10291+ end += line.widths[index];
10292+ if index < last {
10293+ end += line.gaps.get(index).copied().unwrap_or(0.0);
10294+ }
10295+ }
10296+ (end > ink_left && end < ink_right && end - ink_left <= body * 3.0).then_some(end + body * 0.25)
10297+ }
10298+
1019210299fn nearest_hole_origin(line: &Drawn, ink_left: f32, body: f32) -> Option<f32> {
1019310300 let chars: Vec<char> = line.text.chars().collect();
1019410301 if line.widths.len() != chars.len() {
@@ -10417,6 +10524,11 @@ fn extend_ink_across_tail_except(
1041710524 if left < origin_left - cap || right > origin_right + cap {
1041810525 continue;
1041910526 }
10527+ // A tail trails the formula (`1},`). A standing glyph left of
10528+ // the seated ink is the sentence it was seated after.
10529+ if right < ink_left + 0.2 {
10530+ continue;
10531+ }
1042010532 let next_left = ink_left.min(left);
1042110533 let next_right = ink_right.max(right);
1042210534 if next_right - next_left > ink_right - ink_left + 0.2 {
@@ -16342,7 +16454,9 @@ fn layout_segment(
1634216454 // A justified body column also shares a right edge. Sliding a short body
1634316455 // line onto it (`“来自一线的 LLM…` at x=270) opens a hole the column
1634416456 // closer cannot cross, because the line no longer shares a left edge.
16345- let align = if bilingual {
16457+ // A numbered item hangs its words on one left edge. `3. The mural … and`
16458+ // over `north = … northwest.` can look centered by coincidence.
16459+ let align = if bilingual || numbered.is_some() {
1634616460 crate::overlap::BlockAlign::Start
1634716461 } else {
1634816462 source_block_align(&ink, media, extraction)
@@ -37802,6 +37916,63 @@ q\n1 0 0 1 501.7 120.182 cm\n[]0 d 0 J 0.398 w 0 0 m 0 6.725 l S\nQ\n";
3780237916 assert!((101.0..104.0).contains(&kept), "{kept}");
3780337917 }
3780437918
37919+ #[test]
37920+ fn digits_between_math_operators_join_the_placeholder() {
37921+ // `−6/`: the minus and slash are math font, the 6 is prose digits.
37922+ let mut minus = ink_glyph(1, 100.0, 400.0, 10.0, "−");
37923+ minus.font_name = "CMSY10".into();
37924+ let six = ink_glyph(2, 105.0, 400.0, 10.0, "6");
37925+ let mut slash = ink_glyph(3, 110.0, 400.0, 10.0, "/");
37926+ slash.font_name = "CMMI10".into();
37927+ let one = ink_glyph(4, 115.0, 400.0, 10.0, "1");
37928+ let far = ink_glyph(5, 140.0, 400.0, 10.0, "2");
37929+ let glyphs = [minus, six, slash, one, far];
37930+ let refs: Vec<&Glyph> = glyphs.iter().collect();
37931+ let mut groups = vec![vec![1, 3]];
37932+ absorb_interposed_digits(&refs, 10.0, &mut groups);
37933+ let mut group = groups[0].clone();
37934+ group.sort_unstable();
37935+ assert_eq!(group, vec![1, 2, 3, 4]);
37936+ }
37937+
37938+ #[test]
37939+ fn a_standing_glyph_left_of_the_seat_is_not_formula_tail() {
37940+ let mut minus = ink_glyph(1, 200.0, 400.0, 10.0, "−");
37941+ minus.font_name = "CMSY10".into();
37942+ minus.bbox = [200.0, 398.0, 208.0, 406.0];
37943+ let mut left = ink_glyph(2, 190.0, 400.0, 10.0, ")");
37944+ left.unmapped = true;
37945+ left.bbox = [190.0, 398.0, 199.5, 406.0];
37946+ let extraction = Extraction {
37947+ coordinate_space: "user",
37948+ pages: Vec::new(),
37949+ glyphs: vec![minus, left],
37950+ regions: Vec::new(),
37951+ diagnostics: Vec::new(),
37952+ };
37953+ let shift = ListingShift {
37954+ page: 0,
37955+ ids: vec![1],
37956+ dx: 0.0,
37957+ dy: 0.0,
37958+ host_y: 400.0,
37959+ };
37960+ let (ink_left, _) = extend_ink_across_formula_tail(200.0, 208.0, &shift, &extraction);
37961+ assert!((ink_left - 200.0).abs() < 0.01, "{ink_left}");
37962+ }
37963+
37964+ #[test]
37965+ fn a_hole_trimmed_at_the_slice_end_seats_after_the_last_character() {
37966+ let mut line = han_line(100.0, 400.0, "对上的 ");
37967+ line.widths[3] = 2.5;
37968+ // The seated ink starts on top of 的 (120..130).
37969+ let hole = trailing_hole_origin(&line, 124.0, 150.0, 10.0).unwrap();
37970+ assert!((hole - 132.5).abs() < 0.01, "{hole}");
37971+ assert!(
37972+ trailing_hole_origin(&han_line(100.0, 400.0, "对上的"), 124.0, 150.0, 10.0).is_none()
37973+ );
37974+ }
37975+
3780537976 #[test]
3780637977 fn a_formula_hole_reserves_the_seated_ink() {
3780737978 let mut formula = ink_glyph(1, 90.0, 400.0, 10.0, "∩");
0 commit comments