use crate::glue::{Glue, Order};
pub mod hooks;
pub mod show;
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,
AboveDisplaySkip,
BelowDisplaySkip,
AboveDisplayShortSkip,
BelowDisplayShortSkip,
TabSkip,
SpaceSkip,
XSpaceSkip,
ThinMuSkip,
MedMuSkip,
ThickMuSkip,
CondMath,
MuGlue,
}
impl GlueSource {
pub fn param_code(self) -> Option<usize> {
use GlueSource::*;
Some(match self {
LineSkip => 0,
BaselineSkip => 1,
ParSkip => 2,
AboveDisplaySkip => 3,
BelowDisplaySkip => 4,
AboveDisplayShortSkip => 5,
BelowDisplayShortSkip => 6,
LeftSkip => 7,
RightSkip => 8,
TopSkip => 9,
SplitTopSkip => 10,
TabSkip => 11,
SpaceSkip => 12,
XSpaceSkip => 13,
ParFillSkip => 14,
ThinMuSkip => 15,
MedMuSkip => 16,
ThickMuSkip => 17,
Normal | CondMath | MuGlue => return None,
})
}
pub fn of_param(code: usize) -> GlueSource {
use GlueSource::*;
match code {
0 => LineSkip,
1 => BaselineSkip,
2 => ParSkip,
3 => AboveDisplaySkip,
4 => BelowDisplaySkip,
5 => AboveDisplayShortSkip,
6 => BelowDisplayShortSkip,
7 => LeftSkip,
8 => RightSkip,
9 => TopSkip,
10 => SplitTopSkip,
11 => TabSkip,
12 => SpaceSkip,
13 => XSpaceSkip,
14 => ParFillSkip,
15 => ThinMuSkip,
16 => MedMuSkip,
17 => ThickMuSkip,
_ => Normal,
}
}
}
#[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,
pub subtype: u8,
}
pub const BOX_UNKNOWN: u8 = 0;
pub const BOX_LINE: u8 = 1;
pub const BOX_BOX: u8 = 2;
pub const BOX_INDENT: u8 = 3;
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, PartialEq, Eq)]
pub struct Ligature {
pub components: Vec<char>,
pub left_boundary: bool,
pub right_boundary: bool,
}
#[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 struct OutNode {
pub display: String,
pub action: usize,
}
#[derive(Clone, Debug)]
pub enum Node {
Char(CharNode),
Box(BoxNode),
Rule(RuleNode),
Ins(InsNode),
Mark(String),
Adjust(Vec<Node>),
Ligature(CharNode, Ligature),
Disc(DiscNode),
Whatsit(String),
Out(OutNode),
Math(Scaled),
MathOff(Scaled),
Glue(GlueNode),
Kern {
width: Scaled,
explicit: bool,
},
AccKern(Scaled),
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(_) | Node::Out(_) => 8,
Node::Math(_) | Node::MathOff(_) => 9,
Node::Glue(_) => 10,
Node::Kern { .. } | Node::AccKern(_) => 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);
}
}