use crate::glue::{Glue, Order};
pub type Scaled = i64;
pub const NULL_FLAG: Scaled = -0x4000_0000;
pub const MAX_DIMEN: Scaled = 0x3FFF_FFFF;
pub const INF_BAD: i64 = 10000;
pub const INF_PENALTY: i64 = INF_BAD;
pub const EJECT_PENALTY: i64 = -INF_PENALTY;
pub const AWFUL_BAD: i64 = 0x3FFF_FFFF;
pub const DEPLORABLE: i64 = 100_000;
pub const IGNORE_DEPTH: Scaled = -65_536_000;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum GlueSign {
#[default]
Normal,
Stretching,
Shrinking,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum LeaderKind {
#[default]
Normal,
Aligned,
Centred,
Expanded,
}
impl LeaderKind {
pub fn is_leaders(self) -> bool {
self != LeaderKind::Normal
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum GlueSource {
#[default]
Normal,
LineSkip,
BaselineSkip,
ParSkip,
LeftSkip,
RightSkip,
TopSkip,
SplitTopSkip,
ParFillSkip,
}
#[derive(Clone, Debug, Default)]
pub struct BoxNode {
pub width: Scaled,
pub depth: Scaled,
pub height: Scaled,
pub shift_amount: Scaled,
pub list: Vec<Node>,
pub glue_set: f64,
pub glue_sign: GlueSign,
pub glue_order: Order,
pub vertical: bool,
}
impl BoxNode {
pub fn null() -> BoxNode {
BoxNode::default()
}
pub fn height_plus_depth(&self) -> Scaled {
self.height + self.depth
}
}
#[derive(Clone, Copy, Debug)]
pub struct RuleNode {
pub width: Scaled,
pub depth: Scaled,
pub height: Scaled,
}
impl RuleNode {
pub fn running() -> RuleNode {
RuleNode {
width: NULL_FLAG,
depth: NULL_FLAG,
height: NULL_FLAG,
}
}
pub fn is_running(d: Scaled) -> bool {
d == NULL_FLAG
}
}
#[derive(Clone, Debug)]
pub struct InsNode {
pub number: u8,
pub height: Scaled,
pub depth: Scaled,
pub float_cost: i64,
pub split_top_skip: Glue,
pub list: Vec<Node>,
}
#[derive(Clone, Debug, Default)]
pub struct GlueNode {
pub spec: Glue,
pub kind: LeaderKind,
pub source: GlueSource,
pub leader: Option<Box<Node>>,
}
impl GlueNode {
pub fn new(spec: Glue) -> GlueNode {
GlueNode {
spec,
..GlueNode::default()
}
}
pub fn param(spec: Glue, source: GlueSource) -> GlueNode {
GlueNode {
spec,
source,
..GlueNode::default()
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct CharNode {
pub font: usize,
pub character: char,
pub width: Scaled,
pub height: Scaled,
pub depth: Scaled,
}
#[derive(Clone, Debug, Default)]
pub struct DiscNode {
pub pre_break: Vec<Node>,
pub post_break: Vec<Node>,
pub replace_count: usize,
}
#[derive(Clone, Debug)]
pub enum Node {
Char(CharNode),
Box(BoxNode),
Rule(RuleNode),
Ins(InsNode),
Mark(String),
Adjust(Vec<Node>),
Ligature(CharNode),
Disc(DiscNode),
Whatsit(String),
Math(Scaled),
Glue(GlueNode),
Kern {
width: Scaled,
explicit: bool,
},
Penalty(i64),
}
impl Node {
pub fn type_code(&self) -> u8 {
match self {
Node::Box(b) if b.vertical => 1,
Node::Box(_) => 0,
Node::Rule(_) => 2,
Node::Ins(_) => 3,
Node::Mark(_) => 4,
Node::Adjust(_) => 5,
Node::Ligature(_) => 6,
Node::Disc(_) => 7,
Node::Whatsit(_) => 8,
Node::Math(_) => 9,
Node::Glue(_) => 10,
Node::Kern { .. } => 11,
Node::Penalty(_) => 12,
Node::Char(_) => 255,
}
}
pub fn is_char(&self) -> bool {
matches!(self, Node::Char(_))
}
pub fn precedes_break(&self) -> bool {
self.type_code() < 9
}
pub fn non_discardable(&self) -> bool {
self.precedes_break()
}
pub fn shift_amount(&self) -> Scaled {
match self {
Node::Box(b) => b.shift_amount,
_ => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn type_codes_are_tex_webs_own() {
assert_eq!(Node::Box(BoxNode::null()).type_code(), 0);
let vbox = BoxNode {
vertical: true,
..BoxNode::null()
};
assert_eq!(Node::Box(vbox).type_code(), 1);
assert_eq!(Node::Rule(RuleNode::running()).type_code(), 2);
assert_eq!(Node::Mark(String::new()).type_code(), 4);
assert_eq!(Node::Math(0).type_code(), 9);
assert_eq!(Node::Glue(GlueNode::default()).type_code(), 10);
assert_eq!(
Node::Kern {
width: 0,
explicit: true
}
.type_code(),
11
);
assert_eq!(Node::Penalty(0).type_code(), 12);
}
#[test]
fn glue_breaks_only_after_something_that_stays() {
assert!(Node::Box(BoxNode::null()).precedes_break());
assert!(Node::Rule(RuleNode::running()).precedes_break());
assert!(Node::Disc(DiscNode::default()).precedes_break());
assert!(Node::Whatsit(String::new()).precedes_break());
assert!(!Node::Math(0).precedes_break());
assert!(!Node::Glue(GlueNode::default()).precedes_break());
assert!(!Node::Penalty(0).precedes_break());
}
#[test]
fn only_a_box_carries_a_shift() {
let shifted = BoxNode {
shift_amount: 7,
..BoxNode::null()
};
assert_eq!(Node::Box(shifted).shift_amount(), 7);
assert_eq!(Node::Rule(RuleNode::running()).shift_amount(), 0);
}
}