Keep the content of scaled box wrappers
\scalebox, \resizebox, \rotatebox and \raisebox were unsupported, so a wrapped graphic or table could leak its command text. A body pass now parses each wrapper's numeric/optional arguments and replaces the whole construct with its content, leaving the content at natural size and position because the engine has no display-list transform. A test wraps four fragments in the four commands and checks the visible text concatenates with no warnings and no leaked command names.
This commit is contained in:
1 parent
dc9428e87e
commit
7fbcb326d5
4 files changed
+113
-6
No files matched your search
+107
@@ -6443,6 +6443,7 @@ fn geometry_and_body_with_images(
|
||||
geometry.skip_registers = skip_registers;
|
||||
body = expand_links(&body);
|
||||
body = strip_inline_marks(&body, &mut geometry);
|
||||
body = strip_scaled_boxes(&body);
|
||||
let today = today_string();
|
||||
body = expand_today(&body, &today);
|
||||
body = expand_lipsum(&body);
|
||||
@@ -9939,6 +9940,89 @@ fn expand_phantoms(text: &str) -> String {
|
||||
output
|
||||
}
|
||||
|
||||
/// Drop the scaling/rotating box wrappers but keep their content: the engine
|
||||
/// has no display-list transform, so `\scalebox`, `\resizebox`, `\rotatebox`
|
||||
/// and `\raisebox` render their contents at the natural size and position.
|
||||
fn strip_scaled_boxes(text: &str) -> String {
|
||||
let mut output = String::new();
|
||||
let mut rest = text;
|
||||
loop {
|
||||
let mut next: Option<(usize, &str)> = None;
|
||||
for command in [
|
||||
"\\resizebox*",
|
||||
"\\resizebox",
|
||||
"\\scalebox",
|
||||
"\\rotatebox",
|
||||
"\\raisebox",
|
||||
] {
|
||||
if let Some(index) = rest.find(command) {
|
||||
let better = next.is_none_or(|(known, _)| index < known);
|
||||
if better {
|
||||
next = Some((index, command));
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some((index, command)) = next else {
|
||||
output.push_str(rest);
|
||||
break;
|
||||
};
|
||||
output.push_str(&rest[..index]);
|
||||
let mut tail = rest[index + command.len()..].trim_start();
|
||||
let arguments = match command {
|
||||
"\\resizebox" | "\\resizebox*" => 2,
|
||||
_ => 1,
|
||||
};
|
||||
let mut failed = false;
|
||||
for _ in 0..arguments {
|
||||
if command == "\\rotatebox" || command == "\\raisebox" {
|
||||
tail = skip_optional_groups(tail);
|
||||
}
|
||||
if let Some((_, used)) = braced_group(tail) {
|
||||
tail = tail[used..].trim_start();
|
||||
} else {
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if command == "\\raisebox" {
|
||||
tail = skip_optional_groups(tail);
|
||||
}
|
||||
if failed {
|
||||
output.push_str(command);
|
||||
rest = &rest[index + command.len()..];
|
||||
continue;
|
||||
}
|
||||
let Some((content, used)) = braced_group(tail) else {
|
||||
output.push_str(command);
|
||||
rest = &rest[index + command.len()..];
|
||||
continue;
|
||||
};
|
||||
output.push_str(&content);
|
||||
rest = &tail[used..];
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// Skip optional `[...]` groups in front of an argument.
|
||||
fn skip_optional_groups(mut text: &str) -> &str {
|
||||
loop {
|
||||
let trimmed = text.trim_start();
|
||||
let Some(after) = trimmed.strip_prefix('[') else {
|
||||
return trimmed;
|
||||
};
|
||||
let Some((_, used)) = optional_group(after) else {
|
||||
return trimmed;
|
||||
};
|
||||
text = &after[used..];
|
||||
}
|
||||
}
|
||||
|
||||
/// Read an optional `[...]` group's body and the bytes consumed with the `]`.
|
||||
fn optional_group(text: &str) -> Option<(String, usize)> {
|
||||
let end = text.find(']')?;
|
||||
Some((text[..end].to_string(), end + 1))
|
||||
}
|
||||
|
||||
/// Consume `\markboth`/`\markright` that appear inside running text (the
|
||||
/// line-start forms are handled while the page geometry is read) and record the
|
||||
/// most recent marks for the running heads.
|
||||
@@ -16914,6 +16998,29 @@ A definition.
|
||||
assert!(!text.contains("??"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scaled_box_wrappers_keep_their_content() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\nA \\scalebox{0.5}{small} B \\resizebox{2cm}{!}{wide} C \\rotatebox{90}{turned} D \\raisebox{2pt}{lifted} E.\n\n\\end{document}\n";
|
||||
let json = preview_json(source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
assert_eq!(
|
||||
value["warnings"].as_array().map(Vec::len),
|
||||
Some(0),
|
||||
"{}",
|
||||
value["warnings"]
|
||||
);
|
||||
let svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||
let text: String = svg
|
||||
.split('>')
|
||||
.filter_map(|piece| piece.split('<').next())
|
||||
.collect::<String>()
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
assert!(text.contains("AsmallBwideCturnedDliftedE."), "{text}");
|
||||
assert!(!text.contains("scalebox"), "{text}");
|
||||
assert!(!text.contains("resizebox"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_par_breaks_paragraphs() {
|
||||
let (body, geometry) = geometry_and_body("First line.\\par Second line.");
|
||||
|
||||
Reference in new issue
Block a user