strata-render 0.1.4

High-performance Display List, Stacking Context Compositor, and GPU rasterization pipeline
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
//! Pure-Rust Vector PDF 1.7 Exporter directly from `strata_render::compositor::DisplayList`.
//!
//! Compiles rectangles, cubic-Bezier rounded rects, borders, gradients, vector SVG paths,
//! text runs, embedded RGB/RGBA images, transforms, opacity states, and clip regions directly
//! into spec-compliant PDF 1.7 bytes with exact `xref` byte offsets and optional `FlateDecode`
//! compression (using `miniz_oxide`, zero extra crates).

use crate::compositor::{DisplayList, DrawCommand};
use std::fmt::Write as _;
use strata_layout::Rect;
use strata_style::types::{BorderRadius, Color, FontWeight};

/// Standard print page geometry in PDF points (`72 pt = 1 inch`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PdfPageSize {
    /// ISO 216 A4 (`595.28 x 841.89 pt` = `210 x 297 mm`).
    A4,
    /// US Letter (`612.0 x 792.0 pt` = `8.5 x 11 in`).
    Letter,
    /// Explicit page dimensions in PDF points (`72 pt/in`).
    Custom { width_pt: f32, height_pt: f32 },
}

impl PdfPageSize {
    pub const fn dimensions_pt(self) -> (f32, f32) {
        match self {
            Self::A4 => (595.28, 841.89),
            Self::Letter => (612.0, 792.0),
            Self::Custom {
                width_pt,
                height_pt,
            } => (width_pt, height_pt),
        }
    }
}

/// Configuration controlling pure-Rust Vector PDF 1.7 compilation.
#[derive(Debug, Clone, PartialEq)]
pub struct PdfExportOptions {
    pub page_size: PdfPageSize,
    /// Uniform page margin in PDF points (default: `24.0 pt`).
    pub margin_pt: f32,
    /// Logical CSS DPI reference (`96.0` CSS px per inch -> `0.75 pt/px`).
    pub dpi: f32,
    /// Automatically scale the viewport uniformly to fit inside the printable page box.
    pub fit_to_page: bool,
    /// Compress PDF content & image streams with zlib (`FlateDecode`).
    pub compress_streams: bool,
    pub title: String,
    pub author: String,
}

impl Default for PdfExportOptions {
    fn default() -> Self {
        Self {
            page_size: PdfPageSize::A4,
            margin_pt: 24.0,
            dpi: 96.0,
            fit_to_page: true,
            compress_streams: false,
            title: "Strata Vector Export".into(),
            author: "Strata Engine".into(),
        }
    }
}

struct PdfImageObject {
    name: String,
    width: u32,
    height: u32,
    rgb_bytes: Vec<u8>,
}

/// Compiles a [`DisplayList`] directly into a valid, spec-compliant Vector PDF 1.7 document.
pub fn export_display_list_to_pdf(
    display_list: &DisplayList,
    viewport_width_px: f32,
    viewport_height_px: f32,
    options: &PdfExportOptions,
) -> Vec<u8> {
    let (page_w, page_h) = options.page_size.dimensions_pt();
    let margin = options.margin_pt.max(0.0);
    let avail_w = (page_w - margin * 2.0).max(1.0);
    let avail_h = (page_h - margin * 2.0).max(1.0);
    let vw = viewport_width_px.max(1.0);
    let vh = viewport_height_px.max(1.0);

    let pt_per_px = 72.0 / options.dpi.max(1.0);
    let scale = if options.fit_to_page {
        (avail_w / vw).min(avail_h / vh)
    } else {
        pt_per_px
    };

    let mut content = String::with_capacity(4096);
    let mut images: Vec<PdfImageObject> = Vec::new();
    let mut ext_gstates: Vec<(String, f32)> = Vec::new();

    // Establish CSS top-left origin -> PDF bottom-left origin transform:
    // x_pdf = margin + x_px * scale
    // y_pdf = (page_h - margin) - y_px * scale
    let _ = writeln!(content, "q");
    let _ = writeln!(
        content,
        "{scale:.4} 0 0 {scale:.4} {margin:.3} {:.3} cm",
        page_h - margin
    );

    let flip_y = |y: f32| -y;
    let alpha_f = |a: u8| a as f32 / 255.0;

    for cmd in &display_list.commands {
        match cmd {
            DrawCommand::DrawRect { bounds, color } => {
                if color.a == 0 || bounds.width <= 0.0 || bounds.height <= 0.0 {
                    continue;
                }
                emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
                emit_fill_rgb(&mut content, *color);
                let _ = writeln!(
                    content,
                    "{:.3} {:.3} {:.3} {:.3} re f",
                    bounds.x,
                    flip_y(bounds.y + bounds.height),
                    bounds.width,
                    bounds.height
                );
            }
            DrawCommand::DrawRoundedRect {
                bounds,
                color,
                border_radius,
                border_width,
                border_color,
            } => {
                if bounds.width <= 0.0 || bounds.height <= 0.0 {
                    continue;
                }
                if color.a > 0 {
                    emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
                    emit_fill_rgb(&mut content, *color);
                    emit_rounded_rect_path(&mut content, *bounds, *border_radius);
                    let _ = writeln!(content, "f");
                }
                if let Some(bc) = border_color {
                    let bw = border_width
                        .top
                        .max(border_width.right)
                        .max(border_width.bottom)
                        .max(border_width.left);
                    if bw > 0.0 && bc.a > 0 {
                        emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(bc.a));
                        emit_stroke_rgb(&mut content, *bc);
                        let _ = writeln!(content, "{bw:.3} w");
                        emit_rounded_rect_path(&mut content, *bounds, *border_radius);
                        let _ = writeln!(content, "S");
                    }
                }
            }
            DrawCommand::DrawLinearGradient {
                bounds,
                stops,
                border_radius,
                ..
            } => {
                if let Some(first) = stops.first() {
                    emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(first.color.a));
                    emit_fill_rgb(&mut content, first.color);
                    emit_rounded_rect_path(&mut content, *bounds, *border_radius);
                    let _ = writeln!(content, "f");
                }
            }
            DrawCommand::DrawBorder {
                bounds,
                widths,
                color,
                border_radius,
                ..
            } => {
                let bw = widths
                    .top
                    .max(widths.right)
                    .max(widths.bottom)
                    .max(widths.left);
                if bw > 0.0 && color.a > 0 {
                    emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
                    emit_stroke_rgb(&mut content, *color);
                    let _ = writeln!(content, "{bw:.3} w");
                    emit_rounded_rect_path(&mut content, *bounds, *border_radius);
                    let _ = writeln!(content, "S");
                }
            }
            DrawCommand::DrawText {
                origin,
                text,
                font_size,
                font_weight,
                color,
                is_monospace,
                ..
            } => {
                if text.is_empty() || color.a == 0 {
                    continue;
                }
                emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(color.a));
                emit_fill_rgb(&mut content, *color);
                let font_key = match (*is_monospace, *font_weight) {
                    (true, _) => "/F3",
                    (
                        false,
                        FontWeight::Bold
                        | FontWeight::SemiBold
                        | FontWeight::ExtraBold
                        | FontWeight::Black,
                    ) => "/F2",
                    _ => "/F1",
                };
                let baseline_y = flip_y(origin.1 + font_size * 0.82);
                let _ = writeln!(
                    content,
                    "BT {font_key} {:.2} Tf 1 0 0 1 {:.3} {:.3} Tm ({}) Tj ET",
                    font_size.max(1.0),
                    origin.0,
                    baseline_y,
                    escape_pdf_string(text)
                );
            }
            DrawCommand::DrawTextFragments { fragments } => {
                for frag in fragments {
                    if frag.text.is_empty() || frag.style.color.a == 0 {
                        continue;
                    }
                    emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(frag.style.color.a));
                    emit_fill_rgb(&mut content, frag.style.color);
                    let font_key = if frag.style.is_monospace { "/F3" } else { "/F1" };
                    let baseline_y = flip_y(frag.bounds.y + frag.style.font_size * 0.82);
                    let _ = writeln!(
                        content,
                        "BT {font_key} {:.2} Tf 1 0 0 1 {:.3} {:.3} Tm ({}) Tj ET",
                        frag.style.font_size.max(1.0),
                        frag.bounds.x,
                        baseline_y,
                        escape_pdf_string(&frag.text)
                    );
                }
            }
            DrawCommand::DrawSvgPath {
                bounds,
                path_data,
                stroke_color,
                fill_color,
                stroke_width,
                view_box,
                ..
            } => {
                let sx = if view_box.0 > 0.0 {
                    bounds.width / view_box.0
                } else {
                    1.0
                };
                let sy = if view_box.1 > 0.0 {
                    bounds.height / view_box.1
                } else {
                    1.0
                };
                let path_ops = svg_path_to_pdf_ops(path_data, bounds.x, bounds.y, sx, sy);
                if path_ops.is_empty() {
                    continue;
                }
                if let Some(fc) = fill_color {
                    if fc.a > 0 {
                        emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(fc.a));
                        emit_fill_rgb(&mut content, *fc);
                        content.push_str(&path_ops);
                        let _ = writeln!(content, "f");
                    }
                }
                if let Some(sc) = stroke_color {
                    if sc.a > 0 && *stroke_width > 0.0 {
                        emit_alpha_state(&mut content, &mut ext_gstates, alpha_f(sc.a));
                        emit_stroke_rgb(&mut content, *sc);
                        let _ = writeln!(content, "{:.3} w", stroke_width);
                        content.push_str(&path_ops);
                        let _ = writeln!(content, "S");
                    }
                }
            }
            DrawCommand::DrawImage { bounds, image } => {
                if image.width > 0 && image.height > 0 && !image.data.is_empty() {
                    let img_idx = images.len() + 1;
                    let name = format!("Im{img_idx}");
                    let mut rgb = Vec::with_capacity((image.width * image.height * 3) as usize);
                    for px in image.data.chunks_exact(4) {
                        rgb.push(px[0]);
                        rgb.push(px[1]);
                        rgb.push(px[2]);
                    }
                    images.push(PdfImageObject {
                        name: name.clone(),
                        width: image.width,
                        height: image.height,
                        rgb_bytes: rgb,
                    });
                    let _ = writeln!(
                        content,
                        "q {:.3} 0 0 {:.3} {:.3} {:.3} cm /{name} Do Q",
                        bounds.width,
                        bounds.height,
                        bounds.x,
                        flip_y(bounds.y + bounds.height)
                    );
                }
            }
            DrawCommand::PushClip { bounds, radius } => {
                let _ = writeln!(content, "q");
                emit_rounded_rect_path(&mut content, *bounds, *radius);
                let _ = writeln!(content, "W n");
            }
            DrawCommand::PopClip => {
                let _ = writeln!(content, "Q");
            }
            DrawCommand::PushOpacity { alpha } => {
                let _ = writeln!(content, "q");
                emit_alpha_state(&mut content, &mut ext_gstates, *alpha);
            }
            DrawCommand::PopOpacity => {
                let _ = writeln!(content, "Q");
            }
            DrawCommand::PushTransform { matrix } => {
                let m = &matrix.m;
                // Extract 2D affine components `[a b c d e f]` from column-major 4x4 matrix
                let _ = writeln!(
                    content,
                    "q {:.4} {:.4} {:.4} {:.4} {:.3} {:.3} cm",
                    m[0], -m[1], -m[4], m[5], m[12], -m[13]
                );
            }
            DrawCommand::PopTransform => {
                let _ = writeln!(content, "Q");
            }
            _ => {}
        }
    }

    let _ = writeln!(content, "Q");

    assemble_pdf_document(
        page_w,
        page_h,
        content.as_bytes(),
        &images,
        &ext_gstates,
        options,
    )
}

fn emit_fill_rgb(out: &mut String, color: Color) {
    let _ = writeln!(
        out,
        "{:.4} {:.4} {:.4} rg",
        color.r as f32 / 255.0,
        color.g as f32 / 255.0,
        color.b as f32 / 255.0
    );
}

fn emit_stroke_rgb(out: &mut String, color: Color) {
    let _ = writeln!(
        out,
        "{:.4} {:.4} {:.4} RG",
        color.r as f32 / 255.0,
        color.g as f32 / 255.0,
        color.b as f32 / 255.0
    );
}

fn emit_alpha_state(out: &mut String, gstates: &mut Vec<(String, f32)>, alpha: f32) {
    let a = alpha.clamp(0.0, 1.0);
    if (a - 1.0).abs() < 0.005 {
        return;
    }
    let key = if let Some((existing, _)) = gstates.iter().find(|(_, v)| (*v - a).abs() < 0.005) {
        existing.clone()
    } else {
        let name = format!("GS{}", gstates.len() + 1);
        gstates.push((name.clone(), a));
        name
    };
    let _ = writeln!(out, "/{key} gs");
}

/// Emits a closed rounded rectangle path using exact cubic Bezier corner arcs ($\kappa = 0.55228475$).
fn emit_rounded_rect_path(out: &mut String, b: Rect, r: BorderRadius) {
    let max_r = (b.width * 0.5).min(b.height * 0.5).max(0.0);
    let tl = r.top_left.clamp(0.0, max_r);
    let tr = r.top_right.clamp(0.0, max_r);
    let br = r.bottom_right.clamp(0.0, max_r);
    let bl = r.bottom_left.clamp(0.0, max_r);

    let x0 = b.x;
    let x1 = b.x + b.width;
    let y_top = -b.y;
    let y_bot = -(b.y + b.height);

    if tl < 0.01 && tr < 0.01 && br < 0.01 && bl < 0.01 {
        let _ = writeln!(out, "{x0:.3} {y_bot:.3} {:.3} {:.3} re", b.width, b.height);
        return;
    }

    const K: f32 = 0.552_284_8;
    let _ = writeln!(out, "{:.3} {:.3} m", x0 + tl, y_top);
    let _ = writeln!(out, "{:.3} {:.3} l", x1 - tr, y_top);
    if tr > 0.01 {
        let _ = writeln!(
            out,
            "{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
            x1 - tr * (1.0 - K),
            y_top,
            x1,
            y_top - tr * (1.0 - K),
            x1,
            y_top - tr
        );
    }
    let _ = writeln!(out, "{:.3} {:.3} l", x1, y_bot + br);
    if br > 0.01 {
        let _ = writeln!(
            out,
            "{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
            x1,
            y_bot + br * (1.0 - K),
            x1 - br * (1.0 - K),
            y_bot,
            x1 - br,
            y_bot
        );
    }
    let _ = writeln!(out, "{:.3} {:.3} l", x0 + bl, y_bot);
    if bl > 0.01 {
        let _ = writeln!(
            out,
            "{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
            x0 + bl * (1.0 - K),
            y_bot,
            x0,
            y_bot + bl * (1.0 - K),
            x0,
            y_bot + bl
        );
    }
    let _ = writeln!(out, "{:.3} {:.3} l", x0, y_top - tl);
    if tl > 0.01 {
        let _ = writeln!(
            out,
            "{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
            x0,
            y_top - tl * (1.0 - K),
            x0 + tl * (1.0 - K),
            y_top,
            x0 + tl,
            y_top
        );
    }
    let _ = writeln!(out, "h");
}

/// Minimal SVG `d` path translator (`M`/`m`, `L`/`l`, `H`/`h`, `V`/`v`, `C`/`c`, `Z`/`z`) to PDF operators.
fn svg_path_to_pdf_ops(d: &str, ox: f32, oy: f32, sx: f32, sy: f32) -> String {
    let mut out = String::new();
    let mut tokens = Vec::new();
    let mut cur = String::new();
    for ch in d.chars() {
        if ch.is_ascii_alphabetic() {
            if !cur.is_empty() {
                tokens.push(std::mem::take(&mut cur));
            }
            tokens.push(ch.to_string());
        } else if ch.is_ascii_whitespace() || ch == ',' {
            if !cur.is_empty() {
                tokens.push(std::mem::take(&mut cur));
            }
        } else {
            cur.push(ch);
        }
    }
    if !cur.is_empty() {
        tokens.push(cur);
    }

    let mut i = 0;
    let mut cx = 0.0f32;
    let mut cy = 0.0f32;
    let mut cmd = ' ';
    let tx = |x: f32| ox + x * sx;
    let ty = |y: f32| -(oy + y * sy);

    while i < tokens.len() {
        if tokens[i].len() == 1 && tokens[i].chars().next().unwrap().is_ascii_alphabetic() {
            cmd = tokens[i].chars().next().unwrap();
            i += 1;
        }
        match cmd {
            'M' | 'L' if i + 1 < tokens.len() => {
                if let (Ok(x), Ok(y)) = (tokens[i].parse::<f32>(), tokens[i + 1].parse::<f32>()) {
                    cx = x;
                    cy = y;
                    let op = if cmd == 'M' { 'm' } else { 'l' };
                    let _ = writeln!(out, "{:.3} {:.3} {op}", tx(cx), ty(cy));
                    if cmd == 'M' {
                        cmd = 'L';
                    }
                }
                i += 2;
            }
            'm' | 'l' if i + 1 < tokens.len() => {
                if let (Ok(dx), Ok(dy)) = (tokens[i].parse::<f32>(), tokens[i + 1].parse::<f32>()) {
                    cx += dx;
                    cy += dy;
                    let op = if cmd == 'm' { 'm' } else { 'l' };
                    let _ = writeln!(out, "{:.3} {:.3} {op}", tx(cx), ty(cy));
                    if cmd == 'm' {
                        cmd = 'l';
                    }
                }
                i += 2;
            }
            'H' if i < tokens.len() => {
                if let Ok(x) = tokens[i].parse::<f32>() {
                    cx = x;
                    let _ = writeln!(out, "{:.3} {:.3} l", tx(cx), ty(cy));
                }
                i += 1;
            }
            'V' if i < tokens.len() => {
                if let Ok(y) = tokens[i].parse::<f32>() {
                    cy = y;
                    let _ = writeln!(out, "{:.3} {:.3} l", tx(cx), ty(cy));
                }
                i += 1;
            }
            'C' if i + 5 < tokens.len() => {
                if let (Ok(x1), Ok(y1), Ok(x2), Ok(y2), Ok(x), Ok(y)) = (
                    tokens[i].parse::<f32>(),
                    tokens[i + 1].parse::<f32>(),
                    tokens[i + 2].parse::<f32>(),
                    tokens[i + 3].parse::<f32>(),
                    tokens[i + 4].parse::<f32>(),
                    tokens[i + 5].parse::<f32>(),
                ) {
                    cx = x;
                    cy = y;
                    let _ = writeln!(
                        out,
                        "{:.3} {:.3} {:.3} {:.3} {:.3} {:.3} c",
                        tx(x1),
                        ty(y1),
                        tx(x2),
                        ty(y2),
                        tx(x),
                        ty(y)
                    );
                }
                i += 6;
            }
            'Z' | 'z' => {
                let _ = writeln!(out, "h");
                cmd = ' ';
            }
            _ => i += 1,
        }
    }
    out
}

fn escape_pdf_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '(' => out.push_str("\\("),
            ')' => out.push_str("\\)"),
            '\n' | '\r' => out.push(' '),
            c if c.is_ascii() => out.push(c),
            _ => out.push('?'),
        }
    }
    out
}

fn assemble_pdf_document(
    page_w: f32,
    page_h: f32,
    raw_content: &[u8],
    images: &[PdfImageObject],
    ext_gstates: &[(String, f32)],
    options: &PdfExportOptions,
) -> Vec<u8> {
    let mut pdf = Vec::with_capacity(raw_content.len() + 2048);
    // Binary comment marker per PDF 1.7 Section 7.5.2
    pdf.extend_from_slice(b"%PDF-1.7\n%\xE2\xE3\xCF\xD3\n");

    let mut offsets: Vec<usize> = Vec::new();
    let push_obj = |pdf: &mut Vec<u8>, offsets: &mut Vec<usize>, id: usize, body: &[u8]| {
        offsets.push(pdf.len());
        pdf.extend_from_slice(format!("{id} 0 obj\n").as_bytes());
        pdf.extend_from_slice(body);
        pdf.extend_from_slice(b"\nendobj\n");
    };

    // Object ID layout:
    // 1: Catalog
    // 2: Pages
    // 3: Page
    // 4: Font F1 (Helvetica)
    // 5: Font F2 (Helvetica-Bold)
    // 6: Font F3 (Courier)
    // 7: Info dictionary
    // 8: Content stream
    // 9..: Image XObjects (one per image)
    let first_img_id = 9usize;

    push_obj(
        &mut pdf,
        &mut offsets,
        1,
        b"<< /Type /Catalog /Pages 2 0 R >>",
    );
    push_obj(
        &mut pdf,
        &mut offsets,
        2,
        b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
    );

    // Build Page Resources dictionary (Fonts + ExtGStates + Image XObjects)
    let mut resources =
        String::from("<< /Font << /F1 4 0 R /F2 5 0 R /F3 6 0 R >>");
    if !ext_gstates.is_empty() {
        resources.push_str(" /ExtGState <<");
        for (name, alpha) in ext_gstates {
            let _ = write!(
                resources,
                " /{name} << /Type /ExtGState /ca {alpha:.3} /CA {alpha:.3} >>"
            );
        }
        resources.push_str(" >>");
    }
    if !images.is_empty() {
        resources.push_str(" /XObject <<");
        for (idx, img) in images.iter().enumerate() {
            let _ = write!(resources, " /{} {} 0 R", img.name, first_img_id + idx);
        }
        resources.push_str(" >>");
    }
    resources.push_str(" >>");

    let page_dict = format!(
        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {page_w:.2} {page_h:.2}] /Resources {resources} /Contents 8 0 R >>"
    );
    push_obj(&mut pdf, &mut offsets, 3, page_dict.as_bytes());

    push_obj(
        &mut pdf,
        &mut offsets,
        4,
        b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
    );
    push_obj(
        &mut pdf,
        &mut offsets,
        5,
        b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold >>",
    );
    push_obj(
        &mut pdf,
        &mut offsets,
        6,
        b"<< /Type /Font /Subtype /Type1 /BaseFont /Courier >>",
    );

    let info_dict = format!(
        "<< /Title ({}) /Author ({}) /Producer (Strata Pure-Rust Vector PDF 1.7 Engine) >>",
        escape_pdf_string(&options.title),
        escape_pdf_string(&options.author)
    );
    push_obj(&mut pdf, &mut offsets, 7, info_dict.as_bytes());

    // Object 8: Content stream (optionally FlateDecode compressed via miniz_oxide)
    let (stream_bytes, filter_clause) = if options.compress_streams {
        (
            miniz_oxide::deflate::compress_to_vec_zlib(raw_content, 6),
            " /Filter /FlateDecode",
        )
    } else {
        (raw_content.to_vec(), "")
    };
    let mut content_obj = Vec::with_capacity(stream_bytes.len() + 96);
    content_obj.extend_from_slice(
        format!(
            "<< /Length {}{filter_clause} >>\nstream\n",
            stream_bytes.len()
        )
        .as_bytes(),
    );
    content_obj.extend_from_slice(&stream_bytes);
    content_obj.extend_from_slice(b"\nendstream");
    push_obj(&mut pdf, &mut offsets, 8, &content_obj);

    // Objects 9+: Embedded RGB Image XObjects
    for (idx, img) in images.iter().enumerate() {
        let (img_data, img_filter) = if options.compress_streams {
            (
                miniz_oxide::deflate::compress_to_vec_zlib(&img.rgb_bytes, 6),
                " /Filter /FlateDecode",
            )
        } else {
            (img.rgb_bytes.clone(), "")
        };
        let mut obj = Vec::with_capacity(img_data.len() + 160);
        obj.extend_from_slice(
            format!(
                "<< /Type /XObject /Subtype /Image /Width {} /Height {} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Length {}{img_filter} >>\nstream\n",
                img.width,
                img.height,
                img_data.len()
            )
            .as_bytes(),
        );
        obj.extend_from_slice(&img_data);
        obj.extend_from_slice(b"\nendstream");
        push_obj(&mut pdf, &mut offsets, first_img_id + idx, &obj);
    }

    // Exact spec-compliant xref table
    let xref_offset = pdf.len();
    let total_objs = offsets.len() + 1;
    pdf.extend_from_slice(format!("xref\n0 {total_objs}\n").as_bytes());
    pdf.extend_from_slice(b"0000000000 65535 f \n");
    for off in &offsets {
        pdf.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
    }
    pdf.extend_from_slice(
        format!(
            "trailer\n<< /Size {total_objs} /Root 1 0 R /Info 7 0 R >>\nstartxref\n{xref_offset}\n%%EOF\n"
        )
        .as_bytes(),
    );

    pdf
}