stet-engine 0.4.0

Execution engine for stet PostScript interpreter
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
// stet - A PostScript Interpreter
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Integration tests for Phase 3 graphics rendering.
//!
//! Each test executes a PostScript program with a SkiaDevice attached,
//! then inspects the resulting PNG output to verify correct rendering.

use stet_core::context::Context;
use stet_core::geometry::Matrix;
use stet_render::SkiaDevice;

/// Create a rendering context with a SkiaDevice and default CTM.
fn render_ctx(width: u32, height: u32) -> Context {
    let device = SkiaDevice::new(width, height);
    let mut ctx = Context::new();
    stet_ops::build_system_dict(&mut ctx);
    ctx.exec_sync_fn = Some(stet_engine::eval::exec_sync);
    ctx.device = Some(Box::new(device));
    ctx.page_width = width;
    ctx.page_height = height;
    // Default CTM: Y-flip so PS origin is at bottom-left
    let ctm = Matrix {
        a: 1.0,
        b: 0.0,
        c: 0.0,
        d: -1.0,
        tx: 0.0,
        ty: height as f64,
    };
    ctx.gstate.ctm = ctm;
    ctx.gstate.default_ctm = ctm;
    // Set font resource path for font tests
    let font_dir = std::path::Path::new("resources/Font");
    if font_dir.is_dir() {
        ctx.font_resource_path = Some(
            font_dir
                .canonicalize()
                .unwrap()
                .to_string_lossy()
                .to_string(),
        );
    } else {
        // Fallback: search from workspace root
        let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .parent()
            .unwrap();
        let candidate = workspace_root.join("resources").join("Font");
        if candidate.is_dir() {
            ctx.font_resource_path = Some(candidate.to_string_lossy().to_string());
        }
    }
    ctx
}

/// Atomic counter for unique temp file names across parallel tests.
static TEST_COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);

/// Run PostScript source, call showpage via PS, return PNG bytes.
fn render_to_png(source: &[u8], width: u32, height: u32) -> Vec<u8> {
    let id = TEST_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let tmp_path =
        std::env::temp_dir().join(format!("stet_test_{}_{}.png", std::process::id(), id));
    let path_str = tmp_path.to_str().unwrap().to_string();

    let mut ctx = render_ctx(width, height);
    ctx.output_path = Some(path_str.clone());

    // Append showpage if source doesn't contain it
    let mut full_source = source.to_vec();
    if !source.windows(8).any(|w| w == b"showpage") {
        full_source.extend_from_slice(b"\nshowpage\n");
    }

    stet_engine::eval::parse_and_exec(&mut ctx, &full_source).expect("PS execution failed");

    let png_data = std::fs::read(&path_str).expect("read output PNG");
    std::fs::remove_file(&path_str).ok();
    png_data
}

/// Verify PNG header and extract dimensions.
fn verify_png(data: &[u8]) -> (u32, u32) {
    assert!(data.len() > 24, "PNG too small: {} bytes", data.len());
    assert_eq!(&data[..8], b"\x89PNG\r\n\x1a\n", "Not a valid PNG");
    let width = u32::from_be_bytes([data[16], data[17], data[18], data[19]]);
    let height = u32::from_be_bytes([data[20], data[21], data[22], data[23]]);
    (width, height)
}

// --- Integration Tests ---

/// Test 1: Rectangle fill produces a valid PNG with rendered content.
#[test]
fn test_rectangle_fill() {
    let png = render_to_png(
        b"1 0 0 setrgbcolor
          100 100 moveto 200 100 lineto 200 200 lineto 100 200 lineto
          closepath fill",
        300,
        300,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 300);
    assert_eq!(h, 300);
    assert!(png.len() > 200, "PNG should have rendered content");
}

/// Test 2: Stroke with line width produces visible pixels.
#[test]
fn test_stroke_with_linewidth() {
    let png = render_to_png(
        b"0 0 1 setrgbcolor
          5 setlinewidth
          50 50 moveto 250 50 lineto
          stroke",
        300,
        100,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 300);
    assert_eq!(h, 100);
    assert!(png.len() > 200);
}

/// Test 3: gsave/grestore preserves and restores graphics state.
#[test]
fn test_gsave_grestore() {
    let png = render_to_png(
        b"1 0 0 setrgbcolor
          gsave
            0 0 1 setrgbcolor
            10 10 moveto 50 10 lineto 50 50 lineto 10 50 lineto closepath fill
          grestore
          60 10 moveto 100 10 lineto 100 50 lineto 60 50 lineto closepath fill",
        110,
        60,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 110);
    assert_eq!(h, 60);
    assert!(png.len() > 200);
}

/// Test 4: Translate moves drawing position correctly.
#[test]
fn test_translate() {
    let png = render_to_png(
        b"100 100 translate
          1 0 0 setrgbcolor
          0 0 moveto 50 0 lineto 50 50 lineto 0 50 lineto
          closepath fill",
        200,
        200,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 200);
    assert_eq!(h, 200);
    assert!(png.len() > 200);
}

/// Test 5: Scale transform magnifies drawing.
#[test]
fn test_scale() {
    let png = render_to_png(
        b"2 2 scale
          1 0 0 setrgbcolor
          10 10 moveto 50 10 lineto 50 50 lineto 10 50 lineto
          closepath fill",
        200,
        200,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 200);
    assert_eq!(h, 200);
    assert!(png.len() > 200);
}

/// Test 6: Clipping restricts fill area.
#[test]
fn test_clipping() {
    let png = render_to_png(
        b"newpath 50 50 moveto 150 50 lineto 150 150 lineto 50 150 lineto closepath clip
          1 0 0 setrgbcolor
          0 0 moveto 200 0 lineto 200 200 lineto 0 200 lineto closepath fill",
        200,
        200,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 200);
    assert_eq!(h, 200);
    assert!(png.len() > 200);
}

/// Test 7: tiger.ps produces a valid PNG with substantial content.
#[test]
fn test_tiger_ps() {
    let tiger_path = std::env::temp_dir().join("tiger.ps");
    if !tiger_path.exists() {
        eprintln!(
            "Skipping tiger.ps test — file not found at {}",
            tiger_path.display()
        );
        return;
    }

    let source = std::fs::read(&tiger_path).expect("read tiger.ps");
    let tmp_path = std::env::temp_dir().join(format!("stet_tiger_test_{}.png", std::process::id()));
    let path_str = tmp_path.to_str().unwrap().to_string();

    let mut ctx = render_ctx(612, 792);
    ctx.output_path = Some(path_str.clone());

    stet_engine::eval::parse_and_exec(&mut ctx, &source).expect("tiger.ps execution failed");

    let png_data = std::fs::read(&path_str).expect("read tiger output PNG");
    let (w, h) = verify_png(&png_data);
    assert_eq!(w, 612);
    assert_eq!(h, 792);
    // Tiger produces a substantial PNG (>10KB)
    assert!(
        png_data.len() > 10_000,
        "Tiger PNG should be substantial, got {} bytes",
        png_data.len()
    );
    std::fs::remove_file(&path_str).ok();
}

/// Test 8: CMYK color conversion.
#[test]
fn test_cmyk_color() {
    let png = render_to_png(
        b"0 0 0 1 setcmykcolor
          10 10 moveto 50 10 lineto 50 50 lineto 10 50 lineto closepath fill
          1 0 0 0 setcmykcolor
          60 10 moveto 100 10 lineto 100 50 lineto 60 50 lineto closepath fill",
        110,
        60,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 110);
    assert_eq!(h, 60);
    assert!(png.len() > 200);
}

/// Test 9: Arc drawing.
#[test]
fn test_arc() {
    let png = render_to_png(
        b"1 0 0 setrgbcolor
          2 setlinewidth
          100 100 50 0 360 arc
          stroke",
        200,
        200,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 200);
    assert_eq!(h, 200);
    assert!(png.len() > 200);
}

/// Test 10: Dash pattern rendering.
#[test]
fn test_dash_pattern() {
    let png = render_to_png(
        b"0 0 0 setrgbcolor
          2 setlinewidth
          [10 5] 0 setdash
          50 50 moveto 250 50 lineto
          stroke",
        300,
        100,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 300);
    assert_eq!(h, 100);
    assert!(png.len() > 200);
}

// --- Phase 4: Font & Text Rendering Tests ---

/// Helper to check if font resources are available.
fn fonts_available() -> bool {
    let ctx = render_ctx(100, 100);
    ctx.font_resource_path.is_some()
}

/// Test 11: Simple show renders text (non-empty PNG with content).
#[test]
fn test_show_simple() {
    if !fonts_available() {
        eprintln!("Skipping font test — resources/Font not found");
        return;
    }
    let png = render_to_png(
        b"/Helvetica findfont 24 scalefont setfont
          72 700 moveto
          (Hello, World!) show",
        612,
        792,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 612);
    assert_eq!(h, 792);
    // Text should produce visible content beyond a blank page
    assert!(
        png.len() > 300,
        "show should render visible text, got {} bytes",
        png.len()
    );
}

/// Test 12: stringwidth returns positive width values via PS execution.
#[test]
fn test_stringwidth_via_ps() {
    if !fonts_available() {
        eprintln!("Skipping font test — resources/Font not found");
        return;
    }
    let mut ctx = render_ctx(612, 792);
    // Capture output to check width values
    let output = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let output_clone = output.clone();
    ctx.stdout = Box::new(OutputCapture(output_clone));

    let source = b"/Helvetica findfont 12 scalefont setfont
                   (Hello) stringwidth
                   2 copy
                   20 string cvs print ( ) print
                   20 string cvs print
                   ";
    stet_engine::eval::parse_and_exec(&mut ctx, source).expect("PS execution failed");

    let bytes = output.lock().unwrap().clone();
    let text = String::from_utf8_lossy(&bytes);
    // Should have printed two numbers: wy (likely 0) and wx (positive)
    let parts: Vec<&str> = text.trim().split_whitespace().collect();
    assert_eq!(parts.len(), 2, "expected 2 values, got: {:?}", parts);
    // First printed is wy (on top), second is wx
    let wy: f64 = parts[0].parse().unwrap_or(999.0);
    let wx: f64 = parts[1].parse().unwrap_or(0.0);
    assert!(wx > 0.0, "wx should be positive, got {}", wx);
    assert!(wy.abs() < 0.01, "wy should be ~0, got {}", wy);
}

/// Test 13: Multiple fonts in same document.
#[test]
fn test_multiple_fonts() {
    if !fonts_available() {
        eprintln!("Skipping font test — resources/Font not found");
        return;
    }
    let png = render_to_png(
        b"/Helvetica findfont 20 scalefont setfont
          72 700 moveto (Helvetica) show
          /Times-Roman findfont 20 scalefont setfont
          72 670 moveto (Times) show
          /Courier findfont 20 scalefont setfont
          72 640 moveto (Courier) show",
        612,
        792,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 612);
    assert_eq!(h, 792);
    assert!(png.len() > 300);
}

/// Test 14: charpath + stroke produces outlined text.
#[test]
fn test_charpath_stroke() {
    if !fonts_available() {
        eprintln!("Skipping font test — resources/Font not found");
        return;
    }
    let png = render_to_png(
        b"/Helvetica findfont 48 scalefont setfont
          72 400 moveto
          (Outlined) true charpath
          2 setlinewidth stroke",
        612,
        792,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 612);
    assert_eq!(h, 792);
    assert!(png.len() > 300);
}

/// Test 15: selectfont convenience operator.
#[test]
fn test_selectfont() {
    if !fonts_available() {
        eprintln!("Skipping font test — resources/Font not found");
        return;
    }
    let png = render_to_png(
        b"/Helvetica 30 selectfont
          72 400 moveto (selectfont works) show",
        612,
        792,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 612);
    assert_eq!(h, 792);
    assert!(png.len() > 300);
}

/// Test 16: tiger.ps continues to render correctly after Phase 4 changes.
#[test]
fn test_tiger_ps_regression() {
    let tiger_path = std::env::temp_dir().join("tiger.ps");
    if !tiger_path.exists() {
        eprintln!("Skipping tiger.ps regression test — file not found");
        return;
    }

    let source = std::fs::read(&tiger_path).expect("read tiger.ps");
    let tmp_path =
        std::env::temp_dir().join(format!("stet_tiger_phase4_{}.png", std::process::id()));
    let path_str = tmp_path.to_str().unwrap().to_string();

    let mut ctx = render_ctx(612, 792);
    ctx.output_path = Some(path_str.clone());

    stet_engine::eval::parse_and_exec(&mut ctx, &source).expect("tiger.ps execution failed");

    let png_data = std::fs::read(&path_str).expect("read tiger output PNG");
    let (w, h) = verify_png(&png_data);
    assert_eq!(w, 612);
    assert_eq!(h, 792);
    assert!(
        png_data.len() > 10_000,
        "Tiger PNG should be substantial, got {} bytes",
        png_data.len()
    );
    std::fs::remove_file(&path_str).ok();
}

// --- Phase 5: Filters, Images & Advanced Color Tests ---

/// Test 17: turkey-imagemask.ps renders a turkey bitmap.
#[test]
fn test_turkey_imagemask() {
    let turkey_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("ps_samples")
        .join("turkey-imagemask.ps");
    if !turkey_path.exists() {
        eprintln!("Skipping turkey-imagemask.ps test — file not found");
        return;
    }

    let source = std::fs::read(&turkey_path).expect("read turkey-imagemask.ps");
    let png = render_to_png(&source, 200, 200);
    let (w, h) = verify_png(&png);
    assert_eq!(w, 200);
    assert_eq!(h, 200);
    assert!(
        png.len() > 200,
        "turkey PNG should have rendered content, got {} bytes",
        png.len()
    );
}

/// Test 18: Inline 8-bit grayscale image renders correctly.
#[test]
fn test_inline_grayscale_image() {
    // 4x4 grayscale image: alternating black and white pixels
    let png = render_to_png(
        b"100 100 translate 100 100 scale
          4 4 8 [4 0 0 -4 0 4]
          <00FF00FF FF00FF00 00FF00FF FF00FF00>
          image",
        300,
        300,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 300);
    assert_eq!(h, 300);
    assert!(
        png.len() > 200,
        "grayscale image PNG should have content, got {} bytes",
        png.len()
    );
}

/// Test 19: Inline 1-bit imagemask with hex data.
#[test]
fn test_imagemask_inline() {
    // 8x8 checkerboard mask
    let png = render_to_png(
        b"100 100 translate 100 100 scale
          1 0 0 setrgbcolor
          8 8 true [8 0 0 -8 0 8]
          <AA55AA55 AA55AA55>
          imagemask",
        300,
        300,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 300);
    assert_eq!(h, 300);
    assert!(png.len() > 200);
}

/// Test 20: RGB colorimage renders correctly.
#[test]
fn test_colorimage_rgb() {
    // 2x2 RGB image: red, green, blue, white
    let png = render_to_png(
        b"100 100 translate 200 200 scale
          2 2 8 [2 0 0 -2 0 2]
          <FF0000 00FF00 0000FF FFFFFF>
          false 3 colorimage",
        400,
        400,
    );
    let (w, h) = verify_png(&png);
    assert_eq!(w, 400);
    assert_eq!(h, 400);
    assert!(png.len() > 200);
}

/// Test 21: ASCIIHexDecode filter decodes hex data correctly.
#[test]
fn test_asciihex_filter() {
    let mut ctx = render_ctx(100, 100);
    let output = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let output_clone = output.clone();
    ctx.stdout = Box::new(OutputCapture(output_clone));

    let source = b"(48656C6C6F>) /ASCIIHexDecode filter
                   5 string readstring pop print";
    stet_engine::eval::parse_and_exec(&mut ctx, source).expect("PS execution failed");

    let bytes = output.lock().unwrap().clone();
    assert_eq!(&bytes, b"Hello", "ASCIIHexDecode should decode to 'Hello'");
}

/// Test 22: ASCII85Decode filter decodes data correctly.
#[test]
fn test_ascii85_filter() {
    let mut ctx = render_ctx(100, 100);
    let output = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let output_clone = output.clone();
    ctx.stdout = Box::new(OutputCapture(output_clone));

    // "Hello" in ASCII85 is 87cURD]j
    let source = b"(87cURD]j~>) /ASCII85Decode filter
                   5 string readstring pop print";
    stet_engine::eval::parse_and_exec(&mut ctx, source).expect("PS execution failed");

    let bytes = output.lock().unwrap().clone();
    assert_eq!(&bytes, b"Hello", "ASCII85Decode should decode to 'Hello'");
}

/// Test 23: Halftone stubs don't crash and consume operands correctly.
#[test]
fn test_halftone_stubs() {
    let mut ctx = render_ctx(100, 100);

    // Run various halftone/transfer operators — they should not error
    let source = b"60 45 {} setscreen
                   currentscreen pop pop pop
                   {} settransfer
                   currenttransfer pop
                   {} {} {} {} setcolortransfer
                   currentcolortransfer pop pop pop pop
                   {} setblackgeneration
                   currentblackgeneration pop
                   {} setundercolorremoval
                   currentundercolorremoval pop";
    stet_engine::eval::parse_and_exec(&mut ctx, source).expect("halftone stubs should not error");
    assert!(
        ctx.o_stack.is_empty(),
        "all halftone results should be consumed"
    );
}

/// Test 24: setpagedevice / currentpagedevice work.
#[test]
fn test_pagedevice_ops() {
    let mut ctx = render_ctx(612, 792);

    // Set up a page device with PageSize and HWResolution
    let source =
        b"<< /PageSize [612 792] /HWResolution [72 72] /.IsPageDevice true >> setpagedevice
                   currentpagedevice pop";
    ctx.device_factory = Some(Box::new(|w, h| {
        Box::new(stet_render::SkiaDevice::new(w, h))
    }));
    stet_engine::eval::parse_and_exec(&mut ctx, source).expect("pagedevice should not error");
    // Note: without init scripts, setpagedevice's Install procedure may leave
    // residual items on the stack. We only verify that page_device was set.
    assert!(ctx.gstate.page_device.is_some());
}

/// Test 25: FlateDecode filter decompresses data correctly.
#[test]
fn test_flate_filter() {
    let mut ctx = render_ctx(100, 100);
    let output = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let output_clone = output.clone();
    ctx.stdout = Box::new(OutputCapture(output_clone));

    // Compress "Hello World!" with flate2 and embed as hex
    let input = b"Hello World!";
    let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
    std::io::Write::write_all(&mut encoder, input).unwrap();
    let compressed = encoder.finish().unwrap();

    // Build hex string of compressed data
    let hex: String = compressed.iter().map(|b| format!("{:02X}", b)).collect();

    let source = format!(
        "(<{}>) /ASCIIHexDecode filter /FlateDecode filter\n\
         {} string readstring pop print",
        hex,
        input.len()
    );
    stet_engine::eval::parse_and_exec(&mut ctx, source.as_bytes())
        .expect("FlateDecode filter should work");

    let bytes = output.lock().unwrap().clone();
    assert_eq!(
        &bytes, input,
        "FlateDecode should decompress to original data"
    );
}

/// Re-installing the same CIE colour space must not cost arena memory per
/// installation.
///
/// `setcolorspace` samples each decode procedure 256 times per channel. The
/// procedures `pdftops` emits for an ICCBased space contain an inline array
/// literal, so `[` / `]` are executable and every one of those evaluations
/// builds a fresh array in the array arena — which is never reclaimed without
/// a `restore`. A file that re-installs the space on each of its pages (the
/// normal shape of `pdftops` output) therefore grew without bound: a 55 KB
/// corpus file reached over 3 GB, and running the corpus concurrently
/// exhausted host memory.
///
/// The memo in `Context::cie_decode_cache` collapses the repeats, so arena
/// growth after the first installation must be negligible.
#[test]
fn cie_decode_tables_are_memoised_across_reinstalls() {
    let mut ctx = render_ctx(64, 64);

    // A DecodeABC whose procedures each build a 64-element array literal and
    // index it — the construction that made this unbounded.
    let table: String = (0..64)
        .map(|i| (i * 4).to_string())
        .collect::<Vec<_>>()
        .join(" ");
    let space = format!(
        "/cs [ /CIEBasedABC << \
           /DecodeABC [ \
             {{ dup 0 lt {{ pop 0 }} if [ {table} ] exch 63 mul cvi get 256 div }} \
             {{ dup 0 lt {{ pop 0 }} if [ {table} ] exch 63 mul cvi get 256 div }} \
             {{ dup 0 lt {{ pop 0 }} if [ {table} ] exch 63 mul cvi get 256 div }} \
           ] \
           /RangeABC [0 1 0 1 0 1] \
           /MatrixABC [1 0 0 0 1 0 0 0 1] \
           /WhitePoint [0.9505 1.0 1.089] \
         >> ] def\n"
    );
    stet_engine::eval::parse_and_exec(&mut ctx, space.as_bytes())
        .expect("colour space definition should execute");

    let install = b"cs setcolorspace 0.2 0.4 0.6 setcolor\n";

    stet_engine::eval::parse_and_exec(&mut ctx, install).expect("first install");
    let after_first = ctx.arrays.allocated_objects();
    let cache_after_first = ctx.cie_decode_cache.len();
    assert!(
        cache_after_first > 0,
        "the first install should populate the decode-table memo"
    );

    // Re-install many times, as a multi-page document would.
    const REINSTALLS: usize = 40;
    for _ in 0..REINSTALLS {
        stet_engine::eval::parse_and_exec(&mut ctx, install).expect("re-install");
    }
    let after_repeats = ctx.arrays.allocated_objects();

    assert_eq!(
        ctx.cie_decode_cache.len(),
        cache_after_first,
        "re-installing an unchanged space must not add cache entries"
    );

    // Uncached, each re-install re-ran 3 procedures x 256 samples, every one
    // building a 64-element array: ~49k objects per install. Allow generous
    // slack for the interpreter's own per-execution allocations while still
    // failing decisively if the sampling is happening again.
    let growth = after_repeats - after_first;
    let per_install_uncached = 3 * 256 * 64;
    assert!(
        growth < per_install_uncached,
        "arena grew {growth} objects over {REINSTALLS} re-installs \
         (>= {per_install_uncached}, one uncached install) — decode tables \
         are being resampled instead of reused"
    );
}

/// A Type 3 font whose `A` glyph fills a triangle and whose `B` glyph strokes
/// the same three points. `resources/Font` is not needed — the glyph
/// procedures paint directly.
const TYPE3_CHARPATH_FONT: &[u8] = b"
    /T3 8 dict def
    T3 begin
      /FontType 3 def
      /FontMatrix [0.001 0 0 0.001 0 0] def
      /FontBBox [0 0 1000 1000] def
      /Encoding 256 array def
      0 1 255 { Encoding exch /.notdef put } for
      Encoding 65 /filled put
      Encoding 66 /stroked put
      /CharProcs 3 dict def
      CharProcs begin
        /.notdef { 0 0 setcharwidth } bind def
        /filled { 1000 0 setcharwidth
                  100 200 moveto 400 200 lineto 400 700 lineto fill } bind def
        /stroked { 1000 0 setcharwidth 40 setlinewidth
                   100 200 moveto 400 200 lineto 400 700 lineto stroke } bind def
      end
      /BuildGlyph { exch /CharProcs get exch
                    2 copy known not { pop /.notdef } if get exec } bind def
      /BuildChar { 1 index /Encoding get exch get
                   1 index /BuildGlyph get exec } bind def
    end
    /T3 T3 definefont pop
    /T3 findfont 100 scalefont setfont
";

/// `charpath` on a Type 3 font builds the path without marking the page.
///
/// The glyph procedure runs for real — that is the only way to learn its
/// outline — so the check that matters is that nothing it painted survives on
/// the display list. Ghostscript renders a blank page for the same input.
#[test]
fn test_charpath_type3_paints_nothing() {
    let mut ctx = render_ctx(200, 200);
    let mut src = TYPE3_CHARPATH_FONT.to_vec();
    src.extend_from_slice(b"newpath 0 0 moveto (AB) false charpath\n");
    stet_engine::eval::parse_and_exec(&mut ctx, &src).expect("PS execution failed");

    assert!(
        ctx.display_list.is_empty(),
        "a Type 3 charpath must leave no marks, found {} display elements",
        ctx.display_list.len()
    );
    assert!(
        !ctx.gstate.path.segments.is_empty(),
        "a Type 3 charpath must append the glyph outlines to the current path"
    );

    // Both glyphs cover the same 100..400 x 200..700 glyph-space box, at
    // FontMatrix 0.001 and 100pt that is 10..40 x 20..70 in user space, and
    // the second glyph sits 100 units to the right. The Y flip in render_ctx
    // puts device Y at 200 - user Y.
    let (mut min_x, mut min_y) = (f64::MAX, f64::MAX);
    let (mut max_x, mut max_y) = (f64::MIN, f64::MIN);
    for seg in &ctx.gstate.path.segments {
        let pts: Vec<(f64, f64)> = match *seg {
            stet_core::graphics_state::PathSegment::MoveTo(x, y) => vec![(x, y)],
            stet_core::graphics_state::PathSegment::LineTo(x, y) => vec![(x, y)],
            stet_core::graphics_state::PathSegment::CurveTo {
                x1,
                y1,
                x2,
                y2,
                x3,
                y3,
            } => vec![(x1, y1), (x2, y2), (x3, y3)],
            _ => vec![],
        };
        for (x, y) in pts {
            min_x = min_x.min(x);
            min_y = min_y.min(y);
            max_x = max_x.max(x);
            max_y = max_y.max(y);
        }
    }
    assert!((min_x - 10.0).abs() < 0.01, "min x was {min_x}");
    assert!((max_x - 140.0).abs() < 0.01, "max x was {max_x}");
    assert!((min_y - 130.0).abs() < 0.01, "min y was {min_y}");
    assert!((max_y - 180.0).abs() < 0.01, "max y was {max_y}");
}

/// Helper: Write adapter that captures bytes to a shared Vec.
struct OutputCapture(std::sync::Arc<std::sync::Mutex<Vec<u8>>>);

impl std::io::Write for OutputCapture {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.lock().unwrap().extend_from_slice(buf);
        Ok(buf.len())
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}