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
// stet - A PostScript Interpreter
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Type 1 charstring interpreter.
//!
//! Decrypts and executes Type 1 charstring opcodes to produce path segments
//! and glyph width information.
use crate::encoding::STANDARD_ENCODING;
use crate::geometry::{PathSegment, PsPath};
/// Result of executing a charstring: the glyph path and advance width.
pub struct CharstringResult {
pub path: PsPath,
pub width_x: f64,
pub width_y: f64,
pub lsb_x: f64,
pub lsb_y: f64,
/// Deprecated seac (Standard Encoding Accented Character) from endchar with 4 args.
/// Contains (adx, ady, bchar, achar) — Standard Encoding codes for base and accent.
pub seac: Option<(f64, f64, u8, u8)>,
}
/// Decrypt a charstring using the Type 1 charstring cipher (R=4330).
/// Skips the first `len_iv` random bytes.
pub fn decrypt_charstring(data: &[u8], len_iv: usize) -> Vec<u8> {
// len_iv == usize::MAX is a sentinel for /lenIV -1 (no encryption).
// Return raw bytes without decryption or prefix stripping.
if len_iv == usize::MAX {
return data.to_vec();
}
let c1: u32 = 52845;
let c2: u32 = 22719;
let mut r: u32 = 4330;
let mut result = Vec::with_capacity(data.len().saturating_sub(len_iv));
for (i, &cipher) in data.iter().enumerate() {
let plain = (cipher as u32 ^ (r >> 8)) as u8;
if i >= len_iv {
result.push(plain);
}
r = ((cipher as u32 + r) * c1 + c2) & 0xFFFF;
}
result
}
/// Charstring lookup function for seac composite character support.
/// Maps glyph name (bytes) to encrypted charstring bytes.
pub type CharstringLookup<'a> = dyn Fn(&str) -> Option<Vec<u8>> + 'a;
/// Execute a Type 1 charstring and produce path segments + width.
///
/// If `width_only` is true, path operations are skipped — only width is extracted.
/// If `cs_lookup` is provided, seac (composite characters) can look up component charstrings.
pub fn execute_charstring(
charstring: &[u8],
subrs: &[Vec<u8>],
len_iv: usize,
width_only: bool,
) -> Result<CharstringResult, String> {
execute_charstring_ex(charstring, subrs, len_iv, width_only, None)
}
/// Execute a Type 1 charstring with optional charstring lookup for seac support.
pub fn execute_charstring_ex(
charstring: &[u8],
subrs: &[Vec<u8>],
len_iv: usize,
width_only: bool,
cs_lookup: Option<&CharstringLookup<'_>>,
) -> Result<CharstringResult, String> {
execute_charstring_mm(charstring, subrs, len_iv, width_only, cs_lookup, None)
}
/// Execute a Type 1 charstring with Multiple Master weight vector support.
pub fn execute_charstring_mm(
charstring: &[u8],
subrs: &[Vec<u8>],
len_iv: usize,
width_only: bool,
cs_lookup: Option<&CharstringLookup<'_>>,
weight_vector: Option<&[f64]>,
) -> Result<CharstringResult, String> {
let decrypted = decrypt_charstring(charstring, len_iv);
let mut interp = CharstringInterp::new(subrs, len_iv, width_only, cs_lookup);
interp.weight_vector = weight_vector.map(|wv| wv.to_vec());
interp.execute(&decrypted)?;
Ok(CharstringResult {
path: interp.path,
width_x: interp.width_x,
width_y: interp.width_y,
lsb_x: interp.lsb_x,
lsb_y: interp.lsb_y,
seac: None,
})
}
/// Execute a charstring for seac (accent composition), applying an offset.
pub fn execute_charstring_with_offset(
charstring: &[u8],
subrs: &[Vec<u8>],
len_iv: usize,
offset_x: f64,
offset_y: f64,
) -> Result<CharstringResult, String> {
execute_charstring_with_offset_mm(charstring, subrs, len_iv, offset_x, offset_y, None)
}
/// Execute a charstring for seac with MM weight vector support.
pub fn execute_charstring_with_offset_mm(
charstring: &[u8],
subrs: &[Vec<u8>],
len_iv: usize,
offset_x: f64,
offset_y: f64,
weight_vector: Option<&[f64]>,
) -> Result<CharstringResult, String> {
let decrypted = decrypt_charstring(charstring, len_iv);
let mut interp = CharstringInterp::new(subrs, len_iv, false, None);
interp.x = offset_x;
interp.y = offset_y;
interp.weight_vector = weight_vector.map(|wv| wv.to_vec());
interp.execute(&decrypted)?;
Ok(CharstringResult {
path: interp.path,
width_x: interp.width_x,
width_y: interp.width_y,
lsb_x: interp.lsb_x,
lsb_y: interp.lsb_y,
seac: None,
})
}
/// Internal charstring interpreter state.
struct CharstringInterp<'a> {
stack: Vec<f64>,
path: PsPath,
x: f64,
y: f64,
width_x: f64,
width_y: f64,
lsb_x: f64,
lsb_y: f64,
subrs: &'a [Vec<u8>],
len_iv: usize,
width_only: bool,
done: bool,
// Flex support (OtherSubrs 0-3)
flex_active: bool,
flex_points: Vec<(f64, f64)>,
// OtherSubrs return stack (for pop operator)
ps_stack: Vec<f64>,
// Charstring lookup for seac composite character support
cs_lookup: Option<&'a CharstringLookup<'a>>,
// Multiple Master weight vector for blend OtherSubrs (14-17)
weight_vector: Option<Vec<f64>>,
// seac accent offset: when executing the accent component of a seac,
// hsbw/sbw adds this offset to the sidebearing instead of resetting
// the current point to zero.
seac_accent_offset: Option<(f64, f64)>,
}
impl<'a> CharstringInterp<'a> {
fn new(
subrs: &'a [Vec<u8>],
len_iv: usize,
width_only: bool,
cs_lookup: Option<&'a CharstringLookup<'a>>,
) -> Self {
Self {
stack: Vec::with_capacity(48),
path: PsPath::new(),
x: 0.0,
y: 0.0,
width_x: 0.0,
width_y: 0.0,
lsb_x: 0.0,
lsb_y: 0.0,
subrs,
len_iv,
width_only,
done: false,
flex_active: false,
flex_points: Vec::new(),
ps_stack: Vec::new(),
cs_lookup,
weight_vector: None,
seac_accent_offset: None,
}
}
fn execute(&mut self, data: &[u8]) -> Result<(), String> {
self.execute_inner(data, 0)
}
fn execute_inner(&mut self, data: &[u8], depth: usize) -> Result<(), String> {
if depth > 10 {
return Err("Charstring subroutine depth exceeded".to_string());
}
let mut pos = 0;
while pos < data.len() && !self.done {
let b = data[pos];
pos += 1;
match b {
// Commands (0–31)
0 => {} // reserved, ignore
1 => {
// hstem: y dy — ignore (hint), pop 2 args
if self.stack.len() >= 2 {
self.stack.pop();
self.stack.pop();
}
}
2 => {} // reserved
3 => {
// vstem: x dx — ignore (hint), pop 2 args
if self.stack.len() >= 2 {
self.stack.pop();
self.stack.pop();
}
}
4 => {
// vmoveto: dy
if self.stack.is_empty() {
return Err("vmoveto: stack underflow".to_string());
}
let dy = self.stack.pop().unwrap();
self.y += dy;
if !self.width_only && !self.flex_active {
self.path.segments.push(PathSegment::MoveTo(self.x, self.y));
}
// During flex, moveto just updates current point — OtherSubrs 2 handles flex_points
}
5 => {
// rlineto: dx dy
if self.stack.len() < 2 {
return Err("rlineto: stack underflow".to_string());
}
let dy = self.stack.pop().unwrap();
let dx = self.stack.pop().unwrap();
self.x += dx;
self.y += dy;
if !self.width_only {
self.path.segments.push(PathSegment::LineTo(self.x, self.y));
}
}
6 => {
// hlineto: dx
if self.stack.is_empty() {
return Err("hlineto: stack underflow".to_string());
}
let dx = self.stack.pop().unwrap();
self.x += dx;
if !self.width_only {
self.path.segments.push(PathSegment::LineTo(self.x, self.y));
}
}
7 => {
// vlineto: dy
if self.stack.is_empty() {
return Err("vlineto: stack underflow".to_string());
}
let dy = self.stack.pop().unwrap();
self.y += dy;
if !self.width_only {
self.path.segments.push(PathSegment::LineTo(self.x, self.y));
}
}
8 => {
// rrcurveto: dx1 dy1 dx2 dy2 dx3 dy3
if self.stack.len() < 6 {
return Err("rrcurveto: stack underflow".to_string());
}
let dy3 = self.stack.pop().unwrap();
let dx3 = self.stack.pop().unwrap();
let dy2 = self.stack.pop().unwrap();
let dx2 = self.stack.pop().unwrap();
let dy1 = self.stack.pop().unwrap();
let dx1 = self.stack.pop().unwrap();
let x1 = self.x + dx1;
let y1 = self.y + dy1;
let x2 = x1 + dx2;
let y2 = y1 + dy2;
let x3 = x2 + dx3;
let y3 = y2 + dy3;
if !self.width_only {
self.path.segments.push(PathSegment::CurveTo {
x1,
y1,
x2,
y2,
x3,
y3,
});
}
self.x = x3;
self.y = y3;
}
9 => {
// closepath
if !self.width_only {
self.path.segments.push(PathSegment::ClosePath);
}
}
10 => {
// callsubr: index
if self.stack.is_empty() {
return Err("callsubr: stack underflow".to_string());
}
let idx = self.stack.pop().unwrap() as usize;
if idx >= self.subrs.len() {
return Err(format!("callsubr: index {} out of range", idx));
}
let subr_data = decrypt_charstring(&self.subrs[idx], self.len_iv);
self.execute_inner(&subr_data, depth + 1)?;
}
11 => {
// return — return from subroutine
return Ok(());
}
12 => {
// Two-byte escape
if pos >= data.len() {
break;
}
let b2 = data[pos];
pos += 1;
self.execute_escape(b2, depth)?;
}
13 => {
// hsbw: sbx wx
// Sets sidebearing and width. Does NOT emit a MoveTo —
// the first real moveto in the glyph body will do that.
if self.stack.len() < 2 {
return Err("hsbw: stack underflow".to_string());
}
let wx = self.stack.pop().unwrap();
let sbx = self.stack.pop().unwrap();
self.lsb_x = sbx;
self.lsb_y = 0.0;
self.width_x = wx;
self.width_y = 0.0;
if let Some((ox, oy)) = self.seac_accent_offset {
// seac accent: offset from accent's sidebearing origin
self.x = sbx + ox;
self.y = oy;
} else {
self.x = sbx;
self.y = 0.0;
}
}
14 => {
// endchar — signal completion
if !self.width_only && !self.path.is_empty() {
// Implicit closepath if path is open
}
self.done = true;
return Ok(());
}
15..=20 => {} // reserved
21 => {
// rmoveto: dx dy
if self.stack.len() < 2 {
return Err("rmoveto: stack underflow".to_string());
}
let dy = self.stack.pop().unwrap();
let dx = self.stack.pop().unwrap();
self.x += dx;
self.y += dy;
if !self.width_only && !self.flex_active {
self.path.segments.push(PathSegment::MoveTo(self.x, self.y));
}
// During flex, moveto just updates current point — OtherSubrs 2 handles flex_points
}
22 => {
// hmoveto: dx
if self.stack.is_empty() {
return Err("hmoveto: stack underflow".to_string());
}
let dx = self.stack.pop().unwrap();
self.x += dx;
if !self.width_only && !self.flex_active {
self.path.segments.push(PathSegment::MoveTo(self.x, self.y));
}
// During flex, moveto just updates current point — OtherSubrs 2 handles flex_points
}
23..=29 => {} // reserved
30 => {
// vhcurveto: dy1 dx2 dy2 dx3
if self.stack.len() < 4 {
return Err("vhcurveto: stack underflow".to_string());
}
let dx3 = self.stack.pop().unwrap();
let dy2 = self.stack.pop().unwrap();
let dx2 = self.stack.pop().unwrap();
let dy1 = self.stack.pop().unwrap();
let x1 = self.x;
let y1 = self.y + dy1;
let x2 = x1 + dx2;
let y2 = y1 + dy2;
let x3 = x2 + dx3;
let y3 = y2;
if !self.width_only {
self.path.segments.push(PathSegment::CurveTo {
x1,
y1,
x2,
y2,
x3,
y3,
});
}
self.x = x3;
self.y = y3;
}
31 => {
// hvcurveto: dx1 dx2 dy2 dy3
if self.stack.len() < 4 {
return Err("hvcurveto: stack underflow".to_string());
}
let dy3 = self.stack.pop().unwrap();
let dy2 = self.stack.pop().unwrap();
let dx2 = self.stack.pop().unwrap();
let dx1 = self.stack.pop().unwrap();
let x1 = self.x + dx1;
let y1 = self.y;
let x2 = x1 + dx2;
let y2 = y1 + dy2;
let x3 = x2;
let y3 = y2 + dy3;
if !self.width_only {
self.path.segments.push(PathSegment::CurveTo {
x1,
y1,
x2,
y2,
x3,
y3,
});
}
self.x = x3;
self.y = y3;
}
// Number encoding
32..=246 => {
// Single-byte integer: value = b - 139
self.stack.push(b as f64 - 139.0);
}
247..=250 => {
// Two-byte positive: ((b - 247) * 256 + next) + 108
if pos >= data.len() {
break;
}
let b2 = data[pos];
pos += 1;
let val = ((b as i32 - 247) * 256 + b2 as i32) + 108;
self.stack.push(val as f64);
}
251..=254 => {
// Two-byte negative: -((b - 251) * 256 + next) - 108
if pos >= data.len() {
break;
}
let b2 = data[pos];
pos += 1;
let val = -((b as i32 - 251) * 256 + b2 as i32) - 108;
self.stack.push(val as f64);
}
255 => {
// Five-byte signed 32-bit integer
if pos + 4 > data.len() {
break;
}
let val = i32::from_be_bytes([
data[pos],
data[pos + 1],
data[pos + 2],
data[pos + 3],
]);
pos += 4;
self.stack.push(val as f64);
}
}
}
Ok(())
}
/// Handle a two-byte (escape) operator.
///
/// `depth` is the caller's subroutine nesting level, threaded through so
/// the `seac` handler can keep counting rather than restarting at zero.
fn execute_escape(&mut self, b2: u8, depth: usize) -> Result<(), String> {
match b2 {
0 => {
// dotsection — ignore (hint), no args
}
1 => {
// vstem3: x0 dx0 x1 dx1 x2 dx2 — ignore (hint), pop 6 args
for _ in 0..6.min(self.stack.len()) {
self.stack.pop();
}
}
2 => {
// hstem3: y0 dy0 y1 dy1 y2 dy2 — ignore (hint), pop 6 args
for _ in 0..6.min(self.stack.len()) {
self.stack.pop();
}
}
6 => {
// seac: asb adx ady bchar achar
// Builds a composite glyph from base + accent characters
if self.stack.len() < 5 {
return Err("seac: stack underflow".to_string());
}
let achar = self.stack.pop().unwrap() as u8;
let bchar = self.stack.pop().unwrap() as u8;
let ady = self.stack.pop().unwrap();
let adx = self.stack.pop().unwrap();
let asb = self.stack.pop().unwrap();
// Look up base and accent glyph names in StandardEncoding
let bname = STANDARD_ENCODING[bchar as usize];
let aname = STANDARD_ENCODING[achar as usize];
// Extract charstring data from lookup before executing (borrow checker)
let bchar_data = self.cs_lookup.as_ref().and_then(|f| f(bname));
let achar_data = self.cs_lookup.as_ref().and_then(|f| f(aname));
if let Some(bchar_data) = bchar_data {
let saved_width_x = self.width_x;
let saved_width_y = self.width_y;
let saved_x = self.x;
let saved_y = self.y;
// Execute base character charstring.
//
// `execute_inner`, not `execute`: the latter restarts the
// counter at 0, so the depth guard above never fires and a
// seac naming its own glyph recurses until the native stack
// is gone — an abort rather than a panic.
let decrypted = decrypt_charstring(&bchar_data, self.len_iv);
self.x = 0.0;
self.y = 0.0;
self.done = false;
self.execute_inner(&decrypted, depth + 1)?;
let base_lsb = self.lsb_x;
self.done = false;
// Execute accent character charstring with offset.
// Per the Type 1 spec, the accent's origin (0,0) is placed
// at (adx - asb + base_lsb, ady) in the composite's
// coordinate system. hsbw/sbw adds this translation to
// the accent's sidebearing so all path elements shift.
if let Some(achar_data) = achar_data {
let decrypted = decrypt_charstring(&achar_data, self.len_iv);
self.seac_accent_offset = Some((adx - asb + base_lsb, ady));
// Threaded, for the same reason as the base above.
self.execute_inner(&decrypted, depth + 1)?;
self.seac_accent_offset = None;
}
// Restore original width (from the composite's hsbw/sbw)
self.width_x = saved_width_x;
self.width_y = saved_width_y;
self.x = saved_x;
self.y = saved_y;
}
// If no lookup available, seac produces no path (graceful degradation)
}
7 => {
// sbw: sbx sby wx wy
// Sets sidebearing and width. Does NOT emit a MoveTo —
// the first real moveto in the glyph body will do that.
if self.stack.len() < 4 {
return Err("sbw: stack underflow".to_string());
}
let wy = self.stack.pop().unwrap();
let wx = self.stack.pop().unwrap();
let sby = self.stack.pop().unwrap();
let sbx = self.stack.pop().unwrap();
self.lsb_x = sbx;
self.lsb_y = sby;
self.width_x = wx;
self.width_y = wy;
if let Some((ox, oy)) = self.seac_accent_offset {
self.x = sbx + ox;
self.y = sby + oy;
} else {
self.x = sbx;
self.y = sby;
}
}
12 => {
// div: num1 num2 → num1/num2
if self.stack.len() < 2 {
return Err("div: stack underflow".to_string());
}
let b = self.stack.pop().unwrap();
let a = self.stack.pop().unwrap();
if b == 0.0 {
self.stack.push(0.0);
} else {
self.stack.push(a / b);
}
}
16 => {
// callothersubr: args... n subr#
if self.stack.len() < 2 {
return Err("callothersubr: stack underflow".to_string());
}
let subr_num = self.stack.pop().unwrap() as i32;
let n_args = self.stack.pop().unwrap() as usize;
if self.stack.len() < n_args {
return Err("callothersubr: not enough args".to_string());
}
// Pop arguments from charstring stack
let mut args: Vec<f64> = Vec::with_capacity(n_args);
for _ in 0..n_args {
args.push(self.stack.pop().unwrap());
}
args.reverse(); // Args were popped in reverse order
match subr_num {
0 => {
// EndFlex: construct two bezier curves from flex points
// args[0] = flex_depth (unused — we always draw curves)
if self.flex_points.len() >= 7 {
let _p0 = self.flex_points[0]; // reference point
let p1 = self.flex_points[1];
let p2 = self.flex_points[2];
let p3 = self.flex_points[3];
let p4 = self.flex_points[4];
let p5 = self.flex_points[5];
let p6 = self.flex_points[6];
if !self.width_only {
// First curve: from current (should be p0) to p3
self.path.segments.push(PathSegment::CurveTo {
x1: p1.0,
y1: p1.1,
x2: p2.0,
y2: p2.1,
x3: p3.0,
y3: p3.1,
});
// Second curve: from p3 to p6
self.path.segments.push(PathSegment::CurveTo {
x1: p4.0,
y1: p4.1,
x2: p5.0,
y2: p5.1,
x3: p6.0,
y3: p6.1,
});
}
self.x = p6.0;
self.y = p6.1;
}
self.flex_active = false;
self.flex_points.clear();
// Push y then x onto ps_stack so pop+pop+setcurrentpoint
// gets the correct order (x on top, popped first into
// charstring stack, then y).
self.ps_stack.push(self.y);
self.ps_stack.push(self.x);
}
1 => {
// StartFlex: begin accumulating flex points
// Do NOT pre-push current point — OtherSubrs 2 (AddFlex)
// handles all point accumulation.
self.flex_active = true;
self.flex_points.clear();
}
2 => {
// AddFlex: add current point to flex list
self.flex_points.push((self.x, self.y));
// Push y then x onto ps_stack for the subsequent pop+pop
// in the standard flex subroutine.
self.ps_stack.push(self.y);
self.ps_stack.push(self.x);
}
3 => {
// Hint replacement — push 3 onto ps_stack for pop
self.ps_stack.push(3.0);
}
14..=18 => {
// Multiple Master blend OtherSubrs:
// OtherSubr 14 = blend 1 value, 15 = 2, 16 = 3, 17 = 4, 18 = 6
let num_results = match subr_num {
14 => 1,
15 => 2,
16 => 3,
17 => 4,
18 => 6,
_ => unreachable!(),
};
if let Some(ref wv) = self.weight_vector {
let nm = wv.len(); // number of masters
let nd = nm - 1; // number of deltas per result
// Layout after pop+reverse:
// [base0, base1, ..., baseN-1,
// d0_w1, d0_w2, ..., d0_wN-1,
// d1_w1, d1_w2, ..., d1_wN-1, ...]
// result[r] = base[r] + w[1]*d[r][0] + w[2]*d[r][1] + ... + w[nm-1]*d[r][nd-1]
//
// Push results in REVERSE order so pop retrieves result0
// first (matching the PS OtherSubr code's stack layout).
let mut results = Vec::with_capacity(num_results);
for r in 0..num_results {
let base_val = if r < args.len() { args[r] } else { 0.0 };
let mut blended = base_val;
for j in 0..nd {
let delta_idx = num_results + r * nd + j;
let weight_idx = j + 1;
if delta_idx < args.len() && weight_idx < wv.len() {
blended += wv[weight_idx] * args[delta_idx];
}
}
results.push(blended);
}
for r in results.into_iter().rev() {
self.ps_stack.push(r);
}
} else {
// No weight vector — use base values only
for r in 0..num_results {
self.ps_stack
.push(if r < args.len() { args[r] } else { 0.0 });
}
}
}
_ => {
// Unknown OtherSubr — push args onto ps_stack
for &a in &args {
self.ps_stack.push(a);
}
}
}
}
17 => {
// pop: move value from OtherSubrs stack to charstring stack
if let Some(val) = self.ps_stack.pop() {
self.stack.push(val);
} else {
self.stack.push(0.0);
}
}
33 => {
// setcurrentpoint: x y
if self.stack.len() < 2 {
return Err("setcurrentpoint: stack underflow".to_string());
}
let y = self.stack.pop().unwrap();
let x = self.stack.pop().unwrap();
self.x = x;
self.y = y;
}
_ => {
// Unknown escape — ignore
}
}
Ok(())
}
}
// Fix: p0 is used in the flex code above but the compiler may not see it.
// The flex code references p0 via flex_points[0] directly.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_decrypt_charstring_basic() {
// Encrypt some data with R=4330, then decrypt and verify
let plain = b"\x8b\x0e"; // push 0 (0x8b = 139-139=0), endchar (0x0e = 14)
let c1: u32 = 52845;
let c2: u32 = 22719;
let mut r: u32 = 4330;
// Prepend 4 random bytes (zeros)
let mut to_encrypt = vec![0u8; 4];
to_encrypt.extend_from_slice(plain);
let mut encrypted = Vec::new();
for &p in &to_encrypt {
let c = (p as u32 ^ (r >> 8)) as u8;
encrypted.push(c);
r = ((c as u32 + r) * c1 + c2) & 0xFFFF;
}
let decrypted = decrypt_charstring(&encrypted, 4);
assert_eq!(decrypted, plain);
}
#[test]
fn test_number_encoding_single_byte() {
// Test that single-byte numbers are decoded correctly
// byte 139 = 0, byte 140 = 1, byte 246 = 107, byte 32 = -107
// Use hsbw to consume 2 values, then endchar
let code = vec![
139, // push 0 (sbx)
140, // push 1 (wx)
13, // hsbw
14, // endchar
];
let mut interp = CharstringInterp::new(&[], 4, true, None);
interp.execute_inner(&code, 0).unwrap();
assert!((interp.width_x - 1.0).abs() < 0.01);
}
#[test]
fn test_hsbw_sets_width() {
// hsbw: sbx=0 wx=600
// For 600: value = ((b-247)*256 + b2) + 108
// 600 - 108 = 492; 492 / 256 = 1 rem 236 → b=248, b2=236
let data = vec![
139, // push 0 (sbx)
248, 236, // push 600 (wx)
13, // hsbw
14, // endchar
];
let mut interp = CharstringInterp::new(&[], 4, true, None);
interp.execute_inner(&data, 0).unwrap();
assert!((interp.width_x - 600.0).abs() < 0.01);
assert!((interp.lsb_x - 0.0).abs() < 0.01);
}
#[test]
fn test_rmoveto_rlineto() {
let data = vec![
139, // push 0 (sbx)
248,
236, // push 600 (wx)
13, // hsbw
// rmoveto: dx=100, dy=200
139 + 100, // push 100
139 + 107, // push 107 (max single byte)
21, // rmoveto
// rlineto: dx=50, dy=50
139 + 50, // push 50
139 + 50, // push 50
5, // rlineto
9, // closepath
14, // endchar
];
let mut interp = CharstringInterp::new(&[], 4, false, None);
interp.execute_inner(&data, 0).unwrap();
// hsbw(0, 600): sets x=0, y=0 but does NOT emit MoveTo
// rmoveto(100, 107): x=100, y=107, emits MoveTo(100,107)
// rlineto(50, 50): x=150, y=157, emits LineTo(150,157)
// closepath
assert_eq!(interp.path.segments.len(), 3); // moveto(rmoveto), lineto, closepath
match &interp.path.segments[1] {
PathSegment::LineTo(x, y) => {
assert!((x - 150.0).abs() < 0.01);
assert!((y - 157.0).abs() < 0.01);
}
_ => panic!("Expected LineTo"),
}
}
#[test]
fn test_execute_real_charstring() {
// Load a real font and execute the 'space' charstring
let font_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../resources/Font/NimbusSans-Regular.t1");
if !font_path.exists() {
eprintln!("Skipping test — font file not found");
return;
}
let data = std::fs::read(&font_path).unwrap();
let font = crate::type1_parser::parse_type1(&data).unwrap();
// Execute 'space' charstring — should have a width but no path
let space_cs = font.charstrings.get("space").expect("'space' charstring");
let result = execute_charstring(space_cs, &font.subrs, font.len_iv, false).unwrap();
assert!(result.width_x > 0.0, "space should have positive width");
// Execute 'A' charstring — should have paths
let a_cs = font.charstrings.get("A").expect("'A' charstring");
let result = execute_charstring(a_cs, &font.subrs, font.len_iv, false).unwrap();
assert!(result.width_x > 0.0, "A should have positive width");
assert!(!result.path.is_empty(), "A should have path segments");
// Width-only mode should produce same width but empty path
let result_wo = execute_charstring(a_cs, &font.subrs, font.len_iv, true).unwrap();
assert!((result_wo.width_x - result.width_x).abs() < 0.01);
assert!(result_wo.path.is_empty());
}
#[test]
fn test_execute_multiple_glyphs() {
let font_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../resources/Font/NimbusSans-Regular.t1");
if !font_path.exists() {
eprintln!("Skipping test — font file not found");
return;
}
let data = std::fs::read(&font_path).unwrap();
let font = crate::type1_parser::parse_type1(&data).unwrap();
// Execute several common glyphs
for glyph_name in &["A", "B", "a", "b", "zero", "one", "period", "comma"] {
if let Some(cs) = font.charstrings.get(*glyph_name) {
let result = execute_charstring(cs, &font.subrs, font.len_iv, false).unwrap();
assert!(
result.width_x > 0.0,
"'{}' should have positive width",
glyph_name
);
assert!(
!result.path.is_empty(),
"'{}' should have path segments",
glyph_name
);
}
}
}
}