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
#![deny(missing_copy_implementations, missing_debug_implementations, missing_docs)]
#![no_std]
#[macro_use]
extern crate tarrasque;
pub mod dict;
pub mod error;
pub mod glyphs;
pub mod index;
use tarrasque::{Extract, ExtractError, ExtractResult, Stream};
use dict::{Top};
use error::{OFFSET_SIZE};
use glyphs::{Glyphs, parse_glyphs};
use index::{Index, Strings};
pub use error::{CffError};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct OffsetSize(pub u8);
impl<'a> Extract<'a, ()> for OffsetSize {
#[inline]
fn extract(stream: &mut Stream<'a>, _: ()) -> ExtractResult<'a, Self> {
let size: u8 = stream.extract(())?;
if size >= 1 && size <= 4 {
Ok(OffsetSize(size))
} else {
Err(ExtractError::Code(OFFSET_SIZE))
}
}
}
extract! {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub Header[4] {
major_version: u8 = ([extract]),
minor_version: u8 = ([extract]),
header_size: u8 = ([extract]),
offset_size: OffsetSize = ([extract]),
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Cff<'a> {
pub bytes: &'a [u8],
pub header: Header,
pub names: Index<'a, &'a str>,
pub tops: Index<'a, Top<'a>>,
pub strings: Strings<'a>,
pub subroutines: Index<'a, &'a [u8]>,
}
impl<'a> Cff<'a> {
#[inline]
pub fn glyphs(&self, index: usize) -> Option<Result<Glyphs<'a>, CffError<'a>>> {
self.tops.extract(index).map(|t| parse_glyphs(&self.bytes, t?, self.subroutines))
}
}
#[inline]
pub fn parse_cff(bytes: &[u8]) -> Result<Cff, CffError> {
let mut stream = Stream(bytes);
let header: Header = stream.extract(())?;
let names: Index<&str> = stream.extract(())?;
let tops: Index<Top> = stream.extract(())?;
let strings: Strings = stream.extract(())?;
let subroutines: Index<&[u8]> = stream.extract(())?;
Ok(Cff { bytes, header, names, tops, strings, subroutines })
}