1use super::StringId;
2use crate::parser::{FromData, LazyArray16, Stream};
3use crate::GlyphId;
4use core::num::NonZeroU16;
5
6#[rustfmt::skip]
8const EXPERT_ENCODING: &[u16] = &[
9 0, 1, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 13, 14, 15, 99,
10 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 27, 28, 249, 250, 251, 252,
11 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 109, 110,
12 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282,
13 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
14 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314,
15 315, 316, 317, 318, 158, 155, 163, 319, 320, 321, 322, 323, 324, 325, 326, 150,
16 164, 169, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340,
17 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356,
18 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372,
19 373, 374, 375, 376, 377, 378,
20];
21
22#[rustfmt::skip]
24const EXPERT_SUBSET_ENCODING: &[u16] = &[
25 0, 1, 231, 232, 235, 236, 237, 238, 13, 14, 15, 99, 239, 240, 241, 242,
26 243, 244, 245, 246, 247, 248, 27, 28, 249, 250, 251, 253, 254, 255, 256, 257,
27 258, 259, 260, 261, 262, 263, 264, 265, 266, 109, 110, 267, 268, 269, 270, 272,
28 300, 301, 302, 305, 314, 315, 158, 155, 163, 320, 321, 322, 323, 324, 325, 326,
29 150, 164, 169, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339,
30 340, 341, 342, 343, 344, 345, 346
31];
32
33#[derive(Clone, Copy, Debug)]
34pub struct Format1Range {
35 pub first: StringId,
36 pub left: u8,
37}
38
39impl FromData for Format1Range {
40 const SIZE: usize = 3;
41
42 #[inline]
43 fn parse(data: &[u8]) -> Option<Self> {
44 let mut s = Stream::new(data);
45 Some(Format1Range {
46 first: s.read::<StringId>()?,
47 left: s.read::<u8>()?,
48 })
49 }
50}
51
52#[derive(Clone, Copy, Debug)]
53pub struct Format2Range {
54 pub first: StringId,
55 pub left: u16,
56}
57
58impl FromData for Format2Range {
59 const SIZE: usize = 4;
60
61 #[inline]
62 fn parse(data: &[u8]) -> Option<Self> {
63 let mut s = Stream::new(data);
64 Some(Format2Range {
65 first: s.read::<StringId>()?,
66 left: s.read::<u16>()?,
67 })
68 }
69}
70
71#[derive(Clone, Copy, Debug)]
72pub enum Charset<'a> {
73 ISOAdobe,
74 Expert,
75 ExpertSubset,
76 Format0(LazyArray16<'a, StringId>),
77 Format1(LazyArray16<'a, Format1Range>),
78 Format2(LazyArray16<'a, Format2Range>),
79}
80
81impl Charset<'_> {
82 pub fn sid_to_gid(&self, sid: StringId) -> Option<GlyphId> {
83 if sid.0 == 0 {
84 return Some(GlyphId(0));
85 }
86
87 match self {
88 Charset::ISOAdobe | Charset::Expert | Charset::ExpertSubset => None,
89 Charset::Format0(ref array) => {
90 array
92 .into_iter()
93 .position(|n| n == sid)
94 .map(|n| GlyphId(n as u16 + 1))
95 }
96 Charset::Format1(array) => {
97 let mut glyph_id = GlyphId(1);
98 for range in *array {
99 let last = u32::from(range.first.0) + u32::from(range.left);
100 if range.first <= sid && u32::from(sid.0) <= last {
101 glyph_id.0 += sid.0 - range.first.0;
102 return Some(glyph_id);
103 }
104
105 glyph_id.0 += u16::from(range.left) + 1;
106 }
107
108 None
109 }
110 Charset::Format2(array) => {
111 let mut glyph_id = GlyphId(1);
113 for range in *array {
114 let last = u32::from(range.first.0) + u32::from(range.left);
115 if sid >= range.first && u32::from(sid.0) <= last {
116 glyph_id.0 += sid.0 - range.first.0;
117 return Some(glyph_id);
118 }
119
120 glyph_id.0 += range.left + 1;
121 }
122
123 None
124 }
125 }
126 }
127
128 pub fn get_table(&self) -> Vec<StringId> {
129 match self {
130 Charset::ISOAdobe => {
131 (0..=228).map(StringId).collect()
132 }
133 Charset::Expert => EXPERT_ENCODING.iter().map(|&n| StringId(n)).collect(),
134 Charset::ExpertSubset => EXPERT_SUBSET_ENCODING.iter().map(|&n| StringId(n)).collect(),
135 Charset::Format0(ref array) => array.clone().into_iter().collect(),
136 Charset::Format1(array) => {
137 let mut vec = Vec::new();
138 for range in *array {
139 vec.push(range.first);
140 vec.extend((range.first.0 + 1..=u16::from(range.left)).map(StringId));
141 }
142 vec
143 }
144 Charset::Format2(array) => {
145 let mut vec = Vec::new();
146 for range in *array {
147 vec.push(range.first);
148 vec.extend((range.first.0 + 1..=u16::from(range.left)).map(StringId));
149 }
150 vec
151 }
152 }
153 }
154
155 pub fn gid_to_sid(&self, gid: GlyphId) -> Option<StringId> {
156 match self {
157 Charset::ISOAdobe => {
158 if gid.0 <= 228 {
159 Some(StringId(gid.0))
160 } else {
161 None
162 }
163 }
164 Charset::Expert => EXPERT_ENCODING
165 .get(usize::from(gid.0))
166 .cloned()
167 .map(StringId),
168 Charset::ExpertSubset => EXPERT_SUBSET_ENCODING
169 .get(usize::from(gid.0))
170 .cloned()
171 .map(StringId),
172 Charset::Format0(ref array) => {
173 if gid.0 == 0 {
174 Some(StringId(0))
175 } else {
176 array.get(gid.0 - 1)
177 }
178 }
179 Charset::Format1(array) => {
180 if gid.0 == 0 {
181 Some(StringId(0))
182 } else {
183 let mut sid = gid.0 - 1;
184 for range in *array {
185 if sid <= u16::from(range.left) {
186 sid = sid.checked_add(range.first.0)?;
187 return Some(StringId(sid));
188 }
189
190 sid = sid.checked_sub(u16::from(range.left) + 1)?;
191 }
192
193 None
194 }
195 }
196 Charset::Format2(array) => {
197 if gid.0 == 0 {
198 Some(StringId(0))
199 } else {
200 let mut sid = gid.0 - 1;
201 for range in *array {
202 if sid <= range.left {
203 sid = sid.checked_add(range.first.0)?;
204 return Some(StringId(sid));
205 }
206
207 sid = sid.checked_sub(range.left.checked_add(1)?)?;
208 }
209
210 None
211 }
212 }
213 }
214 }
215}
216
217pub(crate) fn parse_charset<'a>(
218 number_of_glyphs: NonZeroU16,
219 s: &mut Stream<'a>,
220) -> Option<Charset<'a>> {
221 let format = s.read::<u8>()?;
223 match format {
224 0 => Some(Charset::Format0(
225 s.read_array16::<StringId>(number_of_glyphs.get() - 1)?,
226 )),
227 1 => {
228 let mut count = 0;
231 {
232 let mut s = s.clone();
233 let mut total_left = number_of_glyphs.get() - 1;
234 while total_left > 0 {
235 s.skip::<StringId>(); let left = s.read::<u8>()?;
237 total_left = total_left.checked_sub(u16::from(left) + 1)?;
238 count += 1;
239 }
240 }
241
242 s.read_array16::<Format1Range>(count).map(Charset::Format1)
243 }
244 2 => {
245 let mut count = 0;
247 {
248 let mut s = s.clone();
249 let mut total_left = number_of_glyphs.get() - 1;
250 while total_left > 0 {
251 s.skip::<StringId>(); let left = s.read::<u16>()?.checked_add(1)?;
253 total_left = total_left.checked_sub(left)?;
254 count += 1;
255 }
256 }
257
258 s.read_array16::<Format2Range>(count).map(Charset::Format2)
259 }
260 _ => None,
261 }
262}