Apply e-TeX line penalty arrays to native pagination
This commit is contained in:
1 parent
09cd19594c
commit
c094d9a81d
12 files changed
+243
-24
No files matched your search
@@ -2356,6 +2356,7 @@ impl Executor {
|
||||
p @ (Primitive::Wd | Primitive::Ht | Primitive::Dp | Primitive::Prevdepth),
|
||||
) => self.dimension_value(p, files),
|
||||
Meaning::Primitive(Primitive::Parshape) => Ok(self.read_paragraph_shape().len() as i32),
|
||||
Meaning::Primitive(Primitive::LinePenalties(kind)) => self.line_penalty_value(kind, files),
|
||||
Meaning::Primitive(Primitive::Prevgraf) => {
|
||||
if self.writing {
|
||||
Ok(0)
|
||||
@@ -3336,6 +3337,7 @@ impl Executor {
|
||||
}
|
||||
Primitive::Etexversion => "eTeXversion".into(),
|
||||
Primitive::Etexrevision => "eTeXrevision".into(),
|
||||
Primitive::LinePenalties(kind) => kind.name().into(),
|
||||
Primitive::Uchar => "Uchar".into(),
|
||||
Primitive::Ucharcat => "Ucharcat".into(),
|
||||
Primitive::Umathcode => "Umathcode".into(),
|
||||
@@ -4514,6 +4516,7 @@ impl Executor {
|
||||
self.scan_math_code_assignment(p, global, files)?;
|
||||
}
|
||||
Some(Parshape) => self.scan_paragraph_shape(global, files)?,
|
||||
Some(LinePenalties(kind)) => self.scan_line_penalties(kind, global, files)?,
|
||||
Some(Setbox) => {
|
||||
if !allow_setbox {
|
||||
return self.fail("improper native setbox in an assignment prefix");
|
||||
|
||||
@@ -432,6 +432,7 @@ impl Executor {
|
||||
| Spacefactor
|
||||
| Setbox
|
||||
| Parshape
|
||||
| LinePenalties(_)
|
||||
| Wd
|
||||
| Ht
|
||||
| Dp
|
||||
|
||||
@@ -885,7 +885,7 @@ impl Executor {
|
||||
| Day | Month | Year | BeginGroup | EndGroup | Relax | Message | Errmessage
|
||||
| Endinput | Showbox | Show | Showthe | Showlists | Showifs | Showgroups
|
||||
| Discretionary | DiscretionaryHyphen
|
||||
| Parshape | Partokenname => {
|
||||
| Parshape | LinePenalties(_) | Partokenname => {
|
||||
return Ok(false);
|
||||
}
|
||||
_ if self.execute_output_extension(*p, token, files)? => {}
|
||||
|
||||
@@ -244,6 +244,56 @@ impl Executor {
|
||||
self.assign_paragraph_shape(shape, global, files)
|
||||
}
|
||||
|
||||
pub(super) fn read_line_penalties(&mut self, kind: LinePenaltyKind) -> &[i32] {
|
||||
self.reads.insert(Dependency::LinePenalties(kind));
|
||||
self.state.line_penalties(kind)
|
||||
}
|
||||
|
||||
fn assign_line_penalties(
|
||||
&mut self,
|
||||
kind: LinePenaltyKind,
|
||||
values: Vec<i32>,
|
||||
global: bool,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
let equivalent = values.is_empty() && self.state.line_penalties(kind).is_empty();
|
||||
self.assign_equivalence(Key::LinePenalties(kind), global, equivalent, files, |state| {
|
||||
state.assign_line_penalties(kind, values, global);
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn scan_line_penalties(
|
||||
&mut self,
|
||||
kind: LinePenaltyKind,
|
||||
global: bool,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
self.optional_equals(files)?;
|
||||
let count = self.integer(files)?.max(0) as usize;
|
||||
if count > self.limits.tokens {
|
||||
return self.fail("native line penalty array capacity exceeded");
|
||||
}
|
||||
let mut values = Vec::new();
|
||||
for _ in 0..count {
|
||||
values.push(self.integer(files)?);
|
||||
}
|
||||
self.assign_line_penalties(kind, values, global, files)
|
||||
}
|
||||
|
||||
pub(super) fn line_penalty_value(
|
||||
&mut self,
|
||||
kind: LinePenaltyKind,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<i32, Error> {
|
||||
let index = self.integer(files)?;
|
||||
let values = self.read_line_penalties(kind);
|
||||
Ok(match index {
|
||||
..0 => 0,
|
||||
0 => values.len() as i32,
|
||||
_ => super::super::paragraph::line_penalty_at(values, index as usize).unwrap_or(0),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn paragraph_shape_dimension(
|
||||
&mut self,
|
||||
primitive: Primitive,
|
||||
@@ -493,6 +543,11 @@ impl Executor {
|
||||
self.reads.insert(Dependency::Parameter(*p));
|
||||
}
|
||||
self.reads.insert(Dependency::ParagraphShape);
|
||||
self.reads.extend([
|
||||
Dependency::LinePenalties(LinePenaltyKind::Interline),
|
||||
Dependency::LinePenalties(LinePenaltyKind::Club),
|
||||
Dependency::LinePenalties(LinePenaltyKind::for_widow_parameter(final_widow)),
|
||||
]);
|
||||
self.reads.insert(Dependency::Hyphenation);
|
||||
for n in &b.nodes {
|
||||
let text = match n {
|
||||
@@ -636,6 +691,9 @@ impl Executor {
|
||||
if !self.read_paragraph_shape().is_empty() {
|
||||
self.assign_paragraph_shape(Vec::new(), false, files)?;
|
||||
}
|
||||
if !self.read_line_penalties(LinePenaltyKind::Interline).is_empty() {
|
||||
self.assign_line_penalties(LinePenaltyKind::Interline, Vec::new(), false, files)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub(super) fn horizontal_command(
|
||||
|
||||
@@ -238,6 +238,7 @@ impl Executor {
|
||||
| Primitive::Gluestretchorder
|
||||
| Primitive::Glueshrinkorder
|
||||
| Primitive::Parshape
|
||||
| Primitive::LinePenalties(_)
|
||||
| Primitive::Prevgraf
|
||||
| Primitive::Spacefactor
|
||||
| Primitive::Hyphenchar
|
||||
|
||||
@@ -199,6 +199,15 @@ impl Executor {
|
||||
"parshape".into(),
|
||||
self.read_paragraph_shape().len().to_string(),
|
||||
),
|
||||
Key::LinePenalties(kind) => {
|
||||
let values = self.read_line_penalties(*kind);
|
||||
let text = match values {
|
||||
[] => "0".to_owned(),
|
||||
[first] => format!("1 {first}"),
|
||||
[first, ..] => format!("{} {first}{escape}ETC.", values.len()),
|
||||
};
|
||||
(kind.name().into(), text)
|
||||
}
|
||||
Key::Font => {
|
||||
self.reads.insert(Dependency::FontResources);
|
||||
let font = self.font_token_name(self.state.text_font());
|
||||
|
||||
@@ -16,9 +16,9 @@ use std::{
|
||||
sync::{Arc, Mutex, OnceLock, Weak},
|
||||
};
|
||||
|
||||
const SCHEMA: u32 = 99;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 65] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, SCHEMA,
|
||||
const SCHEMA: u32 = 100;
|
||||
const COMPATIBLE_SCHEMAS: [u32; 66] = [
|
||||
27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, SCHEMA,
|
||||
];
|
||||
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
@@ -214,6 +214,9 @@ impl FormatImage {
|
||||
if image.schema < 99 {
|
||||
image.state.restore_legacy_etex_parameter(Parameter::Savinghyphcodes);
|
||||
}
|
||||
if image.schema < 100 {
|
||||
image.state.install_line_penalty_primitives();
|
||||
}
|
||||
let pdf_objects = image.state.pdf_objects.as_deref();
|
||||
let mut numbered_images = 0;
|
||||
for (index, saved) in image.pdf_images.iter().enumerate() {
|
||||
|
||||
@@ -95,7 +95,7 @@ pub use rule::NativeRule;
|
||||
pub use session::{
|
||||
NativeSession, RevisionError, RevisionExecution, RevisionFailure, RevisionJob, SourceVersion,
|
||||
};
|
||||
pub use state::{CommandName, Macro, Meaning, Primitive, Profile, Replacement, State};
|
||||
pub use state::{CommandName, LinePenaltyKind, Macro, Meaning, Primitive, Profile, Replacement, State};
|
||||
|
||||
#[derive(
|
||||
Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
|
||||
@@ -487,6 +487,7 @@ pub enum Dependency {
|
||||
FontResources,
|
||||
ParagraphMode,
|
||||
ParagraphShape,
|
||||
LinePenalties(LinePenaltyKind),
|
||||
PreviousParagraphLines,
|
||||
Hyphenation,
|
||||
BoxRegister(u16),
|
||||
|
||||
@@ -19,6 +19,11 @@ pub(super) struct ParagraphShapeLine {
|
||||
pub width: i32,
|
||||
}
|
||||
|
||||
/// A one-based index, extended by the final element of a nonempty array.
|
||||
pub(super) fn line_penalty_at(values: &[i32], index: usize) -> Option<i32> {
|
||||
values.get(index.min(values.len()).wrapping_sub(1)).copied()
|
||||
}
|
||||
|
||||
pub(super) fn shaped_line(shape: &[ParagraphShapeLine], line: usize) -> Option<ParagraphShapeLine> {
|
||||
shape.get(line).or_else(|| shape.last()).copied()
|
||||
}
|
||||
@@ -1524,6 +1529,7 @@ pub(super) fn finish(
|
||||
let mut pre_display_size = -1073741823;
|
||||
let directions = super::direction::has_directions(&b.nodes);
|
||||
let mut direction_stack = Vec::new();
|
||||
let widow_kind = LinePenaltyKind::for_widow_parameter(final_widow);
|
||||
for (i, (point, bad)) in path.iter().enumerate() {
|
||||
let mut material = line_material(
|
||||
&b.nodes,
|
||||
@@ -1580,25 +1586,23 @@ pub(super) fn finish(
|
||||
let disc_break = i + 1 < path.len() && point.hyphen;
|
||||
discretionary_breaks.push(disc_break);
|
||||
if i + 1 < path.len() {
|
||||
// TeX post_line_break: all applicable terms add, including both
|
||||
// club and widow for a two-line paragraph. Never penalize the end.
|
||||
let get = |p| parameters[&p].width() as i64;
|
||||
let penalty = get(Parameter::Interlinepenalty)
|
||||
+ if i == 0 {
|
||||
get(Parameter::Clubpenalty)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
+ if i + 2 == path.len() {
|
||||
get(final_widow)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
+ if disc_break {
|
||||
get(Parameter::Brokenpenalty)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
// The base array uses prevgraf, including lines before a display.
|
||||
// Club counts from this segment's start and widow from its end.
|
||||
let from_start = i + 1;
|
||||
let from_end = path.len() - from_start;
|
||||
let term = |kind, index, fallback, applies| {
|
||||
line_penalty_at(state.line_penalties(kind), index)
|
||||
.map(i64::from)
|
||||
.unwrap_or_else(|| if applies { i64::from(parameters[&fallback].width()) } else { 0 })
|
||||
};
|
||||
let penalty = term(
|
||||
LinePenaltyKind::Interline,
|
||||
b.previous_lines + from_start,
|
||||
Parameter::Interlinepenalty,
|
||||
true,
|
||||
) + term(LinePenaltyKind::Club, from_start, Parameter::Clubpenalty, from_start == 1)
|
||||
+ term(widow_kind, from_end, final_widow, from_end == 1)
|
||||
+ if disc_break { i64::from(parameters[&Parameter::Brokenpenalty].width()) } else { 0 };
|
||||
interline_penalties
|
||||
.push(i32::try_from(penalty).map_err(|_| "native interline penalty sum overflow")?);
|
||||
}
|
||||
|
||||
@@ -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()),
|
||||
|
||||
@@ -128,6 +128,43 @@ fn last_line_fit_reuses_the_selected_fill_only_for_the_same_parameter() {
|
||||
assert_eq!((work.solved_passes, work.reused_passes), (2, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn etex_penalty_arrays_index_actual_lines_across_display_and_reset_at_par() {
|
||||
let execution = run(
|
||||
r"\catcode36=3
|
||||
\font\symbols=cmsy10 \textfont2=\symbols \scriptfont2=\symbols \scriptscriptfont2=\symbols
|
||||
\font\extensions=cmex10 \textfont3=\extensions \scriptfont3=\extensions \scriptscriptfont3=\extensions
|
||||
\pretolerance=10000 \tolerance=10000
|
||||
\interlinepenalty=7
|
||||
\clubpenalties=2 3 5
|
||||
\widowpenalties=3 31 41 51
|
||||
\displaywidowpenalties=3 80 90 100
|
||||
\let\w=\widowpenalties
|
||||
{\w=2 92 93 \message{LOCAL:\the\w0/\the\w9}}
|
||||
\message{VALUES:\the\w-1/\the\w0/\the\w2/\the\numexpr\w20+1\relax}
|
||||
\def\br{\hfil\penalty-10000\relax}
|
||||
\setbox0=\vbox{\interlinepenalties=10 1 2 3 4 5 6 7 8 9 10
|
||||
\noindent AA\br BB\br CC$$\hbox{DD}$$EE\br FF\br GG\par
|
||||
\message{RESET:\the\interlinepenalties0/\the\clubpenalties0/\the\w0}
|
||||
\noindent AA\br BB\par}",
|
||||
);
|
||||
let built = paragraphs(&execution);
|
||||
assert_eq!(built.len(), 3);
|
||||
assert_eq!(built[0].interline_penalties, [94, 87]);
|
||||
assert_eq!(built[1].previous_lines, 6);
|
||||
assert_eq!(built[1].interline_penalties, [51, 44]);
|
||||
assert_eq!(built[2].interline_penalties, [41]);
|
||||
let messages: Vec<_> = execution
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::Message { text, .. } => Some(text.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(messages, ["LOCAL:2/93", "VALUES:0/3/41/52", "RESET:0/2/3"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_parameters_have_scopes_and_par_end_timing() {
|
||||
let e = run(
|
||||
|
||||
Reference in new issue
Block a user