diff --git a/render-wasm/src/fonts/notosansjp-vmtx-test.ttf b/render-wasm/src/fonts/notosansjp-vmtx-test.ttf new file mode 100644 index 0000000000..5b36929f63 Binary files /dev/null and b/render-wasm/src/fonts/notosansjp-vmtx-test.ttf differ diff --git a/render-wasm/src/fonts/notosansjp-vpal-test.ttf b/render-wasm/src/fonts/notosansjp-vpal-test.ttf new file mode 100644 index 0000000000..e1fd58e08f Binary files /dev/null and b/render-wasm/src/fonts/notosansjp-vpal-test.ttf differ diff --git a/render-wasm/src/shapes/text_vertical/annotations.rs b/render-wasm/src/shapes/text_vertical/annotations.rs new file mode 100644 index 0000000000..e8cef8443e --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/annotations.rs @@ -0,0 +1,1767 @@ +// Ruby (furigana) and emphasis marks (圏点) in vertical flow. Annotations +// stay out of `cells`, so base metrics, caret geometry and position data +// ignore them; they are placed from the base cells' final columns and flow +// extents. + +use std::collections::HashMap; + +use skia_safe::{self as skia, Font}; + +use crate::shapes::text_attrs::{emphasis_char_allowed, EMPHASIS_FONT_SCALE}; +use crate::shapes::text_ruby::{ + distribute_ruby_pieces, long_ruby_gap, ruby_overhang_room, ruby_pieces, OverhangClaims, +}; +use crate::shapes::{ + AnnotationSide, AppliedTextTransform, FontFeatures, Paragraph, RubyOverhang, TextContent, + TextSpan, +}; + +use super::cells::Fonts; +use super::flow::FlowCell; +use super::layout::{ + column_base_center, CellKind, ParagraphTextMap, VerticalCell, VerticalColumn, VerticalLayout, +}; +use crate::shapes::text_shaping::segment_by_orientation; +use crate::shapes::text_shaping::{ + shape_segment, shape_segment_with_fallbacks, span_font_families, ShapedRun, +}; + +/// One shaped ruby glyph, retaining its fallback-font run and source range. +#[derive(Debug, Clone, Copy)] +pub struct RubyGlyph { + pub run: usize, + pub glyph: usize, + pub utf16_start: usize, + pub utf16_end: usize, + /// Part of a Western word set on its side, as base text in mixed + /// orientation; other glyphs stand upright. + pub sideways: bool, + /// Flow extent: the ruby em upright, the glyph advance sideways. + pub extent: f32, +} + +/// The reading of one ruby span, beside its base in one column, painted from +/// `ruby_runs`. Group ruby keeps a base in one column. +#[derive(Debug, Clone)] +pub struct RubyCell { + /// Glyphs in order; each keeps its shaped run so fallback fonts survive. + pub glyphs: Vec, + pub paragraph: usize, + pub span: usize, + pub column: usize, + /// Flow-axis top of each glyph. + pub glyph_tops: Vec, + pub zhuyin_origins: Option>, + pub font_size: f32, + pub base_font_size: f32, + pub outside_offset: f32, + pub side: AnnotationSide, + pub paint: usize, +} + +impl RubyCell { + pub(super) fn strip_x(&self, column: &VerticalColumn) -> f32 { + let sign = if self.side == AnnotationSide::Over { + 1.0 + } else { + -1.0 + }; + ruby_strip_x(column, self.font_size, self.base_font_size, self.side) + + sign * self.outside_offset + } +} + +/// One emphasis mark (圏点 / bouten) beside a base character: the +/// single-glyph `run`, centred on the character along the flow, in the +/// column's selected-side gutter. +pub struct EmphasisMark { + pub run: usize, + /// Index of the annotated base cell in `VerticalLayout::cells`. + pub cell: usize, + /// Flow offset of the mark centre from the cell top. + pub flow_center: f32, + pub font_size: f32, + /// Cross-axis offset past a stacked ruby layer. + pub outside_offset: f32, + pub side: AnnotationSide, +} + +/// Cross-axis start of a vertical ruby strip. Ruby attaches to the edge of the +/// base em, centred in the column advance (`base_width`), so line height adds +/// no gap between ruby and base. +pub(super) fn ruby_strip_x( + column: &VerticalColumn, + ruby_font_size: f32, + base_font_size: f32, + side: AnnotationSide, +) -> f32 { + let base_center = column_base_center(column); + match side { + AnnotationSide::Over => base_center + base_font_size / 2.0, + AnnotationSide::Under => base_center - base_font_size / 2.0 - ruby_font_size, + } +} + +/// The base text of one reading: its spans (`Paragraph::ruby_groups`), the +/// transformed UTF-16 range they cover in their paragraph and the glyph +/// count of the reading, which the first span holds. +#[derive(Debug, Clone)] +pub(super) struct RubyBaseUnit { + spans: std::ops::Range, + start: usize, + end: usize, + /// Flow length of the reading set solid. + ruby_length: f32, + ruby_font_size: f32, + overhang: RubyOverhang, + /// Overhang room over the characters before and after the base. + room: (f32, f32), +} + +impl RubyBaseUnit { + fn annotates(&self, cell: &VerticalCell) -> bool { + self.spans.contains(&cell.span) && cell.start < self.end && cell.end > self.start + } + + /// Flow length of the reading set solid. + fn ruby_line(&self) -> f32 { + self.ruby_length + } +} + +/// Ruby base units of a paragraph, from its spans' layout text. Shapes each +/// reading to count its glyphs. +pub(super) fn ruby_base_units( + paragraph: &Paragraph, + transforms: &[AppliedTextTransform], + span_starts: &[usize], + fonts: &Fonts, +) -> Vec { + let spans = paragraph.children(); + paragraph + .ruby_groups() + .into_iter() + .filter(|group| { + group + .clone() + .any(|index| !transforms[index].text.is_empty()) + }) + .map(|group| { + let span = &spans[group.start]; + let last = group.end - 1; + ( + group.clone(), + span, + span_starts[group.start], + span_starts[last] + transforms[last].text.encode_utf16().count(), + ) + }) + .map(|(group, span, start, end)| RubyBaseUnit { + spans: group, + start, + end, + ruby_length: if span.is_zhuyin() { + span.font_size + } else { + ruby_glyphs( + span.ruby_text(), + 0, + &shape_ruby(span, fonts), + 0, + span.ruby_font_size(), + ) + .iter() + .map(|glyph| glyph.extent) + .sum() + }, + ruby_font_size: span.ruby_font_size(), + overhang: span.ruby_overhang, + room: (0.0, 0.0), + }) + .collect() +} + +/// The span's reading shaped at the ruby size: CJK upright, Western words on +/// their side under mixed orientation, as base text is set. Each run comes +/// with whether it is sideways; clusters index the whole reading. +fn shape_ruby(span: &TextSpan, fonts: &Fonts) -> Vec<(ShapedRun, bool)> { + let families = span_font_families(span, fonts.fallback_families); + let mut utf8_start = 0usize; + let mut runs = Vec::new(); + for segment in segment_by_orientation(span.ruby_text(), span.text_orientation) { + let shaped = shape_segment_with_fallbacks( + &segment.text, + span.ruby_font_size(), + &families, + fonts.provider, + segment.upright, + span.font_features, + fonts.fallback_mgr, + &span.language, + ); + for mut run in shaped { + for cluster in &mut run.clusters { + *cluster += utf8_start as u32; + } + run.utf8_range = run.utf8_range.start + utf8_start..run.utf8_range.end + utf8_start; + runs.push((run, !segment.upright)); + } + utf8_start += segment.text.len(); + } + runs +} + +/// Overhang room of every unit over its flow neighbours, before placement. +/// Placement later removes the room at a column edge. A kana between two +/// readings lends the later one only what the earlier one leaves (JLREQ +/// Fig 138), so the two readings never meet over it. +pub(super) fn set_ruby_overhang_rooms(flow: &[FlowCell], ruby_units: &mut [RubyBaseUnit]) { + let unit_of_span = units_by_span(ruby_units); + let annotated = |index: usize| { + let cell = &flow[index].cell; + unit_of_span + .get(&cell.span) + .is_some_and(|unit| ruby_units[*unit].annotates(cell)) + }; + let indices = annotated_indices(flow.iter().map(|item| &item.cell), ruby_units); + let mut claims = OverhangClaims::new(); + let rooms: Vec<(f32, f32)> = ruby_units + .iter() + .zip(&indices) + .map(|(unit, indices)| { + let (Some(first), Some(last)) = (indices.first(), indices.last()) else { + return unit.room; + }; + let room = |index: Option| { + index.map_or(0.0, |index| { + ruby_overhang_room( + unit.overhang, + flow[index].ch, + annotated(index), + flow[index].cell.extent, + unit.ruby_font_size, + ) + }) + }; + let before = first.checked_sub(1); + let after = Some(last + 1).filter(|index| *index < flow.len()); + let base: f32 = indices.iter().map(|index| flow[*index].cell.extent).sum(); + claims.room( + before, + after, + (room(before), room(after)), + unit.ruby_line() - base, + ) + }) + .collect(); + for (unit, room) in ruby_units.iter_mut().zip(rooms) { + unit.room = room; + } +} + +/// Index of the unit of each ruby span. +fn units_by_span(ruby_units: &[RubyBaseUnit]) -> HashMap { + ruby_units + .iter() + .enumerate() + .flat_map(|(index, unit)| unit.spans.clone().map(move |span| (span, index))) + .collect() +} + +/// Indices of the cells each unit annotates, in cell order. +fn annotated_indices<'a>( + cells: impl Iterator, + ruby_units: &[RubyBaseUnit], +) -> Vec> { + let unit_of_span = units_by_span(ruby_units); + let mut indices = vec![Vec::new(); ruby_units.len()]; + for (index, cell) in cells.enumerate() { + if let Some(unit) = unit_of_span.get(&cell.span) { + if ruby_units[*unit].annotates(cell) { + indices[*unit].push(index); + } + } + } + indices +} + +/// Indices of the cells of each (paragraph, span), in cell order. +fn cells_by_span(cells: &[VerticalCell]) -> HashMap<(usize, usize), Vec> { + let mut by_span: HashMap<(usize, usize), Vec> = HashMap::new(); + for (index, cell) in cells.iter().enumerate() { + by_span + .entry((cell.paragraph, cell.span)) + .or_default() + .push(index); + } + by_span +} + +/// Indices of the cells of each column, in cell order. +fn cells_by_column(cells: &[VerticalCell]) -> HashMap> { + let mut by_column: HashMap> = HashMap::new(); + for (index, cell) in cells.iter().enumerate() { + by_column.entry(cell.column).or_default().push(index); + } + by_column +} + +/// Overhang room (before, after) of each ruby span, keyed by (paragraph, +/// span). +pub(super) type RubyRooms = HashMap<(usize, usize), (f32, f32)>; + +/// Overhang room of every ruby span of a paragraph, for `layout_ruby`. +pub(super) fn ruby_rooms(paragraph: usize, ruby_units: &[RubyBaseUnit]) -> RubyRooms { + ruby_units + .iter() + .map(|unit| ((paragraph, unit.spans.start), unit.room)) + .collect() +} + +/// Grow the flow extent of base cells under long ruby before column planning, +/// so wrapping makes room (forced spreading). Only the part of the reading +/// that its overhang room cannot take grows the base: each cell grows by one +/// `long_ruby_gap`, its glyph centred, which leaves a full gap between +/// characters and half a gap at each end. +pub(super) fn grow_ruby_bases(flow: &mut [FlowCell], ruby_units: &[RubyBaseUnit]) { + let all_indices = annotated_indices(flow.iter().map(|item| &item.cell), ruby_units); + for (unit, indices) in ruby_units.iter().zip(&all_indices) { + let base_total: f32 = indices.iter().map(|index| flow[*index].cell.extent).sum(); + let gap = long_ruby_gap(unit.ruby_length, base_total, unit.room, indices.len()); + if gap <= 0.0 { + continue; + } + for index in indices { + let cell = &mut flow[*index].cell; + cell.extent += gap; + cell.glyph_flow_shift += gap / 2.0; + cell.ink_top += gap / 2.0; + cell.ink_bottom += gap / 2.0; + } + } +} + +/// Widen gaps between placed base cells under long ruby. Shifts only later +/// cells of the span, and only into slack before the next cell or the +/// column bottom, so columns never re-wrap. A base sits in one column. +pub(super) fn spread_ruby_base_cells( + cells: &mut [VerticalCell], + ruby_units: &[RubyBaseUnit], + max_height: f32, +) { + let column_limit = if max_height.is_finite() && max_height < f32::MAX { + max_height + } else { + f32::MAX + }; + let column_cells = cells_by_column(cells); + let all_indices = annotated_indices(cells.iter(), ruby_units); + for (unit, mut base) in ruby_units.iter().zip(all_indices) { + if base.len() < 2 { + continue; + } + base.sort_by(|a, b| cells[*a].top.total_cmp(&cells[*b].top)); + let ruby_line = unit.ruby_line() - unit.room.0 - unit.room.1; + let first = base[0]; + let last = base[base.len() - 1]; + let base_top = cells[first].top; + let base_bottom = cells[last].top + cells[last].extent; + let base_extent = base_bottom - base_top; + if ruby_line <= base_extent { + continue; + } + + let next_top = column_cells[&cells[first].column] + .iter() + .map(|index| &cells[*index]) + .filter(|cell| !unit.annotates(cell) && cell.top >= base_bottom) + .map(|cell| cell.top) + .min_by(f32::total_cmp); + let limit = next_top.unwrap_or(column_limit); + let spread = (ruby_line - base_extent).min((limit - base_bottom).max(0.0)); + if spread <= 0.0 { + continue; + } + + let gap = spread / (base.len() - 1) as f32; + for (position, index) in base.iter().enumerate().skip(1) { + cells[*index].top += gap * position as f32; + } + } +} + +/// One `RubyGlyph` per shaped glyph of `ruby_text`, with UTF-16 offsets into +/// the span's untrimmed ruby string (`leading_utf16` is the trimmed prefix). +/// `run_start` is the index the first shaped run will take in `ruby_runs`. +fn ruby_glyphs( + ruby_text: &str, + leading_utf16: usize, + shaped: &[(ShapedRun, bool)], + run_start: usize, + ruby_font_size: f32, +) -> Vec { + let mut glyphs: Vec = shaped + .iter() + .enumerate() + .flat_map(|(run_index, (run, sideways))| { + run.clusters + .iter() + .enumerate() + .map(move |(glyph, cluster)| { + let utf8 = (*cluster as usize).min(ruby_text.len()); + RubyGlyph { + run: run_start + run_index, + glyph, + utf16_start: leading_utf16 + ruby_text[..utf8].encode_utf16().count(), + utf16_end: 0, + sideways: *sideways, + extent: if *sideways { + run.advances.get(glyph).copied().unwrap_or(0.0) + } else { + ruby_font_size + }, + } + }) + }) + .collect(); + let ruby_end = leading_utf16 + ruby_text.encode_utf16().count(); + // A base letter and its combining marks can share a cluster. Every + // glyph in that cluster covers the same source range, even when the + // shaper emits more than one glyph or orders clusters right to left. + let mut boundaries: Vec = glyphs.iter().map(|glyph| glyph.utf16_start).collect(); + boundaries.push(ruby_end); + boundaries.sort_unstable(); + boundaries.dedup(); + for glyph in &mut glyphs { + let next = boundaries.partition_point(|offset| *offset <= glyph.utf16_start); + glyph.utf16_end = boundaries.get(next).copied().unwrap_or(ruby_end); + } + glyphs +} + +/// Overhang room of a base segment, without the sides that touch a column +/// edge: ruby never sticks out past the first or last character. +/// `column_cells` indexes the cells of the column. +fn room_inside_column( + cells: &[VerticalCell], + column_cells: &[usize], + base_top: f32, + base_bottom: f32, + room: (f32, f32), +) -> (f32, f32) { + let in_column = || column_cells.iter().map(|index| &cells[*index]); + let has_before = in_column().any(|cell| cell.top + cell.extent <= base_top + 0.01); + let has_after = in_column().any(|cell| cell.top >= base_bottom - 0.01); + ( + if has_before { room.0 } else { 0.0 }, + if has_after { room.1 } else { 0.0 }, + ) +} + +/// Ruby (furigana) placement. Runs after column placement, since ruby follows +/// its base's final column and flow extent. Group ruby keeps each base in one +/// column, so each reading is one `RubyCell`, keyed by the first span of its +/// base (`Paragraph::ruby_groups`). `rooms` holds the overhang room of each +/// (paragraph, first span). +pub(super) fn layout_ruby( + text_content: &TextContent, + cells: &[VerticalCell], + fonts: &Fonts, + rooms: &RubyRooms, +) -> (Vec, Vec) { + let mut ruby_runs: Vec = Vec::new(); + let mut ruby_cells: Vec = Vec::new(); + let span_cells = cells_by_span(cells); + let column_cells = cells_by_column(cells); + for (paragraph_index, paragraph) in text_content.paragraphs().iter().enumerate() { + for group in paragraph.ruby_groups() { + // The first span holds the reading; the base is the text of + // every span of the group. + let span_index = group.start; + let span = ¶graph.children()[span_index]; + let base: Vec = group + .filter_map(|index| span_cells.get(&(paragraph_index, index))) + .flatten() + .copied() + .collect(); + if base.is_empty() { + continue; + } + let (Some(first), Some(last)) = ( + base.iter() + .map(|index| &cells[*index]) + .min_by(|a, b| a.top.total_cmp(&b.top)), + base.iter() + .map(|index| &cells[*index]) + .max_by(|a, b| (a.top + a.extent).total_cmp(&(b.top + b.extent))), + ) else { + continue; + }; + if let Some(syllable) = super::super::text_zhuyin::syllable(span) { + let shaped = super::super::text_zhuyin::shape( + span, + &syllable, + fonts.provider, + fonts.fallback_families, + ); + let top = first.top + (first.extent - span.font_size) / 2.0; + let mut glyphs = Vec::new(); + let mut origins = Vec::new(); + for glyph in shaped { + glyphs.push(RubyGlyph { + run: ruby_runs.len(), + glyph: glyph.glyph, + utf16_start: glyph.range.start, + utf16_end: glyph.range.end, + sideways: false, + extent: span.ruby_font_size(), + }); + origins.push(skia::Point::new(glyph.origin.x, top + glyph.origin.y)); + ruby_runs.push(glyph.run); + } + ruby_cells.push(RubyCell { + glyph_tops: origins.iter().map(|p| p.y).collect(), + zhuyin_origins: Some(origins), + glyphs, + paragraph: paragraph_index, + span: span_index, + column: first.column, + font_size: span.ruby_font_size(), + base_font_size: span.font_size, + outside_offset: span.ruby_decoration_offset(true), + side: AnnotationSide::Over, + paint: first.paint, + }); + continue; + } + let ruby_text = span.ruby_text(); + let ruby_font_size = span.ruby_font_size(); + let shaped = shape_ruby(span, fonts); + let leading_utf16 = span.ruby[..span.ruby.len() - span.ruby.trim_start().len()] + .encode_utf16() + .count(); + let glyphs = ruby_glyphs( + ruby_text, + leading_utf16, + &shaped, + ruby_runs.len(), + ruby_font_size, + ); + if glyphs.is_empty() { + continue; + } + ruby_runs.extend(shaped.into_iter().map(|(run, _)| run)); + + let top = first.top; + let bottom = last.top + last.extent; + let room = rooms + .get(&(paragraph_index, span_index)) + .copied() + .unwrap_or((0.0, 0.0)); + let room = room_inside_column(cells, &column_cells[&first.column], top, bottom, room); + let (pieces, placement) = ruby_pieces( + &glyphs + .iter() + .map(|glyph| (glyph.sideways, glyph.extent)) + .collect::>(), + ruby_font_size, + ); + let piece_tops = distribute_ruby_pieces( + top, + (bottom - top).max(0.0), + &pieces, + span.ruby_align, + room, + ); + let glyph_tops = placement + .iter() + .map(|(piece, offset)| piece_tops[*piece] + offset) + .collect(); + ruby_cells.push(RubyCell { + glyphs, + paragraph: paragraph_index, + span: span_index, + column: first.column, + glyph_tops, + zhuyin_origins: None, + font_size: ruby_font_size, + base_font_size: span.font_size, + outside_offset: span.ruby_decoration_offset(true), + side: span.ruby_side, + paint: first.paint, + }); + } + } + (ruby_runs, ruby_cells) +} + +/// Flow centres (from the cell top) of the emphasis marks of one base cell: +/// one per eligible character of a sideways run, one for a tate-chu-yoko +/// composite and one centred on the glyph ink of an upright character. +/// `text` is the cell's slice of its span's transformed text. +fn emphasis_flow_centers(cell: &VerticalCell, text: &str, runs: &[ShapedRun]) -> Vec { + let ink_center = || { + if cell.ink_bottom > cell.ink_top { + (cell.ink_top + cell.ink_bottom) / 2.0 + } else { + cell.extent / 2.0 + } + }; + match cell.kind { + CellKind::Upright { .. } + | CellKind::SyntheticRotated { .. } + | CellKind::TateChuYoko { .. } => { + if text.chars().any(emphasis_char_allowed) { + vec![ink_center()] + } else { + Vec::new() + } + } + CellKind::Rotated { run } => { + let chars: Vec = text.chars().collect(); + let Some(offsets) = runs + .get(run) + .and_then(|run| run.scalar_flow_offsets(cell.extent, chars.len())) + else { + return Vec::new(); + }; + chars + .iter() + .enumerate() + .filter(|(_, ch)| emphasis_char_allowed(**ch)) + .map(|(index, _)| (offsets[index] + offsets[index + 1]) / 2.0) + .collect() + } + _ => Vec::new(), + } +} + +/// (UTF-16, byte) offset of every character boundary of a text, end +/// included, for slicing it by UTF-16 ranges. +struct Utf16Boundaries<'a> { + text: &'a str, + boundaries: Vec<(usize, usize)>, +} + +impl<'a> Utf16Boundaries<'a> { + fn new(text: &'a str) -> Self { + let mut utf16 = 0; + let mut boundaries: Vec<(usize, usize)> = text + .char_indices() + .map(|(byte, ch)| { + let boundary = (utf16, byte); + utf16 += ch.len_utf16(); + boundary + }) + .collect(); + boundaries.push((utf16, text.len())); + Self { text, boundaries } + } + + /// The characters that start inside the UTF-16 range `start..end`. + fn slice(&self, start: usize, end: usize) -> &'a str { + let byte = |offset: usize| { + let index = self + .boundaries + .partition_point(|(utf16, _)| *utf16 < offset); + self.boundaries[index.min(self.boundaries.len() - 1)].1 + }; + let start = byte(start); + &self.text[start..byte(end).max(start)] + } +} + +/// Emphasis marks (圏点 / bouten) of each span with `text_emphasis`, shaped +/// once per span: one per upright character, per letter of a sideways run +/// and per tate-chu-yoko composite. Whitespace and Japanese punctuation get +/// no mark, as in CSS `text-emphasis`. +pub(super) fn layout_emphasis( + text_content: &TextContent, + cells: &[VerticalCell], + runs: &[ShapedRun], + text_maps: &[ParagraphTextMap], + fonts: &Fonts, +) -> (Vec, Vec) { + let mut emphasis_runs: Vec = Vec::new(); + let mut emphasis_marks: Vec = Vec::new(); + let span_cells = cells_by_span(cells); + for (paragraph_index, paragraph) in text_content.paragraphs().iter().enumerate() { + for (span_index, span) in paragraph.children().iter().enumerate() { + let Some(mark) = span.text_emphasis.mark_char() else { + continue; + }; + let Some(span_cells) = span_cells.get(&(paragraph_index, span_index)) else { + continue; + }; + let mark_font_size = span.font_size * EMPHASIS_FONT_SCALE; + let families = span_font_families(span, fonts.fallback_families); + let match_family = |family: &str| { + fonts + .provider + .match_family_style(family, skia::FontStyle::default()) + }; + let typeface = families + .iter() + .filter_map(|family| match_family(family)) + .find(|typeface| typeface.unichar_to_glyph(mark as i32) != 0) + .or_else(|| match_family(&families[0])); + let Some(typeface) = typeface else { + continue; + }; + let mut shaped = shape_segment( + &mark.to_string(), + &Font::new(typeface, mark_font_size), + true, + FontFeatures::None, + fonts.fallback_mgr.clone(), + &span.language, + ); + if shaped.first().is_none_or(|run| run.glyphs.is_empty()) { + continue; + } + let run_index = emphasis_runs.len(); + emphasis_runs.push(shaped.remove(0)); + let text_map = &text_maps[paragraph_index]; + let span_start = text_map.span_starts[span_index]; + // Cell offsets index the span's transformed text. + let span_text = Utf16Boundaries::new(&text_map.span_transforms[span_index].text); + for cell_index in span_cells.iter().copied() { + let cell = &cells[cell_index]; + let text = span_text.slice(cell.start - span_start, cell.end - span_start); + for flow_center in emphasis_flow_centers(cell, text, runs) { + emphasis_marks.push(EmphasisMark { + run: run_index, + cell: cell_index, + flow_center, + font_size: mark_font_size, + outside_offset: span.emphasis_outside_offset(true), + side: span.emphasis_side(true), + }); + } + } + } + } + (emphasis_runs, emphasis_marks) +} + +/// Centre of an emphasis mark, relative to the layout's content origin: in +/// its column's gutter past any stacked ruby, at its character's centre. +pub(super) fn emphasis_mark_center(layout: &VerticalLayout, mark: &EmphasisMark) -> (f32, f32) { + let cell = &layout.cells[mark.cell]; + let base_font_size = mark.font_size / EMPHASIS_FONT_SCALE; + let sign = if mark.side == AnnotationSide::Over { + 1.0 + } else { + -1.0 + }; + let x = column_base_center(&layout.columns[cell.column]) + + sign * (base_font_size / 2.0 + mark.outside_offset + mark.font_size / 2.0); + (x, cell.top + mark.flow_center) +} + +#[cfg(test)] +mod tests { + use super::super::layout::VerticalLayout; + use super::super::positions::position_data; + use super::super::test_support::*; + use super::*; + use crate::shapes::DIRECTION_VERTICAL_RUBY; + use crate::shapes::{AnnotationClearance, GrowType, RubySize}; + use crate::shapes::{ + TextAlign, TextEmphasis, TextOrientation, TextSpan, TextTransform, VerticalAlign, + }; + + fn emphasis_content(base: &str, emphasis: TextEmphasis) -> TextContent { + spans_content( + vec![TextSpan { + text_emphasis: emphasis, + text_orientation: TextOrientation::Upright, + ..make_span(base) + }], + 400.0, + ) + } + + fn ruby_content_with_line_height(line_height: f32) -> TextContent { + let span = TextSpan { + ruby: "ab".to_string(), + ..make_span("AB") + }; + content_of( + vec![vertical_paragraph(vec![span], TextAlign::Left, line_height)], + 400.0, + GrowType::Fixed, + ) + } + + #[test] + fn zhuyin_is_upright_per_base_and_tones_have_exact_source_ranges() { + let content = spans_content( + vec![ + TextSpan { + ruby: "˙ㄉㄜ".into(), + ruby_zhuyin: true, + annotation_clearance: crate::shapes::AnnotationClearance::Auto, + ..make_span("中") + }, + TextSpan { + ruby: "ㄋㄧˇ".into(), + ruby_zhuyin: true, + annotation_clearance: crate::shapes::AnnotationClearance::Auto, + ..make_span("文") + }, + ], + 400.0, + ); + let layout = layout_with( + &provider(include_bytes!("../../fonts/cjk-locl-test.ttf")), + &content, + ); + assert_eq!(layout.ruby_cells.len(), 2); + for ruby in &layout.ruby_cells { + assert_eq!(ruby.glyphs.len(), 3); + assert!(ruby.glyphs.iter().all(|glyph| !glyph.sideways)); + let origins = ruby.zhuyin_origins.as_ref().unwrap(); + assert_eq!(origins.len(), 3); + assert!((ruby.font_size - EM * 0.3).abs() < 0.01); + } + let neutral = layout.ruby_cells[0].zhuyin_origins.as_ref().unwrap(); + assert!(neutral[0].y < neutral[1].y); + let tone = layout.ruby_cells[1].zhuyin_origins.as_ref().unwrap(); + assert!(tone[2].x > tone[1].x); + assert!(tone[2].y < tone[1].y); + let entries = super::super::positions::position_data( + &layout, + &content.bounds(), + crate::shapes::VerticalAlign::Top, + ); + let readings: Vec<_> = entries + .iter() + .filter(|p| p.direction == crate::shapes::DIRECTION_ZHUYIN) + .collect(); + assert_eq!(readings.len(), 6); + assert!(readings + .iter() + .all(|p| p.end_pos == p.start_pos + 1 && p.width == EM * 0.3)); + } + + #[test] + fn utf16_slices_keep_the_characters_starting_in_range() { + let linear = |text: &str, start: usize, end: usize| { + let mut offset = 0; + text.chars() + .filter(|ch| { + let inside = offset >= start && offset < end; + offset += ch.len_utf16(); + inside + }) + .collect::() + }; + let text = "a𠮷漢🇯🇵b"; + let boundaries = Utf16Boundaries::new(text); + for start in 0..10 { + for end in 0..10 { + assert_eq!( + boundaries.slice(start, end), + linear(text, start, end), + "{start}..{end}" + ); + } + } + } + + #[test] + fn emphasis_span_reserves_gutter_and_emits_one_mark_per_upright_cell() { + let mut content = emphasis_content("AB", TextEmphasis::FilledDot); + content.paragraphs_mut()[0].children_mut()[0].annotation_clearance = + AnnotationClearance::Auto; + let layout = layout_content(&content, 400.0); + let upright = layout + .cells + .iter() + .filter(|c| matches!(c.kind, CellKind::Upright { .. })) + .count(); + assert_eq!(upright, 2, "AB upright yields two base cells"); + assert_eq!( + layout.emphasis_marks.len(), + upright, + "one emphasis mark per upright base cell" + ); + for column in &layout.columns { + assert!( + column.width > column.base_width, + "an emphasis paragraph reserves a gutter beside the base band" + ); + } + let mark = &layout.emphasis_marks[0]; + assert!( + !layout.emphasis_runs[mark.run].glyphs.is_empty(), + "the emphasis mark must be shaped" + ); + } + + #[test] + fn emphasis_skips_whitespace_cells() { + let content = emphasis_content("A B", TextEmphasis::FilledDot); + let layout = layout_content(&content, 400.0); + let upright = layout + .cells + .iter() + .filter(|c| matches!(c.kind, CellKind::Upright { .. })) + .count(); + assert_eq!(upright, 3, "'A B' upright yields three base cells"); + assert_eq!( + layout.emphasis_marks.len(), + 2, + "the whitespace cell gets no emphasis mark" + ); + } + + #[test] + fn emphasis_skips_japanese_commas_stops_and_brackets() { + let content = emphasis_content("A、。(B)」", TextEmphasis::FilledDot); + let layout = layout_content(&content, 400.0); + + assert_eq!( + layout.emphasis_marks.len(), + 2, + "only A and B receive emphasis marks" + ); + } + + #[test] + fn emphasis_follows_the_transformed_text() { + // Uppercase expands ß into SS, so the transformed cells S S 、 A are + // offset from the source text ß、A. + let content = spans_content( + vec![TextSpan { + text_emphasis: TextEmphasis::FilledDot, + text_orientation: TextOrientation::Upright, + text_transform: Some(TextTransform::Uppercase), + ..make_span("ß、A") + }], + 400.0, + ); + let layout = layout_content(&content, 400.0); + assert_eq!(layout.cells.len(), 4); + let marked: Vec = layout.emphasis_marks.iter().map(|mark| mark.cell).collect(); + assert_eq!(marked, vec![0, 1, 3], "S, S and A are marked; 、 is not"); + } + + #[test] + fn no_emphasis_emits_no_marks() { + let content = make_content(&["AB"], 400.0); + let layout = layout_content(&content, 400.0); + assert!(layout.emphasis_marks.is_empty()); + assert!(layout.emphasis_runs.is_empty()); + } + + #[test] + fn long_ruby_stays_inside_the_column_end_under_end_alignment() { + // JLREQ Fig 141: ……の径(こみち) set at the column end; the reading + // overhangs の but never the line end. + let height = 8.0 * EM; + let spans = vec![ + make_span("……の"), + TextSpan { + ruby: "こみち".to_string(), + ..make_span("径") + }, + ]; + let content = content_of( + vec![vertical_paragraph(spans, TextAlign::Right, 1.0)], + height, + GrowType::Fixed, + ); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, height); + let ruby = &layout.ruby_cells[0]; + let end = ruby + .glyph_tops + .iter() + .zip(&ruby.glyphs) + .map(|(top, glyph)| top + glyph.extent) + .fold(f32::MIN, f32::max); + let base = layout.cells.last().expect("base cell"); + assert!( + (base.top + base.extent - height).abs() < 0.01, + "the base sits at the column end" + ); + assert!(end <= height + 0.01, "ruby ends at {end}, past {height}"); + } + + #[test] + fn long_ruby_overhangs_the_kana_side_only() { + // JLREQ Fig 122: の旬(しゅん)又 hangs しゅん over の, never over 又. + let content = spans_content( + vec![ + make_span("の"), + TextSpan { + ruby: "しゅん".to_string(), + ..make_span("旬") + }, + make_span("又"), + ], + 400.0, + ); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let ruby = &layout.ruby_cells[0]; + let base = layout + .cells + .iter() + .find(|cell| cell.span == 1) + .expect("base"); + let start = ruby.glyph_tops[0]; + let end = ruby.glyph_tops[2] + ruby.glyphs[2].extent; + assert!( + (end - (base.top + base.extent)).abs() < 0.01, + "ends with 旬: {end}" + ); + assert!( + (base.top - start - EM / 2.0).abs() < 0.01, + "hangs 1/2 em over の" + ); + } + + #[test] + fn neighbouring_readings_never_meet_over_a_shared_kana() { + // JLREQ Fig 138: に暁(あかつき)の趣(おもむき)を. あかつき hangs over の; + // おもむき may not, so 趣 spreads and the readings stay apart. + let content = spans_content( + vec![ + make_span("に"), + TextSpan { + ruby: "あかつき".to_string(), + ..make_span("暁") + }, + make_span("の"), + TextSpan { + ruby: "おもむき".to_string(), + ..make_span("趣") + }, + make_span("を"), + ], + 400.0, + ); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let no = layout.cells.iter().find(|cell| cell.span == 2).expect("の"); + let (first, second) = (&layout.ruby_cells[0], &layout.ruby_cells[1]); + let first_end = first.glyph_tops[3] + first.glyphs[3].extent; + let second_start = second.glyph_tops[0]; + assert!(first_end > no.top + 0.01, "あかつき hangs over の"); + assert!( + second_start >= no.top + no.extent - 0.01, + "おもむき does not" + ); + assert!( + second_start - first_end >= EM / 2.0 - 0.01, + "a ruby em apart" + ); + } + + #[test] + fn vertical_emphasis_reserves_its_side_and_only_stacks_with_same_side_ruby() { + for position in [ + crate::shapes::TextEmphasisPosition::OverLeft, + crate::shapes::TextEmphasisPosition::UnderRight, + ] { + for side in [AnnotationSide::Over, AnnotationSide::Under] { + let mut content = ruby_content("你好", "nǐ hǎo", 400.0); + content.paragraphs_mut()[0].set_language("zh-Hans".to_string()); + let span = &mut content.paragraphs_mut()[0].children_mut()[0]; + span.text_emphasis = TextEmphasis::FilledDot; + span.text_emphasis_position = position; + span.annotation_clearance = AnnotationClearance::Auto; + span.ruby_side = side; + let same_side = span.emphasis_side(true) == side; + let ruby_size = span.ruby_font_size(); + let layout = layout_content(&content, 400.0); + assert_eq!(layout.emphasis_marks.len(), 2); + for mark in &layout.emphasis_marks { + let column = &layout.columns[layout.cells[mark.cell].column]; + let (x, _) = emphasis_mark_center(&layout, mark); + assert_eq!( + x < column_base_center(column), + mark.side == AnnotationSide::Under + ); + assert!( + (mark.outside_offset - if same_side { ruby_size } else { 0.0 }).abs() + < 0.01 + ); + assert!(x - mark.font_size / 2.0 >= column.x - 0.01); + assert!(x + mark.font_size / 2.0 <= column.x + column.width + 0.01); + } + } + } + } + + #[test] + fn default_japanese_clearance_keeps_mixed_font_annotation_gutters() { + let content = spans_content( + vec![ + TextSpan { + font_size: 40.0, + ruby: "a".to_string(), + annotation_clearance: AnnotationClearance::Auto, + ..make_span("日") + }, + TextSpan { + font_size: 30.0, + text_emphasis: TextEmphasis::FilledDot, + annotation_clearance: AnnotationClearance::Auto, + ..make_span("本") + }, + TextSpan { + font_size: 20.0, + ruby: "b".to_string(), + ruby_size: RubySize::Quarter, + text_emphasis: TextEmphasis::FilledDot, + annotation_clearance: AnnotationClearance::Auto, + ..make_span("語") + }, + ], + 400.0, + ); + let layout = layout_content(&content, 400.0); + // The original gutter stacks the largest ruby (20px) and emphasis + // (15px) when any span combines them, beside the 40px base band. + assert_eq!(layout.columns.len(), 1); + assert!((layout.columns[0].width - 75.0).abs() < 0.01); + } + + #[test] + fn underline_ruby_and_emphasis_fit_in_one_automatic_gutter() { + let mut content = ruby_content("你好", "nǐ hǎo", 400.0); + let span = &mut content.paragraphs_mut()[0].children_mut()[0]; + span.ruby_side = AnnotationSide::Under; + span.text_emphasis = TextEmphasis::FilledDot; + span.text_emphasis_position = crate::shapes::TextEmphasisPosition::UnderLeft; + span.text_decoration = Some(crate::shapes::TextDecoration::UNDERLINE); + span.text_underline_position = crate::shapes::TextUnderlinePosition::UnderLeft; + span.annotation_clearance = AnnotationClearance::Auto; + let layout = layout_content(&content, 400.0); + let column = &layout.columns[0]; + let ruby = &layout.ruby_cells[0]; + let base_left = column_base_center(column) - EM / 2.0; + assert!(ruby.strip_x(column) + ruby.font_size < base_left - EM / 9.0); + for mark in &layout.emphasis_marks { + let (x, _) = emphasis_mark_center(&layout, mark); + assert!(x + mark.font_size / 2.0 <= ruby.strip_x(column) + 0.01); + assert!(x - mark.font_size / 2.0 >= column.x - 0.01); + } + } + + #[test] + fn vertical_pinyin_keeps_tone_clusters_and_base_groups() { + let mut results = Vec::new(); + for reading in ["nǐ hǎo", "ni\u{30c} ha\u{30c}o"] { + let mut content = ruby_content("你好", reading, 400.0); + content.paragraphs_mut()[0].set_language("zh-Hans-CN".to_string()); + let layout = layout_with_height(&provider(PINYIN_TEST_FONT), &content, 400.0); + assert_eq!(layout.ruby_cells.len(), 1); + let ruby = &layout.ruby_cells[0]; + assert_eq!((ruby.paragraph, ruby.span), (0, 0)); + assert!(ruby.glyphs.iter().all(|glyph| glyph.sideways)); + for glyph in &ruby.glyphs { + let run = &layout.ruby_runs[glyph.run]; + assert_ne!( + run.glyphs[glyph.glyph], 0, + "the pinned font covers tone marks" + ); + assert!(glyph.utf16_start < glyph.utf16_end); + } + for (index, pair) in ruby.glyph_tops.windows(2).enumerate() { + assert!((pair[1] - pair[0] - ruby.glyphs[index].extent).abs() < 0.01); + } + let width: f32 = ruby.glyphs.iter().map(|glyph| glyph.extent).sum(); + results.push(( + ruby.glyphs + .iter() + .map(|glyph| layout.ruby_runs[glyph.run].glyphs[glyph.glyph]) + .collect::>(), + width, + )); + } + assert_eq!(results[0].0, results[1].0); + assert!((results[0].1 - results[1].1).abs() < 0.01); + + let content = spans_content( + vec![ + TextSpan { + ruby: "nǐ".to_string(), + ..make_span("你") + }, + TextSpan { + ruby: "hǎo".to_string(), + ..make_span("好") + }, + ], + 400.0, + ); + let layout = layout_with_height(&provider(PINYIN_TEST_FONT), &content, 400.0); + assert_eq!( + layout + .ruby_cells + .iter() + .map(|ruby| ruby.span) + .collect::>(), + vec![0, 1] + ); + } + + #[test] + fn western_ruby_is_one_sideways_word_centred_on_its_base() { + // JLREQ Fig 112: "editor" beside 編集者 turns on its side like base + // Latin text and is never spread letter by letter. + let content = ruby_content("くくくくく", "editor", 400.0); + let layout = layout_content(&content, 400.0); + let ruby = &layout.ruby_cells[0]; + assert_eq!(ruby.glyphs.len(), 6); + assert!(ruby.glyphs.iter().all(|glyph| glyph.sideways)); + for (index, pair) in ruby.glyph_tops.windows(2).enumerate() { + assert!( + (pair[1] - pair[0] - ruby.glyphs[index].extent).abs() < 0.01, + "letters are set at their own advances" + ); + } + let word: f32 = ruby.glyphs.iter().map(|glyph| glyph.extent).sum(); + let base_bottom = layout + .cells + .last() + .map_or(0.0, |cell| cell.top + cell.extent); + let before = ruby.glyph_tops[0] - layout.cells[0].top; + let after = base_bottom - (ruby.glyph_tops[0] + word); + assert!( + (before - after).abs() < 0.01, + "centred: {before} vs {after}" + ); + } + + #[test] + fn ruby_span_reserves_gutter_and_emits_ruby_cells() { + let mut content = ruby_content("AB", "ab", 400.0); + content.paragraphs_mut()[0].children_mut()[0].annotation_clearance = + AnnotationClearance::Auto; + let layout = layout_content(&content, 400.0); + assert!( + !layout.ruby_cells.is_empty(), + "a span with ruby must emit ruby cells" + ); + for column in &layout.columns { + assert!( + column.width > column.base_width, + "a ruby paragraph reserves a gutter beside the base band" + ); + } + let ruby = &layout.ruby_cells[0]; + assert!( + ruby.glyphs.iter().all(|glyph| layout + .ruby_runs + .get(glyph.run) + .and_then(|run| run.glyphs.get(glyph.glyph)) + .is_some()), + "every ruby glyph must retain its shaped run" + ); + } + + #[test] + fn ruby_size_and_side_control_gutter_geometry() { + let mut content = ruby_content("AB", "ab", 400.0); + let span = &mut content.paragraphs_mut()[0].children_mut()[0]; + span.ruby_size = RubySize::Quarter; + span.ruby_side = AnnotationSide::Under; + span.annotation_clearance = AnnotationClearance::Auto; + + let layout = layout_content(&content, 400.0); + let column = &layout.columns[0]; + let ruby = &layout.ruby_cells[0]; + + assert!((ruby.font_size - 5.0).abs() < 0.001); + assert!((column.base_offset - 5.0).abs() < 0.001); + assert!((column.width - column.base_width - 5.0).abs() < 0.001); + assert_eq!(ruby.side, AnnotationSide::Under); + } + + #[test] + fn auto_clearance_stacks_ruby_and_emphasis_gutters() { + let mut none_content = ruby_content("漢字", "かんじ", 400.0); + none_content.paragraphs_mut()[0].children_mut()[0].text_emphasis = TextEmphasis::FilledDot; + let none = layout_content(&none_content, 400.0); + + let mut auto_content = none_content.clone(); + auto_content.paragraphs_mut()[0].children_mut()[0].annotation_clearance = + AnnotationClearance::Auto; + let auto = layout_content(&auto_content, 400.0); + + let none_gutter = none.columns[0].width - none.columns[0].base_width; + let auto_gutter = auto.columns[0].width - auto.columns[0].base_width; + assert!(none_gutter.abs() < 0.001, "none keeps the column advance"); + assert!((auto_gutter - 20.0).abs() < 0.001); + assert!(auto.emphasis_marks[0].outside_offset > 0.0); + } + + #[test] + fn ruby_attachment_distance_is_independent_of_line_height() { + let compact_content = ruby_content_with_line_height(1.0); + let loose_content = ruby_content_with_line_height(2.0); + let compact = layout_content(&compact_content, 400.0); + let loose = layout_content(&loose_content, 400.0); + + let attachment_gap = |layout: &VerticalLayout| { + let ruby = &layout.ruby_cells[0]; + let column = &layout.columns[ruby.column]; + let base_right = column_base_center(column) + ruby.base_font_size / 2.0; + ruby_strip_x(column, ruby.font_size, ruby.base_font_size, ruby.side) - base_right + }; + + assert!( + loose.columns[0].base_width > compact.columns[0].base_width, + "line height must still increase column progression" + ); + assert!(attachment_gap(&compact).abs() < 0.001); + assert!(attachment_gap(&loose).abs() < 0.001); + + let loose_positions = position_data(&loose, &loose_content.bounds(), VerticalAlign::Top); + let ruby_position = loose_positions + .iter() + .find(|entry| entry.direction == DIRECTION_VERTICAL_RUBY) + .expect("ruby position data"); + assert!( + (ruby_position.width - loose.ruby_cells[0].font_size).abs() < 0.001, + "export geometry must use the attached ruby strip, not the line-height band" + ); + } + + #[test] + fn vertical_ruby_preserves_every_fallback_run() { + let content = ruby_content("AB", "aあ", 400.0); + let provider = provider_with_fallback(TEST_FONT, VMTX_TEST_FONT); + let layout = layout_with_fallback(&provider, &content, 400.0, &["fallback".to_string()]); + + let referenced_runs: std::collections::BTreeSet = layout + .ruby_cells + .iter() + .flat_map(|cell| cell.glyphs.iter().map(|glyph| glyph.run)) + .collect(); + assert_eq!(referenced_runs.len(), 2, "ruby must retain both typefaces"); + assert_eq!( + layout + .ruby_cells + .iter() + .map(|cell| cell.glyphs.len()) + .sum::(), + layout.ruby_runs.iter().map(|run| run.glyphs.len()).sum(), + "every fallback glyph must be assigned to a ruby cell" + ); + + let position = position_data(&layout, &content.bounds(), VerticalAlign::Top); + let ruby_position = position + .iter() + .find(|entry| entry.direction == DIRECTION_VERTICAL_RUBY) + .expect("ruby position data"); + assert_eq!(ruby_position.start_pos, 0); + assert_eq!(ruby_position.end_pos, 2); + } + + #[test] + fn no_ruby_keeps_base_width_equal_to_width() { + let content = make_content(&["AB"], 400.0); + let layout = layout_content(&content, 400.0); + assert!(layout.ruby_cells.is_empty()); + for column in &layout.columns { + assert_eq!( + column.width, column.base_width, + "columns without ruby must not reserve a gutter" + ); + } + } + + #[test] + fn ruby_side_attaches_to_opposite_base_edges() { + let column = VerticalColumn { + x: 10.0, + width: 40.0, + base_offset: 8.0, + base_width: 24.0, + }; + + assert_eq!(ruby_strip_x(&column, 8.0, 20.0, AnnotationSide::Over), 40.0); + assert_eq!( + ruby_strip_x(&column, 8.0, 20.0, AnnotationSide::Under), + 12.0 + ); + } + + #[test] + fn long_ruby_spreads_a_vertical_base_with_half_gaps_at_the_ends() { + let plain = layout_content(&make_content(&["日本"], 1000.0), 1000.0); + let mut content = ruby_content("日本", "にほんごです", 1000.0); + content.paragraphs_mut()[0].children_mut()[0].ruby_overhang = RubyOverhang::None; + let layout = layout_content(&content, 1000.0); + // Six half-em ruby glyphs; what the base lacks is shared by its two + // characters. + let base: f32 = plain.cells.iter().map(|cell| cell.extent).sum(); + let gap = (6.0 * EM / 2.0 - base) / 2.0; + assert!(gap > 0.0); + for (spread, solid) in layout.cells.iter().zip(&plain.cells) { + assert!((spread.extent - (solid.extent + gap)).abs() < 0.01); + assert!((spread.glyph_flow_shift - (solid.glyph_flow_shift + gap / 2.0)).abs() < 0.01); + } + } + + #[test] + fn ruby_base_spreading_expands_gap_for_long_reading() { + let content = ruby_content("日本", "にほんご", 120.0); + let layout = layout_content(&content, 120.0); + let plain = layout_content(&make_content(&["日本"], 120.0), 120.0); + let ruby_cells: Vec<&VerticalCell> = layout + .cells + .iter() + .filter(|cell| cell.span == 0 && cell.column == 0) + .collect(); + assert_eq!(ruby_cells.len(), 2); + let spacing = ruby_cells[1].top - ruby_cells[0].top; + assert!( + spacing > plain.cells[1].top - plain.cells[0].top, + "long ruby should spread the base characters apart, got {}", + spacing + ); + let base_extent = ruby_cells[1].top + ruby_cells[1].extent - ruby_cells[0].top; + assert!( + base_extent >= 40.0, + "base span should be at least as long as the 4-glyph half-em ruby line" + ); + } + + #[test] + fn ruby_base_spreading_does_not_overlap_following_text() { + let content = make_content_with_spans(&["日本", "語"], 60.0); + let mut content = content; + content.paragraphs_mut()[0].children_mut()[0].ruby = "にほんご".to_string(); + let layout = layout_content(&content, 60.0); + let mut same_column: Vec<&VerticalCell> = layout + .cells + .iter() + .filter(|cell| cell.column == 0) + .collect(); + same_column.sort_by(|a, b| { + a.top + .partial_cmp(&b.top) + .unwrap_or(std::cmp::Ordering::Equal) + }); + for pair in same_column.windows(2) { + assert!( + pair[0].top + pair[0].extent <= pair[1].top + 0.001, + "base spreading must not overlap the following cell" + ); + } + } + + #[test] + fn ruby_base_spreading_forces_room_when_no_slack() { + // Base 日本 (40) with a 60-long reading, then 語, in a 60 column: with + // no slack after placement, the base grows before planning and pushes + // 語 to the next column. + let mut content = make_content_with_spans(&["日本", "語"], 60.0); + content.paragraphs_mut()[0].children_mut()[0].ruby = "にほんごです".to_string(); + let layout = layout_content(&content, 60.0); + let base: Vec<&VerticalCell> = layout.cells.iter().filter(|c| c.span == 0).collect(); + assert_eq!(base.len(), 2); + let base_extent = + base.last().unwrap().top + base.last().unwrap().extent - base.first().unwrap().top; + assert!( + base_extent >= 60.0 - 0.001, + "the base span must grow to the ruby line length, got {}", + base_extent + ); + let follower = layout + .cells + .iter() + .find(|c| c.span == 1) + .expect("follower cell"); + assert_ne!( + follower.column, base[0].column, + "the follower must wrap to the next column instead of overlapping" + ); + } + + #[test] + fn emphasis_marks_sideways_letters_and_tcy() { + let content = spans_content( + vec![ + TextSpan { + text_emphasis: TextEmphasis::FilledDot, + ..make_span("かabc") + }, + TextSpan { + text_emphasis: TextEmphasis::FilledDot, + text_combine_upright: crate::shapes::TextCombineUpright::All, + ..make_span("12") + }, + ], + 400.0, + ); + let layout = layout_with(&provider_with_fallback(VMTX_TEST_FONT, TEST_FONT), &content); + assert_eq!( + layout.emphasis_marks.len(), + 5, + "か, a, b, c and the TCY composite each get one mark" + ); + } + + #[test] + fn emphasis_mark_centres_on_the_glyph_not_the_spacing() { + let content = spans_content( + vec![TextSpan { + text_emphasis: TextEmphasis::FilledDot, + letter_spacing: 10.0, + ..make_span("かき") + }], + 400.0, + ); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + for mark in &layout.emphasis_marks { + let cell = &layout.cells[mark.cell]; + let ink_centre = cell.top + (cell.ink_top + cell.ink_bottom) / 2.0; + let (_, centre) = emphasis_mark_center(&layout, mark); + assert!( + (centre - ink_centre).abs() < 0.01, + "mark at {centre}, glyph ink centred at {ink_centre}" + ); + } + } + + fn reading_over(spans: Vec) -> VerticalLayout { + layout_content( + &spans_content( + [vec![make_span("か")], spans, vec![make_span("か")]].concat(), + 1000.0, + ), + 1000.0, + ) + } + + #[test] + fn a_reading_joined_over_spans_of_other_styles_is_set_like_one_span() { + let one = reading_over(vec![TextSpan { + ruby: "にほんご".to_string(), + ..make_span("日本") + }]); + let joined = reading_over(vec![ + TextSpan { + ruby: "にほんご".to_string(), + ..make_span("日") + }, + TextSpan { + ruby: "にほんご".to_string(), + ruby_joins_previous: true, + text_emphasis: TextEmphasis::FilledSesame, + ..make_span("本") + }, + ]); + + assert_eq!(joined.ruby_cells.len(), 1, "one reading over the two spans"); + let (one, joined) = (&one.ruby_cells[0], &joined.ruby_cells[0]); + assert_eq!( + joined.span, 1, + "the first span of the base holds the reading" + ); + assert_eq!(joined.glyphs.len(), one.glyphs.len()); + for (a, b) in one.glyph_tops.iter().zip(&joined.glyph_tops) { + assert!( + (a - b).abs() < 0.01, + "glyph at {b}, one span sets it at {a}" + ); + } + } + + #[test] + fn neighbour_readings_without_the_join_annotate_each_span() { + let layout = reading_over(vec![ + TextSpan { + ruby: "てん".to_string(), + ..make_span("天") + }, + TextSpan { + ruby: "てん".to_string(), + ..make_span("天") + }, + ]); + + assert_eq!(layout.ruby_cells.len(), 2); + } + + fn neighbour_ruby_content(overhang: RubyOverhang) -> TextContent { + spans_content( + vec![ + make_span("か"), + TextSpan { + ruby: "かんじかんじ".to_string(), + ruby_overhang: overhang, + ..make_span("漢") + }, + TextSpan { + ruby: "じ".to_string(), + ..make_span("字") + }, + make_span("か"), + ], + 1000.0, + ) + } + + fn ruby_of_span(layout: &VerticalLayout, span: usize) -> &RubyCell { + layout + .ruby_cells + .iter() + .find(|ruby| ruby.span == span) + .expect("ruby cell") + } + + #[test] + fn long_reading_never_covers_the_next_ruby_base() { + let layout = layout_with( + &provider(VMTX_TEST_FONT), + &neighbour_ruby_content(RubyOverhang::Auto), + ); + let ruby = ruby_of_span(&layout, 1); + let next_base = layout.cells.iter().find(|cell| cell.span == 2).unwrap(); + let ruby_bottom = ruby.glyph_tops.last().unwrap() + ruby.font_size; + assert!( + ruby_bottom <= next_base.top + 0.01, + "reading ends at {ruby_bottom}, the next ruby base starts at {}", + next_base.top + ); + } + + #[test] + fn long_reading_overhangs_kana_by_at_most_one_ruby_character() { + let layout = layout_with( + &provider(VMTX_TEST_FONT), + &neighbour_ruby_content(RubyOverhang::Auto), + ); + let ruby = ruby_of_span(&layout, 1); + let base = layout.cells.iter().find(|cell| cell.span == 1).unwrap(); + assert!(ruby.glyph_tops[0] >= base.top - ruby.font_size - 0.01); + } + + #[test] + fn long_reading_without_overhang_grows_a_single_base() { + let layout = layout_with( + &provider(VMTX_TEST_FONT), + &neighbour_ruby_content(RubyOverhang::None), + ); + let ruby = ruby_of_span(&layout, 1); + let base = layout.cells.iter().find(|cell| cell.span == 1).unwrap(); + assert!(ruby.glyph_tops[0] >= base.top - 0.01); + assert!(ruby.glyph_tops.last().unwrap() + ruby.font_size <= base.top + base.extent + 0.01); + } + + /// 文 中(zhuang) , 文 in vertical Chinese: the reading is longer than + /// its base, with a Han character before and a comma after. + fn chinese_pinyin_neighbour_layout(overhang: RubyOverhang) -> VerticalLayout { + let mut content = spans_content( + vec![ + make_span("文"), + TextSpan { + ruby: "zhuangzhuang".to_string(), + ruby_overhang: overhang, + ..make_span("中") + }, + make_span(",文"), + ], + 1000.0, + ); + content.paragraphs_mut()[0].set_language("zh-Hant-TW".to_string()); + layout_with_height(&provider(PINYIN_TEST_FONT), &content, 1000.0) + } + + fn column_extent(layout: &VerticalLayout) -> f32 { + layout + .cells + .iter() + .map(|cell| cell.top + cell.extent) + .fold(0.0, f32::max) + } + + #[test] + fn vertical_chinese_pinyin_overhangs_a_following_comma() { + let overhung = chinese_pinyin_neighbour_layout(RubyOverhang::Auto); + let grown = chinese_pinyin_neighbour_layout(RubyOverhang::None); + assert!( + column_extent(&overhung) < column_extent(&grown) - 0.5, + "overhang shortens the column: {} vs {}", + column_extent(&overhung), + column_extent(&grown) + ); + let ruby = ruby_of_span(&overhung, 1); + let base = overhung.cells.iter().find(|cell| cell.span == 1).unwrap(); + let ruby_end = ruby.glyph_tops.last().unwrap() + ruby.glyphs.last().unwrap().extent; + assert!( + ruby_end > base.top + base.extent + 0.5, + "the reading hangs over the comma" + ); + assert!( + ruby.glyph_tops[0] >= base.top - 0.01, + "a Han neighbour takes no overhang, as in horizontal text" + ); + } + + #[test] + fn vertical_chinese_pinyin_without_overhang_grows_its_base() { + let layout = chinese_pinyin_neighbour_layout(RubyOverhang::None); + let ruby = ruby_of_span(&layout, 1); + let base = layout.cells.iter().find(|cell| cell.span == 1).unwrap(); + let ruby_length: f32 = ruby.glyphs.iter().map(|glyph| glyph.extent).sum(); + assert!(ruby_length > EM, "the reading is longer than its base"); + assert!((base.extent - ruby_length).abs() < 0.01); + assert!(ruby.glyph_tops[0] >= base.top - 0.01); + let ruby_end = ruby.glyph_tops.last().unwrap() + ruby.glyphs.last().unwrap().extent; + assert!(ruby_end <= base.top + base.extent + 0.01); + } + + #[test] + fn ruby_base_moves_to_the_next_column_whole() { + let content = |height: f32| { + spans_content( + vec![ + make_span("あい"), + TextSpan { + ruby: "にほん".to_string(), + ..make_span("日本") + }, + ], + height, + ) + }; + let wide = layout_content(&content(1000.0), 1000.0); + // Room for あい and the first base character only. + let height = wide.cells[..3].iter().map(|cell| cell.extent).sum::() + 0.5; + let layout = layout_content(&content(height), height); + let base_columns: Vec = layout + .cells + .iter() + .filter(|cell| cell.span == 1) + .map(|cell| cell.column) + .collect(); + assert_eq!(base_columns, vec![1, 1], "the ruby base is one group"); + let ruby_columns: std::collections::BTreeSet = + layout.ruby_cells.iter().map(|r| r.column).collect(); + assert_eq!(ruby_columns.into_iter().collect::>(), vec![1]); + } + + #[test] + fn ruby_base_longer_than_a_column_keeps_its_reading_whole() { + let content = ruby_content("日本語文", "にほんごぶん", 40.0); + let layout = layout_content(&content, 40.0); + + let base_columns: std::collections::BTreeSet = + layout.cells.iter().map(|c| c.column).collect(); + assert_eq!(base_columns.len(), 1, "the ruby base never wraps"); + assert_eq!(layout.ruby_cells.len(), 1, "the reading stays in one piece"); + let placed: usize = layout.ruby_cells.iter().map(|r| r.glyphs.len()).sum(); + assert_eq!( + placed, + layout.ruby_runs.iter().map(|run| run.glyphs.len()).sum() + ); + } +} diff --git a/render-wasm/src/shapes/text_vertical/cells.rs b/render-wasm/src/shapes/text_vertical/cells.rs new file mode 100644 index 0000000000..5286f2a907 --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/cells.rs @@ -0,0 +1,1447 @@ +// Builders that turn one span's text into flow cells: one per upright glyph +// cluster, one per rotated (sideways) run, and one per tate-chu-yoko +// composite. Warichu notes span several spans and build their own cells. + +use std::ops::Range; +use unicode_segmentation::UnicodeSegmentation; + +use skia_safe::{textlayout::TypefaceFontProvider, FontMgr, Typeface}; + +use crate::render::FallbackFonts; +use crate::shapes::jlreq::{classify, JlreqClass}; +use crate::shapes::jlreq::{forbidden_at_line_end, forbidden_at_line_start}; +use crate::shapes::text_warichu::{warichu_split_chars, WARICHU_FONT_SCALE}; +use crate::shapes::{FontFeatures, TextCombineUpright, TextSpan}; + +use super::flow::{min_squeezed_extent, FlowCell, FlowScript, FIT_TOLERANCE}; +use super::layout::{CellKind, VerticalCell}; +use crate::shapes::text_shaping::{ + is_upright_char, segment_by_orientation, uses_rotated_vertical_fallback, Segment, +}; +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}; + +/// Smallest horizontal scale for `all` tate-chu-yoko, except short Western +/// runs; digit modes use 1 / run length. +const MIN_TCY_SCALE: f32 = 0.5; + +/// Smallest horizontal scale of an `all` composite of `text`. A Western run +/// no longer than a `digits` composite squeezes as far as one (1 / length), +/// so `2027` combines under `all` as it does under `digits`. Upright CJK +/// keeps MIN_TCY_SCALE: a deeper squeeze makes full-width glyphs illegible. +fn all_min_scale(text: &str) -> f32 { + let count = text.chars().count().max(1); + let digits_max = TextCombineUpright::Digits.digits_max().unwrap_or(4); + if count <= digits_max && !text.chars().any(is_upright_char) { + MIN_TCY_SCALE.min(1.0 / count as f32) + } else { + MIN_TCY_SCALE + } +} + +/// Fonts available to the layout: the provider's registered faces and the +/// fallback families tried after each span's own font. +pub(super) struct Fonts<'a> { + pub provider: &'a TypefaceFontProvider, + pub fallback_mgr: &'a FontMgr, + pub fallback_families: &'a FallbackFonts, +} + +/// Centered punctuation (jlreq cl-05 中点類: middle dot, full-width colon and +/// semicolon), set at the centre of the em box in vertical writing. +fn is_centered_punctuation(c: char) -> bool { + classify(c) == JlreqClass::MiddleDot +} + +/// Flow-axis shift that centres a glyph's ink band in its em body. +fn centered_flow_shift(ink_top: f32, ink_bottom: f32, em_body: f32) -> f32 { + em_body / 2.0 - (ink_top + ink_bottom) / 2.0 +} + +/// A single-glyph cluster whose font has no vertical alternate for it: the +/// shaped glyph is still the horizontal one. +fn needs_rotated_vertical_fallback(run: &ShapedRun, glyph: usize, count: usize, ch: char) -> bool { + count == 1 + && uses_rotated_vertical_fallback(ch) + && run.glyphs[glyph] == run.font.unichar_to_glyph(ch as i32) +} + +fn is_tcy_digit(c: char) -> bool { + c.is_ascii_digit() || ('0'..='9').contains(&c) +} + +/// Split a TCY `digits` span into pieces. Maximal ASCII or full-width digit +/// runs of 1..=max chars are marked tcy, as in CSS `digits`, so a lone digit +/// stands upright too; the rest keep normal vertical layout. Returns (piece text, span-relative UTF-16 start, tcy). +fn split_digit_runs(text: &str, max: usize) -> Vec<(String, usize, bool)> { + let mut pieces: Vec<(String, usize, bool)> = Vec::new(); + let mut utf16 = 0usize; + for c in text.chars() { + let digit = is_tcy_digit(c); + match pieces.last_mut() { + Some((piece, _, piece_digit)) if *piece_digit == digit => piece.push(c), + _ => pieces.push((c.to_string(), utf16, digit)), + } + utf16 += c.len_utf16(); + } + for (piece, _, tcy) in pieces.iter_mut() { + *tcy = *tcy && (1..=max).contains(&piece.chars().count()); + } + pieces +} + +/// UTF-16 offset, within the shaped piece, of a UTF-8 offset in `segment`. +/// A sideways segment split after each run of spaces, so every word is its +/// own cell and a column may break between words. Upright segments stay +/// whole. +fn split_sideways_words(segment: Segment) -> Vec { + if segment.upright { + return vec![segment]; + } + let mut words: Vec = Vec::new(); + let mut utf16 = segment.utf16_start; + let mut previous_space = false; + for ch in segment.text.chars() { + match words.last_mut() { + Some(word) if !previous_space || ch.is_whitespace() => word.text.push(ch), + _ => words.push(Segment { + text: ch.to_string(), + utf16_start: utf16, + upright: false, + }), + } + previous_space = ch.is_whitespace(); + utf16 += ch.len_utf16(); + } + words +} + +fn segment_utf16(segment: &Segment, utf8: usize) -> usize { + Utf16Cursor::new(segment).at(utf8) +} + +/// Paragraph UTF-16 offsets of a segment's UTF-8 offsets, counted from the +/// last lookup so ascending lookups walk the text once. +struct Utf16Cursor<'a> { + segment: &'a Segment, + utf8: usize, + utf16: usize, +} + +impl<'a> Utf16Cursor<'a> { + fn new(segment: &'a Segment) -> Self { + Self { + segment, + utf8: 0, + utf16: segment.utf16_start, + } + } + + fn at(&mut self, utf8: usize) -> usize { + let utf8 = utf8.min(self.segment.text.len()); + if utf8 < self.utf8 { + *self = Self::new(self.segment); + } + self.utf16 += self.segment.text[self.utf8..utf8] + .chars() + .map(char::len_utf16) + .sum::(); + self.utf8 = utf8; + self.utf16 + } +} + +/// Cell builder for one span of a paragraph. +pub(super) struct SpanCells<'a> { + fonts: &'a Fonts<'a>, + span: &'a TextSpan, + paragraph: usize, + span_index: usize, + paint: usize, + /// UTF-16 offset of the span in its paragraph's layout text. + start: usize, + /// Typefaces of the span's own font, then emoji and the registered + /// fallback fonts. + typefaces: Vec, +} + +impl<'a> SpanCells<'a> { + pub(super) fn new( + fonts: &'a Fonts<'a>, + span: &'a TextSpan, + paragraph: usize, + span_index: usize, + paint: usize, + start: usize, + ) -> Self { + Self { + fonts, + span, + paragraph, + span_index, + paint, + start, + typefaces: resolve_typefaces( + &span_font_families(span, fonts.fallback_families), + fonts.provider, + ), + } + } + + fn shape(&self, text: &str, font_size: f32, upright: bool) -> Vec { + shape_with_typefaces( + text, + font_size, + &self.typefaces, + upright, + self.span.font_features, + self.fonts.fallback_mgr, + &self.span.language, + ) + } + + /// A cell of this span covering `start..end` (paragraph UTF-16), whose + /// ink fills its extent. + fn cell(&self, kind: CellKind, start: usize, end: usize, extent: f32) -> VerticalCell { + VerticalCell { + kind, + paragraph: self.paragraph, + span: self.span_index, + start, + end, + column: 0, + top: 0.0, + extent, + min_squeezed_extent: extent, + h_advance: self.span.font_size, + ink_top: 0.0, + ink_bottom: extent - self.span.letter_spacing, + paint: self.paint, + font_size: self.span.font_size, + decoration: self.span.text_decoration, + underline_side: self.span.text_underline_position.side(), + glyph_flow_shift: 0.0, + } + } + + /// Push the cells of the span's layout `text`. Runs are appended to + /// `runs`, which the cells index. + pub(super) fn push(&self, text: &str, runs: &mut Vec, cells: &mut Vec) { + let combine = self.span.text_combine_upright; + if combine == TextCombineUpright::All + && self.push_tate_chu_yoko(text, 0, all_min_scale(text), runs, cells) + { + return; + } + // `digits` merges each run of 1..=max digits into one upright cell. + let pieces = match combine.digits_max() { + Some(max) => split_digit_runs(text, max), + None => vec![(text.to_string(), 0, false)], + }; + for (piece, piece_start, tcy) in &pieces { + let min_scale = 1.0 / piece.chars().count().max(1) as f32; + if *tcy && self.push_tate_chu_yoko(piece, *piece_start, min_scale, runs, cells) { + continue; + } + let piece_base = self.start + piece_start; + let segments = segment_by_orientation(piece, self.span.text_orientation) + .into_iter() + .flat_map(split_sideways_words); + for segment in segments { + for mut run in self.shape(&segment.text, self.span.font_size, segment.upright) { + let run_index = runs.len(); + if segment.upright { + // A font may leave conjoining Jamo as separate glyph + // clusters. Group them by Unicode grapheme boundaries. + let boundaries: Vec = segment + .text + .grapheme_indices(true) + .map(|(at, _)| at) + .collect(); + for cluster in &mut run.clusters { + let at = boundaries + .partition_point(|at| *at <= *cluster as usize) + .saturating_sub(1); + *cluster = boundaries[at].max(run.utf8_range.start) as u32; + } + self.push_upright_clusters(&segment, piece_base, &run, run_index, cells); + } else { + cells.push(self.rotated_cell(&segment, piece_base, &mut run, run_index)); + } + runs.push(run); + } + } + } + } + + /// Compose `piece` (a whole TCY span or a digit run in one) into one + /// upright composite cell, with fallback runs side by side. Returns false + /// when the composite would squeeze below `min_scale`; the caller then uses + /// normal vertical layout. + fn push_tate_chu_yoko( + &self, + piece: &str, + piece_start: usize, + min_scale: f32, + runs: &mut Vec, + cells: &mut Vec, + ) -> bool { + // A lone upright CJK char (e.g. a ruby base span that inherits `all`) + // skips TCY, which would shrink it below its neighbours. + let mut chars = piece.chars(); + if matches!((chars.next(), chars.next()), (Some(ch), None) if is_upright_char(ch)) { + return false; + } + + let letter_spacing = self.span.letter_spacing; + let shaped = self.shape(piece, self.span.font_size, false); + if shaped.is_empty() { + return false; + } + let em = self.span.font_size.max(1.0); + // As in CSS, the composite ignores letter-spacing inside it: spacing + // only separates it from its neighbours down the column (`extent`). + let combined_advance: f32 = shaped.iter().map(|run| run.advance).sum(); + // Only a composite wider than the em is squeezed, and only across: + // glyphs keep their full height, as in JLREQ and CSS. + let h_scale = (em / combined_advance.max(1.0)).min(1.0); + if h_scale < min_scale { + return false; + } + + let extent = em + letter_spacing; + let kind = CellKind::TateChuYoko { + run_start: runs.len(), + run_count: shaped.len(), + h_scale, + }; + runs.extend(shaped); + let start = self.start + piece_start; + let end = start + piece.encode_utf16().count(); + let cell = VerticalCell { + h_advance: combined_advance * h_scale, + ink_bottom: em, + ..self.cell(kind, start, end, extent) + }; + cells.push(FlowCell::new( + cell, + None, + FlowScript::Upright, + letter_spacing, + )); + true + } + + /// One upright cell per glyph cluster of `run`, so combining sequences + /// stay in one cell. + fn push_upright_clusters( + &self, + segment: &Segment, + piece_base: usize, + run: &ShapedRun, + run_index: usize, + cells: &mut Vec, + ) { + 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)) + .flatten(); + + let mut utf16 = Utf16Cursor::new(segment); + for (glyph, count) in run.cluster_spans() { + let glyphs = &run.glyphs[glyph..glyph + count]; + let cluster_utf8 = run.clusters[glyph] as usize; + let next_cluster_utf8 = run + .clusters + .get(glyph + count) + .map_or(run.utf8_range.end, |c| *c as usize); + let ch = segment.text[cluster_utf8..].chars().next(); + let h_advance = match run.cluster_advance(glyph, count) { + 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); + // vpal tightens the cell and lifts the ink by the font's deltas. + let vpal_delta = vpal + .as_ref() + .and_then(|table| table.cluster_delta(glyphs, font_size)); + let (vpal_advance, vpal_flow_shift) = vpal_delta.unwrap_or((0.0, 0.0)); + let extent = vertical_advance + vpal_advance + letter_spacing; + + let synthetic_rotation = + ch.is_some_and(|ch| needs_rotated_vertical_fallback(run, glyph, count, ch)); + let ink = run.cluster_ink_bounds(glyph, count).map(|ink| { + if synthetic_rotation { + (ink.left, ink.right) + } else { + let offset = upright_baseline(&run.font, run.font.size()); + (ink.top + offset, ink.bottom + offset) + } + }); + let (ink_top, ink_bottom) = ink.unwrap_or((0.0, extent - letter_spacing)); + let (mut ink_top, mut ink_bottom) = + (ink_top + vpal_flow_shift, ink_bottom + vpal_flow_shift); + // Centre cl-05 and forced-upright Western ink in the em body, + // unless vpal already centres it. Western glyphs hung from a + // shared baseline leave uneven gaps: a descender meets the next + // cell's ascender (`gf`) while x-height letters float apart. + let glyph_flow_shift = if !synthetic_rotation + && vpal_delta.is_none() + && ink.is_some() + && ch.is_some_and(|c| is_centered_punctuation(c) || !is_upright_char(c)) + { + let shift = centered_flow_shift(ink_top, ink_bottom, extent - letter_spacing); + ink_top += shift; + ink_bottom += shift; + shift + } else { + vpal_flow_shift + }; + + let kind = if synthetic_rotation { + CellKind::SyntheticRotated { + run: run_index, + glyph, + count, + } + } else { + CellKind::Upright { + run: run_index, + glyph, + count, + } + }; + let start = piece_base + utf16.at(cluster_utf8); + let end = piece_base + utf16.at(next_cluster_utf8); + let cell = VerticalCell { + min_squeezed_extent: min_squeezed_extent(ch, extent, font_size, letter_spacing), + h_advance, + ink_top, + ink_bottom, + glyph_flow_shift, + ..self.cell(kind, start, end, extent) + }; + cells.push(FlowCell::new(cell, ch, FlowScript::Upright, letter_spacing)); + } + } + + /// One cell for a whole sideways Western run. Letter-spacing spreads its + /// glyphs down the column (post-rotation +x). + fn rotated_cell( + &self, + segment: &Segment, + piece_base: usize, + run: &mut ShapedRun, + run_index: usize, + ) -> FlowCell { + let letter_spacing = self.span.letter_spacing; + if self.span.layout_policy.normalizes_western_spaces() { + run.normalize_word_spaces(&segment.text, self.span.font_size); + } + let extent = run.spread_glyphs(letter_spacing); + let run_text = &segment.text[run.utf8_range.clone()]; + let script = FlowScript::Rotated { + starts_alphanumeric: run_text.chars().next().is_some_and(char::is_alphanumeric), + ends_alphanumeric: run_text + .chars() + .next_back() + .is_some_and(char::is_alphanumeric), + }; + let start = piece_base + segment_utf16(segment, run.utf8_range.start); + let end = piece_base + segment_utf16(segment, run.utf8_range.end); + let (ink_top, ink_bottom) = run + .ink_bounds() + .map_or((0.0, extent - letter_spacing), |ink| (ink.left, ink.right)); + let cell = VerticalCell { + // Unused for centring: rotated runs draw from `run.positions`. + h_advance: run.advance, + ink_top, + ink_bottom, + ..self.cell(CellKind::Rotated { run: run_index }, start, end, extent) + }; + FlowCell::new(cell, None, script, letter_spacing) + } +} + +/// Most characters a warichu piece split moves back to keep kinsoku. +const MAX_WARICHU_KINSOKU_SHIFT: usize = 4; + +/// One character of a warichu note. +#[derive(Debug, Clone, Copy)] +struct NoteChar { + ch: char, + /// Index of the owning span in the note's members. + member: usize, + /// UTF-8 offset in the member's text. + utf8: usize, + /// Paragraph UTF-16 offset. + utf16: usize, +} + +/// One span's share of a shaped warichu piece: its characters on each +/// sub-line and where they start along the piece. +struct PieceSegment { + member: usize, + chars: Range, + first_runs: Vec, + second_runs: Vec, + first_chars: usize, + first_top: f32, + first_extent: f32, + second_top: f32, + second_extent: f32, +} + +/// A shaped warichu piece: the part of a note set in one column. +struct ShapedPiece { + segments: Vec, + extent: f32, + font_size: f32, + letter_spacing: f32, +} + +/// Warichu (割注): consecutive warichu spans of a paragraph set as one note +/// of two half-size sub-lines (the first on the right, jlreq reading order). +/// The note breaks into pieces at paragraph UTF-16 `splits`, one per column. +/// Each piece emits one cell per span it touches; the first carries the +/// piece's flow extent and the rest share its box. +pub(super) struct WarichuNote<'a> { + members: Vec<(SpanCells<'a>, &'a str)>, + chars: Vec, +} + +impl<'a> WarichuNote<'a> { + pub(super) fn new(members: Vec<(SpanCells<'a>, &'a str)>) -> Self { + let mut chars = Vec::new(); + for (member, (span_cells, text)) in members.iter().enumerate() { + let mut utf16 = span_cells.start; + for (utf8, ch) in text.char_indices() { + chars.push(NoteChar { + ch, + member, + utf8, + utf16, + }); + utf16 += ch.len_utf16(); + } + } + Self { members, chars } + } + + /// True when `utf16` (paragraph offset) falls inside the note. + pub(super) fn contains(&self, utf16: usize) -> bool { + match (self.chars.first(), self.chars.last()) { + (Some(first), Some(last)) => { + (first.utf16..last.utf16 + last.ch.len_utf16()).contains(&utf16) + } + _ => false, + } + } + + /// Push the note's cells, or its spans' normal cells when it cannot be + /// set as warichu (fewer than two characters, or a sub-line that shapes + /// empty). + pub(super) fn push( + &self, + splits: &[usize], + runs: &mut Vec, + cells: &mut Vec, + ) { + if self.chars.len() >= 2 { + let shaped: Option> = self + .pieces(splits) + .into_iter() + .map(|piece| self.shape_piece(piece)) + .collect(); + if let Some(pieces) = shaped { + for piece in pieces { + self.push_piece(piece, runs, cells); + } + return; + } + } + for (span_cells, text) in &self.members { + span_cells.push(text, runs, cells); + } + } + + /// Paragraph UTF-16 offset where the piece starting at `piece_start` + /// should break so its first part fits in `room`, or None when no part + /// of at least one character per sub-line fits. The break keeps two + /// characters on each side and moves back to keep kinsoku. + pub(super) fn split_for_room( + &self, + piece_start: usize, + splits: &[usize], + room: f32, + ) -> Option { + let piece = self + .pieces(splits) + .into_iter() + .find(|piece| self.chars[piece.start].utf16 == piece_start)?; + if piece.len() < 4 { + return None; + } + let half_size = self.max_font_size(piece.clone()) * WARICHU_FONT_SCALE; + let estimate = 2 * (room / half_size.max(1.0)).floor().max(0.0) as usize; + let longest = (estimate + 2).min(piece.len() - 2); + let mut cut = longest; + while cut >= 2 { + let mut at = piece.start + cut; + let mut shift = 0; + while at > piece.start + 2 + && shift < MAX_WARICHU_KINSOKU_SHIFT + && (forbidden_at_line_start(self.chars[at].ch) + || forbidden_at_line_end(self.chars[at - 1].ch)) + { + at -= 1; + shift += 1; + } + let fits = self + .shape_piece(piece.start..at) + .is_some_and(|shaped| shaped.extent <= room + FIT_TOLERANCE); + if fits { + return Some(self.chars[at].utf16); + } + cut = (at - piece.start).min(cut) - 1; + } + None + } + + /// Char index ranges of the pieces cut at `splits`. + fn pieces(&self, splits: &[usize]) -> Vec> { + let mut pieces = Vec::new(); + let mut start = 0; + for (index, note_char) in self.chars.iter().enumerate().skip(1) { + if splits.contains(¬e_char.utf16) { + pieces.push(start..index); + start = index; + } + } + pieces.push(start..self.chars.len()); + pieces + } + + fn max_font_size(&self, chars: Range) -> f32 { + self.chars[chars] + .iter() + .map(|c| self.members[c.member].0.span.font_size) + .fold(0.0, f32::max) + } + + /// Member text of the chars `range`, which belong to one member. + fn member_text(&self, range: Range) -> &'a str { + let first = self.chars[range.start]; + let text = self.members[first.member].1; + let end = self + .chars + .get(range.end) + .filter(|next| next.member == first.member) + .map_or(text.len(), |next| next.utf8); + &text[first.utf8..end] + } + + /// Shape one sub-line part of a member; empty parts shape to no runs. + fn shape_part(&self, member: usize, range: Range) -> Option<(Vec, f32)> { + if range.is_empty() { + return Some((Vec::new(), 0.0)); + } + let span_cells = &self.members[member].0; + let half_size = span_cells.span.font_size * WARICHU_FONT_SCALE; + let runs = span_cells.shape(self.member_text(range), half_size, true); + if runs.is_empty() { + return None; + } + let advance = runs.iter().map(|r| r.advance).sum(); + Some((runs, advance)) + } + + fn shape_piece(&self, piece: Range) -> Option { + let text: String = self.chars[piece.clone()].iter().map(|c| c.ch).collect(); + let split = piece.start + warichu_split_chars(&text); + let mut segments = Vec::new(); + let (mut first_cursor, mut second_cursor) = (0.0f32, 0.0f32); + let mut index = piece.start; + while index < piece.end { + let member = self.chars[index].member; + let end = (index..piece.end) + .find(|&i| self.chars[i].member != member) + .unwrap_or(piece.end); + let first = index.min(split)..end.min(split); + let second = index.max(split)..end.max(split); + let first_chars = self.chars[first.clone()] + .iter() + .map(|c| c.ch.len_utf16()) + .sum(); + let (first_runs, first_extent) = self.shape_part(member, first)?; + let (second_runs, second_extent) = self.shape_part(member, second)?; + segments.push(PieceSegment { + member, + chars: index..end, + first_runs, + second_runs, + first_chars, + first_top: first_cursor, + first_extent, + second_top: second_cursor, + second_extent, + }); + first_cursor += first_extent; + second_cursor += second_extent; + index = end; + } + let letter_spacing = self.chars[piece.clone()] + .iter() + .map(|c| self.members[c.member].0.span.letter_spacing) + .fold(0.0, f32::max); + Some(ShapedPiece { + segments, + extent: first_cursor.max(second_cursor) + letter_spacing, + font_size: self.max_font_size(piece), + letter_spacing, + }) + } + + fn push_piece(&self, piece: ShapedPiece, runs: &mut Vec, cells: &mut Vec) { + for (index, segment) in piece.segments.into_iter().enumerate() { + let span_cells = &self.members[segment.member].0; + let kind = CellKind::Warichu { + run_start: runs.len(), + run_count: segment.first_runs.len() + segment.second_runs.len(), + first_count: segment.first_runs.len(), + first_chars: segment.first_chars, + first_top: segment.first_top, + first_extent: segment.first_extent, + second_top: segment.second_top, + second_extent: segment.second_extent, + }; + runs.extend(segment.first_runs); + runs.extend(segment.second_runs); + let last = self.chars[segment.chars.end - 1]; + let start = self.chars[segment.chars.start].utf16; + let end = last.utf16 + last.ch.len_utf16(); + // Two half-em sub-columns fill the em, the default `h_advance`. + let cell = VerticalCell { + font_size: piece.font_size, + ..span_cells.cell(kind, start, end, piece.extent) + }; + let mut flow = FlowCell::new(cell, None, FlowScript::Upright, piece.letter_spacing); + if index > 0 { + flow.shares_previous_box = true; + flow.cell.extent = 0.0; + flow.cell.min_squeezed_extent = 0.0; + } + cells.push(flow); + } + } +} + +#[cfg(test)] +mod tests { + use super::super::layout::VerticalCell; + use super::super::test_support::*; + use super::*; + use crate::shapes::text_shaping::WESTERN_WORD_SPACING_EM; + use crate::shapes::{Fill, SolidColor, TextDecoration, TextOrientation}; + use skia_safe as skia; + + fn vpal_content(text: &str, font_features: FontFeatures) -> crate::shapes::TextContent { + spans_content( + vec![TextSpan { + font_features, + ..make_span(text) + }], + 1000.0, + ) + } + + #[test] + fn missing_vertical_alternate_uses_a_character_granular_rotated_cell() { + let mut content = make_content(&["“"], 1000.0); + content.paragraphs_mut()[0].children_mut()[0].text_orientation = TextOrientation::Upright; + let layout = layout_with(&provider(TEST_FONT), &content); + assert_eq!(layout.cells.len(), 1); + assert!(matches!( + layout.cells[0].kind, + CellKind::SyntheticRotated { .. } + )); + } + + #[test] + fn upright_narrow_latin_reserves_a_full_em_without_vmtx() { + // Under text-orientation: upright, Latin letters stand upright. The + // test face has no `vmtx`, so each letter advances a full em. + let em = 20.0; + let mut content = make_content(&["ab"], 1000.0); + content.paragraphs_mut()[0].children_mut()[0].text_orientation = TextOrientation::Upright; + let layout = layout_with(&provider(TEST_FONT), &content); + assert_eq!(layout.cells.len(), 2, "one upright cell per Latin letter"); + for (index, cell) in layout.cells.iter().enumerate() { + assert!( + matches!(cell.kind, CellKind::Upright { .. }), + "cell {index} should be an upright Latin letter" + ); + assert!( + (cell.extent - em).abs() < 0.5, + "upright Latin cell {index} reserves a full em, got {}", + cell.extent + ); + } + // The letters advance a full em down the column, so they never overlap. + assert!( + layout.cells[1].top - layout.cells[0].top >= em - 0.5, + "successive upright letters are one em apart, got {}", + layout.cells[1].top - layout.cells[0].top + ); + } + + #[test] + fn upright_latin_ink_is_centred_in_its_cell() { + // A descender followed by an ascender (`gf`) must not touch: each + // letter's ink sits in the middle of its em cell. + let mut content = make_content(&["gf"], 1000.0); + content.paragraphs_mut()[0].children_mut()[0].text_orientation = TextOrientation::Upright; + let layout = layout_with(&provider(TEST_FONT), &content); + for cell in &layout.cells { + let ink_middle = (cell.ink_top + cell.ink_bottom) / 2.0; + assert!( + (ink_middle - cell.extent / 2.0).abs() < 0.01, + "ink middle {ink_middle} in a {} cell", + cell.extent + ); + } + let (g, f) = (&layout.cells[0], &layout.cells[1]); + let gap = f.top + f.ink_top - (g.top + g.ink_bottom); + assert!(gap > 0.2 * EM, "g/f ink gap {gap}"); + } + + #[test] + fn warichu_span_composes_two_half_size_sub_lines() { + let content = warichu_content("ABCD", 400.0); + let layout = layout_content(&content, 400.0); + assert_eq!(layout.cells.len(), 1, "the whole span is one composite"); + let cell = &layout.cells[0]; + let CellKind::Warichu { + run_count, + first_count, + .. + } = cell.kind + else { + panic!("expected a warichu cell"); + }; + assert!(first_count >= 1 && run_count > first_count); + // Two half-size chars per sub-line: about one em, not the four em of + // normal layout. + assert!( + cell.extent < 2.0 * 20.0, + "two half-size sub-lines take about one em, got {}", + cell.extent + ); + assert!( + (cell.h_advance - 20.0).abs() < 0.001, + "the composite fills the full em width" + ); + assert_eq!(cell.start, 0); + assert_eq!(cell.end, 4); + } + + #[test] + fn warichu_single_char_keeps_normal_layout() { + let content = warichu_content("A", 400.0); + let layout = layout_content(&content, 400.0); + assert!(layout + .cells + .iter() + .all(|c| !matches!(c.kind, CellKind::Warichu { .. }))); + } + + #[test] + fn tate_chu_yoko_span_becomes_one_upright_cell() { + let mut content = make_content_with_spans(&["20", "年"], 200.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::All; + + let layout = layout_content(&content, 200.0); + + assert_eq!(layout.cells.len(), 2); + assert!(matches!(layout.cells[0].kind, CellKind::TateChuYoko { .. })); + assert_eq!(layout.cells[0].start, 0); + assert_eq!(layout.cells[0].end, 2); + assert_eq!(layout.cells[0].extent, 20.0); + assert_eq!(layout.cells[1].start, 2); + } + + #[test] + fn tate_chu_yoko_compresses_only_the_width() { + // JLREQ Fig 26: two digits sit at full height in one em; only a run + // wider than the em is squeezed, and only horizontally. + let mut content = make_content_with_spans(&["20", "2026"], 200.0); + for span in content.paragraphs_mut()[0].children_mut() { + span.text_combine_upright = TextCombineUpright::All; + } + + let layout = layout_content(&content, 200.0); + let scales: Vec<(f32, f32)> = layout + .cells + .iter() + .filter_map(|cell| match cell.kind { + CellKind::TateChuYoko { h_scale, .. } => Some((h_scale, cell.h_advance)), + _ => None, + }) + .collect(); + + assert_eq!(scales.len(), 2); + assert_eq!(scales[0].0, 1.0, "two digits fit the em unscaled"); + assert!(scales[1].0 < 1.0, "four digits are squeezed"); + assert!((scales[1].1 - EM).abs() < 0.01, "into exactly one em"); + } + + #[test] + fn tate_chu_yoko_all_keeps_single_upright_ruby_base_at_full_size() { + let mut content = make_content_with_spans(&["く", "くくく"], 400.0); + let spans = content.paragraphs_mut()[0].children_mut(); + spans[0].ruby = "あ".to_string(); + for span in spans { + span.text_combine_upright = TextCombineUpright::All; + } + + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let ruby_base = layout + .cells + .iter() + .find(|cell| cell.span == 0) + .expect("ruby base cell"); + let following_base = layout + .cells + .iter() + .find(|cell| cell.span == 1) + .expect("following base cell"); + + assert!(matches!(ruby_base.kind, CellKind::Upright { .. })); + assert!(!layout.ruby_cells.is_empty(), "ruby annotation is present"); + assert_eq!(ruby_base.font_size, following_base.font_size); + assert!(layout + .cells + .iter() + .all(|cell| !matches!(cell.kind, CellKind::TateChuYoko { .. }))); + } + + #[test] + fn tate_chu_yoko_composes_covered_run_to_single_cell() { + // A CJK-covering face keeps the whole marked span in one upright + // composite cell (run_count >= 1); the next span is a separate cell. + let mut content = make_content_with_spans(&["くく", "あ"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::All; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let CellKind::TateChuYoko { run_count, .. } = layout.cells[0].kind else { + panic!("expected a Tate-chu-yoko cell"); + }; + assert!(run_count >= 1, "composite references at least one run"); + assert_eq!(layout.cells[0].start, 0); + assert_eq!(layout.cells[0].end, 2); + assert_eq!(layout.cells[1].start, 2); + } + + #[test] + fn tate_chu_yoko_wide_run_falls_back_to_normal_layout() { + // A run far wider than the em would scale below MIN_TCY_SCALE, so it + // gets normal layout. + let mut content = make_content_with_spans(&["123456789"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::All; + let layout = layout_content(&content, 400.0); + assert!( + !layout + .cells + .iter() + .any(|c| matches!(c.kind, CellKind::TateChuYoko { .. })), + "an over-wide run must not become a tate-chu-yoko composite" + ); + } + + #[test] + fn tate_chu_yoko_all_squeezes_a_short_run_like_digits() { + // Four wide characters need a scale under MIN_TCY_SCALE. `digits` + // squeezes a four-digit run to 1/4, so `all` must combine a run of + // the same length instead of turning it sideways. + let mut content = make_content_with_spans(&["WWWW"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::All; + let layout = layout_content(&content, 400.0); + let scales: Vec = layout + .cells + .iter() + .filter_map(|c| match c.kind { + CellKind::TateChuYoko { h_scale, .. } => Some(h_scale), + _ => None, + }) + .collect(); + assert_eq!(scales.len(), 1, "one composite for the whole run"); + assert!( + scales[0] < MIN_TCY_SCALE, + "the test run needs a deep squeeze" + ); + } + + #[test] + fn split_digit_runs_honours_the_max_parameter() { + // max 2: runs of up to two digits combine. + let pieces = split_digit_runs("平成31年123日", 2); + let flags: Vec<(&str, bool)> = pieces + .iter() + .map(|(text, _, tcy)| (text.as_str(), *tcy)) + .collect(); + assert_eq!( + flags, + vec![ + ("平成", false), + ("31", true), + ("年", false), + ("123", false), + ("日", false), + ] + ); + // max 3 admits the three-digit run. + let pieces = split_digit_runs("平成31年123日", 3); + assert!(pieces.iter().any(|(text, _, tcy)| text == "123" && *tcy)); + } + + #[test] + fn split_digit_runs_marks_runs_of_up_to_four_digits() { + let pieces = split_digit_runs("平成31年12345日5", 4); + let flags: Vec<(&str, usize, bool)> = pieces + .iter() + .map(|(text, start, tcy)| (text.as_str(), *start, *tcy)) + .collect(); + assert_eq!( + flags, + vec![ + ("平成", 0, false), + ("31", 2, true), + ("年", 4, false), + ("12345", 5, false), + ("日", 10, false), + ("5", 11, true), + ] + ); + } + + #[test] + fn split_digit_runs_recognizes_full_width_digits() { + let pieces = split_digit_runs("2026夏号", 4); + let flags: Vec<(&str, bool)> = pieces + .iter() + .map(|(text, _, tcy)| (text.as_str(), *tcy)) + .collect(); + assert_eq!(flags, vec![("2026", true), ("夏号", false)]); + } + + #[test] + fn tate_chu_yoko_digits_combines_only_digit_runs() { + let mut content = make_content_with_spans(&["あ31く"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::Digits; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let tcy: Vec<&VerticalCell> = layout + .cells + .iter() + .filter(|c| matches!(c.kind, CellKind::TateChuYoko { .. })) + .collect(); + assert_eq!(tcy.len(), 1, "exactly the digit run combines"); + assert_eq!(tcy[0].start, 1); + assert_eq!(tcy[0].end, 3); + // The surrounding characters keep their own cells in text order. + let starts: Vec = layout.cells.iter().map(|c| c.start).collect(); + let mut sorted = starts.clone(); + sorted.sort_unstable(); + assert_eq!(starts, sorted, "cells stay in text order"); + assert_eq!(layout.cells.len(), 3); + } + + #[test] + fn tate_chu_yoko_digits_sets_a_lone_digit_upright() { + let mut content = make_content_with_spans(&["2月5日"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::Digits; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let tcy: Vec<(usize, usize)> = layout + .cells + .iter() + .filter(|c| matches!(c.kind, CellKind::TateChuYoko { .. })) + .map(|c| (c.start, c.end)) + .collect(); + assert_eq!(tcy, vec![(0, 1), (2, 3)]); + } + + #[test] + fn tate_chu_yoko_digits_combines_four_ascii_digits() { + let mut content = make_content_with_spans(&["2025年"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::Digits; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let tcy = layout + .cells + .iter() + .find(|cell| matches!(cell.kind, CellKind::TateChuYoko { .. })) + .expect("four-digit run combines"); + assert_eq!(tcy.start, 0); + assert_eq!(tcy.end, 4); + } + + #[test] + fn tate_chu_yoko_digits_combines_full_width_unicode_digits() { + let mut content = make_content_with_spans(&["2026年"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::Digits; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let tcy = layout + .cells + .iter() + .find(|cell| matches!(cell.kind, CellKind::TateChuYoko { .. })) + .expect("full-width digit run combines"); + assert_eq!(tcy.start, 0); + assert_eq!(tcy.end, 4); + } + + #[test] + fn letter_spacing_extends_upright_cells() { + // Each upright cluster gains `letter_spacing` of flow advance; the + // centring width (`h_advance`) stays the same. + let plain = layout_content(&spaced_content("あい", 0.0), 1000.0); + let spaced = layout_content(&spaced_content("あい", 5.0), 1000.0); + assert_eq!(plain.cells.len(), spaced.cells.len()); + for i in 0..plain.cells.len() { + assert!( + (spaced.cells[i].extent - plain.cells[i].extent - 5.0).abs() < 0.01, + "cell {i} extent grows by letter-spacing" + ); + assert!( + (spaced.cells[i].h_advance - plain.cells[i].h_advance).abs() < 0.01, + "cell {i} centring width is unchanged" + ); + } + assert!( + spaced.cells[1].top - plain.cells[1].top - 5.0 > -0.01, + "the second cell is pushed down by the spacing" + ); + } + + #[test] + fn letter_spacing_spreads_rotated_run() { + // A sideways Latin run grows by `letter_spacing` per glyph and its + // glyphs shift apart along the (post-rotation) column axis. + let plain = layout_content(&spaced_content("AB", 0.0), 1000.0); + let spaced = layout_content(&spaced_content("AB", 5.0), 1000.0); + let plain_cell = plain + .cells + .iter() + .find(|c| matches!(c.kind, CellKind::Rotated { .. })) + .expect("a rotated cell"); + let spaced_cell = spaced + .cells + .iter() + .find(|c| matches!(c.kind, CellKind::Rotated { .. })) + .expect("a rotated cell"); + let (CellKind::Rotated { run: plain_run }, CellKind::Rotated { run: spaced_run }) = + (plain_cell.kind, spaced_cell.kind) + else { + unreachable!(); + }; + let glyphs = spaced.runs[spaced_run].glyphs.len(); + assert!(glyphs >= 2, "AB shapes to at least two glyphs"); + assert!( + (spaced_cell.extent - plain_cell.extent - 5.0 * glyphs as f32).abs() < 0.01, + "rotated extent grows by letter_spacing * glyph count" + ); + let plain_gap = plain.runs[plain_run].positions[1].x - plain.runs[plain_run].positions[0].x; + let spaced_gap = + spaced.runs[spaced_run].positions[1].x - spaced.runs[spaced_run].positions[0].x; + assert!( + (spaced_gap - plain_gap - 5.0).abs() < 0.01, + "adjacent glyph gap grows by letter-spacing" + ); + } + + #[test] + fn letter_spacing_stays_outside_a_tate_chu_yoko_composite() { + // CSS combines the text ignoring letter-spacing. Counting it in the + // composite width set the digits off centre, left of the kanji. + let tcy_cell = |letter_spacing: f32| { + let mut content = spaced_content("く12く", letter_spacing); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::Digits; + let layout = layout_content(&content, 1000.0); + let cell = layout + .cells + .iter() + .find(|c| matches!(c.kind, CellKind::TateChuYoko { .. })) + .cloned() + .expect("the digit run combines"); + let CellKind::TateChuYoko { run_start, .. } = cell.kind else { + unreachable!() + }; + let positions = layout.runs[run_start].positions.clone(); + (cell, positions) + }; + let (plain, plain_positions) = tcy_cell(0.0); + let (spaced, spaced_positions) = tcy_cell(5.0); + assert!( + (spaced.h_advance - plain.h_advance).abs() < 0.01, + "the composite width ignores letter-spacing" + ); + assert_eq!( + plain_positions, spaced_positions, + "glyphs keep their places" + ); + assert!( + (spaced.extent - plain.extent - 5.0).abs() < 0.01, + "letter-spacing still follows the composite down the column" + ); + } + + // A tiny Noto Sans JP subset with `vmtx`/`vhea`: U+3031 (〱, vertical kana + // repeat mark) advances 2em vertically and 1em horizontally; + // U+3042/U+304F are symmetric controls. + + #[test] + fn vertical_advance_uses_vmtx() { + // 〱 flows down the column by its 2em vertical advance (vmtx), not + // its 1em horizontal width; the glyph still centres on the 1em width. + let content = make_content(&["〱"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let cell = &layout.cells[0]; + let CellKind::Upright { run, glyph, count } = cell.kind else { + panic!("expected an upright cell"); + }; + let horizontal: f32 = layout.runs[run].advances[glyph..glyph + count].iter().sum(); + assert!( + (horizontal - 20.0).abs() < 0.5, + "horizontal advance ~1em, got {horizontal}" + ); + assert!( + (cell.extent - 40.0).abs() < 0.5, + "vertical extent ~2em from vmtx, got {}", + cell.extent + ); + assert!( + (cell.h_advance - 20.0).abs() < 0.5, + "h_advance stays ~1em, got {}", + cell.h_advance + ); + } + + #[test] + fn vertical_advance_symmetric_glyph_unchanged() { + // A glyph whose vmtx equals its hmtx keeps extent == h_advance. + let content = make_content(&["く"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let cell = &layout.cells[0]; + assert!( + (cell.extent - 20.0).abs() < 0.5, + "extent ~1em, got {}", + cell.extent + ); + assert!( + (cell.h_advance - cell.extent).abs() < 0.01, + "symmetric glyph: extent == h_advance" + ); + } + + // Subset of Noto Sans JP carrying GSUB `vert` and GPOS `vpal`: the + // vertical alternates of 、。「」 halve their vertical advances (「 also + // lifts its ink by 481 units) and the あ/く alternates tighten by + // 58/60 units with small placement lifts. + + #[test] + fn native_vertical_alternates_are_not_synthetically_rotated() { + let layout = layout_with( + &provider(VPAL_TEST_FONT), + &vpal_content("「」、。", FontFeatures::None), + ); + assert_eq!(layout.cells.len(), 4); + assert!(layout + .cells + .iter() + .all(|cell| matches!(cell.kind, CellKind::Upright { .. }))); + } + + #[test] + fn vpal_tightens_upright_kana() { + let provider = provider(VPAL_TEST_FONT); + let plain = layout_with(&provider, &vpal_content("あ", FontFeatures::None)); + let tight = layout_with(&provider, &vpal_content("あ", FontFeatures::Vpal)); + assert!( + (plain.cells[0].extent - 20.0).abs() < 0.01, + "without vpal あ keeps its full em, got {}", + plain.cells[0].extent + ); + // あ's vertical alternate carries YAdvance -58, YPlacement 39. + let expected = 20.0 * (1000.0 - 58.0) / 1000.0; + assert!( + (tight.cells[0].extent - expected).abs() < 0.01, + "vpal advance delta tightens the extent, got {}", + tight.cells[0].extent + ); + let expected_shift = -20.0 * 39.0 / 1000.0; + assert!( + (tight.cells[0].glyph_flow_shift - expected_shift).abs() < 0.01, + "vpal placement lifts the drawn ink, got {}", + tight.cells[0].glyph_flow_shift + ); + } + + #[test] + fn vpal_opening_bracket_uses_font_placement() { + // Mid-line, an opening bracket keeps its leading aki. Under vpal its + // placement comes from the font's YPlacement (481 units), not a + // synthetic sequence shed. + let provider = provider(VPAL_TEST_FONT); + let shed = layout_with(&provider, &vpal_content("あ「あ", FontFeatures::None)); + let vpal = layout_with(&provider, &vpal_content("あ「あ", FontFeatures::Vpal)); + assert_eq!( + shed.cells[1].glyph_flow_shift, 0.0, + "without vpal the preferred aki needs no synthetic shift, got {}", + shed.cells[1].glyph_flow_shift + ); + let expected = -20.0 * 481.0 / 1000.0; + assert!( + (vpal.cells[1].glyph_flow_shift - expected).abs() < 0.01, + "with vpal 「 lifts by the font's placement delta, got {}", + vpal.cells[1].glyph_flow_shift + ); + assert!( + (vpal.cells[1].extent - 10.0).abs() < 0.01, + "「 is half-width under vpal, got {}", + vpal.cells[1].extent + ); + } + + #[test] + fn centered_punctuation_shift_centres_ink_in_em_body() { + assert!(is_centered_punctuation('・')); + assert!(is_centered_punctuation(':')); + assert!(is_centered_punctuation(';')); + assert!(!is_centered_punctuation('あ')); + // Ink at 14..18 in a 20 body: midpoint 16 moves to 10, so shift == -6. + let shift = centered_flow_shift(14.0, 18.0, 20.0); + assert!((shift + 6.0).abs() < 1e-4, "expected -6, got {shift}"); + // Already-centred ink needs no shift. + assert!(centered_flow_shift(8.0, 12.0, 20.0).abs() < 1e-4); + } + + #[test] + fn vertical_base_text_switches_font_inside_an_upright_segment() { + let content = make_content(&["あ、く"], 400.0); + let provider = provider_with_fallback(VMTX_TEST_FONT, VPAL_TEST_FONT); + let layout = layout_with_fallback(&provider, &content, 400.0, &["fallback".to_string()]); + + assert_eq!(layout.cells.len(), 3); + let run_ids: Vec = layout + .cells + .iter() + .map(|cell| match cell.kind { + CellKind::Upright { run, .. } => layout.runs[run].font.typeface().unique_id(), + _ => panic!("expected upright fallback cells"), + }) + .collect(); + assert_eq!(run_ids[0], run_ids[2]); + assert_ne!(run_ids[0], run_ids[1]); + let CellKind::Upright { run, glyph, .. } = layout.cells[1].kind else { + unreachable!(); + }; + assert_ne!(layout.runs[run].glyphs[glyph], 0); + } + + #[test] + fn layout_mixed_text_has_rotated_run() { + let content = make_content(&["あAB1い"], 1000.0); + let layout = layout_content(&content, 1000.0); + assert!(layout + .cells + .iter() + .any(|c| matches!(c.kind, CellKind::Rotated { .. }))); + // The rotated run covers the Latin range 1..4 (UTF-16). + let rotated = layout + .cells + .iter() + .find(|c| matches!(c.kind, CellKind::Rotated { .. })) + .unwrap(); + assert_eq!(rotated.start, 1); + assert_eq!(rotated.end, 4); + } + + #[test] + fn western_word_space_uses_one_third_em() { + let without_space = layout_content(&make_content(&["ab"], 1000.0), 1000.0); + let with_space = layout_content(&make_content(&["a b"], 1000.0), 1000.0); + assert_eq!(without_space.cells.len(), 1); + assert_eq!(with_space.cells.len(), 2, "one sideways cell per word"); + let extent = |layout: &super::super::layout::VerticalLayout| { + layout.cells.iter().map(|c| c.extent).sum::() + }; + let added = extent(&with_space) - extent(&without_space); + assert!( + (added - 20.0 * WESTERN_WORD_SPACING_EM).abs() < 0.01, + "word space should add one third em, got {added}" + ); + } + + #[test] + fn cells_carry_font_size_and_decoration() { + let content = decorated_content("あい", TextDecoration::UNDERLINE); + let layout = layout_content(&content, 1000.0); + assert!(!layout.cells.is_empty()); + for cell in &layout.cells { + assert_eq!(cell.font_size, 20.0); + assert_eq!(cell.decoration, Some(TextDecoration::UNDERLINE)); + } + } + + #[test] + fn mixed_spans_preserve_fill_color_and_opacity() { + let mut content = make_content_with_spans(&["あ", "A", "い"], 1000.0); + let colors = [ + skia::Color::from_argb(255, 255, 0, 0), + skia::Color::from_argb(128, 0, 255, 0), + skia::Color::from_argb(64, 0, 0, 255), + ]; + for (span, color) in content.paragraphs_mut()[0] + .children_mut() + .iter_mut() + .zip(colors) + { + span.fills = vec![Fill::Solid(SolidColor(color))]; + } + + let layout = layout_content(&content, 1000.0); + assert_eq!(layout.paints.len(), colors.len()); + for (paint, color) in layout.paints.iter().zip(colors) { + assert_eq!(paint.color(), color); + } + assert!(layout + .cells + .iter() + .any(|cell| matches!(cell.kind, CellKind::Rotated { .. }))); + } +} diff --git a/render-wasm/src/shapes/text_vertical/flow/mod.rs b/render-wasm/src/shapes/text_vertical/flow/mod.rs new file mode 100644 index 0000000000..96056f40b8 --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/flow/mod.rs @@ -0,0 +1,1730 @@ +// Column planning and JLREQ spacing along the vertical flow. These passes run +// on a paragraph's `FlowCell`s before placement: they adjust cell extents +// (aki, oikomi, inter-script spacing) and pick column breaks (kinsoku, +// burasage, oidashi). The other policies plan in `pairwise`. + +use crate::shapes::jlreq::{ + aki_class, break_allowed_between, classify, pair_rule, punctuation_aki_sheds, AkiShed, + JlreqClass, +}; +use crate::shapes::jlreq::{forbidden_at_line_end, forbidden_at_line_start, MAX_KINSOKU_SHIFT}; +use crate::shapes::text_allocation::{ + capped_equal_allocations, jlreq_expansion_gap, jlreq_ordered_expansion, +}; +use crate::shapes::{LineAdjustment, TextAlign}; + +use super::layout::{CellKind, VerticalCell}; + +mod pairwise; + +pub(super) use pairwise::{apply_pairwise_spacing, pairwise_aligned_tops, plan_pairwise_columns}; + +const INTER_SCRIPT_SPACING_EM: f32 = 0.25; + +/// Amounts below this are treated as zero by the spacing passes. +const EPSILON: f32 = 0.0001; + +/// Overflow below this still fits a column: shape heights carry float noise +/// (a 19em box can measure 417.99998px for 418px of text). +pub(super) const FIT_TOLERANCE: f32 = 0.01; + +/// An item placed along the column. `ch` is set for single-character cells +/// and drives kinsoku at column breaks; rotated runs have none and never +/// split. +#[derive(Debug, Clone, Copy, PartialEq)] +pub(super) struct FlowItem { + pub extent: f32, + pub ch: Option, + /// Same source char as the previous item (text transform); keep together. + pub keep_with_previous: bool, + /// Continues the previous item's word across a style or font run + /// boundary; a break here would split the word. + pub joins_previous: bool, + /// Trailing aki the item sheds when it ends a column: a closing bracket + /// there is set half width (JLREQ §3.1.4), so this part may pass the + /// column end while planning. + pub end_slack: f32, +} + +#[derive(Debug, Clone, Copy)] +pub(super) enum FlowScript { + Upright, + Rotated { + starts_alphanumeric: bool, + ends_alphanumeric: bool, + }, +} + +/// A cell of one paragraph awaiting column placement, with the facts the +/// spacing passes and the column planner need. +#[derive(Clone)] +pub(super) struct FlowCell { + pub cell: VerticalCell, + /// Character of a single-character cell; classifies it for aki and kinsoku. + pub ch: Option, + pub keep_with_previous: bool, + pub script: FlowScript, + /// Letter-spacing included at the end of the cell's extent. + pub trailing_spacing: f32, + /// A later span's cell of the previous cell's warichu piece: it has no + /// flow extent and takes the piece's box after placement. + pub shares_previous_box: bool, +} + +impl FlowCell { + pub(super) fn new( + cell: VerticalCell, + ch: Option, + script: FlowScript, + trailing_spacing: f32, + ) -> Self { + Self { + cell, + ch, + keep_with_previous: false, + script, + trailing_spacing, + shares_previous_box: false, + } + } + + fn item(&self) -> FlowItem { + FlowItem { + extent: self.cell.extent, + ch: self.ch, + keep_with_previous: self.keep_with_previous || self.shares_previous_box, + joins_previous: false, + end_slack: self.end_slack(), + } + } + + /// Trailing aki of a closing bracket, which it sheds at a column end. + fn end_slack(&self) -> f32 { + let closing = self + .ch + .is_some_and(|ch| classify(ch) == JlreqClass::ClosingBracket); + if closing { + (self.cell.extent - (0.5 * self.cell.font_size + self.trailing_spacing)).max(0.0) + } else { + 0.0 + } + } + + /// Shrink the cell to a half-em frame plus its letter-spacing. Opening + /// punctuation has its ink in the trailing half of the em, so + /// `pull_glyph` lifts it into the shrunk cell. Returns false when the + /// cell is already that narrow. + fn shed_to_half_em(&mut self, pull_glyph: bool) -> bool { + let target = 0.5 * self.cell.font_size + self.trailing_spacing; + if target >= self.cell.extent { + return false; + } + if pull_glyph { + self.cell.glyph_flow_shift -= self.cell.extent - target; + } + self.cell.extent = target; + true + } +} + +pub(super) fn flow_items(cells: &[FlowCell]) -> Vec { + let mut items: Vec = cells.iter().map(FlowCell::item).collect(); + for (index, pair) in cells.windows(2).enumerate() { + items[index + 1].joins_previous = continues_word(&pair[0], &pair[1]); + } + items +} + +/// True when two sideways runs meet inside a word: a letter or digit on +/// both sides of a style or font boundary. +fn continues_word(previous: &FlowCell, next: &FlowCell) -> bool { + matches!( + (previous.script, next.script), + ( + FlowScript::Rotated { + ends_alphanumeric: true, + .. + }, + FlowScript::Rotated { + starts_alphanumeric: true, + .. + } + ) + ) +} + +/// Punctuation that may hang past the column bottom (ぶら下げ / burasage): the +/// ideographic and full-width comma and period. A mark that would overflow +/// the column hangs into the margin. +fn can_hang(c: char) -> bool { + matches!(c, '、' | '。' | ',' | '.') +} + +/// True when `item` would overflow a column already filled to `cursor`. +fn overflows(cursor: f32, item: &FlowItem, max_height: f32) -> bool { + cursor > 0.0 + && cursor + item.extent - item.end_slack > max_height + FIT_TOLERANCE + && !item.ch.is_some_and(can_hang) +} + +/// Why a column may not break before `items[at]`. +#[derive(Debug, Clone, Copy, PartialEq)] +enum BreakBlock { + /// Kinsoku: a forbidden-at-line-start or -end character. + Kinsoku, + /// A word, numeral run, identical inseparable pair or numeral with its + /// unit (JLREQ §3.1.10). + Unbreakable, +} + +fn break_block(items: &[FlowItem], at: usize) -> Option { + let (previous, next) = (&items[at - 1], &items[at]); + if next.joins_previous { + return Some(BreakBlock::Unbreakable); + } + let kinsoku = next.ch.is_some_and(forbidden_at_line_start) + || previous.ch.is_some_and(forbidden_at_line_end); + if kinsoku { + return Some(BreakBlock::Kinsoku); + } + match (previous.ch, next.ch) { + (Some(before), Some(after)) if !break_allowed_between(before, after) => { + Some(BreakBlock::Unbreakable) + } + _ => None, + } +} + +/// First item of the next column when `items[i]` overflows the column +/// starting at `column_start`. Kinsoku: no forbidden-at-line-end char at the +/// column bottom and no forbidden-at-line-start char at the next column top; +/// offending predecessors move to the new column (oidashi), at most +/// `MAX_KINSOKU_SHIFT` of them. Words and other unbreakable sequences move +/// whole when they fit a column. A sequence that cannot move without +/// emptying the column or overflowing the next one breaks where it +/// overflows. `None` +/// keeps `items[i]` in the column when it closes an atomic composite (group +/// ruby or an expanded source scalar) that began at the column head. +fn column_break( + items: &[FlowItem], + column_start: usize, + i: usize, + max_height: f32, +) -> Option { + let skip_kept = |mut index: usize| { + while index > column_start && items[index].keep_with_previous { + index -= 1; + } + index + }; + + let natural = skip_kept(i); + if natural == column_start && items[i].keep_with_previous { + return None; + } + let mut break_at = natural; + let mut kinsoku_shifts = 0; + while break_at > column_start { + match break_block(items, break_at) { + None => break, + Some(BreakBlock::Kinsoku) if kinsoku_shifts >= MAX_KINSOKU_SHIFT => break, + Some(block) => { + if block == BreakBlock::Kinsoku { + kinsoku_shifts += 1; + } + break_at = skip_kept(break_at - 1); + } + } + } + // Drop the shift if it would empty this column or overflow the new one: + // a kinsoku violation is better than overflowing the wrap budget. The + // moved items stay with what follows up to the next allowed break, so + // measure that whole unit: a forbidden run longer than a column would + // otherwise be pushed out again at every column. + let unit_end = (i + 1..items.len()) + .find(|&next| break_block(items, next).is_none()) + .unwrap_or(items.len()); + let unit_extent: f32 = items[break_at..unit_end] + .iter() + .filter(|it| !it.ch.is_some_and(can_hang)) + .map(|it| it.extent) + .sum(); + if break_at == column_start || (break_at < i && unit_extent > max_height + FIT_TOLERANCE) { + break_at = natural; + } + Some(break_at) +} + +/// Assign items to columns, returning (column index, offset from top) per +/// item. An item that overflows starts a new column (see `column_break`); an +/// item taller than the column gets one to itself. Burasage marks never +/// overflow: the next non-hanging item sees the overflowed cursor and wraps, +/// and oidashi never pulls a hung mark since it is forbidden-at-line-start, +/// not -end. +pub(super) fn plan_jlreq_columns(items: &[FlowItem], max_height: f32) -> Vec<(usize, f32)> { + let mut placements: Vec<(usize, f32)> = Vec::with_capacity(items.len()); + let mut column = 0usize; + let mut cursor = 0.0f32; + let mut column_start = 0usize; + + for (i, item) in items.iter().enumerate() { + if overflows(cursor, item, max_height) { + if let Some(break_at) = column_break(items, column_start, i, max_height) { + column += 1; + cursor = 0.0; + for k in break_at..i { + placements[k] = (column, cursor); + cursor += items[k].extent; + } + column_start = break_at; + } + } + placements.push((column, cursor)); + cursor += item.extent; + } + placements +} + +/// True when `max_height` is a real wrap budget (auto-width columns are +/// unbounded). +pub(super) fn is_bounded(max_height: f32) -> bool { + max_height.is_finite() && max_height > 0.0 && max_height < f32::MAX +} + +/// Offset of a column's content along the inline axis for `text-align`. In +/// `vertical-rl` that axis runs top to bottom: Left/Start anchor to the top, +/// Right/End to the bottom, Center to the middle of `budget` (the column wrap +/// height). An unbounded budget (auto-width) and Justify yield no shift; +/// `ordered_expansion_offsets` spreads justify space between cells. +pub(super) fn align_offset_along_column(align: TextAlign, budget: f32, used: f32) -> f32 { + if !is_bounded(budget) { + return 0.0; + } + let slack = (budget - used).max(0.0); + match align { + TextAlign::Center => slack / 2.0, + TextAlign::Right | TextAlign::End => slack, + _ => 0.0, + } +} + +/// Japanese inter-script spacing (JLREQ §3.2.6) between kana or kanji +/// (cl-15, cl-16, cl-19) and a rotated alphanumeric run. Punctuation keeps +/// its own aki and Latin sets solid against it; whitespace gets none. Scales +/// by the smaller adjacent font size so a large neighbour cannot widen the +/// gap. +fn inter_script_spacing(previous: &FlowCell, next: &FlowCell) -> f32 { + let is_japanese_letter = + |flow: &FlowCell| flow.ch.is_some_and(|ch| classify(ch).is_japanese_letter()); + let boundary = match (previous.script, next.script) { + ( + FlowScript::Upright, + FlowScript::Rotated { + starts_alphanumeric: true, + .. + }, + ) => is_japanese_letter(previous), + ( + FlowScript::Rotated { + ends_alphanumeric: true, + .. + }, + FlowScript::Upright, + ) => is_japanese_letter(next), + _ => false, + }; + if boundary { + previous.cell.font_size.min(next.cell.font_size) * INTER_SCRIPT_SPACING_EM + } else { + 0.0 + } +} + +/// Give every upright <-> Western boundary its inter-script gap, measured +/// between ink edges: side bearings differ across fonts, so advances alone +/// would space `うpenあ` unevenly. Adjusts the preceding cell's extent and +/// keeps its trailing letter-spacing. +pub(super) fn apply_jlreq_inter_script_spacing(cells: &mut [FlowCell]) { + for i in 1..cells.len() { + let target_gap = inter_script_spacing(&cells[i - 1], &cells[i]); + if target_gap <= 0.0 { + continue; + } + let previous = &cells[i - 1]; + let natural_gap = previous.cell.extent - previous.trailing_spacing + cells[i].cell.ink_top + - previous.cell.ink_bottom; + let corrected = previous.cell.extent + target_gap - natural_gap; + cells[i - 1].cell.extent = corrected.max(0.0); + } +} + +/// JLREQ character class of each cell. Ruby bases count as simple ruby, so +/// `ruby_spans[span]` overrides the cell's own class. +pub(super) fn flow_classes(cells: &[FlowCell], ruby_spans: &[bool]) -> Vec> { + cells + .iter() + .map(|flow| { + if ruby_spans[flow.cell.span] { + return Some(JlreqClass::SimpleRuby); + } + match flow.cell.kind { + CellKind::TateChuYoko { .. } => Some(JlreqClass::TateChuYoko), + CellKind::Rotated { .. } => Some(JlreqClass::Western), + _ => flow.ch.map(classify), + } + }) + .collect() +} + +/// JLREQ punctuation and cl-30 adjacency. Full-width fonts include a half-em +/// aki in punctuation advances. Ordinary text keeps it; at the internal +/// boundaries of §3.1.4 one half-em goes (see `punctuation_aki_sheds`), and +/// middle dots keep their quarter-em sides. +pub(super) fn shed_punctuation_aki(cells: &mut [FlowCell], aki_classes: &[Option]) { + for (index, shed) in punctuation_aki_sheds(aki_classes) { + cells[index].shed_to_half_em(shed == AkiShed::Leading); + } +} + +/// Punctuation aki class of each cell (see `aki_class`). A cell classed by +/// its own character takes `aki_class`, which leaves out curly quotes and +/// `vpal` spans; ruby bases, tate-chu-yoko and sideways runs keep their +/// `flow_classes` class. +pub(super) fn aki_classes( + cells: &[FlowCell], + classes: &[Option], + proportional_spans: &[bool], +) -> Vec> { + cells + .iter() + .zip(classes) + .map(|(flow, class)| match flow.ch { + Some(ch) if *class == Some(classify(ch)) => { + aki_class(ch, false, proportional_spans[flow.cell.span]) + } + _ => *class, + }) + .collect() +} + +/// Smallest legal flow extent after oikomi. Full-width punctuation floors at +/// a half-em frame, or at its shaped extent when `vpal` has already removed +/// the aki. +pub(super) fn min_squeezed_extent( + ch: Option, + extent: f32, + font_size: f32, + letter_spacing: f32, +) -> f32 { + let half_em_frame = ch.is_some_and(|ch| { + matches!( + classify(ch), + JlreqClass::OpeningBracket + | JlreqClass::ClosingBracket + | JlreqClass::FullStop + | JlreqClass::Comma + | JlreqClass::MiddleDot + ) + }); + if half_em_frame { + extent.min(0.5 * font_size + letter_spacing) + } else { + extent + } +} + +/// The em that scales the spacing between `cells[boundary]` and the next +/// cell: the smaller adjacent font size. +fn boundary_em(cells: &[FlowCell], boundary: usize) -> f32 { + cells[boundary] + .cell + .font_size + .min(cells[boundary + 1].cell.font_size) +} + +fn explicit_pair_spacing_em(before: JlreqClass, after: JlreqClass) -> f32 { + if matches!(before, JlreqClass::DividingPunctuation) { + pair_rule(before, after).preferred_em + } else { + 0.0 + } +} + +/// Adjacent classes at `boundary`, when both cells have one. +fn boundary_classes( + classes: &[Option], + boundary: usize, +) -> Option<(JlreqClass, JlreqClass)> { + Some((classes[boundary]?, classes[boundary + 1]?)) +} + +/// Preferred pair spacing (in em) at every boundary, per the JLREQ table. +pub(super) fn preferred_pair_spacing(classes: &[Option]) -> Vec { + (0..classes.len().saturating_sub(1)) + .map(|boundary| { + boundary_classes(classes, boundary) + .map_or(0.0, |(before, after)| pair_rule(before, after).preferred_em) + }) + .collect() +} + +/// cl-04 question/exclamation marks take a one-em space after their frame. +/// Fonts leave it out of the advance (unlike bracket aki), so this adds it +/// to the flow extent, where oikomi can reduce it. +pub(super) fn materialize_explicit_pair_spacing( + cells: &mut [FlowCell], + classes: &[Option], +) { + for boundary in 0..cells.len().saturating_sub(1) { + let Some((before, after)) = boundary_classes(classes, boundary) else { + continue; + }; + let spacing_em = explicit_pair_spacing_em(before, after); + if spacing_em > 0.0 { + cells[boundary].cell.extent += spacing_em * boundary_em(cells, boundary); + } + } +} + +/// Cell holding the removable spacing of a boundary, and whether that space +/// leads the cell (its glyph then moves up with the reduction). +fn spacing_owner(before: JlreqClass, after: JlreqClass, boundary: usize) -> (usize, bool) { + if explicit_pair_spacing_em(before, after) > 0.0 { + (boundary, false) + } else if matches!(after, JlreqClass::OpeningBracket | JlreqClass::MiddleDot) { + // After sequence shedding, the remaining aki is the next glyph's + // embedded leading space. + (boundary + 1, true) + } else { + (boundary, false) + } +} + +/// Remove up to `amount` of removable spacing from `cells[index]`, never +/// below its oikomi floor. Returns the amount removed. +fn reduce_cell_spacing(cells: &mut [FlowCell], index: usize, amount: f32, leading: bool) -> f32 { + let cell = &mut cells[index].cell; + let removable = (cell.extent - cell.min_squeezed_extent).max(0.0); + let amount = amount.min(cell.extent.max(0.0)).min(removable); + cell.extent -= amount; + if leading { + cell.glyph_flow_shift -= amount; + } + amount +} + +/// Discard explicit spacing at column edges (no trailing `!` space at a +/// column bottom). Returns true when it removed any. +fn discard_explicit_spacing_at_column_edges( + cells: &mut [FlowCell], + classes: &[Option], + pair_spacing_em: &mut [f32], + placements: &[(usize, f32)], +) -> bool { + let mut changed = false; + for boundary in 0..cells.len().saturating_sub(1) { + if placements[boundary].0 == placements[boundary + 1].0 { + continue; + } + let Some((before, after)) = boundary_classes(classes, boundary) else { + continue; + }; + if explicit_pair_spacing_em(before, after) <= 0.0 { + continue; + } + let spacing = pair_spacing_em[boundary] * boundary_em(cells, boundary); + if spacing <= 0.0 { + continue; + } + reduce_cell_spacing(cells, boundary, spacing, false); + pair_spacing_em[boundary] = 0.0; + changed = true; + } + changed +} + +/// True when breaking the column before `cells[i]`, with the column filled +/// to `cursor`, needs line adjustment: the break would violate kinsoku or +/// split a kept group, or would leave the column short of `max_height` (a +/// rotated run or other uneven extent). A column that is already full breaks +/// without it. +fn break_needs_adjustment(cells: &[FlowCell], i: usize, cursor: f32, max_height: f32) -> bool { + let flow = &cells[i]; + let previous_ch = i.checked_sub(1).and_then(|previous| cells[previous].ch); + flow.keep_with_previous + || flow.ch.is_some_and(forbidden_at_line_start) + || previous_ch.is_some_and(forbidden_at_line_end) + || max_height - cursor > FIT_TOLERANCE +} + +/// JLREQ oikomi: before wrapping a non-hanging item whose break needs line +/// adjustment, try to keep it in the column by reducing legal aki in table +/// priority order. When the whole deficit cannot be recovered, leave the +/// line to the oidashi/kinsoku planner. `adjustment` decides whether squeezing +/// comes first, only when pushing out would break kinsoku, or never. +pub(super) fn apply_ordered_oikomi( + cells: &mut [FlowCell], + classes: &[Option], + pair_spacing_em: &mut [f32], + max_height: f32, + adjustment: LineAdjustment, +) { + if !is_bounded(max_height) || adjustment == LineAdjustment::PushOutOnly { + return; + } + let mut column_start = 0usize; + let mut cursor = 0.0f32; + for i in 0..cells.len() { + let item = cells[i].item(); + if overflows(cursor, &item, max_height) { + let needed = item.extent - item.end_slack; + let squeeze = break_needs_adjustment(cells, i, cursor, max_height) + && (adjustment == LineAdjustment::PushInFirst + || !push_out_resolves(cells, column_start, i, max_height)); + if squeeze { + let deficit = cursor + needed - max_height; + cursor -= compress_line(cells, classes, pair_spacing_em, column_start..i, deficit); + } + if cursor + needed > max_height + FIT_TOLERANCE { + column_start = i; + cursor = 0.0; + } + } + cursor += cells[i].cell.extent; + } +} + +/// True when breaking before `cells[i]` can move characters to the next +/// column (oidashi) without leaving a kinsoku violation at the break. +fn push_out_resolves(cells: &[FlowCell], column_start: usize, i: usize, max_height: f32) -> bool { + let items = flow_items(cells); + match column_break(&items, column_start, i, max_height) { + None => true, + Some(break_at) => { + break_at > column_start && break_block(&items, break_at) != Some(BreakBlock::Kinsoku) + } + } +} + +/// Recover `deficit` from the boundaries in `line` (the cells before the +/// overflowing one), in shrink-priority order, when the whole deficit fits. +/// Returns the extent removed from the line. +fn compress_line( + cells: &mut [FlowCell], + classes: &[Option], + pair_spacing_em: &mut [f32], + line: std::ops::Range, + deficit: f32, +) -> f32 { + let overflowing = line.end; + // (boundary, priority, capacity) + let mut opportunities: Vec<(usize, u8, f32)> = Vec::new(); + for boundary in line { + let Some((before, after)) = boundary_classes(classes, boundary) else { + continue; + }; + let rule = pair_rule(before, after); + if rule.shrink_priority == 0 || pair_spacing_em[boundary] <= rule.minimum_em { + continue; + } + let em = boundary_em(cells, boundary); + let (owner, _) = spacing_owner(before, after, boundary); + let owner = &cells[owner].cell; + let rule_capacity = (pair_spacing_em[boundary] - rule.minimum_em) * em; + let physical_capacity = (owner.extent - owner.min_squeezed_extent).max(0.0); + let capacity = rule_capacity.min(physical_capacity); + if capacity > EPSILON { + opportunities.push((boundary, rule.shrink_priority, capacity)); + } + } + let total_capacity: f32 = opportunities.iter().map(|(_, _, cap)| *cap).sum(); + if total_capacity + EPSILON < deficit { + return 0.0; + } + + let mut removed_from_line = 0.0; + let mut remaining = deficit; + for priority in 1..=5 { + if remaining <= EPSILON { + break; + } + let caps: Vec<(usize, f32)> = opportunities + .iter() + .filter(|(_, p, _)| *p == priority) + .map(|(boundary, _, cap)| (*boundary, *cap)) + .collect(); + if caps.is_empty() { + continue; + } + let available: f32 = caps.iter().map(|(_, cap)| *cap).sum(); + for (boundary, reduction) in capped_equal_allocations(&caps, remaining.min(available)) { + let Some((before, after)) = boundary_classes(classes, boundary) else { + continue; + }; + let em = boundary_em(cells, boundary); + let (owner, leading) = spacing_owner(before, after, boundary); + let reduction = reduce_cell_spacing(cells, owner, reduction, leading); + pair_spacing_em[boundary] -= reduction / em; + if owner < overflowing { + removed_from_line += reduction; + } + remaining -= reduction; + } + } + removed_from_line +} + +/// Plan the columns, then trim spacing a break left at a column edge and +/// re-plan, since a shorter line may pull another cell into the column. Each +/// round shrinks a not-yet-trimmed cell, so the cell count bounds the loops. +/// +/// Opening brackets at a column head follow the JIS X 4051 tentsuki policy +/// and drop their leading half-em; closing brackets at a column end drop +/// their trailing half-em. A trim that pulls the bracket off its edge in the +/// re-plan is undone. +pub(super) fn plan_with_edge_trimming( + cells: &mut [FlowCell], + classes: &[Option], + pair_spacing_em: &mut [f32], + max_height: f32, +) -> Vec<(usize, f32)> { + let mut placements = plan_jlreq_columns(&flow_items(cells), max_height); + for _ in 0..cells.len() { + if !discard_explicit_spacing_at_column_edges(cells, classes, pair_spacing_em, &placements) { + break; + } + placements = plan_jlreq_columns(&flow_items(cells), max_height); + } + // Cells whose trim moved them off the edge keep their aki for good, so + // the loop cannot oscillate. + let mut keep_aki = vec![false; cells.len()]; + for _ in 0..cells.len() { + let saved = cells.to_vec(); + // (index, trimmed as a column head) + let mut trimmed: Vec<(usize, bool)> = Vec::new(); + for (index, flow) in cells.iter_mut().enumerate() { + if keep_aki[index] { + continue; + } + let opening = flow + .ch + .is_some_and(|ch| classify(ch) == JlreqClass::OpeningBracket); + if at_column_head(&placements, index) && opening && flow.shed_to_half_em(true) { + trimmed.push((index, true)); + } + if at_column_end(&placements, index) + && flow.end_slack() > EPSILON + && flow.shed_to_half_em(false) + { + trimmed.push((index, false)); + } + } + if trimmed.is_empty() { + break; + } + let replanned = plan_jlreq_columns(&flow_items(cells), max_height); + let mut moved = false; + for (index, head) in trimmed { + let still_at_edge = if head { + at_column_head(&replanned, index) + } else { + at_column_end(&replanned, index) + }; + if !still_at_edge { + cells[index] = saved[index].clone(); + keep_aki[index] = true; + moved = true; + } + } + placements = if moved { + plan_jlreq_columns(&flow_items(cells), max_height) + } else { + replanned + }; + } + placements +} + +fn at_column_head(placements: &[(usize, f32)], index: usize) -> bool { + placements[index].1 == 0.0 +} + +fn at_column_end(placements: &[(usize, f32)], index: usize) -> bool { + placements + .get(index + 1) + .is_none_or(|(next_column, _)| *next_column != placements[index].0) +} + +/// Ordered oidashi expansion for justified columns (JLREQ §3.8.4, see +/// `jlreq_ordered_expansion`). Returns each cell's extra flow offset. +/// Skips the last column. +pub(super) fn ordered_expansion_offsets( + cells: &[FlowCell], + classes: &[Option], + pair_spacing_em: &[f32], + placements: &[(usize, f32)], + column_used: &[f32], + max_height: f32, +) -> Vec { + let last_column = column_used.len().saturating_sub(1); + let mut boundary_expansion = vec![0.0f32; cells.len().saturating_sub(1)]; + for (column, used) in column_used.iter().enumerate().take(last_column) { + // (boundary, priority, cap) + let boundaries: Vec<(usize, u8, f32)> = (0..cells.len().saturating_sub(1)) + .filter(|boundary| { + placements[*boundary].0 == column && placements[boundary + 1].0 == column + }) + .filter_map(|boundary| { + let (before, after) = boundary_classes(classes, boundary)?; + let em = boundary_em(cells, boundary); + jlreq_expansion_gap(before, after, pair_spacing_em[boundary], em) + .map(|(priority, cap)| (boundary, priority, cap)) + }) + .collect(); + let slack = (max_height - used).max(0.0); + for (boundary, expansion) in jlreq_ordered_expansion(&boundaries, slack) { + boundary_expansion[boundary] += expansion; + } + } + + let mut offsets = vec![0.0f32; cells.len()]; + for i in 1..cells.len() { + if placements[i].0 == placements[i - 1].0 { + offsets[i] = offsets[i - 1] + boundary_expansion[i - 1]; + } + } + offsets +} + +#[cfg(test)] +mod tests { + use super::super::layout::{wrap_height, CellKind, VerticalCell}; + use super::super::test_support::*; + use super::*; + use crate::shapes::{FontFeatures, GrowType, TextCombineUpright, TextSpan}; + + fn item(extent: f32, ch: char) -> FlowItem { + FlowItem { + extent, + ch: Some(ch), + keep_with_previous: false, + joins_previous: false, + end_slack: 0.0, + } + } + + fn run_item(extent: f32, joins_previous: bool) -> FlowItem { + FlowItem { + extent, + ch: None, + keep_with_previous: false, + joins_previous, + end_slack: 0.0, + } + } + + fn columns(placements: &[(usize, f32)]) -> Vec { + placements.iter().map(|(column, _)| *column).collect() + } + + #[test] + fn plan_columns_keeps_upright_latin_words_whole() { + let items: Vec = "あいPenpot".chars().map(|c| item(10.0, c)).collect(); + let placements = plan_jlreq_columns(&items, 70.0); + assert_eq!(columns(&placements), vec![0, 0, 1, 1, 1, 1, 1, 1]); + } + + #[test] + fn plan_columns_keeps_a_word_split_across_runs_whole() { + let items = vec![ + item(24.0, 'あ'), + run_item(44.0, false), + run_item(38.0, true), + ]; + let placements = plan_jlreq_columns(&items, 90.0); + assert_eq!(placements, vec![(0, 0.0), (1, 0.0), (1, 44.0)]); + } + + #[test] + fn plan_columns_keeps_full_width_numbers_whole() { + let items: Vec = "あいう2026年".chars().map(|c| item(10.0, c)).collect(); + let placements = plan_jlreq_columns(&items, 40.0); + assert_eq!(columns(&placements), vec![0, 0, 0, 1, 1, 1, 1, 2]); + } + + #[test] + fn full_width_number_moves_to_the_next_column_whole() { + let provider = provider_with_fallback(VMTX_TEST_FONT, TEST_FONT); + let wide = layout_with(&provider, &make_content(&["あいう2026年"], 1000.0)); + let height = wide.cells[..4].iter().map(|cell| cell.extent).sum::() + 2.0; + let layout = layout_with_height( + &provider, + &make_content(&["あいう2026年"], height), + height, + ); + let columns: Vec<(usize, Option)> = layout + .cells + .iter() + .map(|cell| { + ( + cell.column, + Some(format!("{:?}", cell.kind).chars().take(10).collect()), + ) + }) + .collect(); + let digit_columns: Vec = layout.cells[3..7].iter().map(|cell| cell.column).collect(); + assert_eq!(digit_columns, vec![1, 1, 1, 1], "cells: {columns:?}"); + } + + #[test] + fn plan_columns_keeps_a_numeral_with_its_unit() { + let items: Vec = "体重60㎏".chars().map(|c| item(10.0, c)).collect(); + let placements = plan_jlreq_columns(&items, 40.0); + assert_eq!(columns(&placements), vec![0, 0, 1, 1, 1]); + } + + #[test] + fn plan_columns_never_leaves_an_empty_column() { + let items: Vec = "「「あ".chars().map(|c| item(10.0, c)).collect(); + let placements = plan_jlreq_columns(&items, 25.0); + assert_eq!(columns(&placements), vec![0, 0, 1]); + } + + #[test] + fn plan_columns_oidashi_ignores_float_noise_in_the_budget() { + let items = vec![item(22.0, 'あ'), item(22.0, 'い'), item(22.0, '」')]; + let placements = plan_jlreq_columns(&items, 44.0 - 0.00002); + assert_eq!(columns(&placements), vec![0, 1, 1]); + } + + #[test] + fn sideways_words_wrap_at_spaces() { + let provider = provider(TEST_FONT); + let wide = layout_with(&provider, &make_content(&["Penpot Work"], 1000.0)); + let first_word = wide.cells[0].extent; + let layout = layout_with_height( + &provider, + &make_content(&["Penpot Work"], first_word + 1.0), + first_word + 1.0, + ); + assert_eq!(layout.cells.len(), 2, "one sideways cell per word"); + assert_ne!(layout.cells[0].column, layout.cells[1].column); + } + + fn aligned_content(text: &str, height: f32, align: TextAlign) -> crate::shapes::TextContent { + content_of( + vec![vertical_paragraph(vec![make_span(text)], align, 1.0)], + height, + GrowType::Fixed, + ) + } + + fn vpal_content(text: &str) -> crate::shapes::TextContent { + spans_content( + vec![TextSpan { + font_features: FontFeatures::Vpal, + ..make_span(text) + }], + 1000.0, + ) + } + + fn visible_flow_gap(previous: &VerticalCell, next: &VerticalCell) -> f32 { + next.top + next.ink_top - (previous.top + previous.ink_bottom) + } + + #[test] + fn plan_columns_breaks_on_overflow() { + let items: Vec = "あいうえお".chars().map(|c| item(10.0, c)).collect(); + let placements = plan_jlreq_columns(&items, 25.0); + assert_eq!( + placements, + vec![(0, 0.0), (0, 10.0), (1, 0.0), (1, 10.0), (2, 0.0)] + ); + } + + #[test] + fn plan_columns_ignores_float_noise_in_the_budget() { + let items: Vec = "あいう".chars().map(|c| item(22.0, c)).collect(); + let placements = plan_jlreq_columns(&items, 66.0 - 0.00002); + assert!(placements.iter().all(|(column, _)| *column == 0)); + } + + #[test] + fn plan_columns_oversized_item_gets_own_column() { + let items = vec![item(10.0, 'あ'), item(100.0, 'い'), item(10.0, 'う')]; + let placements = plan_jlreq_columns(&items, 25.0); + assert_eq!(placements, vec![(0, 0.0), (1, 0.0), (2, 0.0)]); + } + + #[test] + fn plan_columns_burasage_hangs_comma_period() { + // 。 overflows the two-cell column; burasage hangs it past the column + // bottom, so no oidashi pushes い down and 。 never starts a column. + let items = vec![ + item(10.0, 'あ'), + item(10.0, 'い'), + item(10.0, '。'), + item(10.0, 'う'), + ]; + let placements = plan_jlreq_columns(&items, 25.0); + assert_eq!(placements, vec![(0, 0.0), (0, 10.0), (0, 20.0), (1, 0.0)]); + assert_eq!(placements[2].0, 0, "。 hangs in the current column"); + } + + #[test] + fn plan_columns_kinsoku_no_forbidden_end_at_column_bottom() { + // 「 would be left at the bottom of column 0; it moves down. + let items = vec![ + item(10.0, 'あ'), + item(10.0, '「'), + item(10.0, 'い'), + item(10.0, 'う'), + ]; + let placements = plan_jlreq_columns(&items, 25.0); + assert_eq!(placements, vec![(0, 0.0), (1, 0.0), (1, 10.0), (2, 0.0)]); + } + + #[test] + fn plan_columns_kinsoku_shift_is_bounded() { + // A column full of open brackets cannot be emptied: oidashi moves at + // most four of them along with the overflowing あ. + let items: Vec = "「「「「「「あ".chars().map(|c| item(10.0, c)).collect(); + let placements = plan_jlreq_columns(&items, 60.0); + let first_column_count = placements.iter().filter(|(c, _)| *c == 0).count(); + assert_eq!(first_column_count, 2); + } + + #[test] + fn plan_columns_kinsoku_shift_never_overflows_budget() { + // A two-item budget: moving 「「 down with the overflowing い would put + // three items (30.0) in a 20.0 column, so the planner drops the shift. + let items = vec![ + item(10.0, 'あ'), + item(10.0, '「'), + item(10.0, '「'), + item(10.0, 'い'), + ]; + let placements = plan_jlreq_columns(&items, 20.0); + let mut column_used = std::collections::BTreeMap::new(); + for (it, (column, top)) in items.iter().zip(&placements) { + let used: &mut f32 = column_used.entry(*column).or_default(); + *used = used.max(top + it.extent); + } + for (column, used) in column_used { + assert!(used <= 20.0, "column {column} overflows: {used}"); + } + } + + #[test] + fn plan_columns_does_not_cascade_a_forbidden_run_longer_than_a_column() { + // ー、ャュョッ。 cannot start a column, and the run is longer than a + // column. Pushing its head out cannot satisfy kinsoku: each new column + // would push it out again and leave one or two characters behind. + let items: Vec = "ださい。ー、ャュョッ。" + .chars() + .map(|c| item(10.0, c)) + .collect(); + let placements = plan_jlreq_columns(&items, 50.0); + assert_eq!(columns(&placements), vec![0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]); + } + + #[test] + fn capped_adjustment_redistributes_after_a_boundary_saturates() { + let allocations = capped_equal_allocations(&[(0, 1.0), (1, 3.0)], 3.0); + assert_eq!(allocations, vec![(0, 1.0), (1, 2.0)]); + } + + #[test] + fn align_offset_helper_maps_edges() { + // Unbounded budget (auto-width): no shift. + assert_eq!( + align_offset_along_column(TextAlign::Center, f32::MAX, 40.0), + 0.0 + ); + // Left/Start anchors to the top. + assert_eq!(align_offset_along_column(TextAlign::Left, 100.0, 40.0), 0.0); + // Center splits the slack. + assert_eq!( + align_offset_along_column(TextAlign::Center, 100.0, 40.0), + 30.0 + ); + // Right/End pushes to the bottom. + assert_eq!( + align_offset_along_column(TextAlign::Right, 100.0, 40.0), + 60.0 + ); + assert_eq!(align_offset_along_column(TextAlign::End, 100.0, 40.0), 60.0); + // Overfull column: no negative shift. + assert_eq!( + align_offset_along_column(TextAlign::Right, 40.0, 100.0), + 0.0 + ); + } + + /// Text of each column of `text` laid out in `height` under `policy`. + fn columns_under(text: &str, height: f32, policy: LineAdjustment) -> Vec { + let mut content = make_content(&[text], height); + content.set_line_adjustment(policy); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, height); + let chars: Vec = text.chars().collect(); + (0..layout.columns.len()) + .map(|column| { + layout + .cells + .iter() + .filter(|cell| cell.column == column) + .map(|cell| chars[cell.start]) + .collect() + }) + .collect() + } + + #[test] + fn line_adjustment_decides_whether_a_line_squeezes() { + // Fig 78, line 4: keeping う) needs the bracket aki squeezed. + let text = "ささえる「ボディ」(仮想ボディという)の大きさ"; + let height = 17.0 * EM; + let first = |policy| columns_under(text, height, policy)[0].clone(); + assert_eq!( + first(LineAdjustment::PushInFirst), + "ささえる「ボディ」(仮想ボディという)" + ); + assert_eq!( + first(LineAdjustment::PushOutFirst), + "ささえる「ボディ」(仮想ボディとい" + ); + assert_eq!( + first(LineAdjustment::PushOutOnly), + "ささえる「ボディ」(仮想ボディとい" + ); + } + + #[test] + fn push_out_policies_move_a_bracketed_word_instead_of_squeezing() { + // あ「あ」 in 3.25 em: squeezing 「's aki keeps it in one column; + // pushing out moves 「あ」 whole, since 「 cannot end a column. + let height = 3.25 * EM; + let columns = |policy| columns_under("あ「あ」", height, policy); + assert_eq!(columns(LineAdjustment::PushInFirst), vec!["あ「あ」"]); + assert_eq!(columns(LineAdjustment::PushOutFirst), vec!["あ", "「あ」"]); + assert_eq!(columns(LineAdjustment::PushOutOnly), vec!["あ", "「あ」"]); + } + + #[test] + fn closing_bracket_at_column_end_counts_without_its_aki() { + // JLREQ Fig 78: ……(仮想ボディという)at a 17 em column. A closing + // bracket ending a column is half width, so squeezing 「 and ( keeps + // う) in the column instead of pushing it out. + let text = "ささえる「ボディ」(仮想ボディという)の大きさ"; + let height = 17.0 * EM; + let content = make_content(&[text], height); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, height); + let chars: Vec = text.chars().collect(); + let first: String = layout + .cells + .iter() + .filter(|cell| cell.column == 0) + .map(|cell| chars[cell.start]) + .collect(); + assert_eq!(first, "ささえる「ボディ」(仮想ボディという)"); + let bracket = layout + .cells + .iter() + .find(|cell| chars[cell.start] == ')') + .unwrap(); + assert!( + bracket.top + bracket.extent <= height + 0.01, + "the bracket ends inside the column: {}", + bracket.top + bracket.extent + ); + } + + #[test] + fn layout_kinsoku_no_period_at_column_top() { + // Wrap height fits exactly 2 characters per column; the 。 after + // the second character would start column 2 without kinsoku. + let text = "あい。うえお"; + let content = make_content(&[text], 100.0); + let cell_extent = layout_content(&content, 1000.0).cells[0].extent; + let layout = layout_content(&content, cell_extent * 2.0 + 0.1); + + assert!(layout.columns.len() > 1, "the text must wrap"); + let chars: Vec = text.chars().collect(); + for column_index in 0..layout.columns.len() { + let head = layout + .cells + .iter() + .filter(|c| c.column == column_index) + .min_by(|a, b| a.top.total_cmp(&b.top)) + .expect("every column holds a cell"); + let c = chars[head.start]; + assert!( + !forbidden_at_line_start(c), + "column {column_index} starts with forbidden char {c}" + ); + } + } + + #[test] + fn ordinary_closing_punctuation_keeps_preferred_aki() { + // In ordinary text, the comma and closing bracket keep their normal + // half-em glyph body plus half-em trailing aki: one em in total. + let content = make_content(&["く、く」く"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + assert_eq!(layout.cells.len(), 5, "one cell per character"); + let em = 20.0; + assert!( + (layout.cells[0].extent - em).abs() < 1.0, + "leading ideograph keeps full advance, got {}", + layout.cells[0].extent + ); + assert!( + (layout.cells[1].extent - em).abs() < 1.0, + "、 before an ideograph keeps its aki, got {}", + layout.cells[1].extent + ); + assert!( + (layout.cells[3].extent - em).abs() < 1.0, + "」 before an ideograph keeps its aki, got {}", + layout.cells[3].extent + ); + assert!( + (layout.cells[4].extent - em).abs() < 1.0, + "trailing ideograph keeps full advance, got {}", + layout.cells[4].extent + ); + } + + #[test] + fn closing_then_opening_keeps_half_em_aki() { + // く」「く: the closing bracket sheds its trailing half, but the + // opening bracket after it keeps its full em (leading half blank), so + // the pair keeps the half-em aki of JIS X 4051. + let content = make_content(&["く」「く"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + assert_eq!(layout.cells.len(), 4, "one cell per character"); + let em = 20.0; + assert!( + layout.cells[1].extent <= 0.5 * em + 0.01, + "」 sheds its trailing aki, got {}", + layout.cells[1].extent + ); + assert!( + (layout.cells[2].extent - em).abs() < 1.0, + "「 after a closing mark keeps its full em, got {}", + layout.cells[2].extent + ); + assert_eq!( + layout.cells[2].glyph_flow_shift, 0.0, + "the unshed opening bracket is not shifted" + ); + } + + #[test] + fn dividing_punctuation_materializes_trailing_aki() { + let provider = provider(VMTX_TEST_FONT); + let terminal = layout_with(&provider, &make_content(&["!"], 1000.0)); + let before_ideograph = layout_with(&provider, &make_content(&["!く"], 1000.0)); + let before_dividing = layout_with(&provider, &make_content(&["!?"], 1000.0)); + let before_closing = layout_with(&provider, &make_content(&["!」"], 1000.0)); + let em = 20.0; + let natural_extent = terminal.cells[0].extent; + + assert!( + (before_ideograph.cells[0].extent - natural_extent - em).abs() < 0.01, + "! before ordinary text adds one em, got {} over natural {natural_extent}", + before_ideograph.cells[0].extent + ); + assert!( + (before_dividing.cells[0].extent - natural_extent).abs() < 0.01, + "! before ? adds no aki, got {} over natural {natural_extent}", + before_dividing.cells[0].extent + ); + assert!( + (before_closing.cells[0].extent - natural_extent).abs() < 0.01, + "! before a closing bracket adds no aki, got {} over natural {natural_extent}", + before_closing.cells[0].extent + ); + } + + #[test] + fn dividing_punctuation_oikomi_never_compresses_glyph_frames() { + let provider = provider(VMTX_TEST_FONT); + let terminal_exclamation = layout_with(&provider, &make_content(&["!"], 1000.0)); + let terminal_question = layout_with(&provider, &make_content(&["?"], 1000.0)); + let layout = layout_with_height(&provider, &make_content(&["く!?く"], 60.0), 60.0); + + assert!( + layout.cells[1].extent + 0.01 >= terminal_exclamation.cells[0].extent, + "oikomi compressed the ! glyph frame from {} to {}", + terminal_exclamation.cells[0].extent, + layout.cells[1].extent + ); + assert!( + layout.cells[2].extent + 0.01 >= terminal_question.cells[0].extent, + "oikomi compressed the ? glyph frame from {} to {}", + terminal_question.cells[0].extent, + layout.cells[2].extent + ); + } + + #[test] + fn dividing_punctuation_discards_trailing_aki_at_column_edge() { + let provider = provider(VMTX_TEST_FONT); + let terminal = layout_with(&provider, &make_content(&["!"], 1000.0)); + let layout = layout_with_height(&provider, &make_content(&["!く"], 20.0), 20.0); + + assert_ne!(layout.cells[0].column, layout.cells[1].column); + assert!( + (layout.cells[0].extent - terminal.cells[0].extent).abs() < 0.01, + "column-end ! should discard its trailing aki, got {} over natural {}", + layout.cells[0].extent, + terminal.cells[0].extent + ); + } + + #[test] + fn ordinary_opening_punctuation_keeps_preferred_aki() { + // く「く: the opening bracket keeps its leading half-em aki in the + // middle of a line, so its total advance remains one em. + let content = make_content(&["く「く"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + assert_eq!(layout.cells.len(), 3, "one cell per character"); + let em = 20.0; + assert!( + (layout.cells[0].extent - em).abs() < 1.0, + "leading ideograph keeps full advance, got {}", + layout.cells[0].extent + ); + assert_eq!( + layout.cells[0].glyph_flow_shift, 0.0, + "ideograph is not shifted" + ); + assert!( + (layout.cells[1].extent - em).abs() < 1.0, + "「 keeps its leading aki, got {}", + layout.cells[1].extent + ); + assert_eq!( + layout.cells[1].glyph_flow_shift, 0.0, + "an uncompressed opening bracket is not shifted, got {}", + layout.cells[1].glyph_flow_shift + ); + assert!( + (layout.cells[2].extent - em).abs() < 1.0, + "trailing ideograph keeps full advance, got {}", + layout.cells[2].extent + ); + } + + #[test] + fn consecutive_closing_punctuation_sets_solid_internally() { + let content = make_content(&["く。」く"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let em = 20.0; + assert!( + layout.cells[1].extent <= 0.5 * em + 0.01, + "。 sheds its internal trailing aki, got {}", + layout.cells[1].extent + ); + assert!( + (layout.cells[2].extent - em).abs() < 1.0, + "the final 」 keeps the sequence's trailing aki, got {}", + layout.cells[2].extent + ); + } + + #[test] + fn consecutive_opening_punctuation_sets_solid_after_first() { + let content = make_content(&["く「『く"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let em = 20.0; + assert!( + (layout.cells[1].extent - em).abs() < 1.0, + "the first opening bracket keeps the sequence's leading aki, got {}", + layout.cells[1].extent + ); + assert!( + layout.cells[2].extent <= 0.5 * em + 0.01, + "the second opening bracket sets solid, got {}", + layout.cells[2].extent + ); + assert!(layout.cells[2].glyph_flow_shift < -0.01); + } + + #[test] + fn opening_bracket_at_column_head_is_tentsuki() { + let content = make_content(&["「く"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let em = 20.0; + assert_eq!(layout.cells[0].top, 0.0); + assert!( + layout.cells[0].extent <= 0.5 * em + 0.01, + "line-head 「 sheds its leading aki, got {}", + layout.cells[0].extent + ); + assert!(layout.cells[0].glyph_flow_shift < -0.01); + } + + #[test] + fn line_end_punctuation_keeps_preferred_half_em_aki() { + let content = make_content(&["く、"], 40.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + assert_eq!(layout.cells[0].column, layout.cells[1].column); + assert!( + (layout.cells[1].extent - 20.0).abs() < 1.0, + "line-end 、 keeps a half-em after its glyph, got {}", + layout.cells[1].extent + ); + } + + #[test] + fn plain_ideographs_keep_full_advance() { + let content = make_content(&["くくく"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + for cell in &layout.cells { + assert!( + (cell.extent - 20.0).abs() < 1.0, + "no punctuation: full advance kept, got {}", + cell.extent + ); + } + } + + #[test] + fn vpal_punctuation_is_not_double_compressed() { + // 、's vertical alternate already halves its advance under vpal; + // the aki shed must not compress the half-width cell again. + let provider = provider(VPAL_TEST_FONT); + let layout = layout_with(&provider, &vpal_content("あ、あ")); + assert!( + (layout.cells[1].extent - 10.0).abs() < 0.01, + "、 is exactly half-width under vpal, got {}", + layout.cells[1].extent + ); + } + + #[test] + fn vpal_oikomi_preserves_half_em_punctuation_frame() { + // vpal has removed the opening bracket's leading aki, so in a tight + // fixed-height column oikomi cannot shrink the frame and text wraps. + let provider = provider(VPAL_TEST_FONT); + let content = vpal_content("あ「あ"); + let natural = layout_with(&provider, &content); + let natural_total: f32 = natural.cells.iter().map(|cell| cell.extent).sum(); + let bracket_extent = natural.cells[1].extent; + let constrained = layout_with_height(&provider, &content, natural_total - 5.0); + + assert!( + (bracket_extent - 10.0).abs() < 0.01, + "vpal opening bracket starts at half-em, got {bracket_extent}" + ); + assert!( + (constrained.cells[1].extent - bracket_extent).abs() < 0.01, + "oikomi collapsed the vpal bracket frame from {bracket_extent} to {}", + constrained.cells[1].extent + ); + assert_ne!( + constrained.cells[0].column, constrained.cells[2].column, + "insufficient removable aki should wrap the protected bracket and following glyph" + ); + } + + #[test] + fn tate_chu_yoko_all_uses_asymmetric_punctuation_aki() { + // 」→TCY keeps the closing mark's trailing half-em, and TCY→「 keeps + // the opening mark's leading half-em. TCY on the opposite side of + // either bracket sets solid. + let mut content = make_content_with_spans(&["く", "」", "20", "「", "く"], 400.0); + content.paragraphs_mut()[0].children_mut()[2].text_combine_upright = + TextCombineUpright::All; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + assert!(matches!(layout.cells[2].kind, CellKind::TateChuYoko { .. })); + for (index, label) in [(1, "」 before TCY"), (2, "TCY"), (3, "「 after TCY")] { + assert!( + (layout.cells[index].extent - 20.0).abs() < 0.01, + "{label} should occupy its preferred one-em frame, got {}", + layout.cells[index].extent + ); + } + + let mut reverse = make_content_with_spans(&["く", "「", "20", "」", "く"], 400.0); + reverse.paragraphs_mut()[0].children_mut()[2].text_combine_upright = + TextCombineUpright::All; + let reverse = layout_with(&provider(VMTX_TEST_FONT), &reverse); + assert!(matches!( + reverse.cells[2].kind, + CellKind::TateChuYoko { .. } + )); + assert!((reverse.cells[1].extent - 20.0).abs() < 0.01); + assert!((reverse.cells[3].extent - 20.0).abs() < 0.01); + } + + #[test] + fn tate_chu_yoko_digits_uses_the_same_cl30_adjacency() { + let mut content = make_content_with_spans(&["く」31「く"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::Digits; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let tcy_index = layout + .cells + .iter() + .position(|cell| matches!(cell.kind, CellKind::TateChuYoko { .. })) + .expect("digit TCY cell"); + assert!((layout.cells[tcy_index - 1].extent - 20.0).abs() < 0.01); + assert!((layout.cells[tcy_index].extent - 20.0).abs() < 0.01); + assert!((layout.cells[tcy_index + 1].extent - 20.0).abs() < 0.01); + } + + #[test] + fn inter_script_spacing_separates_cjk_and_latin() { + let expected_gap = 20.0 * INTER_SCRIPT_SPACING_EM; + + let cjk_latin = layout_content(&make_content(&["あa"], 1000.0), 1000.0); + assert_eq!(cjk_latin.cells.len(), 2); + assert!( + (visible_flow_gap(&cjk_latin.cells[0], &cjk_latin.cells[1]) - expected_gap).abs() + < 0.01 + ); + + let latin_cjk = layout_content(&make_content(&["aあ"], 1000.0), 1000.0); + assert_eq!(latin_cjk.cells.len(), 2); + assert!( + (visible_flow_gap(&latin_cjk.cells[0], &latin_cjk.cells[1]) - expected_gap).abs() + < 0.01 + ); + } + + #[test] + fn inter_script_spacing_is_visually_symmetric_around_latin_run() { + let layout = layout_content(&make_content(&["うpenあ"], 1000.0), 1000.0); + assert_eq!(layout.cells.len(), 3); + let expected_gap = 20.0 * INTER_SCRIPT_SPACING_EM; + let before = visible_flow_gap(&layout.cells[0], &layout.cells[1]); + let after = visible_flow_gap(&layout.cells[1], &layout.cells[2]); + assert!((before - expected_gap).abs() < 0.01); + assert!((after - expected_gap).abs() < 0.01); + assert!((before - after).abs() < 0.01); + } + + #[test] + fn inter_script_spacing_preserves_explicit_letter_spacing() { + let letter_spacing = 3.0; + let layout = layout_content(&spaced_content("うpenあ", letter_spacing), 1000.0); + assert_eq!(layout.cells.len(), 3); + let expected_gap = 20.0 * INTER_SCRIPT_SPACING_EM + letter_spacing; + assert!((visible_flow_gap(&layout.cells[0], &layout.cells[1]) - expected_gap).abs() < 0.01); + assert!((visible_flow_gap(&layout.cells[1], &layout.cells[2]) - expected_gap).abs() < 0.01); + } + + #[test] + fn inter_script_spacing_crosses_span_boundaries() { + let joined = layout_content(&make_content(&["あa"], 1000.0), 1000.0); + let split = layout_content(&make_content_with_spans(&["あ", "a"], 1000.0), 1000.0); + assert_eq!(joined.cells.len(), split.cells.len()); + for (joined, split) in joined.cells.iter().zip(&split.cells) { + assert!((joined.top - split.top).abs() < 0.01); + assert!((joined.extent - split.extent).abs() < 0.01); + } + } + + #[test] + fn inter_script_spacing_excludes_punctuation_and_whitespace() { + let cjk_extent = layout_content(&make_content(&["あ"], 1000.0), 1000.0).cells[0].extent; + for text in ["あ.", "あ a"] { + let layout = layout_content(&make_content(&[text], 1000.0), 1000.0); + assert!( + (layout.cells[0].extent - cjk_extent).abs() < 0.01, + "{text:?} must not add spacing immediately after the CJK cell" + ); + } + } + + #[test] + fn latin_sets_against_japanese_punctuation_without_script_spacing() { + // JLREQ §3.2.6 (Figure 103): after 、。」 Latin keeps only the + // punctuation's half-em aki, and Latin sets solid after 「 and + // before 、」. + let provider = provider_with_fallback(VPAL_TEST_FONT, TEST_FONT); + let fallback = ["fallback".to_string()]; + let layout = |text: &str| { + layout_with_fallback(&provider, &make_content(&[text], 1000.0), 1000.0, &fallback) + }; + let natural = layout("editor").cells[0].extent; + for text in ["あ、editor", "あ。editor", "あ」editor", "あ「editor"] { + let cells = layout(text).cells; + assert!( + (cells[2].top - cells[1].top - EM).abs() < 0.01, + "{text}: punctuation frame is {} em", + (cells[2].top - cells[1].top) / EM + ); + } + for text in ["editor、", "editor」"] { + let cells = layout(text).cells; + assert!( + (cells[1].top - natural).abs() < 0.01, + "{text}: Latin must set solid, gap {}", + cells[1].top - natural + ); + } + } + + #[test] + fn oikomi_reduces_script_gap_before_oidashi() { + let natural = layout_content(&make_content(&["あa"], 1000.0), 1000.0); + assert_eq!(natural.cells.len(), 2); + let natural_total: f32 = natural.cells.iter().map(|cell| cell.extent).sum(); + let budget = natural_total - 2.0; + let adjusted = layout_content(&make_content(&["あa"], budget), budget); + assert_eq!(adjusted.cells[0].column, adjusted.cells[1].column); + let gap = visible_flow_gap(&adjusted.cells[0], &adjusted.cells[1]); + assert!( + (gap - (20.0 * INTER_SCRIPT_SPACING_EM - 2.0)).abs() < 0.01, + "oikomi should recover the two-pixel deficit from the script gap, got {gap}" + ); + } + + #[test] + fn oikomi_keeps_a_full_column_solid() { + // Five cells fill the column exactly; the sixth く may start a column, + // so the commas keep their aki instead of compressing to pull it in. + let budget = 5.0 * EM; + let content = make_content(&["く、く、くく"], budget); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, budget); + assert_eq!(layout.cells[4].column, 0); + assert_eq!(layout.cells[5].column, 1); + for comma in [1, 3] { + assert!( + (layout.cells[comma].extent - EM).abs() < 0.01, + "comma {comma} keeps its aki, got {}", + layout.cells[comma].extent + ); + } + } + + #[test] + fn oikomi_fills_a_column_left_short_by_a_rotated_run() { + // The Latin run leaves the column uneven; reducing the comma aki + // pulls the last く in instead of leaving the column short. + let text = "くaく、くく"; + let provider = provider(VMTX_TEST_FONT); + let natural = layout_with_height(&provider, &make_content(&[text], 1000.0), 1000.0); + let total: f32 = natural.cells.iter().map(|cell| cell.extent).sum(); + let budget = total - 2.0; + let layout = layout_with_height(&provider, &make_content(&[text], budget), budget); + let last = layout.cells.last().expect("cells"); + assert_eq!(last.column, 0, "oikomi keeps the last く in the column"); + } + + #[test] + fn oikomi_keeps_a_line_start_forbidden_character_in_the_column() { + let budget = 5.0 * EM; + let content = make_content(&["く、く、く」"], budget); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, budget); + assert_eq!( + layout.cells[5].column, 0, + "」 cannot start a column, so oikomi keeps it in place" + ); + } + + #[test] + fn oikomi_preserves_sentence_final_full_stop_aki() { + let natural = layout_content(&make_content(&["あ。あ"], 1000.0), 1000.0); + let natural_total: f32 = natural.cells.iter().map(|cell| cell.extent).sum(); + let budget = natural_total - 2.0; + let adjusted = layout_content(&make_content(&["あ。あ"], budget), budget); + assert_ne!( + adjusted.cells[1].column, adjusted.cells[2].column, + "fixed full-stop aki must not be compressed to retain the next ideograph" + ); + assert!((adjusted.cells[1].extent - natural.cells[1].extent).abs() < 0.01); + } + + #[test] + fn text_align_shifts_column_cells() { + let text = "あいう"; + // Baseline (top-aligned) used length of the single column. + let used = { + let content = aligned_content(text, 1000.0, TextAlign::Left); + let layout = layout_content(&content, 1000.0); + layout + .cells + .iter() + .map(|c| c.top + c.extent) + .fold(0.0f32, f32::max) + }; + + let top_of = |align: TextAlign| { + let content = aligned_content(text, 1000.0, align); + let layout = layout_content(&content, 1000.0); + layout.cells[0].top + }; + + assert!(top_of(TextAlign::Left).abs() < 0.01); + assert!((top_of(TextAlign::Center) - (1000.0 - used) / 2.0).abs() < 0.5); + assert!((top_of(TextAlign::Right) - (1000.0 - used)).abs() < 0.5); + // Cells stay in reading order and keep tiling under the shift. + let content = aligned_content(text, 1000.0, TextAlign::Right); + let layout = layout_content(&content, 1000.0); + for pair in layout.cells.windows(2) { + assert!(pair[1].top >= pair[0].top); + } + } + + #[test] + fn text_align_auto_width_stays_top() { + // Auto-width columns are snug: alignment must not shift them. + let content = content_of( + vec![vertical_paragraph( + vec![make_span("あいうえお")], + TextAlign::Right, + 1.0, + )], + 60.0, + GrowType::AutoWidth, + ); + + let layout = layout_content(&content, wrap_height(&content, 60.0)); + assert!(layout.cells[0].top.abs() < 0.01); + } + + #[test] + fn justify_fills_non_last_columns() { + use std::collections::BTreeMap; + let text = "あいうえお"; + // Uniform per-cell extent for the test font. + let e = { + let content = aligned_content(text, 1000.0, TextAlign::Left); + layout_content(&content, 1000.0).cells[0].extent + }; + // A 2.5-cell budget wraps into three columns: {0,1}, {2,3}, {4}. The + // first two break with 0.5e of slack; the last column is one cell. + let budget = e * 2.5; + let content = aligned_content(text, budget, TextAlign::Justify); + let layout = layout_content(&content, budget); + + let mut by_col: BTreeMap> = BTreeMap::new(); + for c in &layout.cells { + by_col.entry(c.column).or_default().push(c); + } + assert!(by_col.len() >= 2, "text must wrap into multiple columns"); + let last = *by_col.keys().max().unwrap(); + + for (&col, col_cells) in &by_col { + if col != last && col_cells.len() > 1 { + assert!(col_cells[0].top.abs() < 0.01, "col {col} top cell at top"); + let bottom = col_cells.last().unwrap().top + col_cells.last().unwrap().extent; + assert!( + (bottom - budget).abs() < 0.5, + "justified col {col} fills the budget, bottom {bottom} vs {budget}" + ); + } + } + // The last column keeps its natural top (start-aligned, not stretched). + assert!( + by_col[&last][0].top.abs() < 0.01, + "last column is not justified" + ); + } +} diff --git a/render-wasm/src/shapes/text_vertical/flow/pairwise.rs b/render-wasm/src/shapes/text_vertical/flow/pairwise.rs new file mode 100644 index 0000000000..fa8a91c01b --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/flow/pairwise.rs @@ -0,0 +1,235 @@ +//! Column planning, spacing and alignment of the pairwise policies (plain, +//! Chinese and Korean) along the vertical flow. Break opportunities, spacing +//! and justification come from `TextLayoutPolicy` pair rules over the whole +//! paragraph text. + +use crate::shapes::cjk_chars::is_punctuation; +use crate::shapes::line_breaks::PolicyBoundaries; +use crate::shapes::text_allocation::{allocate_bounded_expansion, bounded_expansion}; +use crate::shapes::text_policy::TextLayoutPolicy; +use crate::shapes::TextAlign; +use unicode_segmentation::UnicodeSegmentation; + +use super::super::layout::CellKind; +use super::{align_offset_along_column, is_bounded, FlowCell, FIT_TOLERANCE}; + +/// Column planning of the pairwise policies (plain, Chinese and Korean): +/// break opportunities come from `TextLayoutPolicy::break_allowed`. +/// Boundaries use the entire paragraph, so font and style runs cannot split +/// a grapheme or create a word boundary. +pub(in crate::shapes::text_vertical) fn plan_pairwise_columns( + cells: &[FlowCell], + text: &str, + policy: TextLayoutPolicy, + max_height: f32, +) -> Vec<(usize, f32)> { + let boundaries = PolicyBoundaries::new(text, policy); + let mut emergency_start = Vec::with_capacity(cells.len()); + let mut unit_start = Vec::with_capacity(cells.len()); + let mut prefix = vec![0.0]; + for (i, flow) in cells.iter().enumerate() { + let boundary = boundaries.at(flow.cell.start); + let atomic = + i > 0 && (flow.keep_with_previous || flow.shares_previous_box || boundary.is_none()); + let emergency_joined = atomic || boundary.is_some_and(|(_, emergency)| !emergency); + emergency_start.push(if i > 0 && emergency_joined { + emergency_start[i - 1] + } else { + i + }); + let joined = atomic || boundary.is_some_and(|(allowed, _)| !allowed); + unit_start.push(if i > 0 && joined { + unit_start[i - 1] + } else { + i + }); + prefix.push(prefix[i] + flow.cell.extent); + } + let mut unit_end = vec![cells.len(); cells.len()]; + for i in (0..cells.len().saturating_sub(1)).rev() { + unit_end[i] = if unit_start[i] == unit_start[i + 1] { + unit_end[i + 1] + } else { + i + 1 + }; + } + let mut placements = Vec::with_capacity(cells.len()); + let (mut column, mut column_start, mut cursor) = (0, 0, 0.0); + for (i, flow) in cells.iter().enumerate() { + let punctuation = |at: usize| cells.get(at).and_then(|c| c.ch).is_some_and(is_punctuation); + let solitary = (i == 0 || !punctuation(i - 1)) && !punctuation(i + 1); + let hang = if solitary { + flow.ch + .map_or(0.0, |ch| policy.hanging_em(ch, true) * flow.cell.font_size) + } else { + 0.0 + }; + if cursor > 0.0 && cursor + flow.cell.extent - hang > max_height + FIT_TOLERANCE { + let candidate = unit_start[i]; + let end = if policy.measures_whole_unit() { + unit_end[i] + } else { + i + 1 + }; + let fits_empty = prefix[end] - prefix[candidate] <= max_height + FIT_TOLERANCE; + let break_at = if candidate > column_start && fits_empty { + candidate + } else { + emergency_start[i] + }; + // Overlong words may use emergency boundaries; punctuation, + // graphemes, explicit no-break controls and annotations may not. + if break_at > column_start { + column += 1; + cursor = 0.0; + for k in break_at..i { + placements[k] = (column, cursor); + cursor += cells[k].cell.extent; + } + column_start = break_at; + } + } + placements.push((column, cursor)); + cursor += flow.cell.extent; + } + placements +} + +/// Source edge characters of every cell, including sideways runs. Offsets +/// stay in UTF-16 source space; typography never inserts saved spaces. +fn pairwise_cell_edges(cells: &[FlowCell], text: &str) -> Vec<(Option, Option)> { + let mut offset = 0; + let chars: Vec<_> = text + .chars() + .map(|ch| { + let at = offset; + offset += ch.len_utf16(); + (at, ch) + }) + .collect(); + cells + .iter() + .map(|flow| { + let start = chars.partition_point(|(at, _)| *at < flow.cell.start); + let end = chars.partition_point(|(at, _)| *at < flow.cell.end); + ( + chars.get(start).filter(|_| start < end).map(|(_, ch)| *ch), + end.checked_sub(1) + .and_then(|at| chars.get(at)) + .filter(|_| start < end) + .map(|(_, ch)| *ch), + ) + }) + .collect() +} + +pub(in crate::shapes::text_vertical) fn apply_pairwise_spacing( + cells: &mut [FlowCell], + text: &str, + policy: TextLayoutPolicy, +) { + let edges = pairwise_cell_edges(cells, text); + for i in 1..cells.len() { + if cells[i].keep_with_previous || cells[i].shares_previous_box { + continue; + } + let (Some(before), Some(after)) = (edges[i - 1].1, edges[i].0) else { + continue; + }; + let em = cells[i - 1].cell.font_size.min(cells[i].cell.font_size); + let available = (cells[i - 1].cell.extent + - cells[i - 1].trailing_spacing + - 0.5 * cells[i - 1].cell.font_size) + .max(0.0); + let spacing = policy.pair_spacing_em(before, after, true); + let compression = (spacing.compression * em).min(available); + // A tate-chu-yoko composite sets in one ideograph's cell, so the + // Han/Western gap of its digits or letters does not apply. + let tate_chu_yoko = + |flow: &FlowCell| matches!(flow.cell.kind, CellKind::TateChuYoko { .. }); + let gap = if tate_chu_yoko(&cells[i - 1]) || tate_chu_yoko(&cells[i]) { + 0.0 + } else { + spacing.script_gap * em + }; + cells[i - 1].cell.extent = (cells[i - 1].cell.extent + gap - compression) + .max(cells[i - 1].trailing_spacing.max(0.0)); + cells[i - 1].cell.min_squeezed_extent = cells[i - 1].cell.extent; + } +} + +/// Justify non-final columns over policy-approved gaps with bounded amounts. +/// Korean word mode only expands spaces; grapheme and annotation interiors +/// never become opportunities merely because a font/style run split them. +pub(in crate::shapes::text_vertical) fn pairwise_aligned_tops( + cells: &[FlowCell], + text: &str, + policy: TextLayoutPolicy, + placements: &[(usize, f32)], + align: TextAlign, + budget: f32, +) -> Vec { + let mut tops: Vec = placements.iter().map(|(_, top)| *top).collect(); + let edges = pairwise_cell_edges(cells, text); + let graphemes: Vec = text + .graphemes(true) + .scan(0, |at, g| { + let start = *at; + *at += g.encode_utf16().count(); + Some(start) + }) + .collect(); + let last_column = placements.last().map_or(0, |p| p.0); + let mut start = 0; + while start < cells.len() { + let column = placements[start].0; + let end = placements.partition_point(|p| p.0 <= column); + let used = placements[end - 1].1 + cells[end - 1].cell.extent; + let shift = align_offset_along_column(align, budget, used); + for top in &mut tops[start..end] { + *top += shift; + } + if (align == TextAlign::Distribute || (align == TextAlign::Justify && column < last_column)) + && is_bounded(budget) + { + let remaining = (budget - used).max(0.0); + let mut extras = vec![0.0; end - start]; + let mut opportunities = Vec::new(); + for i in start..end - 1 { + if cells[i + 1].keep_with_previous + || cells[i + 1].shares_previous_box + || graphemes.binary_search(&cells[i + 1].cell.start).is_err() + { + continue; + } + let (Some(before), Some(after)) = (edges[i].1, edges[i + 1].0) else { + continue; + }; + if align == TextAlign::Distribute + && matches!(before, '\u{00a0}' | '\u{202f}' | '\u{2060}') + || align == TextAlign::Distribute + && matches!(after, '\u{00a0}' | '\u{202f}' | '\u{2060}') + { + continue; + } + let em = cells[i].cell.font_size.min(cells[i + 1].cell.font_size); + let distribute = (align == TextAlign::Distribute).then_some(budget); + if let Some((priority, cap)) = + bounded_expansion(policy, before, after, em, distribute) + { + opportunities.push((i, priority, cap)); + } + } + for (i, amount) in allocate_bounded_expansion(&opportunities, remaining) { + extras[i - start] += amount; + } + let mut extra = 0.0; + for i in start..end { + tops[i] += extra; + extra += extras[i - start]; + } + } + start = end; + } + tops +} diff --git a/render-wasm/src/shapes/text_vertical/layout.rs b/render-wasm/src/shapes/text_vertical/layout.rs new file mode 100644 index 0000000000..b6b7113116 --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/layout.rs @@ -0,0 +1,1355 @@ +use std::cell::OnceCell; + +use skia_safe::{ + self as skia, + textlayout::{TextDecoration, TypefaceFontProvider}, + FontMgr, +}; + +use crate::globals::get_resources; +use crate::math::Rect; +use crate::render::FallbackFonts; +use crate::shapes::jlreq::JlreqClass; +use crate::shapes::text_policy::TextLayoutPolicy; +use crate::shapes::{ + merge_fills, AnnotationSide, AppliedTextTransform, FontFeatures, GrowType, LineAdjustment, + Paragraph, TextAlign, TextContent, TextSpan, VerticalAlign, +}; +use crate::utils::get_fallback_fonts; + +use super::annotations::{ + grow_ruby_bases, layout_emphasis, layout_ruby, ruby_base_units, ruby_rooms, + set_ruby_overhang_rooms, spread_ruby_base_cells, EmphasisMark, RubyBaseUnit, RubyCell, + RubyRooms, +}; +use super::cells::{Fonts, SpanCells, WarichuNote}; +use super::flow::{ + aki_classes, align_offset_along_column, apply_jlreq_inter_script_spacing, apply_ordered_oikomi, + apply_pairwise_spacing, flow_classes, is_bounded, materialize_explicit_pair_spacing, + ordered_expansion_offsets, pairwise_aligned_tops, plan_pairwise_columns, + plan_with_edge_trimming, preferred_pair_spacing, shed_punctuation_aki, FlowCell, FIT_TOLERANCE, +}; +use super::paint::LayoutDraws; +use crate::shapes::text_shaping::ShapedRun; + +#[derive(Debug, Clone, Copy)] +pub enum CellKind { + Upright { + run: usize, + glyph: usize, + count: usize, + }, + /// An upright-flow character whose font lacks a `vert`/`vrt2` alternate, + /// rotated in its own cell so wrapping, kinsoku and editor offsets stay + /// per character. + SyntheticRotated { + run: usize, + glyph: usize, + count: usize, + }, + Rotated { + run: usize, + }, + TateChuYoko { + /// Runs `[run_start, run_start + run_count)`, laid side by side. + run_start: usize, + run_count: usize, + /// Horizontal squeeze that fits the runs into one em; glyphs keep + /// their full height. + h_scale: f32, + }, + /// One span's share of a warichu piece. Every cell of a piece has the + /// piece's box; the `*_top`/`*_extent` pairs place this span's glyphs + /// along each sub-line, relative to `top`. + Warichu { + /// First `first_count` runs are the right sub-line, the rest the left. + run_start: usize, + run_count: usize, + first_count: usize, + /// UTF-16 length of the first sub-line part (split point from `start`). + first_chars: usize, + first_top: f32, + first_extent: f32, + second_top: f32, + second_extent: f32, + }, +} + +/// One placed piece of the vertical flow. Offsets are UTF-16, +/// paragraph-relative (all spans concatenated), in original text space. +#[derive(Clone)] +pub struct VerticalCell { + pub kind: CellKind, + pub paragraph: usize, + pub span: usize, + pub start: usize, + pub end: usize, + pub column: usize, + pub top: f32, + /// Flow-axis advance, from `vmtx` when present, else the horizontal one. + pub extent: f32, + /// Oikomi lower bound: the glyph frame before removable pair spacing. + pub min_squeezed_extent: f32, + /// Shaped horizontal advance, used to centre the glyph in the column. + pub h_advance: f32, + /// Visible glyph-ink edges along the flow axis, relative to `top`. + pub ink_top: f32, + pub ink_bottom: f32, + pub paint: usize, + /// Span font size, for decoration bar geometry. + pub font_size: f32, + /// Span text decoration, painted as vertical bars along the column. + pub decoration: Option, + pub underline_side: AnnotationSide, + /// Draw-only flow shift for half-width opening punctuation (jlreq aki). + pub glyph_flow_shift: f32, +} + +/// A laid-out column, x measured from the *content* left edge (the content +/// block is anchored to the shape's right edge by consumers). +#[derive(Debug, Clone, Copy)] +pub struct VerticalColumn { + pub x: f32, + pub width: f32, + /// Reserved annotation gutter before the base band (left / `under`). + pub base_offset: f32, + /// Line-height column advance that base glyphs centre on. + pub base_width: f32, +} + +pub(super) fn column_base_center(column: &VerticalColumn) -> f32 { + column.x + column.base_offset + column.base_width / 2.0 +} + +pub struct VerticalLayout { + pub runs: Vec, + pub paints: Vec, + pub cells: Vec, + pub columns: Vec, + /// Shaped ruby annotation runs, indexed by `RubyCell::run`. + pub ruby_runs: Vec, + pub ruby_cells: Vec, + /// Single-glyph emphasis runs, indexed by `EmphasisMark::run`. + pub emphasis_runs: Vec, + pub emphasis_marks: Vec, + /// Per paragraph: [start, end) range into `columns`. + pub paragraph_columns: Vec<(usize, usize)>, + /// Text offsets of each paragraph. + pub text_maps: Vec, + pub width: f32, + pub height: f32, + /// Glyph draws, built on the first paint. + pub(super) draws: OnceCell, +} + +/// Text offsets of one laid-out paragraph, which map cells back to the +/// source text. +pub struct ParagraphTextMap { + /// Transformed text of each span, with its source ownership. + pub span_transforms: Vec, + /// UTF-16 start of each span in the paragraph's transformed text. + pub span_starts: Vec, + /// UTF-16 start of each span in the paragraph's source text. + pub span_source_starts: Vec, + /// UTF-16 offset of each scalar boundary of the transformed text, for + /// editor positions. + pub utf16_boundaries: Vec, +} + +impl ParagraphTextMap { + fn new(paragraph: &Paragraph) -> Self { + let span_transforms: Vec = paragraph + .children() + .iter() + .map(TextSpan::apply_text_transform_with_source_ranges) + .collect(); + let span_starts = running_starts( + span_transforms + .iter() + .map(|transform| transform.text.encode_utf16().count()), + ); + let span_source_starts = running_starts( + paragraph + .children() + .iter() + .map(|span| span.text.encode_utf16().count()), + ); + let mut utf16_boundaries = vec![0usize]; + let mut offset = 0usize; + for character in span_transforms.iter().flat_map(|span| span.text.chars()) { + offset += character.len_utf16(); + utf16_boundaries.push(offset); + } + Self { + span_transforms, + span_starts, + span_source_starts, + utf16_boundaries, + } + } +} + +/// Start of each item when items of `lengths` follow one another. +fn running_starts(lengths: impl Iterator) -> Vec { + lengths + .scan(0usize, |offset, length| { + let start = *offset; + *offset += length; + Some(start) + }) + .collect() +} + +impl VerticalLayout { + /// Content origin (top-left of the laid-out block) in the coordinate + /// space of `bounds`. + pub fn origin(&self, bounds: &Rect, align: VerticalAlign) -> (f32, f32) { + ( + bounds.left + block_axis_offset(bounds.width(), self.width, align), + bounds.top, + ) + } +} + +/// Horizontal offset of vertical content within its shape. `VerticalAlign` +/// top/center/bottom mean block start/center/end: right/center/left in +/// vertical-rl. Content wider than the shape keeps that anchor and overflows +/// toward the block end, so a start-anchored block grows leftward. +pub fn block_axis_offset(container_width: f32, content_width: f32, align: VerticalAlign) -> f32 { + let slack = container_width - content_width; + match align { + VerticalAlign::Top => slack, + VerticalAlign::Center => slack / 2.0, + VerticalAlign::Bottom => 0.0, + } +} + +/// Column-wrap limit: auto-width shapes grow to fit (columns never wrap); +/// all others, auto-height included, wrap at the shape height. +pub fn wrap_height(text_content: &TextContent, height: f32) -> f32 { + match text_content.grow_type() { + GrowType::AutoWidth => f32::MAX, + _ => f32::max(height, 1.0), + } +} + +/// Cross-axis geometry shared by every column of a paragraph. +struct ColumnGeometry { + /// Line-height-controlled column advance the base glyphs centre on. + base_width: f32, + /// Annotation gutter on the left (`under`) side of the base band. + under_gutter: f32, + /// Annotation gutter on the right (`over`) side of the base band. + over_gutter: f32, +} + +impl ColumnGeometry { + fn new(paragraph: &Paragraph) -> Self { + let spans = paragraph.children(); + let line_height = if paragraph.line_height() > 0.0 { + paragraph.line_height() + } else { + 1.2 + }; + let max_font_size = spans.iter().map(|s| s.font_size).fold(12.0, f32::max); + + // Under automatic clearance, ruby reserves its configured-size gutter + // on the logical annotation side. In vertical-rl, `over` is right and + // `under` is left. `None` keeps the column advance, as horizontal + // keeps the line height. + let gutter = |side| { + let ruby = spans + .iter() + .filter(|span| { + span.annotation_clearance.is_auto() && span.has_ruby() && span.ruby_side == side + }) + .map(TextSpan::ruby_font_size) + .fold(0.0, f32::max); + let emphasis = spans + .iter() + .filter(|span| { + span.annotation_clearance.is_auto() + && !span.text_emphasis.is_none() + && span.emphasis_side(true) == side + }) + .map(|span| span.font_size * crate::shapes::text_attrs::EMPHASIS_FONT_SCALE) + .fold(0.0, f32::max); + let decoration = spans + .iter() + .map(|span| span.font_size * span.decoration_room_em(side, true)) + .fold(0.0, f32::max); + // Keep the original mixed-font gutter: when any span stacks the + // two annotations, reserve their independent maximum sizes. + let stacked = spans.iter().any(|span| { + !span.text_emphasis.is_none() + && span.ruby_side == side + && span.stacks_emphasis_outside_ruby(true) + }); + decoration + + if stacked { + ruby + emphasis + } else { + ruby.max(emphasis) + } + }; + Self { + base_width: max_font_size * line_height, + under_gutter: gutter(AnnotationSide::Under), + over_gutter: gutter(AnnotationSide::Over), + } + } + + fn column(&self) -> VerticalColumn { + VerticalColumn { + x: 0.0, + width: self.under_gutter + self.base_width + self.over_gutter, + base_offset: self.under_gutter, + base_width: self.base_width, + } + } +} + +/// A run of the paragraph flow: shaped cells, or the warichu note at an +/// index of `ParagraphCells::notes`. +enum FlowSegment { + Shaped(Vec), + Note(usize), +} + +/// A paragraph shaped once for warichu planning. Spans outside warichu are +/// shaped up front; each plan only reshapes the notes. +struct ParagraphCells<'a> { + paragraph: &'a Paragraph, + text_map: &'a ParagraphTextMap, + segments: Vec, + notes: Vec>, +} + +impl<'a> ParagraphCells<'a> { + /// Shapes the spans outside warichu into `runs` and adds one paint per + /// span to `paints`. + fn new( + fonts: &'a Fonts, + paragraph_index: usize, + paragraph: &'a Paragraph, + text_map: &'a ParagraphTextMap, + bounds: Rect, + runs: &mut Vec, + paints: &mut Vec, + ) -> Self { + let mut segments = Vec::new(); + let mut notes: Vec> = Vec::new(); + let mut note_members = Vec::new(); + let mut flush_note = |members: &mut Vec<_>, segments: &mut Vec| { + if !members.is_empty() { + segments.push(FlowSegment::Note(notes.len())); + notes.push(WarichuNote::new(std::mem::take(members))); + } + }; + for (span_index, (span, transform)) in paragraph + .children() + .iter() + .zip(&text_map.span_transforms) + .enumerate() + { + let offset = text_map.span_starts[span_index]; + if transform.text.is_empty() { + continue; + } + paints.push(merge_fills(&span.fills, bounds)); + let span_cells = SpanCells::new( + fonts, + span, + paragraph_index, + span_index, + paints.len() - 1, + offset, + ); + if span.warichu { + note_members.push((span_cells, transform.text.as_str())); + } else { + flush_note(&mut note_members, &mut segments); + let mut cells = Vec::new(); + span_cells.push(&transform.text, runs, &mut cells); + segments.push(FlowSegment::Shaped(cells)); + } + } + flush_note(&mut note_members, &mut segments); + Self { + paragraph, + text_map, + segments, + notes, + } + } + + /// The paragraph's cells in flow order, before spacing and placement, + /// with notes broken into pieces at the paragraph UTF-16 + /// `warichu_splits`. Note runs go to the end of `runs`. + fn flow(&self, warichu_splits: &[usize], runs: &mut Vec) -> Vec { + let mut flow = Vec::new(); + for segment in &self.segments { + match segment { + FlowSegment::Shaped(cells) => flow.extend(cells.iter().cloned()), + FlowSegment::Note(note) => self.notes[*note].push(warichu_splits, runs, &mut flow), + } + } + keep_transform_expansions_together(&mut flow, self.text_map); + keep_ruby_bases_together(&mut flow, self.paragraph); + flow + } +} + +/// Most warichu breaks planned per paragraph. +const MAX_WARICHU_SPLITS: usize = 64; + +/// Next break that lets a warichu piece start where the planner left room +/// for it, as a paragraph UTF-16 offset. +fn next_warichu_split( + notes: &[WarichuNote], + flow: &[FlowCell], + placements: &[(usize, f32)], + splits: &[usize], + max_height: f32, +) -> Option { + warichu_overflows(flow, placements, max_height) + .into_iter() + .find_map(|(i, room)| { + let start = flow[i].cell.start; + let note = notes.iter().find(|note| note.contains(start))?; + note.split_for_room(start, splits, room) + .filter(|split| !splits.contains(split)) + }) +} + +/// Warichu pieces the planner moved whole to a later column, or that overrun +/// their column, with the room left for them where they should start: after +/// the cells kinsoku carried along, at the bottom of the previous column. +fn warichu_overflows( + flow: &[FlowCell], + placements: &[(usize, f32)], + max_height: f32, +) -> Vec<(usize, f32)> { + if !is_bounded(max_height) { + return Vec::new(); + } + let columns = placements.last().map_or(0, |(column, _)| column + 1); + let mut column_used = vec![0.0f32; columns]; + let mut column_first = vec![usize::MAX; columns]; + for (i, (cell, (column, top))) in flow.iter().zip(placements).enumerate() { + column_used[*column] = column_used[*column].max(top + cell.cell.extent); + column_first[*column] = column_first[*column].min(i); + } + let mut overflows = Vec::new(); + for (i, (cell, (column, top))) in flow.iter().zip(placements).enumerate() { + if !matches!(cell.cell.kind, CellKind::Warichu { .. }) || cell.shares_previous_box { + continue; + } + if top + cell.cell.extent > max_height + FIT_TOLERANCE { + overflows.push((i, max_height - top)); + } else if *column > 0 { + let carried: f32 = flow[column_first[*column]..i] + .iter() + .map(|carried| carried.cell.extent) + .sum(); + let room = max_height - column_used[column - 1] - carried; + if room > FIT_TOLERANCE { + overflows.push((i, room)); + } + } + } + overflows +} +/// A CSS transform may expand one source character into several cells +/// (`ß` -> `SS`). Keep them in one column so the SVG fallback renders each +/// source slice once. +fn keep_transform_expansions_together(flow: &mut [FlowCell], text_map: &ParagraphTextMap) { + let source_range = |cell: &VerticalCell| { + let start = text_map.span_starts[cell.span]; + text_map.span_transforms[cell.span].source_utf16_range(cell.start - start..cell.end - start) + }; + for index in 1..flow.len() { + let (previous, current) = (&flow[index - 1].cell, &flow[index].cell); + flow[index].keep_with_previous = + previous.span == current.span && source_range(previous) == source_range(current); + } +} + +/// A reading is group ruby: its base, which can cross spans +/// (`Paragraph::ruby_groups`), moves between columns whole, so the reading +/// never splits. +fn keep_ruby_bases_together(flow: &mut [FlowCell], paragraph: &Paragraph) { + let group_of = paragraph.ruby_group_of_spans(); + let group = |span: usize| group_of.get(span).copied().flatten(); + for index in 1..flow.len() { + let current = group(flow[index].cell.span); + if current.is_some() && group(flow[index - 1].cell.span) == current { + flow[index].keep_with_previous = true; + } + } +} + +/// Final flow-axis top of each cell: its planned offset plus the text-align +/// shift. Justify stretches every column but the last to fill a bounded wrap +/// height. +fn jlreq_aligned_tops( + flow: &[FlowCell], + classes: &[Option], + pair_spacing_em: &[f32], + placements: &[(usize, f32)], + align: TextAlign, + max_height: f32, +) -> Vec { + let columns_used = placements.last().map_or(1, |(column, _)| column + 1); + // A column's used length is its farthest cell bottom. + let mut column_used = vec![0.0f32; columns_used]; + for (flow, (column, top)) in flow.iter().zip(placements) { + column_used[*column] = column_used[*column].max(top + flow.cell.extent); + } + let expansion = if matches!(align, TextAlign::Justify) && is_bounded(max_height) { + ordered_expansion_offsets( + flow, + classes, + pair_spacing_em, + placements, + &column_used, + max_height, + ) + } else { + vec![0.0; flow.len()] + }; + placements + .iter() + .zip(expansion) + .map(|((column, top), extra)| { + top + align_offset_along_column(align, max_height, column_used[*column]) + extra + }) + .collect() +} + +/// A paragraph's flow after spacing and column planning. +struct PlannedParagraph { + policy: TextLayoutPolicy, + text: String, + flow: Vec, + ruby_units: Vec, + classes: Vec>, + pair_spacing_em: Vec, + /// (column, offset from the column top) of each flow cell. + placements: Vec<(usize, f32)>, +} + +impl PlannedParagraph { + /// Plans the flow, then breaks the first warichu piece that does not fit + /// where it starts and plans again, until every piece fits. Note runs go + /// to the end of `runs`. + fn new( + cells: &ParagraphCells, + fonts: &Fonts, + max_height: f32, + line_adjustment: LineAdjustment, + runs: &mut Vec, + ) -> Self { + let spans = cells.paragraph.children(); + let ruby_spans: Vec = spans.iter().map(TextSpan::has_ruby).collect(); + let vpal_spans: Vec = spans + .iter() + .map(|span| span.font_features == FontFeatures::Vpal) + .collect(); + let ruby_units = ruby_base_units( + cells.paragraph, + &cells.text_map.span_transforms, + &cells.text_map.span_starts, + fonts, + ); + let run_mark = runs.len(); + let mut warichu_splits: Vec = Vec::new(); + loop { + runs.truncate(run_mark); + let mut flow = cells.flow(&warichu_splits, runs); + let mut ruby_units = ruby_units.clone(); + + let policy = cells.paragraph.layout_policy(); + if !policy.uses_jlreq() { + // Warichu pieces never split here: only Japanese renders + // warichu, and the frontend drops it for other languages. + // Readings overhang neighbours by the rule horizontal text + // applies to every language (`ruby_overhang_room`). + set_ruby_overhang_rooms(&flow, &mut ruby_units); + grow_ruby_bases(&mut flow, &ruby_units); + let text: String = cells + .text_map + .span_transforms + .iter() + .map(|t| t.text.as_str()) + .collect(); + apply_pairwise_spacing(&mut flow, &text, policy); + let placements = plan_pairwise_columns(&flow, &text, policy, max_height); + return Self { + policy, + text, + classes: vec![None; flow.len()], + pair_spacing_em: vec![0.0; flow.len().saturating_sub(1)], + flow, + ruby_units, + placements, + }; + } + + apply_jlreq_inter_script_spacing(&mut flow); + let classes = flow_classes(&flow, &ruby_spans); + let aki = aki_classes(&flow, &classes, &vpal_spans); + shed_punctuation_aki(&mut flow, &aki); + materialize_explicit_pair_spacing(&mut flow, &classes); + set_ruby_overhang_rooms(&flow, &mut ruby_units); + grow_ruby_bases(&mut flow, &ruby_units); + let mut pair_spacing_em = preferred_pair_spacing(&classes); + apply_ordered_oikomi( + &mut flow, + &classes, + &mut pair_spacing_em, + max_height, + line_adjustment, + ); + let placements = + plan_with_edge_trimming(&mut flow, &classes, &mut pair_spacing_em, max_height); + let split = if warichu_splits.len() < MAX_WARICHU_SPLITS { + next_warichu_split( + &cells.notes, + &flow, + &placements, + &warichu_splits, + max_height, + ) + } else { + None + }; + match split { + Some(split) => warichu_splits.push(split), + None => { + return Self { + policy, + text: String::new(), + flow, + ruby_units, + classes, + pair_spacing_em, + placements, + } + } + } + } + } + + fn columns_used(&self) -> usize { + self.placements.last().map_or(1, |(column, _)| column + 1) + } + + /// Final cells, aligned along their columns, numbered from `column_base`. + /// A later span's cell of a warichu piece takes the piece's box. + fn place(&self, align: TextAlign, max_height: f32, column_base: usize) -> Vec { + let align = if !self.policy.justifies_columns() && align == TextAlign::Justify { + TextAlign::Left + } else { + align + }; + let tops = if self.policy.uses_jlreq() && align != TextAlign::Distribute { + jlreq_aligned_tops( + &self.flow, + &self.classes, + &self.pair_spacing_em, + &self.placements, + align, + max_height, + ) + } else { + pairwise_aligned_tops( + &self.flow, + &self.text, + self.policy, + &self.placements, + align, + max_height, + ) + }; + let mut cells: Vec = Vec::with_capacity(self.flow.len()); + for ((flow, (column, _)), top) in self.flow.iter().zip(&self.placements).zip(tops) { + let mut cell = VerticalCell { + column: column_base + column, + top, + ..flow.cell.clone() + }; + if flow.shares_previous_box { + if let Some(piece) = cells.last() { + cell.top = piece.top; + cell.extent = piece.extent; + cell.min_squeezed_extent = piece.min_squeezed_extent; + cell.ink_top = piece.ink_top; + cell.ink_bottom = piece.ink_bottom; + } + } + cells.push(cell); + } + cells + } +} + +/// Sets each column's x, right to left from column 0, and returns the total +/// width. +fn place_columns_right_to_left(columns: &mut [VerticalColumn]) -> f32 { + let width: f32 = columns.iter().map(|column| column.width).sum(); + let mut right = width; + for column in columns.iter_mut() { + right -= column.width; + column.x = right; + } + width +} + +/// Lay out the whole content vertically. Pure of global state: fonts come +/// through the provider/fallback arguments so native tests can supply +/// their own. +pub fn layout_vertical( + text_content: &TextContent, + max_height: f32, + font_provider: &TypefaceFontProvider, + fallback_mgr: FontMgr, + fallback_families: &FallbackFonts, + bounds: Rect, +) -> VerticalLayout { + let fonts = Fonts { + provider: font_provider, + fallback_mgr: &fallback_mgr, + fallback_families, + }; + let paragraphs = text_content.paragraphs(); + let text_maps: Vec = paragraphs.iter().map(ParagraphTextMap::new).collect(); + + let mut runs: Vec = Vec::new(); + let mut paints: Vec = Vec::new(); + let mut cells: Vec = Vec::new(); + let mut columns: Vec = Vec::new(); + let mut paragraph_columns: Vec<(usize, usize)> = Vec::new(); + let mut ruby_rooms_by_span = RubyRooms::new(); + + for (paragraph_index, (paragraph, text_map)) in paragraphs.iter().zip(&text_maps).enumerate() { + let paragraph_cells = ParagraphCells::new( + &fonts, + paragraph_index, + paragraph, + text_map, + bounds, + &mut runs, + &mut paints, + ); + let planned = PlannedParagraph::new( + ¶graph_cells, + &fonts, + max_height, + text_content.line_adjustment(), + &mut runs, + ); + + let column_base = columns.len(); + let columns_used = planned.columns_used(); + columns.extend(std::iter::repeat_n( + ColumnGeometry::new(paragraph).column(), + columns_used, + )); + paragraph_columns.push((column_base, column_base + columns_used)); + + let mut placed = planned.place(paragraph.text_align(), max_height, column_base); + spread_ruby_base_cells(&mut placed, &planned.ruby_units, max_height); + cells.extend(placed); + ruby_rooms_by_span.extend(ruby_rooms(paragraph_index, &planned.ruby_units)); + } + + let width = place_columns_right_to_left(&mut columns); + let (ruby_runs, ruby_cells) = layout_ruby(text_content, &cells, &fonts, &ruby_rooms_by_span); + let (emphasis_runs, emphasis_marks) = + layout_emphasis(text_content, &cells, &runs, &text_maps, &fonts); + let height = cells + .iter() + .map(|c| c.top + c.extent) + .fold(0.0f32, f32::max); + + VerticalLayout { + runs, + paints, + cells, + columns, + ruby_runs, + ruby_cells, + emphasis_runs, + emphasis_marks, + paragraph_columns, + text_maps, + width, + height, + draws: OnceCell::new(), + } +} + +/// Lay out with the render state's font store; span paints resolve against +/// `bounds`. +fn layout_from_content( + text_content: &TextContent, + max_height: f32, + bounds: Rect, +) -> VerticalLayout { + let font_provider = get_resources().fonts.font_provider(); + let fallback_mgr = FontMgr::from(font_provider.clone()); + layout_vertical( + text_content, + max_height, + font_provider, + fallback_mgr, + get_fallback_fonts(), + bounds, + ) +} + +/// Lay out the content for a box `height` tall, against its stored bounds. +#[cfg(test)] +pub(super) fn layout_for_box(text_content: &TextContent, height: f32) -> VerticalLayout { + layout_from_content( + text_content, + wrap_height(text_content, height), + text_content.bounds(), + ) +} + +/// Lay out the content for the box `rect`: its height is the column-wrap +/// budget and span paints resolve against it. Production code goes through +/// the cached `TextContent::vertical_layout`. +pub fn layout_for_rect(text_content: &TextContent, rect: &Rect) -> VerticalLayout { + layout_from_content( + text_content, + wrap_height(text_content, rect.height()), + *rect, + ) +} + +#[cfg(test)] +mod tests { + use super::super::test_support::*; + use super::*; + use crate::shapes::{TextCombineUpright, TextOrientation, TextSpan}; + + fn cjk_layout( + texts: &[&str], + language: &str, + wrap: crate::shapes::text_policy::KoreanWrap, + align: TextAlign, + height: f32, + ) -> VerticalLayout { + let mut paragraph = vertical_paragraph( + texts.iter().map(|text| make_span(text)).collect(), + align, + 1.0, + ); + paragraph.set_language(language.to_string()); + paragraph.set_korean_wrap(wrap); + let content = content_of(vec![paragraph], height, GrowType::Fixed); + layout_with_height( + &provider(include_bytes!("../../fonts/cjk-locl-test.ttf")), + &content, + height, + ) + } + + #[test] + fn distributed_hangul_fills_the_final_column_across_styles() { + use crate::shapes::text_policy::KoreanWrap; + let layout = cjk_layout( + &["기획", "편집"], + "ko", + KoreanWrap::KeepWords, + TextAlign::Distribute, + 310.0, + ); + assert_eq!(layout.cells.len(), 4); + assert!(layout.cells.iter().all(|cell| cell.column == 0)); + let last = layout.cells.last().unwrap(); + assert!((last.top + last.extent - 310.0).abs() < 0.01); + let steps: Vec<_> = layout + .cells + .windows(2) + .map(|pair| pair[1].top - pair[0].top) + .collect(); + assert!(steps + .windows(2) + .all(|pair| (pair[1] - pair[0]).abs() < 0.01)); + } + + #[test] + fn boxes_shorter_than_atomic_units_keep_closing_punctuation_with_its_base() { + use crate::shapes::text_policy::KoreanWrap; + for (language, wrap) in [ + ("zh-Hans-CN", KoreanWrap::Syllable), + ("zh-Hant-TW", KoreanWrap::Syllable), + ("zh-Hant-HK", KoreanWrap::Syllable), + ("ko", KoreanWrap::Syllable), + ("ko", KoreanWrap::KeepWords), + ] { + for height in [1.0, 10.0, 19.0, 20.0, 30.0, 39.0] { + let layout = cjk_layout(&["文", "。"], language, wrap, TextAlign::Left, height); + assert_eq!(layout.cells.len(), 2); + assert_eq!( + layout.cells[0].column, layout.cells[1].column, + "{language} {wrap:?} {height}px: an indivisible unit overflows together" + ); + for cell in &layout.cells { + assert!(cell.top.is_finite()); + assert!(cell.extent.is_finite() && cell.extent > 0.0); + } + } + } + } + + #[test] + fn korean_emergency_wrap_keeps_closing_punctuation_with_its_base() { + use crate::shapes::text_policy::KoreanWrap; + let layout = cjk_layout( + &["한한", "한한", "。"], + "ko", + KoreanWrap::KeepWords, + TextAlign::Left, + EM * 2.1, + ); + let cells = &layout.cells; + assert!(cells.last().unwrap().column > 0); + assert_eq!(cells[cells.len() - 1].column, cells[cells.len() - 2].column); + assert!(cells.iter().all(|cell| cell.extent > 0.0)); + } + + #[test] + fn regional_chinese_compression_and_justification_change_geometry() { + use crate::shapes::text_policy::KoreanWrap; + let cn = cjk_layout( + &["文,", "。文"], + "zh-Hans-CN", + KoreanWrap::Syllable, + TextAlign::Left, + EM * 10.0, + ); + let tw = cjk_layout( + &["文,", "。文"], + "zh-Hant-TW", + KoreanWrap::Syllable, + TextAlign::Left, + EM * 10.0, + ); + let end = |layout: &VerticalLayout| { + layout.cells.last().unwrap().top + layout.cells.last().unwrap().extent + }; + assert!((end(&tw) - end(&cn) - EM * 0.5).abs() < 0.01); + let justified = cjk_layout( + &["文文文文"], + "zh-Hant-TW", + KoreanWrap::Syllable, + TextAlign::Justify, + EM * 2.6, + ); + assert!(justified.cells[1].top > EM + 1.0); + assert_eq!(justified.cells[3].top, EM, "last column remains unexpanded"); + } + + #[test] + fn traditional_chinese_hangs_only_a_single_comma_in_vertical_text() { + use crate::shapes::text_policy::KoreanWrap; + let single = cjk_layout( + &["文文,文"], + "zh-Hant-TW", + KoreanWrap::Syllable, + TextAlign::Left, + EM * 2.6, + ); + assert_eq!(single.cells[2].column, 0); + assert_eq!(single.cells[3].column, 1); + let paired = cjk_layout( + &["文文,,文"], + "zh-Hant-TW", + KoreanWrap::Syllable, + TextAlign::Left, + EM * 2.6, + ); + assert!(paired.cells[2].column > 0); + assert_eq!(paired.cells[1].column, paired.cells[2].column); + } + + #[test] + fn word_mode_preserves_words_and_decomposed_syllables_across_styles() { + use crate::shapes::text_policy::KoreanWrap; + let layout = cjk_layout( + &["한글 ", "한", "국어"], + "ko", + KoreanWrap::KeepWords, + TextAlign::Left, + EM * 3.6, + ); + let word: Vec<_> = layout.cells.iter().filter(|c| c.start >= 3).collect(); + assert!(word.iter().all(|c| c.column == 1)); + let layout = cjk_layout( + &["ᄒ", "ᅡᆫ한。"], + "ko", + KoreanWrap::KeepWords, + TextAlign::Left, + EM * 4.1, + ); + for unit in [0..3, 3..7] { + let cells: Vec<_> = layout + .cells + .iter() + .filter(|c| unit.contains(&c.start)) + .collect(); + assert!(cells.iter().all(|c| c.column == cells[0].column)); + } + } + + #[test] + fn explicit_profiles_change_wrapping_without_japanese_spacing() { + let columns = |language: &str, spaces_only: bool, texts: &[&str]| { + let mut paragraph = vertical_paragraph( + texts.iter().map(|text| make_span(text)).collect(), + TextAlign::Left, + 1.0, + ); + paragraph.set_language(language.to_string()); + paragraph.set_korean_wrap(if spaces_only { + crate::shapes::text_policy::KoreanWrap::SpacesOnly + } else { + crate::shapes::text_policy::KoreanWrap::Syllable + }); + let content = content_of(vec![paragraph], EM * 2.1, GrowType::Fixed); + let layout = layout_with_height( + &provider(include_bytes!("../../fonts/cjk-locl-test.ttf")), + &content, + EM * 2.1, + ); + ( + layout + .cells + .iter() + .map(|cell| (cell.start, cell.column)) + .collect::>(), + layout, + ) + }; + // Small kana stay attached in Japanese only; Chinese uses its own + // basic punctuation policy even when the fixture contains kana. + let (ja, _) = columns("ja", false, &["文あっ文"]); + let (zh, _) = columns("zh-Hans", false, &["文あっ文"]); + assert_ne!(ja, zh); + let (syllables, _) = columns("ko", false, &["한", "국어 문장"]); + let (words, layout) = columns("ko", true, &["한", "국어 문장"]); + // A three-syllable word cannot fit here: safe emergency wrapping. + assert!(words.iter().any(|(_, column)| *column > 0)); + assert_eq!(syllables.len(), words.len()); + assert!(layout + .cells + .iter() + .all(|cell| cell.min_squeezed_extent <= cell.extent)); + } + + #[test] + fn regional_chinese_spacing_skips_tate_chu_yoko_but_spaces_sideways_latin() { + let tops = |language: &str, text: &str, combine: TextCombineUpright| { + let span = TextSpan { + text_combine_upright: combine, + ..make_span(text) + }; + let mut paragraph = vertical_paragraph(vec![span], TextAlign::Left, 1.0); + paragraph.set_language(language.to_string()); + let content = content_of(vec![paragraph], EM * 10.0, GrowType::Fixed); + let layout = layout_with_height( + &provider(include_bytes!("../../fonts/cjk-locl-test.ttf")), + &content, + EM * 10.0, + ); + layout.cells.iter().map(|cell| cell.top).collect::>() + }; + // Upright digits take one ideograph's cell: no Han/Western gap. + assert_eq!( + tops("zh-Hans-CN", "文12文", TextCombineUpright::Digits), + tops("", "文12文", TextCombineUpright::Digits) + ); + // A sideways Latin run keeps its quarter-em gap. + assert_ne!( + tops("zh-Hans-CN", "文AB文", TextCombineUpright::None), + tops("", "文AB文", TextCombineUpright::None) + ); + } + + #[test] + fn korean_word_boundaries_cross_styles_and_graphemes_stay_together() { + let mut paragraph = vertical_paragraph( + vec![make_span("한글 "), make_span("한"), make_span("국어")], + TextAlign::Left, + 1.0, + ); + paragraph.set_language("ko".to_string()); + paragraph.set_korean_wrap(crate::shapes::text_policy::KoreanWrap::SpacesOnly); + let content = content_of(vec![paragraph], EM * 3.6, GrowType::Fixed); + let layout = layout_with_height( + &provider(include_bytes!("../../fonts/cjk-locl-test.ttf")), + &content, + EM * 3.6, + ); + let word: Vec<_> = layout.cells.iter().filter(|cell| cell.start >= 3).collect(); + assert_eq!(word.len(), 3); + assert!(word.iter().all(|cell| cell.column == word[0].column)); + assert!(word[0].column > 0); + + let mut paragraph = + vertical_paragraph(vec![make_span("ᄒ"), make_span("ᅡᆫ국")], TextAlign::Left, 1.0); + paragraph.set_language("ko".to_string()); + let content = content_of(vec![paragraph], EM, GrowType::Fixed); + let layout = layout_with_height( + &provider(include_bytes!("../../fonts/cjk-locl-test.ttf")), + &content, + EM, + ); + let syllable: Vec<_> = layout.cells.iter().filter(|cell| cell.start < 3).collect(); + assert!(syllable + .iter() + .all(|cell| cell.column == syllable[0].column)); + } + + fn with_headless_fonts(test: impl FnOnce() -> T) -> T { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + test() + } + + #[test] + fn vertical_layout_is_cached_until_the_content_changes() { + with_headless_fonts(|| { + let mut content = make_content(&["あいう"], 100.0); + let rect = Rect::from_xywh(0.0, 0.0, 60.0, 100.0); + let first = content.vertical_layout(&rect); + + assert!(std::rc::Rc::ptr_eq(&first, &content.vertical_layout(&rect))); + assert!( + std::rc::Rc::ptr_eq(&first, &content.clone().vertical_layout(&rect)), + "clones share the cached layout" + ); + let taller = Rect::from_xywh(0.0, 0.0, 60.0, 200.0); + assert!(!std::rc::Rc::ptr_eq( + &first, + &content.vertical_layout(&taller) + )); + + content.paragraphs_mut()[0].children_mut()[0].text = "えお".to_string(); + let edited = content.vertical_layout(&rect); + assert!(!std::rc::Rc::ptr_eq(&first, &edited)); + assert_eq!(edited.cells.len(), 2, "an edit lays out again"); + }); + } + + #[test] + fn vertical_layout_wraps_at_the_given_box_not_the_stored_bounds() { + with_headless_fonts(|| { + // Stored bounds are taller than the shape, as after measuring a fixed text. + let content = make_content(&["あいうえ"], 1000.0); + let wide = content.vertical_layout(&Rect::from_xywh(0.0, 0.0, 60.0, 1000.0)); + let height = wide.cells[0].extent * 2.0 + 1.0; + let layout = content.vertical_layout(&Rect::from_xywh(0.0, 0.0, 60.0, height)); + assert_ne!(layout.cells[0].column, layout.cells[3].column); + }); + } + + #[test] + fn update_layout_of_vertical_text_skips_the_horizontal_layout() { + with_headless_fonts(|| { + let mut content = make_content(&["あいう"], 100.0); + content.update_layout(Rect::from_xywh(0.0, 0.0, 60.0, 100.0)); + assert!(!content.needs_update_layout()); + assert!(content.layout.paragraphs.iter().all(Vec::is_empty)); + }); + } + + fn warichu_span(text: &str) -> TextSpan { + TextSpan { + warichu: true, + text_orientation: TextOrientation::Upright, + ..make_span(text) + } + } + + /// Cells that start a warichu piece, in flow order. + fn warichu_pieces(layout: &VerticalLayout) -> Vec<&VerticalCell> { + let mut pieces: Vec<&VerticalCell> = Vec::new(); + for cell in &layout.cells { + if !matches!(cell.kind, CellKind::Warichu { .. }) { + continue; + } + let same_box = pieces + .last() + .is_some_and(|piece| piece.column == cell.column && piece.top == cell.top); + if !same_box { + pieces.push(cell); + } + } + pieces + } + + #[test] + fn warichu_breaks_at_the_column_end() { + // Four ems of base text leave two ems; ten half-size characters need + // two and a half, so four per sub-line stay and the rest wrap. + let budget = 6.0 * EM; + let content = spans_content( + vec![make_span("くくくく"), warichu_span("あくあくあくあくあく")], + budget, + ); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, budget); + let pieces = warichu_pieces(&layout); + assert_eq!(pieces.len(), 2, "the note breaks into two pieces"); + let (first, second) = (pieces[0], pieces[1]); + assert_eq!(first.column, 0); + assert!((first.top - 4.0 * EM).abs() < 0.01); + assert!(first.top + first.extent <= budget + 0.01); + assert_eq!(first.end - first.start, 8, "four characters per sub-line"); + assert_eq!(second.column, 1); + assert_eq!(second.top, 0.0); + assert_eq!((second.start, second.end), (first.end, 14)); + } + + #[test] + fn warichu_span_with_a_reading_breaks_and_shows_no_ruby() { + let budget = 6.0 * EM; + let note = TextSpan { + ruby: "よみ".to_string(), + ..warichu_span("あくあくあくあくあく") + }; + let content = spans_content(vec![make_span("くくくく"), note], budget); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, budget); + assert_eq!(warichu_pieces(&layout).len(), 2, "the note still breaks"); + assert!(layout.ruby_cells.is_empty(), "warichu shows no ruby"); + } + + #[test] + fn adjacent_warichu_spans_form_one_note() { + let content = spans_content(vec![warichu_span("くあく"), warichu_span("あくあ")], 400.0); + let layout = layout_with_height(&provider(VMTX_TEST_FONT), &content, 400.0); + assert_eq!(warichu_pieces(&layout).len(), 1, "one note, one piece"); + let [first, second] = &layout.cells[..] else { + panic!("one cell per span, got {}", layout.cells.len()); + }; + assert_eq!((first.span, second.span), (0, 1)); + assert_eq!((first.top, first.extent), (second.top, second.extent)); + let ( + CellKind::Warichu { + first_chars: first_span_chars, + first_extent, + .. + }, + CellKind::Warichu { + first_chars: second_span_chars, + second_top, + second_extent, + .. + }, + ) = (first.kind, second.kind) + else { + panic!("expected warichu cells"); + }; + assert_eq!( + first_span_chars, 3, + "the first span fills the first sub-line" + ); + assert_eq!(second_span_chars, 0, "the second span fills the second"); + assert_eq!( + second_top, 0.0, + "the second sub-line starts at the piece top" + ); + assert!((first_extent - second_extent).abs() < 0.01); + assert!( + (first.extent - 3.0 * EM / 2.0).abs() < 0.01, + "three half-size characters per sub-line, got {}", + first.extent + ); + } + + #[test] + fn layout_cells_tile_the_text() { + let text = "縦書きのAB12テスト。"; + let content = make_content(&[text], 1000.0); + let layout = layout_content(&content, 1000.0); + + let mut expected = 0usize; + for cell in &layout.cells { + assert_eq!(cell.paragraph, 0); + assert_eq!(cell.start, expected, "cells must tile without gaps"); + assert!(cell.end > cell.start); + expected = cell.end; + } + assert_eq!(expected, text.encode_utf16().count()); + } + + #[test] + fn layout_columns_respect_wrap_height() { + let content = make_content(&["あいうえおかきくけこ"], 100.0); + let layout = layout_content(&content, 60.0); + + assert!(layout.columns.len() > 1, "content must wrap into columns"); + for column_index in 0..layout.columns.len() { + let bottom = layout + .cells + .iter() + .filter(|c| c.column == column_index) + .map(|c| c.top + c.extent) + .fold(0.0f32, f32::max); + assert!(bottom <= 60.0 + 0.01, "column overflows the wrap height"); + } + // Columns advance leftward: column 0 is the rightmost. + assert!(layout.columns[0].x > layout.columns[1].x); + let total: f32 = layout.columns.iter().map(|c| c.width).sum(); + assert!((layout.width - total).abs() < 0.01); + } + + #[test] + fn layout_each_paragraph_starts_a_new_column() { + let content = make_content(&["あい", "うえ"], 1000.0); + let layout = layout_content(&content, 1000.0); + + assert_eq!(layout.paragraph_columns.len(), 2); + let (p0_start, p0_end) = layout.paragraph_columns[0]; + let (p1_start, _) = layout.paragraph_columns[1]; + assert_eq!(p0_start, 0); + assert_eq!(p0_end, p1_start); + assert!(layout + .cells + .iter() + .all(|c| (c.paragraph == 0) == (c.column < p0_end))); + } + + #[test] + fn layout_empty_paragraph_still_takes_a_column() { + let content = make_content(&["あ", "", "い"], 1000.0); + let layout = layout_content(&content, 1000.0); + assert_eq!(layout.columns.len(), 3); + assert_eq!(layout.paragraph_columns[1], (1, 2)); + } + + #[test] + fn block_axis_alignment_maps_start_center_end_to_right_center_left() { + assert_eq!(block_axis_offset(200.0, 40.0, VerticalAlign::Top), 160.0); + assert_eq!(block_axis_offset(200.0, 40.0, VerticalAlign::Center), 80.0); + assert_eq!(block_axis_offset(200.0, 40.0, VerticalAlign::Bottom), 0.0); + assert_eq!(block_axis_offset(20.0, 40.0, VerticalAlign::Top), -20.0); + assert_eq!(block_axis_offset(20.0, 40.0, VerticalAlign::Center), -10.0); + assert_eq!(block_axis_offset(20.0, 40.0, VerticalAlign::Bottom), 0.0); + } + + #[test] + fn wrapped_vertical_content_grows_across_columns() { + let content = make_content(&["あいうえおかきくけこ"], 60.0); + let layout = layout_content(&content, 60.0); + assert!(layout.columns.len() > 1); + assert!(layout.width > layout.columns[0].width); + assert!(layout.height <= 60.0 + 0.01); + } +} diff --git a/render-wasm/src/shapes/text_vertical/mod.rs b/render-wasm/src/shapes/text_vertical/mod.rs new file mode 100644 index 0000000000..e8b5cab249 --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/mod.rs @@ -0,0 +1,48 @@ +// Vertical (tategaki) text layout: columns flow top->bottom and advance +// right->left. Skia's skparagraph has no vertical writing mode, so this +// module owns the whole vertical pipeline: +// +// segment spans by glyph orientation and shape each segment with +// SkShaper, OpenType `vert`/`vrt2` for upright runs (`text_shaping`) -> +// derive cells: one per upright glyph cluster, one per rotated run, one per +// composite (`cells`) -> JLREQ spacing and kinsoku-aware column breaks +// (`flow`) -> place columns and annotations (`layout`, `annotations`) -> +// paint / outline export (`paint`) and position data / hit-testing +// (`positions`) from the cells. +// +// Offsets: cells use UTF-16 offsets in transformed layout text. Position +// data maps those ranges back to the original span text before it crosses +// the WASM boundary; the WORD JOINER OffsetMap applies only to +// skparagraph-driven horizontal breaks. +// +// Layouts are computed on demand, as the horizontal path rebuilds its +// skparagraph objects per paint; only per-typeface font tables are cached. +// +// Text-align aligns each column's glyphs along the vertical (inline) axis: +// Left/Start->top, Center->middle, Right/End->bottom of the wrap budget. +// Justify stretches every column but the last to fill the wrap budget. +// +// Letter-spacing adds advance along the column once per upright cluster +// and once per glyph inside a rotated run, matching the horizontal +// `letter-spacing` that Skia applies to each glyph advance. +// +// Not supported yet: PDF/vector emoji overlays and inner shadows. + +mod annotations; +mod cells; +mod flow; +mod layout; +mod paint; +mod positions; + +#[cfg(test)] +pub(crate) mod test_support; + +#[cfg(test)] +pub(crate) use layout::CellKind; +pub use layout::{block_axis_offset, layout_for_rect, VerticalLayout}; +pub use paint::{ + paint_drop_shadow, paint_grid, paint_layout, paint_stroke, paint_text_vertical, + paint_text_vertical_with, vertical_text_paths, GlyphOutput, +}; +pub use positions::{caret_from_point, caret_rect, intersects, position_data, range_rects}; diff --git a/render-wasm/src/shapes/text_vertical/paint.rs b/render-wasm/src/shapes/text_vertical/paint.rs new file mode 100644 index 0000000000..40df7f4c1c --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/paint.rs @@ -0,0 +1,1105 @@ +use skia_safe::{ + self as skia, canvas::SaveLayerRec, textlayout::Paragraph as SkiaParagraph, + textlayout::TextDecoration, BlendMode, Canvas, ImageFilter, Matrix, Paint, Point as SkPoint, + TextBlob, +}; + +use crate::math::Rect; +use crate::shapes::{Stroke, StrokeKind, TextContent, VerticalAlign}; + +use super::annotations::{emphasis_mark_center, EmphasisMark, RubyCell}; +use super::layout::{column_base_center, CellKind, VerticalCell, VerticalLayout}; +use crate::shapes::text_shaping::{single_glyph_blob, ShapedRun}; +use crate::shapes::text_shaping::{upright_baseline, upright_centre_metrics}; + +/// How glyphs reach the canvas. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum GlyphOutput { + /// Text blobs. + Text, + /// Glyph outlines. SVG export needs them: `SkSVGDevice` rebuilds `` + /// from glyph IDs through the font's cmap, which loses vertical + /// alternates. + Outlines, +} + +/// One text blob drawn at `offset` in the local space that `transform` maps +/// to the layout's content space (identity when `None`). Canvas painting and +/// outline export share these draws, so they match for every cell kind. +struct GlyphDraw { + blob: TextBlob, + offset: SkPoint, + transform: Option, +} + +impl GlyphDraw { + fn at(blob: TextBlob, offset: impl Into) -> Self { + Self { + blob, + offset: offset.into(), + transform: None, + } + } + + fn transformed(blob: TextBlob, offset: impl Into, transform: Matrix) -> Self { + Self { + blob, + offset: offset.into(), + transform: Some(transform), + } + } + + /// Draws with the content origin at `origin`. + fn draw(&self, canvas: &Canvas, paint: &Paint, origin: SkPoint, output: GlyphOutput) { + if output == GlyphOutput::Outlines { + canvas.draw_path(&self.path(origin), paint); + return; + } + match &self.transform { + None => { + canvas.draw_text_blob(&self.blob, self.offset + origin, paint); + } + Some(transform) => { + canvas.save(); + canvas.translate(origin); + canvas.concat(transform); + canvas.draw_text_blob(&self.blob, self.offset, paint); + canvas.restore(); + } + } + } + + /// Outline with the content origin at `origin`. + fn path(&self, origin: SkPoint) -> skia::Path { + match &self.transform { + None => text_blob_path(self.blob.clone(), self.offset + origin), + Some(transform) => { + let mut transform = *transform; + transform.post_translate(origin); + text_blob_path(self.blob.clone(), self.offset).make_transform(&transform) + } + } + } +} + +/// Glyph draws of a layout with its content origin at (0, 0), indexed like +/// its cells, ruby cells and emphasis marks. Built on the first paint. +pub(super) struct LayoutDraws { + cells: Vec>, + ruby: Vec>, + emphasis: Vec>, +} + +impl LayoutDraws { + fn new(layout: &VerticalLayout) -> Self { + let origin = (0.0, 0.0); + Self { + cells: layout + .cells + .iter() + .map(|cell| cell_draws(layout, cell, origin)) + .collect(), + ruby: layout + .ruby_cells + .iter() + .map(|ruby| ruby_draws(layout, ruby, origin)) + .collect(), + emphasis: layout + .emphasis_marks + .iter() + .map(|mark| emphasis_draw(layout, mark, origin)) + .collect(), + } + } +} + +fn layout_draws(layout: &VerticalLayout) -> &LayoutDraws { + layout.draws.get_or_init(|| LayoutDraws::new(layout)) +} + +/// Glyphs on their side: +x of the blob runs down the column from `origin`. +fn rotated_at(origin: (f32, f32)) -> Matrix { + let mut matrix = Matrix::translate(origin); + matrix.pre_rotate(90.0, None); + matrix +} + +/// Glyph draws of one cell, for a layout whose content origin is `origin`. +fn cell_draws(layout: &VerticalLayout, cell: &VerticalCell, origin: (f32, f32)) -> Vec { + let column = &layout.columns[cell.column]; + let x_center = origin.0 + column_base_center(column); + let y_top = origin.1 + cell.top; + match cell.kind { + CellKind::Upright { run, glyph, count } => { + let run = &layout.runs[run]; + let baseline = + y_top + cell.glyph_flow_shift + upright_baseline(&run.font, cell.font_size); + run.cluster_blob(glyph, count) + .map(|blob| GlyphDraw::at(blob, (x_center - cell.h_advance / 2.0, baseline))) + .into_iter() + .collect() + } + CellKind::SyntheticRotated { run, glyph, count } => { + let run = &layout.runs[run]; + run.cluster_blob(glyph, count) + .map(|blob| { + GlyphDraw::transformed( + blob, + (0.0, run.cluster_rotated_baseline_shift(glyph, count)), + rotated_at((x_center, y_top + cell.glyph_flow_shift)), + ) + }) + .into_iter() + .collect() + } + CellKind::Rotated { run } => { + let run = &layout.runs[run]; + // After rotation +x runs down the column and +y runs across it; + // the shift puts the font's central baseline on the column axis. + run.blob() + .map(|blob| { + GlyphDraw::transformed( + blob, + (0.0, run.rotated_baseline_shift), + rotated_at((x_center, y_top)), + ) + }) + .into_iter() + .collect() + } + CellKind::TateChuYoko { + run_start, + run_count, + h_scale, + } => { + let composite = &layout.runs[run_start..run_start + run_count]; + let combined_advance: f32 = composite.iter().map(|run| run.advance).sum(); + // Vertical centring uses the tallest run's em box, the metrics + // upright glyphs use, so the composite shares their baseline. + let (ascent, descent) = composite.iter().fold((0.0f32, 0.0f32), |(a, d), run| { + let (ascent, descent) = upright_centre_metrics(&run.font); + (a.min(ascent), d.max(descent)) + }); + let x0 = x_center - (combined_advance * h_scale) / 2.0; + let baseline = y_top + cell.font_size / 2.0 - (ascent + descent) / 2.0; + let mut transform = Matrix::translate((x0, baseline)); + transform.pre_scale((h_scale, 1.0), None); + let mut cursor = 0.0f32; + let mut draws = Vec::with_capacity(composite.len()); + for run in composite { + if let Some(blob) = run.blob() { + draws.push(GlyphDraw::transformed(blob, (cursor, 0.0), transform)); + } + cursor += run.advance; + } + draws + } + CellKind::Warichu { + run_start, + run_count, + first_count, + first_top, + second_top, + .. + } => { + let (first, second) = + layout.runs[run_start..run_start + run_count].split_at(first_count); + let quarter = cell.font_size / 4.0; + // vertical-rl: the first sub-line reads first, on the right half. + let mut draws = warichu_line_draws(first, x_center + quarter, y_top + first_top); + draws.extend(warichu_line_draws( + second, + x_center - quarter, + y_top + second_top, + )); + draws + } + } +} + +/// One warichu sub-line: half-size upright glyphs stacked down the +/// sub-column centred on `x_center`, one cluster per cell. +fn warichu_line_draws(runs: &[ShapedRun], x_center: f32, y_top: f32) -> Vec { + let mut draws = Vec::new(); + let mut cursor = y_top; + for run in runs { + let font_size = run.font.size(); + let baseline_offset = upright_baseline(&run.font, font_size); + for (glyph, count) in run.cluster_spans() { + let advance = match run.cluster_advance(glyph, count) { + advance if advance > 0.0 => advance, + _ => font_size, + }; + if let Some(blob) = run.cluster_blob(glyph, count) { + draws.push(GlyphDraw::at( + blob, + (x_center - advance / 2.0, cursor + baseline_offset), + )); + } + cursor += advance; + } + } + draws +} + +/// One column's slice of a ruby annotation in the column's ruby gutter: +/// upright glyphs stacked, Western words on their side. Flow positions come +/// from the whole base span; each glyph is centred across the gutter by its +/// own fallback-font run. +fn ruby_draws(layout: &VerticalLayout, ruby: &RubyCell, origin: (f32, f32)) -> Vec { + let column = &layout.columns[ruby.column]; + if let Some(origins) = &ruby.zhuyin_origins { + return ruby + .glyphs + .iter() + .zip(origins) + .filter_map(|(glyph, offset)| { + let run = &layout.ruby_runs[glyph.run]; + Some(GlyphDraw::at( + run.glyph_blob(glyph.glyph)?, + ( + origin.0 + ruby.strip_x(column) + offset.x, + origin.1 + offset.y, + ), + )) + }) + .collect(); + } + let gutter_center = origin.0 + ruby.strip_x(column) + ruby.font_size / 2.0; + ruby.glyphs + .iter() + .zip(&ruby.glyph_tops) + .filter_map(|(ruby_glyph, top)| { + let run = &layout.ruby_runs[ruby_glyph.run]; + let blob = run.glyph_blob(ruby_glyph.glyph)?; + if ruby_glyph.sideways { + return Some(GlyphDraw::transformed( + blob, + (0.0, run.rotated_baseline_shift), + rotated_at((gutter_center, origin.1 + top)), + )); + } + let advance = run + .advances + .get(ruby_glyph.glyph) + .copied() + .unwrap_or(ruby.font_size); + Some(GlyphDraw::at( + blob, + ( + gutter_center - advance / 2.0, + origin.1 + top + upright_baseline(&run.font, ruby.font_size), + ), + )) + }) + .collect() +} + +/// One emphasis mark (圏点) centred on its base cell's flow extent, in the +/// column's right-side gutter. +fn emphasis_draw( + layout: &VerticalLayout, + mark: &EmphasisMark, + origin: (f32, f32), +) -> Option { + let run = &layout.emphasis_runs[mark.run]; + let blob = single_glyph_blob(&run.font, *run.glyphs.first()?)?; + let (center_x, center_y) = emphasis_mark_center(layout, mark); + let gutter_center = origin.0 + center_x; + let (_, metrics) = run.font.metrics(); + let baseline = origin.1 + center_y - (metrics.ascent + metrics.descent) / 2.0; + let advance = run.advances.first().copied().unwrap_or(0.0); + Some(GlyphDraw::at( + blob, + (gutter_center - advance / 2.0, baseline), + )) +} + +/// Underline and line-through bars of a cell. +fn decoration_rects( + layout: &VerticalLayout, + cell: &VerticalCell, + origin: (f32, f32), +) -> Vec { + let Some(decoration) = cell.decoration else { + return Vec::new(); + }; + let mut rects = Vec::new(); + if decoration.contains(TextDecoration::UNDERLINE) { + rects.push(decoration_bar(layout, cell, origin.0, origin.1, false)); + } + if decoration.contains(TextDecoration::LINE_THROUGH) { + rects.push(decoration_bar(layout, cell, origin.0, origin.1, true)); + } + rects +} + +/// A glyph draw or a decoration bar of the layout. +enum LayoutItem<'a> { + Glyphs(&'a GlyphDraw), + Bar(skia::Rect), +} + +/// Every glyph draw and decoration bar of the layout, with its content origin +/// at `origin`, and the fill paint of its span. Paint order: each cell's +/// glyphs and bars, then ruby, then emphasis marks. +fn for_each_item( + layout: &VerticalLayout, + origin: (f32, f32), + mut visit: impl FnMut(LayoutItem, &Paint), +) { + let draws = layout_draws(layout); + for (cell, cell_draws) in layout.cells.iter().zip(&draws.cells) { + let paint = &layout.paints[cell.paint]; + for draw in cell_draws { + visit(LayoutItem::Glyphs(draw), paint); + } + for rect in decoration_rects(layout, cell, origin) { + visit(LayoutItem::Bar(rect), paint); + } + } + for (ruby, ruby_draws) in layout.ruby_cells.iter().zip(&draws.ruby) { + for draw in ruby_draws { + visit(LayoutItem::Glyphs(draw), &layout.paints[ruby.paint]); + } + } + for (mark, draw) in layout.emphasis_marks.iter().zip(&draws.emphasis) { + if let Some(draw) = draw { + visit( + LayoutItem::Glyphs(draw), + &layout.paints[layout.cells[mark.cell].paint], + ); + } + } +} + +/// Paint every item of the layout with its span's fill paint, or with +/// `single_paint` for every item (stroke, shadow and silhouette passes). +fn paint_items( + canvas: &Canvas, + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + single_paint: Option<&Paint>, + output: GlyphOutput, +) { + let origin = layout.origin(bounds, vertical_align); + for_each_item(layout, origin, |item, fill| { + let paint = single_paint.unwrap_or(fill); + match item { + LayoutItem::Glyphs(draw) => draw.draw(canvas, paint, origin.into(), output), + LayoutItem::Bar(rect) => { + let mut bar_paint = paint.clone(); + if single_paint.is_none() { + bar_paint.set_style(skia::PaintStyle::Fill); + } + bar_paint.set_anti_alias(true); + canvas.draw_rect(rect, &bar_paint); + } + } + }); +} + +/// Fill pass: every glyph and decoration with its span's fill paint. +pub fn paint_layout( + canvas: &Canvas, + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + output: GlyphOutput, +) { + paint_items(canvas, layout, bounds, vertical_align, None, output); +} + +/// Paint every glyph and decoration with one `paint` (stroke / shadow +/// silhouette passes). +fn paint_glyphs( + canvas: &Canvas, + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + paint: &Paint, + output: GlyphOutput, +) { + paint_items(canvas, layout, bounds, vertical_align, Some(paint), output); +} + +/// Paint the text content vertically inside its bounds. Returns false +/// when the content is not vertical, so the caller falls back to the +/// horizontal path. +pub fn paint_text_vertical( + canvas: &Canvas, + text_content: &TextContent, + bounds: &Rect, + vertical_align: VerticalAlign, + output: GlyphOutput, +) -> bool { + if !text_content.is_vertical() { + return false; + } + let layout = text_content.vertical_layout(bounds); + paint_layout(canvas, &layout, bounds, vertical_align, output); + true +} + +/// Paint the glyphs of vertical text content with one `paint` (a stroke or +/// an opaque silhouette) instead of the span fills. Returns false when the +/// content is not vertical. +pub fn paint_text_vertical_with( + canvas: &Canvas, + text_content: &TextContent, + bounds: &Rect, + vertical_align: VerticalAlign, + paint: &Paint, + output: GlyphOutput, +) -> bool { + if !text_content.is_vertical() { + return false; + } + let layout = text_content.vertical_layout(bounds); + paint_glyphs(canvas, &layout, bounds, vertical_align, paint, output); + true +} + +/// Glyph-outline paths of the layout, in the same order and with the same +/// paints as the canvas fill pass. +fn paths_from_layout( + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + antialias: bool, +) -> Vec<(skia::Path, Paint)> { + let mut paths = Vec::new(); + let origin = layout.origin(bounds, vertical_align); + for_each_item(layout, origin, |item, paint| { + let path = match item { + LayoutItem::Glyphs(draw) => draw.path(origin.into()), + LayoutItem::Bar(rect) => skia::Path::rect(rect, None), + }; + push_text_path(&mut paths, path, paint, antialias); + }); + paths +} + +/// Glyph-outline paths of the vertical layout, matching the canvas pass. +pub fn vertical_text_paths( + text_content: &TextContent, + vertical_align: VerticalAlign, + antialias: bool, +) -> Vec<(skia::Path, Paint)> { + if !text_content.is_vertical() { + return Vec::new(); + } + let bounds = text_content.bounds(); + let layout = text_content.vertical_layout(&bounds); + paths_from_layout(&layout, &bounds, vertical_align, antialias) +} + +/// Developer overlay: a jlreq-style character-frame grid. Outlines each +/// column band and, per cell, the advance box, the virtual body / em square +/// centred on the column axis, and the glyph-ink band, so aki and +/// letter-spacing are visible. Only the on-screen fills pass calls it, behind +/// the `TEXT_GRID_VISIBLE` render flag; it never reaches exported output. +pub fn paint_grid(canvas: &Canvas, layout: &VerticalLayout, bounds: &Rect, align: VerticalAlign) { + let (origin_x, origin_y) = layout.origin(bounds, align); + + let mut column_paint = Paint::default(); + column_paint.set_anti_alias(true); + column_paint.set_style(skia::PaintStyle::Stroke); + column_paint.set_stroke_width(1.0); + column_paint.set_color(skia::Color::from_argb(0x55, 0x88, 0x88, 0x88)); + + let mut advance_paint = Paint::default(); + advance_paint.set_anti_alias(true); + advance_paint.set_style(skia::PaintStyle::Stroke); + advance_paint.set_stroke_width(1.0); + advance_paint.set_color(skia::Color::from_argb(0xAA, 0x2F, 0x80, 0xED)); + + let mut em_paint = Paint::default(); + em_paint.set_anti_alias(true); + em_paint.set_style(skia::PaintStyle::Stroke); + em_paint.set_stroke_width(1.0); + em_paint.set_color(skia::Color::from_argb(0x99, 0xEB, 0x57, 0x57)); + + let mut ink_paint = Paint::default(); + ink_paint.set_anti_alias(true); + ink_paint.set_style(skia::PaintStyle::Stroke); + ink_paint.set_stroke_width(1.0); + ink_paint.set_color(skia::Color::from_argb(0x77, 0x27, 0xAE, 0x60)); + + // Column bands over the full used height. + for column in &layout.columns { + let x = origin_x + column.x; + canvas.draw_rect( + Rect::from_xywh(x, origin_y, column.width, layout.height), + &column_paint, + ); + } + + for cell in &layout.cells { + let column = &layout.columns[cell.column]; + let x_left = origin_x + column.x; + let y_top = origin_y + cell.top; + let x_center = origin_x + column_base_center(column); + + // Advance box: the real layout cell along the column axis. + canvas.draw_rect( + Rect::from_xywh(x_left, y_top, column.base_width, cell.extent), + &advance_paint, + ); + + // Virtual body / em square, centred on the column axis and on the + // cell's advance so aki and letter-spacing are visible. + let em = cell.font_size.max(1.0); + canvas.draw_rect( + Rect::from_xywh( + x_center - em / 2.0, + y_top + (cell.extent - em) / 2.0, + em, + em, + ), + &em_paint, + ); + + // Glyph-ink band along the flow axis. + if cell.ink_bottom > cell.ink_top { + canvas.draw_line( + (x_left, origin_y + cell.top + cell.ink_top), + (x_left + column.width, origin_y + cell.top + cell.ink_top), + &ink_paint, + ); + canvas.draw_line( + (x_left, origin_y + cell.top + cell.ink_bottom), + (x_left + column.width, origin_y + cell.top + cell.ink_bottom), + &ink_paint, + ); + } + } +} + +/// Paint the vertical glyph shadows. The shadow paint carries the +/// blur/offset image filter, so glyphs draw through it with no offscreen +/// layer (a `save_layer` with the filter can exceed GPU limits on tall +/// columns). +pub fn paint_drop_shadow( + canvas: &Canvas, + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + shadow_paint: &Paint, + output: GlyphOutput, +) { + let mut paint = shadow_paint.clone(); + paint.set_color(skia::Color::BLACK); + paint.set_anti_alias(true); + paint_glyphs(canvas, layout, bounds, vertical_align, &paint, output); +} + +/// Paint a stroke on the vertical glyphs. Center strokes draw directly; +/// inner/outer strokes mask with `SrcIn` / `SrcOut` against the glyph +/// silhouette, as the horizontal path does. +#[allow(clippy::too_many_arguments)] +pub fn paint_stroke( + canvas: &Canvas, + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + stroke: &Stroke, + selrect: &Rect, + blur: Option<&ImageFilter>, + output: GlyphOutput, +) { + let (stroke_paints, layer_opacity) = + crate::render::text::get_text_stroke_paints(stroke, selrect, false); + + if let Some(blur_filter) = blur { + let mut blur_paint = Paint::default(); + blur_paint.set_image_filter(blur_filter.clone()); + canvas.save_layer(&SaveLayerRec::default().paint(&blur_paint)); + } + if let Some(opacity) = layer_opacity { + let mut opacity_paint = Paint::default(); + opacity_paint.set_alpha_f(opacity); + canvas.save_layer(&SaveLayerRec::default().paint(&opacity_paint)); + } + + for stroke_paint in &stroke_paints { + match stroke.kind { + StrokeKind::Center => { + paint_glyphs(canvas, layout, bounds, vertical_align, stroke_paint, output) + } + StrokeKind::Inner => paint_masked_stroke( + canvas, + layout, + bounds, + vertical_align, + stroke_paint, + BlendMode::SrcIn, + output, + ), + StrokeKind::Outer => paint_masked_stroke( + canvas, + layout, + bounds, + vertical_align, + stroke_paint, + BlendMode::SrcOut, + output, + ), + } + } + + if layer_opacity.is_some() { + canvas.restore(); + } + if blur.is_some() { + canvas.restore(); + } +} + +#[allow(clippy::too_many_arguments)] +fn paint_masked_stroke( + canvas: &Canvas, + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + stroke_paint: &Paint, + blend: BlendMode, + output: GlyphOutput, +) { + let mut mask = Paint::default(); + mask.set_color(skia::Color::BLACK); + mask.set_anti_alias(true); + + canvas.save_layer(&SaveLayerRec::default()); + paint_glyphs(canvas, layout, bounds, vertical_align, &mask, output); + + let mut blend_paint = Paint::default(); + blend_paint.set_blend_mode(blend); + canvas.save_layer(&SaveLayerRec::default().paint(&blend_paint)); + paint_glyphs(canvas, layout, bounds, vertical_align, stroke_paint, output); + canvas.restore(); + + canvas.restore(); +} + +fn text_blob_path(mut blob: TextBlob, offset: impl Into) -> skia::Path { + // get_path is relative to the blob's ink bounds; add them back to match. + let bounds = *blob.bounds(); + let offset = offset.into(); + SkiaParagraph::get_path(&mut blob).with_offset((offset.x + bounds.left, offset.y + bounds.top)) +} + +fn push_text_path( + paths: &mut Vec<(skia::Path, Paint)>, + path: skia::Path, + paint: &Paint, + antialias: bool, +) { + if path.is_empty() { + return; + } + let mut paint = paint.clone(); + paint.set_anti_alias(antialias); + paths.push((path, paint)); +} + +/// Decoration bar of a cell in absolute coordinates. Underline runs along +/// the selected side of the column; line-through runs down the centre. +/// Both span the cell's extent, so adjacent decorated cells form one bar. +fn decoration_bar( + layout: &VerticalLayout, + cell: &VerticalCell, + origin_x: f32, + origin_y: f32, + line_through: bool, +) -> skia::Rect { + let column = &layout.columns[cell.column]; + let x_center = origin_x + column_base_center(column); + let y_top = origin_y + cell.top; + let thickness = (cell.font_size * 0.06).max(1.0); + let bar_x = if line_through { + x_center + } else { + let sign = if cell.underline_side == crate::shapes::AnnotationSide::Over { + 1.0 + } else { + -1.0 + }; + x_center + sign * (cell.font_size / 2.0 + cell.font_size / 9.0) + }; + skia::Rect::from_ltrb( + bar_x - thickness / 2.0, + y_top, + bar_x + thickness / 2.0, + y_top + cell.extent, + ) +} + +#[cfg(test)] +mod tests { + use super::super::test_support::*; + use super::*; + use crate::shapes::{StrokeStyle, TextCombineUpright, TextEmphasis, TextOrientation, TextSpan}; + + #[test] + fn glyph_draws_are_built_once_and_follow_the_origin() { + let content = make_content(&["あくABC"], 1000.0); + let provider = provider_with_fallback(TEST_FONT, VMTX_TEST_FONT); + let layout = layout_with_fallback(&provider, &content, 1000.0, &["fallback".to_string()]); + let bounds = content.bounds(); + assert!(layout.draws.get().is_none()); + + let paths = paths_from_layout(&layout, &bounds, VerticalAlign::Top, true); + let draws = layout + .draws + .get() + .expect("the first paint builds the draws") as *const _; + let shifted = paths_from_layout( + &layout, + &bounds.with_offset((30.0, 40.0)), + VerticalAlign::Top, + true, + ); + + assert!(std::ptr::eq(draws, layout.draws.get().unwrap())); + assert!(paths.len() >= 3, "upright and sideways glyphs are drawn"); + assert_eq!(paths.len(), shifted.len()); + for ((path, _), (moved, _)) in paths.iter().zip(&shifted) { + let (a, b) = (path.bounds(), moved.bounds()); + assert!((b.left - a.left - 30.0).abs() < 0.01, "{a:?} -> {b:?}"); + assert!((b.top - a.top - 40.0).abs() < 0.01, "{a:?} -> {b:?}"); + } + } + + #[test] + fn shape_to_path_places_vertical_glyphs_down_the_column() { + let content = make_content(&["あく"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + + let paths = paths_from_layout(&layout, &content.bounds(), VerticalAlign::Top, true); + + assert_eq!(paths.len(), 2, "one outline path per upright glyph"); + let first = paths[0].0.bounds(); + let second = paths[1].0.bounds(); + assert!( + second.top > first.top, + "the second glyph outline follows the first down the vertical flow axis" + ); + let horizontal_shift = (second.center_x() - first.center_x()).abs(); + let vertical_shift = second.center_y() - first.center_y(); + assert!( + vertical_shift > horizontal_shift, + "vertical flow dominates the glyphs' optical side-bearing difference" + ); + } + + #[test] + fn shape_to_path_preserves_the_text_blob_draw_origin() { + let content = make_content(&["あ"], 1000.0); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let CellKind::Upright { run, glyph, count } = layout.cells[0].kind else { + panic!("expected an upright cell"); + }; + let blob = layout.runs[run] + .cluster_blob(glyph, count) + .expect("a glyph text blob"); + let blob_bounds = *blob.bounds(); + let draw_origin = SkPoint::new(120.0, 340.0); + let mut normalized_blob = blob.clone(); + let normalized_bounds = *SkiaParagraph::get_path(&mut normalized_blob).bounds(); + + let path_bounds = *text_blob_path(blob, draw_origin).bounds(); + + assert!( + (path_bounds.left - (draw_origin.x + blob_bounds.left + normalized_bounds.left)).abs() + < 0.01 + ); + assert!( + (path_bounds.top - (draw_origin.y + blob_bounds.top + normalized_bounds.top)).abs() + < 0.01 + ); + assert!((path_bounds.width() - normalized_bounds.width()).abs() < 0.01); + assert!((path_bounds.height() - normalized_bounds.height()).abs() < 0.01); + } + + #[test] + fn underline_side_changes_the_bar_without_moving_the_base_or_strike() { + let mut content = decorated_content("中文", TextDecoration::UNDERLINE); + let left = layout_content(&content, 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_underline_position = + crate::shapes::TextUnderlinePosition::Right; + let right = layout_content(&content, 400.0); + let left_bar = decoration_bar(&left, &left.cells[0], 0.0, 0.0, false); + let right_bar = decoration_bar(&right, &right.cells[0], 0.0, 0.0, false); + let center = column_base_center(&left.columns[0]); + assert!(left_bar.center_x() < center); + assert!(right_bar.center_x() > center); + assert_eq!(left.cells[0].top, right.cells[0].top); + assert_eq!( + decoration_bar(&left, &left.cells[0], 0.0, 0.0, true), + decoration_bar(&right, &right.cells[0], 0.0, 0.0, true) + ); + } + + #[test] + fn decoration_bar_underline_left_of_center_line_through_centered() { + let content = decorated_content("あい", TextDecoration::UNDERLINE); + let layout = layout_content(&content, 1000.0); + let (ox, oy) = layout.origin(&content.bounds(), VerticalAlign::Top); + let cell = &layout.cells[0]; + let column = &layout.columns[cell.column]; + let x_center = ox + column.x + column.width / 2.0; + + let underline = decoration_bar(&layout, cell, ox, oy, false); + let strike = decoration_bar(&layout, cell, ox, oy, true); + + assert!(underline.center_x() < x_center); + assert!((strike.center_x() - x_center).abs() < 0.01); + assert!((underline.height() - cell.extent).abs() < 0.01); + assert!(underline.width() >= 1.0 - 0.01); + } + + #[test] + fn outline_export_includes_every_annotation_glyph() { + let content = spans_content( + vec![TextSpan { + ruby: "aあ".to_string(), + text_emphasis: TextEmphasis::FilledDot, + text_orientation: TextOrientation::Upright, + ..make_span("AB") + }], + 400.0, + ); + let provider = provider_with_fallback(TEST_FONT, VMTX_TEST_FONT); + let layout = layout_with_fallback(&provider, &content, 400.0, &["fallback".to_string()]); + let ruby_glyphs: usize = layout.ruby_cells.iter().map(|ruby| ruby.glyphs.len()).sum(); + assert_eq!(ruby_glyphs, 2, "both fallback runs carry a ruby glyph"); + + let paths = paths_from_layout(&layout, &content.bounds(), VerticalAlign::Top, true); + + assert_eq!( + paths.len(), + layout.cells.len() + ruby_glyphs + layout.emphasis_marks.len(), + "one outline per base cell, ruby glyph and emphasis mark" + ); + let mut surface = skia::surfaces::raster_n32_premul((256, 256)).unwrap(); + paint_layout( + surface.canvas(), + &layout, + &content.bounds(), + VerticalAlign::Top, + GlyphOutput::Text, + ); + } + + /// Pixels with any ink inside `rect`. + fn inked_pixels(surface: &mut skia::Surface, rect: skia::Rect) -> usize { + let image = surface.image_snapshot(); + let pixmap = image.peek_pixels().expect("raster pixels"); + let mut inked = 0; + for y in rect.top.max(0.0) as i32..rect.bottom.min(image.height() as f32) as i32 { + for x in rect.left.max(0.0) as i32..rect.right.min(image.width() as f32) as i32 { + if pixmap.get_color((x, y)).a() > 0 { + inked += 1; + } + } + } + inked + } + + #[test] + fn upright_ruby_glyphs_sit_in_their_em_slot_like_base_glyphs() { + // JLREQ Fig 122 and 141: hanging a ruby glyph from the font ascent + // put CJK faces (ascent larger than the em) about a quarter em below + // their slot, onto the next kanji and past the column end. + let content = spans_content( + vec![TextSpan { + ruby: "あく".to_string(), + ..make_span("くく") + }], + 400.0, + ); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let ruby = &layout.ruby_cells[0]; + let draws = ruby_draws(&layout, ruby, (0.0, 0.0)); + assert_eq!(draws.len(), 2); + for ((draw, glyph), top) in draws.iter().zip(&ruby.glyphs).zip(&ruby.glyph_tops) { + let run = &layout.ruby_runs[glyph.run]; + let expected = top + upright_baseline(&run.font, ruby.font_size); + assert!( + (draw.offset.y - expected).abs() < 0.01, + "baseline {} instead of {expected}", + draw.offset.y + ); + } + } + + #[test] + fn tate_chu_yoko_digits_share_the_upright_glyph_baseline() { + // The hhea ascent of a CJK face exceeds the em; centring the + // composite on it set digits about 0.07em below the kanji beside them. + let mut content = make_content_with_spans(&["く12く"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::Digits; + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let cell = layout + .cells + .iter() + .find(|cell| matches!(cell.kind, CellKind::TateChuYoko { .. })) + .expect("the digit run combines"); + let CellKind::TateChuYoko { run_start, .. } = cell.kind else { + unreachable!() + }; + let draws = cell_draws(&layout, cell, (0.0, 0.0)); + let transform = draws[0].transform.expect("a composite is transformed"); + let expected = cell.top + upright_baseline(&layout.runs[run_start].font, cell.font_size); + assert!( + (transform.translate_y() - expected).abs() < 0.01, + "baseline {} instead of {expected}", + transform.translate_y() + ); + } + + #[test] + fn single_paint_passes_cover_ruby_like_the_fill_pass() { + let content = spans_content( + vec![TextSpan { + ruby: "あ".to_string(), + ..make_span("く") + }], + 400.0, + ); + let layout = layout_with(&provider(VMTX_TEST_FONT), &content); + let bounds = content.bounds(); + let ruby = &layout.ruby_cells[0]; + let column = &layout.columns[ruby.column]; + let (origin_x, origin_y) = layout.origin(&bounds, VerticalAlign::Top); + let strip_x = origin_x + ruby.strip_x(column); + let strip = skia::Rect::from_xywh(strip_x, origin_y, ruby.font_size, 400.0); + + let mut surface = skia::surfaces::raster_n32_premul((256, 512)).unwrap(); + let mut stroke = Paint::default(); + stroke.set_color(skia::Color::BLACK); + paint_glyphs( + surface.canvas(), + &layout, + &bounds, + VerticalAlign::Top, + &stroke, + GlyphOutput::Text, + ); + + assert!(inked_pixels(&mut surface, strip) > 0, "the ruby is painted"); + } + + #[test] + fn paint_passes_do_not_panic() { + let content = decorated_content("あいAB。", TextDecoration::LINE_THROUGH); + let layout = layout_content(&content, 1000.0); + let bounds = content.bounds(); + let selrect = content.bounds(); + + let mut surface = skia::surfaces::raster_n32_premul((256, 256)).unwrap(); + let canvas = surface.canvas(); + + // Callers apply shape transforms to the canvas; run every pass under + // a non-trivial transform with upright and rotated cells. + canvas.translate((12.0, 8.0)); + canvas.rotate(7.0, Some((64.0, 64.0).into())); + + paint_layout( + canvas, + &layout, + &bounds, + VerticalAlign::Top, + GlyphOutput::Text, + ); + paint_drop_shadow( + canvas, + &layout, + &bounds, + VerticalAlign::Top, + &Paint::default(), + GlyphOutput::Text, + ); + + // A real drop-shadow image filter, as `drop_shadow_paints` builds. + let mut shadow_paint = Paint::default(); + shadow_paint.set_image_filter(skia::image_filters::drop_shadow( + (12.0, 12.0), + (6.0, 6.0), + skia::Color::from_argb(230, 0, 0, 255), + None, + None, + None, + )); + paint_drop_shadow( + canvas, + &layout, + &bounds, + VerticalAlign::Top, + &shadow_paint, + GlyphOutput::Text, + ); + + for kind in [ + Stroke::new_center_stroke(3.0, StrokeStyle::Solid, None, None, None, None), + Stroke::new_inner_stroke(3.0, StrokeStyle::Solid, None, None, None, None), + Stroke::new_outer_stroke(3.0, StrokeStyle::Solid, None, None, None, None), + ] { + paint_stroke( + canvas, + &layout, + &bounds, + VerticalAlign::Top, + &kind, + &selrect, + None, + GlyphOutput::Text, + ); + } + } + + /// Uncompressed PDF of the layout's fill pass, as Latin-1 text. + fn layout_pdf(layout: &VerticalLayout, bounds: &Rect) -> String { + let mut bytes: Vec = Vec::new(); + let metadata = skia::pdf::Metadata { + compression_level: skia::pdf::CompressionLevel::None, + ..Default::default() + }; + { + let document = skia::pdf::new_document(&mut bytes, Some(&metadata)); + let mut page = document.begin_page((400.0, 400.0), None); + paint_layout( + page.canvas(), + layout, + bounds, + VerticalAlign::Top, + GlyphOutput::Text, + ); + page.end_page().close(); + } + bytes.iter().map(|byte| *byte as char).collect() + } + + #[test] + fn pdf_text_of_vertical_forms_is_the_source_text() { + let content = make_content(&["「あ」"], 400.0); + let layout = layout_with(&provider(VPAL_TEST_FONT), &content); + + let pdf = layout_pdf(&layout, &content.bounds()).to_uppercase(); + let to_unicode = pdf + .split_once("BEGINBFCHAR") + .and_then(|(_, rest)| rest.split_once("ENDBFCHAR")) + .map(|(entries, _)| entries) + .expect("a ToUnicode CMap"); + + // The vertical alternates have no cmap entry of their own; the PDF + // text maps them back to the source characters. + for source in ["<300C>", "<3042>", "<300D>"] { + assert!(to_unicode.contains(source), "ToUnicode maps {source}"); + } + assert!(!to_unicode.contains("<0000>"), "no glyph maps to U+0000"); + } +} diff --git a/render-wasm/src/shapes/text_vertical/positions.rs b/render-wasm/src/shapes/text_vertical/positions.rs new file mode 100644 index 0000000000..3e9fceb282 --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/positions.rs @@ -0,0 +1,1133 @@ +use std::ops::Range; + +use skia_safe::{Contains, Point as SkPoint}; + +use crate::math::Rect; +use crate::shapes::{ + PositionData, VerticalAlign, DIRECTION_EMPHASIS_MARK, DIRECTION_VERTICAL_RL, + DIRECTION_VERTICAL_RUBY, DIRECTION_ZHUYIN, +}; + +use super::annotations::emphasis_mark_center; +use super::layout::{column_base_center, CellKind, VerticalCell, VerticalLayout}; + +/// Paragraph source UTF-16 range of a `transformed` span-text range. +fn source_utf16_range( + layout: &VerticalLayout, + paragraph: usize, + span: usize, + transformed: Range, +) -> Range { + let text_map = &layout.text_maps[paragraph]; + let transformed_span_start = text_map.span_starts[span]; + let source_span_start = text_map.span_source_starts[span]; + let relative = text_map.span_transforms[span].source_utf16_range( + transformed.start - transformed_span_start..transformed.end - transformed_span_start, + ); + source_span_start + relative.start..source_span_start + relative.end +} + +pub(super) fn cell_source_utf16_range( + layout: &VerticalLayout, + cell: &VerticalCell, +) -> Range { + source_utf16_range(layout, cell.paragraph, cell.span, cell.start..cell.end) +} + +/// Entry of `cell`'s span covering the paragraph source range `source`. +fn span_entry( + layout: &VerticalLayout, + cell: &VerticalCell, + source: Range, + (x, y, width, height): (f32, f32, f32, f32), + direction: u32, +) -> PositionData { + let span_start = layout.text_maps[cell.paragraph].span_source_starts[cell.span]; + PositionData { + paragraph: cell.paragraph as u32, + span: cell.span as u32, + start_pos: (source.start - span_start) as u32, + end_pos: (source.end - span_start) as u32, + x, + y, + width, + height, + direction, + } +} + +/// Flow placement (top relative to the cell, extent) of a warichu cell's +/// part of its first (right) or second (left) sub-line. +fn warichu_line_span(cell: &VerticalCell, first_line: bool) -> (f32, f32) { + match cell.kind { + CellKind::Warichu { + first_top, + first_extent, + second_top, + second_extent, + .. + } => { + if first_line { + (first_top, first_extent) + } else { + (second_top, second_extent) + } + } + _ => (0.0, cell.extent), + } +} + +/// The sub-line strips of a warichu cell: its first sub-line part on the +/// right half of the base band, its second on the left, each holding its own +/// characters. A cell with no characters on a sub-line has no strip there. +fn warichu_entries( + layout: &VerticalLayout, + cell: &VerticalCell, + first_chars: usize, + origin: (f32, f32), +) -> Vec { + let column = &layout.columns[cell.column]; + let center = origin.0 + column_base_center(column); + let half = cell.font_size / 2.0; + let split = cell.start + first_chars; + let line = |range: Range, x: f32, first_line: bool| { + let source = source_utf16_range(layout, cell.paragraph, cell.span, range); + let (top, extent) = warichu_line_span(cell, first_line); + let rect = (x, origin.1 + cell.top + top, half, extent); + span_entry(layout, cell, source, rect, DIRECTION_VERTICAL_RL) + }; + [ + (cell.start..split, center, true), + (split..cell.end, center - half, false), + ] + .into_iter() + .filter(|(range, _, _)| !range.is_empty()) + .map(|(range, x, first_line)| line(range, x, first_line)) + .collect() +} + +/// Position-data entries for the v2 editor / exports: the base text strips, +/// then the ruby strips, then one box per emphasis mark. +pub fn position_data( + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, +) -> Vec { + let origin = layout.origin(bounds, vertical_align); + let mut result = base_entries(layout, origin); + result.extend(ruby_entries(layout, origin)); + result.extend(emphasis_entries(layout, origin)); + result +} + +/// A cell that keeps its own strip: a warichu piece, or a tate-chu-yoko +/// composite, which SVG text sets with `text-combine-upright: all` per +/// element. +fn has_own_strip(cell: &VerticalCell) -> bool { + matches!( + cell.kind, + CellKind::Warichu { .. } | CellKind::TateChuYoko { .. } + ) +} + +/// Base text strips: consecutive cells of the same span in the same column +/// merge into one strip over the column's base band, so overlay and +/// selection skip the ruby gutter. +fn base_entries(layout: &VerticalLayout, (origin_x, origin_y): (f32, f32)) -> Vec { + let mut result: Vec = Vec::new(); + let mut i = 0; + while i < layout.cells.len() { + let first = &layout.cells[i]; + if let CellKind::Warichu { first_chars, .. } = first.kind { + result.extend(warichu_entries( + layout, + first, + first_chars, + (origin_x, origin_y), + )); + i += 1; + continue; + } + let mut source_range = cell_source_utf16_range(layout, first); + let mut bottom = first.top + first.extent; + let mut j = i + 1; + while j < layout.cells.len() && !has_own_strip(first) { + let next = &layout.cells[j]; + let same_strip = next.paragraph == first.paragraph + && next.span == first.span + && next.column == first.column + && !has_own_strip(next); + if !same_strip { + break; + } + let next_source = cell_source_utf16_range(layout, next); + source_range.start = source_range.start.min(next_source.start); + source_range.end = source_range.end.max(next_source.end); + bottom = next.top + next.extent; + j += 1; + } + let column = &layout.columns[first.column]; + let rect = ( + origin_x + column.x, + origin_y + first.top, + column.base_width, + bottom - first.top, + ); + result.push(span_entry( + layout, + first, + source_range, + rect, + DIRECTION_VERTICAL_RL, + )); + i = j; + } + result +} + +/// Ruby strips at the flow positions the canvas paints. `start_pos` and +/// `end_pos` are UTF-16 offsets into the span's ruby string, taken from the +/// shaped clusters so surrogate pairs slice correctly. +fn ruby_entries(layout: &VerticalLayout, (origin_x, origin_y): (f32, f32)) -> Vec { + layout + .ruby_cells + .iter() + .flat_map(|ruby| { + if let Some(origins) = &ruby.zhuyin_origins { + let column = &layout.columns[ruby.column]; + return ruby + .glyphs + .iter() + .zip(origins) + .map(|(glyph, origin)| PositionData { + paragraph: ruby.paragraph as u32, + span: ruby.span as u32, + start_pos: glyph.utf16_start as u32, + end_pos: glyph.utf16_end as u32, + x: origin_x + ruby.strip_x(column) + origin.x, + y: origin_y + origin.y - ruby.font_size, + width: ruby.font_size, + height: ruby.font_size, + direction: DIRECTION_ZHUYIN, + }) + .collect::>(); + } + (|| { + let (first, last) = (ruby.glyphs.first()?, ruby.glyphs.last()?); + let column = &layout.columns[ruby.column]; + let top = ruby.glyph_tops.iter().copied().fold(f32::MAX, f32::min); + let bottom = ruby + .glyph_tops + .iter() + .zip(&ruby.glyphs) + .map(|(top, glyph)| top + glyph.extent) + .fold(f32::MIN, f32::max); + Some(PositionData { + paragraph: ruby.paragraph as u32, + span: ruby.span as u32, + start_pos: first.utf16_start as u32, + end_pos: last.utf16_end as u32, + x: origin_x + ruby.strip_x(column), + y: origin_y + top, + width: ruby.font_size, + height: bottom - top, + direction: DIRECTION_VERTICAL_RUBY, + }) + })() + .into_iter() + .collect::>() + }) + .collect() +} + +/// One box per emphasis mark, covering its base cell's source range. +fn emphasis_entries( + layout: &VerticalLayout, + (origin_x, origin_y): (f32, f32), +) -> Vec { + layout + .emphasis_marks + .iter() + .map(|mark| { + let cell = &layout.cells[mark.cell]; + let (center_x, center_y) = emphasis_mark_center(layout, mark); + let size = mark.font_size; + let rect = ( + origin_x + center_x - size / 2.0, + origin_y + center_y - size / 2.0, + size, + size, + ); + let source = cell_source_utf16_range(layout, cell); + span_entry(layout, cell, source, rect, DIRECTION_EMPHASIS_MARK) + }) + .collect() +} + +/// True when the point (in the same space as `bounds`) hits laid-out text. +pub fn intersects( + layout: &VerticalLayout, + bounds: &Rect, + vertical_align: VerticalAlign, + x: f32, + y: f32, +) -> bool { + let (origin_x, origin_y) = layout.origin(bounds, vertical_align); + layout.cells.iter().any(|cell| { + let column = &layout.columns[cell.column]; + let rect = Rect::from_xywh( + origin_x + column.x, + origin_y + cell.top, + column.width, + cell.extent, + ); + rect.contains(&SkPoint::new(x, y)) + }) +} + +fn scalar_offset_from_utf16( + layout: &VerticalLayout, + paragraph: usize, + utf16_offset: usize, +) -> Option { + let boundaries = &layout.text_maps.get(paragraph)?.utf16_boundaries; + Some(match boundaries.binary_search(&utf16_offset) { + Ok(index) => index, + Err(index) => index.saturating_sub(1), + }) +} + +fn cell_scalar_range(layout: &VerticalLayout, cell: &VerticalCell) -> Option<(usize, usize)> { + Some(( + scalar_offset_from_utf16(layout, cell.paragraph, cell.start)?, + scalar_offset_from_utf16(layout, cell.paragraph, cell.end)?, + )) +} + +/// Flow offset from the cell top of each scalar boundary (0..=`chars`) of a +/// sideways run, from its glyph positions; `None` for other cells. +fn rotated_boundaries( + layout: &VerticalLayout, + cell: &VerticalCell, + chars: usize, +) -> Option> { + let CellKind::Rotated { run } = cell.kind else { + return None; + }; + layout + .runs + .get(run)? + .scalar_flow_offsets(cell.extent, chars) +} + +/// Flow offset from the cell top of the scalar boundary `index` (0..=`chars`). +fn boundary_offset( + layout: &VerticalLayout, + cell: &VerticalCell, + index: usize, + chars: usize, +) -> f32 { + rotated_boundaries(layout, cell, chars) + .and_then(|boundaries| boundaries.get(index).copied()) + .unwrap_or(index as f32 / chars as f32 * cell.extent) +} + +/// Scalar length of a warichu cell's first sub-line. `first_chars` is its +/// UTF-16 length; the cell holds `chars` scalars from `cell_start`. +fn warichu_first_line_len( + layout: &VerticalLayout, + cell: &VerticalCell, + first_chars: usize, + cell_start: usize, + chars: usize, +) -> Option { + let first_end = scalar_offset_from_utf16(layout, cell.paragraph, cell.start + first_chars)?; + Some(first_end.saturating_sub(cell_start).min(chars)) +} + +/// Index of the column under `x`; points outside every column take the +/// nearest end. Columns are ordered right to left, i.e. descending x. +fn column_at(layout: &VerticalLayout, x: f32) -> usize { + layout + .columns + .iter() + .position(|c| x >= c.x && x < c.x + c.width) + .unwrap_or(if x >= layout.width { + 0 + } else { + layout.columns.len() - 1 + }) +} + +/// True when a point inside a warichu cell's box lies past this cell's part +/// of its sub-line and the next cell shares the box, so the caret belongs to +/// that next span. +fn passes_to_next_warichu_cell( + layout: &VerticalLayout, + cell: &VerticalCell, + next: Option<&&VerticalCell>, + x: f32, + y: f32, +) -> bool { + if !matches!(cell.kind, CellKind::Warichu { .. }) { + return false; + } + let first_line = x >= column_base_center(&layout.columns[cell.column]); + let (top, extent) = warichu_line_span(cell, first_line); + let next_shares_box = next + .is_some_and(|next| matches!(next.kind, CellKind::Warichu { .. }) && next.top == cell.top); + next_shares_box && (extent <= 0.0 || y >= cell.top + top + extent) +} + +/// Paragraph scalar offset of the caret for a point inside `cell`, which +/// holds the scalars `cell_start..cell_end`. +fn caret_offset_in_cell( + layout: &VerticalLayout, + cell: &VerticalCell, + (cell_start, cell_end): (usize, usize), + x: f32, + y: f32, +) -> Option { + let chars = (cell_end - cell_start).max(1); + let column = &layout.columns[cell.column]; + Some(match cell.kind { + CellKind::Rotated { .. } => { + // The glyph boundary nearest the point along the run. + let within = y - cell.top; + let nearest = (0..=chars) + .min_by(|a, b| { + let distance = |index: &usize| { + (boundary_offset(layout, cell, *index, chars) - within).abs() + }; + distance(a).total_cmp(&distance(b)) + }) + .unwrap_or(0); + cell_start + nearest + } + CellKind::TateChuYoko { .. } => { + // The digits run left->right inside the composite, so the + // horizontal position picks the offset within it. + let left = column_base_center(column) - cell.h_advance / 2.0; + let frac = ((x - left) / cell.h_advance.max(1.0)).clamp(0.0, 1.0); + cell_start + ((frac * chars as f32).round() as usize).min(chars) + } + CellKind::Warichu { first_chars, .. } => { + // The right sub-column holds the first sub-line, the left the + // second; y picks the offset within that sub-line. + let first = warichu_first_line_len(layout, cell, first_chars, cell_start, chars)?; + let first_line = x >= column_base_center(column); + let (lo, hi) = if first_line { + (0, first) + } else { + (first, chars) + }; + let (top, extent) = warichu_line_span(cell, first_line); + let line_chars = (hi - lo).max(1); + let frac = ((y - cell.top - top) / extent.max(1.0)).clamp(0.0, 1.0); + cell_start + lo + ((frac * line_chars as f32).round() as usize).min(hi - lo) + } + CellKind::Upright { .. } | CellKind::SyntheticRotated { .. } => { + if y < cell.top + cell.extent / 2.0 { + cell_start + } else { + cell_end + } + } + }) +} + +/// Caret position (paragraph index, paragraph-relative Unicode scalar +/// offset) for a point relative to the content block's top-left origin. +pub fn caret_from_point(layout: &VerticalLayout, x: f32, y: f32) -> Option<(usize, usize)> { + if layout.columns.is_empty() { + return None; + } + let column_index = column_at(layout, x); + let paragraph = layout + .paragraph_columns + .iter() + .position(|(start, end)| column_index >= *start && column_index < *end)?; + let column_cells: Vec<&VerticalCell> = layout + .cells + .iter() + .filter(|c| c.column == column_index) + .collect(); + let Some(last) = column_cells.last() else { + return Some((paragraph, 0)); + }; + + for (index, cell) in column_cells.iter().enumerate() { + if y >= cell.top + cell.extent + || passes_to_next_warichu_cell(layout, cell, column_cells.get(index + 1), x, y) + { + continue; + } + let range = cell_scalar_range(layout, cell)?; + return Some((paragraph, caret_offset_in_cell(layout, cell, range, x, y)?)); + } + + Some(( + paragraph, + scalar_offset_from_utf16(layout, paragraph, last.end)?, + )) +} + +/// Caret rect for a paragraph-relative Unicode scalar offset, in +/// content-local coordinates. It spans the column width; its height is the +/// extent of the character at the offset (overtype carets cover the glyph), +/// or zero after the last character. +pub fn caret_rect(layout: &VerticalLayout, paragraph: usize, offset: usize) -> Option { + let (col_start, _) = *layout.paragraph_columns.get(paragraph)?; + + let mut result: Option = None; + for cell in layout.cells.iter().filter(|c| c.paragraph == paragraph) { + let (cell_start, cell_end) = cell_scalar_range(layout, cell)?; + if cell_start > offset { + continue; + } + let column = &layout.columns[cell.column]; + let chars = (cell_end - cell_start).max(1); + let rect = if offset >= cell_end { + Rect::from_xywh(column.x, cell.top + cell.extent, column.base_width, 0.0) + } else { + match cell.kind { + CellKind::TateChuYoko { .. } => { + // Digits run left->right inside the composite: the offset + // moves the caret along the horizontal axis while the + // rect keeps the composite's flow extent. + let left = column_base_center(column) - cell.h_advance / 2.0; + let width = cell.h_advance / chars as f32; + let x = left + (offset - cell_start) as f32 * width; + Rect::from_xywh(x, cell.top, width, cell.extent) + } + CellKind::Warichu { first_chars, .. } => { + // The right sub-column holds the first sub-line, the + // left the second (jlreq reading order); the caret moves + // down that half-width sub-line. + let first = + warichu_first_line_len(layout, cell, first_chars, cell_start, chars)?; + let within = offset - cell_start; + let centre = column_base_center(column); + let half = cell.font_size / 2.0; + let (band_x, line_start, line_chars) = if within < first { + (centre, 0, first) + } else { + (centre - half, first, chars - first) + }; + let line_chars = line_chars.max(1); + let frac = (within - line_start) as f32 / line_chars as f32; + let (top, extent) = warichu_line_span(cell, within < first); + Rect::from_xywh( + band_x, + cell.top + top + frac * extent, + half, + extent / line_chars as f32, + ) + } + _ => { + let index = offset - cell_start; + let top = boundary_offset(layout, cell, index, chars); + let bottom = boundary_offset(layout, cell, index + 1, chars); + Rect::from_xywh(column.x, cell.top + top, column.base_width, bottom - top) + } + } + }; + result = Some(rect); + if offset < cell_end { + break; + } + } + + result.or_else(|| { + // Empty paragraph: caret at the top of its (empty) first column. + layout + .columns + .get(col_start) + .map(|column| Rect::from_xywh(column.x, 0.0, column.base_width, 0.0)) + }) +} + +/// Selection rects for a paragraph-relative Unicode scalar offset range, in +/// content-local coordinates. +pub fn range_rects( + layout: &VerticalLayout, + paragraph: usize, + start: usize, + end: usize, +) -> Vec { + let mut rects: Vec = Vec::new(); + for cell in layout.cells.iter().filter(|c| c.paragraph == paragraph) { + let Some((cell_start, cell_end)) = cell_scalar_range(layout, cell) else { + continue; + }; + if cell_end <= start || cell_start >= end { + continue; + } + let column = &layout.columns[cell.column]; + let chars = (cell_end - cell_start).max(1); + if let CellKind::Warichu { first_chars, .. } = cell.kind { + let Some(first) = warichu_first_line_len(layout, cell, first_chars, cell_start, chars) + else { + continue; + }; + rects.extend(warichu_range_rects( + layout, + cell, + (cell_start, cell_start + first, cell_end), + (start, end), + )); + continue; + } + let sel_top = if start > cell_start { + cell.top + boundary_offset(layout, cell, start - cell_start, chars) + } else { + cell.top + }; + let sel_bottom = if end < cell_end { + cell.top + boundary_offset(layout, cell, end - cell_start, chars) + } else { + cell.top + cell.extent + }; + if sel_bottom <= sel_top { + continue; + } + // Merge with the previous rect when contiguous in the same column. + if let Some(last) = rects.last_mut() { + if (last.left - column.x).abs() < f32::EPSILON && (last.bottom - sel_top).abs() < 0.01 { + last.bottom = sel_bottom; + continue; + } + } + rects.push(Rect::from_ltrb( + column.x, + sel_top, + column.x + column.base_width, + sel_bottom, + )); + } + rects +} + +/// Selection rects of the scalar range `start..end` over a warichu cell's +/// two sub-line parts, given as (cell start, first-part end, cell end). +fn warichu_range_rects( + layout: &VerticalLayout, + cell: &VerticalCell, + (cell_start, split, cell_end): (usize, usize, usize), + (start, end): (usize, usize), +) -> Vec { + let centre = column_base_center(&layout.columns[cell.column]); + let half = cell.font_size / 2.0; + [ + (cell_start, split, centre, true), + (split, cell_end, centre - half, false), + ] + .into_iter() + .filter_map(|(line_start, line_end, left, first_line)| { + let from = start.max(line_start); + let to = end.min(line_end); + if from >= to { + return None; + } + let (top, extent) = warichu_line_span(cell, first_line); + let per_char = extent / (line_end - line_start) as f32; + let sel_top = cell.top + top + (from - line_start) as f32 * per_char; + let sel_bottom = cell.top + top + (to - line_start) as f32 * per_char; + Some(Rect::from_ltrb(left, sel_top, left + half, sel_bottom)) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::super::test_support::*; + use super::*; + use crate::shapes::{ + TextCombineUpright, TextEmphasis, TextOrientation, TextPositionWithAffinity, TextSpan, + TextTransform, + }; + use crate::wasm::text::helpers as text_helpers; + + #[test] + fn position_data_gives_each_digit_composite_its_own_strip() { + let content = spans_content( + vec![TextSpan { + text_combine_upright: TextCombineUpright::Digits2, + ..make_span("平成31年") + }], + 1000.0, + ); + let layout = layout_content(&content, 1000.0); + let strips: Vec<(u32, u32)> = position_data(&layout, &content.bounds(), VerticalAlign::Top) + .iter() + .filter(|entry| entry.direction == DIRECTION_VERTICAL_RL) + .map(|entry| (entry.start_pos, entry.end_pos)) + .collect(); + assert_eq!(strips, vec![(0, 2), (2, 4), (4, 5)]); + } + + #[test] + fn caret_from_shape_coords_wraps_columns_at_the_selrect_height() { + let mut resources = + crate::render::RenderResources::try_new_headless().expect("headless resources"); + let _guard = crate::globals::TestRenderResourcesGuard::install(&mut resources); + // Stored bounds are taller than the shape, as after measuring a fixed text. + let content = make_content(&["あいうえ"], 1000.0); + let wide = super::super::layout::layout_for_box(&content, 1000.0); + let height = wide.cells[0].extent * 2.0 + 1.0; + let selrect = Rect::from_xywh(0.0, 0.0, 200.0, height); + let layout = super::super::layout::layout_for_box(&content, selrect.height()); + let caret = caret_rect(&layout, 0, 2).unwrap(); + assert_ne!(layout.cells[2].column, layout.cells[0].column); + let left = super::super::layout::block_axis_offset( + selrect.width(), + layout.width, + VerticalAlign::Top, + ); + let point = crate::math::Point::new(left + caret.center_x(), caret.top + 1.0); + + let position = content + .get_caret_position_from_shape_coords(&point, &selrect, VerticalAlign::Top) + .expect("a caret position"); + assert_eq!((position.paragraph, position.offset), (0, 2)); + } + + #[test] + fn caret_rect_on_a_sideways_run_follows_glyph_positions() { + let content = make_content(&["WWWiii"], 1000.0); + let layout = layout_content(&content, 1000.0); + let cell = &layout.cells[0]; + let run = &layout.runs[match cell.kind { + CellKind::Rotated { run } => run, + _ => panic!("expected a sideways run"), + }]; + let boundary = cell.top + run.positions[3].x - run.positions[0].x; + + let caret = caret_rect(&layout, 0, 3).unwrap(); + assert!( + (caret.top - boundary).abs() < 0.01, + "caret at {} but the glyph boundary is at {boundary}", + caret.top + ); + assert_eq!( + caret_from_point(&layout, caret.left + 1.0, boundary + 0.5), + Some((0, 3)) + ); + + let selection = range_rects(&layout, 0, 0, 3); + assert!((selection[0].bottom - boundary).abs() < 0.01); + } + + #[test] + fn caret_from_point_lands_inside_tcy_composite() { + let mut content = make_content_with_spans(&["1234", "あ"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::All; + let layout = layout_content(&content, 400.0); + let cell = &layout.cells[0]; + assert!(matches!(cell.kind, CellKind::TateChuYoko { .. })); + let column = &layout.columns[cell.column]; + let y_mid = cell.top + cell.extent / 2.0; + let (_, at_left) = caret_from_point(&layout, column.x + 0.5, y_mid).unwrap(); + let (_, at_right) = + caret_from_point(&layout, column.x + column.base_width - 0.5, y_mid).unwrap(); + assert!(at_left <= 1, "left edge maps near the start, got {at_left}"); + assert!( + at_right >= 3, + "right edge maps near the end, got {at_right}" + ); + } + + #[test] + fn caret_rect_tracks_horizontal_axis_inside_tcy_composite() { + let mut content = make_content_with_spans(&["1234", "あ"], 400.0); + content.paragraphs_mut()[0].children_mut()[0].text_combine_upright = + TextCombineUpright::All; + let layout = layout_content(&content, 400.0); + let cell = &layout.cells[0]; + assert!(matches!(cell.kind, CellKind::TateChuYoko { .. })); + let left = column_base_center(&layout.columns[cell.column]) - cell.h_advance / 2.0; + let digit = cell.h_advance / 4.0; + for i in 0..4 { + let rect = caret_rect(&layout, 0, cell.start + i).expect("caret rect"); + assert!( + (rect.left - (left + i as f32 * digit)).abs() < 0.01, + "digit {i} caret sits at its horizontal slot, got {}", + rect.left + ); + assert!( + (rect.width() - digit).abs() < 0.01, + "digit {i} caret is one digit wide, got {}", + rect.width() + ); + assert!( + (rect.top - cell.top).abs() < 0.01 && (rect.height() - cell.extent).abs() < 0.01, + "digit {i} caret spans the composite's flow extent" + ); + } + // After the composite the caret falls back to the flow axis. + let rect = caret_rect(&layout, 0, cell.end).expect("caret rect"); + assert!(rect.height() < 0.01 || rect.top >= cell.top + cell.extent - 0.01); + } + + #[test] + fn position_data_emits_one_strip_per_warichu_sub_line() { + let content = warichu_content("あいう、えお", 400.0); + let layout = layout_content(&content, 400.0); + let data = position_data(&layout, &content.bounds(), VerticalAlign::Top); + + let strips: Vec<&PositionData> = data + .iter() + .filter(|entry| entry.direction == DIRECTION_VERTICAL_RL) + .collect(); + assert_eq!(strips.len(), 2); + assert_eq!((strips[0].start_pos, strips[0].end_pos), (0, 4)); + assert_eq!((strips[1].start_pos, strips[1].end_pos), (4, 6)); + assert!( + strips[0].x > strips[1].x, + "the first sub-line reads first, on the right" + ); + assert!((strips[0].width - EM / 2.0).abs() < 0.01); + } + + #[test] + fn position_data_emits_each_emphasis_mark_box() { + let content = spans_content( + vec![TextSpan { + text_emphasis: TextEmphasis::FilledDot, + text_orientation: TextOrientation::Upright, + ..make_span("A、B") + }], + 400.0, + ); + let layout = layout_content(&content, 400.0); + let data = position_data(&layout, &content.bounds(), VerticalAlign::Top); + + let marks: Vec<&PositionData> = data + .iter() + .filter(|entry| entry.direction == DIRECTION_EMPHASIS_MARK) + .collect(); + let ranges: Vec<(u32, u32)> = marks.iter().map(|m| (m.start_pos, m.end_pos)).collect(); + assert_eq!( + ranges, + vec![(0, 1), (2, 3)], + "A and B are marked; 、 is not" + ); + let base = data + .iter() + .find(|entry| entry.direction == DIRECTION_VERTICAL_RL) + .expect("base strip"); + for mark in marks { + assert!((mark.width - EM / 2.0).abs() < 0.01); + assert!( + mark.x >= base.x + base.width - 0.01, + "marks sit in the gutter right of the base band" + ); + } + } + + #[test] + fn warichu_cell_carries_kinsoku_split() { + let content = warichu_content("あいう、えお", 400.0); + let layout = layout_content(&content, 400.0); + let cell = layout + .cells + .iter() + .find(|c| matches!(c.kind, CellKind::Warichu { .. })) + .expect("a warichu cell"); + let CellKind::Warichu { first_chars, .. } = cell.kind else { + unreachable!(); + }; + assert_eq!( + first_chars, 4, + "the comma is pulled up into the first sub-line" + ); + // The caret for the second sub-line's first character restarts at + // the composite top, left of the axis. + let column = &layout.columns[cell.column]; + let centre = column_base_center(column); + let rect = caret_rect(&layout, 0, cell.start + 4).expect("caret rect"); + assert!((rect.top - cell.top).abs() < 0.01); + assert!(rect.left < centre); + } + + #[test] + fn caret_rect_tracks_sub_lines_inside_warichu() { + let content = warichu_content("割注二行説明", 400.0); + let layout = layout_content(&content, 400.0); + let cell = layout + .cells + .iter() + .find(|c| matches!(c.kind, CellKind::Warichu { .. })) + .expect("a warichu cell"); + let column = &layout.columns[cell.column]; + let centre = column_base_center(column); + let half = cell.font_size / 2.0; + // First sub-line (offsets 0..3) sits right of the column axis. + let first = caret_rect(&layout, 0, cell.start).expect("caret rect"); + assert!( + (first.left - centre).abs() < 0.01, + "first sub-line caret starts at the axis, got {}", + first.left + ); + assert!( + (first.width() - half).abs() < 0.01, + "sub-line caret is half-size wide, got {}", + first.width() + ); + assert!((first.top - cell.top).abs() < 0.01); + // Second sub-line (offsets 3..6) sits left of the axis, restarting + // at the composite top. + let second = caret_rect(&layout, 0, cell.start + 3).expect("caret rect"); + assert!( + (second.left - (centre - half)).abs() < 0.01, + "second sub-line caret sits left of the axis, got {}", + second.left + ); + assert!( + (second.top - cell.top).abs() < 0.01, + "second sub-line restarts at the composite top, got {}", + second.top + ); + let deeper = caret_rect(&layout, 0, cell.start + 4).expect("caret rect"); + assert!( + deeper.top > second.top, + "later offsets move down the sub-line" + ); + } + + #[test] + fn caret_round_trip() { + let text = "あいうえお"; + let content = make_content(&[text], 1000.0); + let layout = layout_content(&content, 1000.0); + + for cell in &layout.cells { + let column = &layout.columns[cell.column]; + // A point in the upper half of the cell resolves to its start. + let (paragraph, offset) = caret_from_point( + &layout, + column.x + column.width / 2.0, + cell.top + cell.extent * 0.25, + ) + .expect("caret"); + assert_eq!(paragraph, 0); + assert_eq!(offset, cell.start); + + // caret_rect for that offset lands inside the same column and + // carries the character extent (for overtype carets). + let rect = caret_rect(&layout, paragraph, offset).expect("caret rect"); + assert!((rect.x() - column.x).abs() < 0.01); + let chars = (cell.end - cell.start).max(1); + assert!((rect.height() - cell.extent / chars as f32).abs() < 0.01); + } + + // After the last character there is no glyph to cover: zero height. + let end_offset = layout.cells.last().unwrap().end; + let rect = caret_rect(&layout, 0, end_offset).expect("caret rect"); + assert_eq!(rect.height(), 0.0); + } + + #[test] + fn non_bmp_caret_offsets_round_trip_through_editor_operations() { + let content = make_content(&["𠀀あ"], 1000.0); + let layout = layout_content(&content, 1000.0); + let first = &layout.cells[0]; + let second = &layout.cells[1]; + let column = &layout.columns[first.column]; + let x = column_base_center(column); + + let before = caret_from_point(&layout, x, first.top + first.extent * 0.25).unwrap(); + let after = caret_from_point(&layout, x, first.top + first.extent * 0.75).unwrap(); + assert_eq!(before, (0, 0)); + assert_eq!(after, (0, 1)); + + let rect = caret_rect(&layout, after.0, after.1).expect("caret rect"); + assert!((rect.top - second.top).abs() < 0.01); + assert!((rect.height() - second.extent).abs() < 0.01); + + let rects = range_rects(&layout, 0, 0, 1); + assert_eq!(rects.len(), 1); + assert!((rects[0].height() - first.extent).abs() < 0.01); + + let start = TextPositionWithAffinity::new_downstream_affinity(0, 0); + let end = TextPositionWithAffinity::new_downstream_affinity(after.0, after.1); + assert_eq!( + text_helpers::move_cursor_forward(&start, content.paragraphs(), false), + end + ); + assert_eq!( + text_helpers::move_cursor_backward(&end, content.paragraphs(), false), + start + ); + + let mut inserted = make_content(&["𠀀あ"], 1000.0); + assert_eq!( + text_helpers::insert_text_at_cursor(&mut inserted, &end, "X"), + Some(2) + ); + assert_eq!(inserted.paragraphs()[0].children()[0].text, "𠀀Xあ"); + + let mut deleted = make_content(&["𠀀あ"], 1000.0); + assert_eq!( + text_helpers::delete_char_before(&mut deleted, &end), + Some(start) + ); + assert_eq!(deleted.paragraphs()[0].children()[0].text, "あ"); + } + + #[test] + fn selection_rects_cover_the_range() { + let text = "あいうえお"; + let content = make_content(&[text], 1000.0); + let layout = layout_content(&content, 1000.0); + + let rects = range_rects(&layout, 0, 1, 3); + assert!(!rects.is_empty()); + let covered: f32 = rects.iter().map(|r| r.height()).sum(); + let expected: f32 = layout + .cells + .iter() + .filter(|c| c.start >= 1 && c.end <= 3) + .map(|c| c.extent) + .sum(); + assert!((covered - expected).abs() < 0.01); + } + + #[test] + fn position_data_merges_by_column_and_maps_span_offsets() { + let text = "あいうえお"; + let content = make_content(&[text], 1000.0); + let layout = layout_content(&content, 1000.0); + let bounds = content.bounds(); + + let data = position_data(&layout, &bounds, VerticalAlign::Top); + assert_eq!(data.len(), 1, "one column, one span => one entry"); + assert_eq!(data[0].start_pos, 0); + assert_eq!(data[0].end_pos, text.encode_utf16().count() as u32); + // Right-anchored: the strip ends at the bounds right edge. + assert!((data[0].x + data[0].width - bounds.right).abs() < 0.01); + } + + #[test] + fn position_data_keeps_expanded_transform_in_one_source_safe_strip() { + let mut content = make_content(&["AßB"], 1000.0); + let span = &mut content.paragraphs_mut()[0].children_mut()[0]; + span.text_transform = Some(TextTransform::Uppercase); + span.text_orientation = TextOrientation::Upright; + let unwrapped = layout_content(&content, 1000.0); + let expanded_extent: f32 = unwrapped + .cells + .iter() + .filter(|cell| cell_source_utf16_range(&unwrapped, cell) == (1..2)) + .map(|cell| cell.extent) + .sum(); + let layout = layout_content(&content, expanded_extent + 0.01); + + let expanded: Vec<&VerticalCell> = layout + .cells + .iter() + .filter(|cell| cell_source_utf16_range(&layout, cell) == (1..2)) + .collect(); + assert_eq!(expanded.len(), 2, "ß must shape as the two cells in SS"); + assert_eq!( + expanded[0].column, expanded[1].column, + "glyphs from one source character must not split across columns" + ); + + let data = position_data(&layout, &content.bounds(), VerticalAlign::Top); + let ranges: Vec<(u32, u32)> = data + .iter() + .filter(|entry| entry.direction == DIRECTION_VERTICAL_RL) + .map(|entry| (entry.start_pos, entry.end_pos)) + .collect(); + assert_eq!(ranges, vec![(0, 1), (1, 2), (2, 3)]); + } + + #[test] + fn position_data_emits_ruby_annotation_strips() { + let content = ruby_content("漢字", "かんじ", 400.0); + let layout = layout_content(&content, 400.0); + let bounds = content.bounds(); + let data = position_data(&layout, &bounds, VerticalAlign::Top); + + let base: Vec<&PositionData> = data + .iter() + .filter(|d| d.direction == DIRECTION_VERTICAL_RL) + .collect(); + let ruby: Vec<&PositionData> = data + .iter() + .filter(|d| d.direction == DIRECTION_VERTICAL_RUBY) + .collect(); + assert_eq!(base.len(), 1); + assert_eq!(ruby.len(), 1, "one annotated column => one ruby strip"); + assert_eq!(ruby[0].start_pos, 0); + assert_eq!(ruby[0].end_pos, 3, "offsets index the ruby string"); + // The strip sits in the gutter, to the right of the base band. + assert!(ruby[0].x >= base[0].x + base[0].width - 0.001); + assert!(ruby[0].width > 0.0); + assert!(ruby[0].height > 0.0); + } + + #[test] + fn position_data_ruby_offsets_are_utf16_for_non_bmp_readings() { + // Two surrogate-pair reading characters: 2 glyphs but 4 UTF-16 + // units. The strip offsets must slice the ruby string by UTF-16. + let content = ruby_content("\u{6f22}\u{5b57}", "\u{1d4aa}\u{1d4ab}", 400.0); + let layout = layout_content(&content, 400.0); + let bounds = content.bounds(); + let data = position_data(&layout, &bounds, VerticalAlign::Top); + let ruby: Vec<&PositionData> = data + .iter() + .filter(|d| d.direction == DIRECTION_VERTICAL_RUBY) + .collect(); + assert_eq!(ruby.len(), 1); + assert_eq!(ruby[0].start_pos, 0); + assert_eq!( + ruby[0].end_pos, 4, + "surrogate pairs count two UTF-16 units each" + ); + } + + #[test] + fn position_data_follows_block_axis_alignment() { + let content = make_content(&["あいう"], 1000.0); + let layout = layout_content(&content, 1000.0); + let bounds = content.bounds(); + let top = position_data(&layout, &bounds, VerticalAlign::Top); + let center = position_data(&layout, &bounds, VerticalAlign::Center); + let bottom = position_data(&layout, &bounds, VerticalAlign::Bottom); + + assert!(top[0].x > center[0].x); + assert!(center[0].x > bottom[0].x); + assert!((bottom[0].x - bounds.left).abs() < 0.01); + } + + #[test] + fn ruby_selection_geometry_excludes_gutter() { + let mut content = ruby_content("AB", "ab", 400.0); + content.paragraphs_mut()[0].children_mut()[0].annotation_clearance = + crate::shapes::AnnotationClearance::Auto; + let layout = layout_content(&content, 400.0); + let base_width = layout.columns[0].base_width; + assert!(base_width < layout.columns[0].width); + + let pd = position_data(&layout, &content.bounds(), VerticalAlign::Top); + assert!(!pd.is_empty()); + for entry in pd.iter().filter(|e| e.direction == DIRECTION_VERTICAL_RL) { + assert!( + (entry.width - base_width).abs() < 0.01, + "position-data strip must be the base band, not the full column" + ); + } + + let rects = range_rects(&layout, 0, 0, 10); + assert!(!rects.is_empty()); + for rect in &rects { + assert!( + (rect.width() - base_width).abs() < 0.01, + "selection rect must be the base band, not the full column" + ); + } + } +} diff --git a/render-wasm/src/shapes/text_vertical/test_support.rs b/render-wasm/src/shapes/text_vertical/test_support.rs new file mode 100644 index 0000000000..93aec7b896 --- /dev/null +++ b/render-wasm/src/shapes/text_vertical/test_support.rs @@ -0,0 +1,194 @@ +// Fixtures for the vertical layout tests: real shaping with bundled test +// fonts. Glyph coverage does not matter for offsets/columns; tofu still +// shapes with real advances. + +use skia_safe::{textlayout::TypefaceFontProvider, FontMgr}; + +use crate::math::Rect; +use crate::render::FallbackFonts; +use crate::shapes::{ + FontFamily, FontStyle, GrowType, Paragraph, TextAlign, TextContent, TextDecoration, + TextDirection, TextOrientation, TextSpan, WritingMode, +}; +use crate::Uuid; + +use super::layout::{layout_vertical, VerticalLayout}; + +pub(crate) const TEST_FONT: &[u8] = include_bytes!("../../fonts/sourcesanspro-regular.ttf"); + +/// Work Sans subset with Latin letters and combining Pinyin tone marks. +pub(crate) const PINYIN_TEST_FONT: &[u8] = include_bytes!("../../fonts/pinyin-test.ttf"); + +/// Noto Sans JP subset with `vmtx`/`vhea`: 〱 advances 2em vertically, あ/く 1em. +pub(crate) const VMTX_TEST_FONT: &[u8] = include_bytes!("../../fonts/notosansjp-vmtx-test.ttf"); + +/// Noto Sans JP subset with GSUB `vert` and GPOS `vpal` for 、。「」あく. +pub(crate) const VPAL_TEST_FONT: &[u8] = include_bytes!("../../fonts/notosansjp-vpal-test.ttf"); + +/// Font size of every test span. +pub(crate) const EM: f32 = 20.0; + +/// Provider with `font` registered under the family every test span uses +/// (spans serialize their family as "uuid-weight-style"). +pub(crate) fn provider(font: &[u8]) -> TypefaceFontProvider { + let typeface = FontMgr::new() + .new_from_data(skia_safe::Data::new_copy(font), None) + .expect("failed to load test font"); + let mut provider = TypefaceFontProvider::new(); + let family = format!("{}", FontFamily::new(Uuid::nil(), 400, FontStyle::Normal)); + provider.register_typeface(typeface, Some(family.as_str())); + provider +} + +/// `provider(font)` plus `fallback` registered as the "fallback" family. +pub(crate) fn provider_with_fallback(font: &[u8], fallback: &[u8]) -> TypefaceFontProvider { + let mut provider = provider(font); + let typeface = FontMgr::new() + .new_from_data(skia_safe::Data::new_copy(fallback), None) + .expect("failed to load fallback font"); + provider.register_typeface(typeface, Some("fallback")); + provider +} + +pub(crate) fn make_span(text: &str) -> TextSpan { + TextSpan { + text: text.to_string(), + font_size: EM, + line_height: 1.0, + ..TextSpan::default() + } +} + +pub(crate) fn vertical_paragraph( + spans: Vec, + align: TextAlign, + line_height: f32, +) -> Paragraph { + let mut paragraph = Paragraph::new( + align, + TextDirection::LTR, + None, + None, + line_height, + 0.0, + spans, + ); + paragraph.set_writing_mode(WritingMode::VerticalRl); + paragraph.set_language("ja".to_string()); + paragraph +} + +/// A 200px wide box `height` tall holding `paragraphs`. +pub(crate) fn content_of( + paragraphs: Vec, + height: f32, + grow_type: GrowType, +) -> TextContent { + crate::globals::design_init(); + let mut content = TextContent::new(Rect::from_xywh(0.0, 0.0, 200.0, height), grow_type); + for paragraph in paragraphs { + content.add_paragraph(paragraph); + } + content +} + +/// One left-aligned paragraph holding `spans`. +pub(crate) fn spans_content(spans: Vec, height: f32) -> TextContent { + content_of( + vec![vertical_paragraph(spans, TextAlign::Left, 1.0)], + height, + GrowType::Fixed, + ) +} + +/// One paragraph per text, each a single span. +pub(crate) fn make_content(texts: &[&str], height: f32) -> TextContent { + let paragraphs = texts + .iter() + .map(|text| vertical_paragraph(vec![make_span(text)], TextAlign::Left, 1.0)) + .collect(); + content_of(paragraphs, height, GrowType::Fixed) +} + +/// One paragraph with a span per text. +pub(crate) fn make_content_with_spans(texts: &[&str], height: f32) -> TextContent { + spans_content(texts.iter().map(|text| make_span(text)).collect(), height) +} + +pub(crate) fn ruby_content(base: &str, ruby: &str, height: f32) -> TextContent { + spans_content( + vec![TextSpan { + ruby: ruby.to_string(), + ..make_span(base) + }], + height, + ) +} + +pub(crate) fn warichu_content(base: &str, height: f32) -> TextContent { + spans_content( + vec![TextSpan { + warichu: true, + text_orientation: TextOrientation::Upright, + ..make_span(base) + }], + height, + ) +} + +pub(crate) fn spaced_content(text: &str, letter_spacing: f32) -> TextContent { + spans_content( + vec![TextSpan { + letter_spacing, + ..make_span(text) + }], + 1000.0, + ) +} + +pub(crate) fn decorated_content(text: &str, decoration: TextDecoration) -> TextContent { + spans_content( + vec![TextSpan { + text_decoration: Some(decoration), + ..make_span(text) + }], + 1000.0, + ) +} + +pub(crate) fn layout_with_fallback( + provider: &TypefaceFontProvider, + content: &TextContent, + max_height: f32, + fallback_families: &[String], +) -> VerticalLayout { + layout_vertical( + content, + max_height, + provider, + FontMgr::from(provider.clone()), + &FallbackFonts::from_families(fallback_families.iter().cloned()), + content.bounds(), + ) +} + +pub(crate) fn layout_with_height( + provider: &TypefaceFontProvider, + content: &TextContent, + max_height: f32, +) -> VerticalLayout { + layout_with_fallback(provider, content, max_height, &[]) +} + +/// Layout with `provider` and a 1000px wrap budget. +pub(crate) fn layout_with( + provider: &TypefaceFontProvider, + content: &TextContent, +) -> VerticalLayout { + layout_with_height(provider, content, 1000.0) +} + +/// Layout with the Latin test face. +pub(crate) fn layout_content(content: &TextContent, max_height: f32) -> VerticalLayout { + layout_with_height(&provider(TEST_FONT), content, max_height) +}