Reserve invisible space for phantom commands
Text-mode \phantom and \hphantom were unsupported. A text-stage pass measures
the scrubbed argument with the Roman metrics and replaces the command with
\hspace of that width, so the glyphs are omitted but the line keeps their
footprint; \vphantom contributes no horizontal material.
A test renders "A\phantom{wide}B and \vphantom{XX}C and \hphantom{QQ}D." and
checks "AB and C and D." with no warnings and none of the hidden text.
This commit is contained in:
1 parent
5fbcc34ddd
commit
ebb178bff5
4 files changed
+67
-6
No files matched your search
@@ -9800,6 +9800,42 @@ fn strip_noop_commands(text: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// Replace text-mode `\phantom`/`\hphantom` with invisible horizontal space of
|
||||
/// the measured width; `\vphantom` contributes no horizontal material.
|
||||
fn expand_phantoms(text: &str) -> String {
|
||||
let mut output = String::new();
|
||||
let mut rest = text;
|
||||
loop {
|
||||
let mut next: Option<(usize, &str)> = None;
|
||||
for command in ["\\hphantom", "\\vphantom", "\\phantom"] {
|
||||
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 after = rest[index + command.len()..].trim_start();
|
||||
let Some((body, used)) = braced_group(after) else {
|
||||
output.push_str(command);
|
||||
rest = &rest[index + command.len()..];
|
||||
continue;
|
||||
};
|
||||
if command != "\\vphantom" {
|
||||
let cleaned = scrub_title_metadata(&body);
|
||||
let width = text_width_roman(&cleaned) as f64 / 65_536.0;
|
||||
output.push_str(&format!("\\hspace{{{width:.3}pt}}"));
|
||||
}
|
||||
rest = &after[used..];
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// Keep the visible label of `\hyperref[label]{text}`, `\hyperlink{name}{text}`
|
||||
/// and `\hypertarget{name}{text}`; internal PDF targets are not modelled.
|
||||
fn strip_hyperref_commands(text: &str) -> String {
|
||||
@@ -12544,6 +12580,7 @@ fn build_document_with_labels(
|
||||
text = rest.trim_start().to_string();
|
||||
}
|
||||
|
||||
text = expand_phantoms(&text);
|
||||
text = expand_parbox(&text);
|
||||
text = strip_hyperref_commands(&text);
|
||||
text = expand_orcidlink(&text);
|
||||
@@ -16478,6 +16515,30 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phantom_commands_reserve_invisible_space() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\nA\\phantom{wide}B and \\vphantom{XX}C and \\hphantom{QQ}D.\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("ABandCandD."), "{text}");
|
||||
assert!(!text.contains("wide"), "{text}");
|
||||
assert!(!text.contains("XX"), "{text}");
|
||||
assert!(!text.contains("QQ"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hyperref_commands_keep_their_labels() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\nSee \\hyperref[sec:intro]{the intro}, \\hyperlink{target}{linked} and \\hypertarget{id}{anchor}.\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user