Compose Unicode letters from TeX accent commands
This commit is contained in:
1 parent
bb26825e12
commit
9dea19d5aa
4 files changed
+129
-4
No files matched your search
+125
@@ -4314,6 +4314,94 @@ fn text_symbol(rest: &str) -> Option<(&'static str, usize)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Return the precomposed letter for a base letter and a TeX accent command.
|
||||
fn compose_accent(base: char, accent: char) -> Option<char> {
|
||||
let (bases, composed) = match accent {
|
||||
'\'' => ("aeiouycAEIOUYCnsz", "áéíóúýćÁÉÍÓÚÝńśź"),
|
||||
'`' => ("aeiouAEIOU", "àèìòùÀÈÌÒÙ"),
|
||||
'^' => ("aeiouAEIOU", "âêîôûÂÊÎÔÛ"),
|
||||
'"' => ("aeiouyAEIOUY", "äëïöüÿÄËÏÖÜŸ"),
|
||||
'~' => ("anoANO", "ãñõÃÑÕ"),
|
||||
'c' => ("cC", "çÇ"),
|
||||
'r' => ("aA", "åÅ"),
|
||||
'v' => ("cszerndtlCSZERNDTL", "čšžěřňȊŽĚŘŇĎŤĽ"),
|
||||
'=' => ("aeiouAEIOU", "āēīōūĀĒĪŌŪ"),
|
||||
'u' => ("aeiouAEIOU", "ăĕĭŏŭĂĔĬŎŬ"),
|
||||
'.' => ("czCZ", "ċżĊŻ"),
|
||||
'H' => ("oOuU", "őűŐŰ"),
|
||||
_ => return None,
|
||||
};
|
||||
let index = bases.chars().position(|candidate| candidate == base)?;
|
||||
composed.chars().nth(index)
|
||||
}
|
||||
|
||||
/// The combining mark used when no precomposed letter exists.
|
||||
fn accent_mark(accent: char) -> Option<char> {
|
||||
Some(match accent {
|
||||
'\'' => '\u{301}',
|
||||
'`' => '\u{300}',
|
||||
'^' => '\u{302}',
|
||||
'"' => '\u{308}',
|
||||
'~' => '\u{303}',
|
||||
'c' => '\u{327}',
|
||||
'r' => '\u{30a}',
|
||||
'v' => '\u{30c}',
|
||||
'=' => '\u{304}',
|
||||
'u' => '\u{306}',
|
||||
'.' => '\u{307}',
|
||||
'H' => '\u{30b}',
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Recognize a TeX accent command such as `caf\'e` or `na\"{i}ve`.
|
||||
fn accent_run(rest: &str) -> Option<(String, usize)> {
|
||||
let mut characters = rest.char_indices();
|
||||
let (_, backslash) = characters.next()?;
|
||||
if backslash != '\\' {
|
||||
return None;
|
||||
}
|
||||
let (accent_offset, accent) = characters.next()?;
|
||||
let word_accent = accent.is_ascii_alphabetic();
|
||||
if word_accent && !matches!(accent, 'c' | 'v' | 'u' | 'r' | 'H') {
|
||||
return None;
|
||||
}
|
||||
if accent_mark(accent).is_none() {
|
||||
return None;
|
||||
}
|
||||
let mut after_start = accent_offset + accent.len_utf8();
|
||||
let after = &rest[after_start..];
|
||||
let (after, spaced) = if word_accent {
|
||||
let trimmed = after.trim_start_matches([' ', '\t', '\n']);
|
||||
after_start += after.len() - trimmed.len();
|
||||
(trimmed, trimmed.len() != after.len())
|
||||
} else {
|
||||
(after, false)
|
||||
};
|
||||
let (base, extra) = if let Some((group, length)) = braced_group(after) {
|
||||
let mut group = group.trim().chars();
|
||||
let base = group.next()?;
|
||||
if group.next().is_some() {
|
||||
return None;
|
||||
}
|
||||
(base, length)
|
||||
} else {
|
||||
if word_accent && !spaced {
|
||||
return None;
|
||||
}
|
||||
let base = after.chars().next()?;
|
||||
if base.is_whitespace() || base == '{' {
|
||||
return None;
|
||||
}
|
||||
(base, base.len_utf8())
|
||||
};
|
||||
let text = match compose_accent(base, accent) {
|
||||
Some(composed) => composed.to_string(),
|
||||
None => format!("{base}{}", accent_mark(accent)?),
|
||||
};
|
||||
Some((text, after_start + extra))
|
||||
}
|
||||
|
||||
fn parse_runs(
|
||||
text: &str,
|
||||
font: ParagraphFont,
|
||||
@@ -4355,6 +4443,12 @@ fn parse_runs(
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
if let Some((accent, length)) = accent_run(rest) {
|
||||
*found = true;
|
||||
push_run(runs, font, size, &accent);
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
if let Some((symbol, length)) = text_symbol(rest) {
|
||||
*found = true;
|
||||
push_run(runs, font, size, symbol);
|
||||
@@ -9047,6 +9141,13 @@ mod inline_tests {
|
||||
assert!(inline_runs("\\LaTeX{}", ParagraphFont::Roman).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accent_commands_paint_precomposed_glyphs() {
|
||||
let svg = preview_json("Caf\\'e au lait.").expect("preview");
|
||||
assert!(svg.contains(">é</text>"), "missing composed glyph: {svg}");
|
||||
assert!(!svg.contains(">'</text>"), "accent leaked as text: {svg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_symbols_paint_in_previews() {
|
||||
let json = preview_json("\\LaTeX{} \\S1").expect("preview");
|
||||
@@ -9055,6 +9156,30 @@ mod inline_tests {
|
||||
assert!(!json.contains("LaTeX{}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accent_commands_compose_characters() {
|
||||
assert_eq!(
|
||||
inline_runs("caf\\'e", ParagraphFont::Roman),
|
||||
Some(vec![(ParagraphFont::Roman, "café".to_string())])
|
||||
);
|
||||
assert_eq!(
|
||||
inline_runs("na\\\"ive", ParagraphFont::Roman),
|
||||
Some(vec![(ParagraphFont::Roman, "naïve".to_string())])
|
||||
);
|
||||
assert_eq!(
|
||||
inline_runs("\\c{c}a", ParagraphFont::Roman),
|
||||
Some(vec![(ParagraphFont::Roman, "ça".to_string())])
|
||||
);
|
||||
assert_eq!(
|
||||
inline_runs("\\'q", ParagraphFont::Roman),
|
||||
Some(vec![(ParagraphFont::Roman, "q\u{301}".to_string())])
|
||||
);
|
||||
assert_eq!(
|
||||
inline_runs("\\vspace and \\cup", ParagraphFont::Roman),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verb_spans_render_monospaced() {
|
||||
let runs = inline_runs("a \\verb|x&y| b", ParagraphFont::Roman).expect("runs");
|
||||
|
||||
Reference in new issue
Block a user