1use core::{
2 ops::{Bound, RangeBounds},
3 str::FromStr,
4};
5use smallvec::SmallVec;
6
7use read_fonts::types::Tag;
8
9use super::text_parser::TextParser;
10
11pub const HB_FEATURE_GLOBAL_START: u32 = 0;
12pub const HB_FEATURE_GLOBAL_END: u32 = u32::MAX;
13
14#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
16pub enum Direction {
17 Invalid,
19 LeftToRight,
21 RightToLeft,
23 TopToBottom,
25 BottomToTop,
27}
28
29impl Direction {
30 #[inline]
31 pub(crate) fn is_horizontal(self) -> bool {
32 match self {
33 Direction::Invalid => false,
34 Direction::LeftToRight => true,
35 Direction::RightToLeft => true,
36 Direction::TopToBottom => false,
37 Direction::BottomToTop => false,
38 }
39 }
40
41 #[inline]
42 pub(crate) fn is_vertical(self) -> bool {
43 !self.is_horizontal()
44 }
45
46 #[inline]
47 pub(crate) fn is_forward(self) -> bool {
48 match self {
49 Direction::Invalid => false,
50 Direction::LeftToRight => true,
51 Direction::RightToLeft => false,
52 Direction::TopToBottom => true,
53 Direction::BottomToTop => false,
54 }
55 }
56
57 #[inline]
58 pub(crate) fn is_backward(self) -> bool {
59 !self.is_forward()
60 }
61
62 #[inline]
63 pub(crate) fn reverse(self) -> Self {
64 match self {
65 Direction::Invalid => Direction::Invalid,
66 Direction::LeftToRight => Direction::RightToLeft,
67 Direction::RightToLeft => Direction::LeftToRight,
68 Direction::TopToBottom => Direction::BottomToTop,
69 Direction::BottomToTop => Direction::TopToBottom,
70 }
71 }
72
73 pub(crate) fn from_script(script: Script) -> Option<Self> {
74 match script {
77 script::ARABIC |
79 script::HEBREW |
80
81 script::SYRIAC |
83 script::THAANA |
84
85 script::CYPRIOT |
87
88 script::KHAROSHTHI |
90
91 script::PHOENICIAN |
93 script::NKO |
94
95 script::LYDIAN |
97
98 script::AVESTAN |
100 script::IMPERIAL_ARAMAIC |
101 script::INSCRIPTIONAL_PAHLAVI |
102 script::INSCRIPTIONAL_PARTHIAN |
103 script::OLD_SOUTH_ARABIAN |
104 script::OLD_TURKIC |
105 script::SAMARITAN |
106
107 script::MANDAIC |
109
110 script::MEROITIC_CURSIVE |
112 script::MEROITIC_HIEROGLYPHS |
113
114 script::MANICHAEAN |
116 script::MENDE_KIKAKUI |
117 script::NABATAEAN |
118 script::OLD_NORTH_ARABIAN |
119 script::PALMYRENE |
120 script::PSALTER_PAHLAVI |
121
122 script::HATRAN |
124
125 script::ADLAM |
127
128 script::HANIFI_ROHINGYA |
130 script::OLD_SOGDIAN |
131 script::SOGDIAN |
132
133 script::ELYMAIC |
135
136 script::CHORASMIAN |
138 script::YEZIDI |
139
140 script::OLD_UYGHUR |
142
143 script::GARAY |
145
146 script::SIDETIC => {
148 Some(Direction::RightToLeft)
149 }
150
151 script::OLD_HUNGARIAN |
153 script::OLD_ITALIC |
154 script::RUNIC |
155 script::TIFINAGH => {
156 None
157 }
158
159 _ => Some(Direction::LeftToRight),
160 }
161 }
162}
163
164impl Default for Direction {
165 #[inline]
166 fn default() -> Self {
167 Direction::Invalid
168 }
169}
170
171impl FromStr for Direction {
172 type Err = &'static str;
173
174 fn from_str(s: &str) -> Result<Self, Self::Err> {
175 if s.is_empty() {
176 return Err("invalid direction");
177 }
178
179 match s.as_bytes()[0].to_ascii_lowercase() {
181 b'l' => Ok(Direction::LeftToRight),
182 b'r' => Ok(Direction::RightToLeft),
183 b't' => Ok(Direction::TopToBottom),
184 b'b' => Ok(Direction::BottomToTop),
185 _ => Err("invalid direction"),
186 }
187 }
188}
189
190type SmallVecLanguage = SmallVec<[u8; 8]>;
191
192#[derive(Clone, PartialEq, Eq, Hash, Debug)]
194pub struct Language(SmallVecLanguage);
195
196impl Language {
197 #[inline]
199 pub fn as_str(&self) -> &str {
200 core::str::from_utf8(&self.0).unwrap_or_default()
201 }
202
203 fn from_bytes(bytes: &[u8]) -> Self {
204 if bytes.is_empty() {
205 Language(SmallVec::new())
206 } else {
207 let mut bytes = SmallVecLanguage::from_slice(bytes);
208
209 for b in &mut bytes.iter_mut() {
211 if b.is_ascii_uppercase() {
212 *b = b.to_ascii_lowercase();
213 } else if *b == b'_' {
214 *b = b'-';
215 }
216 }
217
218 Language(bytes)
219 }
220 }
221}
222
223impl FromStr for Language {
224 type Err = &'static str;
225
226 fn from_str(s: &str) -> Result<Self, Self::Err> {
227 if !s.is_empty() {
228 Ok(Language::from_bytes(s.as_bytes()))
229 } else {
230 Err("invalid language")
231 }
232 }
233}
234
235#[allow(missing_docs)]
240#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
241pub struct Script(pub(crate) Tag);
242
243impl Script {
244 #[inline]
245 pub(crate) const fn from_bytes(bytes: &[u8; 4]) -> Self {
246 Script(Tag::new(bytes))
247 }
248
249 pub const fn from_iso15924_tag(tag: Tag) -> Option<Script> {
251 let tag = u32::from_be_bytes(tag.to_be_bytes());
252
253 if tag == 0 {
254 return None;
255 }
256
257 let tag = (tag & 0xDFDF_DFDF) | 0x0020_2020;
259
260 if tag & 0xE0E0_E0E0 != 0x4060_6060 {
261 return Some(script::UNKNOWN);
262 }
263
264 Some(match &tag.to_be_bytes() {
265 b"Qaai" => script::INHERITED,
269 b"Qaac" => script::COPTIC,
270
271 b"Aran" => script::ARABIC,
273 b"Cyrs" => script::CYRILLIC,
274 b"Geok" => script::GEORGIAN,
275 b"Hans" | b"Hant" => script::HAN,
276 b"Jamo" => script::HANGUL,
277 b"Latf" | b"Latg" => script::LATIN,
278 b"Syre" | b"Syrj" | b"Syrn" => script::SYRIAC,
279 &t => Script(Tag::from_be_bytes(t)),
280 })
281 }
282
283 #[inline]
285 pub fn tag(&self) -> Tag {
286 self.0
287 }
288}
289
290impl FromStr for Script {
291 type Err = &'static str;
292
293 fn from_str(s: &str) -> Result<Self, Self::Err> {
294 let tag = Tag::from_bytes_lossy(s.as_bytes());
295 Script::from_iso15924_tag(tag).ok_or("invalid script")
296 }
297}
298
299pub mod script {
301 #![allow(missing_docs)]
302
303 use crate::Script;
304
305 pub const COMMON: Script = Script::from_bytes(b"Zyyy");
307 pub const INHERITED: Script = Script::from_bytes(b"Zinh");
308 pub const ARABIC: Script = Script::from_bytes(b"Arab");
309 pub const ARMENIAN: Script = Script::from_bytes(b"Armn");
310 pub const BENGALI: Script = Script::from_bytes(b"Beng");
311 pub const CYRILLIC: Script = Script::from_bytes(b"Cyrl");
312 pub const DEVANAGARI: Script = Script::from_bytes(b"Deva");
313 pub const GEORGIAN: Script = Script::from_bytes(b"Geor");
314 pub const GREEK: Script = Script::from_bytes(b"Grek");
315 pub const GUJARATI: Script = Script::from_bytes(b"Gujr");
316 pub const GURMUKHI: Script = Script::from_bytes(b"Guru");
317 pub const HANGUL: Script = Script::from_bytes(b"Hang");
318 pub const HAN: Script = Script::from_bytes(b"Hani");
319 pub const HEBREW: Script = Script::from_bytes(b"Hebr");
320 pub const HIRAGANA: Script = Script::from_bytes(b"Hira");
321 pub const KANNADA: Script = Script::from_bytes(b"Knda");
322 pub const KATAKANA: Script = Script::from_bytes(b"Kana");
323 pub const LAO: Script = Script::from_bytes(b"Laoo");
324 pub const LATIN: Script = Script::from_bytes(b"Latn");
325 pub const MALAYALAM: Script = Script::from_bytes(b"Mlym");
326 pub const ORIYA: Script = Script::from_bytes(b"Orya");
327 pub const TAMIL: Script = Script::from_bytes(b"Taml");
328 pub const TELUGU: Script = Script::from_bytes(b"Telu");
329 pub const THAI: Script = Script::from_bytes(b"Thai");
330 pub const TIBETAN: Script = Script::from_bytes(b"Tibt");
332 pub const BOPOMOFO: Script = Script::from_bytes(b"Bopo");
334 pub const BRAILLE: Script = Script::from_bytes(b"Brai");
335 pub const CANADIAN_SYLLABICS: Script = Script::from_bytes(b"Cans");
336 pub const CHEROKEE: Script = Script::from_bytes(b"Cher");
337 pub const ETHIOPIC: Script = Script::from_bytes(b"Ethi");
338 pub const KHMER: Script = Script::from_bytes(b"Khmr");
339 pub const MONGOLIAN: Script = Script::from_bytes(b"Mong");
340 pub const MYANMAR: Script = Script::from_bytes(b"Mymr");
341 pub const OGHAM: Script = Script::from_bytes(b"Ogam");
342 pub const RUNIC: Script = Script::from_bytes(b"Runr");
343 pub const SINHALA: Script = Script::from_bytes(b"Sinh");
344 pub const SYRIAC: Script = Script::from_bytes(b"Syrc");
345 pub const THAANA: Script = Script::from_bytes(b"Thaa");
346 pub const YI: Script = Script::from_bytes(b"Yiii");
347 pub const DESERET: Script = Script::from_bytes(b"Dsrt");
349 pub const GOTHIC: Script = Script::from_bytes(b"Goth");
350 pub const OLD_ITALIC: Script = Script::from_bytes(b"Ital");
351 pub const BUHID: Script = Script::from_bytes(b"Buhd");
353 pub const HANUNOO: Script = Script::from_bytes(b"Hano");
354 pub const TAGALOG: Script = Script::from_bytes(b"Tglg");
355 pub const TAGBANWA: Script = Script::from_bytes(b"Tagb");
356 pub const CYPRIOT: Script = Script::from_bytes(b"Cprt");
358 pub const LIMBU: Script = Script::from_bytes(b"Limb");
359 pub const LINEAR_B: Script = Script::from_bytes(b"Linb");
360 pub const OSMANYA: Script = Script::from_bytes(b"Osma");
361 pub const SHAVIAN: Script = Script::from_bytes(b"Shaw");
362 pub const TAI_LE: Script = Script::from_bytes(b"Tale");
363 pub const UGARITIC: Script = Script::from_bytes(b"Ugar");
364 pub const BUGINESE: Script = Script::from_bytes(b"Bugi");
366 pub const COPTIC: Script = Script::from_bytes(b"Copt");
367 pub const GLAGOLITIC: Script = Script::from_bytes(b"Glag");
368 pub const KHAROSHTHI: Script = Script::from_bytes(b"Khar");
369 pub const NEW_TAI_LUE: Script = Script::from_bytes(b"Talu");
370 pub const OLD_PERSIAN: Script = Script::from_bytes(b"Xpeo");
371 pub const SYLOTI_NAGRI: Script = Script::from_bytes(b"Sylo");
372 pub const TIFINAGH: Script = Script::from_bytes(b"Tfng");
373 pub const UNKNOWN: Script = Script::from_bytes(b"Zzzz"); pub const BALINESE: Script = Script::from_bytes(b"Bali");
376 pub const CUNEIFORM: Script = Script::from_bytes(b"Xsux");
377 pub const NKO: Script = Script::from_bytes(b"Nkoo");
378 pub const PHAGS_PA: Script = Script::from_bytes(b"Phag");
379 pub const PHOENICIAN: Script = Script::from_bytes(b"Phnx");
380 pub const CARIAN: Script = Script::from_bytes(b"Cari");
382 pub const CHAM: Script = Script::from_bytes(b"Cham");
383 pub const KAYAH_LI: Script = Script::from_bytes(b"Kali");
384 pub const LEPCHA: Script = Script::from_bytes(b"Lepc");
385 pub const LYCIAN: Script = Script::from_bytes(b"Lyci");
386 pub const LYDIAN: Script = Script::from_bytes(b"Lydi");
387 pub const OL_CHIKI: Script = Script::from_bytes(b"Olck");
388 pub const REJANG: Script = Script::from_bytes(b"Rjng");
389 pub const SAURASHTRA: Script = Script::from_bytes(b"Saur");
390 pub const SUNDANESE: Script = Script::from_bytes(b"Sund");
391 pub const VAI: Script = Script::from_bytes(b"Vaii");
392 pub const AVESTAN: Script = Script::from_bytes(b"Avst");
394 pub const BAMUM: Script = Script::from_bytes(b"Bamu");
395 pub const EGYPTIAN_HIEROGLYPHS: Script = Script::from_bytes(b"Egyp");
396 pub const IMPERIAL_ARAMAIC: Script = Script::from_bytes(b"Armi");
397 pub const INSCRIPTIONAL_PAHLAVI: Script = Script::from_bytes(b"Phli");
398 pub const INSCRIPTIONAL_PARTHIAN: Script = Script::from_bytes(b"Prti");
399 pub const JAVANESE: Script = Script::from_bytes(b"Java");
400 pub const KAITHI: Script = Script::from_bytes(b"Kthi");
401 pub const LISU: Script = Script::from_bytes(b"Lisu");
402 pub const MEETEI_MAYEK: Script = Script::from_bytes(b"Mtei");
403 pub const OLD_SOUTH_ARABIAN: Script = Script::from_bytes(b"Sarb");
404 pub const OLD_TURKIC: Script = Script::from_bytes(b"Orkh");
405 pub const SAMARITAN: Script = Script::from_bytes(b"Samr");
406 pub const TAI_THAM: Script = Script::from_bytes(b"Lana");
407 pub const TAI_VIET: Script = Script::from_bytes(b"Tavt");
408 pub const BATAK: Script = Script::from_bytes(b"Batk");
410 pub const BRAHMI: Script = Script::from_bytes(b"Brah");
411 pub const MANDAIC: Script = Script::from_bytes(b"Mand");
412 pub const CHAKMA: Script = Script::from_bytes(b"Cakm");
414 pub const MEROITIC_CURSIVE: Script = Script::from_bytes(b"Merc");
415 pub const MEROITIC_HIEROGLYPHS: Script = Script::from_bytes(b"Mero");
416 pub const MIAO: Script = Script::from_bytes(b"Plrd");
417 pub const SHARADA: Script = Script::from_bytes(b"Shrd");
418 pub const SORA_SOMPENG: Script = Script::from_bytes(b"Sora");
419 pub const TAKRI: Script = Script::from_bytes(b"Takr");
420 pub const BASSA_VAH: Script = Script::from_bytes(b"Bass");
422 pub const CAUCASIAN_ALBANIAN: Script = Script::from_bytes(b"Aghb");
423 pub const DUPLOYAN: Script = Script::from_bytes(b"Dupl");
424 pub const ELBASAN: Script = Script::from_bytes(b"Elba");
425 pub const GRANTHA: Script = Script::from_bytes(b"Gran");
426 pub const KHOJKI: Script = Script::from_bytes(b"Khoj");
427 pub const KHUDAWADI: Script = Script::from_bytes(b"Sind");
428 pub const LINEAR_A: Script = Script::from_bytes(b"Lina");
429 pub const MAHAJANI: Script = Script::from_bytes(b"Mahj");
430 pub const MANICHAEAN: Script = Script::from_bytes(b"Mani");
431 pub const MENDE_KIKAKUI: Script = Script::from_bytes(b"Mend");
432 pub const MODI: Script = Script::from_bytes(b"Modi");
433 pub const MRO: Script = Script::from_bytes(b"Mroo");
434 pub const NABATAEAN: Script = Script::from_bytes(b"Nbat");
435 pub const OLD_NORTH_ARABIAN: Script = Script::from_bytes(b"Narb");
436 pub const OLD_PERMIC: Script = Script::from_bytes(b"Perm");
437 pub const PAHAWH_HMONG: Script = Script::from_bytes(b"Hmng");
438 pub const PALMYRENE: Script = Script::from_bytes(b"Palm");
439 pub const PAU_CIN_HAU: Script = Script::from_bytes(b"Pauc");
440 pub const PSALTER_PAHLAVI: Script = Script::from_bytes(b"Phlp");
441 pub const SIDDHAM: Script = Script::from_bytes(b"Sidd");
442 pub const TIRHUTA: Script = Script::from_bytes(b"Tirh");
443 pub const WARANG_CITI: Script = Script::from_bytes(b"Wara");
444 pub const AHOM: Script = Script::from_bytes(b"Ahom");
446 pub const ANATOLIAN_HIEROGLYPHS: Script = Script::from_bytes(b"Hluw");
447 pub const HATRAN: Script = Script::from_bytes(b"Hatr");
448 pub const MULTANI: Script = Script::from_bytes(b"Mult");
449 pub const OLD_HUNGARIAN: Script = Script::from_bytes(b"Hung");
450 pub const SIGNWRITING: Script = Script::from_bytes(b"Sgnw");
451 pub const ADLAM: Script = Script::from_bytes(b"Adlm");
453 pub const BHAIKSUKI: Script = Script::from_bytes(b"Bhks");
454 pub const MARCHEN: Script = Script::from_bytes(b"Marc");
455 pub const OSAGE: Script = Script::from_bytes(b"Osge");
456 pub const TANGUT: Script = Script::from_bytes(b"Tang");
457 pub const NEWA: Script = Script::from_bytes(b"Newa");
458 pub const MASARAM_GONDI: Script = Script::from_bytes(b"Gonm");
460 pub const NUSHU: Script = Script::from_bytes(b"Nshu");
461 pub const SOYOMBO: Script = Script::from_bytes(b"Soyo");
462 pub const ZANABAZAR_SQUARE: Script = Script::from_bytes(b"Zanb");
463 pub const DOGRA: Script = Script::from_bytes(b"Dogr");
465 pub const GUNJALA_GONDI: Script = Script::from_bytes(b"Gong");
466 pub const HANIFI_ROHINGYA: Script = Script::from_bytes(b"Rohg");
467 pub const MAKASAR: Script = Script::from_bytes(b"Maka");
468 pub const MEDEFAIDRIN: Script = Script::from_bytes(b"Medf");
469 pub const OLD_SOGDIAN: Script = Script::from_bytes(b"Sogo");
470 pub const SOGDIAN: Script = Script::from_bytes(b"Sogd");
471 pub const ELYMAIC: Script = Script::from_bytes(b"Elym");
473 pub const NANDINAGARI: Script = Script::from_bytes(b"Nand");
474 pub const NYIAKENG_PUACHUE_HMONG: Script = Script::from_bytes(b"Hmnp");
475 pub const WANCHO: Script = Script::from_bytes(b"Wcho");
476 pub const CHORASMIAN: Script = Script::from_bytes(b"Chrs");
478 pub const DIVES_AKURU: Script = Script::from_bytes(b"Diak");
479 pub const KHITAN_SMALL_SCRIPT: Script = Script::from_bytes(b"Kits");
480 pub const YEZIDI: Script = Script::from_bytes(b"Yezi");
481 pub const CYPRO_MINOAN: Script = Script::from_bytes(b"Cpmn");
483 pub const OLD_UYGHUR: Script = Script::from_bytes(b"Ougr");
484 pub const TANGSA: Script = Script::from_bytes(b"Tnsa");
485 pub const TOTO: Script = Script::from_bytes(b"Toto");
486 pub const VITHKUQI: Script = Script::from_bytes(b"Vith");
487 pub const KAWI: Script = Script::from_bytes(b"Kawi");
489 pub const NAG_MUNDARI: Script = Script::from_bytes(b"Nagm");
490 pub const GARAY: Script = Script::from_bytes(b"Gara");
492 pub const GURUNG_KHEMA: Script = Script::from_bytes(b"Gukh");
493 pub const KIRAT_RAI: Script = Script::from_bytes(b"Krai");
494 pub const OL_ONAL: Script = Script::from_bytes(b"Onao");
495 pub const SUNUWAR: Script = Script::from_bytes(b"Sunu");
496 pub const TODHRI: Script = Script::from_bytes(b"Todr");
497 pub const TULU_TIGALARI: Script = Script::from_bytes(b"Tutg");
498 pub const BERIA_ERFE: Script = Script::from_bytes(b"Berf");
500 pub const SIDETIC: Script = Script::from_bytes(b"Sidt");
501 pub const TAI_YO: Script = Script::from_bytes(b"Tayo");
502 pub const TOLONG_SIKI: Script = Script::from_bytes(b"Tols");
503
504 pub const MATH: Script = Script::from_bytes(b"Zmth");
505
506 pub const MYANMAR_ZAWGYI: Script = Script::from_bytes(b"Qaag");
508}
509
510#[repr(C)]
513#[allow(missing_docs)]
514#[derive(Clone, Copy, PartialEq, Hash, Debug)]
515pub struct Feature {
516 pub tag: Tag,
517 pub value: u32,
518 pub start: u32,
519 pub end: u32,
520}
521
522impl Feature {
523 pub fn new(tag: Tag, value: u32, range: impl RangeBounds<usize>) -> Feature {
525 let max = u32::MAX as usize;
526 let start = match range.start_bound() {
527 Bound::Included(&included) => included.min(max) as u32,
528 Bound::Excluded(&excluded) => excluded.min(max - 1) as u32 + 1,
529 Bound::Unbounded => 0,
530 };
531 let end = match range.end_bound() {
532 Bound::Included(&included) => included.min(max) as u32,
533 Bound::Excluded(&excluded) => excluded.saturating_sub(1).min(max) as u32,
534 Bound::Unbounded => max as u32,
535 };
536
537 Feature {
538 tag,
539 value,
540 start,
541 end,
542 }
543 }
544
545 pub(crate) fn is_global(&self) -> bool {
546 self.start == 0 && self.end == u32::MAX
547 }
548}
549
550impl FromStr for Feature {
551 type Err = &'static str;
552
553 fn from_str(s: &str) -> Result<Self, Self::Err> {
571 fn parse(s: &str) -> Option<Feature> {
572 if s.is_empty() {
573 return None;
574 }
575
576 let mut p = TextParser::new(s);
577
578 let mut value = 1;
580 match p.curr_byte()? {
581 b'-' => {
582 value = 0;
583 p.advance(1);
584 }
585 b'+' => {
586 value = 1;
587 p.advance(1);
588 }
589 _ => {}
590 }
591
592 p.skip_spaces();
594 let quote = p.consume_quote();
595
596 let tag = p.consume_tag()?;
597
598 if let Some(quote) = quote {
600 p.consume_byte(quote)?;
601 }
602
603 p.skip_spaces();
605
606 let (start, end) = if p.consume_byte(b'[').is_some() {
607 let start_opt = p.consume_i32();
608 let start = start_opt.unwrap_or(0) as u32; let end = if matches!(p.curr_byte(), Some(b':') | Some(b';')) {
611 p.advance(1);
612 p.consume_i32().unwrap_or(-1) as u32 } else {
614 if start_opt.is_some() && start != u32::MAX {
615 start + 1
616 } else {
617 u32::MAX
618 }
619 };
620
621 p.consume_byte(b']')?;
622
623 (start, end)
624 } else {
625 (0, u32::MAX)
626 };
627
628 let had_equal = p.consume_byte(b'=').is_some();
630 let value1 = p
631 .consume_i32()
632 .or_else(|| p.consume_bool().map(|b| b as i32));
633
634 if had_equal && value1.is_none() {
635 return None;
636 };
637
638 if let Some(value1) = value1 {
639 value = value1 as u32; }
641
642 p.skip_spaces();
643
644 if !p.at_end() {
645 return None;
646 }
647
648 Some(Feature {
649 tag,
650 value,
651 start,
652 end,
653 })
654 }
655
656 parse(s).ok_or("invalid feature")
657 }
658}
659
660#[cfg(test)]
661mod tests_features {
662 use super::*;
663 use core::str::FromStr;
664
665 macro_rules! test {
666 ($name:ident, $text:expr, $tag:expr, $value:expr, $range:expr) => {
667 #[test]
668 fn $name() {
669 assert_eq!(
670 Feature::from_str($text).unwrap(),
671 Feature::new(Tag::new($tag), $value, $range)
672 );
673 }
674 };
675 }
676
677 test!(parse_01, "kern", b"kern", 1, ..);
678 test!(parse_02, "+kern", b"kern", 1, ..);
679 test!(parse_03, "-kern", b"kern", 0, ..);
680 test!(parse_04, "kern=0", b"kern", 0, ..);
681 test!(parse_05, "kern=1", b"kern", 1, ..);
682 test!(parse_06, "kern=2", b"kern", 2, ..);
683 test!(parse_07, "kern[]", b"kern", 1, ..);
684 test!(parse_08, "kern[:]", b"kern", 1, ..);
685 test!(parse_09, "kern[5:]", b"kern", 1, 5..);
686 test!(parse_10, "kern[:5]", b"kern", 1, ..=5);
687 test!(parse_11, "kern[3:5]", b"kern", 1, 3..=5);
688 test!(parse_12, "kern[3]", b"kern", 1, 3..=4);
689 test!(parse_13, "kern[3:5]=2", b"kern", 2, 3..=5);
690 test!(parse_14, "kern[3;5]=2", b"kern", 2, 3..=5);
691 test!(parse_15, "kern[:-1]", b"kern", 1, ..);
692 test!(parse_16, "kern[-1]", b"kern", 1, u32::MAX as usize..);
693 test!(parse_17, "kern=on", b"kern", 1, ..);
694 test!(parse_18, "kern=off", b"kern", 0, ..);
695 test!(parse_19, "kern=oN", b"kern", 1, ..);
696 test!(parse_20, "kern=oFf", b"kern", 0, ..);
697}
698
699#[repr(C)]
701#[allow(missing_docs)]
702#[derive(Clone, Copy, PartialEq, Debug)]
703pub struct Variation {
704 pub tag: Tag,
705 pub value: f32,
706}
707
708impl FromStr for Variation {
709 type Err = &'static str;
710
711 fn from_str(s: &str) -> Result<Self, Self::Err> {
712 fn parse(s: &str) -> Option<Variation> {
713 if s.is_empty() {
714 return None;
715 }
716
717 let mut p = TextParser::new(s);
718
719 p.skip_spaces();
721 let quote = p.consume_quote();
722
723 let tag = p.consume_tag()?;
724
725 if let Some(quote) = quote {
727 p.consume_byte(quote)?;
728 }
729
730 let _ = p.consume_byte(b'=');
731 let value = p.consume_f32()?;
732 p.skip_spaces();
733
734 if !p.at_end() {
735 return None;
736 }
737
738 Some(Variation { tag, value })
739 }
740
741 parse(s).ok_or("invalid variation")
742 }
743}
744
745impl From<&Variation> for Variation {
748 fn from(value: &Variation) -> Self {
749 *value
750 }
751}
752
753impl From<(&str, f32)> for Variation {
754 fn from(value: (&str, f32)) -> Self {
755 Self {
756 tag: Tag::from_str(value.0).unwrap_or_default(),
757 value: value.1,
758 }
759 }
760}
761
762impl From<&(&str, f32)> for Variation {
763 fn from(value: &(&str, f32)) -> Self {
764 (*value).into()
765 }
766}
767
768impl From<(Tag, f32)> for Variation {
769 fn from(value: (Tag, f32)) -> Self {
770 Self {
771 tag: value.0,
772 value: value.1,
773 }
774 }
775}
776
777impl From<&(Tag, f32)> for Variation {
778 fn from(value: &(Tag, f32)) -> Self {
779 (*value).into()
780 }
781}
782
783pub trait TagExt {
784 fn from_bytes_lossy(bytes: &[u8]) -> Self;
785 fn as_u32(self) -> u32;
786 fn is_null(self) -> bool;
787 fn default_script() -> Self;
788 fn default_language() -> Self;
789 #[cfg(test)]
790 fn to_lowercase(&self) -> Self;
791 fn to_uppercase(&self) -> Self;
792}
793
794impl TagExt for Tag {
795 fn from_bytes_lossy(bytes: &[u8]) -> Self {
796 let mut array = [b' '; 4];
797 for (src, dest) in bytes.iter().zip(&mut array) {
798 *dest = *src;
799 }
800 Tag::new(&array)
801 }
802
803 fn as_u32(self) -> u32 {
804 u32::from_be_bytes(self.to_be_bytes())
805 }
806
807 fn is_null(self) -> bool {
808 self.to_be_bytes() == [0, 0, 0, 0]
809 }
810
811 #[inline]
812 fn default_script() -> Self {
813 Tag::new(b"DFLT")
814 }
815
816 #[inline]
817 fn default_language() -> Self {
818 Tag::new(b"dflt")
819 }
820
821 #[cfg(test)]
823 #[inline]
824 fn to_lowercase(&self) -> Self {
825 let b = self.to_be_bytes();
826 Tag::new(&[
827 b[0].to_ascii_lowercase(),
828 b[1].to_ascii_lowercase(),
829 b[2].to_ascii_lowercase(),
830 b[3].to_ascii_lowercase(),
831 ])
832 }
833
834 #[inline]
836 fn to_uppercase(&self) -> Self {
837 let b = self.to_be_bytes();
838 Tag::new(&[
839 b[0].to_ascii_uppercase(),
840 b[1].to_ascii_uppercase(),
841 b[2].to_ascii_uppercase(),
842 b[3].to_ascii_uppercase(),
843 ])
844 }
845}