1use std::{fmt, rc::Rc};
2
3use libxml::tree::Node;
4
5use crate::{
6 common::{error::Result, font::Font},
7 document::Document,
8};
9
10pub type LigatureClosure = Rc<dyn Fn(&str) -> String>;
11pub type FontTestClosure = Rc<dyn Fn(&Font) -> Result<bool>>;
13pub type LigatureMatcher =
14 Rc<dyn Fn(&mut Document, &mut Node) -> Result<Option<(usize, String, MathLigatureOptions)>>>;
15
16thread_local! {
19 static SCRIPT_LIGATURES: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
20}
21
22pub fn note_script_ligature() { SCRIPT_LIGATURES.with(|on| on.set(true)); }
24
25pub fn script_ligatures() -> bool { SCRIPT_LIGATURES.with(std::cell::Cell::get) }
27
28#[derive(Debug, Default, Clone, PartialEq, Eq)]
29pub struct MathLigatureOptions {
30 pub role: Option<String>,
31 pub name: Option<String>,
32 pub meaning: Option<String>,
33}
34
35impl MathLigatureOptions {
36 pub fn sorted_each(&self) -> [(&str, Option<&String>); 3] {
37 [
38 ("meaning", self.meaning.as_ref()),
39 ("name", self.name.as_ref()),
40 ("role", self.role.as_ref()),
41 ]
42 }
43}
44
45#[derive(Clone, Default)]
46pub struct Ligature {
47 pub id: usize,
48 pub regex: Option<String>,
49 pub code: Option<LigatureClosure>,
50 pub font_test: Option<FontTestClosure>,
51 pub matcher: Option<LigatureMatcher>,
52}
53
54impl fmt::Debug for Ligature {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self.regex) }
56}
57
58impl PartialEq for Ligature {
59 fn eq(&self, other: &Ligature) -> bool { self.id == other.id }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn math_ligature_options_default_is_none_triple() {
68 let m = MathLigatureOptions::default();
69 assert!(m.role.is_none());
70 assert!(m.name.is_none());
71 assert!(m.meaning.is_none());
72 }
73
74 #[test]
75 fn math_ligature_options_equality() {
76 let a = MathLigatureOptions {
77 role: Some("ADDOP".into()),
78 name: None,
79 meaning: Some("plus".into()),
80 };
81 let b = MathLigatureOptions {
82 role: Some("ADDOP".into()),
83 name: None,
84 meaning: Some("plus".into()),
85 };
86 let c = MathLigatureOptions {
87 role: Some("RELOP".into()), name: None,
89 meaning: Some("plus".into()),
90 };
91 assert_eq!(a, b);
92 assert_ne!(a, c);
93 }
94
95 #[test]
96 fn math_ligature_sorted_each_fixed_order() {
97 let m = MathLigatureOptions {
99 role: Some("r".into()),
100 name: Some("n".into()),
101 meaning: Some("m".into()),
102 };
103 let ordered = m.sorted_each();
104 assert_eq!(ordered[0].0, "meaning");
105 assert_eq!(ordered[1].0, "name");
106 assert_eq!(ordered[2].0, "role");
107 }
108
109 #[test]
110 fn math_ligature_sorted_each_none_values_preserved() {
111 let m = MathLigatureOptions::default();
113 let ordered = m.sorted_each();
114 for (_, v) in ordered {
115 assert!(v.is_none());
116 }
117 }
118
119 #[test]
120 fn ligature_default_has_zero_id_and_none_fields() {
121 let l = Ligature::default();
122 assert_eq!(l.id, 0);
123 assert!(l.regex.is_none());
124 assert!(l.code.is_none());
125 assert!(l.font_test.is_none());
126 assert!(l.matcher.is_none());
127 }
128
129 #[test]
130 fn ligature_equality_by_id_only() {
131 let mut a = Ligature::default();
132 let mut b = Ligature::default();
133 a.id = 1;
134 b.id = 1;
135 assert_eq!(a, b);
138 b.id = 2;
139 assert_ne!(a, b);
140 }
141
142 #[test]
143 fn ligature_debug_format_uses_regex() {
144 let l = Ligature {
147 regex: Some("test_regex".to_string()),
148 ..Default::default()
149 };
150 let out = format!("{l:?}");
151 assert!(out.contains("test_regex"), "got {out:?}");
152 }
153}