postscript/compact1/
encoding.rs

1//! The glyph encodings.
2
3use crate::compact1::{GlyphID, StringID};
4use crate::Result;
5
6/// A glyph encoding.
7#[derive(Clone, Debug)]
8pub enum Encoding {
9    Standard,
10    Expert,
11    Format0(Encoding0),
12    Format1(Encoding1),
13    FormatSupplemental(EncodingSupplemental),
14}
15
16table! {
17    /// An encoding in format 0.
18    pub Encoding0 { // Format 0
19        format     (u8) = { 0 }, // format
20        code_count (u8), // nCodes
21
22        codes (Vec<u8>) |this, tape| { // code
23            tape.take_given(this.code_count as usize)
24        },
25    }
26}
27
28table! {
29    /// An encoding in format 1.
30    pub Encoding1 { // Format 1
31        format      (u8) = { 1 }, // format
32        range_count (u8), // nRanges
33
34        ranges (Vec<Range1>) |this, tape| { // Range1
35            tape.take_given(this.range_count as usize)
36        },
37    }
38}
39
40table! {
41    /// An encoding in the supplemental format.
42    pub EncodingSupplemental { // Supplemental Encoding Data
43        format           (u8),
44        supplement_count (u8), // nSups
45
46        supplements (Vec<Supplement>) |this, tape| { // Supplement
47            tape.take_given(this.supplement_count as usize)
48        },
49    }
50}
51
52table! {
53    /// A range of an encoding in format 1.
54    #[derive(Copy)]
55    pub Range1 {
56        first_code (u8), // first
57        left_count (u8), // nLeft
58    }
59}
60
61table! {
62    /// A supplement of an encoding in the supplemental format.
63    #[derive(Copy)]
64    pub Supplement {
65        code  (u8      ), // code
66        glyph (StringID), // glyph
67    }
68}
69
70impl Encoding {
71    /// Return the string identifier of a glyph.
72    pub fn get(&self, glyph_id: GlyphID) -> Option<StringID> {
73        match self {
74            Encoding::Standard => get_standard(glyph_id),
75            Encoding::Expert => get_expert(glyph_id),
76            Encoding::Format0(ref encoding) => encoding.get(glyph_id),
77            Encoding::Format1(ref encoding) => encoding.get(glyph_id),
78            Encoding::FormatSupplemental(ref encoding) => encoding.get(glyph_id),
79        }
80    }
81}
82
83impl crate::value::Read for Encoding {
84    fn read<T: crate::tape::Read>(tape: &mut T) -> Result<Self> {
85        Ok(match tape.peek::<u8>()? {
86            0 => Encoding::Format0(tape.take()?),
87            1 => Encoding::Format1(tape.take()?),
88            format if format & 0x80 > 0 => Encoding::FormatSupplemental(tape.take()?),
89            format => raise!("found an unknown format of encodings ({format})"),
90        })
91    }
92}
93
94impl Encoding0 {
95    #[inline]
96    fn get(&self, _: GlyphID) -> Option<StringID> {
97        None
98    }
99}
100
101impl Encoding1 {
102    #[inline]
103    fn get(&self, _: GlyphID) -> Option<StringID> {
104        None
105    }
106}
107
108impl EncodingSupplemental {
109    #[inline]
110    fn get(&self, _: GlyphID) -> Option<StringID> {
111        None
112    }
113}
114
115macro_rules! get(
116    ($one:ident { $($glyph_id:pat => $string_id:expr => $name:expr,)+ }) => (
117        Some(match $one {
118            $($glyph_id => $string_id,)+
119            _ => return None,
120        })
121    );
122);
123
124fn get_standard(glyph_id: GlyphID) -> Option<StringID> {
125    get!(glyph_id {
126        0 => 0 => ".notdef",
127        1 => 0 => ".notdef",
128        2 => 0 => ".notdef",
129        3 => 0 => ".notdef",
130        4 => 0 => ".notdef",
131        5 => 0 => ".notdef",
132        6 => 0 => ".notdef",
133        7 => 0 => ".notdef",
134        8 => 0 => ".notdef",
135        9 => 0 => ".notdef",
136        10 => 0 => ".notdef",
137        11 => 0 => ".notdef",
138        12 => 0 => ".notdef",
139        13 => 0 => ".notdef",
140        14 => 0 => ".notdef",
141        15 => 0 => ".notdef",
142        16 => 0 => ".notdef",
143        17 => 0 => ".notdef",
144        18 => 0 => ".notdef",
145        19 => 0 => ".notdef",
146        20 => 0 => ".notdef",
147        21 => 0 => ".notdef",
148        22 => 0 => ".notdef",
149        23 => 0 => ".notdef",
150        24 => 0 => ".notdef",
151        25 => 0 => ".notdef",
152        26 => 0 => ".notdef",
153        27 => 0 => ".notdef",
154        28 => 0 => ".notdef",
155        29 => 0 => ".notdef",
156        30 => 0 => ".notdef",
157        31 => 0 => ".notdef",
158        32 => 1 => "space",
159        33 => 2 => "exclam",
160        34 => 3 => "quotedbl",
161        35 => 4 => "numbersign",
162        36 => 5 => "dollar",
163        37 => 6 => "percent",
164        38 => 7 => "ampersand",
165        39 => 8 => "quoteright",
166        40 => 9 => "parenleft",
167        41 => 10 => "parenright",
168        42 => 11 => "asterisk",
169        43 => 12 => "plus",
170        44 => 13 => "comma",
171        45 => 14 => "hyphen",
172        46 => 15 => "period",
173        47 => 16 => "slash",
174        48 => 17 => "zero",
175        49 => 18 => "one",
176        50 => 19 => "two",
177        51 => 20 => "three",
178        52 => 21 => "four",
179        53 => 22 => "five",
180        54 => 23 => "six",
181        55 => 24 => "seven",
182        56 => 25 => "eight",
183        57 => 26 => "nine",
184        58 => 27 => "colon",
185        59 => 28 => "semicolon",
186        60 => 29 => "less",
187        61 => 30 => "equal",
188        62 => 31 => "greater",
189        63 => 32 => "question",
190        64 => 33 => "at",
191        65 => 34 => "A",
192        66 => 35 => "B",
193        67 => 36 => "C",
194        68 => 37 => "D",
195        69 => 38 => "E",
196        70 => 39 => "F",
197        71 => 40 => "G",
198        72 => 41 => "H",
199        73 => 42 => "I",
200        74 => 43 => "J",
201        75 => 44 => "K",
202        76 => 45 => "L",
203        77 => 46 => "M",
204        78 => 47 => "N",
205        79 => 48 => "O",
206        80 => 49 => "P",
207        81 => 50 => "Q",
208        82 => 51 => "R",
209        83 => 52 => "S",
210        84 => 53 => "T",
211        85 => 54 => "U",
212        86 => 55 => "V",
213        87 => 56 => "W",
214        88 => 57 => "X",
215        89 => 58 => "Y",
216        90 => 59 => "Z",
217        91 => 60 => "bracketleft",
218        92 => 61 => "backslash",
219        93 => 62 => "bracketright",
220        94 => 63 => "asciicircum",
221        95 => 64 => "underscore",
222        96 => 65 => "quoteleft",
223        97 => 66 => "a",
224        98 => 67 => "b",
225        99 => 68 => "c",
226        100 => 69 => "d",
227        101 => 70 => "e",
228        102 => 71 => "f",
229        103 => 72 => "g",
230        104 => 73 => "h",
231        105 => 74 => "i",
232        106 => 75 => "j",
233        107 => 76 => "k",
234        108 => 77 => "l",
235        109 => 78 => "m",
236        110 => 79 => "n",
237        111 => 80 => "o",
238        112 => 81 => "p",
239        113 => 82 => "q",
240        114 => 83 => "r",
241        115 => 84 => "s",
242        116 => 85 => "t",
243        117 => 86 => "u",
244        118 => 87 => "v",
245        119 => 88 => "w",
246        120 => 89 => "x",
247        121 => 90 => "y",
248        122 => 91 => "z",
249        123 => 92 => "braceleft",
250        124 => 93 => "bar",
251        125 => 94 => "braceright",
252        126 => 95 => "asciitilde",
253        127 => 0 => ".notdef",
254        128 => 0 => ".notdef",
255        129 => 0 => ".notdef",
256        130 => 0 => ".notdef",
257        131 => 0 => ".notdef",
258        132 => 0 => ".notdef",
259        133 => 0 => ".notdef",
260        134 => 0 => ".notdef",
261        135 => 0 => ".notdef",
262        136 => 0 => ".notdef",
263        137 => 0 => ".notdef",
264        138 => 0 => ".notdef",
265        139 => 0 => ".notdef",
266        140 => 0 => ".notdef",
267        141 => 0 => ".notdef",
268        142 => 0 => ".notdef",
269        143 => 0 => ".notdef",
270        144 => 0 => ".notdef",
271        145 => 0 => ".notdef",
272        146 => 0 => ".notdef",
273        147 => 0 => ".notdef",
274        148 => 0 => ".notdef",
275        149 => 0 => ".notdef",
276        150 => 0 => ".notdef",
277        151 => 0 => ".notdef",
278        152 => 0 => ".notdef",
279        153 => 0 => ".notdef",
280        154 => 0 => ".notdef",
281        155 => 0 => ".notdef",
282        156 => 0 => ".notdef",
283        157 => 0 => ".notdef",
284        158 => 0 => ".notdef",
285        159 => 0 => ".notdef",
286        160 => 0 => ".notdef",
287        161 => 96 => "exclamdown",
288        162 => 97 => "cent",
289        163 => 98 => "sterling",
290        164 => 99 => "fraction",
291        165 => 100 => "yen",
292        166 => 101 => "florin",
293        167 => 102 => "section",
294        168 => 103 => "currency",
295        169 => 104 => "quotesingle",
296        170 => 105 => "quotedblleft",
297        171 => 106 => "guillemotleft",
298        172 => 107 => "guilsinglleft",
299        173 => 108 => "guilsinglright",
300        174 => 109 => "fi",
301        175 => 110 => "fl",
302        176 => 0 => ".notdef",
303        177 => 111 => "endash",
304        178 => 112 => "dagger",
305        179 => 113 => "daggerdbl",
306        180 => 114 => "periodcentered",
307        181 => 0 => ".notdef",
308        182 => 115 => "paragraph",
309        183 => 116 => "bullet",
310        184 => 117 => "quotesinglbase",
311        185 => 118 => "quotedblbase",
312        186 => 119 => "quotedblright",
313        187 => 120 => "guillemotright",
314        188 => 121 => "ellipsis",
315        189 => 122 => "perthousand",
316        190 => 0 => ".notdef",
317        191 => 123 => "questiondown",
318        192 => 0 => ".notdef",
319        193 => 124 => "grave",
320        194 => 125 => "acute",
321        195 => 126 => "circumflex",
322        196 => 127 => "tilde",
323        197 => 128 => "macron",
324        198 => 129 => "breve",
325        199 => 130 => "dotaccent",
326        200 => 131 => "dieresis",
327        201 => 0 => ".notdef",
328        202 => 132 => "ring",
329        203 => 133 => "cedilla",
330        204 => 0 => ".notdef",
331        205 => 134 => "hungarumlaut",
332        206 => 135 => "ogonek",
333        207 => 136 => "caron",
334        208 => 137 => "emdash",
335        209 => 0 => ".notdef",
336        210 => 0 => ".notdef",
337        211 => 0 => ".notdef",
338        212 => 0 => ".notdef",
339        213 => 0 => ".notdef",
340        214 => 0 => ".notdef",
341        215 => 0 => ".notdef",
342        216 => 0 => ".notdef",
343        217 => 0 => ".notdef",
344        218 => 0 => ".notdef",
345        219 => 0 => ".notdef",
346        220 => 0 => ".notdef",
347        221 => 0 => ".notdef",
348        222 => 0 => ".notdef",
349        223 => 0 => ".notdef",
350        224 => 0 => ".notdef",
351        225 => 138 => "AE",
352        226 => 0 => ".notdef",
353        227 => 139 => "ordfeminine",
354        228 => 0 => ".notdef",
355        229 => 0 => ".notdef",
356        230 => 0 => ".notdef",
357        231 => 0 => ".notdef",
358        232 => 140 => "Lslash",
359        233 => 141 => "Oslash",
360        234 => 142 => "OE",
361        235 => 143 => "ordmasculine",
362        236 => 0 => ".notdef",
363        237 => 0 => ".notdef",
364        238 => 0 => ".notdef",
365        239 => 0 => ".notdef",
366        240 => 0 => ".notdef",
367        241 => 144 => "ae",
368        242 => 0 => ".notdef",
369        243 => 0 => ".notdef",
370        244 => 0 => ".notdef",
371        245 => 145 => "dotlessi",
372        246 => 0 => ".notdef",
373        247 => 0 => ".notdef",
374        248 => 146 => "lslash",
375        249 => 147 => "oslash",
376        250 => 148 => "oe",
377        251 => 149 => "germandbls",
378        252 => 0 => ".notdef",
379        253 => 0 => ".notdef",
380        254 => 0 => ".notdef",
381        255 => 0 => ".notdef",
382    })
383}
384
385fn get_expert(glyph_id: GlyphID) -> Option<StringID> {
386    get!(glyph_id {
387        0 => 0 => ".notdef",
388        1 => 0 => ".notdef",
389        2 => 0 => ".notdef",
390        3 => 0 => ".notdef",
391        4 => 0 => ".notdef",
392        5 => 0 => ".notdef",
393        6 => 0 => ".notdef",
394        7 => 0 => ".notdef",
395        8 => 0 => ".notdef",
396        9 => 0 => ".notdef",
397        10 => 0 => ".notdef",
398        11 => 0 => ".notdef",
399        12 => 0 => ".notdef",
400        13 => 0 => ".notdef",
401        14 => 0 => ".notdef",
402        15 => 0 => ".notdef",
403        16 => 0 => ".notdef",
404        17 => 0 => ".notdef",
405        18 => 0 => ".notdef",
406        19 => 0 => ".notdef",
407        20 => 0 => ".notdef",
408        21 => 0 => ".notdef",
409        22 => 0 => ".notdef",
410        23 => 0 => ".notdef",
411        24 => 0 => ".notdef",
412        25 => 0 => ".notdef",
413        26 => 0 => ".notdef",
414        27 => 0 => ".notdef",
415        28 => 0 => ".notdef",
416        29 => 0 => ".notdef",
417        30 => 0 => ".notdef",
418        31 => 0 => ".notdef",
419        32 => 1 => "space",
420        33 => 229 => "exclamsmall",
421        34 => 230 => "Hungarumlautsmall",
422        35 => 0 => ".notdef",
423        36 => 231 => "dollaroldstyle",
424        37 => 232 => "dollarsuperior",
425        38 => 233 => "ampersandsmall",
426        39 => 234 => "Acutesmall",
427        40 => 235 => "parenleftsuperior",
428        41 => 236 => "parenrightsuperior",
429        42 => 237 => "twodotenleader",
430        43 => 238 => "onedotenleader",
431        44 => 13 => "comma",
432        45 => 14 => "hyphen",
433        46 => 15 => "period",
434        47 => 99 => "fraction",
435        48 => 239 => "zerooldstyle",
436        49 => 240 => "oneoldstyle",
437        50 => 241 => "twooldstyle",
438        51 => 242 => "threeoldstyle",
439        52 => 243 => "fouroldstyle",
440        53 => 244 => "fiveoldstyle",
441        54 => 245 => "sixoldstyle",
442        55 => 246 => "sevenoldstyle",
443        56 => 247 => "eightoldstyle",
444        57 => 248 => "nineoldstyle",
445        58 => 27 => "colon",
446        59 => 28 => "semicolon",
447        60 => 249 => "commasuperior",
448        61 => 250 => "threequartersemdash",
449        62 => 251 => "periodsuperior",
450        63 => 252 => "questionsmall",
451        64 => 0 => ".notdef",
452        65 => 253 => "asuperior",
453        66 => 254 => "bsuperior",
454        67 => 255 => "centsuperior",
455        68 => 256 => "dsuperior",
456        69 => 257 => "esuperior",
457        70 => 0 => ".notdef",
458        71 => 0 => ".notdef",
459        72 => 0 => ".notdef",
460        73 => 258 => "isuperior",
461        74 => 0 => ".notdef",
462        75 => 0 => ".notdef",
463        76 => 259 => "lsuperior",
464        77 => 260 => "msuperior",
465        78 => 261 => "nsuperior",
466        79 => 262 => "osuperior",
467        80 => 0 => ".notdef",
468        81 => 0 => ".notdef",
469        82 => 263 => "rsuperior",
470        83 => 264 => "ssuperior",
471        84 => 265 => "tsuperior",
472        85 => 0 => ".notdef",
473        86 => 266 => "ff",
474        87 => 109 => "fi",
475        88 => 110 => "fl",
476        89 => 267 => "ffi",
477        90 => 268 => "ffl",
478        91 => 269 => "parenleftinferior",
479        92 => 0 => ".notdef",
480        93 => 270 => "parenrightinferior",
481        94 => 271 => "Circumflexsmall",
482        95 => 272 => "hyphensuperior",
483        96 => 273 => "Gravesmall",
484        97 => 274 => "Asmall",
485        98 => 275 => "Bsmall",
486        99 => 276 => "Csmall",
487        100 => 277 => "Dsmall",
488        101 => 278 => "Esmall",
489        102 => 279 => "Fsmall",
490        103 => 280 => "Gsmall",
491        104 => 281 => "Hsmall",
492        105 => 282 => "Ismall",
493        106 => 283 => "Jsmall",
494        107 => 284 => "Ksmall",
495        108 => 285 => "Lsmall",
496        109 => 286 => "Msmall",
497        110 => 287 => "Nsmall",
498        111 => 288 => "Osmall",
499        112 => 289 => "Psmall",
500        113 => 290 => "Qsmall",
501        114 => 291 => "Rsmall",
502        115 => 292 => "Ssmall",
503        116 => 293 => "Tsmall",
504        117 => 294 => "Usmall",
505        118 => 295 => "Vsmall",
506        119 => 296 => "Wsmall",
507        120 => 297 => "Xsmall",
508        121 => 298 => "Ysmall",
509        122 => 299 => "Zsmall",
510        123 => 300 => "colonmonetary",
511        124 => 301 => "onefitted",
512        125 => 302 => "rupiah",
513        126 => 303 => "Tildesmall",
514        127 => 0 => ".notdef",
515        128 => 0 => ".notdef",
516        129 => 0 => ".notdef",
517        130 => 0 => ".notdef",
518        131 => 0 => ".notdef",
519        132 => 0 => ".notdef",
520        133 => 0 => ".notdef",
521        134 => 0 => ".notdef",
522        135 => 0 => ".notdef",
523        136 => 0 => ".notdef",
524        137 => 0 => ".notdef",
525        138 => 0 => ".notdef",
526        139 => 0 => ".notdef",
527        140 => 0 => ".notdef",
528        141 => 0 => ".notdef",
529        142 => 0 => ".notdef",
530        143 => 0 => ".notdef",
531        144 => 0 => ".notdef",
532        145 => 0 => ".notdef",
533        146 => 0 => ".notdef",
534        147 => 0 => ".notdef",
535        148 => 0 => ".notdef",
536        149 => 0 => ".notdef",
537        150 => 0 => ".notdef",
538        151 => 0 => ".notdef",
539        152 => 0 => ".notdef",
540        153 => 0 => ".notdef",
541        154 => 0 => ".notdef",
542        155 => 0 => ".notdef",
543        156 => 0 => ".notdef",
544        157 => 0 => ".notdef",
545        158 => 0 => ".notdef",
546        159 => 0 => ".notdef",
547        160 => 0 => ".notdef",
548        161 => 304 => "exclamdownsmall",
549        162 => 305 => "centoldstyle",
550        163 => 306 => "Lslashsmall",
551        164 => 0 => ".notdef",
552        165 => 0 => ".notdef",
553        166 => 307 => "Scaronsmall",
554        167 => 308 => "Zcaronsmall",
555        168 => 309 => "Dieresissmall",
556        169 => 310 => "Brevesmall",
557        170 => 311 => "Caronsmall",
558        171 => 0 => ".notdef",
559        172 => 312 => "Dotaccentsmall",
560        173 => 0 => ".notdef",
561        174 => 0 => ".notdef",
562        175 => 313 => "Macronsmall",
563        176 => 0 => ".notdef",
564        177 => 0 => ".notdef",
565        178 => 314 => "figuredash",
566        179 => 315 => "hypheninferior",
567        180 => 0 => ".notdef",
568        181 => 0 => ".notdef",
569        182 => 316 => "Ogoneksmall",
570        183 => 317 => "Ringsmall",
571        184 => 318 => "Cedillasmall",
572        185 => 0 => ".notdef",
573        186 => 0 => ".notdef",
574        187 => 0 => ".notdef",
575        188 => 158 => "onequarter",
576        189 => 155 => "onehalf",
577        190 => 163 => "threequarters",
578        191 => 319 => "questiondownsmall",
579        192 => 320 => "oneeighth",
580        193 => 321 => "threeeighths",
581        194 => 322 => "fiveeighths",
582        195 => 323 => "seveneighths",
583        196 => 324 => "onethird",
584        197 => 325 => "twothirds",
585        198 => 0 => ".notdef",
586        199 => 0 => ".notdef",
587        200 => 326 => "zerosuperior",
588        201 => 150 => "onesuperior",
589        202 => 164 => "twosuperior",
590        203 => 169 => "threesuperior",
591        204 => 327 => "foursuperior",
592        205 => 328 => "fivesuperior",
593        206 => 329 => "sixsuperior",
594        207 => 330 => "sevensuperior",
595        208 => 331 => "eightsuperior",
596        209 => 332 => "ninesuperior",
597        210 => 333 => "zeroinferior",
598        211 => 334 => "oneinferior",
599        212 => 335 => "twoinferior",
600        213 => 336 => "threeinferior",
601        214 => 337 => "fourinferior",
602        215 => 338 => "fiveinferior",
603        216 => 339 => "sixinferior",
604        217 => 340 => "seveninferior",
605        218 => 341 => "eightinferior",
606        219 => 342 => "nineinferior",
607        220 => 343 => "centinferior",
608        221 => 344 => "dollarinferior",
609        222 => 345 => "periodinferior",
610        223 => 346 => "commainferior",
611        224 => 347 => "Agravesmall",
612        225 => 348 => "Aacutesmall",
613        226 => 349 => "Acircumflexsmall",
614        227 => 350 => "Atildesmall",
615        228 => 351 => "Adieresissmall",
616        229 => 352 => "Aringsmall",
617        230 => 353 => "AEsmall",
618        231 => 354 => "Ccedillasmall",
619        232 => 355 => "Egravesmall",
620        233 => 356 => "Eacutesmall",
621        234 => 357 => "Ecircumflexsmall",
622        235 => 358 => "Edieresissmall",
623        236 => 359 => "Igravesmall",
624        237 => 360 => "Iacutesmall",
625        238 => 361 => "Icircumflexsmall",
626        239 => 362 => "Idieresissmall",
627        240 => 363 => "Ethsmall",
628        241 => 364 => "Ntildesmall",
629        242 => 365 => "Ogravesmall",
630        243 => 366 => "Oacutesmall",
631        244 => 367 => "Ocircumflexsmall",
632        245 => 368 => "Otildesmall",
633        246 => 369 => "Odieresissmall",
634        247 => 370 => "OEsmall",
635        248 => 371 => "Oslashsmall",
636        249 => 372 => "Ugravesmall",
637        250 => 373 => "Uacutesmall",
638        251 => 374 => "Ucircumflexsmall",
639        252 => 375 => "Udieresissmall",
640        253 => 376 => "Yacutesmall",
641        254 => 377 => "Thornsmall",
642        255 => 378 => "Ydieresissmall",
643    })
644}