pdfboss-write 0.24.0

PDF creation for pdfboss: COS object writer, content canvas and document assembly
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
//! The compose element vocabulary: painted content one step above raw
//! canvas operators. A [`Content`] value sits in [`crate::pdf::Page::content`]
//! and lowers onto the page's [`Canvas`] at assemble time, after any
//! operators the caller already painted there directly — elements paint
//! over manual canvas work, never under it.

use pdfboss_core::content::Op;
use pdfboss_core::Point;

use crate::canvas::Canvas;
use crate::color::Color;
use crate::error::{Error, Result};
use crate::font::Standard14;
use crate::image::ImageData;
use crate::pdf::{LinkAnnotation, LinkTarget};

/// Something that paints itself onto a [`Canvas`]. `Send` so a
/// [`Content::Custom`] value never blocks a `Pdf` from crossing a thread
/// boundary.
pub trait Draw: Send {
    /// Paints this value's content onto `canvas`.
    fn draw(&self, canvas: &mut Canvas) -> Result<()>;
}

/// One line of text at a fixed baseline origin.
#[derive(Debug, Clone, PartialEq)]
pub struct Text {
    /// The characters to show.
    pub value: String,
    /// Baseline origin, in page user-space units.
    pub at: Point,
    /// Face to draw in.
    pub font: Standard14,
    /// Font size, in points.
    pub size: f32,
    /// Fill color.
    pub color: Color,
}

impl Default for Text {
    fn default() -> Text {
        Text {
            value: String::new(),
            at: Point::default(),
            font: Standard14::Helvetica,
            size: 12.0,
            color: Color::BLACK,
        }
    }
}

impl Draw for Text {
    fn draw(&self, canvas: &mut Canvas) -> Result<()> {
        canvas.set_fill(self.color);
        canvas.text(&self.value, self.at.x, self.at.y, self.font, self.size)
    }
}

/// A raster image placed at a point.
///
/// No `Default`: there is no meaningful default for `data`.
#[derive(Debug, Clone, PartialEq)]
pub struct Image {
    /// The pixels to embed.
    pub data: ImageData,
    /// Placement origin (the box's bottom-left corner), in page user-space
    /// units.
    pub at: Point,
    /// Explicit width, in points. `None` derives it from `height` (by
    /// aspect) or, with `height` also `None`, from the image's pixel width
    /// at 72 dpi.
    pub width: Option<f32>,
    /// Explicit height, in points. `None` derives it from `width` (by
    /// aspect) or, with `width` also `None`, from the image's pixel height
    /// at 72 dpi.
    pub height: Option<f32>,
}

impl Image {
    /// The box this image paints into: the natural size at 72 dpi when
    /// neither dimension is given, the other dimension scaled by aspect
    /// when one is given, or the exact box when both are given.
    pub fn placed_size(&self) -> (f32, f32) {
        let natural_width = self.data.width() as f32;
        let natural_height = self.data.height() as f32;
        match (self.width, self.height) {
            (Some(width), Some(height)) => (width, height),
            (Some(width), None) => (width, width * natural_height / natural_width),
            (None, Some(height)) => (height * natural_width / natural_height, height),
            (None, None) => (natural_width, natural_height),
        }
    }
}

impl Draw for Image {
    fn draw(&self, canvas: &mut Canvas) -> Result<()> {
        let (width, height) = self.placed_size();
        let handle = canvas.add_image(self.data.clone());
        canvas.draw_image(handle, self.at.x, self.at.y, width, height);
        Ok(())
    }
}

/// A clickable rectangle, lowered into a [`LinkAnnotation`] on the page.
#[derive(Debug, Clone, PartialEq)]
pub struct Link {
    /// The clickable area, `[x0, y0, x1, y1]` in the page's user space.
    pub rect: [f32; 4],
    /// Where the link goes.
    pub target: LinkTarget,
}

/// Horizontal alignment for a [`Paragraph`].
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum ParagraphAlign {
    /// Flush to the rect's left edge.
    #[default]
    Left,
    /// Centered within the rect width.
    Center,
    /// Flush to the rect's right edge.
    Right,
    /// Flush to both edges: word spacing stretches every line but the
    /// last.
    Justify,
}

/// A block of text wrapped, aligned, and (for [`ParagraphAlign::Justify`])
/// stretched to fill a rectangle.
#[derive(Debug, Clone, PartialEq)]
pub struct Paragraph {
    /// The text to lay out. `\n` forces a line break; other whitespace runs
    /// between words collapse to a single space.
    pub text: String,
    /// The box to wrap into, `[x0, y0, x1, y1]` in page user-space units.
    pub rect: [f32; 4],
    /// Face to draw in.
    pub font: Standard14,
    /// Font size, in points.
    pub size: f32,
    /// Line-to-line advance, in points. `None` defaults to `1.2 * size`.
    pub leading: Option<f32>,
    /// Horizontal alignment within the rect.
    pub align: ParagraphAlign,
}

impl Default for Paragraph {
    fn default() -> Paragraph {
        Paragraph {
            text: String::new(),
            rect: [0.0, 0.0, 0.0, 0.0],
            font: Standard14::Helvetica,
            size: 11.0,
            leading: None,
            align: ParagraphAlign::Left,
        }
    }
}

/// Greedily wraps `text` to `max_width`: `\n` forces a break, other
/// whitespace runs between words collapse to single spaces, and a blank
/// source line yields an empty line so its vertical advance survives. A
/// word wider than `max_width` on its own still gets a line — no
/// hyphenation.
fn wrap_lines(text: &str, font: Standard14, size: f32, max_width: f32) -> Result<Vec<Vec<&str>>> {
    let mut lines = Vec::new();
    for source_line in text.split('\n') {
        let words: Vec<&str> = source_line.split_whitespace().collect();
        if words.is_empty() {
            lines.push(Vec::new());
            continue;
        }
        let mut current: Vec<&str> = Vec::new();
        let mut current_text = String::new();
        for word in words {
            let candidate = if current.is_empty() {
                word.to_string()
            } else {
                format!("{current_text} {word}")
            };
            let width = font.text_width(&candidate, size)?;
            if current.is_empty() || width <= max_width {
                current.push(word);
                current_text = candidate;
                continue;
            }
            lines.push(std::mem::take(&mut current));
            current_text = word.to_string();
            current.push(word);
        }
        lines.push(current);
    }
    Ok(lines)
}

/// The number of lines whose baseline fits between `y0` and the first
/// baseline at `y1 - size`, stepping down by `leading` per further line. A
/// small epsilon absorbs floating-point rounding at exact boundaries.
fn lines_that_fit(y0: f32, y1: f32, size: f32, leading: f32) -> usize {
    const EPSILON: f32 = 1e-3;
    let first_baseline = y1 - size;
    if first_baseline < y0 - EPSILON {
        return 0;
    }
    (((first_baseline - y0) / leading) + EPSILON).floor() as usize + 1
}

/// `Tw` (word spacing) is persistent text state that `BeginText` does not
/// clear, so a stretch an earlier line left active must be reset before the
/// first following line that does not itself stretch — not only once, at
/// the very end.
impl Draw for Paragraph {
    fn draw(&self, canvas: &mut Canvas) -> Result<()> {
        let [x0, y0, x1, y1] = self.rect;
        let width = x1 - x0;
        let leading = self.leading.unwrap_or(1.2 * self.size);
        let lines = wrap_lines(&self.text, self.font, self.size, width)?;
        let fits = lines_that_fit(y0, y1, self.size, leading);
        if lines.len() > fits {
            return Err(Error::Other(format!(
                "paragraph overflows its rect: {fits} lines fit, {} needed",
                lines.len()
            )));
        }
        let last_visible = lines.iter().rposition(|words| !words.is_empty());
        let mut stretch_active = false;
        for (index, words) in lines.iter().enumerate() {
            if words.is_empty() {
                continue;
            }
            let baseline = y1 - self.size - index as f32 * leading;
            let line_text = words.join(" ");
            let line_width = self.font.text_width(&line_text, self.size)?;
            let is_final = Some(index) == last_visible;
            let stretch = match self.align {
                ParagraphAlign::Justify if !is_final && words.len() >= 2 => {
                    Some((width - line_width) / (words.len() as f32 - 1.0))
                }
                _ => None,
            };
            let x = match self.align {
                ParagraphAlign::Right => x1 - line_width,
                ParagraphAlign::Center => x0 + (width - line_width) / 2.0,
                ParagraphAlign::Left | ParagraphAlign::Justify => x0,
            };
            if stretch.is_none() && stretch_active {
                canvas.op(Op::SetWordSpacing(0.0));
                stretch_active = false;
            }
            if let Some(spacing) = stretch {
                canvas.op(Op::SetWordSpacing(spacing));
                stretch_active = true;
            }
            canvas.text(&line_text, x, baseline, self.font, self.size)?;
        }
        if stretch_active {
            canvas.op(Op::SetWordSpacing(0.0));
        }
        Ok(())
    }
}

/// One piece of composed page content.
pub enum Content {
    /// A line of text.
    Text(Text),
    /// A placed image.
    Image(Image),
    /// A clickable link area.
    Link(Link),
    /// A wrapped, aligned block of text.
    Paragraph(Paragraph),
    /// Anything implementing [`Draw`].
    Custom(Box<dyn Draw>),
}

impl std::fmt::Debug for Content {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Content::Text(text) => f.debug_tuple("Text").field(text).finish(),
            Content::Image(image) => f.debug_tuple("Image").field(image).finish(),
            Content::Link(link) => f.debug_tuple("Link").field(link).finish(),
            Content::Paragraph(paragraph) => f.debug_tuple("Paragraph").field(paragraph).finish(),
            Content::Custom(..) => f.write_str("Custom(..)"),
        }
    }
}

impl From<Text> for Content {
    fn from(value: Text) -> Content {
        Content::Text(value)
    }
}

impl From<Image> for Content {
    fn from(value: Image) -> Content {
        Content::Image(value)
    }
}

impl From<Link> for Content {
    fn from(value: Link) -> Content {
        Content::Link(value)
    }
}

impl From<Paragraph> for Content {
    fn from(value: Paragraph) -> Content {
        Content::Paragraph(value)
    }
}

impl Content {
    /// Wraps any [`Draw`] value as custom content.
    pub fn custom(value: impl Draw + 'static) -> Content {
        Content::Custom(Box::new(value))
    }
}

/// Paints `content` onto `canvas`, in order, after any operators already
/// there; link elements are appended to `links` instead of painted.
pub(crate) fn lower(
    content: Vec<Content>,
    canvas: &mut Canvas,
    links: &mut Vec<LinkAnnotation>,
) -> Result<()> {
    for item in content {
        match item {
            Content::Text(text) => text.draw(canvas)?,
            Content::Image(image) => image.draw(canvas)?,
            Content::Link(link) => links.push(LinkAnnotation {
                rect: link.rect,
                target: link.target,
            }),
            Content::Paragraph(paragraph) => paragraph.draw(canvas)?,
            Content::Custom(drawable) => drawable.draw(canvas)?,
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use pdfboss_core::content::{parse_content, Op};
    use pdfboss_core::{Document, Name};
    use pdfboss_output::extract_text;

    use super::*;
    use crate::pdf::{Page, PageSize};
    use crate::Pdf;

    #[test]
    fn elements_lower_in_sequence_order_after_canvas_ops() {
        let mut page = Page::new(PageSize::A4);
        page.canvas
            .text("under", 72.0, 100.0, Standard14::Helvetica, 10.0)
            .unwrap();
        page.content.push(Content::from(Text {
            value: "over".into(),
            at: Point::new(72.0, 700.0),
            size: 24.0,
            ..Text::default()
        }));
        let ops_before = page.canvas.ops().len();
        let bytes = Pdf {
            pages: vec![page],
            ..Pdf::default()
        }
        .to_bytes()
        .unwrap();
        let doc = Document::load(bytes).unwrap();
        let loaded = doc.page(0).unwrap();
        let text = extract_text(&doc, &loaded).unwrap();
        assert!(text.contains("under") && text.contains("over"));
        assert!(ops_before > 0);

        let stream = loaded.content(&doc).unwrap();
        let ops = parse_content(&stream).unwrap();
        let index_of = |needle: &[u8]| {
            ops.iter()
                .position(|op| matches!(op, Op::ShowText(s) if s == needle))
                .unwrap_or_else(|| {
                    panic!(
                        "no ShowText carrying {:?} in {:?}",
                        String::from_utf8_lossy(needle),
                        ops
                    )
                })
        };
        let under_index = index_of(b"under");
        let over_index = index_of(b"over");
        assert!(
            under_index < over_index,
            "expected \"under\" ({under_index}) before \"over\" ({over_index})"
        );
    }

    #[test]
    fn custom_draw_paints_through_the_canvas() {
        struct Letterhead;
        impl Draw for Letterhead {
            fn draw(&self, canvas: &mut Canvas) -> Result<()> {
                canvas.set_line_width(0.5);
                canvas.move_to(72.0, 806.0);
                canvas.line_to(523.0, 806.0);
                canvas.stroke();
                canvas.text("ACME GmbH", 72.0, 812.0, Standard14::Helvetica, 8.0)
            }
        }
        let custom = Content::custom(Letterhead);
        assert_eq!(format!("{custom:?}"), "Custom(..)");
        let mut page = Page::new(PageSize::A4);
        page.content.push(custom);
        let bytes = Pdf {
            pages: vec![page],
            ..Pdf::default()
        }
        .to_bytes()
        .unwrap();
        let doc = Document::load(bytes).unwrap();
        let loaded = doc.page(0).unwrap();
        let text = extract_text(&doc, &loaded).unwrap();
        assert!(text.contains("ACME GmbH"));
    }

    #[test]
    fn image_placed_size_is_natural_at_72dpi_when_both_none() {
        let image = Image {
            data: ImageData::gray8(16, 8, vec![0u8; 128]).unwrap(),
            at: Point::new(10.0, 10.0),
            width: None,
            height: None,
        };
        assert_eq!(image.placed_size(), (16.0, 8.0));
    }

    #[test]
    fn image_element_scales_by_aspect_when_one_dimension_is_given() {
        let image = Image {
            data: ImageData::gray8(16, 8, vec![0u8; 128]).unwrap(),
            at: Point::new(10.0, 10.0),
            width: Some(32.0),
            height: None,
        };
        assert_eq!(image.placed_size(), (32.0, 16.0));
    }

    #[test]
    fn image_placed_size_scales_by_aspect_when_only_height_is_given() {
        let image = Image {
            data: ImageData::gray8(16, 8, vec![0u8; 128]).unwrap(),
            at: Point::new(10.0, 10.0),
            width: None,
            height: Some(4.0),
        };
        assert_eq!(image.placed_size(), (8.0, 4.0));
    }

    #[test]
    fn image_placed_size_is_exact_when_both_dimensions_are_given() {
        let image = Image {
            data: ImageData::gray8(16, 8, vec![0u8; 128]).unwrap(),
            at: Point::new(10.0, 10.0),
            width: Some(50.0),
            height: Some(90.0),
        };
        assert_eq!(image.placed_size(), (50.0, 90.0));
    }

    #[test]
    fn text_draw_sets_fill_then_shows_text() {
        let mut canvas = Canvas::new();
        let text = Text {
            value: "hi".into(),
            at: Point::new(1.0, 2.0),
            font: Standard14::Helvetica,
            size: 10.0,
            color: Color::Rgb(1.0, 0.0, 0.0),
        };
        text.draw(&mut canvas).unwrap();
        assert_eq!(
            canvas.ops(),
            [
                Op::SetFillRGB(1.0, 0.0, 0.0),
                Op::BeginText,
                Op::SetFont(Name("F1".into()), 10.0),
                Op::TextMove(1.0, 2.0),
                Op::ShowText(b"hi".to_vec()),
                Op::EndText,
            ]
        );
    }

    #[test]
    fn paragraph_default_is_helvetica_11_left_and_none_leading() {
        let paragraph = Paragraph::default();
        assert_eq!(paragraph.text, "");
        assert_eq!(paragraph.font, Standard14::Helvetica);
        assert_eq!(paragraph.size, 11.0);
        assert_eq!(paragraph.leading, None);
        assert_eq!(paragraph.align, ParagraphAlign::Left);
    }

    #[test]
    fn content_from_paragraph_debug_prints_paragraph() {
        let content = Content::from(Paragraph::default());
        assert_eq!(
            format!("{content:?}"),
            format!("Paragraph({:?})", Paragraph::default())
        );
    }

    #[test]
    fn paragraph_wraps_at_word_boundaries_courier_metrics() {
        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: "aaaaaaaaa bbbbbbbbbb cccccccccc".into(),
            rect: [0.0, 0.0, 120.0, 100.0],
            font: Standard14::Courier,
            size: 10.0,
            ..Paragraph::default()
        };
        lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap();
        assert_eq!(
            canvas.ops(),
            [
                Op::BeginText,
                Op::SetFont(Name("F1".into()), 10.0),
                Op::TextMove(0.0, 90.0),
                Op::ShowText(b"aaaaaaaaa bbbbbbbbbb".to_vec()),
                Op::EndText,
                Op::BeginText,
                Op::SetFont(Name("F1".into()), 10.0),
                Op::TextMove(0.0, 78.0),
                Op::ShowText(b"cccccccccc".to_vec()),
                Op::EndText,
            ]
        );
    }

    #[test]
    fn paragraph_overflow_reports_lines_fit_and_needed() {
        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: "aaaaaaaaa bbbbbbbbbb cccccccccc".into(),
            rect: [0.0, 80.0, 120.0, 95.0],
            font: Standard14::Courier,
            size: 10.0,
            ..Paragraph::default()
        };
        let err = lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap_err();
        match err {
            Error::Other(msg) => {
                assert_eq!(msg, "paragraph overflows its rect: 1 lines fit, 2 needed")
            }
            other => panic!("expected Error::Other, got {other:?}"),
        }
    }

    #[test]
    fn paragraph_justify_stretches_non_final_lines_and_resets_once() {
        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: "aaaaaaa bbbbbbb ddddd".into(),
            rect: [0.0, 0.0, 120.0, 100.0],
            font: Standard14::Courier,
            size: 10.0,
            align: ParagraphAlign::Justify,
            ..Paragraph::default()
        };
        lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap();
        assert_eq!(
            canvas.ops(),
            [
                Op::SetWordSpacing(30.0),
                Op::BeginText,
                Op::SetFont(Name("F1".into()), 10.0),
                Op::TextMove(0.0, 90.0),
                Op::ShowText(b"aaaaaaa bbbbbbb".to_vec()),
                Op::EndText,
                Op::SetWordSpacing(0.0),
                Op::BeginText,
                Op::SetFont(Name("F1".into()), 10.0),
                Op::TextMove(0.0, 78.0),
                Op::ShowText(b"ddddd".to_vec()),
                Op::EndText,
            ],
            "the reset must land before the final line's BeginText, not after it \
             (Tw is persistent text state that BeginText does not clear)"
        );

        let bytes = crate::content::serialize_ops(canvas.ops());
        let parsed = parse_content(&bytes).unwrap();
        let stretched: Vec<f32> = parsed
            .iter()
            .filter_map(|op| match op {
                Op::SetWordSpacing(value) if *value > 0.0 => Some(*value),
                _ => None,
            })
            .collect();
        assert_eq!(stretched, [30.0]);
        assert!(parsed.contains(&Op::SetWordSpacing(0.0)));
    }

    #[test]
    fn paragraph_justify_final_line_with_multiple_words_gets_no_leftover_spacing() {
        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: "aaaaaaa bbbbbbb ccccc dd".into(),
            rect: [0.0, 0.0, 120.0, 100.0],
            font: Standard14::Courier,
            size: 10.0,
            align: ParagraphAlign::Justify,
            ..Paragraph::default()
        };
        lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap();
        let expected = [
            Op::SetWordSpacing(30.0),
            Op::BeginText,
            Op::SetFont(Name("F1".into()), 10.0),
            Op::TextMove(0.0, 90.0),
            Op::ShowText(b"aaaaaaa bbbbbbb".to_vec()),
            Op::EndText,
            Op::SetWordSpacing(0.0),
            Op::BeginText,
            Op::SetFont(Name("F1".into()), 10.0),
            Op::TextMove(0.0, 78.0),
            Op::ShowText(b"ccccc dd".to_vec()),
            Op::EndText,
        ];
        assert_eq!(
            canvas.ops(),
            expected,
            "the final line has 2 words (a space glyph) — a leftover non-zero \
             Tw here would visibly over-stretch it, which is exactly the bug"
        );

        let bytes = crate::content::serialize_ops(canvas.ops());
        let parsed = parse_content(&bytes).unwrap();
        let reset_index = parsed
            .iter()
            .position(|op| *op == Op::SetWordSpacing(0.0))
            .expect("a zero reset must be present");
        let final_show_index = parsed
            .iter()
            .position(|op| matches!(op, Op::ShowText(s) if s == b"ccccc dd"))
            .expect("final line's ShowText must be present");
        assert!(
            reset_index < final_show_index,
            "reset (index {reset_index}) must precede the final line's ShowText \
             (index {final_show_index}): {parsed:?}"
        );
        let stray_nonzero_between = parsed[reset_index..final_show_index]
            .iter()
            .any(|op| matches!(op, Op::SetWordSpacing(value) if *value != 0.0));
        assert!(
            !stray_nonzero_between,
            "no non-zero word spacing may sit between the reset and the final \
             line's ShowText: {:?}",
            &parsed[reset_index..final_show_index]
        );
    }

    #[test]
    fn paragraph_trailing_blank_line_does_not_become_the_justify_final_line() {
        let text = "aaaaaaa bbbbbbb ccccc dd\n";

        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: text.into(),
            rect: [0.0, 0.0, 120.0, 100.0],
            font: Standard14::Courier,
            size: 10.0,
            align: ParagraphAlign::Justify,
            ..Paragraph::default()
        };
        lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap();
        assert_eq!(
            canvas.ops(),
            [
                Op::SetWordSpacing(30.0),
                Op::BeginText,
                Op::SetFont(Name("F1".into()), 10.0),
                Op::TextMove(0.0, 90.0),
                Op::ShowText(b"aaaaaaa bbbbbbb".to_vec()),
                Op::EndText,
                Op::SetWordSpacing(0.0),
                Op::BeginText,
                Op::SetFont(Name("F1".into()), 10.0),
                Op::TextMove(0.0, 78.0),
                Op::ShowText(b"ccccc dd".to_vec()),
                Op::EndText,
            ],
            "the trailing blank line (from the trailing \\n) must not steal the \
             'final line never stretches' exemption from \"ccccc dd\""
        );

        let tight_rect_without_trailing_newline = Paragraph {
            text: "aaaaaaa bbbbbbb ccccc dd".into(),
            rect: [0.0, 0.0, 120.0, 24.0],
            font: Standard14::Courier,
            size: 10.0,
            align: ParagraphAlign::Justify,
            ..Paragraph::default()
        };
        let mut fits_canvas = Canvas::new();
        lower(
            vec![tight_rect_without_trailing_newline.into()],
            &mut fits_canvas,
            &mut links,
        )
        .expect("two visible lines fit in a rect sized for exactly two lines");

        let tight_rect_with_trailing_newline = Paragraph {
            text: text.into(),
            rect: [0.0, 0.0, 120.0, 24.0],
            font: Standard14::Courier,
            size: 10.0,
            align: ParagraphAlign::Justify,
            ..Paragraph::default()
        };
        let mut overflow_canvas = Canvas::new();
        let err = lower(
            vec![tight_rect_with_trailing_newline.into()],
            &mut overflow_canvas,
            &mut links,
        )
        .unwrap_err();
        match err {
            Error::Other(msg) => assert_eq!(
                msg, "paragraph overflows its rect: 2 lines fit, 3 needed",
                "the trailing blank line must still count toward vertical advance"
            ),
            other => panic!("expected Error::Other, got {other:?}"),
        }
    }

    #[test]
    fn paragraph_center_and_right_align_offset_by_rect_width() {
        let mut links = Vec::new();
        let base = Paragraph {
            text: "aaaaaaaaa".into(),
            rect: [0.0, 0.0, 120.0, 50.0],
            font: Standard14::Courier,
            size: 10.0,
            ..Paragraph::default()
        };

        let mut center_canvas = Canvas::new();
        lower(
            vec![Paragraph {
                align: ParagraphAlign::Center,
                ..base.clone()
            }
            .into()],
            &mut center_canvas,
            &mut links,
        )
        .unwrap();
        assert_eq!(center_canvas.ops()[2], Op::TextMove(33.0, 40.0));

        let mut right_canvas = Canvas::new();
        lower(
            vec![Paragraph {
                align: ParagraphAlign::Right,
                ..base
            }
            .into()],
            &mut right_canvas,
            &mut links,
        )
        .unwrap();
        assert_eq!(right_canvas.ops()[2], Op::TextMove(66.0, 40.0));
    }

    #[test]
    fn paragraph_blank_line_keeps_advance_without_drawing() {
        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: "a\n\nb".into(),
            rect: [0.0, 0.0, 100.0, 100.0],
            font: Standard14::Helvetica,
            size: 10.0,
            ..Paragraph::default()
        };
        lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap();
        let moves: Vec<&Op> = canvas
            .ops()
            .iter()
            .filter(|op| matches!(op, Op::TextMove(..)))
            .collect();
        assert_eq!(
            moves,
            [&Op::TextMove(0.0, 90.0), &Op::TextMove(0.0, 66.0)],
            "blank line should still consume a leading slot"
        );
        let shows: Vec<&Op> = canvas
            .ops()
            .iter()
            .filter(|op| matches!(op, Op::ShowText(..)))
            .collect();
        assert_eq!(
            shows,
            [&Op::ShowText(b"a".to_vec()), &Op::ShowText(b"b".to_vec()),]
        );
    }

    #[test]
    fn paragraph_leading_override_changes_line_advance() {
        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: "aaaaaaaaaa bbbbbbbbbb".into(),
            rect: [0.0, 0.0, 60.0, 100.0],
            font: Standard14::Courier,
            size: 10.0,
            leading: Some(20.0),
            ..Paragraph::default()
        };
        lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap();
        let moves: Vec<&Op> = canvas
            .ops()
            .iter()
            .filter(|op| matches!(op, Op::TextMove(..)))
            .collect();
        assert_eq!(moves, [&Op::TextMove(0.0, 90.0), &Op::TextMove(0.0, 70.0)]);
    }

    #[test]
    fn paragraph_propagates_unencodable_character_error_untouched() {
        let mut canvas = Canvas::new();
        let mut links = Vec::new();
        let paragraph = Paragraph {
            text: "\u{2318}".into(),
            rect: [0.0, 0.0, 100.0, 100.0],
            font: Standard14::Helvetica,
            size: 10.0,
            ..Paragraph::default()
        };
        let err = lower(vec![paragraph.into()], &mut canvas, &mut links).unwrap_err();
        assert!(matches!(
            err,
            Error::Unencodable {
                ch: '\u{2318}',
                font: "Helvetica"
            }
        ));
    }

    #[test]
    fn image_draw_uses_placed_size() {
        use pdfboss_core::Matrix;

        let mut canvas = Canvas::new();
        let image = Image {
            data: ImageData::gray8(2, 2, vec![0u8; 4]).unwrap(),
            at: Point::new(5.0, 6.0),
            width: Some(40.0),
            height: None,
        };
        image.draw(&mut canvas).unwrap();
        assert_eq!(
            canvas.ops(),
            [
                Op::Save,
                Op::Concat(Matrix {
                    a: 40.0,
                    b: 0.0,
                    c: 0.0,
                    d: 40.0,
                    e: 5.0,
                    f: 6.0,
                }),
                Op::XObject(Name("Im1".into())),
                Op::Restore,
            ]
        );
    }
}