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
/*!
A high-level, ergonomic Rust library creating PDF documents.
[krilla] is a high-level Rust crate that allows for the creation of PDF files. It builds
on top of the [pdf-writer] crate, but abstracts away all complexities that are
involved in creating a PDF file, instead providing an interface with high-level primitives, such
as fills, strokes, gradient, glyphs and images which can be used and combined easily
without having to worry about low-level details.
To get started, take a look at the [`document`] module that explains how you can create
a document using krilla.
# Example
The following example shows some of the features of krilla in action.
The example creates a PDF file with two pages. On the first page,
we add two small pieces of text, and on the second page we draw a triangle
with a gradient fill.
For more examples, feel free to take a look at the [examples] directory of the GitHub repository.
```
use std::path::{self, PathBuf};
use krilla::color::rgb;
use krilla::text::Font;
use krilla::geom::{Point, PathBuilder};
use krilla::paint::{SpreadMethod, LinearGradient, Stop, FillRule};
use krilla::text::TextDirection;
use krilla::paint::Fill;
use krilla::Document;
use krilla::page::PageSettings;
use krilla::num::NormalizedF32;
// Create a new document.
let mut document = Document::new();
// Load a font.
let font = {
let path =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets/fonts/NotoSans-Regular.ttf");
let data = std::fs::read(&path).unwrap();
Font::new(data.into(), 0).unwrap()
};
// Add a new page with dimensions 200x200.
let mut page = document.start_page_with(PageSettings::from_wh(200.0, 200.0).unwrap());
// Get the surface of the page.
let mut surface = page.surface();
// Draw some text.
surface.draw_text(
Point::from_xy(0.0, 25.0),
font.clone(),
14.0,
"This text has font size 14!",
false,
TextDirection::Auto
);
surface.set_fill(Some(Fill {
paint: rgb::Color::new(255, 0, 0).into(),
opacity: NormalizedF32::new(0.5).unwrap(),
rule: Default::default(),
}));
// Draw some more text, in a different color with an opacity and bigger font size.
surface.draw_text(
Point::from_xy(0.0, 50.0),
font.clone(),
16.0,
"This text has font size 16!",
false,
TextDirection::Auto
);
// Finish the page.
surface.finish();
page.finish();
// Start a new page.
let mut page = document.start_page_with(PageSettings::from_wh(200.0, 200.0).unwrap());
// Create the triangle.
let triangle = {
let mut pb = PathBuilder::new();
pb.move_to(100.0, 20.0);
pb.line_to(160.0, 160.0);
pb.line_to(40.0, 160.0);
pb.close();
pb.finish().unwrap()
};
// Create the linear gradient.
let lg = LinearGradient {
x1: 60.0,
y1: 0.0,
x2: 140.0,
y2: 0.0,
transform: Default::default(),
spread_method: SpreadMethod::Repeat,
stops: vec![
Stop {
offset: NormalizedF32::new(0.2).unwrap(),
color: rgb::Color::new(255, 0, 0).into(),
opacity: NormalizedF32::ONE,
},
Stop {
offset: NormalizedF32::new(0.8).unwrap(),
color: rgb::Color::new(255, 255, 0).into(),
opacity: NormalizedF32::ONE,
},
],
anti_alias: false,
};
let mut surface = page.surface();
// Set the fill.
surface.set_fill(Some(Fill {
paint: lg.into(),
rule: FillRule::EvenOdd,
opacity: NormalizedF32::ONE
}));
// Fill the path.
surface.draw_path(&triangle);
// Finish up and write the resulting PDF.
surface.finish();
page.finish();
let pdf = document.finish().unwrap();
let path = path::absolute("basic.pdf").unwrap();
eprintln!("Saved PDF to '{}'", path.display());
// Write the PDF to a file.
std::fs::write(path, &pdf).unwrap();
```
# Minimum Supported Rust Version (MSRV)
The minimum supported Rust version is **1.89**.
[krilla]: https://github.com/LaurenzV/krilla
[pdf-writer]: https://github.com/typst/pdf-writer
[examples]: https://github.com/LaurenzV/krilla/tree/main/crates/krilla/examples
[`document`]: crate::document
*/
pub
pub
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use SerializeSettings;