Skip to main content

screenplay_doc_parser_rs/
pdf_document.rs

1// TODO: impl defaults for standard US-LETTER indentations
2#[derive(Default)]
3pub struct ElementIndentationsInches {
4    pub pagewidth: f64,
5    pub pageheight: f64,
6    pub left: f64,
7    pub right: f64,
8    pub top: f64,
9    pub bottom: f64,
10
11    pub action: f64,
12    pub character: f64,
13    pub dialogue: f64,
14    pub parenthetical: f64,
15}
16impl ElementIndentationsInches {
17    pub fn us_letter_default() -> Self {
18        ElementIndentationsInches {
19            top: 10.0,
20            bottom: 1.0,
21            left: 1.5,
22            right: 7.25, // Final Draft default???
23            pageheight: 11.0,
24            pagewidth: 8.5,
25            action: 1.5,
26            character: 3.7,
27            dialogue:2.5,
28            parenthetical: 3.1,
29        }
30    }
31
32    pub fn top(mut self, new_top: f64) -> Self {
33        self.top = new_top;
34        self
35    }
36
37    pub fn bottom(mut self, new_bottom: f64) -> Self {
38        self.bottom = new_bottom;
39        self
40    }
41    pub fn left(mut self, new_left: f64) -> Self {
42        self.left = new_left;
43        self
44    }
45    pub fn right(mut self, new_right: f64) -> Self {
46        self.right = new_right;
47        self
48    }
49
50    //
51
52    pub fn pageheight(mut self, new_pageheight: f64) -> Self {
53        self.pageheight = new_pageheight;
54        self
55    }
56    pub fn pagewidth(mut self, new_pagewidth: f64) -> Self {
57        self.pagewidth = new_pagewidth;
58        self
59    }
60
61    pub fn action(mut self, new_action: f64) -> Self {
62        self.action = new_action;
63        self
64    }
65    pub fn character(mut self, new_character: f64) -> Self {
66        self.character = new_character;
67        self
68    }
69    pub fn dialogue(mut self, new_dialogue: f64) -> Self {
70        self.dialogue = new_dialogue;
71        self
72    }
73    pub fn parenthetical(mut self, new_parenthetical: f64) -> Self {
74        self.parenthetical = new_parenthetical;
75        self
76    }
77
78    pub fn from_points(indentations: &ElementIndentationsPoints, resolution: &f64) -> ElementIndentationsInches {
79        ElementIndentationsInches {
80            top: indentations.top / resolution,
81            bottom: indentations.bottom / resolution,
82            pagewidth: indentations.pagewidth / resolution,
83            pageheight: indentations.pageheight / resolution,
84            left: indentations.left / resolution,
85            right: indentations.right / resolution,
86            action: indentations.action / resolution,
87            character: indentations.character / resolution,
88            dialogue: indentations.dialogue / resolution,
89            parenthetical: indentations.parenthetical / resolution
90        }
91    }
92}
93
94#[derive(Default)]
95pub struct ElementIndentationsPoints {
96    pub pagewidth: f64,
97    pub pageheight: f64,
98    pub left: f64,
99    pub right: f64,
100    pub top: f64,
101    pub bottom: f64,
102
103    pub action: f64,
104    pub character: f64,
105    pub dialogue: f64,
106    pub parenthetical: f64,
107}
108impl ElementIndentationsPoints {
109
110    /// Gets a default struct of Indentations in Points for US-Letter formatted screenplays.
111    /// 
112    /// Takes an optional resolution. `None` will use a default of 72.0 point-per-inch resolution.
113    pub fn us_letter_default(resolution: &Option<f64>) -> Self {
114        let mut current_resolution: f64 = 72.0;
115        if let Some(r) = resolution {
116            current_resolution = r.clone()
117        }
118
119        let indentations = ElementIndentationsInches::us_letter_default();
120        
121        ElementIndentationsPoints {
122            top: indentations.top * current_resolution,
123            bottom: indentations.bottom * current_resolution,
124            pagewidth: indentations.pagewidth * current_resolution,
125            pageheight: indentations.pageheight * current_resolution,
126            left: indentations.left * current_resolution,
127            right: indentations.right *current_resolution,
128            action: indentations.action * current_resolution,
129            character: indentations.character * current_resolution,
130            dialogue: indentations.dialogue *current_resolution,
131            parenthetical: indentations.parenthetical * current_resolution
132        }
133    }
134
135    pub fn from_inches(indentations: &ElementIndentationsInches, resolution: &Option<f64>) -> ElementIndentationsPoints {
136        let mut current_resolution: f64 = 72.0;
137            if let Some(r) = resolution {
138                current_resolution = r.clone()
139            }
140            
141            ElementIndentationsPoints {
142                top: indentations.top * current_resolution,
143                bottom: indentations.bottom * current_resolution,
144                pagewidth: indentations.pagewidth * current_resolution,
145                pageheight: indentations.pageheight * current_resolution,
146                left: indentations.left * current_resolution,
147                right: indentations.right *current_resolution,
148                action: indentations.action * current_resolution,
149                character: indentations.character * current_resolution,
150                dialogue: indentations.dialogue *current_resolution,
151                parenthetical: indentations.parenthetical * current_resolution
152            }
153    }
154}
155
156
157
158
159
160#[derive(Default, Clone, Copy, Debug, PartialEq)]
161pub struct TextPosition {
162    pub x: f64,
163    pub y: f64,
164}
165#[derive(Default, Debug)]
166pub struct PageSize {
167    pub width: f64,
168    pub height: f64,
169}
170#[derive(Default, Debug)]
171pub struct Word {
172    pub text: String,
173    pub bbox_width: f64,
174    pub bbox_height: f64,
175    pub position:TextPosition,
176    pub font_name: Option<String>,
177    pub font_size: f64,
178    pub font_character_width: f64,
179}
180#[derive(Default, Debug)]
181pub struct Line {
182    pub words:Vec<Word>
183}
184#[derive(Default, Debug)]
185pub struct Page {
186    pub lines: Vec<Line>,
187    pub page_size: PageSize
188}
189#[derive(Default)]
190pub struct PDFDocument {
191    pub pages: Vec<Page>,
192    pub pdf_creator: Option<String>,
193}