pdfrum-edit 0.5.0

PDF serializer: full/incremental save, page import, subsetting
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
//! Per-subtype builders for [`AnnotSpec`].
//!
//! [`AnnotSpec`] is one enum because the write path matches on it, but its
//! options are not shared: `/IC` belongs to Line, `/H` to Link, `/Open` to
//! Text. Setting them through the enum means every setter has to accept every
//! variant and do nothing on the ones it does not apply to, which turns a
//! misuse into silence rather than an error.
//!
//! The builders here carry one subtype each, so only the options that subtype
//! has exist as methods. Asking a [`TextSpec`] for an interior colour does not
//! compile, where the removed `AnnotSpec::with_interior` accepted a `Text`
//! and dropped the call.
//!
//! An option that does not belong to a subtype is not a method on that
//! subtype's builder, so the mistake is a compile error rather than a
//! silently dropped call:
//!
//! ```compile_fail
//! use kurbo::Rect;
//! use peniko::Color;
//! use pdfrum_edit::TextSpec;
//!
//! // `/IC` belongs to Line, not Text.
//! let _ = TextSpec::new(Rect::new(0.0, 0.0, 1.0, 1.0), Color::from_rgb8(0, 0, 0))
//!     .interior(Color::from_rgb8(0, 0, 255));
//! ```
//!
//! ```compile_fail
//! use kurbo::Rect;
//! use peniko::Color;
//! use pdfrum_edit::{AnnotLinkHighlight, CaretSpec};
//!
//! // `/H` belongs to Link, not Caret.
//! let _ = CaretSpec::new(Rect::new(0.0, 0.0, 1.0, 1.0), Color::from_rgb8(0, 0, 0))
//!     .highlight(AnnotLinkHighlight::Outline);
//! ```
//!
//! Each builder converts with [`Into<AnnotSpec>`], so they are accepted
//! wherever a spec is:
//!
//! ```
//! use kurbo::{Point, Rect};
//! use peniko::Color;
//! use pdfrum_edit::{LineEndingStyle, LineSpec};
//!
//! let spec = LineSpec::new(
//!     Rect::new(0.0, 0.0, 100.0, 20.0),
//!     Color::from_rgb8(255, 0, 0),
//!     Point::new(0.0, 0.0),
//!     Point::new(100.0, 20.0),
//! )
//! .endings(LineEndingStyle::OpenArrow, LineEndingStyle::ClosedArrow)
//! .interior(Color::from_rgb8(0, 0, 255));
//! ```

use kurbo::{Point, Rect};
use pdfrum_object::{Name, ObjRef};
use peniko::Color;

use crate::annot::{
    AnnotBorder, AnnotGoToView, AnnotLinkAction, AnnotLinkHighlight, AnnotMeta, AnnotSpec,
    AnnotWrite, LineEndingStyle, Quad,
};

/// Declares the setters every subtype shares.
///
/// `contents` stays on the builder, since `/Contents` is part of the spec.
/// The rest are annotation *metadata* rather than subtype options, so they
/// answer [`AnnotWrite`] — the same shape `AnnotSpec`'s own metadata setters
/// have, and the reason a typed builder no longer has to fall back to the
/// enum to name an author.
macro_rules! spec_builder {
    ($builder:ident, $doc:literal) => {
        impl $builder {
            #[doc = $doc]
            ///
            /// Sets `/Contents`.
            #[must_use]
            pub fn contents(mut self, contents: impl Into<String>) -> Self {
                self.contents = Some(contents.into());
                self
            }

            #[doc = $doc]
            ///
            /// Sets `/T`, the author.
            #[must_use]
            pub fn author(self, author: impl Into<String>) -> AnnotWrite {
                AnnotSpec::from(self).with_author(author)
            }

            #[doc = $doc]
            ///
            /// Sets `/NM`, the annotation's unique name.
            #[must_use]
            pub fn name(self, name: impl Into<String>) -> AnnotWrite {
                AnnotSpec::from(self).with_name(name)
            }

            #[doc = $doc]
            ///
            /// Sets `/M`, the modification date.
            #[must_use]
            pub fn modified(self, modified: impl Into<String>) -> AnnotWrite {
                AnnotSpec::from(self).with_modified(modified)
            }

            #[doc = $doc]
            ///
            /// Sets `/F`, the annotation flags.
            #[must_use]
            pub fn flags(self, flags: pdfrum_doc::AnnotFlags) -> AnnotWrite {
                AnnotSpec::from(self).with_flags(flags)
            }

            #[doc = $doc]
            ///
            /// Sets every metadata field at once.
            #[must_use]
            pub fn meta(self, meta: AnnotMeta) -> AnnotWrite {
                AnnotSpec::from(self).with_meta(meta)
            }
        }

        // So a builder reaches `add_annotation` directly, the way an
        // `AnnotSpec` does — without it every call site would need an
        // `.into()` naming a type the caller never mentions.
        impl From<$builder> for AnnotWrite {
            fn from(builder: $builder) -> Self {
                AnnotSpec::from(builder).into()
            }
        }
    };
}

/// A text-markup annotation: Highlight, Underline, `StrikeOut`, or Squiggly.
///
/// The four share a shape — a colour and a set of `/QuadPoints` — so one
/// builder covers them and [`MarkupKind`] picks the subtype.
///
/// ```
/// use kurbo::Rect;
/// use peniko::Color;
/// use pdfrum_edit::{AnnotSpec, MarkupKind, MarkupSpec};
///
/// let rect = Rect::new(0.0, 0.0, 80.0, 12.0);
/// let spec: AnnotSpec = MarkupSpec::new(MarkupKind::Highlight, rect, Color::from_rgb8(255, 255, 0))
///     .quads([rect.into()])
///     .contents("note")
///     .into();
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct MarkupSpec {
    kind: MarkupKind,
    rect: Rect,
    color: Color,
    quads: Vec<Quad>,
    contents: Option<String>,
}

/// Which text-markup subtype a [`MarkupSpec`] writes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum MarkupKind {
    /// `/Subtype /Highlight`.
    Highlight,
    /// `/Subtype /Underline`.
    Underline,
    /// `/Subtype /StrikeOut`.
    StrikeOut,
    /// `/Subtype /Squiggly`.
    Squiggly,
}

impl MarkupSpec {
    /// A markup annotation covering `rect` as a single quadrilateral.
    ///
    /// Replace the geometry with [`MarkupSpec::quads`] to mark up a
    /// selection of several runs.
    #[must_use]
    pub fn new(kind: MarkupKind, rect: Rect, color: Color) -> Self {
        Self {
            kind,
            rect,
            color,
            quads: vec![Quad::from(rect)],
            contents: None,
        }
    }

    /// Replaces the `/QuadPoints`, one quadrilateral per marked run.
    ///
    /// Writing a spec whose quads are empty fails with
    /// [`crate::Error::EmptyQuadPoints`]. `pdfrum_text::rects_loose` gives
    /// the em-box rectangles Acrobat marks up.
    #[must_use]
    pub fn quads(mut self, quads: impl IntoIterator<Item = Quad>) -> Self {
        self.quads = quads.into_iter().collect();
        self
    }
}

spec_builder!(MarkupSpec, "A text-markup annotation.");

impl From<MarkupSpec> for AnnotSpec {
    fn from(b: MarkupSpec) -> Self {
        let MarkupSpec {
            kind,
            rect,
            color,
            quads,
            contents,
        } = b;
        match kind {
            MarkupKind::Highlight => Self::Highlight {
                rect,
                color,
                quads,
                contents,
            },
            MarkupKind::Underline => Self::Underline {
                rect,
                color,
                quads,
                contents,
            },
            MarkupKind::StrikeOut => Self::StrikeOut {
                rect,
                color,
                quads,
                contents,
            },
            MarkupKind::Squiggly => Self::Squiggly {
                rect,
                color,
                quads,
                contents,
            },
        }
    }
}

/// A sticky-note text annotation (`/Subtype /Text`).
///
/// ```
/// use kurbo::Rect;
/// use peniko::Color;
/// use pdfrum_edit::{AnnotSpec, TextSpec};
///
/// let spec: AnnotSpec = TextSpec::new(Rect::new(0.0, 0.0, 20.0, 20.0), Color::from_rgb8(255, 255, 0))
///     .icon("Note")
///     .open(true)
///     .into();
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct TextSpec {
    rect: Rect,
    color: Color,
    contents: Option<String>,
    icon: Option<Name>,
    open: bool,
}

impl TextSpec {
    /// A sticky note at `rect`, `/Comment` icon, pop-up closed.
    #[must_use]
    pub fn new(rect: Rect, color: Color) -> Self {
        Self {
            rect,
            color,
            contents: None,
            icon: None,
            open: false,
        }
    }

    /// Sets the icon name (`/Name`), such as `Note` or `Help`.
    #[must_use]
    pub fn icon(mut self, icon: impl Into<Name>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Sets whether the pop-up starts open (`/Open`).
    #[must_use]
    pub fn open(mut self, open: bool) -> Self {
        self.open = open;
        self
    }
}

spec_builder!(TextSpec, "A sticky-note annotation.");

impl From<TextSpec> for AnnotSpec {
    fn from(b: TextSpec) -> Self {
        let TextSpec {
            rect,
            color,
            contents,
            icon,
            open,
        } = b;
        Self::Text {
            rect,
            color,
            contents,
            icon: icon.unwrap_or_else(|| crate::names::COMMENT.clone()),
            open,
        }
    }
}

/// A square / area annotation (`/Subtype /Square`).
#[derive(Debug, Clone, PartialEq)]
pub struct SquareSpec {
    rect: Rect,
    color: Color,
    contents: Option<String>,
    border: Option<AnnotBorder>,
    interior: Option<Color>,
}

impl SquareSpec {
    /// A square over `rect` with the default border (width 2, solid).
    #[must_use]
    pub fn new(rect: Rect, color: Color) -> Self {
        Self {
            rect,
            color,
            contents: None,
            border: None,
            interior: None,
        }
    }

    /// Sets `/BS` width and style.
    #[must_use]
    pub fn border(mut self, border: AnnotBorder) -> Self {
        self.border = Some(border);
        self
    }

    /// Sets `/IC`, the colour filling the shape.
    ///
    /// Left unset the shape is an outline, which is what an absent `/IC`
    /// means to a reader.
    #[must_use]
    pub fn interior(mut self, color: Color) -> Self {
        self.interior = Some(color);
        self
    }
}

spec_builder!(SquareSpec, "A square annotation.");

impl From<SquareSpec> for AnnotSpec {
    fn from(b: SquareSpec) -> Self {
        let SquareSpec {
            rect,
            color,
            contents,
            border,
            interior,
        } = b;
        Self::Square {
            rect,
            color,
            contents,
            border: border.unwrap_or_default(),
            interior,
        }
    }
}

/// A circle / ellipse annotation (`/Subtype /Circle`).
#[derive(Debug, Clone, PartialEq)]
pub struct CircleSpec {
    rect: Rect,
    color: Color,
    contents: Option<String>,
    border: Option<AnnotBorder>,
    interior: Option<Color>,
}

impl CircleSpec {
    /// An ellipse inscribed in `rect` with the default border.
    #[must_use]
    pub fn new(rect: Rect, color: Color) -> Self {
        Self {
            rect,
            color,
            contents: None,
            border: None,
            interior: None,
        }
    }

    /// Sets `/BS` width and style.
    #[must_use]
    pub fn border(mut self, border: AnnotBorder) -> Self {
        self.border = Some(border);
        self
    }

    /// Sets `/IC`, the colour filling the shape.
    ///
    /// Left unset the shape is an outline, which is what an absent `/IC`
    /// means to a reader.
    #[must_use]
    pub fn interior(mut self, color: Color) -> Self {
        self.interior = Some(color);
        self
    }
}

spec_builder!(CircleSpec, "A circle annotation.");

impl From<CircleSpec> for AnnotSpec {
    fn from(b: CircleSpec) -> Self {
        let CircleSpec {
            rect,
            color,
            contents,
            border,
            interior,
        } = b;
        Self::Circle {
            rect,
            color,
            contents,
            border: border.unwrap_or_default(),
            interior,
        }
    }
}

/// A freehand ink annotation (`/Subtype /Ink`).
#[derive(Debug, Clone, PartialEq)]
pub struct InkSpec {
    rect: Rect,
    color: Color,
    strokes: Vec<Vec<Point>>,
    contents: Option<String>,
    border: Option<AnnotBorder>,
}

impl InkSpec {
    /// Ink over `rect`, one polyline per stroke.
    #[must_use]
    pub fn new(rect: Rect, color: Color, strokes: Vec<Vec<Point>>) -> Self {
        Self {
            rect,
            color,
            strokes,
            contents: None,
            border: None,
        }
    }

    /// Sets `/BS` width and style.
    #[must_use]
    pub fn border(mut self, border: AnnotBorder) -> Self {
        self.border = Some(border);
        self
    }
}

spec_builder!(InkSpec, "An ink annotation.");

impl From<InkSpec> for AnnotSpec {
    fn from(b: InkSpec) -> Self {
        let InkSpec {
            rect,
            color,
            strokes,
            contents,
            border,
        } = b;
        Self::Ink {
            rect,
            color,
            strokes,
            contents,
            border: border.unwrap_or_default(),
        }
    }
}

/// A line annotation (`/Subtype /Line`).
///
/// ```
/// use kurbo::{Point, Rect};
/// use peniko::Color;
/// use pdfrum_edit::{AnnotSpec, LineEndingStyle, LineSpec};
///
/// let spec: AnnotSpec = LineSpec::new(
///     Rect::new(0.0, 0.0, 100.0, 20.0),
///     Color::from_rgb8(255, 0, 0),
///     Point::new(0.0, 0.0),
///     Point::new(100.0, 20.0),
/// )
/// .endings(LineEndingStyle::OpenArrow, LineEndingStyle::ClosedArrow)
/// .into();
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct LineSpec {
    rect: Rect,
    color: Color,
    start: Point,
    end: Point,
    contents: Option<String>,
    border: Option<AnnotBorder>,
    endings: Option<(LineEndingStyle, LineEndingStyle)>,
    interior: Option<Color>,
}

impl LineSpec {
    /// A line from `start` to `end`, with `/Rect` as its bounding box.
    #[must_use]
    pub fn new(rect: Rect, color: Color, start: Point, end: Point) -> Self {
        Self {
            rect,
            color,
            start,
            end,
            contents: None,
            border: None,
            endings: None,
            interior: None,
        }
    }

    /// Sets `/BS` width and style. Ending decorations scale with the width.
    #[must_use]
    pub fn border(mut self, border: AnnotBorder) -> Self {
        self.border = Some(border);
        self
    }

    /// Sets the `/LE` ending styles. Omitted by default.
    #[must_use]
    pub fn endings(mut self, start: LineEndingStyle, end: LineEndingStyle) -> Self {
        self.endings = Some((start, end));
        self
    }

    /// Sets `/IC`, the fill for closed endings. Unfilled without it.
    #[must_use]
    pub fn interior(mut self, color: Color) -> Self {
        self.interior = Some(color);
        self
    }
}

spec_builder!(LineSpec, "A line annotation.");

impl From<LineSpec> for AnnotSpec {
    fn from(b: LineSpec) -> Self {
        let LineSpec {
            rect,
            color,
            start,
            end,
            contents,
            border,
            endings,
            interior,
        } = b;
        Self::Line {
            rect,
            color,
            start,
            end,
            contents,
            border: border.unwrap_or_default(),
            line_endings: endings,
            interior,
        }
    }
}

/// A link annotation (`/Subtype /Link`).
///
/// The action is required, so it is taken at construction; the constructors
/// mirror [`AnnotSpec`]'s link family.
///
/// ```
/// use kurbo::Rect;
/// use pdfrum_edit::{AnnotLinkHighlight, AnnotSpec, LinkSpec};
///
/// let spec: AnnotSpec = LinkSpec::uri(Rect::new(0.0, 0.0, 50.0, 10.0), "https://example.com")
///     .highlight(AnnotLinkHighlight::Outline)
///     .into();
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct LinkSpec {
    rect: Rect,
    action: AnnotLinkAction,
    contents: Option<String>,
    color: Option<Color>,
    border: Option<AnnotBorder>,
    highlight: Option<AnnotLinkHighlight>,
}

impl LinkSpec {
    /// A link at `rect` running `action`.
    #[must_use]
    pub fn new(rect: Rect, action: AnnotLinkAction) -> Self {
        Self {
            rect,
            action,
            contents: None,
            color: None,
            border: None,
            highlight: None,
        }
    }

    /// A link opening `uri`.
    #[must_use]
    pub fn uri(rect: Rect, uri: impl Into<String>) -> Self {
        Self::new(rect, AnnotLinkAction::Uri(uri.into()))
    }

    /// A link jumping to `page` in this document.
    #[must_use]
    pub fn goto(rect: Rect, page: ObjRef, view: AnnotGoToView) -> Self {
        Self::new(rect, AnnotLinkAction::GoTo { page, view })
    }

    /// A link to a named destination, registering `name` on write.
    #[must_use]
    pub fn named(rect: Rect, name: impl Into<String>, page: ObjRef, view: AnnotGoToView) -> Self {
        Self::new(
            rect,
            AnnotLinkAction::Named {
                name: name.into(),
                page,
                view,
            },
        )
    }

    /// A link to a destination the document already names.
    #[must_use]
    pub fn named_existing(rect: Rect, name: impl Into<String>) -> Self {
        Self::new(rect, AnnotLinkAction::NamedExisting { name: name.into() })
    }

    /// A link into another file, at a page number and view.
    #[must_use]
    pub fn goto_r(rect: Rect, file: impl Into<String>, page: i64, view: AnnotGoToView) -> Self {
        Self::new(
            rect,
            AnnotLinkAction::GoToR {
                file: file.into(),
                dest: crate::AnnotRemoteDest::Page { page, view },
                new_window: None,
            },
        )
    }

    /// A link into another file, at a destination that file names.
    #[must_use]
    pub fn goto_r_named(rect: Rect, file: impl Into<String>, name: impl Into<String>) -> Self {
        Self::new(
            rect,
            AnnotLinkAction::GoToR {
                file: file.into(),
                dest: crate::AnnotRemoteDest::Named(name.into()),
                new_window: None,
            },
        )
    }

    /// A link that launches a file or application.
    #[must_use]
    pub fn launch(rect: Rect, file: impl Into<String>) -> Self {
        Self::new(rect, AnnotLinkAction::Launch { file: file.into() })
    }

    /// Sets `/C`, the colour of the link's border chrome.
    #[must_use]
    pub fn color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Sets `/BS` width and style.
    #[must_use]
    pub fn border(mut self, border: AnnotBorder) -> Self {
        self.border = Some(border);
        self
    }

    /// Sets `/H`, the viewer's click feedback. Defaults to Invert.
    #[must_use]
    pub fn highlight(mut self, highlight: AnnotLinkHighlight) -> Self {
        self.highlight = Some(highlight);
        self
    }
}

spec_builder!(LinkSpec, "A link annotation.");

impl From<LinkSpec> for AnnotSpec {
    fn from(b: LinkSpec) -> Self {
        let LinkSpec {
            rect,
            action,
            contents,
            color,
            border,
            highlight,
        } = b;
        Self::Link {
            rect,
            action,
            contents,
            color,
            // A link's border defaults to a hairline, not the width-2 the
            // drawn shapes take: a link boxed as thickly as a square would be
            // wrong over text.
            border: border.unwrap_or_else(|| AnnotBorder::solid(1.0)),
            highlight: highlight.unwrap_or_default(),
        }
    }
}

/// A caret annotation (`/Subtype /Caret`).
#[derive(Debug, Clone, PartialEq)]
pub struct CaretSpec {
    rect: Rect,
    color: Color,
    contents: Option<String>,
}

impl CaretSpec {
    /// A caret over `rect`.
    #[must_use]
    pub fn new(rect: Rect, color: Color) -> Self {
        Self {
            rect,
            color,
            contents: None,
        }
    }
}

spec_builder!(CaretSpec, "A caret annotation.");

impl From<CaretSpec> for AnnotSpec {
    fn from(b: CaretSpec) -> Self {
        let CaretSpec {
            rect,
            color,
            contents,
        } = b;
        Self::Caret {
            rect,
            color,
            contents,
        }
    }
}

/// A free-text annotation (`/Subtype /FreeText`): text drawn on the page
/// rather than held behind an icon.
///
/// The one subtype whose text is *required* — a free-text annotation with no
/// `/Contents` has nothing to draw — so it is a constructor argument here
/// rather than the optional `contents` every other builder has, and this
/// builder declares the shared metadata setters itself instead of taking them
/// from `spec_builder!`.
#[derive(Debug, Clone, PartialEq)]
pub struct FreeTextSpec {
    rect: Rect,
    color: Color,
    contents: String,
    da: Option<String>,
    align: Option<pdfrum_doc::vt::Alignment>,
}

impl FreeTextSpec {
    /// Free text over `rect`, with [`DEFAULT_DA`](crate::DEFAULT_DA) as the
    /// default appearance.
    #[must_use]
    pub fn new(rect: Rect, color: Color, contents: impl Into<String>) -> Self {
        Self {
            rect,
            color,
            contents: contents.into(),
            da: None,
            align: None,
        }
    }

    /// Sets `/DA`, the default-appearance string naming the font and size the
    /// body is laid out with.
    ///
    /// Left unset this is [`DEFAULT_DA`](crate::DEFAULT_DA).
    #[must_use]
    pub fn da(mut self, da: impl Into<String>) -> Self {
        self.da = Some(da.into());
        self
    }

    /// Sets `/Q`, the text alignment.
    ///
    /// Unset leaves the key out, which a reader takes as flush left.
    #[must_use]
    pub fn align(mut self, align: pdfrum_doc::vt::Alignment) -> Self {
        self.align = Some(align);
        self
    }

    /// Sets `/T`, the author.
    #[must_use]
    pub fn author(self, author: impl Into<String>) -> AnnotWrite {
        AnnotSpec::from(self).with_author(author)
    }

    /// Sets `/NM`, the annotation's unique name.
    #[must_use]
    pub fn name(self, name: impl Into<String>) -> AnnotWrite {
        AnnotSpec::from(self).with_name(name)
    }

    /// Sets `/M`, the modification date.
    #[must_use]
    pub fn modified(self, modified: impl Into<String>) -> AnnotWrite {
        AnnotSpec::from(self).with_modified(modified)
    }

    /// Sets `/F`, the annotation flags.
    #[must_use]
    pub fn flags(self, flags: pdfrum_doc::AnnotFlags) -> AnnotWrite {
        AnnotSpec::from(self).with_flags(flags)
    }

    /// Sets every metadata field at once.
    #[must_use]
    pub fn meta(self, meta: AnnotMeta) -> AnnotWrite {
        AnnotSpec::from(self).with_meta(meta)
    }
}

impl From<FreeTextSpec> for AnnotSpec {
    fn from(b: FreeTextSpec) -> Self {
        let FreeTextSpec {
            rect,
            color,
            contents,
            da,
            align,
        } = b;
        Self::FreeText {
            rect,
            color,
            contents,
            da: da.unwrap_or_else(|| crate::DEFAULT_DA.to_owned()),
            align,
        }
    }
}

impl From<FreeTextSpec> for AnnotWrite {
    fn from(builder: FreeTextSpec) -> Self {
        AnnotSpec::from(builder).into()
    }
}