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 | //! Fills six panels with patterns, each a cell drawn once and repeated.
//!
//! A tiling pattern is a small drawing and a step. The file holds the cell's
//! operators once, whatever the area filled with it, and the cell leaves alone
//! everything it does not paint — which is what lets two patterns cross. The
//! plane is tiled in the page's own space, so the panels below share one grid
//! rather than each starting its own.
//!
//! The page is held in `Words`, once per language, and `HQF_PDF_LANG` picks
//! which set is drawn. What the panels are filled with is not language: the
//! cells, the steps and the matrix are the same drawing whoever reads the page.
//!
//! Usage: `cargo run --example write_tiling -- tmp/tiling.pdf [font.ttf]`
//! `HQF_PDF_LANG=fr cargo run --example write_tiling -- tmp/motifs.pdf`
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use hqf_pdf::content::Content;
use hqf_pdf::{Document, Font, FontHandle, Page, PatternHandle, Rect, Rgb, TilingPattern};
#[path = "shared/out.rs"]
mod out;
#[path = "shared/licence.rs"]
mod licence;
#[path = "shared/language.rs"]
mod language;
use language::Language;
/// The font the example draws with when none is given on the command line.
fn default_font() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fonts")
.join("DejaVuSans.ttf")
}
/// The size of one panel, in points.
const PANEL: (f64, f64) = (215.0, 150.0);
/// The bottom-left corner of each panel.
const CORNERS: &[(f64, f64)] = &[
(70.0, 560.0),
(305.0, 560.0),
(70.0, 380.0),
(305.0, 380.0),
(70.0, 200.0),
(305.0, 200.0),
];
/// How many panels the page holds.
const PANELS: usize = 6;
/// The name set in pattern-filled type at the foot of the page. A name is not
/// language.
const PRODUCT: &str = "hqf-pdf";
/// The words the page is written in, one set per language.
///
/// What each panel is filled with is not here: a cell, a step and a matrix are
/// the same drawing in every language.
#[derive(Debug)]
struct Words {
/// The line at the head of the page.
title: &'static str,
/// The two lines under it, saying what a cell is and where it is tiled.
intro: [&'static str; 2],
/// What each panel shows, in the order the panels are drawn.
captions: [&'static str; PANELS],
/// The line under the pattern-filled name.
filled: &'static str,
}
impl Words {
/// The words the page is written in, in `language`.
fn of(language: Language) -> &'static Self {
language::pick(&WORDS, language)
}
}
/// The page in English.
const ENGLISH: Words = Words {
title: "One cell, tiled across whatever it fills",
intro: [
"Each panel holds one cell's operators, however wide the panel. The plane is tiled in the",
"page's own space, so every panel below is a window onto the same grid.",
],
captions: [
"Bars, stepping their own width",
"One dot, stepping wider than itself",
"The same bars, slanted by the pattern's matrix",
"Bricks: what leaves the cell's box is cut off",
"Two patterns, one over the other",
"The same bars again, at three times the step",
],
filled: "The brick pattern again, this time as the colour a run of text is filled with.",
};
/// The page in French.
const FRENCH: Words = Words {
title: "Une cellule, répétée sur tout ce qu'elle remplit",
intro: [
"Un panneau ne porte qu'une fois les instructions de sa cellule, quelle que soit sa largeur.",
"Le plan est pavé dans l'espace de la page : tous les panneaux donnent sur la même grille.",
],
captions: [
"Des barres, au pas de leur propre largeur",
"Un point, au pas plus large que lui",
"Les mêmes barres, penchées par la matrice",
"Briques : ce qui sort du cadre est coupé",
"Deux motifs, l'un par-dessus l'autre",
"Les mêmes barres, au triple du pas",
],
filled: "Le motif de briques encore, cette fois comme l'encre qui remplit un texte.",
};
/// Every language the example is written in. A language is added by writing its
/// own set of words and naming it here.
static WORDS: [(Language, &Words); 2] =
[(Language::English, &ENGLISH), (Language::French, &FRENCH)];
/// Draws one line of text with its baseline at `(x, y)`.
fn text(
content: &mut Content,
font: &FontHandle,
size: f64,
x: f64,
y: f64,
line: &str,
) -> Result<(), hqf_pdf::Error> {
content.begin_text();
content.set_font(font.name(), size)?;
content.text_origin(x, y)?;
content.show_glyphs(&font.glyphs(line));
content.end_text();
Ok(())
}
/// A cell holding one bar `bar` points wide, repeated every `step` across.
fn bars(bar: f64, step: f64, color: Rgb) -> Result<TilingPattern, hqf_pdf::Error> {
let mut cell = Content::new();
cell.set_fill(color)?;
cell.rect(0.0, 0.0, bar, 8.0)?.fill();
Ok(TilingPattern::new(
cell,
Rect::new(0.0, 0.0, 8.0, 8.0),
(step, 8.0),
))
}
/// A cell holding one square, repeated on a grid three times as wide as it is.
fn dots(color: Rgb) -> Result<TilingPattern, hqf_pdf::Error> {
let mut cell = Content::new();
cell.set_fill(color)?;
cell.rect(0.0, 0.0, 3.0, 3.0)?.fill();
Ok(TilingPattern::new(
cell,
Rect::new(0.0, 0.0, 12.0, 12.0),
(12.0, 12.0),
))
}
/// A cell holding one bar lying flat, repeated every eight points up.
fn rules(color: Rgb) -> Result<TilingPattern, hqf_pdf::Error> {
let mut cell = Content::new();
cell.set_fill(color)?;
cell.rect(0.0, 0.0, 8.0, 2.0)?.fill();
Ok(TilingPattern::new(
cell,
Rect::new(0.0, 0.0, 8.0, 8.0),
(8.0, 8.0),
))
}
/// Two courses of brick, the upper one offset by half a brick.
///
/// The bricks that run off the side of the cell are drawn all the same: the box
/// cuts them, and what the next cell along draws is their other half.
fn bricks() -> Result<TilingPattern, hqf_pdf::Error> {
let mortar = Rgb::new(0.88, 0.86, 0.83);
let brick = Rgb::new(0.70, 0.35, 0.26);
let mut cell = Content::new();
cell.set_fill(mortar)?;
cell.rect(0.0, 0.0, 24.0, 24.0)?.fill();
cell.set_fill(brick)?;
for (x, y) in [
(0.5, 0.5),
(12.5, 0.5),
(-5.5, 12.5),
(6.5, 12.5),
(18.5, 12.5),
] {
cell.rect(x, y, 11.0, 11.0)?.fill();
}
Ok(TilingPattern::new(
cell,
Rect::new(0.0, 0.0, 24.0, 24.0),
(24.0, 24.0),
))
}
/// Fills one panel with each of `fills` in turn, and frames it.
fn panel(
content: &mut Content,
corner: (f64, f64),
fills: &[&PatternHandle],
) -> Result<(), hqf_pdf::Error> {
let (width, height) = PANEL;
let (x, y) = corner;
content.save_state();
content.set_fill(Rgb::new(0.98, 0.98, 0.98))?;
content.rect(x, y, width, height)?.fill();
for fill in fills {
content.set_fill_pattern(fill);
content.rect(x, y, width, height)?.fill();
}
content.restore_state();
content.save_state();
content.set_stroke(Rgb::gray(0.55))?;
content.set_line_width(0.6)?;
content.rect(x, y, width, height)?.stroke();
content.restore_state();
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let language = Language::from_environment()?;
let words = Words::of(language);
let mut args = env::args().skip(1);
// A named file is written as named; the default one carries the language,
// so the two languages do not overwrite each other in `tmp/`.
let out = args
.next()
.unwrap_or_else(|| language.file_name(&out::default_path("tiling")));
let font_path = args.next().map_or_else(default_font, PathBuf::from);
let mut doc = Document::new();
doc.set_license(licence::licensed());
let font = doc.add_font(Font::parse(fs::read(&font_path)?)?);
let ink = Rgb::new(0.12, 0.28, 0.52);
let upright = doc.add_tiling_pattern(bars(3.0, 8.0, ink)?);
let scattered = doc.add_tiling_pattern(dots(Rgb::new(0.16, 0.45, 0.35))?);
let slanted =
doc.add_tiling_pattern(bars(3.0, 8.0, ink)?.matrix([1.0, 0.0, 0.5, 1.0, 0.0, 0.0]));
let wall = doc.add_tiling_pattern(bricks()?);
let flat = doc.add_tiling_pattern(rules(Rgb::new(0.78, 0.30, 0.24))?);
let sparse = doc.add_tiling_pattern(bars(3.0, 24.0, ink)?);
let mut content = Content::new();
text(&mut content, &font, 15.0, 70.0, 780.0, words.title)?;
content.save_state();
content.set_fill(Rgb::gray(0.35))?;
text(&mut content, &font, 10.0, 70.0, 762.0, words.intro[0])?;
text(&mut content, &font, 10.0, 70.0, 748.0, words.intro[1])?;
content.restore_state();
let fills: [&[&PatternHandle]; 6] = [
&[&upright],
&[&scattered],
&[&slanted],
&[&wall],
&[&upright, &flat],
&[&sparse],
];
for ((corner, caption), fill) in CORNERS.iter().zip(words.captions).zip(fills) {
panel(&mut content, *corner, fill)?;
content.save_state();
content.set_fill(Rgb::gray(0.35))?;
text(&mut content, &font, 9.0, corner.0, corner.1 - 14.0, caption)?;
content.restore_state();
}
// Text filled with a pattern: the letters are windows onto the tiled plane,
// not a hundred little copies of the cell.
content.save_state();
content.set_fill_pattern(&wall);
text(&mut content, &font, 58.0, 70.0, 100.0, PRODUCT)?;
content.restore_state();
content.save_state();
content.set_fill(Rgb::gray(0.35))?;
text(&mut content, &font, 10.0, 70.0, 80.0, words.filled)?;
content.restore_state();
let mut page = Page::a4();
page.content = content.into_bytes();
doc.add_page(page)?;
let bytes = doc.to_bytes()?;
if let Some(parent) = Path::new(&out).parent() {
fs::create_dir_all(parent)?;
}
fs::write(&out, &bytes)?;
println!(
"wrote {out}: {} bytes, {} panels from 6 cells",
bytes.len(),
CORNERS.len()
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::{WORDS, language};
/// The lines two languages are allowed to write the same way. Every line
/// here is a sentence or a caption, and no two languages write one alike.
const SPARED: [&str; 0] = [];
#[test]
fn every_language_draws_the_page_in_its_own_words() {
let untranslated = language::untranslated_lines(&WORDS, &SPARED);
assert!(
untranslated.is_empty(),
"the page says these in more than one language: {untranslated:?}"
);
}
}
|