forked from typst/pdf-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructure.rs
More file actions
2407 lines (2142 loc) · 79.9 KB
/
structure.rs
File metadata and controls
2407 lines (2142 loc) · 79.9 KB
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::io::{Cursor, Write};
use std::num::NonZeroU16;
use super::*;
use crate::color::SeparationInfo;
use crate::object::TextStrLike;
/// Writer for a _document catalog dictionary_.
///
/// This struct is created by [`Pdf::catalog`].
pub struct Catalog<'a> {
dict: Dict<'a>,
}
writer!(Catalog: |obj| {
let mut dict = obj.dict();
dict.pair(Name(b"Type"), Name(b"Catalog"));
Self { dict }
});
impl Catalog<'_> {
/// Write the `/Pages` attribute pointing to the root page tree.
pub fn pages(&mut self, id: Ref) -> &mut Self {
self.pair(Name(b"Pages"), id);
self
}
/// Write the `/PageLayout` attribute to determine how the viewer will
/// display the document's pages.
pub fn page_layout(&mut self, layout: PageLayout) -> &mut Self {
self.pair(Name(b"PageLayout"), layout.to_name());
self
}
/// Start writing the `/PageLabels` number tree. PDF 1.3+.
pub fn page_labels(&mut self) -> NumberTree<'_, Ref> {
self.insert(Name(b"PageLabels")).start()
}
/// Write the `/PageMode` attribute to set which chrome elements the viewer
/// should show.
pub fn page_mode(&mut self, mode: PageMode) -> &mut Self {
self.pair(Name(b"PageMode"), mode.to_name());
self
}
/// Start writing the `/ViewerPreferences` dictionary. PDF 1.2+.
pub fn viewer_preferences(&mut self) -> ViewerPreferences<'_> {
self.insert(Name(b"ViewerPreferences")).start()
}
/// Start writing the `/Names` dictionary. PDF 1.2+.
pub fn names(&mut self) -> Names<'_> {
self.insert(Name(b"Names")).start()
}
/// Write the `/Dests` attribute pointing to a
/// [named destinations dictionary](Chunk::destinations). PDF 1.1+.
pub fn destinations(&mut self, id: Ref) -> &mut Self {
self.pair(Name(b"Dests"), id);
self
}
/// Write the `/Outlines` attribute pointing to the root
/// [outline dictionary](Outline).
pub fn outlines(&mut self, id: Ref) -> &mut Self {
self.pair(Name(b"Outlines"), id);
self
}
/// Start writing the `/StructTreeRoot` attribute to specify the root of the
/// document's structure tree. PDF 1.3+.
///
/// Must be present in some PDF/A profiles like PDF/A-2a.
pub fn struct_tree_root(&mut self) -> StructTreeRoot<'_> {
self.insert(Name(b"StructTreeRoot")).start()
}
/// Start writing the `/MarkInfo` dictionary to specify this document's
/// conformance with the tagged PDF specification. PDF 1.4+.
///
/// Must be present in some PDF/A profiles like PDF/A-2a.
pub fn mark_info(&mut self) -> MarkInfo<'_> {
self.insert(Name(b"MarkInfo")).start()
}
/// Write the `/Lang` attribute to specify the language of the document as a
/// RFC 3066 language tag. PDF 1.4+.
///
/// Required in some PDF/A profiles like PDF/A-2a.
pub fn lang(&mut self, lang: TextStr) -> &mut Self {
self.pair(Name(b"Lang"), lang);
self
}
/// Write the `/Version` attribute to override the PDF version stated in the
/// header. PDF 1.4+.
pub fn version(&mut self, major: u8, minor: u8) -> &mut Self {
self.pair(Name(b"Version"), Name(format!("{major}.{minor}").as_bytes()));
self
}
/// Start writing the `/AA` dictionary. This sets the additional actions for
/// the whole document. PDF 1.4+.
///
/// Note that this attribute is forbidden in PDF/A.
pub fn additional_actions(&mut self) -> AdditionalActions<'_> {
self.insert(Name(b"AA")).start()
}
/// Start writing the `/AcroForm` dictionary to specify the document wide
/// form. PDF 1.2+.
pub fn form(&mut self) -> Form<'_> {
self.insert(Name(b"AcroForm")).start()
}
/// Write the `/Metadata` attribute to specify the document's metadata. PDF
/// 1.4+.
///
/// The reference shall point to a [metadata stream](Metadata).
pub fn metadata(&mut self, id: Ref) -> &mut Self {
self.pair(Name(b"Metadata"), id);
self
}
/// Start writing the `/Extensions` dictionary to specify which PDF
/// extensions are in use in the document. PDF 1.5+.
///
/// The dictionary maps a vendor name to an extension dictionary. The Adobe
/// PDF extensions use the Name prefix `ADBE`.
pub fn extensions(&mut self) -> TypedDict<'_, DeveloperExtension<'_>> {
self.insert(Name(b"Extensions")).dict().typed()
}
/// Start writing the `/SeparationInfo` dictionary to specify which
/// separation colors are in use on the page and how it relates to other
/// pages in the document. PDF 1.3+.
pub fn separation_info(&mut self) -> SeparationInfo<'_> {
self.insert(Name(b"SeparationInfo")).start()
}
/// Start writing the `/OutputIntents` array to specify the output
/// destinations for the document. PDF 1.4+.
///
/// Each entry in the array is an [output intent
/// dictionary.](writers::OutputIntent)
///
/// Must be present in PDF/X documents, encouraged in PDF/A documents.
pub fn output_intents(&mut self) -> TypedArray<'_, OutputIntent<'_>> {
self.insert(Name(b"OutputIntents")).array().typed()
}
/// Start writing the `/AF` array to specify the associated files of the
/// document. PDF 2.0+ or PDF/A-3.
pub fn associated_files(&mut self) -> TypedArray<'_, FileSpec<'_>> {
self.insert(Name(b"AF")).array().typed()
}
}
deref!('a, Catalog<'a> => Dict<'a>, dict);
/// How the viewer should lay out the pages in the document.
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Hash)]
pub enum PageLayout {
/// Only a single page at a time.
#[default]
SinglePage,
/// A single, continuously scrolling column of pages.
OneColumn,
/// Two continuously scrolling columns of pages, laid out with odd-numbered
/// pages on the left.
TwoColumnLeft,
/// Two continuously scrolling columns of pages, laid out with odd-numbered
/// pages on the right (like in a left-bound book).
TwoColumnRight,
/// Only two pages are visible at a time, laid out with odd-numbered pages
/// on the left. PDF 1.5+.
TwoPageLeft,
/// Only two pages are visible at a time, laid out with odd-numbered pages
/// on the right (like in a left-bound book). PDF 1.5+.
TwoPageRight,
}
impl PageLayout {
pub(crate) fn to_name(self) -> Name<'static> {
match self {
Self::SinglePage => Name(b"SinglePage"),
Self::OneColumn => Name(b"OneColumn"),
Self::TwoColumnLeft => Name(b"TwoColumnLeft"),
Self::TwoColumnRight => Name(b"TwoColumnRight"),
Self::TwoPageLeft => Name(b"TwoPageLeft"),
Self::TwoPageRight => Name(b"TwoPageRight"),
}
}
}
/// Elements of the viewer chrome that should be visible when opening the
/// document.
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Hash)]
pub enum PageMode {
/// Neither the document outline panel nor a panel with page preview images
/// are visible.
#[default]
UseNone,
/// The document outline panel is visible.
UseOutlines,
/// A panel with page preview images is visible.
UseThumbs,
/// Show the document page in full screen mode, with no chrome.
FullScreen,
/// Show the optional content group panel. PDF 1.5+.
UseOC,
/// Show the attachments panel. PDF 1.6+.
UseAttachments,
}
impl PageMode {
pub(crate) fn to_name(self) -> Name<'static> {
match self {
Self::UseNone => Name(b"UseNone"),
Self::UseOutlines => Name(b"UseOutlines"),
Self::UseThumbs => Name(b"UseThumbs"),
Self::FullScreen => Name(b"FullScreen"),
Self::UseOC => Name(b"UseOC"),
Self::UseAttachments => Name(b"UseAttachments"),
}
}
}
/// Writer for a _developer extension dictionary_. PDF 1.7+.
///
/// An array of this struct is created by [`Catalog::extensions`].
pub struct DeveloperExtension<'a> {
dict: Dict<'a>,
}
writer!(DeveloperExtension: |obj| {
let mut dict = obj.dict();
dict.pair(Name(b"Type"), Name(b"DeveloperExtensions"));
Self { dict }
});
impl DeveloperExtension<'_> {
/// Write the `/BaseVersion` attribute to specify the version of PDF this
/// extension is based on. Required.
pub fn base_version(&mut self, major: u8, minor: u8) -> &mut Self {
self.pair(Name(b"BaseVersion"), Name(format!("{major}.{minor}").as_bytes()));
self
}
/// Write the `/ExtensionLevel` attribute to specify the version of the
/// extension. Required.
pub fn extension_level(&mut self, level: i32) -> &mut Self {
self.pair(Name(b"ExtensionLevel"), level);
self
}
}
deref!('a, DeveloperExtension<'a> => Dict<'a>, dict);
/// Writer for a _viewer preference dictionary_.
///
/// This struct is created by [`Catalog::viewer_preferences`].
pub struct ViewerPreferences<'a> {
dict: Dict<'a>,
}
writer!(ViewerPreferences: |obj| Self { dict: obj.dict() });
impl ViewerPreferences<'_> {
/// Write the `/HideToolbar` attribute to set whether the viewer should hide
/// its toolbars while the document is open.
pub fn hide_toolbar(&mut self, hide: bool) -> &mut Self {
self.pair(Name(b"HideToolbar"), hide);
self
}
/// Write the `/HideMenubar` attribute to set whether the viewer should hide
/// its menu bar while the document is open.
pub fn hide_menubar(&mut self, hide: bool) -> &mut Self {
self.pair(Name(b"HideMenubar"), hide);
self
}
/// Write the `/FitWindow` attribute to set whether the viewer should resize
/// its window to the size of the first page.
pub fn fit_window(&mut self, fit: bool) -> &mut Self {
self.pair(Name(b"FitWindow"), fit);
self
}
/// Write the `/CenterWindow` attribute to set whether the viewer should
/// center its window on the screen.
pub fn center_window(&mut self, center: bool) -> &mut Self {
self.pair(Name(b"CenterWindow"), center);
self
}
/// Write the `/DisplayDocTitle` attribute to set whether the viewer should
/// display the document's title from the `Title` entry as the window's title.
/// PDF 1.4+
pub fn display_doc_title(&mut self, display: bool) -> &mut Self {
self.pair(Name(b"DisplayDocTitle"), display);
self
}
/// Write the `/NonFullScreenPageMode` attribute to set which chrome
/// elements the viewer should show for a document which requests full
/// screen rendering in its catalog when it is not shown in full screen
/// mode.
///
/// Panics if `mode` is [`PageMode::FullScreen`].
pub fn non_full_screen_page_mode(&mut self, mode: PageMode) -> &mut Self {
assert!(mode != PageMode::FullScreen, "mode must not full screen");
self.pair(Name(b"NonFullScreenPageMode"), mode.to_name());
self
}
/// Write the `/Direction` attribute to aid the viewer in how to lay out the
/// pages visually. PDF 1.3+.
pub fn direction(&mut self, dir: Direction) -> &mut Self {
self.pair(Name(b"Direction"), dir.to_name());
self
}
}
deref!('a, ViewerPreferences<'a> => Dict<'a>, dict);
/// Writer for a _structure tree root dictionary_. PDF 1.3+
///
/// This struct is created by [`Catalog::struct_tree_root`].
pub struct StructTreeRoot<'a> {
dict: Dict<'a>,
}
writer!(StructTreeRoot: |obj| {
let mut dict = obj.dict();
dict.pair(Name(b"Type"), Name(b"StructTreeRoot"));
Self { dict }
});
impl StructTreeRoot<'_> {
/// Write the `/K` attribute to reference the immediate child of this
/// element.
pub fn child(&mut self, id: Ref) -> &mut Self {
self.dict.pair(Name(b"K"), id);
self
}
/// Start writing the `/K` attribute to reference the immediate children of
/// this element.
pub fn children(&mut self) -> TypedArray<'_, Ref> {
self.dict.insert(Name(b"K")).array().typed()
}
/// Start writing the `/IDTree` attribute to map element identifiers to
/// their corresponding structure element objects. Required if any elements
/// have element identifiers.
pub fn id_tree(&mut self) -> NameTree<'_, Ref> {
self.dict.insert(Name(b"IDTree")).start()
}
/// Start writing the `/ParentTree` attribute to maps structure elements to
/// the content items they belong to. Required if any structure elements
/// contain content items.
pub fn parent_tree(&mut self) -> NumberTree<'_, Ref> {
self.dict.insert(Name(b"ParentTree")).start()
}
/// Write the `/ParentTreeNextKey` attribute to specify the next available key
/// for the `/ParentTree` dictionary.
pub fn parent_tree_next_key(&mut self, key: i32) -> &mut Self {
self.dict.pair(Name(b"ParentTreeNextKey"), key);
self
}
/// Start writing the `/RoleMap` attribute to map structure element names to
/// their approximate equivalents from the standard set of types. PDF 1.4+.
///
/// For PDF 2.0 documents, note that this mapping maps to the PDF 1.7 roles.
/// For a namespace-aware role-mapping mechanism, see
/// [`Namespace::role_map_ns`].
pub fn role_map(&mut self) -> RoleMap<'_> {
self.dict.insert(Name(b"RoleMap")).start()
}
/// Start writing the `/ClassMap` attribute to map objects designating
/// attribute classes to their corresponding attribute objects or arrays
/// thereof.
pub fn class_map(&mut self) -> ClassMap<'_> {
self.dict.insert(Name(b"ClassMap")).start()
}
/// Start writing the `/Namespaces` attribute to specify the namespaces
/// occurring in the document. Required if namespaced structure types or
/// attributes, including the standard namespace for PDF 2.0, are used.
/// Create these dictionaries with [`Chunk::namespace`] PDF 2.0+.
pub fn namespaces(&mut self) -> TypedArray<'_, Ref> {
self.dict.insert(Name(b"Namespaces")).array().typed()
}
/// Start writing the `PronunciationLexicon` attribute to specify one or
/// multiple pronunciation lexicons for the document. PDF 2.0+.
///
/// The lexicons shall be XML files conforming to the Pronunciation Lexicon
/// Specification (PLS) Version 1.0. Each entry in the array is an indirect
/// reference to a [`FileSpec`] dictionary for a lexicon file.
pub fn pronunciation_lexicon(&mut self) -> TypedArray<'_, Ref> {
self.dict.insert(Name(b"PronunciationLexicon")).array().typed()
}
/// Start writing the `/AF` attribute to specify one or multiple files
/// associated with the entire structure tree. PDF 2.0+.
pub fn associated_files(&mut self) -> TypedArray<'_, FileSpec<'_>> {
self.dict.insert(Name(b"AF")).array().typed()
}
}
deref!('a, StructTreeRoot<'a> => Dict<'a>, dict);
/// Writer for a _structure element dictionary_. PDF 1.3+
pub struct StructElement<'a> {
dict: Dict<'a>,
}
writer!(StructElement: |obj| {
let mut dict = obj.dict();
dict.pair(Name(b"Type"), Name(b"StructElem"));
Self { dict }
});
impl StructElement<'_> {
/// Write the `/S` attribute to specify the role of this structure element
/// using elements from PDF 1.7 and below. Required if neither a PDF 2.0
/// structure type is defined using [`Self::kind_2`] nor a custom type is
/// specified using [`Self::custom_kind`].
pub fn kind(&mut self, role: StructRole) -> &mut Self {
self.dict.pair(Name(b"S"), role.to_name());
self
}
/// Write the `/S` attribute and the `/NS` attribute to specify the role of
/// this structure element in the PDF 2.0 namespace. Required if neither a
/// PDF 1.7 structure type is defined using [`Self::kind`] nor a custom type
/// is specified using [`Self::custom_kind`].
///
/// The `pdf_2_ns` parameter is an indirect reference to a PDF 2.0 namespace
/// dictionary. You can create this dictionary by using [`Chunk::namespace`]
/// and then calling [`Namespace::pdf_2_ns`] on the returned writer.
pub fn kind_2(&mut self, role: StructRole2, pdf_2_ns: Ref) -> &mut Self {
self.dict.pair(Name(b"S"), role.to_name(&mut [0; 6]));
self.namespace(pdf_2_ns)
}
/// Write the `/S` attribute to specify the role of this structure element
/// as a custom name. Required if no standard type is specified with
/// [`Self::kind`].
///
/// In some PDF/A profiles like PDF/A-2a, custom kinds must be mapped to
/// their closest standard type in the role map.
///
/// When using the namespaced model of PDF 2.0, using this may also require
/// setting a namespace using [`Self::namespace`].
pub fn custom_kind(&mut self, name: Name) -> &mut Self {
self.dict.pair(Name(b"S"), name);
self
}
/// Write the `/P` attribute to specify the parent of this structure
/// element. Required.
pub fn parent(&mut self, parent: Ref) -> &mut Self {
self.dict.pair(Name(b"P"), parent);
self
}
/// Write the `/ID` attribute to specify the element identifier of this
/// structure element.
pub fn id(&mut self, id: Str) -> &mut Self {
self.dict.pair(Name(b"ID"), id);
self
}
/// Write the `/Ref` attribute to specify to which structure element this
/// element refers. Used e.g. for footnotes. PDF 2.0+
///
/// The parameter `refs` shall be indirect object references to other
/// structure elements.
pub fn refs(&mut self, refs: impl IntoIterator<Item = Ref>) -> &mut Self {
self.dict.insert(Name(b"Ref")).array().typed().items(refs);
self
}
/// Write the `/Pg` attribute to specify the page some or all of this
/// structure element is located on.
pub fn page(&mut self, page: Ref) -> &mut Self {
self.dict.pair(Name(b"Pg"), page);
self
}
/// Write the `/K` attribute to reference the immediate child of this
/// element.
pub fn child(&mut self, id: Ref) -> &mut Self {
self.dict.pair(Name(b"K"), id);
self
}
/// Start writing the `/K` attribute to reference the immediate marked
/// content child of this element.
pub fn marked_content_child(&mut self) -> MarkedRef<'_> {
self.dict.insert(Name(b"K")).start()
}
/// Start writing the `/K` attribute to reference the immediate object child
/// of this element.
pub fn object_child(&mut self) -> ObjectRef<'_> {
self.dict.insert(Name(b"K")).start()
}
/// Start writing the `/K` attribute to specify the children elements and
/// associated marked content sequences.
pub fn children(&mut self) -> StructChildren<'_> {
self.dict.insert(Name(b"K")).start()
}
/// Start writing the `/A` attribute to specify the attributes of this
/// structure element.
pub fn attributes(&mut self) -> TypedArray<'_, Attributes<'_>> {
self.dict.insert(Name(b"A")).array().typed()
}
/// Start writing the `/C` attribute to associate the structure element with
/// an attribute class.
pub fn attribute_class(&mut self) -> TypedArray<'_, Name<'_>> {
self.dict.insert(Name(b"C")).array().typed()
}
/// Write the `/R` attribute to specify the revision number, starting at 0.
pub fn revision(&mut self, revision: i32) -> &mut Self {
self.dict.pair(Name(b"R"), revision);
self
}
/// Write the `/T` attribute to set a title.
pub fn title(&mut self, title: impl TextStrLike) -> &mut Self {
self.dict.pair(Name(b"T"), title);
self
}
/// Write the `/Lang` attribute to set a language. PDF 1.4+
pub fn lang(&mut self, lang: TextStr) -> &mut Self {
self.dict.pair(Name(b"Lang"), lang);
self
}
/// Write the `/Alt` attribute to provide a description of the structure
/// element.
pub fn alt(&mut self, alt: impl TextStrLike) -> &mut Self {
self.dict.pair(Name(b"Alt"), alt);
self
}
/// Write the `/E` attribute to set the expanded form of the abbreviation
/// in this structure element. PDF 1.5+
pub fn expanded(&mut self, expanded: impl TextStrLike) -> &mut Self {
self.dict.pair(Name(b"E"), expanded);
self
}
/// Write the `/ActualText` attribute to set the exact text replacement. PDF
/// 1.4+
pub fn actual_text(&mut self, actual_text: impl TextStrLike) -> &mut Self {
self.dict.pair(Name(b"ActualText"), actual_text);
self
}
/// Start writing the `/AF` array to specify the associated files of the
/// element. PDF 2.0+ or PDF/A-3.
pub fn associated_files(&mut self) -> TypedArray<'_, FileSpec<'_>> {
self.insert(Name(b"AF")).array().typed()
}
/// Write the `/NS` attribute to indirectly reference a namespace dictionary
/// for this structure element type. PDF 2.0+
pub fn namespace(&mut self, ns: Ref) -> &mut Self {
self.dict.pair(Name(b"NS"), ns);
self
}
/// Write the `/PhoneticAlphabet` attribute to specify the phonetic alphabet
/// used in the [StructElement::phoneme] attribute. PDF 2.0+
pub fn phonetic_alphabet(&mut self, alphabet: PhoneticAlphabet) -> &mut Self {
self.dict.pair(Name(b"PhoneticAlphabet"), alphabet.to_name());
self
}
/// Write the `/Phoneme` attribute to specify the phonetic pronunciation of
/// the text in the structure element. PDF 2.0+
pub fn phoneme(&mut self, phoneme: TextStr) -> &mut Self {
self.dict.pair(Name(b"Phoneme"), phoneme);
self
}
}
deref!('a, StructElement<'a> => Dict<'a>, dict);
/// Writer for a _structure element children array_. PDF 1.3+
///
/// This struct is created by [`StructElement::children`].
pub struct StructChildren<'a> {
arr: Array<'a>,
}
writer!(StructChildren: |obj| Self { arr: obj.array() });
impl StructChildren<'_> {
/// Write a structure element child as an indirect object.
pub fn struct_element(&mut self, id: Ref) -> &mut Self {
self.arr.item(id);
self
}
/// Write an integer marked content identifier.
pub fn marked_content_id(&mut self, id: i32) -> &mut Self {
self.arr.item(id);
self
}
/// Start writing a marked content reference dictionary.
pub fn marked_content_ref(&mut self) -> MarkedRef<'_> {
self.arr.push().start()
}
/// Start writing an object reference dictionary.
pub fn object_ref(&mut self) -> ObjectRef<'_> {
self.arr.push().start()
}
}
deref!('a, StructChildren<'a> => Array<'a>, arr);
/// Writer for a _marked content reference dictionary_. PDF 1.3+
///
/// This struct is created by [`StructChildren::marked_content_ref`] and
/// [`StructElement::marked_content_child`].
pub struct MarkedRef<'a> {
dict: Dict<'a>,
}
writer!(MarkedRef: |obj| {
let mut dict = obj.dict();
dict.pair(Name(b"Type"), Name(b"MCR"));
Self { dict }
});
impl MarkedRef<'_> {
/// Write the `/Pg` attribute to specify the page the referenced marked
/// content sequence is located on.
pub fn page(&mut self, page: Ref) -> &mut Self {
self.dict.pair(Name(b"Pg"), page);
self
}
/// Write the `/Stm` attribute to specify the content stream containing this
/// marked content sequence if it was not on a page. If this content is
/// missing, writing the page attribute here or in the associated structure
/// element is required.
pub fn stream(&mut self, stream: Ref) -> &mut Self {
self.dict.pair(Name(b"Stm"), stream);
self
}
/// Write the `/StmOwn` attribute to specify which object owns the content
/// stream specified by the `/Stm` attribute.
pub fn stream_owner(&mut self, owner: Ref) -> &mut Self {
self.dict.pair(Name(b"StmOwn"), owner);
self
}
/// Write the `/MCID` attribute to specify the integer marked content
/// identifier. Required.
pub fn marked_content_id(&mut self, id: i32) -> &mut Self {
self.dict.pair(Name(b"MCID"), id);
self
}
}
deref!('a, MarkedRef<'a> => Dict<'a>, dict);
/// Writer for an _object reference dictionary_. PDF 1.3+
///
/// This struct is created by [`StructChildren::object_ref`].
pub struct ObjectRef<'a> {
dict: Dict<'a>,
}
writer!(ObjectRef: |obj| {
let mut dict = obj.dict();
dict.pair(Name(b"Type"), Name(b"OBJR"));
Self { dict }
});
impl ObjectRef<'_> {
/// Write the `/Pg` attribute to specify the page some or all of this
/// structure element is located on.
pub fn page(&mut self, page: Ref) -> &mut Self {
self.dict.pair(Name(b"Pg"), page);
self
}
/// Write the `/Obj` attribute to specify the object to be referenced. Required.
pub fn object(&mut self, obj: Ref) -> &mut Self {
self.dict.pair(Name(b"Obj"), obj);
self
}
}
deref!('a, ObjectRef<'a> => Dict<'a>, dict);
/// Writer for a _role map dictionary_. PDF 1.4+
///
/// This struct is created by [`StructTreeRoot::role_map`].
///
/// For PDF 2.0 documents, note that this mapping maps to the PDF 1.7 roles. For
/// a namespace-aware role-mapping mechanism, see [`Namespace::role_map_ns`].
pub struct RoleMap<'a> {
dict: TypedDict<'a, Name<'a>>,
}
writer!(RoleMap: |obj| Self { dict: obj.dict().typed() });
impl RoleMap<'_> {
/// Write an entry mapping a custom name to a pre-defined role.
pub fn insert(&mut self, name: Name, role: StructRole) -> &mut Self {
self.dict.pair(name, role.to_name());
self
}
}
deref!('a, RoleMap<'a> => TypedDict<'a, Name<'a>>, dict);
/// Writer for a _class map dictionary_. PDF 1.4+
///
/// This struct is created by [`StructTreeRoot::class_map`].
pub struct ClassMap<'a> {
dict: Dict<'a>,
}
writer!(ClassMap: |obj| Self { dict: obj.dict() });
impl ClassMap<'_> {
/// Start writing an attributes dictionary for a class name.
pub fn single(&mut self, name: Name) -> Attributes<'_> {
self.dict.insert(name).start()
}
/// Start writing an array of attribute dictionaries for a class name.
pub fn multiple(&mut self, name: Name) -> TypedArray<'_, Attributes<'_>> {
self.dict.insert(name).array().typed()
}
}
deref!('a, ClassMap<'a> => Dict<'a>, dict);
/// Role the structure element fulfills in the document for PDF 1.7 and below.
///
/// These are the predefined standard roles in PDF 1.7 and below, matching the
/// `https://www.iso.org/pdf/ssn` namespace. The writer may write their own
/// roles and then provide a mapping with [`StructTreeRoot::role_map`], or, if
/// writing PDF 2.0, with [`Namespace::role_map_ns`]. PDF 1.4+.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum StructRole {
/// The whole document.
Document,
/// A part of a document that may contain multiple articles or sections.
Part,
/// An article with largely self-contained content.
Art,
/// Section of a larger document.
Sect,
/// Generic subdivision.
Div,
/// A paragraph-level quote.
BlockQuote,
/// An image or figure caption.
Caption,
/// Table of contents.
TOC,
/// Item in the table of contents.
TOCI,
/// Index of the key terms in the document.
Index,
/// Element only present for grouping purposes that shall not be exported.
NonStruct,
/// Element present only for use by the writer and associated products.
Private,
/// A paragraph
P,
/// A strongly structured heading.
StructuredHeading,
/// First-level heading.
H1,
/// Second-level heading.
H2,
/// Third-level heading.
H3,
/// Fourth-level heading.
H4,
/// Fifth-level heading.
H5,
/// Sixth-level heading.
H6,
/// A list.
L,
/// A list item.
LI,
/// Label for a list item.
Lbl,
/// Description of the list item.
LBody,
/// A table.
Table,
/// A table row.
TR,
/// A table header cell.
TH,
/// A table data cell.
TD,
/// A table header row group.
THead,
/// A table data row group.
TBody,
/// A table footer row group.
TFoot,
/// A generic inline element.
Span,
/// An inline quotation.
Quote,
/// A foot- or endnote.
Note,
/// A reference to elsewhere in the document.
Reference,
/// A reference to an external document.
BibEntry,
/// Computer code.
Code,
/// A link.
Link,
/// An association between an annotation and the content it belongs to. PDF
/// 1.5+
Annot,
/// Ruby annotation for CJK text. PDF 1.5+
Ruby,
/// Warichu annotation for CJK text. PDF 1.5+
Warichu,
/// Base text of a Ruby annotation. PDF 1.5+
RB,
/// Annotation text of a Ruby annotation. PDF 1.5+
RT,
/// Punctuation of a Ruby annotation. PDF 1.5+
RP,
/// Text of a Warichu annotation. PDF 1.5+
WT,
/// Punctuation of a Warichu annotation. PDF 1.5+
WP,
/// Item of graphical content.
Figure,
/// Mathematical formula.
Formula,
/// Form widget.
Form,
}
impl StructRole {
/// Convert the role into its [`Name`] serialization.
pub fn to_name(self) -> Name<'static> {
match self {
Self::Document => Name(b"Document"),
Self::Part => Name(b"Part"),
Self::Art => Name(b"Art"),
Self::Sect => Name(b"Sect"),
Self::Div => Name(b"Div"),
Self::BlockQuote => Name(b"BlockQuote"),
Self::Caption => Name(b"Caption"),
Self::TOC => Name(b"TOC"),
Self::TOCI => Name(b"TOCI"),
Self::Index => Name(b"Index"),
Self::NonStruct => Name(b"NonStruct"),
Self::Private => Name(b"Private"),
Self::P => Name(b"P"),
Self::StructuredHeading => Name(b"H"),
Self::H1 => Name(b"H1"),
Self::H2 => Name(b"H2"),
Self::H3 => Name(b"H3"),
Self::H4 => Name(b"H4"),
Self::H5 => Name(b"H5"),
Self::H6 => Name(b"H6"),
Self::L => Name(b"L"),
Self::LI => Name(b"LI"),
Self::Lbl => Name(b"Lbl"),
Self::LBody => Name(b"LBody"),
Self::Table => Name(b"Table"),
Self::TR => Name(b"TR"),
Self::TH => Name(b"TH"),
Self::TD => Name(b"TD"),
Self::THead => Name(b"THead"),
Self::TBody => Name(b"TBody"),
Self::TFoot => Name(b"TFoot"),
Self::Span => Name(b"Span"),
Self::Quote => Name(b"Quote"),
Self::Note => Name(b"Note"),
Self::Reference => Name(b"Reference"),
Self::BibEntry => Name(b"BibEntry"),
Self::Code => Name(b"Code"),
Self::Link => Name(b"Link"),
Self::Annot => Name(b"Annot"),
Self::Ruby => Name(b"Ruby"),
Self::Warichu => Name(b"Warichu"),
Self::RB => Name(b"RB"),
Self::RT => Name(b"RT"),
Self::RP => Name(b"RP"),
Self::WT => Name(b"WT"),
Self::WP => Name(b"WP"),
Self::Figure => Name(b"Figure"),
Self::Formula => Name(b"Formula"),
Self::Form => Name(b"Form"),
}
}
/// Return the corresponding PDF 2.0 [`StructRole2`] for this role or
/// `None`.
pub fn into_pdf_2_0(self) -> Option<StructRole2> {
match self {
Self::Document => Some(StructRole2::Document),
Self::Part => Some(StructRole2::Part),
Self::Art => Some(StructRole2::Article),
Self::Sect => Some(StructRole2::Sect),
Self::Div => Some(StructRole2::Div),
Self::BlockQuote => None,
Self::Caption => Some(StructRole2::Caption),
Self::TOC => None,
Self::TOCI => None,
Self::Index => None,
Self::NonStruct => Some(StructRole2::NonStruct),
Self::Private => None,
Self::P => Some(StructRole2::P),
Self::StructuredHeading => Some(StructRole2::StructuredHeading),
Self::H1 => Some(StructRole2::Heading(NonZeroU16::new(1).unwrap())),
Self::H2 => Some(StructRole2::Heading(NonZeroU16::new(2).unwrap())),
Self::H3 => Some(StructRole2::Heading(NonZeroU16::new(3).unwrap())),
Self::H4 => Some(StructRole2::Heading(NonZeroU16::new(4).unwrap())),
Self::H5 => Some(StructRole2::Heading(NonZeroU16::new(5).unwrap())),
Self::H6 => Some(StructRole2::Heading(NonZeroU16::new(6).unwrap())),
Self::L => Some(StructRole2::L),
Self::LI => Some(StructRole2::LI),
Self::Lbl => Some(StructRole2::Lbl),
Self::LBody => Some(StructRole2::LBody),
Self::Table => Some(StructRole2::Table),
Self::TR => Some(StructRole2::TR),
Self::TH => Some(StructRole2::TH),
Self::TD => Some(StructRole2::TD),
Self::THead => Some(StructRole2::THead),
Self::TBody => Some(StructRole2::TBody),
Self::TFoot => Some(StructRole2::TFoot),
Self::Span => Some(StructRole2::Span),
Self::Quote => Some(StructRole2::Em),
Self::Note => Some(StructRole2::FENote),
Self::Reference => Some(StructRole2::Link),
Self::BibEntry => None,
Self::Code => Some(StructRole2::Strong),
Self::Link => Some(StructRole2::Link),
Self::Annot => Some(StructRole2::Annot),
Self::Ruby => Some(StructRole2::Ruby),
Self::Warichu => Some(StructRole2::Warichu),
Self::RB => Some(StructRole2::RB),
Self::RT => Some(StructRole2::RT),
Self::RP => Some(StructRole2::WP),
Self::WT => Some(StructRole2::WT),
Self::WP => Some(StructRole2::WP),
Self::Figure => Some(StructRole2::Figure),
Self::Formula => Some(StructRole2::Formula),
Self::Form => Some(StructRole2::Form),
}
}
/// Return the type of the structure element.
pub fn role_type(self) -> StructRoleType {
match self {
Self::Document
| Self::Part
| Self::Art
| Self::Sect
| Self::Div
| Self::BlockQuote
| Self::Caption
| Self::TOC
| Self::TOCI
| Self::Index
| Self::NonStruct
| Self::Private => StructRoleType::Grouping,
Self::P