-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdf.rs
132 lines (122 loc) · 4.31 KB
/
pdf.rs
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
use lester::{Backdrop, PdfDocument, RenderOptions};
use std::env;
use std::fs::File;
use std::io::Write;
use victor::fonts::{FontError, BITSTREAM_VERA_SANS};
use victor::pdf::Document;
use victor::primitives::{point, rect, Length, Size, TextRun, RGBA};
use victor::text::ShapedSegment;
include_fonts! {
AHEM: "fonts/ahem/ahem.ttf",
NOTO: "fonts/noto/NotoSansLinearB-Regular.ttf",
}
fn doc() -> Result<Vec<u8>, FontError> {
let vera = BITSTREAM_VERA_SANS.clone();
let noto = NOTO.clone();
let ahem = AHEM.clone();
let mut doc = Document::new();
doc.add_page(Size::new(140., 50.))
.show_text(&TextRun {
segment: &(ShapedSegment::naive_shape("Têst→iimm", vera)?),
font_size: Length::new(15.),
origin: point(10., 20.),
})?
.show_text(&TextRun {
segment: &(ShapedSegment::naive_shape("pÉX", ahem)?),
font_size: Length::new(15.),
origin: point(10., 40.),
})?
.show_text(&TextRun {
segment: &(ShapedSegment::naive_shape("𐁉 𐁁𐀓𐀠𐀴𐀍", noto)?),
font_size: Length::new(15.),
origin: point(65., 40.),
})?;
doc.add_page(Size::new(4., 4.))
.set_color(&RGBA(0., 0., 1., 1.))
.paint_rectangle(&rect(0., 1., 4., 3.))
.set_color(&RGBA(1., 0., 0., 0.5))
.paint_rectangle(&rect(0., 0., 1., 2.));
Ok(doc.write_to_pdf_bytes())
}
#[test]
fn pdf() {
let pdf_bytes = doc().unwrap();
if env::var("VICTOR_WRITE_TO_TMP").is_ok() {
File::create("/tmp/victor.pdf")
.unwrap()
.write_all(&pdf_bytes)
.unwrap();
}
if env::var("VICTOR_PRINT").is_ok() {
println!("{}", String::from_utf8_lossy(&pdf_bytes));
}
let doc = PdfDocument::from_bytes(&pdf_bytes).unwrap();
assert_eq!(
doc.producer().unwrap().to_str().unwrap(),
"Victor <https://github.com/SimonSapin/victor>"
);
let pages: Vec<_> = doc.pages().collect();
assert_eq!(pages[0].size_in_css_px(), (140., 50.));
assert_eq!(pages[1].size_in_css_px(), (4., 4.));
// FIXME: find a way to round-trip code points without a glyph like '→'
assert_eq!(
pages[0].text().to_str().unwrap(),
"Têst iimm\npÉX 𐁉 𐁁𐀓𐀠𐀴𐀍"
);
assert_eq!(pages[1].text().to_str().unwrap(), "");
if env::var("VICTOR_WRITE_TO_TMP").is_ok() {
pages[0]
.render_with_dppx(3.)
.unwrap()
.write_to_png_file("/tmp/victor.png")
.unwrap()
}
let mut surface = pages[1].render().unwrap();
const RED_: u32 = 0x8080_0000;
const BLUE: u32 = 0xFF00_00FF;
const BOTH: u32 = 0xFF80_007F;
const ____: u32 = 0x0000_0000;
#[rustfmt::skip]
assert_pixels_eq!(
surface.pixels().buffer,
&[
RED_, ____, ____, ____,
BOTH, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE,
]
);
let mut surface = pages[1]
.render_with_options(RenderOptions {
dppx_x: 2.0,
dppx_y: 3.0,
backdrop: Backdrop::White,
..RenderOptions::default()
})
.unwrap();
let pixels = surface.pixels();
assert_eq!((pixels.width, pixels.height), (8, 12));
{
const RED_: u32 = 0xFFFF_7F7F;
const ____: u32 = 0xFFFF_FFFF;
#[rustfmt::skip]
assert_pixels_eq!(
pixels.buffer,
&[
RED_, RED_, ____, ____, ____, ____, ____, ____,
RED_, RED_, ____, ____, ____, ____, ____, ____,
RED_, RED_, ____, ____, ____, ____, ____, ____,
BOTH, BOTH, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BOTH, BOTH, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BOTH, BOTH, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE, BLUE,
][..]
);
}
assert!(pdf_bytes == include_bytes!("expected.pdf").as_ref());
}