Parenthesise glue expressions and keep register components

This commit is contained in:
gloamxun committed 2026-10-26 03:16:20 +08:00
1 parent fe1a001e62
commit 7dbcea2319
4 files changed
+62 -10

No files matched your search

+58 -6
View File
@@ -2838,13 +2838,13 @@ fn dimension_expression(text: &str, registers: &[(String, i64)]) -> Option<(i64,
/// `\multiply` / `\divide` and `\the` printing.
fn format_glue(glue: [i64; 3]) -> String {
let mut text = format_dimension(glue[0]);
if glue[1] > 0 {
if glue[1] != 0 {
text.push_str(" plus ");
text.push_str(&format_dimension(glue[1]));
text.push_str(&format_dimension(glue[1].abs()));
}
if glue[2] > 0 {
if glue[2] != 0 {
text.push_str(" minus ");
text.push_str(&format_dimension(glue[2]));
text.push_str(&format_dimension(glue[2].abs()));
}
text
}
@@ -2911,8 +2911,40 @@ impl<'a> GlueParser<'a> {
Some(value)
}
fn factor(&mut self) -> Option<[i64; 3]> {
self.skip_space();
if self.text[self.position..].starts_with('(') {
self.position += 1;
let value = self.expression()?;
self.skip_space();
if !self.text[self.position..].starts_with(')') {
return None;
}
self.position += 1;
return Some(value);
}
let rest = &self.text[self.position..];
if rest.starts_with('\\') {
if let Some((command, length)) = control_sequence(rest) {
let name = command.trim_start_matches('\\');
if let Some((_, glue)) = self.registers.iter().find(|(known, _)| known == name) {
self.position += length;
return Some(*glue);
}
}
}
let mut parser = DimensionParser {
text: rest,
position: 0,
registers: &[],
};
let value = parser.expression()?;
self.position += parser.position;
Some([value, 0, 0])
}
fn term(&mut self) -> Option<[i64; 3]> {
let mut glue = [self.dimension()?, 0, 0];
let mut glue = self.factor()?;
loop {
self.skip_space();
let rest = &self.text[self.position..];
@@ -10146,7 +10178,27 @@ a & b
.find(|(name, _)| name == "gap")
.expect("gap register");
assert_eq!(*glue, [131_072, 65_536, -65_536]);
assert!(_body.contains("Gap 3.0pt."), "{_body:?}");
assert!(
_body.contains("Gap 3.0pt plus 1.0pt minus 1.0pt."),
"{_body:?}"
);
}
#[test]
fn glue_expressions_parenthesise_components() {
let (body, geometry) = geometry_and_body(
"\\newskip\\gap\n\\gap=\\glueexpr (2pt plus 1pt minus 1pt) + 3pt\\relax\n\nGap \\the\\gap.",
);
let (_, glue) = geometry
.skip_registers
.iter()
.find(|(name, _)| name == "gap")
.expect("gap register");
assert_eq!(*glue, [327_680, 65_536, -65_536]);
assert!(
body.contains("Gap 5.0pt plus 1.0pt minus 1.0pt."),
"{body:?}"
);
}
#[test]