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
//! This module allows to create beautiful documents.
use std::fmt;
use std::fs::File;
use std::io::BufWriter;
use std::path::Path;
use hyphenation::load::Load;
use hyphenation::{Language, Standard};
use printpdf::{PdfDocument, PdfDocumentReference, PdfLayerReference, PdfPageReference, Pt};
use crate::font::{Font, FontConfig};
use crate::parser::ast::Ast;
use crate::typography::justification::{Justifier, LatexJustifier};
use crate::typography::paragraphs::itemize_ast;
/// The struct that manages the counters for the document.
#[derive(Clone)]
pub struct Counters {
/// The counters.
pub counters: Vec<usize>,
}
impl Counters {
/// Creates a new empty counters.
pub fn new() -> Counters {
Counters { counters: vec![0] }
}
/// Increases the corresponding counter and returns it if it is correct.
///
/// The counters of the subsections will be reinitialized.
///
/// # Example
///
/// ```
/// # use spandex::document::Counters;
/// let mut counters = Counters::new();
/// counters.increment(0);
/// assert_eq!(counters.counter(0), 1);
/// assert_eq!(counters.counter(1), 0);
/// assert_eq!(counters.counter(2), 0);
/// counters.increment(1);
/// assert_eq!(counters.counter(1), 1);
/// counters.increment(1);
/// assert_eq!(counters.counter(1), 2);
/// counters.increment(0);
/// assert_eq!(counters.counter(0), 2);
/// assert_eq!(counters.counter(1), 0);
/// println!("{}", counters);
/// ```
pub fn increment(&mut self, counter_id: usize) -> usize {
self.counters.resize(counter_id + 1, 0);
self.counters[counter_id] += 1;
self.counters[counter_id]
}
/// Returns a specific value of a counter.
pub fn counter(&self, counter_id: usize) -> usize {
match self.counters.get(counter_id) {
Some(i) => *i,
None => 0,
}
}
}
impl fmt::Display for Counters {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"{}",
self.counters
.iter()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join(".")
)
}
}
/// The window that is the part of the page on which we're allowed to write.
#[derive(Copy, Clone)]
pub struct Window {
/// The x coordinate of the window, in pt.
pub x: Pt,
/// The y coordinate of the window, in pt.
pub y: Pt,
/// The width of the window, in pt.
pub width: Pt,
/// The height of the window, in pt.
pub height: Pt,
}
/// This struct contains the pdf document.
pub struct Document {
/// The inner document from printpdf.
document: PdfDocumentReference,
/// The current page.
page: PdfPageReference,
/// The current layer.
layer: PdfLayerReference,
/// The window on which we're allowed to write on the page.
window: Window,
/// The cursor, the position where we supposed to write next.
cursor: (Pt, Pt),
/// The current page size, in pt.
page_size: (Pt, Pt),
/// The counters of the document
counters: Counters,
}
impl Document {
/// Creates a new pdf document from its name and its size in pt.
pub fn new<T: Into<Pt>, U: Into<Pt>>(
name: &str,
width: T,
height: U,
window: Window,
) -> Document {
let width: Pt = width.into();
let height: Pt = height.into();
let (document, page, layer) = PdfDocument::new(name, width.into(), height.into(), "");
let page = document.get_page(page);
let layer = page.get_layer(layer);
Document {
document,
page,
layer,
window,
cursor: (window.x, window.height + window.y),
page_size: (width.into(), height.into()),
counters: Counters::new(),
}
}
/// Returns a reference to the inner pdf document.
pub fn inner(&self) -> &PdfDocumentReference {
&self.document
}
/// Returns a mutable reference to the inner pdf document.
pub fn inner_mut(&mut self) -> &mut PdfDocumentReference {
&mut self.document
}
/// Renders an AST to the document.
pub fn render(&mut self, ast: &Ast, font_config: &FontConfig, size: Pt) {
let en = Standard::from_embedded(Language::EnglishUS).unwrap();
match ast {
Ast::Group(children) => {
for child in children {
self.render(child, font_config, size);
}
}
Ast::Title { level, content } => {
self.counters.increment(*level as usize);
match &**content {
Ast::Group(children) => {
let mut new_children = vec![Ast::Text(format!("{} ", self.counters))];
new_children.extend_from_slice(children);
let new_ast = Ast::Title {
level: *level,
content: Box::new(Ast::Group(new_children)),
};
self.write_paragraph::<LatexJustifier>(&new_ast, font_config, size, &en);
}
_ => self.write_paragraph::<LatexJustifier>(ast, font_config, size, &en),
}
self.new_line(size);
}
Ast::Paragraph(_) => {
self.write_paragraph::<LatexJustifier>(ast, font_config, size, &en);
self.new_line(size);
self.new_line(size);
}
_ => (),
}
}
/// Writes content on the document.
pub fn write_content(&mut self, content: &str, font_config: &FontConfig, size: Pt) {
let en = Standard::from_embedded(Language::EnglishUS).unwrap();
for paragraph in content.split("\n") {
let ast = Ast::Text(paragraph.to_owned());
self.write_paragraph::<LatexJustifier>(&ast, font_config, size, &en);
self.new_line(size);
}
}
/// Writes a paragraph on the document.
pub fn write_paragraph<'a, J: Justifier>(
&mut self,
paragraph: &Ast,
font_config: &FontConfig,
size: Pt,
dict: &Standard,
) {
let paragraph = itemize_ast(paragraph, font_config, size, dict, Pt(0.0));
let justified = J::justify(¶graph, self.window.width);
for line in justified {
for glyph in line {
self.layer.use_text(
glyph.0.glyph.to_string(),
Into::<Pt>::into(glyph.0.scale).0 as i64,
(self.window.x + glyph.1).into(),
self.cursor.1.into(),
glyph.0.font.printpdf(),
);
}
self.new_line(size);
self.cursor.0 = self.window.x;
if self.cursor.1 <= size + self.window.y {
self.new_page();
}
}
}
/// Writes a line in the document.
pub fn write_line(&mut self, words: &[&str], font: &Font, size: Pt, spacing: Pt) {
let size_i64 = Into::<Pt>::into(size).0 as i64;
let mut current_width = self.window.x;
for word in words {
let width = current_width;
let height = self.cursor.1;
self.layer.use_text(
word.to_owned(),
size_i64,
width.into(),
(height - size).into(),
font.printpdf(),
);
current_width += font.text_width(word, size) + spacing;
}
self.new_line(size);
}
/// Goes to the beginning of the next line.
pub fn new_line(&mut self, size: Pt) {
self.cursor.1 -= size;
}
/// Creates a new page and append it to the document.
pub fn new_page(&mut self) {
let page = self
.document
.add_page(self.page_size.0.into(), self.page_size.1.into(), "");
self.page = self.document.get_page(page.0);
self.layer = self.page.get_layer(page.1);
self.cursor.1 = self.window.height + self.window.y;
}
/// Saves the document into a file.
pub fn save<P: AsRef<Path>>(self, path: P) {
let file = File::create(path.as_ref()).unwrap();
let mut writer = BufWriter::new(file);
self.document.save(&mut writer).unwrap();
}
}