Scan TeX math skips and correct math-unit spacing
This commit is contained in:
1 parent
cce656a407
commit
03925e3116
4 files changed
+240
-12
No files matched your search
@@ -305,12 +305,13 @@ pub fn math_class(character: char) -> MathClass {
|
||||
}
|
||||
}
|
||||
|
||||
/// Width of one math unit at 10pt (TeX's `\thinmuskip` is 3mu).
|
||||
const THIN_MU: i64 = 3 * 65_536 / 18;
|
||||
/// TeX's `\thinmuskip` at 10pt: `3mu`, where `1mu` is `1/18em` and `1em`
|
||||
/// is 10pt (so one mu is 36,408.9sp).
|
||||
const THIN_MU: i64 = 3 * 655_360 / 18;
|
||||
/// Width of `\medmuskip` (4mu plus 2mu minus 4mu at 10pt).
|
||||
const MED_MU: i64 = 4 * 65_536 / 18;
|
||||
const MED_MU: i64 = 4 * 655_360 / 18;
|
||||
/// Width of `\thickmuskip` (5mu plus 5mu at 10pt).
|
||||
const THICK_MU: i64 = 5 * 65_536 / 18;
|
||||
const THICK_MU: i64 = 5 * 655_360 / 18;
|
||||
|
||||
/// TeX's inter-atom spacing table.
|
||||
///
|
||||
@@ -1215,6 +1216,170 @@ fn math_symbol_command(name: &str) -> Option<char> {
|
||||
Some(character)
|
||||
}
|
||||
|
||||
/// Scan a TeX dimension at `index`, returning scaled points and the next index.
|
||||
///
|
||||
/// `muskip` makes a unitless value a math unit, which is what `\mskip` and
|
||||
/// `\mkern` require.
|
||||
fn scan_math_dimension(chars: &[char], index: usize, muskip: bool) -> Option<(i64, usize)> {
|
||||
let mut at = skip_math_space(chars, index);
|
||||
let mut negative = false;
|
||||
if chars.get(at) == Some(&'-') {
|
||||
negative = true;
|
||||
at += 1;
|
||||
} else if chars.get(at) == Some(&'+') {
|
||||
at += 1;
|
||||
}
|
||||
let mut whole = 0i64;
|
||||
let mut digits = 0u32;
|
||||
while let Some(digit) = chars.get(at).and_then(|ch| ch.to_digit(10)) {
|
||||
whole = whole.saturating_mul(10).saturating_add(digit as i64);
|
||||
at += 1;
|
||||
digits += 1;
|
||||
}
|
||||
let mut fraction = 0i64;
|
||||
if chars.get(at) == Some(&'.') {
|
||||
at += 1;
|
||||
let mut scale = 65_536i64;
|
||||
while let Some(digit) = chars.get(at).and_then(|ch| ch.to_digit(10)) {
|
||||
scale /= 10;
|
||||
fraction += digit as i64 * scale;
|
||||
at += 1;
|
||||
digits += 1;
|
||||
}
|
||||
}
|
||||
if digits == 0 {
|
||||
return None;
|
||||
}
|
||||
let mut unit = String::new();
|
||||
while let Some(ch) = chars.get(at) {
|
||||
if ch.is_ascii_alphabetic() {
|
||||
unit.push(*ch);
|
||||
at += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let (numerator, denominator) = match unit.as_str() {
|
||||
"mu" => (10, 18),
|
||||
"pt" => (1, 1),
|
||||
"bp" => (7227, 7200),
|
||||
"in" => (4_736_286, 65_536),
|
||||
"cm" => (1_864_679, 65_536),
|
||||
"mm" => (186_468, 65_536),
|
||||
"pc" => (786_432, 65_536),
|
||||
"dd" => (70_124, 65_536),
|
||||
"cc" => (841_489, 65_536),
|
||||
"em" => (10, 1),
|
||||
"ex" => (5, 1),
|
||||
"sp" => (1, 65_536),
|
||||
"" if muskip => (10, 18),
|
||||
"" => (1, 1),
|
||||
_ => return None,
|
||||
};
|
||||
let total = whole.saturating_mul(65_536).saturating_add(fraction);
|
||||
let value = total.saturating_mul(numerator) / denominator;
|
||||
Some((if negative { -value } else { value }, at))
|
||||
}
|
||||
|
||||
/// Match a word after skipping math spaces; rejects longer identifiers.
|
||||
fn math_keyword(chars: &[char], index: usize, word: &str) -> Option<usize> {
|
||||
let at = skip_math_space(chars, index);
|
||||
let mut cursor = at;
|
||||
for expected in word.chars() {
|
||||
if chars.get(cursor) != Some(&expected) {
|
||||
return None;
|
||||
}
|
||||
cursor += 1;
|
||||
}
|
||||
if chars
|
||||
.get(cursor)
|
||||
.is_some_and(|character| character.is_ascii_alphabetic())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some(cursor)
|
||||
}
|
||||
|
||||
/// Scan `\mskip`-style dimensions, consuming any `plus`/`minus` parts.
|
||||
///
|
||||
/// Only the natural width participates in the layout: math boxes are not
|
||||
/// glue lists, so stretch and shrink are accepted but not distributed.
|
||||
fn unbraced_math_space(chars: &[char], index: usize, muskip: bool) -> Option<(i64, usize)> {
|
||||
let (value, mut at) = scan_math_dimension(chars, index, muskip)?;
|
||||
let mut seen_plus = false;
|
||||
let mut seen_minus = false;
|
||||
loop {
|
||||
if !seen_plus {
|
||||
if let Some(after) = math_keyword(chars, at, "plus") {
|
||||
if let Some((_, next)) = scan_math_dimension(chars, after, muskip) {
|
||||
at = next;
|
||||
seen_plus = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !seen_minus {
|
||||
if let Some(after) = math_keyword(chars, at, "minus") {
|
||||
if let Some((_, next)) = scan_math_dimension(chars, after, muskip) {
|
||||
at = next;
|
||||
seen_minus = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
Some((value, at))
|
||||
}
|
||||
|
||||
/// Scan a braced dimension such as `\mspace{18mu}`.
|
||||
fn braced_math_space(chars: &[char], index: usize, muskip: bool) -> Option<(i64, usize)> {
|
||||
let at = skip_math_space(chars, index);
|
||||
if chars.get(at) != Some(&'{') {
|
||||
return None;
|
||||
}
|
||||
let mut depth = 0i64;
|
||||
let mut end = at;
|
||||
while end < chars.len() {
|
||||
match chars[end] {
|
||||
'{' => depth += 1,
|
||||
'}' => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
end += 1;
|
||||
}
|
||||
if depth != 0 {
|
||||
return None;
|
||||
}
|
||||
let (value, _) = scan_math_dimension(&chars[at + 1..end], 0, muskip)?;
|
||||
Some((value, end + 1))
|
||||
}
|
||||
|
||||
/// Read a math-space command argument, allowing an optional star.
|
||||
fn math_space_argument(
|
||||
chars: &[char],
|
||||
index: usize,
|
||||
muskip: bool,
|
||||
braced: bool,
|
||||
) -> Option<(i64, usize)> {
|
||||
let at = skip_math_space(chars, index);
|
||||
let at = if chars.get(at) == Some(&'*') {
|
||||
skip_math_space(chars, at + 1)
|
||||
} else {
|
||||
at
|
||||
};
|
||||
if braced {
|
||||
braced_math_space(chars, at, muskip)
|
||||
} else {
|
||||
unbraced_math_space(chars, at, muskip)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
let mut index = index;
|
||||
while chars.get(index) == Some(&'\u{0}') {
|
||||
@@ -1260,6 +1425,36 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
}
|
||||
}
|
||||
return match command.as_str() {
|
||||
"mid" => (
|
||||
MathAtom::Classed {
|
||||
class: MathClass::Relation,
|
||||
body: Box::new(MathAtom::Symbol('|')),
|
||||
},
|
||||
next,
|
||||
),
|
||||
"colon" => (
|
||||
MathAtom::Classed {
|
||||
class: MathClass::Punctuation,
|
||||
body: Box::new(MathAtom::Symbol(':')),
|
||||
},
|
||||
next,
|
||||
),
|
||||
"mskip" | "mkern" => match math_space_argument(chars, next, true, false) {
|
||||
Some((width, after)) => (MathAtom::Space(width), after),
|
||||
None => (MathAtom::Symbol('?'), next),
|
||||
},
|
||||
"mspace" => match math_space_argument(chars, next, true, true) {
|
||||
Some((width, after)) => (MathAtom::Space(width), after),
|
||||
None => (MathAtom::Symbol('?'), next),
|
||||
},
|
||||
"hskip" | "kern" => match math_space_argument(chars, next, false, false) {
|
||||
Some((width, after)) => (MathAtom::Space(width), after),
|
||||
None => (MathAtom::Symbol('?'), next),
|
||||
},
|
||||
"hspace" => match math_space_argument(chars, next, false, true) {
|
||||
Some((width, after)) => (MathAtom::Space(width), after),
|
||||
None => (MathAtom::Symbol('?'), next),
|
||||
},
|
||||
"quad" => (MathAtom::Space(655_360), next),
|
||||
"qquad" => (MathAtom::Space(1_310_720), next),
|
||||
"lvert" | "rvert" | "vert" => (MathAtom::Symbol('|'), next),
|
||||
@@ -2336,6 +2531,39 @@ mod tests {
|
||||
assert!(quad.width > medium.width);
|
||||
assert!(double.width > quad.width);
|
||||
}
|
||||
#[test]
|
||||
fn explicit_math_skips_have_their_natural_width() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let plain = layout_math(&parse_math("ab"), &font).width;
|
||||
let thin = layout_math(&parse_math(r"a\mskip 3mu b"), &font).width;
|
||||
let mspace = layout_math(&parse_math(r"a\mspace{18mu}b"), &font).width;
|
||||
let em = layout_math(&parse_math(r"a\hspace{1em}b"), &font).width;
|
||||
assert_eq!(thin - plain, THIN_MU);
|
||||
assert_eq!(mspace - plain, 655_360);
|
||||
assert_eq!(em - plain, 655_360);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_skips_accept_plus_minus_and_negative_values() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let plain = layout_math(&parse_math("ab"), &font).width;
|
||||
let stretchy = layout_math(&parse_math(r"a\mskip 3mu plus 1mu minus 1mu b"), &font).width;
|
||||
let tight = layout_math(&parse_math(r"a\mkern-2mu b"), &font).width;
|
||||
assert_eq!(stretchy - plain, THIN_MU);
|
||||
assert_eq!(plain - tight, 2 * 655_360 / 18);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mid_spaces_like_a_relation() {
|
||||
let font = Cmr10Metrics::default();
|
||||
let mid = layout_math(&parse_math(r"a\mid b"), &font).width;
|
||||
let bar = layout_math(&parse_math("a|b"), &font).width;
|
||||
assert_eq!(mid - bar, 2 * THICK_MU);
|
||||
let colon = layout_math(&parse_math(r"a\colon b"), &font).width;
|
||||
let relation = layout_math(&parse_math("a:b"), &font).width;
|
||||
assert!(colon < relation, "\\colon is punctuation, not a relation");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn math_text_preserves_internal_spaces() {
|
||||
let font = Cmr10Metrics::default();
|
||||
@@ -2660,7 +2888,7 @@ mod spacing_class_tests {
|
||||
fn punctuation_gets_a_thin_space() {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let packed = font.width('a') + font.width(',') + font.width('b');
|
||||
assert_eq!(width("a,b") - packed, 3 * 65_536 / 18);
|
||||
assert_eq!(width("a,b") - packed, THIN_MU);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2676,16 +2904,16 @@ mod spacing_class_tests {
|
||||
fn relation_and_binary_spacing_stay_distinct() {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let base = font.width('x') + font.width('+') + font.width('y');
|
||||
assert_eq!(width("x+y") - base, 2 * (4 * 65_536 / 18));
|
||||
assert_eq!(width("x+y") - base, 2 * MED_MU);
|
||||
let relation = font.width('x') + font.width('=') + font.width('y');
|
||||
assert_eq!(width("x=y") - relation, 2 * (5 * 65_536 / 18));
|
||||
assert_eq!(width("x=y") - relation, 2 * THICK_MU);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn big_operators_are_operator_atoms() {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let sum = width(r"\sum");
|
||||
assert_eq!(width(r"\sum x") - sum - font.width('x'), 3 * 65_536 / 18);
|
||||
assert_eq!(width(r"\sum x") - sum - font.width('x'), THIN_MU);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user