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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! The [naming table][1].
//!
//! [1]: https://learn.microsoft.com/en-us/typography/opentype/spec/name

pub mod language;

mod encoding;
mod name;
mod platform;

pub use encoding::EncodingID;
pub use language::LanguageID;
pub use name::NameID;
pub use platform::PlatformID;

use crate::Result;

/// A naming table.
#[derive(Clone, Debug)]
pub enum Names {
    /// Format 0.
    Format0(Names0),
    /// Format 1.
    Format1(Names1),
}

table! {
    @position
    @write
    #[doc = "A naming table in format 0."]
    pub Names0 {
        format (u16), // format
        count  (u16), // count
        offset (u16), // stringOffset

        records (Vec<Record>) |this, tape, _| { // nameRecord
            tape.take_given(this.count as usize)
        },

        data (Vec<u8>) |this, tape, position| {
            tape.jump(position + this.offset as u64)?;
            tape.take_bytes(measure(&this.records))
        },
    }
}

table! {
    @position
    @write
    #[doc = "A naming table in format 1."]
    pub Names1 {
        format (u16), // format
        count  (u16), // count
        offset (u16), // stringOffset

        records (Vec<Record>) |this, tape, _| { // nameRecord
            tape.take_given(this.count as usize)
        },

        language_tag_count (u16), // langTagCount

        language_tags (Vec<LanguageTag>) |this, tape, _| { // langTagRecord
            tape.take_given(this.language_tag_count as usize)
        },

        data (Vec<u8>) |this, tape, position| {
            tape.jump(position + this.offset as u64)?;
            tape.take_bytes(measure(&this.records))
        },
    }
}

table! {
    @write
    #[doc = "A record of a naming table."]
    #[derive(Copy)]
    pub Record { // NameRecord
        platform_id (PlatformID), // platformID
        encoding_id (EncodingID), // encodingID

        language_id (LanguageID) |this, tape| { // languageID
            tape.take_given(this.platform_id)
        },

        name_id     (NameID), // nameID
        size        (u16   ), // length
        offset      (u16   ), // offset
    }
}

table! {
    @write
    #[doc = "A language tag."]
    #[derive(Copy)]
    pub LanguageTag { // LangTagRecord
        size   (u16), // length
        offset (u16), // offset
    }
}

/// An encoding context.
pub type Context = encoding::macintosh::Context;

impl Names {
    /// Iterate over name records.
    pub fn iter(
        &self,
    ) -> impl Iterator<Item = ((PlatformID, EncodingID, LanguageID, NameID), Option<String>)>
           + DoubleEndedIterator
           + '_ {
        let (records, data) = match self {
            Self::Format0(ref table) => (&table.records, &table.data),
            Self::Format1(ref table) => (&table.records, &table.data),
        };
        records.iter().map(move |record| {
            let offset = record.offset as usize;
            let size = record.size as usize;
            (
                (
                    record.platform_id,
                    record.encoding_id,
                    record.language_id,
                    record.name_id,
                ),
                decode(
                    record.platform_id,
                    record.encoding_id,
                    record.language_id,
                    &data[offset..(offset + size)],
                ),
            )
        })
    }

    /// Create an instance from an iterator over name records.
    pub fn from_iter<T, U, V, W>(
        records: T,
        language_tags: U,
        context: &mut Context,
    ) -> Result<Self>
    where
        T: IntoIterator<Item = ((PlatformID, EncodingID, LanguageID, NameID), V)>,
        U: IntoIterator<Item = W>,
        V: AsRef<str>,
        W: AsRef<str>,
    {
        let mut data = vec![];
        let records = records
            .into_iter()
            .map(
                |((platform_id, encoding_id, language_id, name_id), value)| {
                    let offset = data.len();
                    encode(
                        platform_id,
                        encoding_id,
                        language_id,
                        value.as_ref(),
                        &mut data,
                        context,
                    )?;
                    Ok(Record {
                        platform_id,
                        encoding_id,
                        language_id,
                        name_id,
                        size: (data.len() - offset) as _,
                        offset: offset as _,
                    })
                },
            )
            .collect::<Result<Vec<_>>>()?;
        let language_tags = language_tags
            .into_iter()
            .map(|value| {
                let offset = data.len();
                encoding::unicode::encode_utf16(value.as_ref(), &mut data);
                LanguageTag {
                    size: (data.len() - offset) as _,
                    offset: offset as _,
                }
            })
            .collect::<Vec<_>>();
        let table = if language_tags.is_empty() {
            Self::Format0(Names0 {
                format: 0,
                count: records.len() as _,
                offset: (2 * (3 + records.len() * 6)) as _,
                records,
                data,
            })
        } else {
            Self::Format1(Names1 {
                format: 1,
                count: records.len() as _,
                offset: (2 * (3 + records.len() * 6 + 1 + language_tags.len() * 2)) as _,
                records,
                language_tag_count: language_tags.len() as _,
                language_tags,
                data,
            })
        };
        Ok(table)
    }

    /// Iterate over the language tags.
    pub fn language_tags(&self) -> impl Iterator<Item = Option<String>> + DoubleEndedIterator + '_ {
        let (records, data) = match self {
            Self::Format0(ref table) => (&[][..], &table.data),
            Self::Format1(ref table) => (&table.language_tags[..], &table.data),
        };
        records.iter().map(|record| {
            let offset = record.offset as usize;
            let size = record.size as usize;
            encoding::unicode::decode_utf16(&data[offset..(offset + size)])
        })
    }
}

impl crate::value::Read for Names {
    fn read<T: crate::tape::Read>(tape: &mut T) -> Result<Self> {
        Ok(match tape.peek::<u16>()? {
            0 => Self::Format0(tape.take()?),
            1 => Self::Format1(tape.take()?),
            _ => raise!("found an unknown format of the naming table"),
        })
    }
}

impl crate::value::Write for Names {
    fn write<T: crate::tape::Write>(&self, tape: &mut T) -> Result<()> {
        match self {
            Self::Format0(value) => tape.give(value),
            Self::Format1(value) => tape.give(value),
        }
    }
}

fn decode(
    platform_id: PlatformID,
    encoding_id: EncodingID,
    language_id: LanguageID,
    data: &[u8],
) -> Option<String> {
    match platform_id {
        PlatformID::Unicode => encoding::unicode::decode(data, encoding_id),
        PlatformID::Macintosh => encoding::macintosh::decode(data, encoding_id, language_id),
        PlatformID::Windows => encoding::windows::decode(data, encoding_id),
    }
}

fn encode(
    platform_id: PlatformID,
    encoding_id: EncodingID,
    language_id: LanguageID,
    value: &str,
    data: &mut Vec<u8>,
    context: &mut encoding::macintosh::Context,
) -> Result<()> {
    match platform_id {
        PlatformID::Unicode => encoding::unicode::encode(value, encoding_id, data),
        PlatformID::Macintosh => {
            encoding::macintosh::encode(value, encoding_id, language_id, data, context)
        }
        PlatformID::Windows => encoding::windows::encode(value, encoding_id, data),
    }
}

fn measure(records: &[Record]) -> usize {
    let mut size = 0;
    for record in records {
        let end = record.offset + record.size;
        if end > size {
            size = end;
        }
    }
    size as usize
}