paper-age 1.4.0

Easy and secure paper backups of secrets
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
//! PaperAge

use std::io::Write;

use log::{debug, trace};
use printpdf::{
    Color, DateTime, Line, LineDashPattern, LinePoint, Mm, Op, PaintMode, ParsedFont, PdfDocument,
    PdfFontHandle, PdfPage, PdfSaveOptions, Point, Pt, Rect, Rgb, Svg, TextItem, WindingOrder,
    XObjectTransform,
};

use crate::page::*;

pub mod svg;

/// PaperAge version
pub const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION");

/// Font width / height = 3 / 5
const FONT_RATIO: f32 = 3.0 / 5.0;

const CODE_FONT_BYTES: &[u8] = include_bytes!("assets/fonts/IBMPlexMono-Regular.ttf");
const TITLE_FONT_BYTES: &[u8] = include_bytes!("assets/fonts/IBMPlexMono-Medium.ttf");

/// Container for all the data required to insert elements into the PDF
pub struct Document {
    /// The printpdf PDF document
    pub doc: PdfDocument,

    /// Operations to perform on the page
    ops: Vec<Op>,

    /// The medium weight font handle
    pub title_font: PdfFontHandle,

    /// The regular weight font handle
    pub code_font: PdfFontHandle,

    /// Page size
    pub page_size: PageSize,

    /// Document title
    pub title: String,
}

impl Document {
    /// Initialize the PDF with default dimensions and the required fonts. Also
    /// sets the title and the producer in the PDF metadata.
    pub fn new(title: String, page_size: PageSize) -> Result<Document, Box<dyn std::error::Error>> {
        debug!("Initializing PDF");

        let dimensions = page_size.dimensions();

        let mut doc = PdfDocument::new(&title);

        let producer = format!("PaperAge v{}", VERSION.unwrap_or("0.0.0"));
        let now = DateTime::now();
        doc.metadata.info.producer = producer;
        doc.metadata.info.creation_date = now;
        doc.metadata.info.modification_date = now;

        let mut warnings = Vec::new();

        let code_parsed = ParsedFont::from_bytes(CODE_FONT_BYTES, 0, &mut warnings)
            .ok_or("Failed to parse code font")?;
        let code_font_id = doc.add_font(&code_parsed);
        let code_font = PdfFontHandle::External(code_font_id);

        let title_parsed = ParsedFont::from_bytes(TITLE_FONT_BYTES, 0, &mut warnings)
            .ok_or("Failed to parse title font")?;
        let title_font_id = doc.add_font(&title_parsed);
        let title_font = PdfFontHandle::External(title_font_id);

        let ops = vec![
            // White background
            Op::SetFillColor {
                col: Color::Rgb(Rgb::new(1.0, 1.0, 1.0, None)),
            },
            Op::DrawRectangle {
                rectangle: Rect {
                    x: Pt(0.0),
                    y: Pt(0.0),
                    width: dimensions.width.into_pt(),
                    height: dimensions.height.into_pt(),
                    mode: Some(PaintMode::Fill),
                    winding_order: Some(WindingOrder::NonZero),
                },
            },
            // Reset fill color to black for text and QR code
            Op::SetFillColor {
                col: Color::Rgb(Rgb::new(0.0, 0.0, 0.0, None)),
            },
        ];

        Ok(Document {
            doc,
            ops,
            title_font,
            code_font,
            page_size,
            title: title.clone(),
        })
    }

    /// Insert the given title at the top of the PDF
    pub fn insert_title_text(&mut self, title: String) {
        debug!("Inserting title: {}", title.as_str());

        let font_size = 14.0;

        // Align the title with the QR code if the title is narrower than the QR code
        let margin = {
            if title.len() <= 37 {
                self.page_size.qrcode_left_edge()
            } else {
                self.page_size.dimensions().margin
            }
        };

        let y = self.page_size.dimensions().height
            - self.page_size.dimensions().margin
            - Mm::from(Pt(font_size));

        self.ops.push(Op::StartTextSection);
        self.ops.push(Op::SetTextCursor {
            pos: Point::new(margin, y),
        });
        self.ops.push(Op::SetFillColor {
            col: Color::Rgb(Rgb::new(0.0, 0.0, 0.0, None)),
        });
        self.ops.push(Op::SetFont {
            font: self.title_font.clone(),
            size: Pt(font_size),
        });
        self.ops.push(Op::ShowText {
            items: vec![TextItem::Text(title)],
        });
        self.ops.push(Op::EndTextSection);
    }

    /// Insert the given PEM ciphertext in the bottom half of the page
    pub fn insert_pem_text(&mut self, pem: String) {
        debug!("Inserting PEM encoded ciphertext");

        let mut font_size = 13.0;
        let mut line_height = 15.0;

        // Rudimentary text scaling to get the Ascii Armor text to fit
        if pem.lines().count() > 42 {
            font_size = 6.5;
            line_height = 7.0;
        } else if pem.lines().count() > 39 {
            font_size = 7.0;
            line_height = 8.0;
        } else if pem.lines().count() > 27 {
            font_size = 8.0;
            line_height = 9.0;
        } else if pem.lines().count() > 22 {
            font_size = 10.0;
            line_height = 12.0;
        }

        self.ops.push(Op::StartTextSection);

        self.ops.push(Op::SetTextCursor {
            pos: Point::new(
                self.page_size.dimensions().margin,
                (self.page_size.dimensions().height / 2.0)
                    - Mm::from(Pt(font_size))
                    - self.page_size.dimensions().margin,
            ),
        });
        self.ops.push(Op::SetLineHeight {
            lh: Pt(line_height),
        });
        self.ops.push(Op::SetFont {
            font: self.code_font.clone(),
            size: Pt(font_size),
        });

        for line in pem.lines() {
            self.ops.push(Op::ShowText {
                items: vec![TextItem::Text(line.to_string())],
            });
            self.ops.push(Op::AddLineBreak);
        }

        self.ops.push(Op::EndTextSection);
    }

    /// Insert the QR code of the PEM encoded ciphertext in the top half of the page
    pub fn insert_qr_code(&mut self, text: String) -> Result<(), Box<dyn std::error::Error>> {
        debug!("Inserting QR code");

        let image = svg::qrcode(text)?;

        let (svg_width_px, svg_height_px) =
            parse_svg_pixel_dimensions(&image).ok_or("Failed to parse SVG dimensions")?;

        let mut warnings = Vec::new();
        let xobject = Svg::parse(&image, &mut warnings)
            .map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
        let xobj_id = self.doc.add_xobject(&xobject);

        let dpi = 300.0;
        let svg_height_pt = Pt(svg_height_px * 72.0 / dpi);
        let svg_width_pt = Pt(svg_width_px * 72.0 / dpi);

        let desired_qr_size = self.page_size.qrcode_size();
        let initial_qr_size = Mm::from(svg_height_pt);
        let qr_scale = desired_qr_size.0 / initial_qr_size.0;

        let scale = qr_scale;
        let code_width = Pt(svg_width_pt.0 * scale);
        let code_height = Pt(svg_height_pt.0 * scale);

        let page_width_pt = self.page_size.dimensions().width.into_pt();
        let page_height_pt = self.page_size.dimensions().height.into_pt();
        let margin_pt = self.page_size.dimensions().margin.into_pt();

        let translate_x = Pt((page_width_pt.0 - code_width.0) / 2.0);
        let translate_y = Pt(page_height_pt.0 - code_height.0 - margin_pt.0 * 2.0);

        self.ops.push(Op::UseXobject {
            id: xobj_id,
            transform: XObjectTransform {
                translate_x: Some(translate_x),
                translate_y: Some(translate_y),
                rotate: None,
                scale_x: Some(scale),
                scale_y: Some(scale),
                dpi: Some(dpi),
            },
        });

        Ok(())
    }

    /// Draw a grid debugging layout issues
    pub fn draw_grid(&mut self) {
        debug!("Drawing grid");

        let grid_size = Mm(5.0);
        let thickness = 0.0;

        let mut x = Mm(0.0);
        let mut y = self.page_size.dimensions().height;
        while x < self.page_size.dimensions().width {
            x += grid_size;

            self.draw_line(
                vec![
                    Point::new(x, self.page_size.dimensions().height),
                    Point::new(x, Mm(0.0)),
                ],
                thickness,
                LineDashPattern::default(),
            );

            while y > Mm(0.0) {
                y -= grid_size;

                self.draw_line(
                    vec![
                        Point::new(self.page_size.dimensions().width, y),
                        Point::new(Mm(0.0), y),
                    ],
                    thickness,
                    LineDashPattern::default(),
                );
            }
        }
    }

    /// Draw a line on the page
    pub fn draw_line(&mut self, points: Vec<Point>, thickness: f32, dash_pattern: LineDashPattern) {
        trace!("Drawing line");

        self.ops.push(Op::SetLineDashPattern { dash: dash_pattern });

        let outline_color = Color::Rgb(Rgb::new(0.75, 0.75, 0.75, None));
        self.ops.push(Op::SetOutlineColor { col: outline_color });

        self.ops.push(Op::SetOutlineThickness { pt: Pt(thickness) });

        let divider = Line {
            points: points
                .iter()
                .map(|p| LinePoint {
                    p: *p,
                    bezier: false,
                })
                .collect(),
            is_closed: false,
        };

        self.ops.push(Op::DrawLine { line: divider });
    }

    /// Insert the notes field label and placeholder in the PDF
    pub fn insert_notes_field(&mut self, label: String, skip_line: bool) {
        debug!("Inserting notes/passphrase placeholder");
        const MAX_LABEL_LEN: usize = 32;

        let baseline =
            self.page_size.dimensions().height / 2.0 + self.page_size.dimensions().margin;

        let label_len = label.len();

        let font_size = 13.0;

        self.ops.push(Op::StartTextSection);
        self.ops.push(Op::SetTextCursor {
            pos: Point::new(self.page_size.qrcode_left_edge(), baseline),
        });
        self.ops.push(Op::SetFont {
            font: self.title_font.clone(),
            size: Pt(font_size),
        });
        self.ops.push(Op::ShowText {
            items: vec![TextItem::Text(label)],
        });
        self.ops.push(Op::EndTextSection);

        // If the placeholder line would be ridiculously short, don't draw it
        if label_len <= MAX_LABEL_LEN && !skip_line {
            self.draw_line(
                vec![
                    Point::new(
                        self.page_size.qrcode_left_edge()
                            + Mm::from(Pt(FONT_RATIO * font_size * label_len as f32)),
                        baseline - Mm(1.0),
                    ),
                    Point::new(
                        self.page_size.qrcode_left_edge() + self.page_size.qrcode_size(),
                        baseline - Mm(1.0),
                    ),
                ],
                1.0,
                LineDashPattern::default(),
            )
        }
    }

    /// Add the footer at the bottom of the page
    pub fn insert_footer(&mut self) {
        debug!("Inserting footer");

        self.ops.push(Op::StartTextSection);
        self.ops.push(Op::SetTextCursor {
            pos: Point::new(
                self.page_size.dimensions().margin,
                self.page_size.dimensions().margin,
            ),
        });
        self.ops.push(Op::SetFont {
            font: self.title_font.clone(),
            size: Pt(13.0),
        });
        self.ops.push(Op::ShowText {
            items: vec![TextItem::Text(
                "Scan QR code and decrypt using Age <https://age-encryption.org>".to_string(),
            )],
        });
        self.ops.push(Op::EndTextSection);
    }

    /// Build the final PDF and return as bytes
    pub fn save_to_bytes(mut self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        let dimensions = self.page_size.dimensions();
        let page = PdfPage::new(dimensions.width, dimensions.height, self.ops);
        self.doc.pages.push(page);

        let mut warnings = Vec::new();
        let bytes = self.doc.save(&PdfSaveOptions::default(), &mut warnings);
        Ok(bytes)
    }

    /// Build the final PDF and write to a writer
    pub fn save_to_writer<W: Write>(
        mut self,
        writer: &mut W,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let dimensions = self.page_size.dimensions();
        let page = PdfPage::new(dimensions.width, dimensions.height, self.ops);
        self.doc.pages.push(page);

        let mut warnings = Vec::new();
        self.doc
            .save_writer(writer, &PdfSaveOptions::default(), &mut warnings);
        Ok(())
    }

    /// Build a PaperAge PDF and return its bytes.
    ///
    /// # Arguments
    /// * `grid` - Whether to draw a debug grid
    /// * `notes_label` - Label for the notes/passphrase field
    /// * `skip_notes_line` - Whether to omit the notes placeholder line
    /// * `encrypted` - The encrypted ciphertext to encode as a QR code and PEM block
    pub fn create_pdf(
        mut self,
        grid: bool,
        notes_label: String,
        skip_notes_line: bool,
        encrypted: String,
    ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        if grid {
            self.draw_grid();
        }

        self.insert_title_text(self.title.clone());

        self.insert_qr_code(encrypted.clone())?;

        self.insert_notes_field(notes_label, skip_notes_line);

        self.draw_line(
            vec![
                self.page_size.dimensions().center_left(),
                self.page_size.dimensions().center_right(),
            ],
            1.0,
            LineDashPattern {
                dash_1: Some(5),
                ..LineDashPattern::default()
            },
        );

        self.insert_pem_text(encrypted);

        self.insert_footer();

        self.save_to_bytes()
    }
}

/// Parse width and height pixel values from an SVG string
fn parse_svg_pixel_dimensions(svg: &str) -> Option<(f32, f32)> {
    let width_start = svg.find("width=\"")? + 7;
    let width_end = svg[width_start..].find('"')? + width_start;
    let width: f32 = svg[width_start..width_end].parse().ok()?;

    let height_start = svg.find("height=\"")? + 8;
    let height_end = svg[height_start..].find('"')? + height_start;
    let height: f32 = svg[height_start..height_end].parse().ok()?;

    Some((width, height))
}

#[test]
fn test_paper_dimensions_default() {
    let default = PageDimensions::default();
    assert_eq!(default.width, Mm(210.0));
    assert_eq!(default.height, Mm(297.0));
}

#[test]
fn test_new_document() {
    let title = String::from("Hello World!");
    let result = Document::new(title, PageSize::A4);
    assert!(result.is_ok());

    let doc = result.unwrap();
    assert_eq!(doc.page_size.dimensions(), crate::page::A4_PAGE);
}

#[test]
fn test_new_letter_document() {
    let title = String::from("Hello Letter!");
    let result = Document::new(title, PageSize::Letter);
    assert!(result.is_ok());

    let doc = result.unwrap();
    assert_eq!(doc.page_size.dimensions(), crate::page::LETTER_PAGE);
}

#[test]
fn test_qrcode() {
    let result = Document::new(String::from("QR code"), PageSize::A4);
    let mut document = result.unwrap();
    let result = document.insert_qr_code(String::from("payload"));
    assert!(result.is_ok());
}

#[test]
fn test_qrcode_too_large() {
    let mut document = Document::new(String::from("QR code"), PageSize::A4).unwrap();
    let result = document.insert_qr_code(String::from(include_str!("../tests/data/too_large.txt")));

    assert!(result.is_err());
    assert!(result.unwrap_err().is::<qrcode::types::QrError>());
}