297 lines
10 KiB
Rust
297 lines
10 KiB
Rust
//! The same shaped edges drive break search and the owned margin kerns.
|
|
use super::*;
|
|
|
|
#[derive(Clone, Copy)]
|
|
enum Edge {
|
|
Skip,
|
|
Solid,
|
|
Glyph(FontId, u8),
|
|
OpenType(FontId, u16),
|
|
}
|
|
|
|
impl Edge {
|
|
fn opentype(run: &OpenTypeRun, right: bool) -> Self {
|
|
let glyph = if right { run.glyphs.last() } else { run.glyphs.first() };
|
|
glyph.map_or(Self::Solid, |glyph| Self::OpenType(run.font.id, glyph.id))
|
|
}
|
|
|
|
fn character(node: &TextNode, right: bool) -> Self {
|
|
match node {
|
|
TextNode::Glyph { font, slot, .. } => Self::Glyph(*font, *slot),
|
|
TextNode::OpenType(run) => Self::opentype(run, right),
|
|
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),
|
|
MathItem::OpenType(run) => Self::opentype(run, right),
|
|
_ => 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, right),
|
|
}
|
|
}
|
|
|
|
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) -> Result<i32, String> {
|
|
let xe = state.input_profile() == InputProfile::XeTeX;
|
|
let (font, code) = match self {
|
|
Self::Glyph(font, slot) if !xe => (font, state.fonts.protrusion_code(font, slot, right)),
|
|
Self::Glyph(font, slot) => (font, state.fonts.xe_protrusion_code(font, i32::from(slot), right)),
|
|
Self::OpenType(font, glyph) if xe => (font, state.fonts.xe_protrusion_code(font, i32::from(glyph), right)),
|
|
_ => return Ok(0),
|
|
};
|
|
if code == 0 {
|
|
return Ok(0);
|
|
}
|
|
let quad = state.fonts.font(font)?.quad();
|
|
let scaled = i64::from(quad) * i64::from(code);
|
|
// XeTeX's signed remainder does not round up for negative factors.
|
|
let rounding = if xe && code < 0 { 0 } else { 500 };
|
|
let width = scaled.signum() * ((scaled.abs() + rounding) / 1000);
|
|
if width.abs() > 1073741823 {
|
|
return Err("native character protrusion exceeds dimension capacity".into());
|
|
}
|
|
Ok(width as i32)
|
|
}
|
|
}
|
|
|
|
pub(super) fn level(state: &State, parameters: &BTreeMap<Parameter, ParameterValue>) -> i32 {
|
|
let parameter = if state.input_profile() == InputProfile::XeTeX {
|
|
Parameter::XeTeXprotrudechars
|
|
} else {
|
|
Parameter::Pdfprotrudechars
|
|
};
|
|
parameters[¶meter].width()
|
|
}
|
|
|
|
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::Direction(_)
|
|
| TextNode::Kern { explicit: false, .. }
|
|
| TextNode::Kern { width: 0, .. } => true,
|
|
TextNode::Output(node) => node.dimensions().is_none(),
|
|
TextNode::Glue(glue) | TextNode::FontSpace(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,
|
|
) -> Result<Option<TextNode>, String> {
|
|
let edge = if direct {
|
|
nodes.last().map_or(Edge::Solid, |node| Edge::character(node, right))
|
|
} else {
|
|
Edge::list(nodes, right)
|
|
};
|
|
let (font, slot) = match edge {
|
|
Edge::Glyph(font, slot) => (font, u16::from(slot)),
|
|
Edge::OpenType(font, glyph) => (font, glyph),
|
|
_ => return Ok(None),
|
|
};
|
|
let width = -edge.width(state, right)?;
|
|
Ok((width != 0).then_some(TextNode::MarginKern {
|
|
width,
|
|
right,
|
|
font,
|
|
slot,
|
|
}))
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
|
pub(super) struct EdgeMeasure {
|
|
pub(super) width: i32,
|
|
expanding: Option<(FontId, u8)>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
|
struct Projected {
|
|
left: Option<(usize, EdgeMeasure)>,
|
|
right: Option<(usize, EdgeMeasure)>,
|
|
pre: Option<EdgeMeasure>,
|
|
post: Option<EdgeMeasure>,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
|
pub(super) struct Search {
|
|
widths: Vec<Projected>,
|
|
variations: BTreeMap<(FontId, u8), expansion::MarginVariation>,
|
|
}
|
|
|
|
impl Search {
|
|
pub(super) fn new(state: &State, nodes: &[TextNode], expand: bool) -> Result<Self, String> {
|
|
let mut widths = vec![Projected::default(); nodes.len() + 1];
|
|
let mut variations = BTreeMap::new();
|
|
let mut project = |edge: Edge, right| -> Result<EdgeMeasure, String> {
|
|
use std::collections::btree_map::Entry;
|
|
let width = edge.width(state, right)?;
|
|
let mut expanding = None;
|
|
if expand && let Edge::Glyph(font, slot) = edge {
|
|
let key = (font, slot);
|
|
match variations.entry(key) {
|
|
Entry::Occupied(_) => expanding = Some(key),
|
|
Entry::Vacant(entry) => {
|
|
if let Some(variation) = expansion::MarginVariation::project(state, font, slot)? {
|
|
entry.insert(variation);
|
|
expanding = Some(key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(EdgeMeasure { width, expanding })
|
|
};
|
|
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, project(edge, true)?))
|
|
};
|
|
if let TextNode::Discretionary { pre_break, post_break, .. } = node {
|
|
widths[i].pre = pre_break.last()
|
|
.map(|node| project(Edge::character(node, true), true)).transpose()?;
|
|
widths[i].post = post_break.first()
|
|
.map(|node| project(Edge::character(node, false), false)).transpose()?;
|
|
}
|
|
}
|
|
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, project(edge, false)?))
|
|
};
|
|
}
|
|
Ok(Self { widths, variations })
|
|
}
|
|
|
|
pub(super) fn len(&self) -> usize {
|
|
self.widths.len() + self.variations.len()
|
|
}
|
|
|
|
pub(super) fn needs_fonts(&self) -> bool {
|
|
self.variations.values().any(|v| matches!(v, expansion::MarginVariation::Pending { .. }))
|
|
}
|
|
|
|
pub(super) fn edges(
|
|
&self,
|
|
start: usize,
|
|
end: usize,
|
|
post: Option<usize>,
|
|
pre: Option<usize>,
|
|
right_visible: bool,
|
|
) -> [EdgeMeasure; 2] {
|
|
let left = post.and_then(|i| self.widths[i].post).or_else(|| {
|
|
self.widths[start].left
|
|
.filter(|(i, _)| *i < end)
|
|
.map(|(_, edge)| edge)
|
|
});
|
|
let right = pre.and_then(|i| self.widths[i].pre).or_else(|| {
|
|
self.widths[end].right
|
|
.filter(|(i, _)| right_visible && *i >= start)
|
|
.map(|(_, edge)| edge)
|
|
});
|
|
[left.unwrap_or_default(), right.unwrap_or_default()]
|
|
}
|
|
|
|
pub(super) fn variation(
|
|
&mut self,
|
|
edges: [EdgeMeasure; 2],
|
|
state: &mut State,
|
|
files: &mut impl SourceProvider,
|
|
) -> Result<expansion::Capacity, String> {
|
|
let mut total = expansion::Capacity::default();
|
|
for edge in edges {
|
|
if let Some(key) = edge.expanding {
|
|
total += self.variations.get_mut(&key).expect("projected margin glyph")
|
|
.resolve(state, key.0, files)?;
|
|
}
|
|
}
|
|
Ok(total)
|
|
}
|
|
}
|