1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Resolved per-unit typography for CSS vertical lines.
//!
//! CSS `text-orientation` is an authored policy, not a shaping instruction.
//! Resolve it once into a complete unit mode before shaping so that vertical
//! font features, vertical metrics, and PDF placement cannot disagree.
use super::*;
/// One complete shaping, metrics, and placement mode for a typographic unit.
///
/// The variants deliberately own all three decisions: code outside this
/// module cannot construct a sideways unit that requests vertical forms or
/// vertical advances.
/// <https://drafts.csswg.org/css-writing-modes-4/#text-orientation>
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum VerticalUnitTypesetting {
/// Individual vertical units using vertical OpenType features and metrics.
UprightVertical,
/// A horizontally composed run rotated onto a vertical line.
SidewaysHorizontal,
}
impl VerticalUnitTypesetting {
pub(crate) const fn uses_vertical_font_metrics(self) -> bool {
matches!(self, Self::UprightVertical)
}
}
/// One source range and its fully resolved vertical typesetting mode.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ResolvedVerticalUnit {
pub(crate) range: Range<usize>,
pub(crate) typesetting: VerticalUnitTypesetting,
}
/// The resolved typography plan for one shaped text line.
///
/// Horizontal text has no per-unit vertical choices. Vertical plans cover the
/// entire source string with ordered CSS typographic units.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TextTypesettingPlan {
Horizontal,
Vertical(Rc<[ResolvedVerticalUnit]>),
}
impl TextTypesettingPlan {
pub(crate) fn resolve(text: &str, style: &ComputedStyle) -> Self {
match style.text_layout_policy() {
TextLayoutPolicy::Horizontal => Self::Horizontal,
TextLayoutPolicy::Sideways(_) => {
Self::vertical_single(text, VerticalUnitTypesetting::SidewaysHorizontal)
}
TextLayoutPolicy::Vertical(orientation) => {
let mut units = CursiveProtectedUnitRanges::new(text)
.into_iter()
.map(|range| {
let unit_text = &text[range.clone()];
ResolvedVerticalUnit {
typesetting: resolve_vertical_unit(orientation, unit_text),
range,
}
})
.collect::<Vec<_>>();
// Default-ignorables inherit the following visible unit's
// orientation; coalesce equal neighboring decisions so font
// shaping receives one contiguous feature range.
for index in (0..units.len()).rev() {
if text[units[index].range.clone()]
.chars()
.all(character_inherits_vertical_orientation)
&& let Some(next) = units.get(index + 1)
{
units[index].typesetting = next.typesetting;
}
}
Self::Vertical(units.into())
}
}
}
fn vertical_single(text: &str, typesetting: VerticalUnitTypesetting) -> Self {
if text.is_empty() {
Self::Horizontal
} else {
Self::Vertical(
vec![ResolvedVerticalUnit {
range: 0..text.len(),
typesetting,
}]
.into(),
)
}
}
pub(crate) fn units(&self) -> &[ResolvedVerticalUnit] {
match self {
Self::Horizontal => &[],
Self::Vertical(units) => units,
}
}
/// Returns the resolved typographic unit that fully contains `range`.
///
/// CSS Writing Modes resolves vertical orientation per typographic
/// character unit. A shaped glyph can therefore be assigned to a unit
/// only when its source provenance is wholly contained by that unit.
///
/// <https://www.w3.org/TR/css-writing-modes-4/#vertical-orientations>
pub(crate) fn resolved_unit_for_range(
&self,
range: &Range<usize>,
) -> Option<&ResolvedVerticalUnit> {
self.units()
.iter()
.find(|unit| unit.range.start <= range.start && range.end <= unit.range.end)
}
pub(crate) fn typesetting_for_range(
&self,
range: &Range<usize>,
) -> Option<VerticalUnitTypesetting> {
self.resolved_unit_for_range(range)
.map(|unit| unit.typesetting)
}
/// Return one mode when `range` is fully covered by resolved units that
/// all agree on their vertical typesetting.
///
/// A shaping cluster can cover several CSS typographic character units.
/// The cluster has no single unit identity in that case, but its font
/// metrics and paint matrix are still unambiguous when each covered unit
/// resolves to the same mode. A range crossing upright and sideways units
/// deliberately remains unresolved.
/// <https://www.w3.org/TR/css-writing-modes-4/#vertical-orientations>
pub(crate) fn unanimous_typesetting_for_range(
&self,
range: &Range<usize>,
) -> Option<VerticalUnitTypesetting> {
if let Some(typesetting) = self.typesetting_for_range(range) {
return Some(typesetting);
}
if range.start >= range.end {
return None;
}
let mut covered_until = range.start;
let mut typesetting = None;
for unit in self.units() {
if unit.range.end <= range.start {
continue;
}
if range.end <= unit.range.start {
break;
}
let overlap_start = unit.range.start.max(range.start);
let overlap_end = unit.range.end.min(range.end);
if overlap_start != covered_until {
return None;
}
match typesetting {
Some(previous) if previous != unit.typesetting => return None,
Some(_) => {}
None => typesetting = Some(unit.typesetting),
}
covered_until = overlap_end;
}
(covered_until == range.end)
.then_some(typesetting)
.flatten()
}
/// Returns the common typesetting mode when every resolved unit agrees.
///
/// This is a fallback for a rendered run with no source provenance.
/// PDF-facing Unicode summaries cannot safely reconstruct CSS
/// typographic character-unit boundaries.
pub(crate) fn uniform_typesetting(&self) -> Option<VerticalUnitTypesetting> {
let first = self.units().first()?.typesetting;
self.units()
.iter()
.all(|unit| unit.typesetting == first)
.then_some(first)
}
pub(crate) fn upright_vertical_ranges(&self) -> Vec<Range<usize>> {
let mut ranges = self
.units()
.iter()
.filter(|unit| unit.typesetting.uses_vertical_font_metrics())
.map(|unit| unit.range.clone())
.collect::<Vec<_>>();
let mut coalesced = Vec::<Range<usize>>::with_capacity(ranges.len());
for range in ranges.drain(..) {
if let Some(previous) = coalesced.last_mut()
&& previous.end == range.start
{
previous.end = range.end;
} else {
coalesced.push(range);
}
}
coalesced
}
pub(crate) fn source_slice(&self, range: Range<usize>) -> Option<Self> {
match self {
Self::Horizontal => Some(Self::Horizontal),
Self::Vertical(units) => {
let mut selected = Vec::new();
for unit in units.iter() {
if unit.range.end <= range.start || range.end <= unit.range.start {
continue;
}
let cuts_unit = unit.range.start < range.start || range.end < unit.range.end;
if cuts_unit
&& !matches!(
unit.typesetting,
VerticalUnitTypesetting::SidewaysHorizontal
)
{
return None;
}
selected.push(ResolvedVerticalUnit {
// Native vertical cursive shaping may establish one
// sideways-horizontal unit across several authored
// inline fragments. A source slice must retain that
// unit's already-resolved mode for its selected
// intersection rather than re-shaping without its
// synthetic context.
range: unit.range.start.max(range.start) - range.start
..unit.range.end.min(range.end) - range.start,
typesetting: unit.typesetting,
});
}
(!selected.is_empty())
.then(|| Self::Vertical(selected.into()))
.or(Some(Self::Horizontal))
}
}
}
}
fn resolve_vertical_unit(orientation: TextOrientation, text: &str) -> VerticalUnitTypesetting {
match orientation {
TextOrientation::Sideways => VerticalUnitTypesetting::SidewaysHorizontal,
TextOrientation::Mixed => match typographic_unit_mixed_orientation(text) {
MixedTextOrientation::Upright => VerticalUnitTypesetting::UprightVertical,
MixedTextOrientation::Sideways => VerticalUnitTypesetting::SidewaysHorizontal,
},
TextOrientation::Upright => {
// Mongolian and Phags-pa are intrinsically vertical, but their
// normal horizontal composition is counter-clockwise from that
// intrinsic presentation. CSS's sideways-horizontal path supplies
// the clockwise transform without applying vertical substitutions
// or metrics; this is the behavior asserted by the WPTs.
if text
.chars()
.find(|character| !character_inherits_vertical_orientation(*character))
.is_some_and(character_is_native_vertical_script)
{
VerticalUnitTypesetting::SidewaysHorizontal
} else {
VerticalUnitTypesetting::UprightVertical
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::css::WritingMode;
fn vertical_style(orientation: TextOrientation) -> ComputedStyle {
let mut style = ComputedStyle::initial();
style.writing_mode = WritingMode::VerticalLr;
style.text_orientation = orientation;
style
}
fn modes(text: &str, orientation: TextOrientation) -> Vec<VerticalUnitTypesetting> {
TextTypesettingPlan::resolve(text, &vertical_style(orientation))
.units()
.iter()
.map(|unit| unit.typesetting)
.collect()
}
#[test]
fn mixed_resolves_u_tu_and_tr_upright_but_r_sideways() {
assert_eq!(
modes("a§、〈", TextOrientation::Mixed),
[
VerticalUnitTypesetting::SidewaysHorizontal,
VerticalUnitTypesetting::UprightVertical,
VerticalUnitTypesetting::UprightVertical,
VerticalUnitTypesetting::UprightVertical,
]
);
}
#[test]
fn native_vertical_scripts_never_request_vertical_metrics() {
for orientation in [TextOrientation::Mixed, TextOrientation::Upright] {
for text in ["ᠮ", "ꡀ"] {
assert_eq!(
modes(text, orientation),
[VerticalUnitTypesetting::SidewaysHorizontal],
);
}
}
}
#[test]
fn unanimous_typesetting_handles_multi_unit_clusters_without_crossing_modes() {
let upright = vertical_style(TextOrientation::Upright);
let upright_plan = TextTypesettingPlan::resolve("AB", &upright);
assert_eq!(
upright_plan.unanimous_typesetting_for_range(&(0..2)),
Some(VerticalUnitTypesetting::UprightVertical)
);
let mixed = vertical_style(TextOrientation::Mixed);
let mixed_plan = TextTypesettingPlan::resolve("a、", &mixed);
assert_eq!(
mixed_plan.unanimous_typesetting_for_range(&(0..4)),
None,
"a sideways and an upright ideographic comma are ambiguous together"
);
}
#[test]
fn source_slice_projects_a_contextual_native_vertical_unit() {
let text = "ᠨᠨᠨ";
let plan = TextTypesettingPlan::resolve(text, &vertical_style(TextOrientation::Upright));
let slice = plan
.source_slice('ᠨ'.len_utf8()..'ᠨ'.len_utf8() * 2)
.expect("a source fragment may reuse its contextual vertical mode");
assert_eq!(
slice.units(),
[ResolvedVerticalUnit {
range: 0..'ᠨ'.len_utf8(),
typesetting: VerticalUnitTypesetting::SidewaysHorizontal,
}]
);
}
#[test]
fn sideways_and_horizontal_plans_have_no_upright_ranges() {
let sideways =
TextTypesettingPlan::resolve("中文", &vertical_style(TextOrientation::Sideways));
assert!(sideways.upright_vertical_ranges().is_empty());
assert!(
TextTypesettingPlan::resolve("中文", &ComputedStyle::initial())
.upright_vertical_ranges()
.is_empty()
);
}
}