Skip to main content

harfrust/
script.rs

1use crate::{tag::TagExt, Tag};
2use core::str::FromStr;
3
4// In harfbuzz, despite having `Script`, script can actually have any tag.
5// So we're doing the same.
6// The only difference is that `Script` cannot be set to `HB_SCRIPT_INVALID`.
7/// A text script identified by an ISO 15924 tag.
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
9pub struct Script(pub(crate) Tag);
10
11impl Script {
12    #[inline]
13    pub(crate) const fn from_bytes(bytes: &[u8; 4]) -> Self {
14        Script(Tag::new(bytes))
15    }
16
17    /// Converts an ISO 15924 script tag to a corresponding `Script`.
18    pub const fn from_iso15924_tag(tag: Tag) -> Option<Script> {
19        let tag = u32::from_be_bytes(tag.to_be_bytes());
20
21        if tag == 0 {
22            return None;
23        }
24
25        // Be lenient, adjust case (one capital letter followed by three small letters).
26        let tag = (tag & 0xDFDF_DFDF) | 0x0020_2020;
27
28        if tag & 0xE0E0_E0E0 != 0x4060_6060 {
29            return Some(Script::UNKNOWN);
30        }
31
32        Some(match &tag.to_be_bytes() {
33            // Script variants from https://unicode.org/iso15924/
34            b"Aran" => Script::ARABIC,
35            b"Cyrs" => Script::CYRILLIC,
36            b"Geok" => Script::GEORGIAN,
37            b"Hans" | b"Hant" => Script::HAN,
38            b"Jamo" => Script::HANGUL,
39            b"Latf" | b"Latg" => Script::LATIN,
40            b"Syre" | b"Syrj" | b"Syrn" => Script::SYRIAC,
41            &t => Script(Tag::from_be_bytes(t)),
42        })
43    }
44
45    /// Returns script's tag.
46    #[inline]
47    pub fn tag(&self) -> Tag {
48        self.0
49    }
50}
51
52impl FromStr for Script {
53    type Err = &'static str;
54
55    fn from_str(s: &str) -> Result<Self, Self::Err> {
56        let tag = Tag::from_bytes_lossy(s.as_bytes());
57        Script::from_iso15924_tag(tag).ok_or("invalid script")
58    }
59}
60
61#[allow(missing_docs)]
62impl Script {
63    // Since 1.1
64    pub const COMMON: Self = Self::from_bytes(b"Zyyy");
65    pub const INHERITED: Self = Self::from_bytes(b"Zinh");
66    pub const ARABIC: Self = Self::from_bytes(b"Arab");
67    pub const ARMENIAN: Self = Self::from_bytes(b"Armn");
68    pub const BENGALI: Self = Self::from_bytes(b"Beng");
69    pub const CYRILLIC: Self = Self::from_bytes(b"Cyrl");
70    pub const DEVANAGARI: Self = Self::from_bytes(b"Deva");
71    pub const GEORGIAN: Self = Self::from_bytes(b"Geor");
72    pub const GREEK: Self = Self::from_bytes(b"Grek");
73    pub const GUJARATI: Self = Self::from_bytes(b"Gujr");
74    pub const GURMUKHI: Self = Self::from_bytes(b"Guru");
75    pub const HANGUL: Self = Self::from_bytes(b"Hang");
76    pub const HAN: Self = Self::from_bytes(b"Hani");
77    pub const HEBREW: Self = Self::from_bytes(b"Hebr");
78    pub const HIRAGANA: Self = Self::from_bytes(b"Hira");
79    pub const KANNADA: Self = Self::from_bytes(b"Knda");
80    pub const KATAKANA: Self = Self::from_bytes(b"Kana");
81    pub const LAO: Self = Self::from_bytes(b"Laoo");
82    pub const LATIN: Self = Self::from_bytes(b"Latn");
83    pub const MALAYALAM: Self = Self::from_bytes(b"Mlym");
84    pub const ORIYA: Self = Self::from_bytes(b"Orya");
85    pub const TAMIL: Self = Self::from_bytes(b"Taml");
86    pub const TELUGU: Self = Self::from_bytes(b"Telu");
87    pub const THAI: Self = Self::from_bytes(b"Thai");
88    // Since 2.0
89    pub const TIBETAN: Self = Self::from_bytes(b"Tibt");
90    // Since 3.0
91    pub const BOPOMOFO: Self = Self::from_bytes(b"Bopo");
92    pub const BRAILLE: Self = Self::from_bytes(b"Brai");
93    pub const CANADIAN_SYLLABICS: Self = Self::from_bytes(b"Cans");
94    pub const CHEROKEE: Self = Self::from_bytes(b"Cher");
95    pub const ETHIOPIC: Self = Self::from_bytes(b"Ethi");
96    pub const KHMER: Self = Self::from_bytes(b"Khmr");
97    pub const MONGOLIAN: Self = Self::from_bytes(b"Mong");
98    pub const MYANMAR: Self = Self::from_bytes(b"Mymr");
99    pub const OGHAM: Self = Self::from_bytes(b"Ogam");
100    pub const RUNIC: Self = Self::from_bytes(b"Runr");
101    pub const SINHALA: Self = Self::from_bytes(b"Sinh");
102    pub const SYRIAC: Self = Self::from_bytes(b"Syrc");
103    pub const THAANA: Self = Self::from_bytes(b"Thaa");
104    pub const YI: Self = Self::from_bytes(b"Yiii");
105    // Since 3.1
106    pub const DESERET: Self = Self::from_bytes(b"Dsrt");
107    pub const GOTHIC: Self = Self::from_bytes(b"Goth");
108    pub const OLD_ITALIC: Self = Self::from_bytes(b"Ital");
109    // Since 3.2
110    pub const BUHID: Self = Self::from_bytes(b"Buhd");
111    pub const HANUNOO: Self = Self::from_bytes(b"Hano");
112    pub const TAGALOG: Self = Self::from_bytes(b"Tglg");
113    pub const TAGBANWA: Self = Self::from_bytes(b"Tagb");
114    // Since 4.0
115    pub const CYPRIOT: Self = Self::from_bytes(b"Cprt");
116    pub const LIMBU: Self = Self::from_bytes(b"Limb");
117    pub const LINEAR_B: Self = Self::from_bytes(b"Linb");
118    pub const OSMANYA: Self = Self::from_bytes(b"Osma");
119    pub const SHAVIAN: Self = Self::from_bytes(b"Shaw");
120    pub const TAI_LE: Self = Self::from_bytes(b"Tale");
121    pub const UGARITIC: Self = Self::from_bytes(b"Ugar");
122    // Since 4.1
123    pub const BUGINESE: Self = Self::from_bytes(b"Bugi");
124    pub const COPTIC: Self = Self::from_bytes(b"Copt");
125    pub const GLAGOLITIC: Self = Self::from_bytes(b"Glag");
126    pub const KHAROSHTHI: Self = Self::from_bytes(b"Khar");
127    pub const NEW_TAI_LUE: Self = Self::from_bytes(b"Talu");
128    pub const OLD_PERSIAN: Self = Self::from_bytes(b"Xpeo");
129    pub const SYLOTI_NAGRI: Self = Self::from_bytes(b"Sylo");
130    pub const TIFINAGH: Self = Self::from_bytes(b"Tfng");
131    // Since 5.0
132    pub const UNKNOWN: Self = Self::from_bytes(b"Zzzz"); // Script can be Unknown, but not Invalid.
133    pub const BALINESE: Self = Self::from_bytes(b"Bali");
134    pub const CUNEIFORM: Self = Self::from_bytes(b"Xsux");
135    pub const NKO: Self = Self::from_bytes(b"Nkoo");
136    pub const PHAGS_PA: Self = Self::from_bytes(b"Phag");
137    pub const PHOENICIAN: Self = Self::from_bytes(b"Phnx");
138    // Since 5.1
139    pub const CARIAN: Self = Self::from_bytes(b"Cari");
140    pub const CHAM: Self = Self::from_bytes(b"Cham");
141    pub const KAYAH_LI: Self = Self::from_bytes(b"Kali");
142    pub const LEPCHA: Self = Self::from_bytes(b"Lepc");
143    pub const LYCIAN: Self = Self::from_bytes(b"Lyci");
144    pub const LYDIAN: Self = Self::from_bytes(b"Lydi");
145    pub const OL_CHIKI: Self = Self::from_bytes(b"Olck");
146    pub const REJANG: Self = Self::from_bytes(b"Rjng");
147    pub const SAURASHTRA: Self = Self::from_bytes(b"Saur");
148    pub const SUNDANESE: Self = Self::from_bytes(b"Sund");
149    pub const VAI: Self = Self::from_bytes(b"Vaii");
150    // Since 5.2
151    pub const AVESTAN: Self = Self::from_bytes(b"Avst");
152    pub const BAMUM: Self = Self::from_bytes(b"Bamu");
153    pub const EGYPTIAN_HIEROGLYPHS: Self = Self::from_bytes(b"Egyp");
154    pub const IMPERIAL_ARAMAIC: Self = Self::from_bytes(b"Armi");
155    pub const INSCRIPTIONAL_PAHLAVI: Self = Self::from_bytes(b"Phli");
156    pub const INSCRIPTIONAL_PARTHIAN: Self = Self::from_bytes(b"Prti");
157    pub const JAVANESE: Self = Self::from_bytes(b"Java");
158    pub const KAITHI: Self = Self::from_bytes(b"Kthi");
159    pub const LISU: Self = Self::from_bytes(b"Lisu");
160    pub const MEETEI_MAYEK: Self = Self::from_bytes(b"Mtei");
161    pub const OLD_SOUTH_ARABIAN: Self = Self::from_bytes(b"Sarb");
162    pub const OLD_TURKIC: Self = Self::from_bytes(b"Orkh");
163    pub const SAMARITAN: Self = Self::from_bytes(b"Samr");
164    pub const TAI_THAM: Self = Self::from_bytes(b"Lana");
165    pub const TAI_VIET: Self = Self::from_bytes(b"Tavt");
166    // Since 6.0
167    pub const BATAK: Self = Self::from_bytes(b"Batk");
168    pub const BRAHMI: Self = Self::from_bytes(b"Brah");
169    pub const MANDAIC: Self = Self::from_bytes(b"Mand");
170    // Since 6.1
171    pub const CHAKMA: Self = Self::from_bytes(b"Cakm");
172    pub const MEROITIC_CURSIVE: Self = Self::from_bytes(b"Merc");
173    pub const MEROITIC_HIEROGLYPHS: Self = Self::from_bytes(b"Mero");
174    pub const MIAO: Self = Self::from_bytes(b"Plrd");
175    pub const SHARADA: Self = Self::from_bytes(b"Shrd");
176    pub const SORA_SOMPENG: Self = Self::from_bytes(b"Sora");
177    pub const TAKRI: Self = Self::from_bytes(b"Takr");
178    // Since 7.0
179    pub const BASSA_VAH: Self = Self::from_bytes(b"Bass");
180    pub const CAUCASIAN_ALBANIAN: Self = Self::from_bytes(b"Aghb");
181    pub const DUPLOYAN: Self = Self::from_bytes(b"Dupl");
182    pub const ELBASAN: Self = Self::from_bytes(b"Elba");
183    pub const GRANTHA: Self = Self::from_bytes(b"Gran");
184    pub const KHOJKI: Self = Self::from_bytes(b"Khoj");
185    pub const KHUDAWADI: Self = Self::from_bytes(b"Sind");
186    pub const LINEAR_A: Self = Self::from_bytes(b"Lina");
187    pub const MAHAJANI: Self = Self::from_bytes(b"Mahj");
188    pub const MANICHAEAN: Self = Self::from_bytes(b"Mani");
189    pub const MENDE_KIKAKUI: Self = Self::from_bytes(b"Mend");
190    pub const MODI: Self = Self::from_bytes(b"Modi");
191    pub const MRO: Self = Self::from_bytes(b"Mroo");
192    pub const NABATAEAN: Self = Self::from_bytes(b"Nbat");
193    pub const OLD_NORTH_ARABIAN: Self = Self::from_bytes(b"Narb");
194    pub const OLD_PERMIC: Self = Self::from_bytes(b"Perm");
195    pub const PAHAWH_HMONG: Self = Self::from_bytes(b"Hmng");
196    pub const PALMYRENE: Self = Self::from_bytes(b"Palm");
197    pub const PAU_CIN_HAU: Self = Self::from_bytes(b"Pauc");
198    pub const PSALTER_PAHLAVI: Self = Self::from_bytes(b"Phlp");
199    pub const SIDDHAM: Self = Self::from_bytes(b"Sidd");
200    pub const TIRHUTA: Self = Self::from_bytes(b"Tirh");
201    pub const WARANG_CITI: Self = Self::from_bytes(b"Wara");
202    // Since 8.0
203    pub const AHOM: Self = Self::from_bytes(b"Ahom");
204    pub const ANATOLIAN_HIEROGLYPHS: Self = Self::from_bytes(b"Hluw");
205    pub const HATRAN: Self = Self::from_bytes(b"Hatr");
206    pub const MULTANI: Self = Self::from_bytes(b"Mult");
207    pub const OLD_HUNGARIAN: Self = Self::from_bytes(b"Hung");
208    pub const SIGNWRITING: Self = Self::from_bytes(b"Sgnw");
209    // Since 9.0
210    pub const ADLAM: Self = Self::from_bytes(b"Adlm");
211    pub const BHAIKSUKI: Self = Self::from_bytes(b"Bhks");
212    pub const MARCHEN: Self = Self::from_bytes(b"Marc");
213    pub const OSAGE: Self = Self::from_bytes(b"Osge");
214    pub const TANGUT: Self = Self::from_bytes(b"Tang");
215    pub const NEWA: Self = Self::from_bytes(b"Newa");
216    // Since 10.0
217    pub const MASARAM_GONDI: Self = Self::from_bytes(b"Gonm");
218    pub const NUSHU: Self = Self::from_bytes(b"Nshu");
219    pub const SOYOMBO: Self = Self::from_bytes(b"Soyo");
220    pub const ZANABAZAR_SQUARE: Self = Self::from_bytes(b"Zanb");
221    // Since 11.0
222    pub const DOGRA: Self = Self::from_bytes(b"Dogr");
223    pub const GUNJALA_GONDI: Self = Self::from_bytes(b"Gong");
224    pub const HANIFI_ROHINGYA: Self = Self::from_bytes(b"Rohg");
225    pub const MAKASAR: Self = Self::from_bytes(b"Maka");
226    pub const MEDEFAIDRIN: Self = Self::from_bytes(b"Medf");
227    pub const OLD_SOGDIAN: Self = Self::from_bytes(b"Sogo");
228    pub const SOGDIAN: Self = Self::from_bytes(b"Sogd");
229    // Since 12.0
230    pub const ELYMAIC: Self = Self::from_bytes(b"Elym");
231    pub const NANDINAGARI: Self = Self::from_bytes(b"Nand");
232    pub const NYIAKENG_PUACHUE_HMONG: Self = Self::from_bytes(b"Hmnp");
233    pub const WANCHO: Self = Self::from_bytes(b"Wcho");
234    // Since 13.0
235    pub const CHORASMIAN: Self = Self::from_bytes(b"Chrs");
236    pub const DIVES_AKURU: Self = Self::from_bytes(b"Diak");
237    pub const KHITAN_SMALL_SCRIPT: Self = Self::from_bytes(b"Kits");
238    pub const YEZIDI: Self = Self::from_bytes(b"Yezi");
239    // Since 14.0
240    pub const CYPRO_MINOAN: Self = Self::from_bytes(b"Cpmn");
241    pub const OLD_UYGHUR: Self = Self::from_bytes(b"Ougr");
242    pub const TANGSA: Self = Self::from_bytes(b"Tnsa");
243    pub const TOTO: Self = Self::from_bytes(b"Toto");
244    pub const VITHKUQI: Self = Self::from_bytes(b"Vith");
245    // Since 15.0
246    pub const KAWI: Self = Self::from_bytes(b"Kawi");
247    pub const NAG_MUNDARI: Self = Self::from_bytes(b"Nagm");
248    // Since 16.0
249    pub const GARAY: Self = Self::from_bytes(b"Gara");
250    pub const GURUNG_KHEMA: Self = Self::from_bytes(b"Gukh");
251    pub const KIRAT_RAI: Self = Self::from_bytes(b"Krai");
252    pub const OL_ONAL: Self = Self::from_bytes(b"Onao");
253    pub const SUNUWAR: Self = Self::from_bytes(b"Sunu");
254    pub const TODHRI: Self = Self::from_bytes(b"Todr");
255    pub const TULU_TIGALARI: Self = Self::from_bytes(b"Tutg");
256    // Since 17.0
257    pub const BERIA_ERFE: Self = Self::from_bytes(b"Berf");
258    pub const SIDETIC: Self = Self::from_bytes(b"Sidt");
259    pub const TAI_YO: Self = Self::from_bytes(b"Tayo");
260    pub const TOLONG_SIKI: Self = Self::from_bytes(b"Tols");
261    // Since 18.0
262    pub const JURCHEN: Self = Self::from_bytes(b"Jurc");
263    pub const PROTO_CUNEIFORM: Self = Self::from_bytes(b"Pcun");
264    pub const SEAL: Self = Self::from_bytes(b"Seal");
265
266    pub const MATH: Self = Self::from_bytes(b"Zmth");
267
268    // https://github.com/harfbuzz/harfbuzz/issues/1162
269    pub const MYANMAR_ZAWGYI: Self = Self::from_bytes(b"Qaag");
270}