Skip to main content

font_parse/
tag.rs

1//! Known SFNT table tags and the [`TableTag`] lookup enum.
2
3use crate::error::{ParseError, RawTag};
4use core::fmt;
5
6/// Re-exported from `font-types`: a validated four-byte font tag.
7pub use font_types::Tag;
8
9/// The subset of SFNT table tags this parser knows by name.
10///
11/// Typed accessors on [`crate::FontRef`] accept this enum; arbitrary
12/// (unknown) tags are reachable through [`crate::FontRef::table_by_tag`] with
13/// a raw [`Tag`].
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15#[doc(alias = "tag")]
16pub enum TableTag {
17    /// Character-to-glyph mapping.
18    Cmap,
19    /// Font header.
20    Head,
21    /// Horizontal header.
22    Hhea,
23    /// Horizontal metrics.
24    Hmtx,
25    /// Maximum profile.
26    Maxp,
27    /// Naming table.
28    Name,
29    /// OS/2 — Windows metrics.
30    Os2,
31    /// PostScript table.
32    Post,
33    /// Vertical header.
34    Vhea,
35    /// Vertical metrics.
36    Vmtx,
37    /// Control value table.
38    Cvt,
39    /// Font program.
40    Fpgm,
41    /// CVT program.
42    Prep,
43    /// Glyph data (TrueType outlines).
44    Glyf,
45    /// Index to location.
46    Loca,
47    /// Compact Font Format (CFF) outlines.
48    Cff,
49    /// Font variations — axis data.
50    Fvar,
51    /// Font variations — axis segment maps.
52    Avar,
53    /// Font variations — glyph deltas.
54    Gvar,
55    /// Glyph substitution.
56    Gsub,
57    /// Glyph positioning.
58    Gpos,
59    /// Glyph definition.
60    Gdef,
61    /// Style attributes.
62    Stat,
63    /// Digital signature.
64    Dsig,
65    /// Baseline table.
66    Base,
67    /// Kerning.
68    Kern,
69}
70
71impl TableTag {
72    /// Every tag this parser names, in declaration order.
73    pub const ALL: [TableTag; 26] = [
74        TableTag::Cmap,
75        TableTag::Head,
76        TableTag::Hhea,
77        TableTag::Hmtx,
78        TableTag::Maxp,
79        TableTag::Name,
80        TableTag::Os2,
81        TableTag::Post,
82        TableTag::Vhea,
83        TableTag::Vmtx,
84        TableTag::Cvt,
85        TableTag::Fpgm,
86        TableTag::Prep,
87        TableTag::Glyf,
88        TableTag::Loca,
89        TableTag::Cff,
90        TableTag::Fvar,
91        TableTag::Avar,
92        TableTag::Gvar,
93        TableTag::Gsub,
94        TableTag::Gpos,
95        TableTag::Gdef,
96        TableTag::Stat,
97        TableTag::Dsig,
98        TableTag::Base,
99        TableTag::Kern,
100    ];
101
102    /// The raw four-byte representation.
103    #[must_use]
104    pub const fn to_bytes(self) -> RawTag {
105        match self {
106            TableTag::Cmap => *b"cmap",
107            TableTag::Head => *b"head",
108            TableTag::Hhea => *b"hhea",
109            TableTag::Hmtx => *b"hmtx",
110            TableTag::Maxp => *b"maxp",
111            TableTag::Name => *b"name",
112            TableTag::Os2 => *b"OS/2",
113            TableTag::Post => *b"post",
114            TableTag::Vhea => *b"vhea",
115            TableTag::Vmtx => *b"vmtx",
116            TableTag::Cvt => *b"cvt ",
117            TableTag::Fpgm => *b"fpgm",
118            TableTag::Prep => *b"prep",
119            TableTag::Glyf => *b"glyf",
120            TableTag::Loca => *b"loca",
121            TableTag::Cff => *b"CFF ",
122            TableTag::Fvar => *b"fvar",
123            TableTag::Avar => *b"avar",
124            TableTag::Gvar => *b"gvar",
125            TableTag::Gsub => *b"GSUB",
126            TableTag::Gpos => *b"GPOS",
127            TableTag::Gdef => *b"GDEF",
128            TableTag::Stat => *b"stat",
129            TableTag::Dsig => *b"DSIG",
130            TableTag::Base => *b"BASE",
131            TableTag::Kern => *b"kern",
132        }
133    }
134
135    /// Look up a known tag by its raw bytes.
136    ///
137    /// # Errors
138    /// [`ParseError::UnknownTableTag`] when the bytes do not name a tag in
139    /// [`TableTag::ALL`].
140    pub fn from_bytes(tag: &RawTag) -> Result<Self, ParseError> {
141        Self::ALL
142            .iter()
143            .find(|t| t.to_bytes() == *tag)
144            .copied()
145            .ok_or(ParseError::UnknownTableTag(*tag))
146    }
147
148    /// The [`font_types::Tag`] for this table.
149    #[must_use]
150    pub const fn to_tag(self) -> Tag {
151        Tag::new(&self.to_bytes())
152    }
153}
154
155impl From<TableTag> for Tag {
156    fn from(t: TableTag) -> Tag {
157        t.to_tag()
158    }
159}
160
161impl fmt::Display for TableTag {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        write!(f, "{}", crate::error::tag_display(&self.to_bytes()))
164    }
165}
166
167#[cfg(test)]
168mod tests {
169    #![allow(clippy::unwrap_used, clippy::expect_used)]
170    use super::TableTag;
171    use alloc::string::ToString;
172
173    // Test harness: assertions legitimately panic; the lib target stays
174    // lint-clean.
175
176    #[test]
177    fn round_trip_all_tags() {
178        for tag in TableTag::ALL {
179            let bytes = tag.to_bytes();
180            assert_eq!(TableTag::from_bytes(&bytes), Ok(tag), "{tag}");
181            // Every known tag is four printable-or-space bytes.
182            assert!(bytes.iter().all(|&b| b.is_ascii_graphic() || b == b' '));
183        }
184        assert_eq!(
185            TableTag::from_bytes(b"nope"),
186            Err(crate::ParseError::UnknownTableTag(*b"nope"))
187        );
188        assert_eq!(TableTag::Cmap.to_string(), "'cmap'");
189        assert_eq!(TableTag::Os2.to_string(), "'OS/2'");
190        assert_eq!(
191            font_types::Tag::from(TableTag::Stat).to_be_bytes(),
192            *b"stat"
193        );
194    }
195}