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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
use super::argstack::ArgumentsStack;
use super::{f32_abs, Builder, CFFError, IsEven};
use crate::parser::{Fixed, Stream};

pub(crate) struct CharStringParser<'a> {
    pub stack: ArgumentsStack<'a>,
    pub builder: &'a mut Builder<'a>,
    pub x: f32,
    pub y: f32,
    pub has_move_to: bool,
    pub is_first_move_to: bool,
    pub width_only: bool, // Exit right after the glyph width is parsed.
}

impl CharStringParser<'_> {
    #[inline]
    pub fn parse_move_to(&mut self, offset: usize) -> Result<(), CFFError> {
        // dx1 dy1

        if self.stack.len() != offset + 2 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        if self.is_first_move_to {
            self.is_first_move_to = false;
        } else {
            self.builder.close();
        }

        self.has_move_to = true;

        self.x += self.stack.at(offset + 0);
        self.y += self.stack.at(offset + 1);
        self.builder.move_to(self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_horizontal_move_to(&mut self, offset: usize) -> Result<(), CFFError> {
        // dx1

        if self.stack.len() != offset + 1 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        if self.is_first_move_to {
            self.is_first_move_to = false;
        } else {
            self.builder.close();
        }

        self.has_move_to = true;

        self.x += self.stack.at(offset);
        self.builder.move_to(self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_vertical_move_to(&mut self, offset: usize) -> Result<(), CFFError> {
        // dy1

        if self.stack.len() != offset + 1 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        if self.is_first_move_to {
            self.is_first_move_to = false;
        } else {
            self.builder.close();
        }

        self.has_move_to = true;

        self.y += self.stack.at(offset);
        self.builder.move_to(self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_line_to(&mut self) -> Result<(), CFFError> {
        // {dxa dya}+

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len().is_odd() {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let mut i = 0;
        while i < self.stack.len() {
            self.x += self.stack.at(i + 0);
            self.y += self.stack.at(i + 1);
            self.builder.line_to(self.x, self.y);
            i += 2;
        }

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_horizontal_line_to(&mut self) -> Result<(), CFFError> {
        // dx1 {dya dxb}*
        //     {dxa dyb}+

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.is_empty() {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let mut i = 0;
        while i < self.stack.len() {
            self.x += self.stack.at(i);
            i += 1;
            self.builder.line_to(self.x, self.y);

            if i == self.stack.len() {
                break;
            }

            self.y += self.stack.at(i);
            i += 1;
            self.builder.line_to(self.x, self.y);
        }

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_vertical_line_to(&mut self) -> Result<(), CFFError> {
        // dy1 {dxa dyb}*
        //     {dya dxb}+

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.is_empty() {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let mut i = 0;
        while i < self.stack.len() {
            self.y += self.stack.at(i);
            i += 1;
            self.builder.line_to(self.x, self.y);

            if i == self.stack.len() {
                break;
            }

            self.x += self.stack.at(i);
            i += 1;
            self.builder.line_to(self.x, self.y);
        }

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_curve_to(&mut self) -> Result<(), CFFError> {
        // {dxa dya dxb dyb dxc dyc}+

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() % 6 != 0 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let mut i = 0;
        while i < self.stack.len() {
            let x1 = self.x + self.stack.at(i + 0);
            let y1 = self.y + self.stack.at(i + 1);
            let x2 = x1 + self.stack.at(i + 2);
            let y2 = y1 + self.stack.at(i + 3);
            self.x = x2 + self.stack.at(i + 4);
            self.y = y2 + self.stack.at(i + 5);

            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
            i += 6;
        }

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_curve_line(&mut self) -> Result<(), CFFError> {
        // {dxa dya dxb dyb dxc dyc}+ dxd dyd

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() < 8 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        if (self.stack.len() - 2) % 6 != 0 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let mut i = 0;
        while i < self.stack.len() - 2 {
            let x1 = self.x + self.stack.at(i + 0);
            let y1 = self.y + self.stack.at(i + 1);
            let x2 = x1 + self.stack.at(i + 2);
            let y2 = y1 + self.stack.at(i + 3);
            self.x = x2 + self.stack.at(i + 4);
            self.y = y2 + self.stack.at(i + 5);

            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
            i += 6;
        }

        self.x += self.stack.at(i + 0);
        self.y += self.stack.at(i + 1);
        self.builder.line_to(self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_line_curve(&mut self) -> Result<(), CFFError> {
        // {dxa dya}+ dxb dyb dxc dyc dxd dyd

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() < 8 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        if (self.stack.len() - 6).is_odd() {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let mut i = 0;
        while i < self.stack.len() - 6 {
            self.x += self.stack.at(i + 0);
            self.y += self.stack.at(i + 1);

            self.builder.line_to(self.x, self.y);
            i += 2;
        }

        let x1 = self.x + self.stack.at(i + 0);
        let y1 = self.y + self.stack.at(i + 1);
        let x2 = x1 + self.stack.at(i + 2);
        let y2 = y1 + self.stack.at(i + 3);
        self.x = x2 + self.stack.at(i + 4);
        self.y = y2 + self.stack.at(i + 5);
        self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_hh_curve_to(&mut self) -> Result<(), CFFError> {
        // dy1? {dxa dxb dyb dxc}+

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        let mut i = 0;

        // The odd argument count indicates an Y position.
        if self.stack.len().is_odd() {
            self.y += self.stack.at(0);
            i += 1;
        }

        if (self.stack.len() - i) % 4 != 0 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        while i < self.stack.len() {
            let x1 = self.x + self.stack.at(i + 0);
            let y1 = self.y;
            let x2 = x1 + self.stack.at(i + 1);
            let y2 = y1 + self.stack.at(i + 2);
            self.x = x2 + self.stack.at(i + 3);
            self.y = y2;

            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
            i += 4;
        }

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_vv_curve_to(&mut self) -> Result<(), CFFError> {
        // dx1? {dya dxb dyb dyc}+

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        let mut i = 0;

        // The odd argument count indicates an X position.
        if self.stack.len().is_odd() {
            self.x += self.stack.at(0);
            i += 1;
        }

        if (self.stack.len() - i) % 4 != 0 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        while i < self.stack.len() {
            let x1 = self.x;
            let y1 = self.y + self.stack.at(i + 0);
            let x2 = x1 + self.stack.at(i + 1);
            let y2 = y1 + self.stack.at(i + 2);
            self.x = x2;
            self.y = y2 + self.stack.at(i + 3);

            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
            i += 4;
        }

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_hv_curve_to(&mut self) -> Result<(), CFFError> {
        // dx1 dx2 dy2 dy3 {dya dxb dyb dxc dxd dxe dye dyf}* dxf?
        //                 {dxa dxb dyb dyc dyd dxe dye dxf}+ dyf?

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() < 4 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        self.stack.reverse();
        while !self.stack.is_empty() {
            if self.stack.len() < 4 {
                return Err(CFFError::InvalidArgumentsStackLength);
            }

            let x1 = self.x + self.stack.pop();
            let y1 = self.y;
            let x2 = x1 + self.stack.pop();
            let y2 = y1 + self.stack.pop();
            self.y = y2 + self.stack.pop();
            self.x = x2;
            if self.stack.len() == 1 {
                self.x += self.stack.pop();
            }
            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
            if self.stack.is_empty() {
                break;
            }

            if self.stack.len() < 4 {
                return Err(CFFError::InvalidArgumentsStackLength);
            }

            let x1 = self.x;
            let y1 = self.y + self.stack.pop();
            let x2 = x1 + self.stack.pop();
            let y2 = y1 + self.stack.pop();
            self.x = x2 + self.stack.pop();
            self.y = y2;
            if self.stack.len() == 1 {
                self.y += self.stack.pop()
            }
            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
        }

        debug_assert!(self.stack.is_empty());
        Ok(())
    }

    #[inline]
    pub fn parse_vh_curve_to(&mut self) -> Result<(), CFFError> {
        // dy1 dx2 dy2 dx3 {dxa dxb dyb dyc dyd dxe dye dxf}* dyf?
        //                 {dya dxb dyb dxc dxd dxe dye dyf}+ dxf?

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() < 4 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        self.stack.reverse();
        while !self.stack.is_empty() {
            if self.stack.len() < 4 {
                return Err(CFFError::InvalidArgumentsStackLength);
            }

            let x1 = self.x;
            let y1 = self.y + self.stack.pop();
            let x2 = x1 + self.stack.pop();
            let y2 = y1 + self.stack.pop();
            self.x = x2 + self.stack.pop();
            self.y = y2;
            if self.stack.len() == 1 {
                self.y += self.stack.pop();
            }
            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
            if self.stack.is_empty() {
                break;
            }

            if self.stack.len() < 4 {
                return Err(CFFError::InvalidArgumentsStackLength);
            }

            let x1 = self.x + self.stack.pop();
            let y1 = self.y;
            let x2 = x1 + self.stack.pop();
            let y2 = y1 + self.stack.pop();
            self.y = y2 + self.stack.pop();
            self.x = x2;
            if self.stack.len() == 1 {
                self.x += self.stack.pop();
            }
            self.builder.curve_to(x1, y1, x2, y2, self.x, self.y);
        }

        debug_assert!(self.stack.is_empty());
        Ok(())
    }

    #[inline]
    pub fn parse_flex(&mut self) -> Result<(), CFFError> {
        // dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 dx6 dy6 fd

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() != 13 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let dx1 = self.x + self.stack.at(0);
        let dy1 = self.y + self.stack.at(1);
        let dx2 = dx1 + self.stack.at(2);
        let dy2 = dy1 + self.stack.at(3);
        let dx3 = dx2 + self.stack.at(4);
        let dy3 = dy2 + self.stack.at(5);
        let dx4 = dx3 + self.stack.at(6);
        let dy4 = dy3 + self.stack.at(7);
        let dx5 = dx4 + self.stack.at(8);
        let dy5 = dy4 + self.stack.at(9);
        self.x = dx5 + self.stack.at(10);
        self.y = dy5 + self.stack.at(11);
        self.builder.curve_to(dx1, dy1, dx2, dy2, dx3, dy3);
        self.builder.curve_to(dx4, dy4, dx5, dy5, self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_flex1(&mut self) -> Result<(), CFFError> {
        // dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 d6

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() != 11 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let dx1 = self.x + self.stack.at(0);
        let dy1 = self.y + self.stack.at(1);
        let dx2 = dx1 + self.stack.at(2);
        let dy2 = dy1 + self.stack.at(3);
        let dx3 = dx2 + self.stack.at(4);
        let dy3 = dy2 + self.stack.at(5);
        let dx4 = dx3 + self.stack.at(6);
        let dy4 = dy3 + self.stack.at(7);
        let dx5 = dx4 + self.stack.at(8);
        let dy5 = dy4 + self.stack.at(9);

        if f32_abs(dx5 - self.x) > f32_abs(dy5 - self.y) {
            self.x = dx5 + self.stack.at(10);
        } else {
            self.y = dy5 + self.stack.at(10);
        }

        self.builder.curve_to(dx1, dy1, dx2, dy2, dx3, dy3);
        self.builder.curve_to(dx4, dy4, dx5, dy5, self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_hflex(&mut self) -> Result<(), CFFError> {
        // dx1 dx2 dy2 dx3 dx4 dx5 dx6

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() != 7 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let dx1 = self.x + self.stack.at(0);
        let dy1 = self.y;
        let dx2 = dx1 + self.stack.at(1);
        let dy2 = dy1 + self.stack.at(2);
        let dx3 = dx2 + self.stack.at(3);
        let dy3 = dy2;
        let dx4 = dx3 + self.stack.at(4);
        let dy4 = dy2;
        let dx5 = dx4 + self.stack.at(5);
        let dy5 = self.y;
        self.x = dx5 + self.stack.at(6);
        self.builder.curve_to(dx1, dy1, dx2, dy2, dx3, dy3);
        self.builder.curve_to(dx4, dy4, dx5, dy5, self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_hflex1(&mut self) -> Result<(), CFFError> {
        // dx1 dy1 dx2 dy2 dx3 dx4 dx5 dy5 dx6

        if !self.has_move_to {
            return Err(CFFError::MissingMoveTo);
        }

        if self.stack.len() != 9 {
            return Err(CFFError::InvalidArgumentsStackLength);
        }

        let dx1 = self.x + self.stack.at(0);
        let dy1 = self.y + self.stack.at(1);
        let dx2 = dx1 + self.stack.at(2);
        let dy2 = dy1 + self.stack.at(3);
        let dx3 = dx2 + self.stack.at(4);
        let dy3 = dy2;
        let dx4 = dx3 + self.stack.at(5);
        let dy4 = dy2;
        let dx5 = dx4 + self.stack.at(6);
        let dy5 = dy4 + self.stack.at(7);
        self.x = dx5 + self.stack.at(8);
        self.builder.curve_to(dx1, dy1, dx2, dy2, dx3, dy3);
        self.builder.curve_to(dx4, dy4, dx5, dy5, self.x, self.y);

        self.stack.clear();
        Ok(())
    }

    #[inline]
    pub fn parse_int1(&mut self, op: u8) -> Result<(), CFFError> {
        let n = i16::from(op) - 139;
        self.stack.push(f32::from(n))?;
        Ok(())
    }

    #[inline]
    pub fn parse_int2(&mut self, op: u8, s: &mut Stream) -> Result<(), CFFError> {
        let b1 = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?;
        let n = (i16::from(op) - 247) * 256 + i16::from(b1) + 108;
        debug_assert!((108..=1131).contains(&n));
        self.stack.push(f32::from(n))?;
        Ok(())
    }

    #[inline]
    pub fn parse_int3(&mut self, op: u8, s: &mut Stream) -> Result<(), CFFError> {
        let b1 = s.read::<u8>().ok_or(CFFError::ReadOutOfBounds)?;
        let n = -(i16::from(op) - 251) * 256 - i16::from(b1) - 108;
        debug_assert!((-1131..=-108).contains(&n));
        self.stack.push(f32::from(n))?;
        Ok(())
    }

    #[inline]
    pub fn parse_fixed(&mut self, s: &mut Stream) -> Result<(), CFFError> {
        let n = s.read::<Fixed>().ok_or(CFFError::ReadOutOfBounds)?;
        self.stack.push(n.0)?;
        Ok(())
    }
}