Skip to main content

ttf_view/platform/
mod.rs

1mod encodings;
2mod languages;
3
4pub use encodings::*;
5pub use languages::*;
6
7macro_rules! define_u16_ids {
8    ($(
9        $(#[$outer:meta])*
10        $vis:vis enum $Name:ident {
11            $($variant:ident = $value:expr),* $(,)?
12        }
13    )+) => ($(
14        $(#[$outer])*
15        #[derive(Copy, Hash)]
16        #[derive_const(Clone, PartialEq, Eq, PartialOrd, Ord)]
17        #[repr(u16)]
18        $vis enum $Name {
19            $( $variant = $value, )*
20        }
21
22        impl $Name {
23            pub const fn new(value: u16) -> Option<Self> {
24                Some(match value {
25                    $( $value => Self::$variant, )*
26                    _ => return None,
27                })
28            }
29            pub const fn get(&self) -> u16 {
30                *self as u16
31            }
32            pub const fn name(&self) -> &'static str {
33                match *self {
34                    $( Self::$variant => stringify!($variant), )*
35                }
36            }
37        }
38
39        impl std::fmt::Debug for $Name {
40            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41                write!(f, "{} ({})", self.get(), self.name())
42            }
43        }
44    )+);
45}
46
47pub(crate) use define_u16_ids;
48
49define_u16_ids! {
50    pub enum PlatformId {
51        Unicode = 0, Macintosh = 1, Iso = 2, Windows = 3, Custom = 4,
52    }
53}
54
55impl PlatformId {
56    pub const fn encoding(&self, enc_id: u16) -> Option<EncodingId> {
57        Some(match self {
58            Self::Unicode => EncodingId::Unicode(UnicodeEncodingId::new(enc_id)?),
59            Self::Macintosh => EncodingId::Macintosh(MacintoshEncodingId::new(enc_id)?),
60            Self::Iso => EncodingId::Iso(IsoEncodingId::new(enc_id)?),
61            Self::Windows => EncodingId::Windows(WindowsEncodingId::new(enc_id)?),
62            Self::Custom => EncodingId::Custom(enc_id.try_into().ok()?),
63        })
64    }
65
66    pub const fn language(&self, lang_id: u16) -> Option<LanguageId> {
67        if lang_id >= 0x8000 {
68            return Some(LanguageId::Tagged(lang_id));
69        }
70        Some(match self {
71            Self::Macintosh => LanguageId::Macintosh(lang_id),
72            Self::Windows => LanguageId::Windows(lang_id),
73            _ => return None,
74        })
75    }
76}