Normalize native XeTeX physical input before tokenization
This commit is contained in:
1 parent
048478c346
commit
76df2d6832
10 files changed
+214
-63
No files matched your search
@@ -1,5 +1,7 @@
|
||||
//! XeTeX physical input encodings. Decoded units retain their source bytes.
|
||||
use std::sync::Arc;
|
||||
use std::{collections::BTreeSet, sync::Arc};
|
||||
|
||||
use super::{Dependency, Parameter, ParameterValue, State};
|
||||
|
||||
pub(super) mod icu;
|
||||
|
||||
@@ -68,6 +70,13 @@ pub(super) struct Character {
|
||||
pub end: usize,
|
||||
}
|
||||
|
||||
impl Character {
|
||||
pub fn cover(&mut self, other: Self) {
|
||||
self.start = self.start.min(other.start);
|
||||
self.end = self.end.max(other.end);
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoding {
|
||||
pub fn parse(name: &str) -> Result<Option<Self>, String> {
|
||||
let builtin = match name.to_ascii_lowercase().as_str() {
|
||||
@@ -106,7 +115,29 @@ impl Encoding {
|
||||
(encoding, bom)
|
||||
}
|
||||
|
||||
pub fn decode(&self, data: &[u8], base: usize) -> Result<(Vec<Character>, usize), String> {
|
||||
/// Decode a complete physical line before TeX looks at any of its catcodes.
|
||||
pub fn decode_line(
|
||||
&self,
|
||||
data: &[u8],
|
||||
base: usize,
|
||||
state: &State,
|
||||
reads: &mut BTreeSet<Dependency>,
|
||||
) -> Result<(Vec<Character>, usize), String> {
|
||||
let (mut characters, warnings) = self.decode(data, base)?;
|
||||
let parameter = Parameter::XeTeXinputnormalization;
|
||||
reads.insert(Dependency::Parameter(parameter));
|
||||
match state.parameter(parameter) {
|
||||
ParameterValue::Integer(1) => characters = normalize(characters, true),
|
||||
ParameterValue::Integer(2) => characters = normalize(characters, false),
|
||||
_ => {}
|
||||
}
|
||||
while characters.last().is_some_and(|c| matches!(c.value, ' ' | '\r' | '\n')) {
|
||||
characters.pop();
|
||||
}
|
||||
Ok((characters, warnings))
|
||||
}
|
||||
|
||||
fn decode(&self, data: &[u8], base: usize) -> Result<(Vec<Character>, usize), String> {
|
||||
let mut invalid_utf8 = 0;
|
||||
let chars = match self {
|
||||
Self::Auto => unreachable!("resolve the encoding at file open"),
|
||||
@@ -174,6 +205,22 @@ impl Encoding {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize(characters: Vec<Character>, composed: bool) -> Vec<Character> {
|
||||
use unicode_normalization::char::decompose_canonical;
|
||||
|
||||
if characters.iter().all(|character| character.value.is_ascii()) {
|
||||
return characters;
|
||||
}
|
||||
let mut decomposed = Vec::with_capacity(characters.len());
|
||||
for character in characters {
|
||||
decompose_canonical(character.value, |value| decomposed.push((value, character)));
|
||||
}
|
||||
crate::normalization::order_and_compose(decomposed, composed, Character::cover)
|
||||
.into_iter()
|
||||
.map(|(value, source)| Character { value, ..source })
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn decode_utf8_character(input: &[u8]) -> (char, usize, bool) {
|
||||
let first = input[0];
|
||||
let (mut value, continuation) = match first {
|
||||
|
||||
Reference in new issue
Block a user