Transform MakeUppercase and MakeLowercase arguments
This commit is contained in:
1 parent
81e6a2befc
commit
18c0288db0
4 files changed
+70
-4
No files matched your search
@@ -4310,6 +4310,43 @@ fn verb_run(rest: &str) -> Option<(String, usize)> {
|
||||
}
|
||||
|
||||
/// Recognise a text command that stands for a printable character.
|
||||
/// Recognize `\MakeUppercase`/`\MakeLowercase` and their TeX spellings.
|
||||
fn case_run(rest: &str) -> Option<(String, usize)> {
|
||||
for (command, upper) in [
|
||||
("\\MakeUppercase", true),
|
||||
("\\MakeLowercase", false),
|
||||
("\\uppercase", true),
|
||||
("\\lowercase", false),
|
||||
] {
|
||||
let Some(after) = rest.strip_prefix(command) else {
|
||||
continue;
|
||||
};
|
||||
let Some(after) = after.strip_prefix('{') else {
|
||||
continue;
|
||||
};
|
||||
let mut depth = 1_usize;
|
||||
for (offset, character) in after.char_indices() {
|
||||
match character {
|
||||
'{' => depth += 1,
|
||||
'}' => {
|
||||
depth -= 1;
|
||||
if depth == 0 {
|
||||
let inner = &after[..offset];
|
||||
let changed = if upper {
|
||||
inner.to_uppercase()
|
||||
} else {
|
||||
inner.to_lowercase()
|
||||
};
|
||||
return Some((changed, command.len() + 1 + offset + 1));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn text_symbol(rest: &str) -> Option<(&'static str, usize)> {
|
||||
const SYMBOLS: &[(&str, &str)] = &[
|
||||
("\\textthreequarters", "\u{be}"),
|
||||
@@ -4559,6 +4596,12 @@ fn parse_runs(
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
if let Some((changed, length)) = case_run(rest) {
|
||||
*found = true;
|
||||
push_run(runs, font, size, raise, &changed);
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
if let Some((symbol, length)) = text_symbol(rest) {
|
||||
*found = true;
|
||||
push_run(runs, font, size, raise, symbol);
|
||||
@@ -9288,6 +9331,13 @@ mod inline_tests {
|
||||
assert!(inline_runs("\\LaTeX{}", ParagraphFont::Roman).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn case_commands_paint_uppercase_text() {
|
||||
let svg = preview_json("\\MakeUppercase{quiet}.").expect("preview");
|
||||
assert!(svg.contains(">Q</text>"), "missing uppercase: {svg}");
|
||||
assert!(!svg.contains("MakeUppercase"), "command leaked: {svg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn letter_commands_paint_unicode_glyphs() {
|
||||
let svg = preview_json("Stra\\ss e").expect("preview");
|
||||
@@ -9321,6 +9371,22 @@ mod inline_tests {
|
||||
assert!(!json.contains("LaTeX{}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn case_commands_transform_their_argument() {
|
||||
assert_eq!(
|
||||
inline_runs("\\MakeUppercase{Straße}", ParagraphFont::Roman),
|
||||
Some(vec![(ParagraphFont::Roman, "STRASSE".to_string())])
|
||||
);
|
||||
assert_eq!(
|
||||
inline_runs("\\MakeLowercase{ÀÉ}", ParagraphFont::Roman),
|
||||
Some(vec![(ParagraphFont::Roman, "àé".to_string())])
|
||||
);
|
||||
assert_eq!(
|
||||
inline_runs("\\uppercase{abc} tail", ParagraphFont::Roman),
|
||||
Some(vec![(ParagraphFont::Roman, "ABC tail".to_string())])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn letter_and_symbol_commands_render_unicode() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user