42 lines
1.3 KiB
Rust
42 lines
1.3 KiB
Rust
//! Job input decoding and the explicit XeTeX compatibility profile.
|
|
//! Both paths retain original byte offsets; the byte path never recodes a file.
|
|
#[derive(
|
|
Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
|
|
)]
|
|
pub enum InputProfile {
|
|
TeX8Bit,
|
|
#[default]
|
|
UnicodeScalar,
|
|
/// UTF-8 input, XeTeX primitives and XDV-device semantics.
|
|
XeTeX,
|
|
}
|
|
impl InputProfile {
|
|
pub(super) fn characters(self, data: &[u8]) -> Result<Characters<'_>, std::str::Utf8Error> {
|
|
Ok(match self {
|
|
Self::TeX8Bit => Characters::Bytes(data.iter().enumerate()),
|
|
Self::UnicodeScalar | Self::XeTeX => {
|
|
Characters::Unicode(std::str::from_utf8(data)?.char_indices())
|
|
}
|
|
})
|
|
}
|
|
pub(super) fn width(self, c: char) -> usize {
|
|
match self {
|
|
Self::TeX8Bit => 1,
|
|
Self::UnicodeScalar | Self::XeTeX => c.len_utf8(),
|
|
}
|
|
}
|
|
}
|
|
pub(super) enum Characters<'a> {
|
|
Bytes(std::iter::Enumerate<std::slice::Iter<'a, u8>>),
|
|
Unicode(std::str::CharIndices<'a>),
|
|
}
|
|
impl Iterator for Characters<'_> {
|
|
type Item = (usize, char);
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
match self {
|
|
Self::Bytes(iter) => iter.next().map(|(i, &b)| (i, char::from(b))),
|
|
Self::Unicode(iter) => iter.next(),
|
|
}
|
|
}
|
|
}
|