🐛 Stop Western digits from overlapping inside a warichu note

A warichu sub-line stacked its upright glyphs by their horizontal
advance. A half-width digit is taller than it is wide, so `39` in a
note drew its two digits on top of each other. Kanji numerals are a full
em wide and hid the problem. Tate-chu-yoko never applied inside a note,
so `digits2` made no difference.

Each note glyph now advances by its upright flow advance, the rule
upright cells of the main text already used: the font's `vmtx` advance,
or else its horizontal advance floored to an em for Latin and digits. The
note's extent and its painter share the rule through
`ShapedRun::upright_cluster_advance`.

Tate-chu-yoko stays off inside a note: its digits stand one by one, as
kanji numerals do. A combined pair would leave each digit a quarter em
wide in a half-size sub-line, and could not cross the note's split into
two sub-lines.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-24 17:31:01 +01:00
parent 03edbe8876
commit 2dc1912af2
3 changed files with 93 additions and 22 deletions

View File

@ -14,7 +14,8 @@ use skia_safe::{
use crate::render::{FallbackFonts, DEFAULT_EMOJI_FONT};
use crate::shapes::{FontFeatures, TextSpan};
use super::font_tables::upright_centre_metrics;
use super::font_tables::{upright_centre_metrics, vertical_metrics};
use super::orientation::is_upright_char;
/// JLREQ preferred width of a word space inside sideways Western text.
pub(crate) const WESTERN_WORD_SPACING_EM: f32 = 1.0 / 3.0;
@ -91,6 +92,37 @@ impl ShapedRun {
self.advances[glyph..glyph + count].iter().sum()
}
/// Flow advance of a cluster set upright in vertical text, before
/// letter-spacing: its `vmtx` advance. Without one, its horizontal
/// advance (an em when that is empty), floored to an em for characters
/// that are upright only by choice, such as Latin and digits: their
/// glyphs are taller than they are wide.
pub(crate) fn upright_cluster_advance(&self, glyph: usize, count: usize) -> f32 {
let font_size = self.font.size();
let horizontal = match self.cluster_advance(glyph, count) {
advance if advance > 0.0 => advance,
_ => font_size,
};
let forced_upright = self.text[self.text_range(glyph, count)]
.chars()
.next()
.is_some_and(|c| !is_upright_char(c));
let horizontal = if forced_upright {
horizontal.max(font_size)
} else {
horizontal
};
vertical_metrics(&self.font)
.map(|vm| {
self.glyphs[glyph..glyph + count]
.iter()
.map(|g| vm.advance(*g, font_size))
.sum::<f32>()
})
.filter(|advance| *advance > 0.0)
.unwrap_or(horizontal)
}
/// Positions of a cluster's glyphs relative to its first glyph's pen x.
fn cluster_positions(&self, glyph: usize, count: usize) -> Vec<SkPoint> {
let base_x = self.positions[glyph].x;

View File

@ -23,7 +23,7 @@ use crate::shapes::text_shaping::{
use crate::shapes::text_shaping::{
resolve_typefaces, shape_with_typefaces, span_font_families, ShapedRun,
};
use crate::shapes::text_shaping::{upright_baseline, vertical_metrics, vpal_table};
use crate::shapes::text_shaping::{upright_baseline, vpal_table};
/// Smallest horizontal scale for `all` tate-chu-yoko, except short Western
/// runs; digit modes use 1 / run length.
@ -402,7 +402,6 @@ impl<'a> SpanCells<'a> {
) {
let font_size = self.span.font_size;
let letter_spacing = self.span.letter_spacing;
let vmetrics = vertical_metrics(&run.font);
// vpal applies here: SkShaper's horizontal shaping skips vertical GPOS.
let vpal = (self.span.font_features == FontFeatures::Vpal)
.then(|| vpal_table(&run.font))
@ -421,22 +420,7 @@ impl<'a> SpanCells<'a> {
advance if advance > 0.0 => advance,
_ => font_size,
};
// No `vmtx`: h-advance, with forced-upright Latin floored to an em.
let horizontal_fallback = if ch.is_some_and(|c| !is_upright_char(c)) {
h_advance.max(font_size)
} else {
h_advance
};
let vertical_advance = vmetrics
.as_ref()
.map(|vm| {
glyphs
.iter()
.map(|g| vm.advance(*g, font_size))
.sum::<f32>()
})
.filter(|advance| *advance > 0.0)
.unwrap_or(horizontal_fallback);
let vertical_advance = run.upright_cluster_advance(glyph, count);
// vpal tightens the cell and lifts the ink by the font's deltas.
let vpal_delta = vpal
.as_ref()
@ -742,6 +726,10 @@ impl<'a> WarichuNote<'a> {
}
/// Shape one sub-line part of a member; empty parts shape to no runs.
/// Every character stands upright in the sub-line, one per cluster.
/// Tate-chu-yoko does not apply in a note: its digits stand one by one,
/// like kanji numerals, as a combined pair would leave each digit a
/// quarter em wide and could not cross the split into two sub-lines.
fn shape_part(&self, member: usize, range: Range<usize>) -> Option<(Vec<ShapedRun>, f32)> {
if range.is_empty() {
return Some((Vec::new(), 0.0));
@ -752,7 +740,13 @@ impl<'a> WarichuNote<'a> {
if runs.is_empty() {
return None;
}
let advance = runs.iter().map(|r| r.advance).sum();
let advance = runs
.iter()
.flat_map(|run| {
run.cluster_spans()
.map(|(glyph, count)| run.upright_cluster_advance(glyph, count))
})
.sum();
Some((runs, advance))
}

View File

@ -221,7 +221,9 @@ fn cell_draws(layout: &VerticalLayout, cell: &VerticalCell, origin: (f32, f32))
}
/// One warichu sub-line: half-size upright glyphs stacked down the
/// sub-column centred on `x_center`, one cluster per cell.
/// sub-column centred on `x_center`, one cluster per cell. Each cell is
/// as tall as the cluster's upright advance, the one the note's extent
/// sums, so narrow digits and Latin do not overlap.
fn warichu_line_draws(runs: &[ShapedRun], x_center: f32, y_top: f32) -> Vec<GlyphDraw> {
let mut draws = Vec::new();
let mut cursor = y_top;
@ -239,7 +241,7 @@ fn warichu_line_draws(runs: &[ShapedRun], x_center: f32, y_top: f32) -> Vec<Glyp
(x_center - advance / 2.0, cursor + baseline_offset),
));
}
cursor += advance;
cursor += run.upright_cluster_advance(glyph, count);
}
}
draws
@ -966,6 +968,49 @@ mod tests {
}
}
#[test]
fn warichu_digits_under_tate_chu_yoko_do_not_overlap_down_their_sub_line() {
// A note sets its digits upright one by one, as it sets kanji
// numerals: tate-chu-yoko does not apply inside it. A digit is
// taller than it is wide, so each needs a full half-size em down
// its sub-line, not its narrow horizontal advance.
let content = spans_content(
vec![TextSpan {
warichu: true,
text_combine_upright: TextCombineUpright::Digits,
..make_span("1939")
}],
400.0,
);
let layout = layout_content(&content, 400.0);
assert!(layout
.cells
.iter()
.all(|cell| matches!(cell.kind, CellKind::Warichu { .. })));
let glyphs: Vec<skia::Rect> =
paths_from_layout(&layout, &content.bounds(), VerticalAlign::Top, true)
.iter()
.map(|(path, _)| *path.bounds())
.collect();
assert_eq!(glyphs.len(), 4, "one glyph per digit");
let middle = glyphs.iter().map(|g| g.center_x()).sum::<f32>() / 4.0;
for first_line in [true, false] {
let mut line: Vec<&skia::Rect> = glyphs
.iter()
.filter(|glyph| (glyph.center_x() > middle) == first_line)
.collect();
line.sort_by(|a, b| a.top.total_cmp(&b.top));
assert_eq!(line.len(), 2, "two digits per sub-line");
assert!(
line[0].bottom <= line[1].top,
"digits overlap down the sub-line: {:?} then {:?}",
line[0],
line[1]
);
}
}
#[test]
fn tate_chu_yoko_digits_share_the_upright_glyph_baseline() {
// The hhea ascent of a CJK face exceeds the em; centring the