printpdf 0.13.0

Rust library for reading and writing PDF files
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
//! Colour spaces: the shape of the arrays and dictionaries that name them, and which families
//! a document uses.

use lopdf::{Object, ObjectId};

use super::util::{decode, look, name_text, resolve, Budget, Decoded, Look};
use super::{Loc, Run, Seen, Severity};

/// A colour space, as far as the preflight looks at it.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Space {
    Gray,
    Rgb,
    Cmyk,
    CalGray,
    CalRgb,
    Lab,
    /// ICC-based, with the number of components.
    Icc(i64),
    Indexed(Box<Space>),
    Separation,
    DeviceN,
    Pattern,
    /// Reported as a finding already.
    Invalid,
}

impl Space {
    /// The number of colour components, when it is known.
    fn components(&self) -> Option<usize> {
        match self {
            Space::Gray | Space::CalGray | Space::Indexed(_) | Space::Separation => Some(1),
            Space::Rgb | Space::CalRgb | Space::Lab => Some(3),
            Space::Cmyk => Some(4),
            Space::Icc(n) => usize::try_from(*n).ok(),
            _ => None,
        }
    }
}

/// The three device colour spaces, as an index into `Run::device_use`.
fn device_index(sp: &Space) -> Option<usize> {
    match sp {
        Space::Gray => Some(0),
        Space::Rgb => Some(1),
        Space::Cmyk => Some(2),
        _ => None,
    }
}

const DEVICE_NAMES: [&str; 3] = ["DeviceGray", "DeviceRGB", "DeviceCMYK"];

fn malformed(run: &mut Run<'_>, loc: Loc, what: String) -> Space {
    run.sink.error("colour-space-malformed", loc, what);
    Space::Invalid
}

/// Reads a colour space and reports it when it is malformed.
///
/// `inline` allows the abbreviated names of inline images. A colour space that is an indirect
/// object is read once.
pub(crate) fn analyse(
    run: &mut Run<'_>,
    cs: &Object,
    loc: Loc,
    inline: bool,
    depth: usize,
) -> Space {
    if depth > super::MAX_NESTING {
        run.sink
            .limit("colour spaces nested more than 12 levels deep were not followed");
        return Space::Invalid;
    }
    if let Object::Reference(id) = cs {
        if let Some(cached) = run.cs_cache.get(id) {
            return cached.clone();
        }
        let loc = Loc::at(loc.page, Some(*id));
        let space = analyse_resolved(run, cs, loc, inline, depth);
        run.cs_cache.insert(*id, space.clone());
        return space;
    }
    analyse_resolved(run, cs, loc, inline, depth)
}

fn analyse_resolved(run: &mut Run<'_>, cs: &Object, loc: Loc, inline: bool, depth: usize) -> Space {
    match run.resolve(cs) {
        Look::Absent => malformed(run, loc, "the colour space is null".to_string()),
        Look::Dangling(id) => malformed(
            run,
            loc,
            format!(
                "the colour space refers to object {} {}, which does not exist",
                id.0, id.1
            ),
        ),
        Look::Found(_, Object::Name(n)) => named(run, n, loc, inline),
        Look::Found(id, Object::Array(items)) => {
            let loc = Loc::at(loc.page, id.or(loc.obj));
            array(run, items, loc, inline, depth)
        }
        Look::Found(_, other) => malformed(
            run,
            loc,
            format!(
                "a colour space must be a name or an array, found {}",
                other.enum_variant()
            ),
        ),
    }
}

fn named(run: &mut Run<'_>, n: &[u8], loc: Loc, inline: bool) -> Space {
    match n {
        b"DeviceGray" => Space::Gray,
        b"DeviceRGB" => Space::Rgb,
        b"DeviceCMYK" => Space::Cmyk,
        b"Pattern" => Space::Pattern,
        b"G" if inline => Space::Gray,
        b"RGB" if inline => Space::Rgb,
        b"CMYK" if inline => Space::Cmyk,
        b"CalGray" | b"CalRGB" | b"Lab" | b"ICCBased" | b"Indexed" | b"Separation" | b"DeviceN" => {
            malformed(
                run,
                loc,
                format!(
                    "the colour space /{} must be an array, not a bare name",
                    name_text(n)
                ),
            )
        }
        _ => {
            run.sink.error(
                "colour-space-unknown",
                loc,
                format!("/{} is not a colour space that PDF defines", name_text(n)),
            );
            Space::Invalid
        }
    }
}

fn array(run: &mut Run<'_>, items: &[Object], loc: Loc, inline: bool, depth: usize) -> Space {
    let Some(first) = items.first() else {
        return malformed(run, loc, "the colour space array is empty".to_string());
    };
    let Look::Found(_, Object::Name(family)) = run.resolve(first) else {
        return malformed(
            run,
            loc,
            "the first element of a colour space array is not a name".to_string(),
        );
    };
    let rest = items.get(1..).unwrap_or(&[]);
    let fam = name_text(family);
    let want = |run: &mut Run<'_>, ok: bool, what: &str| -> bool {
        if !ok {
            run.sink.error(
                "colour-space-malformed",
                loc,
                format!("the /{fam} colour space {what}"),
            );
        }
        ok
    };

    match family.as_slice() {
        b"DeviceGray" | b"G" => Space::Gray,
        b"DeviceRGB" | b"RGB" => Space::Rgb,
        b"DeviceCMYK" | b"CMYK" => Space::Cmyk,
        b"ICCBased" => {
            if !want(
                run,
                items.len() == 2,
                "needs exactly two elements: the name and the profile stream",
            ) {
                return Space::Invalid;
            }
            match rest.first().map(|o| run.resolve(o)) {
                Some(Look::Found(id, Object::Stream(s))) => icc(run, s, id, loc),
                Some(Look::Dangling(id)) => malformed(
                    run,
                    loc,
                    format!(
                        "the /ICCBased profile is object {} {}, which does not exist",
                        id.0, id.1
                    ),
                ),
                _ => malformed(
                    run,
                    loc,
                    "the second element of /ICCBased is not a profile stream".to_string(),
                ),
            }
        }
        b"CalGray" | b"CalRGB" | b"Lab" => {
            if !want(
                run,
                items.len() == 2,
                "needs exactly two elements: the name and a dictionary",
            ) {
                return Space::Invalid;
            }
            let Some(d) = rest.first().and_then(|o| run.resolve(o).dict()) else {
                return malformed(
                    run,
                    loc,
                    format!("the second element of /{fam} is not a dictionary"),
                );
            };
            let triple = |run: &mut Run<'_>, key: &str, n: usize, required: bool| -> bool {
                match look(run.doc, d, key) {
                    Look::Absent => {
                        if required {
                            run.sink.error(
                                "colour-space-malformed",
                                loc,
                                format!("the /{fam} dictionary has no /{key}"),
                            );
                        }
                        !required
                    }
                    Look::Found(_, Object::Array(a))
                        if a.len() == n
                            && a.iter().all(|x| resolve(run.doc, x).number().is_some()) =>
                    {
                        true
                    }
                    _ => {
                        run.sink.error(
                            "colour-space-malformed",
                            loc,
                            format!("/{key} of /{fam} is not an array of {n} numbers"),
                        );
                        false
                    }
                }
            };
            let mut ok = triple(run, "WhitePoint", 3, true);
            match family.as_slice() {
                b"CalGray" => {}
                b"CalRGB" => {
                    ok &= triple(run, "Gamma", 3, false);
                    ok &= triple(run, "Matrix", 9, false);
                }
                _ => ok &= triple(run, "Range", 4, false),
            }
            if !ok {
                return Space::Invalid;
            }
            match family.as_slice() {
                b"CalGray" => Space::CalGray,
                b"CalRGB" => Space::CalRgb,
                _ => Space::Lab,
            }
        }
        b"Indexed" | b"I" => indexed(run, rest, loc, depth, items.len()),
        b"Separation" => {
            if !want(run, items.len() == 4, "needs four elements: the name, a colorant name, an alternate space and a tint function") {
                return Space::Invalid;
            }
            let mut ok = matches!(
                rest.first().map(|o| run.resolve(o)),
                Some(Look::Found(_, Object::Name(_)))
            );
            if !ok {
                malformed(
                    run,
                    loc,
                    "the colorant of /Separation is not a name".to_string(),
                );
            }
            ok &= alternate(run, rest.get(1), loc, depth, "Separation");
            ok &= function(run, rest.get(2), loc, "Separation");
            if ok {
                Space::Separation
            } else {
                Space::Invalid
            }
        }
        b"DeviceN" => {
            if !want(run, items.len() == 4 || items.len() == 5, "needs four or five elements: the name, the colorant names, an alternate space, a tint function and an optional dictionary") {
                return Space::Invalid;
            }
            let names_ok = match rest.first().map(|o| run.resolve(o)) {
                Some(Look::Found(_, Object::Array(a))) => {
                    !a.is_empty()
                        && a.iter()
                            .all(|x| matches!(run.resolve(x), Look::Found(_, Object::Name(_))))
                }
                _ => false,
            };
            if !names_ok {
                malformed(
                    run,
                    loc,
                    "the colorant names of /DeviceN are not a non-empty array of names".to_string(),
                );
            }
            let mut ok = names_ok;
            ok &= alternate(run, rest.get(1), loc, depth, "DeviceN");
            ok &= function(run, rest.get(2), loc, "DeviceN");
            if ok {
                Space::DeviceN
            } else {
                Space::Invalid
            }
        }
        b"Pattern" => {
            if !want(run, items.len() <= 2, "has more than two elements") {
                return Space::Invalid;
            }
            if let Some(base) = rest.first() {
                let b = analyse(run, base, loc, inline, depth.saturating_add(1));
                if b == Space::Pattern {
                    return malformed(
                        run,
                        loc,
                        "the base of a /Pattern space is a /Pattern space".to_string(),
                    );
                }
            }
            Space::Pattern
        }
        _ => {
            run.sink.error(
                "colour-space-unknown",
                loc,
                format!("/{fam} is not a colour space family that PDF defines"),
            );
            Space::Invalid
        }
    }
}

/// The alternate space of a Separation or DeviceN space.
fn alternate(run: &mut Run<'_>, alt: Option<&Object>, loc: Loc, depth: usize, fam: &str) -> bool {
    let Some(alt) = alt else {
        return false;
    };
    match analyse(run, alt, loc, false, depth.saturating_add(1)) {
        Space::Invalid => false,
        Space::Pattern => {
            run.sink.error(
                "colour-space-malformed",
                loc,
                format!("the alternate space of /{fam} is /Pattern"),
            );
            false
        }
        _ => true,
    }
}

/// The tint transform: a function dictionary or a function stream.
fn function(run: &mut Run<'_>, f: Option<&Object>, loc: Loc, fam: &str) -> bool {
    let ok = f.is_some_and(|o| {
        run.resolve(o)
            .dict()
            .is_some_and(|d| d.has(b"FunctionType"))
    });
    if !ok {
        run.sink.error(
            "colour-space-malformed",
            loc,
            format!("the tint transform of /{fam} is not a function (a dictionary or stream with /FunctionType)"),
        );
    }
    ok
}

fn indexed(run: &mut Run<'_>, rest: &[Object], loc: Loc, depth: usize, len: usize) -> Space {
    if len != 4 {
        return malformed(
            run,
            loc,
            "the /Indexed colour space needs four elements: the name, a base space, the highest index and a lookup table".to_string(),
        );
    }
    let base = match rest.first() {
        Some(b) => analyse(run, b, loc, false, depth.saturating_add(1)),
        None => Space::Invalid,
    };
    match base {
        Space::Invalid => return Space::Invalid,
        Space::Pattern | Space::Indexed(_) => {
            return malformed(
                run,
                loc,
                "the base of an /Indexed space is /Pattern or /Indexed".to_string(),
            )
        }
        _ => {}
    }
    let hival = rest.get(1).and_then(|o| run.resolve(o).integer());
    let Some(hival) = hival.filter(|h| (0..=255).contains(h)) else {
        return malformed(
            run,
            loc,
            "the highest index of an /Indexed space is not an integer from 0 to 255".to_string(),
        );
    };
    let lookup_len = match rest.get(2).map(|o| run.resolve(o)) {
        Some(Look::Found(_, Object::String(s, _))) => Some(s.len()),
        Some(Look::Found(_, Object::Stream(s))) => {
            // Only a lookup table without a filter has a length that can be compared.
            if s.dict.has(b"Filter") {
                None
            } else {
                Some(s.content.len())
            }
        }
        _ => {
            return malformed(
                run,
                loc,
                "the lookup table of an /Indexed space is not a string or a stream".to_string(),
            )
        }
    };
    if let (Some(have), Some(comps)) = (lookup_len, base.components()) {
        let need = usize::try_from(hival)
            .unwrap_or(0)
            .saturating_add(1)
            .saturating_mul(comps);
        if have < need {
            return malformed(
                run,
                loc,
                format!(
                    "the lookup table of an /Indexed space has {have} bytes, but {need} are needed"
                ),
            );
        }
    }
    Space::Indexed(Box::new(base))
}

/// The colour space signature at byte 16 of an ICC profile, as the component count it implies.
fn header_components(sig: &[u8]) -> Option<i64> {
    match sig {
        b"GRAY" => Some(1),
        b"RGB " => Some(3),
        b"CMYK" => Some(4),
        _ => None,
    }
}

fn icc(run: &mut Run<'_>, s: &lopdf::Stream, id: Option<ObjectId>, loc: Loc) -> Space {
    let n = look(run.doc, &s.dict, "N").integer();
    let n = match n {
        Some(n @ (1 | 3 | 4)) => n,
        _ => {
            run.sink.error(
                "icc-profile-invalid",
                loc,
                "the /N of an ICC profile stream is missing or not 1, 3 or 4".to_string(),
            );
            return Space::Invalid;
        }
    };
    // Each profile object is read once.
    let first = id.is_none_or(|i| run.first_visit(Seen::Profile, i));
    if first {
        if let Some(problem) = profile_problem(run, s, n) {
            run.sink.error("icc-profile-invalid", loc, problem);
            return Space::Invalid;
        }
    }
    Space::Icc(n)
}

/// What is wrong with the header of an ICC profile, if anything.
pub(super) fn profile_problem(run: &mut Run<'_>, s: &lopdf::Stream, n: i64) -> Option<String> {
    let data = match decode(run, s, Budget::Validation) {
        Decoded::Data(d) => d,
        Decoded::Failed(e) => {
            return Some(format!("the ICC profile stream cannot be decoded: {e}"))
        }
        Decoded::Skipped => return None,
    };
    if data.len() < 128 {
        return Some(format!(
            "the ICC profile has {} bytes, fewer than its 128 byte header",
            data.len()
        ));
    }
    if data.get(36..40) != Some(b"acsp") {
        return Some("the ICC profile header does not carry the signature \"acsp\"".to_string());
    }
    let implied = data.get(16..20).and_then(header_components);
    match implied {
        Some(m) if m != n => Some(format!(
            "/N is {n}, but the header of the ICC profile describes a space with {m} components"
        )),
        _ => None,
    }
}

/// Records the use of a colour space for the rules about device colour and PDF/X-1a. `page` is
/// the page that uses it.
pub(crate) fn note_use(run: &mut Run<'_>, page: Option<usize>, sp: &Space) {
    let profile = run.profile();
    let loc = Loc::at(page, None);
    match sp {
        Space::Indexed(base) => note_use(run, page, base),
        Space::Gray | Space::Rgb | Space::Cmyk => {
            if let Some(i) = device_index(sp) {
                if let Some(slot) = run.device_use.get_mut(i) {
                    if slot.is_none() {
                        *slot = Some(page);
                    }
                }
            }
            if profile == super::PreflightProfile::PdfX1a_2001 && *sp == Space::Rgb {
                run.sink.once(
                    Severity::Error,
                    "rgb-colour-in-pdfx1a",
                    String::new(),
                    loc,
                    "DeviceRGB is used; PDF/X-1a allows only gray, CMYK and spot colours"
                        .to_string(),
                );
            }
        }
        Space::CalGray | Space::CalRgb | Space::Lab | Space::Icc(_) => {
            if profile == super::PreflightProfile::PdfX1a_2001 {
                let what = match sp {
                    Space::CalGray => "CalGray",
                    Space::CalRgb => "CalRGB",
                    Space::Lab => "Lab",
                    _ => "ICCBased",
                };
                run.sink.once(
                    Severity::Error,
                    "colour-space-not-allowed",
                    what.to_string(),
                    loc,
                    format!("a {what} colour space is used; PDF/X-1a allows only device colour and spot colours"),
                );
            }
        }
        _ => {}
    }
}

/// After the walk: the device colour spaces that PDF/A needs an output intent or a default
/// colour space for.
pub(crate) fn finish_device_colour(run: &mut Run<'_>) {
    if !run.profile().is_pdfa() {
        return;
    }
    for (i, first_page) in run.device_use.into_iter().enumerate() {
        let Some(first_page) = first_page else {
            continue;
        };
        let name = DEVICE_NAMES.get(i).copied().unwrap_or("Device");
        if run.default_spaces.get(i).copied().unwrap_or(false) {
            continue;
        }
        let wanted: Option<i64> = match i {
            1 => Some(3),
            2 => Some(4),
            _ => None,
        };
        let covered = match wanted {
            Some(n) => run.intent_components.contains(&n),
            None => run.has_output_intent,
        };
        if covered {
            continue;
        }
        let need = match wanted {
            Some(n) => format!("an output intent with a {n}-component profile"),
            None => "an output intent".to_string(),
        };
        run.sink.once(
            Severity::Error,
            "device-colour-without-output-intent",
            name.to_string(),
            Loc::at(first_page, None),
            format!(
                "{name} is used, but the file has neither {need} nor a Default{} colour space",
                &name.get(6..).unwrap_or("")
            ),
        );
    }
}