Skip to main content

fop_render/pdf/document/
types.rs

1//! Core PDF type definitions
2//!
3//! Defines the fundamental data structures used throughout the PDF document module.
4
5use std::collections::HashMap;
6
7use fop_types::{Gradient, Length};
8
9use crate::pdf::compliance::PdfCompliance;
10use crate::pdf::font::FontManager;
11use crate::pdf::image::ImageXObject;
12use crate::pdf::security::EncryptionDict;
13
14/// PDF gradient shading object
15#[derive(Clone)]
16pub struct PdfGradient {
17    /// Gradient definition
18    pub gradient: Gradient,
19    /// PDF object ID (assigned during document generation)
20    pub object_id: usize,
21}
22
23/// PDF Extended Graphics State (ExtGState) for transparency
24#[derive(Clone, Debug)]
25pub struct PdfExtGState {
26    /// Fill opacity (0.0 = transparent, 1.0 = opaque)
27    pub fill_opacity: f64,
28    /// Stroke opacity (0.0 = transparent, 1.0 = opaque)
29    pub stroke_opacity: f64,
30    /// PDF object ID (assigned during document generation)
31    pub object_id: usize,
32}
33
34/// PDF outline (bookmarks) structure
35#[derive(Debug, Clone)]
36pub struct PdfOutline {
37    /// Top-level outline items
38    pub items: Vec<PdfOutlineItem>,
39}
40
41/// PDF outline item (bookmark)
42#[derive(Debug, Clone)]
43pub struct PdfOutlineItem {
44    /// Bookmark title
45    pub title: String,
46
47    /// Internal destination (page index for now)
48    pub page_index: Option<usize>,
49
50    /// External destination URL
51    pub external_destination: Option<String>,
52
53    /// Nested child bookmarks
54    pub children: Vec<PdfOutlineItem>,
55}
56
57/// PDF object (indirect object)
58#[derive(Debug, Clone)]
59pub struct PdfObject {
60    /// Object number
61    pub id: u32,
62
63    /// Generation number (usually 0)
64    pub generation: u16,
65
66    /// Object content
67    pub content: PdfValue,
68}
69
70/// PDF value types
71#[derive(Debug, Clone)]
72pub enum PdfValue {
73    Null,
74    Boolean(bool),
75    Integer(i32),
76    Real(f64),
77    String(String),
78    Name(String),
79    Array(Vec<PdfValue>),
80    Dictionary(HashMap<String, PdfValue>),
81    Stream(Vec<u8>),
82}
83
84/// Link annotation (hyperlink)
85#[derive(Debug, Clone)]
86pub struct LinkAnnotation {
87    /// Rectangle of the clickable area (in PDF coordinates: bottom-left origin)
88    pub rect: [f64; 4], // [x, y, width, height]
89
90    /// Link destination
91    pub destination: LinkDestination,
92}
93
94/// Link destination type
95#[derive(Debug, Clone)]
96pub enum LinkDestination {
97    /// External URL
98    External(String),
99
100    /// Internal destination (element ID)
101    Internal(String),
102}
103
104/// PDF page
105pub struct PdfPage {
106    /// Page width
107    pub width: Length,
108
109    /// Page height
110    pub height: Length,
111
112    /// Page content stream
113    pub content: Vec<u8>,
114
115    /// Link annotations on this page
116    pub link_annotations: Vec<LinkAnnotation>,
117}
118
119/// PDF document information
120#[derive(Debug, Clone, Default)]
121pub struct PdfInfo {
122    /// Document title
123    pub title: Option<String>,
124
125    /// Document author
126    pub author: Option<String>,
127
128    /// Document subject
129    pub subject: Option<String>,
130
131    /// Creation date
132    pub creation_date: Option<String>,
133
134    /// Document language (BCP 47 language tag, e.g. "en", "ja")
135    /// Written as /Lang in the PDF catalog
136    pub lang: Option<String>,
137}
138
139/// PDF document structure
140pub struct PdfDocument {
141    /// PDF version (e.g., "1.4")
142    pub version: String,
143
144    /// Objects in the PDF
145    pub objects: Vec<PdfObject>,
146
147    /// Page tree
148    pub pages: Vec<PdfPage>,
149
150    /// Document info
151    pub info: PdfInfo,
152
153    /// Image XObjects (shared resources)
154    pub image_xobjects: Vec<ImageXObject>,
155
156    /// Gradient shading patterns (shared resources)
157    pub gradients: Vec<PdfGradient>,
158
159    /// Extended Graphics States for transparency
160    pub ext_g_states: Vec<PdfExtGState>,
161
162    /// Document outline (bookmarks)
163    pub outline: Option<PdfOutline>,
164
165    /// Font manager for embedded fonts
166    pub font_manager: FontManager,
167
168    /// Encryption settings (if set, all streams/strings will be encrypted)
169    pub encryption: Option<EncryptionDict>,
170
171    /// File ID for the /ID entry in the trailer (required for encryption)
172    pub file_id: Option<Vec<u8>>,
173
174    /// PDF compliance mode (Standard, PDF/A-1b, PDF/UA-1, or both)
175    pub compliance: PdfCompliance,
176}