stet-fonts 0.7.0

Pure-Rust Type 1, CFF / Type 2, and TrueType font parsers plus geometry types, usable standalone or inside stet's PostScript and PDF pipelines
Documentation
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
// stet - A PostScript Interpreter
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Type 2 charstring interpreter for CFF fonts.
//!
//! Executes Type 2 charstring opcodes (Adobe TN#5177) to produce path segments
//! and glyph width information. Uses the same `CharstringResult` return type as
//! the Type 1 interpreter (`charstring.rs`).
//!
//! Key differences from Type 1:
//! - No encryption (raw bytes)
//! - Width from optional first stack operand (not hsbw/sbw)
//! - Multi-arg path ops (rlineto takes N pairs, etc.)
//! - Built-in flex (hflex/flex/hflex1/flex1)
//! - Implicit subpath close on moveto/endchar
//! - 16.16 fixed-point numbers (byte 255)
//! - Transient array (32 entries) for arithmetic ops

use crate::charstring::CharstringResult;
use crate::geometry::{PathSegment, PsPath};

/// Calculate subroutine bias per Type 2 spec.
fn subr_bias(n_subrs: usize) -> i32 {
    if n_subrs < 1240 {
        107
    } else if n_subrs < 33900 {
        1131
    } else {
        32768
    }
}

/// Execute a Type 2 charstring and produce path segments + width.
///
/// If `width_only` is true, path operations are skipped — only width is extracted.
pub fn execute_type2_charstring(
    data: &[u8],
    local_subrs: &[Vec<u8>],
    global_subrs: &[Vec<u8>],
    default_width_x: f64,
    nominal_width_x: f64,
    width_only: bool,
) -> Result<CharstringResult, String> {
    let mut state = T2State {
        stack: Vec::with_capacity(48),
        path: PsPath {
            segments: Vec::new(),
        },
        current_x: 0.0,
        current_y: 0.0,
        advance_width: default_width_x,
        width_parsed: false,
        default_width_x,
        nominal_width_x,
        num_h_hints: 0,
        num_v_hints: 0,
        transient: [0.0; 32],
        width_only,
        has_path: false,
        seac: None,
    };

    execute_bytes(&mut state, data, local_subrs, global_subrs, 0)?;

    Ok(CharstringResult {
        path: state.path,
        width_x: state.advance_width,
        width_y: 0.0,
        lsb_x: 0.0,
        lsb_y: 0.0,
        seac: state.seac,
    })
}

/// Internal interpreter state.
struct T2State {
    stack: Vec<f64>,
    path: PsPath,
    current_x: f64,
    current_y: f64,
    advance_width: f64,
    width_parsed: bool,
    default_width_x: f64,
    nominal_width_x: f64,
    num_h_hints: usize,
    num_v_hints: usize,
    transient: [f64; 32],
    width_only: bool,
    /// Whether we have emitted any path segments (for implicit close).
    has_path: bool,
    /// Deprecated seac from endchar with 4 args: (adx, ady, bchar, achar).
    seac: Option<(f64, f64, u8, u8)>,
}

/// Execute a charstring byte stream (recursive for subroutine calls).
fn execute_bytes(
    state: &mut T2State,
    data: &[u8],
    local_subrs: &[Vec<u8>],
    global_subrs: &[Vec<u8>],
    depth: usize,
) -> Result<(), String> {
    if depth > 10 {
        return Err("Type 2 subroutine call stack overflow".into());
    }

    let mut i = 0;
    let length = data.len();

    while i < length {
        let b0 = data[i];

        // Operators: 0-27 and 29-31 (28 is a number)
        if b0 <= 27 || (29..=31).contains(&b0) {
            if b0 == 12 {
                // Two-byte operator
                i += 1;
                if i >= length {
                    break;
                }
                let b1 = data[i];
                exec_op12(state, b1, local_subrs, global_subrs, depth)?;
                i += 1;
            } else if b0 == 19 || b0 == 20 {
                // hintmask / cntrmask — consume implicit vstems, then skip mask bytes
                handle_hint_mask(state);
                i += 1;
                let n_mask_bytes = (state.num_h_hints + state.num_v_hints).div_ceil(8);
                i += n_mask_bytes;
            } else {
                exec_op(state, b0, local_subrs, global_subrs, depth)?;
                i += 1;
            }
        } else if (32..=246).contains(&b0) {
            state.stack.push((b0 as i32 - 139) as f64);
            i += 1;
        } else if (247..=250).contains(&b0) {
            if i + 1 >= length {
                break;
            }
            let b1 = data[i + 1];
            state
                .stack
                .push(((b0 as i32 - 247) * 256 + b1 as i32 + 108) as f64);
            i += 2;
        } else if (251..=254).contains(&b0) {
            if i + 1 >= length {
                break;
            }
            let b1 = data[i + 1];
            state
                .stack
                .push((-(b0 as i32 - 251) * 256 - b1 as i32 - 108) as f64);
            i += 2;
        } else if b0 == 255 {
            // 16.16 fixed-point number
            if i + 4 >= length {
                break;
            }
            let raw = i32::from_be_bytes([data[i + 1], data[i + 2], data[i + 3], data[i + 4]]);
            state.stack.push(raw as f64 / 65536.0);
            i += 5;
        } else if b0 == 28 {
            // 3-byte signed integer
            if i + 2 >= length {
                break;
            }
            let val = i16::from_be_bytes([data[i + 1], data[i + 2]]);
            state.stack.push(val as f64);
            i += 3;
        } else {
            i += 1;
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Width handling
// ---------------------------------------------------------------------------

/// Check for optional width argument before first stack-clearing operator.
fn check_width(state: &mut T2State, expected_args: usize) {
    if state.width_parsed {
        return;
    }
    state.width_parsed = true;

    if state.stack.len() > expected_args {
        let w = state.stack.remove(0);
        state.advance_width = w + state.nominal_width_x;
    } else {
        state.advance_width = state.default_width_x;
    }
}

// ---------------------------------------------------------------------------
// Path helpers
// ---------------------------------------------------------------------------

/// Close the current subpath (Type 2 implicitly closes on moveto/endchar).
fn close_subpath(state: &mut T2State) {
    if state.width_only {
        return;
    }
    // Add ClosePath if we have path segments and the last wasn't already a close
    if let Some(last) = state.path.segments.last()
        && !matches!(last, PathSegment::ClosePath | PathSegment::MoveTo(_, _))
    {
        state.path.segments.push(PathSegment::ClosePath);
    }
}

fn do_moveto(state: &mut T2State, dx: f64, dy: f64) {
    state.current_x += dx;
    state.current_y += dy;
    if state.width_only {
        return;
    }
    // Implicit close of previous subpath
    if state.has_path {
        close_subpath(state);
    }
    state
        .path
        .segments
        .push(PathSegment::MoveTo(state.current_x, state.current_y));
    state.has_path = true;
}

fn do_lineto(state: &mut T2State, dx: f64, dy: f64) {
    state.current_x += dx;
    state.current_y += dy;
    if state.width_only {
        return;
    }
    state
        .path
        .segments
        .push(PathSegment::LineTo(state.current_x, state.current_y));
}

fn do_curveto(state: &mut T2State, dx1: f64, dy1: f64, dx2: f64, dy2: f64, dx3: f64, dy3: f64) {
    let x1 = state.current_x + dx1;
    let y1 = state.current_y + dy1;
    let x2 = x1 + dx2;
    let y2 = y1 + dy2;
    let x3 = x2 + dx3;
    let y3 = y2 + dy3;
    state.current_x = x3;
    state.current_y = y3;
    if state.width_only {
        return;
    }
    state.path.segments.push(PathSegment::CurveTo {
        x1,
        y1,
        x2,
        y2,
        x3,
        y3,
    });
}

// ---------------------------------------------------------------------------
// Single-byte operator dispatch
// ---------------------------------------------------------------------------

fn exec_op(
    state: &mut T2State,
    op: u8,
    local_subrs: &[Vec<u8>],
    global_subrs: &[Vec<u8>],
    depth: usize,
) -> Result<(), String> {
    match op {
        1 => op_hstem(state),                                         // hstem
        3 => op_vstem(state),                                         // vstem
        4 => op_vmoveto(state),                                       // vmoveto
        5 => op_rlineto(state),                                       // rlineto
        6 => op_hlineto(state),                                       // hlineto
        7 => op_vlineto(state),                                       // vlineto
        8 => op_rrcurveto(state),                                     // rrcurveto
        10 => op_callsubr(state, local_subrs, global_subrs, depth)?,  // callsubr
        11 => {}                    // return (handled by call recursion)
        14 => op_endchar(state),    // endchar
        18 => op_hstemhm(state),    // hstemhm
        21 => op_rmoveto(state),    // rmoveto
        22 => op_hmoveto(state),    // hmoveto
        23 => op_vstemhm(state),    // vstemhm
        24 => op_rcurveline(state), // rcurveline
        25 => op_rlinecurve(state), // rlinecurve
        26 => op_vvcurveto(state),  // vvcurveto
        27 => op_hhcurveto(state),  // hhcurveto
        29 => op_callgsubr(state, local_subrs, global_subrs, depth)?, // callgsubr
        30 => op_vhcurveto(state),  // vhcurveto
        31 => op_hvcurveto(state),  // hvcurveto
        _ => {}                     // unknown — ignore
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Hint operators
// ---------------------------------------------------------------------------

fn op_hstem(state: &mut T2State) {
    let n_pairs = state.stack.len() / 2;
    check_width(state, n_pairs * 2);
    state.num_h_hints += state.stack.len() / 2;
    state.stack.clear();
}

fn op_vstem(state: &mut T2State) {
    let n_pairs = state.stack.len() / 2;
    check_width(state, n_pairs * 2);
    state.num_v_hints += state.stack.len() / 2;
    state.stack.clear();
}

fn op_hstemhm(state: &mut T2State) {
    let n_pairs = state.stack.len() / 2;
    check_width(state, n_pairs * 2);
    state.num_h_hints += state.stack.len() / 2;
    state.stack.clear();
}

fn op_vstemhm(state: &mut T2State) {
    let n_pairs = state.stack.len() / 2;
    check_width(state, n_pairs * 2);
    state.num_v_hints += state.stack.len() / 2;
    state.stack.clear();
}

fn handle_hint_mask(state: &mut T2State) {
    if !state.stack.is_empty() {
        let n_pairs = state.stack.len() / 2;
        check_width(state, n_pairs * 2);
        state.num_v_hints += state.stack.len() / 2;
        state.stack.clear();
    } else if !state.width_parsed {
        check_width(state, 0);
    }
}

// ---------------------------------------------------------------------------
// Path construction operators
// ---------------------------------------------------------------------------

fn op_rmoveto(state: &mut T2State) {
    check_width(state, 2);
    if state.stack.len() < 2 {
        state.stack.clear();
        return;
    }
    let dy = state.stack.pop().unwrap();
    let dx = state.stack.pop().unwrap();
    state.stack.clear();
    do_moveto(state, dx, dy);
}

fn op_hmoveto(state: &mut T2State) {
    check_width(state, 1);
    if state.stack.is_empty() {
        return;
    }
    let dx = state.stack.pop().unwrap();
    state.stack.clear();
    do_moveto(state, dx, 0.0);
}

fn op_vmoveto(state: &mut T2State) {
    check_width(state, 1);
    if state.stack.is_empty() {
        return;
    }
    let dy = state.stack.pop().unwrap();
    state.stack.clear();
    do_moveto(state, 0.0, dy);
}

fn op_rlineto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    let mut i = 0;
    while i + 1 < args.len() {
        do_lineto(state, args[i], args[i + 1]);
        i += 2;
    }
}

fn op_hlineto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    let mut horizontal = true;
    for val in args {
        if horizontal {
            do_lineto(state, val, 0.0);
        } else {
            do_lineto(state, 0.0, val);
        }
        horizontal = !horizontal;
    }
}

fn op_vlineto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    let mut vertical = true;
    for val in args {
        if vertical {
            do_lineto(state, 0.0, val);
        } else {
            do_lineto(state, val, 0.0);
        }
        vertical = !vertical;
    }
}

fn op_rrcurveto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    let mut i = 0;
    while i + 5 < args.len() {
        do_curveto(
            state,
            args[i],
            args[i + 1],
            args[i + 2],
            args[i + 3],
            args[i + 4],
            args[i + 5],
        );
        i += 6;
    }
}

fn op_hhcurveto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    let mut i = 0;
    let mut dy1_extra = 0.0;
    if !args.len().is_multiple_of(4) {
        dy1_extra = args[0];
        i = 1;
    }
    while i + 3 < args.len() {
        let dxa = args[i];
        let dxb = args[i + 1];
        let dyb = args[i + 2];
        let dxc = args[i + 3];
        do_curveto(state, dxa, dy1_extra, dxb, dyb, dxc, 0.0);
        dy1_extra = 0.0;
        i += 4;
    }
}

fn op_vvcurveto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    let mut i = 0;
    let mut dx1_extra = 0.0;
    if !args.len().is_multiple_of(4) {
        dx1_extra = args[0];
        i = 1;
    }
    while i + 3 < args.len() {
        let dya = args[i];
        let dxb = args[i + 1];
        let dyb = args[i + 2];
        let dyc = args[i + 3];
        do_curveto(state, dx1_extra, dya, dxb, dyb, 0.0, dyc);
        dx1_extra = 0.0;
        i += 4;
    }
}

fn op_hvcurveto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    alternating_curves(state, &args, true);
}

fn op_vhcurveto(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    alternating_curves(state, &args, false);
}

/// Shared logic for hvcurveto / vhcurveto.
fn alternating_curves(state: &mut T2State, args: &[f64], start_horizontal: bool) {
    let mut i = 0;
    let mut phase = start_horizontal;
    let n = args.len();

    while i + 3 < n {
        let remaining = n - i;
        let is_last = remaining < 9;

        if phase {
            // H-start: dx1 dx2 dy2 dy3 [dxf]
            let dx1 = args[i];
            let dx2 = args[i + 1];
            let dy2 = args[i + 2];
            let dy3 = args[i + 3];
            let dxf = if is_last && remaining == 5 {
                args[i + 4]
            } else {
                0.0
            };
            do_curveto(state, dx1, 0.0, dx2, dy2, dxf, dy3);
            i += if is_last && remaining == 5 { 5 } else { 4 };
        } else {
            // V-start: dy1 dx2 dy2 dx3 [dyf]
            let dy1 = args[i];
            let dx2 = args[i + 1];
            let dy2 = args[i + 2];
            let dx3 = args[i + 3];
            let dyf = if is_last && remaining == 5 {
                args[i + 4]
            } else {
                0.0
            };
            do_curveto(state, 0.0, dy1, dx2, dy2, dx3, dyf);
            i += if is_last && remaining == 5 { 5 } else { 4 };
        }
        phase = !phase;
    }
}

fn op_rcurveline(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    if args.len() < 2 {
        return;
    }
    let curve_end = args.len() - 2;
    let mut i = 0;
    while i + 5 <= curve_end {
        do_curveto(
            state,
            args[i],
            args[i + 1],
            args[i + 2],
            args[i + 3],
            args[i + 4],
            args[i + 5],
        );
        i += 6;
    }
    if i + 1 < args.len() {
        do_lineto(state, args[i], args[i + 1]);
    }
}

fn op_rlinecurve(state: &mut T2State) {
    let args: Vec<f64> = state.stack.drain(..).collect();
    if args.len() < 6 {
        return;
    }
    let curve_start = args.len() - 6;
    let mut i = 0;
    while i < curve_start {
        do_lineto(state, args[i], args[i + 1]);
        i += 2;
    }
    do_curveto(
        state,
        args[curve_start],
        args[curve_start + 1],
        args[curve_start + 2],
        args[curve_start + 3],
        args[curve_start + 4],
        args[curve_start + 5],
    );
}

// ---------------------------------------------------------------------------
// endchar
// ---------------------------------------------------------------------------

fn op_endchar(state: &mut T2State) {
    // Deprecated seac: endchar with 4+ args = [width] adx ady bchar achar endchar
    // With width: 5 args. Without width: 4 args.
    let seac_args = if state.stack.len() >= 4 {
        // Extract seac args from the END of the stack first
        let n = state.stack.len();
        let adx = state.stack[n - 4];
        let ady = state.stack[n - 3];
        let bchar = state.stack[n - 2] as u8;
        let achar = state.stack[n - 1] as u8;
        // If 5+ args, first arg is width
        if n >= 5 && !state.width_parsed {
            state.width_parsed = true;
            state.advance_width = state.stack[0] + state.nominal_width_x;
        } else if !state.width_parsed {
            state.width_parsed = true;
            state.advance_width = state.default_width_x;
        }
        Some((adx, ady, bchar, achar))
    } else {
        if !state.width_parsed {
            check_width(state, 0);
        }
        None
    };
    state.stack.clear();
    state.seac = seac_args;

    if state.width_only {
        return;
    }

    // Implicit close of final subpath
    close_subpath(state);
}

// ---------------------------------------------------------------------------
// Subroutine operators
// ---------------------------------------------------------------------------

fn op_callsubr(
    state: &mut T2State,
    local_subrs: &[Vec<u8>],
    global_subrs: &[Vec<u8>],
    depth: usize,
) -> Result<(), String> {
    if state.stack.is_empty() {
        return Ok(());
    }
    // `as i32` saturates at the extremes, and Type 2 has arithmetic
    // operators, so a charstring can drive this to `i32::MAX` and make the
    // bias add overflow.
    let idx = state.stack.pop().unwrap() as i32;
    let bias = subr_bias(local_subrs.len());
    let Some(biased) = idx.checked_add(bias) else {
        return Ok(());
    };
    let biased = biased as usize;
    if biased < local_subrs.len() {
        let subr_data = local_subrs[biased].clone();
        execute_bytes(state, &subr_data, local_subrs, global_subrs, depth + 1)?;
    }
    Ok(())
}

fn op_callgsubr(
    state: &mut T2State,
    local_subrs: &[Vec<u8>],
    global_subrs: &[Vec<u8>],
    depth: usize,
) -> Result<(), String> {
    if state.stack.is_empty() {
        return Ok(());
    }
    // See `op_callsubr` — the same saturating cast and bias add.
    let idx = state.stack.pop().unwrap() as i32;
    let bias = subr_bias(global_subrs.len());
    let Some(biased) = idx.checked_add(bias) else {
        return Ok(());
    };
    let biased = biased as usize;
    if biased < global_subrs.len() {
        let subr_data = global_subrs[biased].clone();
        execute_bytes(state, &subr_data, local_subrs, global_subrs, depth + 1)?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Two-byte operator dispatch (12, N)
// ---------------------------------------------------------------------------

fn exec_op12(
    state: &mut T2State,
    sub_op: u8,
    local_subrs: &[Vec<u8>],
    global_subrs: &[Vec<u8>],
    depth: usize,
) -> Result<(), String> {
    match sub_op {
        0 => {} // dotsection — deprecated, no-op
        3 => op12_and(state),
        4 => op12_or(state),
        5 => op12_not(state),
        9 => op12_abs(state),
        10 => op12_add(state),
        11 => op12_sub(state),
        12 => op12_div(state),
        14 => op12_neg(state),
        15 => op12_eq(state),
        18 => op12_drop(state),
        20 => op12_put(state),
        21 => op12_get(state),
        22 => op12_ifelse(state),
        23 => op12_random(state),
        24 => op12_mul(state),
        26 => op12_sqrt(state),
        27 => op12_dup(state),
        28 => op12_exch(state),
        29 => op12_index(state),
        30 => op12_roll(state),
        34 => op12_hflex(state),
        35 => op12_flex(state, local_subrs, global_subrs, depth)?,
        36 => op12_hflex1(state),
        37 => op12_flex1(state),
        _ => {} // unknown — ignore
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Flex operators (12, 34-37)
// ---------------------------------------------------------------------------

fn op12_hflex(state: &mut T2State) {
    if state.stack.len() < 7 {
        state.stack.clear();
        return;
    }
    let dx1 = state.stack[0];
    let dx2 = state.stack[1];
    let dy2 = state.stack[2];
    let dx3 = state.stack[3];
    let dx4 = state.stack[4];
    let dx5 = state.stack[5];
    let dx6 = state.stack[6];
    state.stack.clear();
    do_curveto(state, dx1, 0.0, dx2, dy2, dx3, 0.0);
    do_curveto(state, dx4, 0.0, dx5, -dy2, dx6, 0.0);
}

#[allow(unused_variables)]
fn op12_flex(
    state: &mut T2State,
    local_subrs: &[Vec<u8>],
    global_subrs: &[Vec<u8>],
    depth: usize,
) -> Result<(), String> {
    if state.stack.len() < 13 {
        state.stack.clear();
        return Ok(());
    }
    let dx1 = state.stack[0];
    let dy1 = state.stack[1];
    let dx2 = state.stack[2];
    let dy2 = state.stack[3];
    let dx3 = state.stack[4];
    let dy3 = state.stack[5];
    let dx4 = state.stack[6];
    let dy4 = state.stack[7];
    let dx5 = state.stack[8];
    let dy5 = state.stack[9];
    let dx6 = state.stack[10];
    let dy6 = state.stack[11];
    // fd = state.stack[12] — flex depth, not used for rendering
    state.stack.clear();
    do_curveto(state, dx1, dy1, dx2, dy2, dx3, dy3);
    do_curveto(state, dx4, dy4, dx5, dy5, dx6, dy6);
    Ok(())
}

fn op12_hflex1(state: &mut T2State) {
    if state.stack.len() < 9 {
        state.stack.clear();
        return;
    }
    let dx1 = state.stack[0];
    let dy1 = state.stack[1];
    let dx2 = state.stack[2];
    let dy2 = state.stack[3];
    let dx3 = state.stack[4];
    let dx4 = state.stack[5];
    let dx5 = state.stack[6];
    let dy5 = state.stack[7];
    let dx6 = state.stack[8];
    state.stack.clear();
    do_curveto(state, dx1, dy1, dx2, dy2, dx3, 0.0);
    do_curveto(state, dx4, 0.0, dx5, dy5, dx6, -(dy1 + dy2 + dy5));
}

fn op12_flex1(state: &mut T2State) {
    if state.stack.len() < 11 {
        state.stack.clear();
        return;
    }
    let dx1 = state.stack[0];
    let dy1 = state.stack[1];
    let dx2 = state.stack[2];
    let dy2 = state.stack[3];
    let dx3 = state.stack[4];
    let dy3 = state.stack[5];
    let dx4 = state.stack[6];
    let dy4 = state.stack[7];
    let dx5 = state.stack[8];
    let dy5 = state.stack[9];
    let d6 = state.stack[10];
    state.stack.clear();

    let sum_dx = dx1 + dx2 + dx3 + dx4 + dx5;
    let sum_dy = dy1 + dy2 + dy3 + dy4 + dy5;

    let (dx6, dy6) = if sum_dx.abs() > sum_dy.abs() {
        (d6, -sum_dy)
    } else {
        (-sum_dx, d6)
    };

    do_curveto(state, dx1, dy1, dx2, dy2, dx3, dy3);
    do_curveto(state, dx4, dy4, dx5, dy5, dx6, dy6);
}

// ---------------------------------------------------------------------------
// Arithmetic operators (12, N)
// ---------------------------------------------------------------------------

fn op12_abs(state: &mut T2State) {
    if let Some(v) = state.stack.last_mut() {
        *v = v.abs();
    }
}

fn op12_add(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let b = state.stack.pop().unwrap();
        let a = state.stack.pop().unwrap();
        state.stack.push(a + b);
    }
}

fn op12_sub(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let b = state.stack.pop().unwrap();
        let a = state.stack.pop().unwrap();
        state.stack.push(a - b);
    }
}

fn op12_div(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let b = state.stack.pop().unwrap();
        let a = state.stack.pop().unwrap();
        state.stack.push(if b != 0.0 { a / b } else { 0.0 });
    }
}

fn op12_neg(state: &mut T2State) {
    if let Some(v) = state.stack.last_mut() {
        *v = -*v;
    }
}

fn op12_mul(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let b = state.stack.pop().unwrap();
        let a = state.stack.pop().unwrap();
        state.stack.push(a * b);
    }
}

fn op12_sqrt(state: &mut T2State) {
    if let Some(v) = state.stack.last_mut() {
        *v = v.abs().sqrt();
    }
}

fn op12_random(state: &mut T2State) {
    state.stack.push(1.0); // Simplification
}

// ---------------------------------------------------------------------------
// Logic operators (12, N)
// ---------------------------------------------------------------------------

fn op12_and(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let b = state.stack.pop().unwrap();
        let a = state.stack.pop().unwrap();
        state
            .stack
            .push(if a != 0.0 && b != 0.0 { 1.0 } else { 0.0 });
    }
}

fn op12_or(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let b = state.stack.pop().unwrap();
        let a = state.stack.pop().unwrap();
        state
            .stack
            .push(if a != 0.0 || b != 0.0 { 1.0 } else { 0.0 });
    }
}

fn op12_not(state: &mut T2State) {
    if let Some(a) = state.stack.pop() {
        state.stack.push(if a == 0.0 { 1.0 } else { 0.0 });
    }
}

fn op12_eq(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let b = state.stack.pop().unwrap();
        let a = state.stack.pop().unwrap();
        state.stack.push(if a == b { 1.0 } else { 0.0 });
    }
}

fn op12_ifelse(state: &mut T2State) {
    if state.stack.len() >= 4 {
        let v2 = state.stack.pop().unwrap();
        let v1 = state.stack.pop().unwrap();
        let s2 = state.stack.pop().unwrap();
        let s1 = state.stack.pop().unwrap();
        state.stack.push(if v1 <= v2 { s1 } else { s2 });
    }
}

// ---------------------------------------------------------------------------
// Stack manipulation operators (12, N)
// ---------------------------------------------------------------------------

fn op12_drop(state: &mut T2State) {
    state.stack.pop();
}

fn op12_dup(state: &mut T2State) {
    if let Some(&v) = state.stack.last() {
        state.stack.push(v);
    }
}

fn op12_exch(state: &mut T2State) {
    let n = state.stack.len();
    if n >= 2 {
        state.stack.swap(n - 1, n - 2);
    }
}

fn op12_index(state: &mut T2State) {
    if let Some(idx_val) = state.stack.pop() {
        let idx = idx_val as i32;
        let idx = if idx < 0 { 0 } else { idx as usize };
        if idx < state.stack.len() {
            let val = state.stack[state.stack.len() - 1 - idx];
            state.stack.push(val);
        }
    }
}

fn op12_roll(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let j = state.stack.pop().unwrap() as i32;
        let n = state.stack.pop().unwrap() as usize;
        let slen = state.stack.len();
        if n > 0 && n <= slen {
            let start = slen - n;
            let mut subset: Vec<f64> = state.stack[start..].to_vec();
            let j = ((j % n as i32) + n as i32) as usize % n;
            subset.rotate_right(j);
            state.stack[start..].copy_from_slice(&subset);
        }
    }
}

// ---------------------------------------------------------------------------
// Storage operators (12, N) — transient array
// ---------------------------------------------------------------------------

fn op12_put(state: &mut T2State) {
    if state.stack.len() >= 2 {
        let i = state.stack.pop().unwrap() as usize;
        let val = state.stack.pop().unwrap();
        if i < 32 {
            state.transient[i] = val;
        }
    }
}

fn op12_get(state: &mut T2State) {
    if let Some(idx_val) = state.stack.pop() {
        let i = idx_val as usize;
        if i < 32 {
            state.stack.push(state.transient[i]);
        } else {
            state.stack.push(0.0);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_subr_bias() {
        assert_eq!(subr_bias(0), 107);
        assert_eq!(subr_bias(1239), 107);
        assert_eq!(subr_bias(1240), 1131);
        assert_eq!(subr_bias(33899), 1131);
        assert_eq!(subr_bias(33900), 32768);
    }

    #[test]
    fn test_simple_charstring() {
        // hmoveto(100), hlineto(200), endchar
        // 100 = 239 (b0-139 = 239-139 = 100)
        // 200 = 247 0 92 (108 + 92 = 200? No: (247-247)*256 + 92 + 108 = 200)
        // Actually: 200 = 247, 92 → (0)*256 + 92 + 108 = 200
        let data = [
            239u8, // 100
            22,    // hmoveto
            247, 92, // 200
            6,  // hlineto
            14, // endchar
        ];
        let result = execute_type2_charstring(&data, &[], &[], 0.0, 0.0, false).unwrap();
        // Width should be default (100 was consumed as width since hmoveto expects 1 arg
        // and we have 1 arg, so no extra width)
        assert_eq!(result.width_x, 0.0); // default_width_x
        assert_eq!(result.path.segments.len(), 3); // moveto, lineto, closepath
    }

    #[test]
    fn test_width_parsing() {
        // width=500, hmoveto(100): two args before hmoveto, first is width
        // 500 = 28, 0x01, 0xF4
        // 100 = 239
        let data = [
            28, 0x01, 0xF4, // 500 (width)
            239,  // 100 (dx)
            22,   // hmoveto
            14,   // endchar
        ];
        let result = execute_type2_charstring(&data, &[], &[], 0.0, 200.0, false).unwrap();
        // Width = 500 + nominal_width_x(200) = 700
        assert_eq!(result.width_x, 700.0);
    }

    #[test]
    fn test_width_only_mode() {
        // Same as above but width_only
        let data = [
            28, 0x01, 0xF4, // 500 (width)
            239,  // 100 (dx)
            22,   // hmoveto
            14,   // endchar
        ];
        let result = execute_type2_charstring(&data, &[], &[], 0.0, 200.0, true).unwrap();
        assert_eq!(result.width_x, 700.0);
        assert!(result.path.segments.is_empty()); // No path in width_only mode
    }
}