Apply e-TeX line penalty arrays to native pagination

This commit is contained in:
gloamxun committed 2026-11-28 13:44:30 +08:00
1 parent 09cd19594c
commit c094d9a81d
12 files changed
+243 -24

No files matched your search

@@ -226,6 +226,7 @@ pub enum Primitive {
Parshapelength,
Parshapeindent,
Parshapedimen,
LinePenalties(LinePenaltyKind),
Spacefactor,
Setbox,
Box,
@@ -598,6 +599,38 @@ pub enum Meaning {
UnicodeMathCharGiven(super::MathCode),
}
/// The four indexed e-TeX penalties used between typeset paragraph lines.
#[derive(
Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum LinePenaltyKind {
Interline,
Club,
Widow,
DisplayWidow,
}
impl LinePenaltyKind {
const ALL: [Self; 4] = [Self::Interline, Self::Club, Self::Widow, Self::DisplayWidow];
pub(super) fn name(self) -> &'static str {
match self {
Self::Interline => "interlinepenalties",
Self::Club => "clubpenalties",
Self::Widow => "widowpenalties",
Self::DisplayWidow => "displaywidowpenalties",
}
}
pub(super) fn for_widow_parameter(parameter: Parameter) -> Self {
match parameter {
Parameter::Widowpenalty => Self::Widow,
Parameter::Displaywidowpenalty => Self::DisplayWidow,
_ => unreachable!("paragraph has a widow penalty parameter"),
}
}
}
/// Explicit bootstrap language selection. EtexBootstrap implements the native
/// extended macro/integer language below; it does not claim full e-TeX mode,
/// marks, glue/dimension expression or format-image conformance.
@@ -628,6 +661,7 @@ pub(super) enum Key {
Parameter(Parameter),
Font,
ParagraphShape,
LinePenalties(LinePenaltyKind),
Spacefactor,
Box(u16),
Mathcode(u32),
@@ -651,6 +685,7 @@ pub(super) enum Saved {
Register(Option<RegisterValue>),
Parameter(Option<ParameterValue>),
ParagraphShape(std::sync::Arc<[super::paragraph::ParagraphShapeLine]>),
LinePenalties(Option<std::sync::Arc<[i32]>>),
Font(FontId),
Box(Option<NativeBox>),
Mathcode(Option<super::MathCode>),
@@ -772,6 +807,8 @@ pub struct State {
registers: BTreeMap<Register, RegisterValue>,
parameters: BTreeMap<Parameter, ParameterValue>,
paragraph_shape: std::sync::Arc<[super::paragraph::ParagraphShapeLine]>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
line_penalties: BTreeMap<LinePenaltyKind, std::sync::Arc<[i32]>>,
pub(super) text_font: FontId,
pub(super) fonts: super::FontCatalog,
boxes: BTreeMap<u16, NativeBox>,
@@ -993,6 +1030,7 @@ impl Default for State {
registers: BTreeMap::new(),
parameters: BTreeMap::new(),
paragraph_shape: std::sync::Arc::default(),
line_penalties: BTreeMap::new(),
text_font: FontId::NULL,
fonts: super::FontCatalog::default(),
boxes: BTreeMap::new(),
@@ -1452,6 +1490,24 @@ impl State {
self.parameters.insert(p, value);
}
}
pub(super) fn line_penalties(&self, kind: LinePenaltyKind) -> &[i32] {
self.line_penalties.get(&kind).map_or(&[], |values| values.as_ref())
}
pub(super) fn assign_line_penalties(
&mut self,
kind: LinePenaltyKind,
values: Vec<i32>,
global: bool,
) {
self.save(Key::LinePenalties(kind), global);
if values.is_empty() {
self.line_penalties.remove(&kind);
} else {
self.line_penalties.insert(kind, values.into());
}
}
pub(super) fn paragraph_shape(&self) -> &[super::paragraph::ParagraphShapeLine] {
&self.paragraph_shape
}
@@ -1533,6 +1589,7 @@ impl State {
pub fn with_etex(mut self) -> Self {
use Primitive::*;
self.profile = Profile::EtexBootstrap;
self.install_line_penalty_primitives();
let hook = TokenParameter::Everyeof;
self.commands.insert(
CommandName::Control(hook.name()),
@@ -1798,6 +1855,16 @@ impl State {
}
}
pub(super) fn install_line_penalty_primitives(&mut self) {
if self.profile == Profile::EtexBootstrap {
for kind in LinePenaltyKind::ALL {
self.commands
.entry(CommandName::Control(kind.name().into()))
.or_insert(Meaning::Primitive(Primitive::LinePenalties(kind)));
}
}
}
pub(super) fn restore_legacy_etex_parameter(&mut self, parameter: Parameter) {
if self.profile == Profile::EtexBootstrap {
self.commands
@@ -2353,6 +2420,14 @@ impl State {
}
},
(Key::ParagraphShape, Saved::ParagraphShape(shape)) => self.paragraph_shape = shape,
(Key::LinePenalties(kind), Saved::LinePenalties(values)) => match values {
Some(values) => {
self.line_penalties.insert(kind, values);
}
None => {
self.line_penalties.remove(&kind);
}
},
(Key::Mathcode(c), Saved::Mathcode(v)) => {
if let Some(v) = v {
self.mathcodes.insert(c, v);
@@ -2448,6 +2523,7 @@ impl State {
Key::Register(register) => Saved::Register(self.registers.get(register).copied()),
Key::Parameter(p) => Saved::Parameter(self.parameters.get(p).copied()),
Key::ParagraphShape => Saved::ParagraphShape(self.paragraph_shape.clone()),
Key::LinePenalties(kind) => Saved::LinePenalties(self.line_penalties.get(kind).cloned()),
Key::Font => Saved::Font(self.text_font),
Key::Box(n) => Saved::Box(self.boxes.get(n).cloned()),
Key::Mathcode(c) => Saved::Mathcode(self.mathcodes.get(c).copied()),