Execute and render TeX--XeT direction spans in native layouts
This commit is contained in:
1 parent
a93d1bb902
commit
22e3ba993d
17 files changed
+460
-54
No files matched your search
@@ -147,6 +147,17 @@ enum PackedHorizontal<'a> {
|
||||
Output(&'a OutputNode),
|
||||
}
|
||||
|
||||
impl PackedHorizontal<'_> {
|
||||
fn width(&self) -> i64 {
|
||||
match self {
|
||||
Self::Item(HorizontalItem::Box(b)) => b.width,
|
||||
Self::Item(HorizontalItem::Glue { width, .. }) | Self::Leaders(_, width) => *width,
|
||||
Self::Output(node) => node.dimensions().map_or(0, |(width, _, _)| width),
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) enum BoxPaint<'a> {
|
||||
Box(&'a HorizontalBox),
|
||||
PdfColor,
|
||||
@@ -218,21 +229,24 @@ pub(super) enum GeometryItem {
|
||||
|
||||
impl NativeBox {
|
||||
pub(super) fn output_nodes(&self) -> (Vec<&OutputNode>, bool) {
|
||||
fn walk<'a>(b: &'a NativeBox, out: &mut Vec<&'a OutputNode>, leaders: &mut bool) {
|
||||
fn walk<'a>(b: &'a NativeBox, rtl: bool, out: &mut Vec<&'a OutputNode>, leaders: &mut bool) {
|
||||
if b.kind.is_unset() {
|
||||
return;
|
||||
}
|
||||
match &b.content {
|
||||
BoxContent::Horizontal(nodes) => {
|
||||
for n in nodes {
|
||||
match n {
|
||||
let order = super::direction::visual_order(nodes, rtl && !b.display);
|
||||
let order = order.as_deref();
|
||||
for index in 0..nodes.len() {
|
||||
let (index, rtl) = order.map_or((index, false), |order| order[index]);
|
||||
match &nodes[index] {
|
||||
TextNode::Output(o) => out.push(o),
|
||||
TextNode::Leaders(_) => *leaders = true,
|
||||
TextNode::Box(b)
|
||||
| TextNode::MathChar(b)
|
||||
| TextNode::Glyph { glyph: b, .. } => {
|
||||
if let Some(b) = &b.native_box {
|
||||
walk(b, out, leaders)
|
||||
walk(b, rtl, out, leaders)
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -245,7 +259,7 @@ impl NativeBox {
|
||||
NativeVerticalNode::Output(o) => out.push(o),
|
||||
NativeVerticalNode::Leaders(_) => *leaders = true,
|
||||
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => {
|
||||
walk(b, out, leaders)
|
||||
walk(b, rtl, out, leaders)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -255,7 +269,7 @@ impl NativeBox {
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let mut leaders = false;
|
||||
walk(self, &mut out, &mut leaders);
|
||||
walk(self, false, &mut out, &mut leaders);
|
||||
(out, leaders)
|
||||
}
|
||||
|
||||
@@ -271,7 +285,7 @@ impl NativeBox {
|
||||
cancellation,
|
||||
leader_depth: 0,
|
||||
};
|
||||
self.walk_at(0, 0, 0, false, &mut budget, &mut |x, y, item| {
|
||||
self.walk_at(0, 0, 0, false, false, &mut budget, &mut |x, y, item| {
|
||||
if let BoxPaint::Output(o) = item {
|
||||
if o.needs_shipout_position() {
|
||||
output_positions.push([x, y]);
|
||||
@@ -301,9 +315,9 @@ impl NativeBox {
|
||||
/// The box never stores a second recursive copy of its children for paint.
|
||||
pub fn horizontal_items(&self) -> Option<Vec<HorizontalItem>> {
|
||||
Some(
|
||||
self.packed_horizontal()?
|
||||
self.packed_horizontal(false)?
|
||||
.into_iter()
|
||||
.filter_map(|item| match item {
|
||||
.filter_map(|(item, _)| match item {
|
||||
PackedHorizontal::Item(value) => Some(value),
|
||||
PackedHorizontal::Leaders(_, width) => Some(HorizontalItem::Glue {
|
||||
width,
|
||||
@@ -317,16 +331,14 @@ impl NativeBox {
|
||||
)
|
||||
}
|
||||
|
||||
fn packed_horizontal(&self) -> Option<Vec<PackedHorizontal<'_>>> {
|
||||
fn packed_horizontal<'a>(&'a self, rtl: bool) -> Option<Vec<(PackedHorizontal<'a>, bool)>> {
|
||||
let BoxContent::Horizontal(nodes) = &self.content else {
|
||||
return None;
|
||||
};
|
||||
let (mut cumulative, mut previous) = (0, 0);
|
||||
let mut items = Vec::new();
|
||||
for node in nodes {
|
||||
if matches!(node, TextNode::Box(b) if b.native_box.as_ref().is_some_and(|b| b.kind.is_unset()))
|
||||
{
|
||||
continue;
|
||||
let mut pack = |node: &'a TextNode| {
|
||||
if matches!(node, TextNode::Box(b) if b.native_box.as_ref().is_some_and(|b| b.kind.is_unset())) {
|
||||
return None;
|
||||
}
|
||||
let item = match node {
|
||||
TextNode::Glyph { glyph, .. }
|
||||
@@ -361,18 +373,22 @@ impl NativeBox {
|
||||
let g = leader.glue;
|
||||
let width = i64::from(g.width)
|
||||
+ self.setting.adjustment(g, &mut cumulative, &mut previous);
|
||||
items.push(PackedHorizontal::Leaders(leader, width));
|
||||
continue;
|
||||
return Some(PackedHorizontal::Leaders(leader, width));
|
||||
}
|
||||
TextNode::Output(node) => {
|
||||
items.push(PackedHorizontal::Output(node));
|
||||
continue;
|
||||
return Some(PackedHorizontal::Output(node));
|
||||
}
|
||||
_ => continue,
|
||||
_ => return None,
|
||||
};
|
||||
items.push(PackedHorizontal::Item(item));
|
||||
}
|
||||
Some(items)
|
||||
Some(PackedHorizontal::Item(item))
|
||||
};
|
||||
let Some(order) = super::direction::visual_order(nodes, rtl) else {
|
||||
return Some(nodes.iter().filter_map(|node| pack(node).map(|item| (item, false))).collect());
|
||||
};
|
||||
// Glue accumulates its rounding in logical order before the device
|
||||
// reverses a span. Reuse those adjusted widths in visual order.
|
||||
let mut logical: Vec<_> = nodes.iter().map(pack).collect();
|
||||
Some(order.into_iter().filter_map(|(i, rtl)| logical[i].take().map(|item| (item, rtl))).collect())
|
||||
}
|
||||
|
||||
pub fn node_count(&self) -> usize {
|
||||
@@ -591,11 +607,21 @@ pub(super) fn dimensions_fit(w: i64, h: i64, d: i64) -> Result<(), String> {
|
||||
}
|
||||
}
|
||||
pub(super) fn hpack(
|
||||
mut nodes: Vec<TextNode>,
|
||||
nodes: Vec<TextNode>,
|
||||
spec: PackSpec,
|
||||
span: Span,
|
||||
state: &State,
|
||||
) -> Result<NativeBox, String> {
|
||||
hpack_with_direction_report(nodes, spec, span, state).map(|(packed, _)| packed)
|
||||
}
|
||||
|
||||
pub(super) fn hpack_with_direction_report(
|
||||
mut nodes: Vec<TextNode>,
|
||||
spec: PackSpec,
|
||||
span: Span,
|
||||
state: &State,
|
||||
) -> Result<(NativeBox, (usize, usize)), String> {
|
||||
let direction_problems = super::direction::complete_box(&mut nodes);
|
||||
let (mut width, mut height, mut depth) = (0, 0, 0);
|
||||
let (mut stretch, mut shrink) = ([0; 4], [0; 4]);
|
||||
for node in &nodes {
|
||||
@@ -663,7 +689,7 @@ pub(super) fn hpack(
|
||||
}));
|
||||
}
|
||||
let diagnostic = super::diagnostics::text_nodes(&nodes);
|
||||
Ok(NativeBox {
|
||||
Ok((NativeBox {
|
||||
kind: BoxKind::Horizontal,
|
||||
display: false,
|
||||
width: desired,
|
||||
@@ -677,7 +703,7 @@ pub(super) fn hpack(
|
||||
diagnostic_eligible,
|
||||
excess,
|
||||
span,
|
||||
})
|
||||
}, direction_problems))
|
||||
}
|
||||
|
||||
/// Flatten already packed native geometry for the common PDF/SVG display list.
|
||||
@@ -817,11 +843,11 @@ impl NativeBox {
|
||||
y: i64,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) -> Result<(), &'static str> {
|
||||
self.walk_at(x, y, 0, false, &mut WalkBudget::for_geometry(), emit)
|
||||
self.walk_at(x, y, 0, false, false, &mut WalkBudget::for_geometry(), emit)
|
||||
}
|
||||
|
||||
pub(super) fn walk_pdf_links(&self, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
|
||||
self.walk_at(0, 0, 0, true, &mut WalkBudget::for_geometry(), emit)
|
||||
self.walk_at(0, 0, 0, false, true, &mut WalkBudget::for_geometry(), emit)
|
||||
.expect("link traversal of an already measured list");
|
||||
}
|
||||
|
||||
@@ -830,6 +856,7 @@ impl NativeBox {
|
||||
x: i64,
|
||||
y: i64,
|
||||
level: usize,
|
||||
rtl: bool,
|
||||
links: bool,
|
||||
budget: &mut WalkBudget<'_>,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
@@ -840,9 +867,16 @@ impl NativeBox {
|
||||
match &self.content {
|
||||
BoxContent::Horizontal(nodes) => {
|
||||
budget.inside_leader(nodes.len())?;
|
||||
let rtl = rtl && !self.display;
|
||||
let packed = self.packed_horizontal(rtl).unwrap();
|
||||
let left = if rtl {
|
||||
x + self.width - packed.iter().map(|(item, _)| item.width()).sum::<i64>()
|
||||
} else {
|
||||
x
|
||||
};
|
||||
let row = BoxLayout {
|
||||
horizontal: true,
|
||||
left: x,
|
||||
left,
|
||||
baseline: y,
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
@@ -850,14 +884,15 @@ impl NativeBox {
|
||||
level,
|
||||
};
|
||||
if links {
|
||||
emit(x, y, BoxPaint::RowStart(row));
|
||||
emit(left, y, BoxPaint::RowStart(row));
|
||||
}
|
||||
walk_row(&self.packed_horizontal().unwrap(), row, links, budget, emit)?;
|
||||
walk_row(&packed, row, links, budget, emit)?;
|
||||
}
|
||||
BoxContent::Vertical(nodes) => {
|
||||
let edge = if rtl { x + self.width } else { x };
|
||||
let container = BoxLayout {
|
||||
horizontal: false,
|
||||
left: x,
|
||||
left: edge,
|
||||
baseline: y,
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
@@ -874,7 +909,8 @@ impl NativeBox {
|
||||
continue;
|
||||
}
|
||||
cursor += b.height;
|
||||
b.walk_at(x + b.shift, cursor, level + 1, links, budget, emit)?;
|
||||
let child_x = if rtl { edge - b.shift - b.width } else { x + b.shift };
|
||||
b.walk_at(child_x, cursor, level + 1, rtl, links, budget, emit)?;
|
||||
cursor += b.depth;
|
||||
}
|
||||
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
|
||||
@@ -894,9 +930,10 @@ impl NativeBox {
|
||||
b.height + b.depth,
|
||||
) {
|
||||
b.walk_leader(
|
||||
x + b.shift,
|
||||
if rtl { edge - b.shift - b.width } else { x + b.shift },
|
||||
pos + b.height,
|
||||
level + 1,
|
||||
rtl,
|
||||
links,
|
||||
budget,
|
||||
emit,
|
||||
@@ -922,14 +959,14 @@ impl NativeBox {
|
||||
NativeVerticalNode::Rule(r) => {
|
||||
let b = r.horizontal_item(self.width, 0, 0);
|
||||
cursor += b.height;
|
||||
emit(x, cursor, BoxPaint::Box(&b));
|
||||
emit(if rtl { edge - b.width } else { x }, cursor, BoxPaint::Box(&b));
|
||||
cursor += b.depth;
|
||||
}
|
||||
NativeVerticalNode::Output(node) => {
|
||||
walk_output(node, x, cursor, container, links, emit);
|
||||
walk_output(node, edge, cursor, container, links, emit);
|
||||
if let Some(image) = node.image_box() {
|
||||
cursor += image.height;
|
||||
emit(x, cursor, BoxPaint::Box(&image));
|
||||
emit(if rtl { edge - image.width } else { x }, cursor, BoxPaint::Box(&image));
|
||||
cursor += image.depth;
|
||||
}
|
||||
}
|
||||
@@ -946,13 +983,14 @@ impl NativeBox {
|
||||
x: i64,
|
||||
y: i64,
|
||||
level: usize,
|
||||
rtl: bool,
|
||||
links: bool,
|
||||
budget: &mut WalkBudget<'_>,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) -> Result<(), &'static str> {
|
||||
budget.charge(1)?;
|
||||
budget.leader_depth += 1;
|
||||
let result = self.walk_at(x, y, level, links, budget, &mut |x, y, paint| {
|
||||
let result = self.walk_at(x, y, level, rtl, links, budget, &mut |x, y, paint| {
|
||||
if !matches!(paint, BoxPaint::Output(o) if matches!(o.operation,
|
||||
OutputOperation::Open { .. } | OutputOperation::Write { .. } | OutputOperation::Close { .. }))
|
||||
&& !matches!(paint, BoxPaint::PdfLink(node, _) if matches!(
|
||||
@@ -975,7 +1013,7 @@ pub(super) fn bp(n: i64) -> i64 {
|
||||
}
|
||||
}
|
||||
fn walk_row(
|
||||
items: &[PackedHorizontal<'_>],
|
||||
items: &[(PackedHorizontal<'_>, bool)],
|
||||
row: BoxLayout,
|
||||
links: bool,
|
||||
budget: &mut WalkBudget<'_>,
|
||||
@@ -983,11 +1021,11 @@ fn walk_row(
|
||||
) -> Result<(), &'static str> {
|
||||
let mut x = row.left;
|
||||
let y = row.baseline;
|
||||
for item in items {
|
||||
for (item, rtl) in items {
|
||||
match item {
|
||||
PackedHorizontal::Item(HorizontalItem::Box(b)) => {
|
||||
if let Some(child) = &b.native_box {
|
||||
child.walk_at(x, y + child.shift, row.level + 1, links, budget, emit)?;
|
||||
child.walk_at(x, y + child.shift, row.level + 1, *rtl, links, budget, emit)?;
|
||||
} else {
|
||||
if let Some(geometry) = &b.math_geometry {
|
||||
budget.inside_leader(geometry.items.len())?;
|
||||
@@ -1001,7 +1039,7 @@ fn walk_row(
|
||||
match &leader.pattern {
|
||||
LeaderPattern::Box(b) => {
|
||||
for pos in leader.positions(row.left, x, *width, b.width) {
|
||||
b.walk_leader(pos, y + b.shift, row.level + 1, links, budget, emit)?;
|
||||
b.walk_leader(pos, y + b.shift, row.level + 1, *rtl, links, budget, emit)?;
|
||||
}
|
||||
}
|
||||
LeaderPattern::Rule(r) if *width > 0 => {
|
||||
|
||||
Reference in new issue
Block a user