oxideav-pdf 0.1.1

Pure-Rust PDF writer for the oxideav framework — vector-stays-vector path
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
//! PDF content-stream operator emitter (ISO 32000-1 §8.5).
//!
//! All operators are written as ASCII text into a [`OpBuf`] (a thin
//! wrapper over `Vec<u8>` that handles the formatting nuances).
//! Numerics use the same compact "trim trailing zeros" form as the
//! object serializer (`objects::format_real` shares the policy).
//!
//! The full PDF operator surface is enormous; round 1 only emits the
//! intersection of "operators implied by the imaging-model mapping in
//! the workspace task description" and "operators we have a sane
//! mapping for from `oxideav_core::vector`". Anything outside that is
//! a future round's problem.

use crate::resources::ResourceCollector;
use oxideav_core::vector::{
    DashPattern, FillRule, LineCap, LineJoin, Paint, Path, PathCommand, Point, Rgba, Stroke,
    Transform2D,
};

/// Append-only byte buffer for an in-progress content stream. Wraps a
/// raw `Vec<u8>` so the operator helpers can stay infallible (no
/// I/O — every byte goes into RAM).
pub struct OpBuf {
    bytes: Vec<u8>,
}

impl OpBuf {
    pub fn new() -> Self {
        Self { bytes: Vec::new() }
    }

    pub fn into_bytes(self) -> Vec<u8> {
        self.bytes
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    fn write(&mut self, s: &[u8]) {
        self.bytes.extend_from_slice(s);
    }

    fn space(&mut self) {
        self.bytes.push(b' ');
    }

    fn newline(&mut self) {
        self.bytes.push(b'\n');
    }

    fn write_real(&mut self, f: f32) {
        self.write(format_real(f as f64).as_bytes());
    }

    /// Append an already-formatted byte sequence verbatim. Escape
    /// hatch for one-off operators (`/Imx Do`, etc.) that don't have
    /// a dedicated helper.
    pub fn append_raw(&mut self, bytes: &[u8]) {
        self.write(bytes);
    }
}

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

// ───────────────────────── graphics state ─────────────────────────

/// `q` — push graphics state.
pub fn save(op: &mut OpBuf) {
    op.write(b"q");
    op.newline();
}

/// `Q` — pop graphics state.
pub fn restore(op: &mut OpBuf) {
    op.write(b"Q");
    op.newline();
}

/// `cm` — concat matrix to the CTM. Skipped when `t` is the identity
/// (PDF behaviour is unchanged in that case but the byte savings add
/// up across deep group trees).
pub fn concat_matrix(op: &mut OpBuf, t: &Transform2D) {
    if t.is_identity() {
        return;
    }
    op.write_real(t.a);
    op.space();
    op.write_real(t.b);
    op.space();
    op.write_real(t.c);
    op.space();
    op.write_real(t.d);
    op.space();
    op.write_real(t.e);
    op.space();
    op.write_real(t.f);
    op.space();
    op.write(b"cm");
    op.newline();
}

/// `/Name gs` — set graphic state from named ExtGState resource.
pub fn set_ext_gstate(op: &mut OpBuf, name: &str) {
    op.write(b"/");
    op.write(name.as_bytes());
    op.space();
    op.write(b"gs");
    op.newline();
}

// ───────────────────────── path construction ─────────────────────

/// Emit one [`Path`] as a sequence of `m / l / c / h` operators.
///
/// `QuadCurveTo` is lifted to cubic via the standard
/// `c1 = start + 2/3*(control - start)` /
/// `c2 = end   + 2/3*(control - end)` formula — PDF has no native
/// quadratic.
///
/// `ArcTo` is flattened to one or more cubic segments per SVG 1.1
/// Appendix F.6.5; the implementation lives in [`crate::arc`].
///
/// The function tracks the "current point" so quadratic / arc
/// segments can compute their start point correctly. Paths that begin
/// without an explicit `MoveTo` are emitted relative to (0, 0) — same
/// behaviour as PDF's path-construction rule for an undefined current
/// point.
pub fn emit_path(op: &mut OpBuf, path: &Path) {
    let mut current = Point::default();
    let mut subpath_start = Point::default();
    for cmd in &path.commands {
        match *cmd {
            PathCommand::MoveTo(p) => {
                op.write_real(p.x);
                op.space();
                op.write_real(p.y);
                op.space();
                op.write(b"m");
                op.newline();
                current = p;
                subpath_start = p;
            }
            PathCommand::LineTo(p) => {
                op.write_real(p.x);
                op.space();
                op.write_real(p.y);
                op.space();
                op.write(b"l");
                op.newline();
                current = p;
            }
            PathCommand::CubicCurveTo { c1, c2, end } => {
                emit_cubic(op, c1, c2, end);
                current = end;
            }
            PathCommand::QuadCurveTo { control, end } => {
                let c1 = Point::new(
                    current.x + (2.0 / 3.0) * (control.x - current.x),
                    current.y + (2.0 / 3.0) * (control.y - current.y),
                );
                let c2 = Point::new(
                    end.x + (2.0 / 3.0) * (control.x - end.x),
                    end.y + (2.0 / 3.0) * (control.y - end.y),
                );
                emit_cubic(op, c1, c2, end);
                current = end;
            }
            PathCommand::ArcTo {
                rx,
                ry,
                x_axis_rot,
                large_arc,
                sweep,
                end,
            } => {
                for (c1, c2, e) in crate::arc::svg_arc_to_cubics(
                    current, end, rx, ry, x_axis_rot, large_arc, sweep,
                ) {
                    emit_cubic(op, c1, c2, e);
                }
                current = end;
            }
            PathCommand::Close => {
                op.write(b"h");
                op.newline();
                current = subpath_start;
            }
            // PathCommand is `#[non_exhaustive]` — future smooth /
            // shorthand cubic + quad variants land here, but the
            // workspace IR doesn't carry them yet (the SVG parser
            // expands shorthands at parse time). Round 1 silently
            // drops anything we don't know about.
            _ => {}
        }
    }
}

fn emit_cubic(op: &mut OpBuf, c1: Point, c2: Point, end: Point) {
    op.write_real(c1.x);
    op.space();
    op.write_real(c1.y);
    op.space();
    op.write_real(c2.x);
    op.space();
    op.write_real(c2.y);
    op.space();
    op.write_real(end.x);
    op.space();
    op.write_real(end.y);
    op.space();
    op.write(b"c");
    op.newline();
}

// ───────────────────────── path painting ─────────────────────────

/// Emit `f` / `f*` / `B` / `B*` / `S` / `n` per `(has_fill, has_stroke,
/// fill_rule)`. `n` is "no-op paint" — used after `W`/`W*` to consume
/// the current path as a clip without painting it.
pub enum PaintMode {
    Fill,
    Stroke,
    FillStroke,
    None,
}

pub fn paint(op: &mut OpBuf, mode: PaintMode, fill_rule: FillRule) {
    let bytes: &[u8] = match (mode, fill_rule) {
        (PaintMode::Fill, FillRule::NonZero) => b"f",
        (PaintMode::Fill, FillRule::EvenOdd) => b"f*",
        (PaintMode::Stroke, _) => b"S",
        (PaintMode::FillStroke, FillRule::NonZero) => b"B",
        (PaintMode::FillStroke, FillRule::EvenOdd) => b"B*",
        (PaintMode::None, _) => b"n",
    };
    op.write(bytes);
    op.newline();
}

/// Emit `W` (or `W*`) followed by `n` so the just-constructed path
/// becomes the new clip without itself being painted.
pub fn emit_clip_marker(op: &mut OpBuf, fill_rule: FillRule) {
    match fill_rule {
        FillRule::NonZero => op.write(b"W"),
        FillRule::EvenOdd => op.write(b"W*"),
    }
    op.newline();
    paint(op, PaintMode::None, fill_rule);
}

// ───────────────────────── colour / paint ─────────────────────────

/// Emit colour-setting operators for the given fill paint. For
/// gradients this requires registering a Pattern resource via the
/// [`ResourceCollector`] and emitting `/Pat<n> scn` instead of
/// `r g b sc`.
pub fn set_fill_paint(op: &mut OpBuf, paint: &Paint, resources: &mut ResourceCollector) {
    match paint {
        Paint::Solid(rgba) => {
            emit_rgb(op, *rgba, /* stroke = */ false);
        }
        Paint::LinearGradient(g) => {
            let name = resources.add_linear_gradient(g);
            emit_pattern(op, &name, /* stroke = */ false);
        }
        Paint::RadialGradient(g) => {
            let name = resources.add_radial_gradient(g);
            emit_pattern(op, &name, /* stroke = */ false);
        }
        // Paint is `#[non_exhaustive]` — fall back to a sensible
        // black solid for any future variant we don't yet handle.
        _ => emit_rgb(op, Rgba::opaque(0, 0, 0), /* stroke = */ false),
    }
}

/// Same as [`set_fill_paint`] but for the stroke colour.
pub fn set_stroke_paint(op: &mut OpBuf, paint: &Paint, resources: &mut ResourceCollector) {
    match paint {
        Paint::Solid(rgba) => {
            emit_rgb(op, *rgba, /* stroke = */ true);
        }
        Paint::LinearGradient(g) => {
            let name = resources.add_linear_gradient(g);
            emit_pattern(op, &name, /* stroke = */ true);
        }
        Paint::RadialGradient(g) => {
            let name = resources.add_radial_gradient(g);
            emit_pattern(op, &name, /* stroke = */ true);
        }
        _ => emit_rgb(op, Rgba::opaque(0, 0, 0), /* stroke = */ true),
    }
}

fn emit_rgb(op: &mut OpBuf, rgba: Rgba, stroke: bool) {
    let r = rgba.r as f32 / 255.0;
    let g = rgba.g as f32 / 255.0;
    let b = rgba.b as f32 / 255.0;
    op.write_real(r);
    op.space();
    op.write_real(g);
    op.space();
    op.write_real(b);
    op.space();
    op.write(if stroke { b"RG" } else { b"rg" });
    op.newline();
}

fn emit_pattern(op: &mut OpBuf, name: &str, stroke: bool) {
    // PDF Reference §8.6.8: to paint with a pattern in DeviceRGB, set
    // the colour space to `/Pattern` (CS / cs) then name the pattern
    // resource via SCN / scn. The Pattern colour space carries no
    // base — uncoloured patterns aren't used here, gradients are
    // shading patterns which are always coloured.
    op.write(if stroke {
        b"/Pattern CS"
    } else {
        b"/Pattern cs"
    });
    op.newline();
    op.write(b"/");
    op.write(name.as_bytes());
    op.space();
    op.write(if stroke { b"SCN" } else { b"scn" });
    op.newline();
}

// ───────────────────────── stroke style ─────────────────────────

/// Emit the `w / J / j / M / d` operators implied by `stroke`, then the
/// stroke colour.
pub fn set_stroke_style(op: &mut OpBuf, stroke: &Stroke, resources: &mut ResourceCollector) {
    op.write_real(stroke.width);
    op.space();
    op.write(b"w");
    op.newline();

    let cap = match stroke.cap {
        LineCap::Butt => 0,
        LineCap::Round => 1,
        LineCap::Square => 2,
    };
    op.write(format!("{} J", cap).as_bytes());
    op.newline();

    let join = match stroke.join {
        LineJoin::Miter => 0,
        LineJoin::Round => 1,
        LineJoin::Bevel => 2,
    };
    op.write(format!("{} j", join).as_bytes());
    op.newline();

    op.write_real(stroke.miter_limit);
    op.space();
    op.write(b"M");
    op.newline();

    if let Some(dash) = &stroke.dash {
        emit_dash(op, dash);
    } else {
        // Solid stroke — clear any inherited dash pattern.
        op.write(b"[] 0 d");
        op.newline();
    }

    set_stroke_paint(op, &stroke.paint, resources);
}

fn emit_dash(op: &mut OpBuf, dash: &DashPattern) {
    op.write(b"[");
    for (i, seg) in dash.array.iter().enumerate() {
        if i > 0 {
            op.space();
        }
        op.write_real(*seg);
    }
    op.write(b"] ");
    op.write_real(dash.offset);
    op.space();
    op.write(b"d");
    op.newline();
}

// ───────────────────────── shared formatting ─────────────────────

/// Reused by [`crate::resources`] for gradient function-table values.
pub fn format_real(f: f64) -> String {
    if !f.is_finite() {
        return "0".to_string();
    }
    if f.fract() == 0.0 && f.abs() < 1e16 {
        return format!("{}", f as i64);
    }
    let s = format!("{:.6}", f);
    let trimmed = s.trim_end_matches('0').trim_end_matches('.');
    if trimmed.is_empty() || trimmed == "-" {
        "0".to_string()
    } else {
        trimmed.to_string()
    }
}

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

    #[test]
    fn save_restore_produce_q_capital_q() {
        let mut op = OpBuf::new();
        save(&mut op);
        restore(&mut op);
        assert_eq!(op.into_bytes(), b"q\nQ\n");
    }

    #[test]
    fn identity_matrix_emits_nothing() {
        let mut op = OpBuf::new();
        concat_matrix(&mut op, &Transform2D::identity());
        assert!(op.as_bytes().is_empty());
    }

    #[test]
    fn translate_matrix_emits_cm() {
        let mut op = OpBuf::new();
        concat_matrix(&mut op, &Transform2D::translate(5.0, 10.0));
        assert_eq!(op.into_bytes(), b"1 0 0 1 5 10 cm\n");
    }

    #[test]
    fn rect_path_emits_m_l_l_l_h() {
        let mut p = Path::new();
        p.move_to(Point::new(10.0, 10.0))
            .line_to(Point::new(110.0, 10.0))
            .line_to(Point::new(110.0, 60.0))
            .line_to(Point::new(10.0, 60.0))
            .close();
        let mut op = OpBuf::new();
        emit_path(&mut op, &p);
        let s = String::from_utf8(op.into_bytes()).unwrap();
        assert!(s.contains("10 10 m"));
        assert!(s.contains("110 10 l"));
        assert!(s.contains("110 60 l"));
        assert!(s.contains("10 60 l"));
        assert!(s.contains("h"));
    }

    #[test]
    fn quad_lifts_to_cubic() {
        // start (0,0), control (3, 9), end (6, 0)
        // c1 = (0,0) + 2/3 * ((3,9)-(0,0)) = (2, 6)
        // c2 = (6,0) + 2/3 * ((3,9)-(6,0)) = (4, 6)
        let mut p = Path::new();
        p.move_to(Point::new(0.0, 0.0))
            .quad_to(Point::new(3.0, 9.0), Point::new(6.0, 0.0));
        let mut op = OpBuf::new();
        emit_path(&mut op, &p);
        let s = String::from_utf8(op.into_bytes()).unwrap();
        assert!(s.contains("2 6 4 6 6 0 c"));
    }

    #[test]
    fn fill_paint_solid_emits_rgb() {
        let mut op = OpBuf::new();
        let mut res = ResourceCollector::new();
        set_fill_paint(&mut op, &Paint::Solid(Rgba::opaque(255, 128, 0)), &mut res);
        let s = String::from_utf8(op.into_bytes()).unwrap();
        assert!(s.starts_with("1 0.501961 0 rg"));
    }

    #[test]
    fn paint_modes() {
        for (mode, rule, expected) in [
            (PaintMode::Fill, FillRule::NonZero, "f\n"),
            (PaintMode::Fill, FillRule::EvenOdd, "f*\n"),
            (PaintMode::Stroke, FillRule::NonZero, "S\n"),
            (PaintMode::FillStroke, FillRule::NonZero, "B\n"),
            (PaintMode::FillStroke, FillRule::EvenOdd, "B*\n"),
            (PaintMode::None, FillRule::NonZero, "n\n"),
        ] {
            let mut op = OpBuf::new();
            paint(&mut op, mode, rule);
            assert_eq!(op.into_bytes(), expected.as_bytes());
        }
    }

    #[test]
    fn stroke_style_emits_w_j_join_miter_dash() {
        let mut op = OpBuf::new();
        let mut res = ResourceCollector::new();
        let stroke = Stroke {
            width: 2.5,
            paint: Paint::Solid(Rgba::opaque(0, 0, 0)),
            cap: LineCap::Round,
            join: LineJoin::Bevel,
            miter_limit: 8.0,
            dash: Some(DashPattern {
                array: vec![5.0, 3.0],
                offset: 1.0,
            }),
        };
        set_stroke_style(&mut op, &stroke, &mut res);
        let s = String::from_utf8(op.into_bytes()).unwrap();
        assert!(s.contains("2.5 w"));
        assert!(s.contains("1 J"));
        assert!(s.contains("2 j"));
        assert!(s.contains("8 M"));
        assert!(s.contains("[5 3] 1 d"));
    }

    #[test]
    fn empty_path_writes_nothing() {
        let p = Path::new();
        let mut op = OpBuf::new();
        emit_path(&mut op, &p);
        assert!(op.as_bytes().is_empty());

        // PathNode round-trip sanity (no panics on default).
        let _node = PathNode {
            path: Path::new(),
            fill: None,
            stroke: None,
            fill_rule: FillRule::NonZero,
        };
    }
}