Expand common text symbol commands

This commit is contained in:
gloamxun committed 2026-10-25 01:02:50 +08:00
1 parent 0b4c2ec394
commit 0ea4bd65e8
4 files changed
+67 -4

No files matched your search

+63
View File
@@ -3800,6 +3800,47 @@ fn verb_run(rest: &str) -> Option<(String, usize)> {
))
}
/// Recognise a text command that stands for a printable character.
fn text_symbol(rest: &str) -> Option<(&'static str, usize)> {
const SYMBOLS: &[(&str, &str)] = &[
("\\textbackslash", "\\"),
("\\textasciitilde", "~"),
("\\textasciicircum", "^"),
("\\textquotedblleft", "\u{201c}"),
("\\textquotedblright", "\u{201d}"),
("\\textquoteleft", "\u{2018}"),
("\\textquoteright", "\u{2019}"),
("\\textemdash", "\u{2014}"),
("\\textendash", "\u{2013}"),
("\\textgreater", ">"),
("\\textbullet", "\u{2022}"),
("\\textless", "<"),
("\\copyright", "\u{a9}"),
("\\LaTeXe", "LaTeX2e"),
("\\LaTeX", "LaTeX"),
("\\pounds", "\u{a3}"),
("\\textbar", "|"),
("\\ldots", "\u{2026}"),
("\\dots", "\u{2026}"),
("\\TeX", "TeX"),
("\\S", "\u{a7}"),
("\\P", "\u{b6}"),
];
for (command, replacement) in SYMBOLS {
let Some(after) = rest.strip_prefix(command) else {
continue;
};
if after
.chars()
.next()
.map_or(true, |character| !character.is_ascii_alphabetic())
{
return Some((replacement, command.len()));
}
}
None
}
fn parse_runs(
text: &str,
font: ParagraphFont,
@@ -3841,6 +3882,12 @@ fn parse_runs(
index += length;
continue;
}
if let Some((symbol, length)) = text_symbol(rest) {
*found = true;
push_run(runs, font, size, symbol);
index += length;
continue;
}
if rest.starts_with('\\') {
let step = rest
.char_indices()
@@ -7934,6 +7981,22 @@ mod nfss_tests {
mod inline_tests {
use super::*;
#[test]
fn text_symbols_render_as_characters() {
let runs = inline_runs("\\LaTeX{} and \\ldots", ParagraphFont::Roman).expect("runs");
let text: String = runs.into_iter().map(|(_, text)| text).collect();
assert_eq!(text, "LaTeX and \u{2026}");
assert!(inline_runs("\\LaTeX{}", ParagraphFont::Roman).is_some());
}
#[test]
fn text_symbols_paint_in_previews() {
let json = preview_json("\\LaTeX{} \\S1").expect("preview");
assert!(json.contains(">L</text>"));
assert!(json.contains(">\u{a7}</text>"));
assert!(!json.contains("LaTeX{}"));
}
#[test]
fn verb_spans_render_monospaced() {
let runs = inline_runs("a \\verb|x&y| b", ParagraphFont::Roman).expect("runs");