Persist language-specific e-TeX hyphenation codes
This commit is contained in:
1 parent
11fd66713b
commit
b472f66d04
10 files changed
+198
-45
No files matched your search
@@ -46,6 +46,14 @@ pub(super) fn max_word_length(state: &State) -> usize {
|
||||
pub(super) fn normalize_language(n: i32) -> u8 {
|
||||
if (1..=255).contains(&n) { n as u8 } else { 0 }
|
||||
}
|
||||
|
||||
pub(super) fn lower_code(state: &State, language: u8, c: char) -> u32 {
|
||||
state
|
||||
.hyphenation
|
||||
.saved_code(language, c)
|
||||
.unwrap_or_else(|| state.case_code(CaseCode::Lower, c))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
struct TrieNode {
|
||||
edges: BTreeMap<u32, usize>,
|
||||
@@ -79,10 +87,28 @@ impl Hash for TrieNodes {
|
||||
pub(super) struct HyphenationData {
|
||||
nodes: Arc<TrieNodes>,
|
||||
roots: BTreeMap<u8, usize>,
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
saved_codes: Arc<BTreeMap<u8, BTreeMap<u8, u16>>>,
|
||||
pub exceptions: BTreeMap<u8, BTreeMap<String, Vec<usize>>>,
|
||||
pub sealed: bool,
|
||||
}
|
||||
impl HyphenationData {
|
||||
pub(super) fn save_codes(&mut self, language: u8, codes: BTreeMap<u8, u16>) {
|
||||
Arc::make_mut(&mut self.saved_codes).insert(language, codes);
|
||||
}
|
||||
|
||||
pub(super) fn saved_code(&self, language: u8, c: char) -> Option<u32> {
|
||||
let slot = u8::try_from(c).ok()?;
|
||||
let codes = self.saved_codes.get(&language)?;
|
||||
// An absent slot in a saved table has lccode zero. Only an absent
|
||||
// table, or a XeTeX character above 255, consults the live table.
|
||||
Some(u32::from(codes.get(&slot).copied().unwrap_or(0)))
|
||||
}
|
||||
|
||||
pub(super) fn saved_code_count(&self) -> usize {
|
||||
self.saved_codes.values().map(BTreeMap::len).sum()
|
||||
}
|
||||
|
||||
pub(super) fn resource_counts(&self) -> (usize, usize, usize) {
|
||||
let (edges, weights) = *self.nodes.counts.get_or_init(|| {
|
||||
self.nodes
|
||||
@@ -134,8 +160,14 @@ impl HyphenationData {
|
||||
nodes.push(TrieNode::default());
|
||||
Ok(next)
|
||||
}
|
||||
pub fn points(&self, language: u8, word: &str) -> Vec<usize> {
|
||||
if let Some(points) = self.exceptions.get(&language).and_then(|e| e.get(word)) {
|
||||
pub(super) fn insert_exception(&mut self, language: u8, word: &[u32], points: Vec<usize>) {
|
||||
self.exceptions.entry(language).or_default().insert(exception_key(word), points);
|
||||
}
|
||||
|
||||
pub fn points(&self, language: u8, word: &[u32]) -> Vec<usize> {
|
||||
if let Some(exceptions) = self.exceptions.get(&language)
|
||||
&& let Some(points) = exceptions.get(&exception_key(word))
|
||||
{
|
||||
return points.clone();
|
||||
}
|
||||
let Some(&root) = self.roots.get(&language) else {
|
||||
@@ -143,7 +175,7 @@ impl HyphenationData {
|
||||
};
|
||||
let nodes = &self.nodes.values;
|
||||
let letters: Vec<_> = std::iter::once(0)
|
||||
.chain(word.chars().map(|c| c as u32))
|
||||
.chain(word.iter().copied())
|
||||
.chain([0])
|
||||
.collect();
|
||||
let mut scores = vec![0; letters.len() + 1];
|
||||
@@ -159,12 +191,27 @@ impl HyphenationData {
|
||||
}
|
||||
}
|
||||
}
|
||||
(1..word.chars().count())
|
||||
(1..word.len())
|
||||
.filter(|i| scores[i + 1] % 2 == 1)
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn exception_key(word: &[u32]) -> String {
|
||||
let mut key = String::with_capacity(word.len());
|
||||
for &code in word {
|
||||
if let Some(c) = char::from_u32(code) {
|
||||
key.push(c);
|
||||
} else {
|
||||
// XeTeX can truncate a saved code to a UTF-16 surrogate. Escape
|
||||
// that unit with NUL, which cannot occur as a hyphenation letter.
|
||||
key.push('\0');
|
||||
key.push(char::from_u32(code - 0xd800).expect("saved surrogate code"));
|
||||
}
|
||||
}
|
||||
key
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cloned_patterns_remain_isolated_on_assignment() {
|
||||
let mut format = HyphenationData::default();
|
||||
@@ -177,8 +224,9 @@ fn cloned_patterns_remain_isolated_on_assignment() {
|
||||
format.insert(0, &['a' as u32], &[0, 0], 12).unwrap();
|
||||
assert_eq!(format.resource_counts(), (4, 3, 5));
|
||||
assert_eq!(shared.resource_counts(), (3, 2, 3));
|
||||
assert_eq!(shared.points(0, "tea"), [1]);
|
||||
assert_eq!(format.points(0, "tea"), [1]);
|
||||
let tea = [u32::from(b't'), u32::from(b'e'), u32::from(b'a')];
|
||||
assert_eq!(shared.points(0, &tea), [1]);
|
||||
assert_eq!(format.points(0, &tea), [1]);
|
||||
|
||||
let encoded = serde_json::to_value(&shared).unwrap();
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user