fix(formats): parse leading plus and case-insensitive units in LaTeX lengths

This commit is contained in:
Divyanshu Gupta
2026-10-16 03:47:04 +05:30
parent 86adfd34eb
commit 5817874b6d
+13 -2
View File
@@ -1669,11 +1669,12 @@ fn parse_color(model: Option<&str>, spec: &str) -> Option<Rgb> {
/// A TeX length in points (`3cm`, `0.4\linewidth`), if it is one.
fn length_pt(s: &str) -> Option<f32> {
let s = s.trim();
let s = s.trim().strip_prefix('+').unwrap_or(s.trim());
let split = s.find(|c: char| !(c.is_ascii_digit() || c == '.' || c == '-')).unwrap_or(s.len());
let (n, unit) = s.split_at(split);
let n: f32 = if n.is_empty() { 1.0 } else { n.parse().ok()? };
let k = match unit.trim() {
let unit = unit.trim().to_ascii_lowercase();
let k = match unit.as_str() {
"pt" => 1.0,
"bp" => 72.27 / 72.0,
"in" => 72.27,
@@ -2804,6 +2805,16 @@ mod tests {
assert!(widths.len() <= wordcraft_doc::table::MAX_COLS);
}
#[test]
fn latex_length_pt_case_and_plus() {
assert_eq!(length_pt("12pt"), Some(12.0));
assert_eq!(length_pt("12PT"), Some(12.0));
assert_eq!(length_pt("+12pt"), Some(12.0));
assert_eq!(length_pt("1IN"), Some(72.27));
assert_eq!(length_pt("+2cm"), Some(56.9));
assert_eq!(length_pt("0.5\\TEXTWIDTH"), Some(LINE_WIDTH * 0.5));
}
const ARTICLE: &str = r#"\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} % maths