mirror of
https://github.com/penpot/penpot.git
synced 2026-11-27 21:57:52 +00:00
🐛 Keep a word space between adjacent Western readings
A reading longer than its base grows the base to the reading's width,
and neighbouring readings get no overhang room, so per-character Pinyin
or Jyutping wider than its characters ran into the next reading
("gwong2dung1waa2").
A reading without upright characters that sits beside another reading
now claims an eighth of a ruby em at each end when its base grows, in
horizontal and vertical text. Kana and Han readings stay solid.
AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
e618f3a47f
commit
2c3dfa9a83
@ -271,9 +271,14 @@ pub(super) fn horizontal_ruby_spacing_with(
|
||||
);
|
||||
let (_, pieces, _) =
|
||||
horizontal_ruby_pieces(&ruby_glyph_refs(span.ruby_text(), &shaped, ruby_font_size));
|
||||
let ruby_length: f32 = pieces.iter().sum();
|
||||
let previous = layout_neighbour(texts, group.start, false);
|
||||
let next = layout_neighbour(texts, group.end - 1, true);
|
||||
let beside_reading = [previous, next]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|(owner, _, _)| spans[owner].has_ruby());
|
||||
let end_space = reading_end_space(span.ruby_text(), ruby_font_size, beside_reading);
|
||||
let ruby_length: f32 = pieces.iter().sum::<f32>() + 2.0 * end_space;
|
||||
let room = |neighbour: Option<(usize, usize, char)>| {
|
||||
neighbour.map_or(0.0, |(owner, _, ch)| {
|
||||
ruby_overhang_room(
|
||||
|
||||
@ -779,6 +779,30 @@ fn horizontal_long_ruby_counts_only_source_characters_of_its_base() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adjacent_western_readings_keep_a_space_between_them() {
|
||||
init_state();
|
||||
let reading = |base: &str, ruby: &str| {
|
||||
let mut span = make_span(base, 0.0);
|
||||
span.ruby = ruby.to_string();
|
||||
span.ruby_overhang = RubyOverhang::None;
|
||||
span
|
||||
};
|
||||
let gap = |spans: Vec<TextSpan>| test_ruby_spacing(&make_paragraph(spans, 0.0)).base_gaps[0];
|
||||
|
||||
let alone = gap(vec![reading("i", "gwong2"), make_span("b", 0.0)]);
|
||||
let paired = gap(vec![reading("i", "gwong2"), reading("i", "dung1")]);
|
||||
let space = WESTERN_READING_SPACE * reading("i", "gwong2").ruby_font_size();
|
||||
assert!(
|
||||
(paired - alone - 2.0 * space).abs() < 0.01,
|
||||
"the base grows by a space at each end of the reading: {alone} -> {paired}"
|
||||
);
|
||||
|
||||
let kana_alone = gap(vec![reading("字", "じじじじ"), make_span("字", 0.0)]);
|
||||
let kana_paired = gap(vec![reading("字", "じじじじ"), reading("字", "かかかか")]);
|
||||
assert_eq!(kana_paired, kana_alone, "kana readings stay solid");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn horizontal_short_ruby_adds_no_spacing() {
|
||||
init_state();
|
||||
|
||||
@ -5,6 +5,7 @@ use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
|
||||
use crate::shapes::jlreq::{classify, JlreqClass};
|
||||
use crate::shapes::text_shaping::is_upright_char;
|
||||
use crate::shapes::{RubyAlign, RubyOverhang};
|
||||
|
||||
/// Room a long reading may overhang one neighbouring character (JLREQ
|
||||
@ -94,6 +95,21 @@ impl<K: Eq + Hash> OverhangClaims<K> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Space, in ruby em, that a Western reading keeps at each end beside
|
||||
/// another reading, so adjacent readings read as separate words.
|
||||
pub(crate) const WESTERN_READING_SPACE: f32 = 0.125;
|
||||
|
||||
/// Space a reading keeps at each end of its base: `WESTERN_READING_SPACE` for
|
||||
/// a reading without upright characters that sits `beside_reading`, none
|
||||
/// otherwise. Kana and Han readings set solid, as JLREQ mono-ruby does.
|
||||
pub(crate) fn reading_end_space(ruby_text: &str, ruby_font_size: f32, beside_reading: bool) -> f32 {
|
||||
if beside_reading && !ruby_text.chars().any(is_upright_char) {
|
||||
WESTERN_READING_SPACE * ruby_font_size
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Gap that spreads a base under a reading longer than itself (JLREQ
|
||||
/// §3.3.8). What the reading set solid (`ruby_length`) overflows the base
|
||||
/// by, less the overhang `room`, becomes one gap per base character: a full
|
||||
|
||||
@ -9,7 +9,8 @@ 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,
|
||||
distribute_ruby_pieces, long_ruby_gap, reading_end_space, ruby_overhang_room, ruby_pieces,
|
||||
OverhangClaims,
|
||||
};
|
||||
use crate::shapes::{
|
||||
AnnotationSide, AppliedTextTransform, FontFeatures, Paragraph, RubyOverhang, TextContent,
|
||||
@ -116,6 +117,10 @@ pub(super) struct RubyBaseUnit {
|
||||
overhang: RubyOverhang,
|
||||
/// Overhang room over the characters before and after the base.
|
||||
room: (f32, f32),
|
||||
/// Space the reading keeps at each end when it sits beside another reading.
|
||||
beside_space: f32,
|
||||
/// Space the reading keeps at each end of its base.
|
||||
end_space: f32,
|
||||
}
|
||||
|
||||
impl RubyBaseUnit {
|
||||
@ -123,9 +128,9 @@ impl RubyBaseUnit {
|
||||
self.spans.contains(&cell.span) && cell.start < self.end && cell.end > self.start
|
||||
}
|
||||
|
||||
/// Flow length of the reading set solid.
|
||||
/// Flow length the reading claims: set solid, plus its end spaces.
|
||||
fn ruby_line(&self) -> f32 {
|
||||
self.ruby_length
|
||||
self.ruby_length + 2.0 * self.end_space
|
||||
}
|
||||
}
|
||||
|
||||
@ -177,6 +182,8 @@ pub(super) fn ruby_base_units(
|
||||
ruby_font_size: span.ruby_font_size(),
|
||||
overhang: span.ruby_overhang,
|
||||
room: (0.0, 0.0),
|
||||
beside_space: reading_end_space(span.ruby_text(), span.ruby_font_size(), true),
|
||||
end_space: 0.0,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@ -225,12 +232,12 @@ pub(super) fn set_ruby_overhang_rooms(flow: &[FlowCell], ruby_units: &mut [RubyB
|
||||
};
|
||||
let indices = annotated_indices(flow.iter().map(|item| &item.cell), ruby_units);
|
||||
let mut claims = OverhangClaims::new();
|
||||
let rooms: Vec<(f32, f32)> = ruby_units
|
||||
let rooms: Vec<((f32, f32), f32)> = ruby_units
|
||||
.iter()
|
||||
.zip(&indices)
|
||||
.map(|(unit, indices)| {
|
||||
let (Some(first), Some(last)) = (indices.first(), indices.last()) else {
|
||||
return unit.room;
|
||||
return (unit.room, unit.end_space);
|
||||
};
|
||||
let room = |index: Option<usize>| {
|
||||
index.map_or(0.0, |index| {
|
||||
@ -246,15 +253,23 @@ pub(super) fn set_ruby_overhang_rooms(flow: &[FlowCell], ruby_units: &mut [RubyB
|
||||
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(
|
||||
let beside_reading = [before, after].into_iter().flatten().any(annotated);
|
||||
let end_space = if beside_reading {
|
||||
unit.beside_space
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let room = claims.room(
|
||||
before,
|
||||
after,
|
||||
(room(before), room(after)),
|
||||
unit.ruby_line() - base,
|
||||
)
|
||||
unit.ruby_length + 2.0 * end_space - base,
|
||||
);
|
||||
(room, end_space)
|
||||
})
|
||||
.collect();
|
||||
for (unit, room) in ruby_units.iter_mut().zip(rooms) {
|
||||
for (unit, (room, end_space)) in ruby_units.iter_mut().zip(rooms) {
|
||||
unit.end_space = end_space;
|
||||
unit.room = room;
|
||||
}
|
||||
}
|
||||
@ -327,7 +342,7 @@ 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());
|
||||
let gap = long_ruby_gap(unit.ruby_line(), base_total, unit.room, indices.len());
|
||||
if gap <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
@ -1617,6 +1632,43 @@ mod tests {
|
||||
.expect("ruby cell")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adjacent_western_readings_keep_a_space_between_them() {
|
||||
let content = |second: &str| {
|
||||
spans_content(
|
||||
vec![
|
||||
TextSpan {
|
||||
ruby: "gwong2gwong2".to_string(),
|
||||
ruby_overhang: RubyOverhang::None,
|
||||
..make_span("漢")
|
||||
},
|
||||
TextSpan {
|
||||
ruby: second.to_string(),
|
||||
ruby_overhang: RubyOverhang::None,
|
||||
..make_span("字")
|
||||
},
|
||||
],
|
||||
1000.0,
|
||||
)
|
||||
};
|
||||
let extent = |content: &TextContent| {
|
||||
layout_with(&provider(VMTX_TEST_FONT), content)
|
||||
.cells
|
||||
.iter()
|
||||
.find(|cell| cell.span == 0)
|
||||
.expect("base cell")
|
||||
.extent
|
||||
};
|
||||
let alone = extent(&content(""));
|
||||
let paired = extent(&content("dung1"));
|
||||
let space =
|
||||
crate::shapes::text_ruby::WESTERN_READING_SPACE * make_span("漢").ruby_font_size();
|
||||
assert!(
|
||||
(paired - alone - 2.0 * space).abs() < 0.01,
|
||||
"the base grows by a space at each end of the reading: {alone} -> {paired}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_reading_never_covers_the_next_ruby_base() {
|
||||
let layout = layout_with(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user