webtype/
file.rs

1use std::io::Cursor;
2
3use opentype::truetype::Tag;
4use opentype::Font;
5
6use crate::Result;
7
8/// A file.
9pub struct File {
10    /// The fonts.
11    pub fonts: Vec<Font>,
12    /// The decompressed font data.
13    pub tape: Cursor<Vec<u8>>,
14}
15
16impl File {
17    /// Read a file.
18    pub fn read<T: crate::tape::Read>(tape: &mut T) -> Result<File> {
19        use crate::tape::Read;
20
21        match Read::peek::<(Tag, Tag)>(tape)? {
22            (tag, _) if tag.0 == *b"wOFF" => {
23                raise!("found version 1, which is not supported yet");
24            }
25            (_, tag) if tag.0 == *b"ttfc" => {
26                raise!("found a TrueType collection, which is not supported yet");
27            }
28            _ => {}
29        }
30        let (font, data) = read_version2(tape)?;
31        Ok(File {
32            fonts: vec![font],
33            tape: Cursor::new(data),
34        })
35    }
36}
37
38dereference! { File::fonts => [Font] }
39
40fn read_version2<T: crate::tape::Read>(mut tape: T) -> Result<(Font, Vec<u8>)> {
41    use crate::value::Read as ValueRead;
42    use crate::version2::{FileHeader, TableDirectory};
43    use crate::walue::Read as WalueRead;
44
45    let file_header = FileHeader::read(&mut tape)?;
46    let table_directory = TableDirectory::read(&mut tape, &file_header)?;
47    let offsets = table_directory.as_offsets(&file_header);
48    let data = table_directory.decompress(&mut tape, &file_header)?;
49    Ok((Font { offsets }, data))
50}