diff --git a/render-wasm/src/shapes/text_shaping/shaping.rs b/render-wasm/src/shapes/text_shaping/shaping.rs index 6de2568c35..8c3b79f425 100644 --- a/render-wasm/src/shapes/text_shaping/shaping.rs +++ b/render-wasm/src/shapes/text_shaping/shaping.rs @@ -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::() + }) + .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 { let base_x = self.positions[glyph].x; diff --git a/render-wasm/src/shapes/text_vertical/cells.rs b/render-wasm/src/shapes/text_vertical/cells.rs index ea9b6d5cdb..a755a5fd08 100644 --- a/render-wasm/src/shapes/text_vertical/cells.rs +++ b/render-wasm/src/shapes/text_vertical/cells.rs @@ -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::() - }) - .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) -> Option<(Vec, 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)) } diff --git a/render-wasm/src/shapes/text_vertical/paint.rs b/render-wasm/src/shapes/text_vertical/paint.rs index 78942b89c7..ec5dc9cc02 100644 --- a/render-wasm/src/shapes/text_vertical/paint.rs +++ b/render-wasm/src/shapes/text_vertical/paint.rs @@ -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 { 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 = + 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::() / 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