Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,749 @@
|
||||
//! Owned native boxes. Packing follows tex.web hpack/vpackage, not page costs.
|
||||
use super::*;
|
||||
use crate::{HorizontalBox, HorizontalItem, Paragraph, ParagraphFont};
|
||||
mod tail;
|
||||
pub(super) use tail::TailNodeRef;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum BoxKind {
|
||||
Horizontal,
|
||||
Vertical,
|
||||
Vtop,
|
||||
Unset {
|
||||
span: u8,
|
||||
stretch: Flex,
|
||||
shrink: Flex,
|
||||
},
|
||||
}
|
||||
impl BoxKind {
|
||||
pub fn is_unset(self) -> bool {
|
||||
matches!(self, Self::Unset { .. })
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum PackSpec {
|
||||
Natural,
|
||||
To(i32),
|
||||
Spread(i32),
|
||||
}
|
||||
#[derive(
|
||||
Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
|
||||
)]
|
||||
pub struct GlueSetting {
|
||||
pub sign: i8,
|
||||
pub order: u8,
|
||||
pub numerator: i64,
|
||||
pub denominator: i64,
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum BoxContent {
|
||||
Horizontal(Vec<TextNode>),
|
||||
Vertical(Vec<NativeVerticalNode>),
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub struct NativeBox {
|
||||
pub kind: BoxKind,
|
||||
/// Actual e-TeX dlist subtype of a display math hbox.
|
||||
pub display: bool,
|
||||
pub width: i64,
|
||||
/// TeX shift_amount: horizontal on a vertical list, vertical on a hlist.
|
||||
pub shift: i64,
|
||||
pub height: i64,
|
||||
pub depth: i64,
|
||||
pub content: BoxContent,
|
||||
/// Original horizontal semantics, retained before paint-time glue setting.
|
||||
pub diagnostic: Vec<super::diagnostics::DiagnosticNode>,
|
||||
pub setting: GlueSetting,
|
||||
pub badness: i32,
|
||||
pub diagnostic_eligible: bool,
|
||||
pub excess: i64,
|
||||
pub span: Span,
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
pub enum NativeVerticalNode {
|
||||
Box(Box<NativeBox>),
|
||||
Line(Box<NativeBox>),
|
||||
Glue(Glue),
|
||||
NamedGlue(Glue, Parameter),
|
||||
Kern(i32),
|
||||
/// A normal (engine-generated) kern, distinct from an explicit user kern.
|
||||
ImplicitKern(i32),
|
||||
Penalty(i32),
|
||||
Rule(NativeRule),
|
||||
Mark(NativeMark),
|
||||
Insertion(NativeInsertion),
|
||||
Output(OutputNode),
|
||||
}
|
||||
impl NativeVerticalNode {
|
||||
pub(super) fn node_type(&self) -> i32 {
|
||||
match self {
|
||||
Self::Box(b) | Self::Line(b) => {
|
||||
if b.kind.is_unset() {
|
||||
14
|
||||
} else if b.kind == BoxKind::Horizontal {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
}
|
||||
}
|
||||
Self::Rule(_) => 3,
|
||||
Self::Insertion(_) => 4,
|
||||
Self::Mark(_) => 5,
|
||||
Self::Output(_) => 9,
|
||||
Self::Glue(_) | Self::NamedGlue(_, _) => 11,
|
||||
Self::Kern(_) | Self::ImplicitKern(_) => 12,
|
||||
Self::Penalty(_) => 13,
|
||||
}
|
||||
}
|
||||
pub fn dimensions(&self) -> Option<(i64, i64, i64)> {
|
||||
match self {
|
||||
Self::Box(b) => Some((b.width + b.shift, b.height, b.depth)),
|
||||
Self::Line(b) => Some((b.width + b.shift, b.height, b.depth)),
|
||||
Self::Rule(r) => Some(r.dimensions()),
|
||||
Self::Output(node) => node.dimensions(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub(super) fn node_count(&self) -> usize {
|
||||
match self {
|
||||
Self::Box(b) => b.node_count(),
|
||||
Self::Line(b) => b.node_count(),
|
||||
Self::Insertion(n) => 1 + n.nodes.iter().map(Self::node_count).sum::<usize>(),
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
fn text_node_count(nodes: &[TextNode]) -> usize {
|
||||
nodes
|
||||
.iter()
|
||||
.map(|n| {
|
||||
1 + match n {
|
||||
TextNode::Discretionary {
|
||||
pre_break,
|
||||
post_break,
|
||||
..
|
||||
} => text_node_count(pre_break) + text_node_count(post_break),
|
||||
TextNode::Box(b) => b.native_box.as_ref().map_or(0, |b| b.node_count()),
|
||||
TextNode::Adjustment(value) => {
|
||||
value.nodes.iter().map(NativeVerticalNode::node_count).sum()
|
||||
}
|
||||
TextNode::Insertion(value) => {
|
||||
value.nodes.iter().map(NativeVerticalNode::node_count).sum()
|
||||
}
|
||||
_ => 0,
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
enum PackedHorizontal {
|
||||
Item(HorizontalItem),
|
||||
PdfColor,
|
||||
PdfGraphics,
|
||||
}
|
||||
|
||||
enum BoxPaint<'a> {
|
||||
Box(&'a HorizontalBox),
|
||||
PdfColor,
|
||||
PdfGraphics,
|
||||
}
|
||||
|
||||
pub(super) enum GeometryItem {
|
||||
Paint(crate::math_engine_tex::PositionedItem),
|
||||
PdfColor,
|
||||
PdfGraphics { x: i64, y: i64 },
|
||||
}
|
||||
|
||||
impl NativeBox {
|
||||
pub(super) fn output_nodes(&self) -> Vec<&OutputNode> {
|
||||
fn walk<'a>(b: &'a NativeBox, out: &mut Vec<&'a OutputNode>) {
|
||||
if b.kind.is_unset() {
|
||||
return;
|
||||
}
|
||||
match &b.content {
|
||||
BoxContent::Horizontal(nodes) => {
|
||||
for n in nodes {
|
||||
match n {
|
||||
TextNode::Output(o) => out.push(o),
|
||||
TextNode::Box(b)
|
||||
| TextNode::MathChar(b)
|
||||
| TextNode::Glyph { glyph: b, .. } => {
|
||||
if let Some(b) = &b.native_box {
|
||||
walk(b, out)
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
BoxContent::Vertical(nodes) => {
|
||||
for n in nodes {
|
||||
match n {
|
||||
NativeVerticalNode::Output(o) => out.push(o),
|
||||
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => {
|
||||
walk(b, out)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
walk(self, &mut out);
|
||||
out
|
||||
}
|
||||
/// Derive positioned material from the original owned list and glue set.
|
||||
/// 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()?
|
||||
.into_iter()
|
||||
.filter_map(|item| match item {
|
||||
PackedHorizontal::Item(value) => Some(value),
|
||||
PackedHorizontal::PdfColor | PackedHorizontal::PdfGraphics => None,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn packed_horizontal(&self) -> Option<Vec<PackedHorizontal>> {
|
||||
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 item = match node {
|
||||
TextNode::Glyph { glyph, .. }
|
||||
| TextNode::Box(glyph)
|
||||
| TextNode::MathChar(glyph) => HorizontalItem::Box(glyph.clone()),
|
||||
TextNode::Rule(r) => {
|
||||
HorizontalItem::Box(r.horizontal_item(self.width, self.height, self.depth))
|
||||
}
|
||||
TextNode::Indent(w)
|
||||
| TextNode::MathBoundary { width: w, .. }
|
||||
| TextNode::Kern { width: w, .. } => HorizontalItem::Box(HorizontalBox {
|
||||
width: *w as i64,
|
||||
..HorizontalBox::default()
|
||||
}),
|
||||
TextNode::Penalty(n) => HorizontalItem::Penalty {
|
||||
width: 0,
|
||||
penalty: *n as i64,
|
||||
flagged: false,
|
||||
},
|
||||
TextNode::Glue(g) | TextNode::NamedGlue(g, _) => HorizontalItem::Glue {
|
||||
width: g.width as i64
|
||||
+ self.setting.adjustment(*g, &mut cumulative, &mut previous),
|
||||
stretch: 0,
|
||||
shrink: 0,
|
||||
order: 0,
|
||||
},
|
||||
TextNode::Output(node)
|
||||
if matches!(node.operation, OutputOperation::PdfColor { .. }) =>
|
||||
{
|
||||
items.push(PackedHorizontal::PdfColor);
|
||||
continue;
|
||||
}
|
||||
TextNode::Output(node) if node.is_pdf_graphics() => {
|
||||
items.push(PackedHorizontal::PdfGraphics);
|
||||
continue;
|
||||
}
|
||||
TextNode::Output(node) => {
|
||||
let Some(image) = node.image_box() else {
|
||||
continue;
|
||||
};
|
||||
HorizontalItem::Box(image)
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
items.push(PackedHorizontal::Item(item));
|
||||
}
|
||||
Some(items)
|
||||
}
|
||||
|
||||
pub fn node_count(&self) -> usize {
|
||||
1 + match &self.content {
|
||||
BoxContent::Vertical(v) => v.iter().map(NativeVerticalNode::node_count).sum(),
|
||||
BoxContent::Horizontal(v) => text_node_count(v),
|
||||
}
|
||||
}
|
||||
pub(super) fn horizontal_item(self) -> HorizontalBox {
|
||||
HorizontalBox {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth: self.depth,
|
||||
native_box: Some(Box::new(self)),
|
||||
..HorizontalBox::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) struct VerticalBuilder {
|
||||
pub nodes: Vec<NativeVerticalNode>,
|
||||
pub prev_depth: i32,
|
||||
pub previous_lines: usize,
|
||||
// The semantic tail may belong to a list inside an already packed box.
|
||||
// Paths preserve ownership without storing unsafe pointers into Vecs.
|
||||
tail_path: Vec<usize>,
|
||||
}
|
||||
impl Default for VerticalBuilder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
nodes: Vec::new(),
|
||||
prev_depth: -1000 * 65536,
|
||||
previous_lines: 0,
|
||||
tail_path: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl VerticalBuilder {
|
||||
// A horizontal list can still query and change its enclosing vertical
|
||||
// list's prevgraf. A new vertical list starts with its own zero count.
|
||||
pub fn enter_nested(&mut self, horizontal: bool) -> Self {
|
||||
let outer = std::mem::take(self);
|
||||
if horizontal {
|
||||
self.previous_lines = outer.previous_lines;
|
||||
}
|
||||
outer
|
||||
}
|
||||
|
||||
pub fn leave_nested(&mut self, mut outer: Self, horizontal: bool) {
|
||||
if horizontal {
|
||||
outer.previous_lines = self.previous_lines;
|
||||
}
|
||||
*self = outer;
|
||||
}
|
||||
|
||||
pub fn append_box(&mut self, node: NativeVerticalNode, state: &State) -> Result<(), String> {
|
||||
let (_, height, depth) = node.dimensions().expect("box or line");
|
||||
if self.prev_depth > -1000 * 65536 {
|
||||
let mut label = Parameter::Baselineskip;
|
||||
let mut g = state.parameter(label).glue();
|
||||
let gap = g.width as i64 - self.prev_depth as i64 - height;
|
||||
if gap < state.parameter(Parameter::Lineskiplimit).width() as i64 {
|
||||
label = Parameter::Lineskip;
|
||||
g = state.parameter(Parameter::Lineskip).glue();
|
||||
} else {
|
||||
g.width =
|
||||
i32::try_from(gap).map_err(|_| "native interline glue capacity exceeded")?;
|
||||
}
|
||||
self.push(NativeVerticalNode::NamedGlue(g, label));
|
||||
}
|
||||
self.push(node);
|
||||
self.prev_depth = depth as i32;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn target(spec: PackSpec, natural: i64) -> i64 {
|
||||
match spec {
|
||||
PackSpec::Natural => natural,
|
||||
PackSpec::To(n) => n as i64,
|
||||
PackSpec::Spread(n) => natural + n as i64,
|
||||
}
|
||||
}
|
||||
fn flex(g: Glue, stretch: &mut [i64; 4], shrink: &mut [i64; 4]) {
|
||||
stretch[g.stretch.order as usize] += g.stretch.amount as i64;
|
||||
shrink[g.shrink.order as usize] += g.shrink.amount as i64;
|
||||
}
|
||||
/// TeX's last_badness is a packaging result (overfull = 1_000_000), not the
|
||||
/// 10001 sentinel used by the paragraph break search or a page-break cost.
|
||||
pub(super) fn setting(
|
||||
delta: i64,
|
||||
stretch: [i64; 4],
|
||||
shrink: [i64; 4],
|
||||
nonempty: bool,
|
||||
) -> (GlueSetting, i32, i64) {
|
||||
if delta == 0 {
|
||||
return (GlueSetting::default(), 0, 0);
|
||||
}
|
||||
let totals = if delta > 0 { stretch } else { shrink };
|
||||
let order = (0..4).rev().find(|o| totals[*o] != 0).unwrap_or(0);
|
||||
let total = totals[order];
|
||||
let overfull = delta < 0 && order == 0 && -delta > total && nonempty;
|
||||
let badness = if !nonempty || order > 0 {
|
||||
0
|
||||
} else if overfull {
|
||||
1_000_000
|
||||
} else {
|
||||
paragraph::tex_badness(delta.abs(), total)
|
||||
};
|
||||
let numerator = if overfull { total } else { delta.abs() };
|
||||
let set = GlueSetting {
|
||||
sign: if total == 0 {
|
||||
0
|
||||
} else if delta > 0 {
|
||||
1
|
||||
} else {
|
||||
-1
|
||||
},
|
||||
order: order as u8,
|
||||
numerator,
|
||||
denominator: total,
|
||||
};
|
||||
(set, badness, if overfull { -delta - total } else { 0 })
|
||||
}
|
||||
impl GlueSetting {
|
||||
pub(super) fn adjustment(self, g: Glue, cumulative: &mut i128, previous: &mut i64) -> i64 {
|
||||
let f = if self.sign > 0 { g.stretch } else { g.shrink };
|
||||
if self.sign == 0 || f.order != self.order || self.denominator == 0 {
|
||||
return 0;
|
||||
}
|
||||
*cumulative += f.amount as i128;
|
||||
let n = *cumulative * self.numerator as i128 * self.sign as i128;
|
||||
let d = self.denominator as i128;
|
||||
// web2c rounds signed cumulative glue away from zero at half a sp.
|
||||
// TeX's vet_glue clamps the cumulative adjustment before shipping it.
|
||||
let now = (n.signum() * d.signum() * ((n.abs() + d.abs() / 2) / d.abs()))
|
||||
.clamp(-1_000_000_000, 1_000_000_000) as i64;
|
||||
let result = now - *previous;
|
||||
*previous = now;
|
||||
result
|
||||
}
|
||||
}
|
||||
pub(super) fn vpack(
|
||||
nodes: Vec<NativeVerticalNode>,
|
||||
spec: PackSpec,
|
||||
max_depth: i32,
|
||||
kind: BoxKind,
|
||||
span: Span,
|
||||
) -> Result<NativeBox, String> {
|
||||
let (mut height, mut depth, mut width) = (0, 0, 0);
|
||||
let (mut stretch, mut shrink) = ([0; 4], [0; 4]);
|
||||
for node in &nodes {
|
||||
if let Some((w, h, d)) = node.dimensions() {
|
||||
height += depth + h;
|
||||
depth = d;
|
||||
width = width.max(w);
|
||||
} else {
|
||||
match node {
|
||||
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
|
||||
height += depth + g.width as i64;
|
||||
depth = 0;
|
||||
flex(*g, &mut stretch, &mut shrink);
|
||||
}
|
||||
NativeVerticalNode::Kern(n) | NativeVerticalNode::ImplicitKern(n) => {
|
||||
height += depth + *n as i64;
|
||||
depth = 0;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
if depth > max_depth as i64 {
|
||||
height += depth - max_depth as i64;
|
||||
depth = max_depth as i64;
|
||||
}
|
||||
let desired = target(spec, height);
|
||||
let (set, badness, excess) = setting(desired - height, stretch, shrink, !nodes.is_empty());
|
||||
let diagnostic_eligible = desired != height && set.order == 0 && !nodes.is_empty();
|
||||
height = desired;
|
||||
if kind == BoxKind::Vtop {
|
||||
let first = nodes
|
||||
.first()
|
||||
.and_then(NativeVerticalNode::dimensions)
|
||||
.map_or(0, |d| d.1);
|
||||
depth += height - first;
|
||||
height = first;
|
||||
}
|
||||
dimensions_fit(width, height, depth)?;
|
||||
Ok(NativeBox {
|
||||
kind,
|
||||
display: false,
|
||||
width,
|
||||
height,
|
||||
depth,
|
||||
shift: 0,
|
||||
content: BoxContent::Vertical(nodes),
|
||||
diagnostic: Vec::new(),
|
||||
setting: set,
|
||||
badness,
|
||||
diagnostic_eligible,
|
||||
excess,
|
||||
span,
|
||||
})
|
||||
}
|
||||
pub(super) fn dimensions_fit(w: i64, h: i64, d: i64) -> Result<(), String> {
|
||||
if [w, h, d].iter().any(|n| n.abs() > 1073741823) {
|
||||
Err("native box dimension capacity exceeded".into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub(super) fn hpack(
|
||||
mut nodes: Vec<TextNode>,
|
||||
spec: PackSpec,
|
||||
span: Span,
|
||||
state: &State,
|
||||
) -> Result<NativeBox, String> {
|
||||
let (mut width, mut height, mut depth) = (0, 0, 0);
|
||||
let (mut stretch, mut shrink) = ([0; 4], [0; 4]);
|
||||
for node in &nodes {
|
||||
match node {
|
||||
TextNode::Glyph { glyph, .. } | TextNode::Box(glyph) | TextNode::MathChar(glyph) => {
|
||||
width += glyph.width;
|
||||
let shift = glyph.native_box.as_ref().map_or(0, |b| b.shift);
|
||||
height = height.max(glyph.height - shift);
|
||||
depth = depth.max(glyph.depth + shift);
|
||||
}
|
||||
TextNode::Rule(r) => {
|
||||
let (w, h, d) = r.dimensions();
|
||||
width += w;
|
||||
height = height.max(h);
|
||||
depth = depth.max(d);
|
||||
}
|
||||
TextNode::Output(node) => {
|
||||
if let Some((w, h, d)) = node.dimensions() {
|
||||
width += w;
|
||||
height = height.max(h);
|
||||
depth = depth.max(d);
|
||||
}
|
||||
}
|
||||
TextNode::Indent(w)
|
||||
| TextNode::MathBoundary { width: w, .. }
|
||||
| TextNode::Kern { width: w, .. } => width += *w as i64,
|
||||
TextNode::Glue(g) | TextNode::NamedGlue(g, _) => {
|
||||
width += g.width as i64;
|
||||
flex(*g, &mut stretch, &mut shrink);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let desired = target(spec, width);
|
||||
let (set, badness, excess) = setting(desired - width, stretch, shrink, !nodes.is_empty());
|
||||
let diagnostic_eligible = desired != width && set.order == 0 && !nodes.is_empty();
|
||||
dimensions_fit(desired, height, depth)?;
|
||||
if excess > i64::from(state.parameter(Parameter::Hfuzz).width())
|
||||
&& state.parameter(Parameter::Overfullrule).width() > 0
|
||||
&& badness == 1_000_000
|
||||
{
|
||||
nodes.push(TextNode::Rule(NativeRule {
|
||||
span,
|
||||
origin: None,
|
||||
derivation: None,
|
||||
width: Some(state.parameter(Parameter::Overfullrule).width()),
|
||||
height: None,
|
||||
depth: None,
|
||||
}));
|
||||
}
|
||||
let diagnostic = super::diagnostics::text_nodes(&nodes);
|
||||
Ok(NativeBox {
|
||||
kind: BoxKind::Horizontal,
|
||||
display: false,
|
||||
width: desired,
|
||||
height,
|
||||
depth,
|
||||
shift: 0,
|
||||
content: BoxContent::Horizontal(nodes),
|
||||
diagnostic,
|
||||
setting: set,
|
||||
badness,
|
||||
diagnostic_eligible,
|
||||
excess,
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
||||
/// Flatten already packed native geometry for the common PDF/SVG display list.
|
||||
/// Internal coordinates remain TeX sp until this single export conversion.
|
||||
impl NativeBox {
|
||||
pub(crate) fn paint_display(
|
||||
&self,
|
||||
x: i64,
|
||||
baseline: i64,
|
||||
out: &mut Vec<crate::render::DisplayItem>,
|
||||
) {
|
||||
self.paint_at(0, self.shift, x, baseline, out);
|
||||
}
|
||||
fn paint_at(
|
||||
&self,
|
||||
x: i64,
|
||||
y: i64,
|
||||
ox: i64,
|
||||
oy: i64,
|
||||
out: &mut Vec<crate::render::DisplayItem>,
|
||||
) {
|
||||
self.walk(x, y, &mut |x, y, node| {
|
||||
let BoxPaint::Box(b) = node else { return };
|
||||
if let Some(g) = &b.math_geometry {
|
||||
super::math::paint_geometry(g, x, y, ox, oy, out);
|
||||
} else if let Some(c) = b.character {
|
||||
out.push(crate::render::DisplayItem::Text {
|
||||
x: ox + bp(x),
|
||||
y: oy + bp(y),
|
||||
size: bp(b.size.unwrap_or(655360)),
|
||||
text: c.to_string(),
|
||||
font: crate::render::FontFace::Roman,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
/// Walk original packed lists in TeX sp, before the single display export.
|
||||
pub fn geometry(&self) -> Result<crate::math_engine_tex::MathGeometry, String> {
|
||||
use crate::math_engine_tex::MathGeometry;
|
||||
let mut g = MathGeometry {
|
||||
metrics: crate::math::MathBox {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth: self.depth,
|
||||
},
|
||||
items: Vec::new(),
|
||||
};
|
||||
self.visit_geometry(|item| {
|
||||
if let GeometryItem::Paint(paint) = item {
|
||||
g.items.push(paint);
|
||||
}
|
||||
})?;
|
||||
Ok(g)
|
||||
}
|
||||
|
||||
pub(super) fn visit_geometry(&self, mut emit: impl FnMut(GeometryItem)) -> Result<(), String> {
|
||||
use crate::math_engine_tex::PositionedItem;
|
||||
let mut unsupported = false;
|
||||
self.walk(0, 0, &mut |x, y, node| {
|
||||
let b = match node {
|
||||
BoxPaint::Box(b) => b,
|
||||
BoxPaint::PdfColor => {
|
||||
emit(GeometryItem::PdfColor);
|
||||
return;
|
||||
}
|
||||
BoxPaint::PdfGraphics => {
|
||||
emit(GeometryItem::PdfGraphics { x, y });
|
||||
return;
|
||||
}
|
||||
};
|
||||
if let Some(child) = &b.math_geometry {
|
||||
for p in &child.items {
|
||||
emit(GeometryItem::Paint(PositionedItem {
|
||||
x: x + p.x,
|
||||
y: y + p.y,
|
||||
item: p.item.clone(),
|
||||
source: p.source.clone(),
|
||||
}));
|
||||
}
|
||||
} else if b.character.is_some() {
|
||||
unsupported = true;
|
||||
}
|
||||
});
|
||||
if unsupported {
|
||||
Err("native math box requires actual native glyph geometry".into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn pdf_graphics_positions(&self) -> Vec<[i64; 2]> {
|
||||
let mut all = Vec::new();
|
||||
self.walk(0, 0, &mut |x, y, node| {
|
||||
if matches!(node, BoxPaint::PdfGraphics) {
|
||||
all.push([x, y]);
|
||||
}
|
||||
});
|
||||
all
|
||||
}
|
||||
|
||||
fn walk(&self, x: i64, y: i64, emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>)) {
|
||||
if self.kind.is_unset() {
|
||||
return;
|
||||
}
|
||||
match &self.content {
|
||||
BoxContent::Horizontal(_) => walk_row(&self.packed_horizontal().unwrap(), x, y, emit),
|
||||
BoxContent::Vertical(nodes) => {
|
||||
let mut cursor = y - self.height;
|
||||
let (mut cumulative, mut previous) = (0, 0);
|
||||
for node in nodes {
|
||||
match node {
|
||||
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => {
|
||||
if b.kind.is_unset() {
|
||||
continue;
|
||||
}
|
||||
cursor += b.height;
|
||||
b.walk(x + b.shift, cursor, emit);
|
||||
cursor += b.depth;
|
||||
}
|
||||
NativeVerticalNode::Glue(g) | NativeVerticalNode::NamedGlue(g, _) => {
|
||||
cursor += i64::from(g.width)
|
||||
+ self.setting.adjustment(*g, &mut cumulative, &mut previous)
|
||||
}
|
||||
NativeVerticalNode::Kern(n) | NativeVerticalNode::ImplicitKern(n) => {
|
||||
cursor += i64::from(*n)
|
||||
}
|
||||
NativeVerticalNode::Rule(r) => {
|
||||
let b = r.horizontal_item(self.width, 0, 0);
|
||||
cursor += b.height;
|
||||
emit(x, cursor, BoxPaint::Box(&b));
|
||||
cursor += b.depth;
|
||||
}
|
||||
NativeVerticalNode::Output(node)
|
||||
if matches!(node.operation, OutputOperation::PdfColor { .. }) =>
|
||||
{
|
||||
emit(x, cursor, BoxPaint::PdfColor);
|
||||
}
|
||||
NativeVerticalNode::Output(node) if node.is_pdf_graphics() => {
|
||||
emit(x, cursor, BoxPaint::PdfGraphics);
|
||||
}
|
||||
NativeVerticalNode::Output(node) => {
|
||||
if let Some(image) = node.image_box() {
|
||||
cursor += image.height;
|
||||
emit(x, cursor, BoxPaint::Box(&image));
|
||||
cursor += image.depth;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) fn bp(n: i64) -> i64 {
|
||||
if n < 0 {
|
||||
-((-n * 800 + 401) / 803)
|
||||
} else {
|
||||
(n * 800 + 401) / 803
|
||||
}
|
||||
}
|
||||
fn walk_row(
|
||||
items: &[PackedHorizontal],
|
||||
mut x: i64,
|
||||
y: i64,
|
||||
emit: &mut dyn FnMut(i64, i64, BoxPaint<'_>),
|
||||
) {
|
||||
for item in items {
|
||||
match item {
|
||||
PackedHorizontal::Item(HorizontalItem::Box(b)) => {
|
||||
if let Some(child) = &b.native_box {
|
||||
child.walk(x, y + child.shift, emit);
|
||||
} else {
|
||||
emit(x, y, BoxPaint::Box(b));
|
||||
}
|
||||
x += b.width;
|
||||
}
|
||||
PackedHorizontal::Item(HorizontalItem::Glue { width, .. }) => x += width,
|
||||
PackedHorizontal::PdfColor => emit(x, y, BoxPaint::PdfColor),
|
||||
PackedHorizontal::PdfGraphics => emit(x, y, BoxPaint::PdfGraphics),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(super) fn box_paragraph(mut b: NativeBox) -> Paragraph {
|
||||
let (width, height, depth, shift) = (b.width, b.height, b.depth, b.shift);
|
||||
b.shift = 0;
|
||||
Paragraph {
|
||||
lines: vec![vec![HorizontalItem::Box(b.horizontal_item())]],
|
||||
width,
|
||||
height: height + depth,
|
||||
depth,
|
||||
font: ParagraphFont::Roman,
|
||||
font_size: 655360,
|
||||
left_skip: 0,
|
||||
line_indents: vec![shift],
|
||||
line_baselines: vec![height],
|
||||
line_scales: Vec::new(),
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user