234 lines
7.2 KiB
Rust
234 lines
7.2 KiB
Rust
//! The same font-slot widths drive break search and the owned margin kerns.
|
|
use super::*;
|
|
|
|
#[derive(Clone, Copy)]
|
|
enum Edge {
|
|
Skip,
|
|
Solid,
|
|
Glyph(FontId, u8),
|
|
}
|
|
|
|
impl Edge {
|
|
fn character(node: &TextNode) -> Self {
|
|
match node {
|
|
TextNode::Glyph { font, slot, .. } => Self::Glyph(*font, *slot),
|
|
TextNode::MathChar(glyph) => {
|
|
use crate::math_engine_tex::MathItem;
|
|
match glyph
|
|
.math_geometry
|
|
.as_deref()
|
|
.map(|geometry| geometry.items.as_slice())
|
|
{
|
|
Some([item]) => match &item.item {
|
|
MathItem::NativeGlyph { font, slot, .. } => Self::Glyph(font.id, *slot),
|
|
_ => Self::Solid,
|
|
},
|
|
_ => Self::Solid,
|
|
}
|
|
}
|
|
_ => Self::Solid,
|
|
}
|
|
}
|
|
|
|
fn node(node: &TextNode, right: bool) -> Self {
|
|
match node {
|
|
TextNode::Box(boxed) => match boxed.native_box.as_deref() {
|
|
Some(NativeBox {
|
|
content: BoxContent::Horizontal(nodes),
|
|
..
|
|
}) if !nodes.is_empty() => Self::list(nodes, right),
|
|
Some(NativeBox {
|
|
content: BoxContent::Horizontal(_),
|
|
..
|
|
}) if boxed.width == 0 && boxed.height == 0 && boxed.depth == 0 => Self::Skip,
|
|
_ => Self::Solid,
|
|
},
|
|
_ if skippable(node) => Self::Skip,
|
|
_ => Self::character(node),
|
|
}
|
|
}
|
|
|
|
fn list(nodes: &[TextNode], right: bool) -> Self {
|
|
let mut nodes = nodes.iter();
|
|
while let Some(node) = if right {
|
|
nodes.next_back()
|
|
} else {
|
|
nodes.next()
|
|
} {
|
|
let edge = Self::node(node, right);
|
|
if !matches!(edge, Self::Skip) {
|
|
return edge;
|
|
}
|
|
}
|
|
Self::Skip
|
|
}
|
|
|
|
fn width(self, state: &State, right: bool) -> i32 {
|
|
let Self::Glyph(font, slot) = self else {
|
|
return 0;
|
|
};
|
|
let code = state.fonts.protrusion_code(font, slot, right);
|
|
if code == 0 {
|
|
return 0;
|
|
}
|
|
let quad = state
|
|
.fonts
|
|
.font(font)
|
|
.expect("marginal glyph's font is live")
|
|
.quad();
|
|
let scaled = i64::from(quad) * i64::from(code);
|
|
// Round to the nearest sp, taking half a unit away from zero.
|
|
(scaled.signum() * ((scaled.abs() + 500) / 1000)) as i32
|
|
}
|
|
}
|
|
|
|
fn skippable(node: &TextNode) -> bool {
|
|
match node {
|
|
TextNode::Box(boxed) => matches!(
|
|
boxed.native_box.as_deref(),
|
|
Some(NativeBox { content: BoxContent::Horizontal(nodes), .. })
|
|
if nodes.is_empty() && boxed.width == 0 && boxed.height == 0 && boxed.depth == 0
|
|
),
|
|
TextNode::Language(_)
|
|
| TextNode::Mark(_)
|
|
| TextNode::Adjustment(_)
|
|
| TextNode::Insertion(_)
|
|
| TextNode::Penalty(_)
|
|
| TextNode::Indent(0)
|
|
| TextNode::AccentKern(0)
|
|
| TextNode::AutoKern(_)
|
|
| TextNode::MathBoundary { width: 0, .. }
|
|
| TextNode::Kern { explicit: false, .. }
|
|
| TextNode::Kern { width: 0, .. } => true,
|
|
TextNode::Output(node) => node.dimensions().is_none(),
|
|
TextNode::Glue(glue)
|
|
| TextNode::NamedGlue(glue, _)
|
|
| TextNode::Leaders(LeaderGlue { glue, .. }) => glue.canonical_zero,
|
|
TextNode::Discretionary {
|
|
pre_break,
|
|
post_break,
|
|
replacement_count: 0,
|
|
..
|
|
} => pre_break.is_empty() && post_break.is_empty(),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub(in crate::execution) fn stored_margin_kern(nodes: &[TextNode], right: bool) -> i32 {
|
|
let side_skip = if right { Parameter::Rightskip } else { Parameter::Leftskip };
|
|
let non_skippable = |node: &&TextNode| {
|
|
!matches!(node, TextNode::NamedGlue(_, parameter) if *parameter == side_skip)
|
|
&& !skippable(node)
|
|
};
|
|
// The query walks only the stored hlist. It does not descend into a nested
|
|
// box or recalculate protrusion using the current font codes.
|
|
let edge = if right {
|
|
nodes.iter().rev().find(non_skippable)
|
|
} else {
|
|
nodes.iter().find(non_skippable)
|
|
};
|
|
match edge {
|
|
Some(TextNode::MarginKern { width, right: actual_side, .. }) if *actual_side == right => {
|
|
*width
|
|
}
|
|
_ => 0,
|
|
}
|
|
}
|
|
|
|
pub(super) fn margin_kern(
|
|
state: &State,
|
|
nodes: &[TextNode],
|
|
right: bool,
|
|
direct: bool,
|
|
) -> Option<TextNode> {
|
|
let edge = if direct {
|
|
nodes.last().map_or(Edge::Solid, Edge::character)
|
|
} else {
|
|
Edge::list(nodes, right)
|
|
};
|
|
let Edge::Glyph(font, slot) = edge else {
|
|
return None;
|
|
};
|
|
let width = -edge.width(state, right);
|
|
(width != 0).then_some(TextNode::MarginKern {
|
|
width,
|
|
right,
|
|
font,
|
|
slot,
|
|
})
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
|
struct Projected {
|
|
left: Option<(usize, i32)>,
|
|
right: Option<(usize, i32)>,
|
|
pre: Option<i32>,
|
|
post: Option<i32>,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
pub(super) struct Search(Vec<Projected>);
|
|
|
|
impl Search {
|
|
pub(super) fn new(state: &State, nodes: &[TextNode]) -> Self {
|
|
let mut widths = vec![Projected::default(); nodes.len() + 1];
|
|
for (i, node) in nodes.iter().enumerate() {
|
|
let edge = Edge::node(node, true);
|
|
widths[i + 1].right = if matches!(edge, Edge::Skip) {
|
|
widths[i].right
|
|
} else {
|
|
Some((i, edge.width(state, true)))
|
|
};
|
|
if let TextNode::Discretionary {
|
|
pre_break,
|
|
post_break,
|
|
..
|
|
} = node
|
|
{
|
|
widths[i].pre = pre_break
|
|
.last()
|
|
.map(|node| Edge::character(node).width(state, true));
|
|
widths[i].post = post_break
|
|
.first()
|
|
.map(|node| Edge::character(node).width(state, false));
|
|
}
|
|
}
|
|
for (i, node) in nodes.iter().enumerate().rev() {
|
|
let edge = Edge::node(node, false);
|
|
widths[i].left = if matches!(edge, Edge::Skip) {
|
|
widths[i + 1].left
|
|
} else {
|
|
Some((i, edge.width(state, false)))
|
|
};
|
|
}
|
|
Self(widths)
|
|
}
|
|
|
|
pub(super) fn len(&self) -> usize {
|
|
self.0.len()
|
|
}
|
|
|
|
pub(super) fn width(
|
|
&self,
|
|
start: usize,
|
|
end: usize,
|
|
post: Option<usize>,
|
|
pre: Option<usize>,
|
|
right_visible: bool,
|
|
) -> i64 {
|
|
let left = post.and_then(|i| self.0[i].post).or_else(|| {
|
|
self.0[start]
|
|
.left
|
|
.filter(|(i, _)| *i < end)
|
|
.map(|(_, width)| width)
|
|
});
|
|
let right = pre.and_then(|i| self.0[i].pre).or_else(|| {
|
|
self.0[end]
|
|
.right
|
|
.filter(|(i, _)| right_visible && *i >= start)
|
|
.map(|(_, width)| width)
|
|
});
|
|
i64::from(left.unwrap_or(0)) + i64::from(right.unwrap_or(0))
|
|
}
|
|
}
|