stet 0.7.0

PostScript Level 3 interpreter and PDF rendering engine — library API
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
// stet - A PostScript Interpreter
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Batteries-included PostScript Level 3 interpreter.
//!
//! This crate provides a simple, high-level API for rendering PostScript and
//! EPS files. All resources (35 fonts, init scripts, encodings, ICC profiles)
//! are embedded in the binary — no external `resources/` directory needed.
//!
//! # Quick Start
//!
//! ```no_run
//! let mut interp = stet::Interpreter::new();
//! let pages = interp.render(b"%!PS\n100 100 moveto 200 200 lineto stroke showpage", 300.0).unwrap();
//! // pages[0].rgba — RGBA pixel data
//! // pages[0].width, pages[0].height — dimensions in pixels
//! ```
//!
//! # Output Modes
//!
//! | Method | Returns | Feature |
//! |--------|---------|---------|
//! | [`Interpreter::render`] | RGBA pixels + display list | `render` (default) |
//! | [`Interpreter::render_to_display_list`] | Display lists only | always available |
//! | [`Interpreter::render_to_pdf`] | PDF bytes | `pdf-output` (default) |
//! | [`Interpreter::exec`] | Nothing (side effects only) | always available |
//!
//! # Configuration
//!
//! ```no_run
//! let mut interp = stet::Interpreter::builder()
//!     .no_icc()             // disable ICC color management
//!     .suppress_output()    // silence PS print/==/= operators
//!     .build();
//! ```
//!
//! # Diagnostics
//!
//! A render that returns no pages is not necessarily an error. The common
//! cause is a program that painted marks and then ended without calling
//! `showpage`: the page is discarded, and the render returns `Ok(vec![])` —
//! an empty page list that reads like a legitimate result.
//! [`Interpreter::warnings`] tells the two apart.
//!
//! ```no_run
//! let mut interp = stet::Interpreter::new();
//! let pages = interp.render(b"%!PS\n0 0 100 100 rectfill", 72.0).unwrap();
//! if pages.is_empty() {
//!     for w in interp.warnings() {
//!         eprintln!("warning: {}\n         {}", w, w.hint());
//!     }
//! }
//! ```
//!
//! See [`diagnostics`] for the warning types.
//!
//! # Feature Flags
//!
//! Both features are enabled by default. Use `default-features = false` for
//! a minimal build that only supports display list output.
//!
//! | Feature | Adds | Extra dependency |
//! |---------|------|-----------------|
//! | `render` | `render()` — RGBA pixel output | `stet-render` (tiny-skia) |
//! | `pdf-output` | `render_to_pdf()` — PDF output | `stet-pdf` |

pub mod diagnostics;
pub mod embedded_resources;
mod init;

use std::sync::{Arc, Mutex};

use stet_core::context::Context;
use stet_core::device::OutputDevice;
use stet_core::eps::{content_is_epsf, read_eps_bounding_box, strip_dos_eps_header};
use stet_core::error::PsError;
use stet_core::object::PsValue;
use stet_engine::eval::parse_and_exec;
use stet_graphics::display_list::DisplayList;

pub use diagnostics::{ExecWarning, ExecWarningKind};

// Re-exports for power users
pub use stet_core::context::Context as PsContext;
pub use stet_engine::eval::parse_and_exec as ps_exec;
pub use stet_graphics::display_list::{DisplayElement, DisplayList as PsDisplayList};
pub use stet_graphics::icc::IccCache;

#[cfg(feature = "render")]
pub use stet_render::{
    ImageCache, PreparedDisplayList, build_icc_cache_for_list, prepare_display_list,
    render_region_prepared, render_to_rgba, viewport_band_count,
};

/// Error type for interpreter operations.
#[derive(Debug)]
pub enum StetError {
    /// PostScript execution error.
    PostScript(String),
    /// Initialization error.
    Init(String),
}

impl std::fmt::Display for StetError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StetError::PostScript(msg) => write!(f, "PostScript error: {}", msg),
            StetError::Init(msg) => write!(f, "initialization error: {}", msg),
        }
    }
}

impl std::error::Error for StetError {}

/// A rendered page with display list and optional RGBA pixel data.
pub struct RenderedPage {
    /// The display list for this page (for custom/viewport rendering).
    pub display_list: DisplayList,
    /// Page width in pixels at the rendered DPI.
    pub width: u32,
    /// Page height in pixels at the rendered DPI.
    pub height: u32,
    /// DPI this page was rendered at.
    pub dpi: f64,
    /// RGBA pixel data (4 bytes per pixel, row-major).
    /// Present only when rendered via [`Interpreter::render`].
    #[cfg(feature = "render")]
    pub rgba: Vec<u8>,
}

/// A fully initialized PostScript interpreter.
///
/// Create via [`Interpreter::new`] for defaults or [`Interpreter::builder`]
/// for custom configuration. Reusable across multiple `render` calls.
pub struct Interpreter {
    ctx: Context,
    use_icc: bool,
    warnings: Vec<ExecWarning>,
}

/// Builder for configuring an [`Interpreter`] before creation.
pub struct InterpreterBuilder {
    use_icc: bool,
    suppress_output: bool,
}

impl Interpreter {
    /// Create a fully initialized interpreter with default settings.
    ///
    /// Includes embedded fonts, ICC color management, and all standard
    /// PostScript resources. Ready to render immediately.
    pub fn new() -> Self {
        Self::builder().build()
    }

    /// Create a builder for custom configuration.
    pub fn builder() -> InterpreterBuilder {
        InterpreterBuilder {
            use_icc: true,
            suppress_output: false,
        }
    }

    /// Render PostScript or EPS data to RGBA pages.
    ///
    /// `dpi` is the resolution in dots per inch (e.g., `300.0`). It is `f64`
    /// because PostScript defines HWResolution as a pair of reals; integer
    /// values like `300.0` work for typical use.
    ///
    /// Each `showpage` in the PostScript program produces one [`RenderedPage`]
    /// with RGBA pixel data and the display list. EPS files are detected
    /// automatically and rendered using their bounding box.
    ///
    /// The interpreter state is isolated via save/restore between calls.
    #[cfg(feature = "render")]
    pub fn render(&mut self, ps_data: &[u8], dpi: f64) -> Result<Vec<RenderedPage>, StetError> {
        let dl_pages = self.render_to_display_list(ps_data, dpi)?;

        let icc_cache = if self.use_icc {
            let mut cache = IccCache::new();
            cache.load_cmyk_profile_bytes(embedded_resources::DEFAULT_CMYK_ICC);
            Some(cache)
        } else {
            None
        };

        let mut pages = Vec::with_capacity(dl_pages.len());
        for p in dl_pages {
            let rgba = stet_render::render_to_rgba(
                &p.display_list,
                p.width,
                p.height,
                p.dpi,
                icc_cache.as_ref(),
                false,
            );
            pages.push(RenderedPage {
                display_list: p.display_list,
                width: p.width,
                height: p.height,
                dpi: p.dpi,
                rgba,
            });
        }
        Ok(pages)
    }

    /// Render PostScript or EPS data to display lists only (no pixel rendering).
    ///
    /// `dpi` is the resolution in dots per inch (e.g., `300.0`). It is `f64`
    /// because PostScript defines HWResolution as a pair of reals; integer
    /// values like `300.0` work for typical use.
    ///
    /// Returns one entry per `showpage`. Use this when you want to do your
    /// own rendering (e.g., viewport rendering) or just inspect the display list.
    pub fn render_to_display_list(
        &mut self,
        ps_data: &[u8],
        dpi: f64,
    ) -> Result<Vec<DisplayListPage>, StetError> {
        self.warnings.clear();
        let ps_data = strip_dos_eps_header(ps_data);
        let is_eps = content_is_epsf(ps_data);

        if is_eps {
            if let Some((llx, lly, urx, ury)) = read_eps_bounding_box(ps_data) {
                let w = urx - llx;
                let h = ury - lly;
                if w > 0.0 && h > 0.0 {
                    return self.render_eps(ps_data, dpi, llx, lly, w, h);
                }
            }
        }

        self.render_ps(ps_data, dpi, 612.0, 792.0)
    }

    /// Render PostScript data to a PDF document.
    ///
    /// `dpi` is the resolution in dots per inch (e.g., `300.0`). It is `f64`
    /// because PostScript defines HWResolution as a pair of reals; integer
    /// values like `300.0` work for typical use.
    ///
    /// Returns the PDF file contents as bytes.
    #[cfg(feature = "pdf-output")]
    pub fn render_to_pdf(&mut self, ps_data: &[u8], dpi: f64) -> Result<Vec<u8>, StetError> {
        self.warnings.clear();
        self.ctx.null_device_used = false;
        let ps_data = strip_dos_eps_header(ps_data);
        let is_eps = content_is_epsf(ps_data);

        let (page_w, page_h) = if is_eps {
            if let Some((llx, lly, urx, ury)) = read_eps_bounding_box(ps_data) {
                (urx - llx, ury - lly)
            } else {
                (612.0, 792.0)
            }
        } else {
            (612.0, 792.0)
        };

        // Set up PdfDevice as the device factory
        let dpi_val = dpi;
        self.ctx.device_factory = Some(Box::new(move |w, h| {
            Box::new(stet_pdf::PdfDevice::new(w, h, dpi_val)) as Box<dyn OutputDevice>
        }));

        self.ctx.output_path = Some("output.pdf".to_string());
        install_device(&mut self.ctx, dpi, page_w, page_h)?;

        // PDF output: register pdfmark + distiller params so prologues that
        // branch on `systemdict /pdfmark known` see Distiller-equivalent
        // semantics. The screen/viewer path leaves these undefined so the
        // same prologue takes its CMYK→ICC branch instead.
        stet_ops::register_pdf_authoring_ops(&mut self.ctx);

        // Save/restore isolation
        let save_obj = self.ctx.vm_save();
        let save_id = extract_save_id(&save_obj);

        let exec_result = if is_eps {
            if let Some((llx, lly, _, _)) = read_eps_bounding_box(ps_data) {
                self.exec_eps(ps_data, llx, lly)
            } else {
                parse_and_exec(&mut self.ctx, ps_data).map_err(ps_err)
            }
        } else {
            parse_and_exec(&mut self.ctx, ps_data).map_err(ps_err)
        };

        self.record_end_of_job_warnings();

        // Finish device and extract PDF bytes
        let pdf_bytes = if let Some(mut dev) = self.ctx.device.take() {
            let _ = dev.finish_with_context(&self.ctx);
            // Get the PDF bytes from the device
            let bytes = dev
                .as_any()
                .downcast_ref::<stet_pdf::PdfDevice>()
                .and_then(|pdf_dev| pdf_dev.take_pdf_bytes_with_context(&self.ctx))
                .unwrap_or_default();
            self.ctx.device = Some(dev);
            bytes
        } else {
            Vec::new()
        };

        let _ = self.ctx.vm_restore(save_id);
        reset_context(&mut self.ctx);

        exec_result?;
        Ok(pdf_bytes)
    }

    /// Execute PostScript without rendering (null device).
    ///
    /// Useful for running test suites or scripts that don't produce pages.
    pub fn exec(&mut self, ps_data: &[u8]) -> Result<(), StetError> {
        let save_obj = self.ctx.vm_save();
        let save_id = extract_save_id(&save_obj);

        let result = parse_and_exec(&mut self.ctx, ps_data);

        let _ = self.ctx.vm_restore(save_id);
        reset_context(&mut self.ctx);

        match result {
            Ok(()) | Err(PsError::Quit) => Ok(()),
            Err(e) => Err(StetError::PostScript(e.to_string())),
        }
    }

    /// Non-fatal problems noticed during the most recent render call.
    ///
    /// Cleared at the start of each [`render`], [`render_to_display_list`],
    /// and [`render_to_pdf`] call, so this always describes the latest one.
    ///
    /// The case worth checking for is a program that paints marks and never
    /// calls `showpage`: the page is discarded, the render returns an empty
    /// page list, and without this there is nothing to distinguish that from
    /// a program that legitimately drew nothing.
    ///
    /// ```no_run
    /// let mut interp = stet::Interpreter::new();
    /// let pages = interp.render(b"%!PS\n0 0 100 100 rectfill", 72.0).unwrap();
    /// if pages.is_empty() {
    ///     for w in interp.warnings() {
    ///         eprintln!("warning: {}\n         {}", w, w.hint());
    ///     }
    /// }
    /// ```
    ///
    /// [`exec`] is not covered: it runs on a null device for side effects
    /// only, where pending marks are expected rather than a problem.
    ///
    /// [`render`]: Interpreter::render
    /// [`render_to_display_list`]: Interpreter::render_to_display_list
    /// [`render_to_pdf`]: Interpreter::render_to_pdf
    /// [`exec`]: Interpreter::exec
    pub fn warnings(&self) -> &[ExecWarning] {
        &self.warnings
    }

    /// Access the underlying Context for power-user operations.
    pub fn context(&mut self) -> &mut Context {
        &mut self.ctx
    }

    /// Record a dropped-final-page warning if the job left marks unpainted.
    ///
    /// Must be called before `finish_device`/`vm_restore` — the restore tears
    /// down the page device the check reads `PageCount` from.
    fn record_end_of_job_warnings(&mut self) {
        if let Some(w) = diagnostics::dropped_final_page(&self.ctx) {
            self.warnings.push(w);
        }
    }

    // --- Internal rendering helpers ---

    fn render_ps(
        &mut self,
        ps_data: &[u8],
        dpi: f64,
        page_w_pt: f64,
        page_h_pt: f64,
    ) -> Result<Vec<DisplayListPage>, StetError> {
        self.ctx.capture_display_lists = Some(Vec::new());
        self.ctx.null_device_used = false;

        #[cfg(feature = "render")]
        let pages_ref = {
            let (pages_ref, _) = shared_page_tracker();
            self.setup_capture_device_factory(pages_ref.clone());
            pages_ref
        };
        #[cfg(not(feature = "render"))]
        {
            self.setup_capture_device_factory(Arc::new(Mutex::new(Vec::<()>::new())));
        }

        self.ctx.output_path = Some("stet_output".to_string());
        install_device(&mut self.ctx, dpi, page_w_pt, page_h_pt)?;

        let save_obj = self.ctx.vm_save();
        let save_id = extract_save_id(&save_obj);

        let exec_result = match parse_and_exec(&mut self.ctx, ps_data) {
            Ok(()) => Ok(()),
            Err(PsError::Quit) => Ok(()),
            Err(e) => Err(StetError::PostScript(e.to_string())),
        };

        self.record_end_of_job_warnings();
        finish_device(&mut self.ctx);

        #[cfg(feature = "render")]
        let result = {
            let pages = take_pages(&pages_ref);
            collect_display_lists(&mut self.ctx, &pages, dpi)
        };
        #[cfg(not(feature = "render"))]
        let result = collect_display_lists_simple(&mut self.ctx, dpi);

        let _ = self.ctx.vm_restore(save_id);
        reset_context(&mut self.ctx);

        exec_result?;
        Ok(result)
    }

    fn render_eps(
        &mut self,
        ps_data: &[u8],
        dpi: f64,
        llx: f64,
        lly: f64,
        w: f64,
        h: f64,
    ) -> Result<Vec<DisplayListPage>, StetError> {
        self.ctx.capture_display_lists = Some(Vec::new());
        self.ctx.null_device_used = false;

        #[cfg(feature = "render")]
        let pages_ref = {
            let (pages_ref, _) = shared_page_tracker();
            self.setup_capture_device_factory(pages_ref.clone());
            pages_ref
        };
        #[cfg(not(feature = "render"))]
        {
            self.setup_capture_device_factory(Arc::new(Mutex::new(Vec::<()>::new())));
        }

        self.ctx.output_path = Some("stet_output".to_string());
        install_device(&mut self.ctx, dpi, w, h)?;

        let save_obj = self.ctx.vm_save();
        let save_id = extract_save_id(&save_obj);

        let wrapper = format!("gsave {} {} translate", -llx, -lly);
        parse_and_exec(&mut self.ctx, wrapper.as_bytes()).map_err(ps_err)?;
        let _ = parse_and_exec(&mut self.ctx, ps_data);

        // Call showpage if the EPS didn't already
        #[cfg(feature = "render")]
        let need_showpage = pages_ref.lock().map(|g| g.is_empty()).unwrap_or(true);
        #[cfg(not(feature = "render"))]
        let need_showpage = self
            .ctx
            .capture_display_lists
            .as_ref()
            .map_or(true, |v| v.is_empty());

        if need_showpage {
            let _ = parse_and_exec(&mut self.ctx, b"grestore showpage");
        } else {
            let _ = parse_and_exec(&mut self.ctx, b"grestore");
        }

        // Runs after the implicit `showpage` above, so it fires only if the
        // EPS painted more after its own showpage — a real dropped page.
        self.record_end_of_job_warnings();
        finish_device(&mut self.ctx);

        #[cfg(feature = "render")]
        let result = {
            let pages = take_pages(&pages_ref);
            collect_display_lists(&mut self.ctx, &pages, dpi)
        };
        #[cfg(not(feature = "render"))]
        let result = collect_display_lists_simple(&mut self.ctx, dpi);

        let _ = self.ctx.vm_restore(save_id);
        reset_context(&mut self.ctx);

        Ok(result)
    }

    #[cfg(feature = "pdf-output")]
    fn exec_eps(&mut self, ps_data: &[u8], llx: f64, lly: f64) -> Result<(), StetError> {
        let wrapper = format!("gsave {} {} translate", -llx, -lly);
        parse_and_exec(&mut self.ctx, wrapper.as_bytes()).map_err(ps_err)?;
        let _ = parse_and_exec(&mut self.ctx, ps_data);
        let _ = parse_and_exec(&mut self.ctx, b"grestore showpage");
        Ok(())
    }

    #[cfg(feature = "render")]
    fn setup_capture_device_factory(&mut self, pages_ref: Arc<Mutex<Vec<PageDims>>>) {
        let use_icc = self.use_icc;
        self.ctx.device_factory = Some(Box::new(move |w, h| {
            let factory = NullSinkFactory(pages_ref.clone());
            let mut dev = stet_render::SkiaDevice::with_sink_factory(w, h, Box::new(factory));
            if use_icc {
                dev.set_system_cmyk_bytes(Arc::new(embedded_resources::DEFAULT_CMYK_ICC.to_vec()));
            }
            Box::new(dev) as Box<dyn OutputDevice>
        }));
    }

    #[cfg(not(feature = "render"))]
    fn setup_capture_device_factory<T>(&mut self, _pages_ref: Arc<Mutex<Vec<T>>>) {
        self.ctx.device_factory = Some(Box::new(|w, h| {
            Box::new(stet_core::device::NullDevice::new(w, h))
        }));
    }
}

impl Default for Interpreter {
    fn default() -> Self {
        Self::new()
    }
}

impl InterpreterBuilder {
    /// Disable ICC color management.
    pub fn no_icc(mut self) -> Self {
        self.use_icc = false;
        self
    }

    /// Suppress PostScript stdout (`print`, `=`, `==` operators).
    pub fn suppress_output(mut self) -> Self {
        self.suppress_output = true;
        self
    }

    /// Build the interpreter.
    pub fn build(self) -> Interpreter {
        let ctx = init::create_initialized_context(self.use_icc, self.suppress_output)
            .expect("interpreter initialization failed");
        Interpreter {
            ctx,
            use_icc: self.use_icc,
            warnings: Vec::new(),
        }
    }
}

// --- Page dimension tracking (NullSink) ---

/// Recorded page dimensions from the null sink.
#[cfg(feature = "render")]
struct PageDims {
    width: u32,
    height: u32,
}

/// A PageSinkFactory that discards pixels but records page dimensions.
#[cfg(feature = "render")]
struct NullSinkFactory(Arc<Mutex<Vec<PageDims>>>);

#[cfg(feature = "render")]
unsafe impl Send for NullSinkFactory {}
#[cfg(feature = "render")]
unsafe impl Sync for NullSinkFactory {}

#[cfg(feature = "render")]
impl stet_graphics::device::PageSinkFactory for NullSinkFactory {
    fn create_sink(
        &self,
        _output_path: &str,
    ) -> Result<Box<dyn stet_graphics::device::PageSink>, String> {
        // We don't know the pixel dimensions here — they'll be set by begin_page.
        // Use 0x0 as placeholders; the actual dimensions come from the device.
        let pages = self.0.clone();
        Ok(Box::new(NullSink {
            width: 0,
            height: 0,
            pages,
        }))
    }
}

#[cfg(feature = "render")]
struct NullSink {
    width: u32,
    height: u32,
    pages: Arc<Mutex<Vec<PageDims>>>,
}

#[cfg(feature = "render")]
unsafe impl Send for NullSink {}

#[cfg(feature = "render")]
impl stet_graphics::device::PageSink for NullSink {
    fn begin_page(&mut self, width: u32, height: u32) -> Result<(), String> {
        self.width = width;
        self.height = height;
        Ok(())
    }
    fn write_rows(&mut self, _rows: &[u8], _num_rows: u32) -> Result<(), String> {
        Ok(())
    }
    fn end_page(&mut self) -> Result<(), String> {
        if let Ok(mut guard) = self.pages.lock() {
            guard.push(PageDims {
                width: self.width,
                height: self.height,
            });
        }
        Ok(())
    }
}

/// A page's display list with dimensions (before RGBA rendering).
pub struct DisplayListPage {
    /// The display list for this page.
    pub display_list: DisplayList,
    /// Page width in pixels at the rendered DPI.
    pub width: u32,
    /// Page height in pixels at the rendered DPI.
    pub height: u32,
    /// DPI this page was rendered at.
    pub dpi: f64,
}

// --- Helpers ---

#[cfg(feature = "render")]
fn shared_page_tracker() -> (Arc<Mutex<Vec<PageDims>>>, ()) {
    (Arc::new(Mutex::new(Vec::new())), ())
}

#[cfg(feature = "render")]
fn take_pages(pages_ref: &Arc<Mutex<Vec<PageDims>>>) -> Vec<PageDims> {
    match pages_ref.lock() {
        Ok(mut guard) => std::mem::take(&mut *guard),
        Err(e) => std::mem::take(&mut *e.into_inner()),
    }
}

fn install_device(
    ctx: &mut Context,
    dpi: f64,
    width_pt: f64,
    height_pt: f64,
) -> Result<(), StetError> {
    let setup = format!(
        "<< /PageSize [{w} {h}] /HWResolution [{dpi} {dpi}] \
         /.IsPageDevice true \
         /Install {{ /DeviceRGB setcolorspace }} bind \
         /BeginPage {{pop}} bind \
         /EndPage {{ \
             dup 0 eq {{ pop pop true }} {{ \
                 1 eq {{ pop true }} {{ pop false }} ifelse \
             }} ifelse \
         }} bind \
         /PageCount 0 \
        >> setpagedevice",
        w = width_pt,
        h = height_pt,
        dpi = dpi
    );
    // The caller-supplied DPI is an explicit library-level request, not
    // an unsolicited change from within the PS program. Open the gate
    // around setpagedevice so merge_request_dict accepts HWResolution.
    let saved = ctx.allow_ps_resolution;
    ctx.allow_ps_resolution = true;
    let result = parse_and_exec(ctx, setup.as_bytes()).map_err(ps_err);
    ctx.allow_ps_resolution = saved;
    result
}

fn finish_device(ctx: &mut Context) {
    if let Some(mut dev) = ctx.device.take() {
        let _ = dev.finish_with_context(ctx);
        ctx.device = Some(dev);
    }
}

#[cfg(feature = "render")]
fn collect_display_lists(
    ctx: &mut Context,
    pages: &[PageDims],
    default_dpi: f64,
) -> Vec<DisplayListPage> {
    let captured = ctx.capture_display_lists.take().unwrap_or_default();
    captured
        .into_iter()
        .enumerate()
        .map(|(i, (dl, dpi))| {
            let dpi = if dpi > 0.0 { dpi } else { default_dpi };
            if i < pages.len() {
                DisplayListPage {
                    display_list: dl,
                    width: pages[i].width,
                    height: pages[i].height,
                    dpi,
                }
            } else {
                DisplayListPage {
                    display_list: dl,
                    width: (612.0 * dpi / 72.0) as u32,
                    height: (792.0 * dpi / 72.0) as u32,
                    dpi,
                }
            }
        })
        .collect()
}

/// Collect display lists without page dimension tracking (no-render fallback).
#[cfg(not(feature = "render"))]
fn collect_display_lists_simple(ctx: &mut Context, default_dpi: f64) -> Vec<DisplayListPage> {
    let captured = ctx.capture_display_lists.take().unwrap_or_default();
    captured
        .into_iter()
        .map(|(dl, dpi)| {
            let dpi = if dpi > 0.0 { dpi } else { default_dpi };
            DisplayListPage {
                display_list: dl,
                width: (612.0 * dpi / 72.0) as u32,
                height: (792.0 * dpi / 72.0) as u32,
                dpi,
            }
        })
        .collect()
}

fn reset_context(ctx: &mut Context) {
    ctx.device = None;
    ctx.output_path = None;
    ctx.display_list.clear();
    ctx.capture_display_lists = None;
    ctx.o_stack.clear();
    ctx.e_stack.clear();
    ctx.gstate = stet_core::graphics_state::GraphicsState::new();
    ctx.gstate_stack.clear();
    ctx.d_stack.truncate(3);
    ctx.save_stack = stet_core::save_stack::SaveStack::new();
    ctx.in_error_handler = false;
}

fn extract_save_id(save_obj: &stet_core::object::PsObject) -> u32 {
    match save_obj.value {
        PsValue::Save(stet_core::object::SaveLevel(id)) => id,
        _ => unreachable!(),
    }
}

fn ps_err(e: PsError) -> StetError {
    StetError::PostScript(e.to_string())
}