1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

pub use read_fonts::tables::head::MacStyle;

impl FontWrite for MacStyle {
    fn write_into(&self, writer: &mut TableWriter) {
        writer.write_slice(&self.bits().to_be_bytes())
    }
}

/// The [head](https://docs.microsoft.com/en-us/typography/opentype/spec/head)
/// (font header) table.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Head {
    /// Set by font manufacturer.
    pub font_revision: Fixed,
    /// To compute: set it to 0, sum the entire font as uint32, then
    /// store 0xB1B0AFBA - sum. If the font is used as a component in a
    /// font collection file, the value of this field will be
    /// invalidated by changes to the file structure and font table
    /// directory, and must be ignored.
    pub checksum_adjustment: u32,
    /// Set to 0x5F0F3CF5.
    pub magic_number: u32,
    /// See the flags enum
    pub flags: u16,
    /// Set to a value from 16 to 16384. Any value in this range is
    /// valid. In fonts that have TrueType outlines, a power of 2 is
    /// recommended as this allows performance optimizations in some
    /// rasterizers.
    pub units_per_em: u16,
    /// Number of seconds since 12:00 midnight that started January 1st
    /// 1904 in GMT/UTC time zone.
    pub created: LongDateTime,
    /// Number of seconds since 12:00 midnight that started January 1st
    /// 1904 in GMT/UTC time zone.
    pub modified: LongDateTime,
    /// Minimum x coordinate across all glyph bounding boxes.
    pub x_min: i16,
    /// Minimum y coordinate across all glyph bounding boxes.
    pub y_min: i16,
    /// Maximum x coordinate across all glyph bounding boxes.
    pub x_max: i16,
    /// Maximum y coordinate across all glyph bounding boxes.
    pub y_max: i16,
    /// see somewhere else
    pub mac_style: MacStyle,
    /// Smallest readable size in pixels.
    pub lowest_rec_ppem: u16,
    /// Deprecated (Set to 2).
    pub font_direction_hint: i16,
    /// 0 for short offsets (Offset16), 1 for long (Offset32).
    pub index_to_loc_format: i16,
}

impl Default for Head {
    fn default() -> Self {
        Self {
            font_revision: Default::default(),
            checksum_adjustment: Default::default(),
            magic_number: 0x5F0F3CF5,
            flags: Default::default(),
            units_per_em: Default::default(),
            created: Default::default(),
            modified: Default::default(),
            x_min: Default::default(),
            y_min: Default::default(),
            x_max: Default::default(),
            y_max: Default::default(),
            mac_style: Default::default(),
            lowest_rec_ppem: Default::default(),
            font_direction_hint: 2,
            index_to_loc_format: Default::default(),
        }
    }
}

impl Head {
    /// Construct a new `Head`
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        font_revision: Fixed,
        checksum_adjustment: u32,
        flags: u16,
        units_per_em: u16,
        created: LongDateTime,
        modified: LongDateTime,
        x_min: i16,
        y_min: i16,
        x_max: i16,
        y_max: i16,
        mac_style: MacStyle,
        lowest_rec_ppem: u16,
        index_to_loc_format: i16,
    ) -> Self {
        Self {
            font_revision,
            checksum_adjustment,
            flags,
            units_per_em,
            created,
            modified,
            x_min,
            y_min,
            x_max,
            y_max,
            mac_style,
            lowest_rec_ppem,
            index_to_loc_format,
            ..Default::default()
        }
    }
}

impl FontWrite for Head {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (MajorMinor::VERSION_1_0 as MajorMinor).write_into(writer);
        self.font_revision.write_into(writer);
        self.checksum_adjustment.write_into(writer);
        self.magic_number.write_into(writer);
        self.flags.write_into(writer);
        self.units_per_em.write_into(writer);
        self.created.write_into(writer);
        self.modified.write_into(writer);
        self.x_min.write_into(writer);
        self.y_min.write_into(writer);
        self.x_max.write_into(writer);
        self.y_max.write_into(writer);
        self.mac_style.write_into(writer);
        self.lowest_rec_ppem.write_into(writer);
        self.font_direction_hint.write_into(writer);
        self.index_to_loc_format.write_into(writer);
        (0 as i16).write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::TopLevel(Head::TAG)
    }
}

impl Validate for Head {
    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
}

impl TopLevelTable for Head {
    const TAG: Tag = Tag::new(b"head");
}

impl<'a> FromObjRef<read_fonts::tables::head::Head<'a>> for Head {
    fn from_obj_ref(obj: &read_fonts::tables::head::Head<'a>, _: FontData) -> Self {
        Head {
            font_revision: obj.font_revision(),
            checksum_adjustment: obj.checksum_adjustment(),
            magic_number: obj.magic_number(),
            flags: obj.flags(),
            units_per_em: obj.units_per_em(),
            created: obj.created(),
            modified: obj.modified(),
            x_min: obj.x_min(),
            y_min: obj.y_min(),
            x_max: obj.x_max(),
            y_max: obj.y_max(),
            mac_style: obj.mac_style(),
            lowest_rec_ppem: obj.lowest_rec_ppem(),
            font_direction_hint: obj.font_direction_hint(),
            index_to_loc_format: obj.index_to_loc_format(),
        }
    }
}

impl<'a> FromTableRef<read_fonts::tables::head::Head<'a>> for Head {}

impl<'a> FontRead<'a> for Head {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::tables::head::Head as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}