Une feuille telle qu'elle part à l'impression

Un même dessin sur trois pages : une feuille d'impression qui dit jusqu'où court l'encre, où passera la lame et ce qui mérite d'être gardé, avec ses repères de coupe ; la même feuille une fois rognée à la page finie ; et une page en largeur, obtenue en déclarant un quart de tour sur une feuille debout.

Rust write_press_sheet.rs 437 lignes
  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
//! A sheet as it goes to the press, and what each box a page states does to it.
//!
//! A page has always had a sheet. It now also has the four boxes a printer
//! reads: what the ink runs to, where the blade falls, what is worth keeping,
//! and what a reader shows. The three pages below are the same drawing three
//! times over, and what changes is the boxes they state.
//!
//! Usage: `cargo run --example write_press_sheet -- tmp/press.pdf [font.ttf]`

use std::env;
use std::fs;
use std::path::{Path, PathBuf};

use hqf_pdf::content::Content;
use hqf_pdf::{Color, Document, Error, Font, FontHandle, Page, PageTurn, Rect, Rgb};

#[path = "shared/out.rs"]
mod out;

#[path = "shared/licence.rs"]
mod licence;

/// 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")
}

/// How far past the blade the ink runs.
const BLEED_OVER: f64 = 9.0;
/// How much sheet is left outside the ink for the marks.
const MARK_ROOM: f64 = 27.0;
/// How long one arm of a corner mark is.
const MARK_LENGTH: f64 = 18.0;

/// How much sheet stands outside the finished page on every side.
const MARGIN: f64 = BLEED_OVER + MARK_ROOM;

/// The finished page: where the blade falls, in the sheet's own coordinates.
const TRIM: (f64, f64, f64, f64) = (MARGIN, MARGIN, 400.0, 560.0);

/// The whole sheet: the finished page, the ink that runs past it, and the room
/// the marks stand in.
const SHEET: (f64, f64) = (TRIM.2 + MARGIN + MARGIN, TRIM.3 + MARGIN + MARGIN);

/// How far inside the blade the drawing itself keeps.
const ART_INSET: f64 = 40.0;

/// The colour the band across the top is laid in.
const BAND: Rgb = Rgb {
    r: 0.11,
    g: 0.26,
    b: 0.44,
};

/// Where the blade falls.
const fn trim() -> Rect {
    Rect::new(TRIM.0, TRIM.1, TRIM.2, TRIM.3)
}

/// What the ink runs to.
const fn bleed() -> Rect {
    Rect::new(
        TRIM.0 - BLEED_OVER,
        TRIM.1 - BLEED_OVER,
        TRIM.2 + BLEED_OVER + BLEED_OVER,
        TRIM.3 + BLEED_OVER + BLEED_OVER,
    )
}

/// What is worth keeping of the page.
const fn art() -> Rect {
    Rect::new(
        TRIM.0 + ART_INSET,
        TRIM.1 + ART_INSET,
        TRIM.2 - ART_INSET - ART_INSET,
        TRIM.3 - ART_INSET - ART_INSET,
    )
}

/// 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<(), 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(())
}

/// Draws the four corner marks, each pair of arms standing outside the ink and
/// pointing at a corner of the finished page.
fn corner_marks(content: &mut Content) -> Result<(), Error> {
    let (left, bottom, width, height) = TRIM;
    let right = left + width;
    let top = bottom + height;
    let near = BLEED_OVER;
    let far = BLEED_OVER + MARK_LENGTH;

    content.save_state();
    content.set_stroke(Color::Gray(0.0))?;
    content.set_line_width(0.4)?;
    for (x, from_x, to_x) in [
        (left, left - near, left - far),
        (right, right + near, right + far),
    ] {
        for (y, from_y, to_y) in [
            (bottom, bottom - near, bottom - far),
            (top, top + near, top + far),
        ] {
            content.move_to(from_x, y)?;
            content.line_to(to_x, y)?;
            content.move_to(x, from_y)?;
            content.line_to(x, to_y)?;
        }
    }
    content.stroke();
    content.restore_state();
    Ok(())
}

/// Draws the artwork itself: a band that runs off the blade, a title, a few
/// lines of body and a footer, all inside what is worth keeping.
fn artwork(content: &mut Content, font: &FontHandle) -> Result<(), Error> {
    let ink = bleed();
    let keep = art();

    content.save_state();
    content.set_fill(Color::Gray(0.96))?;
    content
        .rect(ink.x(), ink.y(), ink.width(), ink.height())?
        .fill();
    content.set_fill(BAND)?;
    content
        .rect(ink.x(), ink.y() + ink.height() - 150.0, ink.width(), 150.0)?
        .fill();
    content.restore_state();

    content.save_state();
    content.set_fill(Rgb::new(1.0, 1.0, 1.0))?;
    text(
        content,
        font,
        22.0,
        keep.x(),
        keep.y() + keep.height() - 30.0,
        "Autumn list",
    )?;
    content.set_fill(Rgb::new(0.82, 0.86, 0.92))?;
    text(
        content,
        font,
        10.0,
        keep.x(),
        keep.y() + keep.height() - 50.0,
        "The band above runs off the blade: no white edge is left.",
    )?;
    content.restore_state();

    content.save_state();
    content.set_fill(Color::Gray(0.25))?;
    let mut y = keep.y() + keep.height() - 130.0;
    for line in [
        "The blade never falls exactly where it is",
        "aimed. A sheet allows for that twice over:",
        "the ink runs past the cut, and nothing that",
        "matters stands within a few points of it.",
        "",
        "The boxes say all three distances, so nobody",
        "has to measure the file to find out where the",
        "page really ends.",
    ] {
        text(content, font, 10.0, keep.x(), y, line)?;
        y -= 16.0;
    }
    content.restore_state();

    listing(content, font, keep.y() + keep.height() - 290.0, keep)?;

    content.save_state();
    content.set_fill(Color::Gray(0.5))?;
    text(
        content,
        font,
        8.0,
        keep.x(),
        keep.y(),
        "HQF Development — one sheet, four boxes",
    )?;
    content.restore_state();
    Ok(())
}

/// Draws the list itself, one row a line, each under a hairline that runs the
/// width of what is worth keeping.
fn listing(content: &mut Content, font: &FontHandle, top: f64, keep: Rect) -> Result<(), Error> {
    let right = keep.x() + keep.width();
    let mut y = top;
    for (item, price) in [
        ("Linen paper, ream", "18.40"),
        ("Bookbinder's thread", "6.90"),
        ("Corner mark punch", "42.00"),
        ("Trim gauge, steel", "31.50"),
        ("Bleed guide, acrylic", "12.75"),
        ("Press blanket wash", "9.20"),
    ] {
        content.save_state();
        content.set_fill(Color::Gray(0.25))?;
        text(content, font, 10.0, keep.x(), y, item)?;
        text(content, font, 10.0, right - 30.0, y, price)?;
        content.set_stroke(Color::Gray(0.8))?;
        content.set_line_width(0.4)?;
        content.move_to(keep.x(), y - 8.0)?;
        content.line_to(right, y - 8.0)?;
        content.stroke();
        content.restore_state();
        y -= 26.0;
    }
    Ok(())
}

/// Draws the guide showing where each box falls, and names it.
fn guides(content: &mut Content, font: &FontHandle) -> Result<(), Error> {
    content.save_state();
    content.set_line_width(0.4)?;
    for (box_, color, label) in [
        (bleed(), Rgb::new(0.85, 0.35, 0.20), "bleed"),
        (trim(), Rgb::new(0.20, 0.55, 0.35), "trim"),
        (art(), Rgb::new(0.45, 0.35, 0.65), "art"),
    ] {
        content.set_stroke(color)?;
        content
            .rect(box_.x(), box_.y(), box_.width(), box_.height())?
            .stroke();
        content.set_fill(color)?;
        text(
            content,
            font,
            7.0,
            box_.x() + box_.width() - 30.0,
            box_.y() - 8.0,
            label,
        )?;
    }
    content.restore_state();
    Ok(())
}

/// The whole sheet: artwork, marks, the guides that name the boxes, and the
/// note in the margin saying what this page states.
fn sheet(font: &FontHandle, note: &[&str]) -> Result<Vec<u8>, Error> {
    let mut content = Content::new();
    artwork(&mut content, font)?;
    corner_marks(&mut content)?;
    guides(&mut content, font)?;

    content.save_state();
    content.set_fill(Color::Gray(0.35))?;
    let mut y = 8.0;
    for line in note.iter().rev() {
        text(&mut content, font, 7.0, TRIM.0, y, line)?;
        y += 9.0;
    }
    content.restore_state();
    Ok(content.into_bytes())
}

/// A4, in points.
const A4: (f64, f64) = (595.276, 841.890);

/// Draws one sheet of the little diagram: an outline, the band that marks the
/// head of the drawing, and a caption under it.
///
/// A sheet taller than it is wide is one the reader has not turned yet, so the
/// head of the drawing runs down its left edge; a wide one has been turned, and
/// the head is along its top.
fn sketch(
    content: &mut Content,
    font: &FontHandle,
    corner: (f64, f64, f64, f64),
    caption: &str,
) -> Result<(), Error> {
    let (x, y, width, height) = corner;
    let head = if height > width {
        (x + 8.0, y + 8.0, 12.0, height - 16.0)
    } else {
        (x + 8.0, y + height - 20.0, width - 16.0, 12.0)
    };

    content.save_state();
    content.set_stroke(Color::Gray(0.55))?;
    content.set_line_width(0.8)?;
    content.rect(x, y, width, height)?.stroke();
    content.set_fill(BAND)?;
    content.rect(head.0, head.1, head.2, head.3)?.fill();
    content.set_fill(Color::Gray(0.45))?;
    text(content, font, 8.0, x, y - 14.0, caption)?;
    content.restore_state();
    Ok(())
}

/// The third page: a landscape page inside an upright document.
///
/// Everything is laid out in a space as wide as the sheet is tall, and the one
/// transform at the head of the stream puts that space onto the upright sheet.
/// The page then states a quarter turn clockwise, and a reader undoes the
/// transform by turning the paper.
fn turned(font: &FontHandle) -> Result<Vec<u8>, Error> {
    let (across, up) = (A4.1, A4.0);
    let mut content = Content::new();
    content.transform([0.0, 1.0, -1.0, 0.0, A4.0, 0.0])?;

    content.save_state();
    content.set_fill(Color::Gray(0.96))?;
    content.rect(0.0, 0.0, across, up)?.fill();
    content.set_fill(BAND)?;
    content.rect(0.0, up - 90.0, across, 90.0)?.fill();
    content.restore_state();

    content.save_state();
    content.set_fill(Rgb::new(1.0, 1.0, 1.0))?;
    text(
        &mut content,
        font,
        18.0,
        70.0,
        up - 55.0,
        "A landscape page inside an upright document",
    )?;
    content.restore_state();

    content.save_state();
    content.set_fill(Color::Gray(0.3))?;
    let mut y = up - 130.0;
    for line in [
        "This page is the same upright sheet as the two before it: 595 points across, 842 up. What",
        "it adds is one number saying that a reader turns it a quarter clockwise before showing it.",
        "",
        "So the words are wider than the sheet they are written on, and nobody has to be handed a",
        "second page size to say so. A reader printing the file turns the paper rather than the",
        "drawing, and a reader showing it hands you the page the way round it was meant.",
    ] {
        text(&mut content, font, 10.0, 70.0, y, line)?;
        y -= 16.0;
    }
    content.restore_state();

    sketch(
        &mut content,
        font,
        (150.0, 140.0, 110.0, 155.0),
        "as it sits in the file",
    )?;
    sketch(
        &mut content,
        font,
        (400.0, 165.0, 155.0, 110.0),
        "as a reader shows it",
    )?;

    content.save_state();
    content.set_stroke(Color::Gray(0.55))?;
    content.set_line_width(1.2)?;
    content.move_to(295.0, 218.0)?;
    content.line_to(360.0, 218.0)?;
    content.line_to(348.0, 226.0)?;
    content.move_to(360.0, 218.0)?;
    content.line_to(348.0, 210.0)?;
    content.stroke();
    content.set_fill(Color::Gray(0.45))?;
    text(&mut content, font, 8.0, 288.0, 234.0, "a quarter clockwise")?;
    content.restore_state();
    Ok(content.into_bytes())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut args = env::args().skip(1);
    let out = args.next().unwrap_or_else(|| out::default_path("press"));
    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 mut first = Page::new(SHEET.0, SHEET.1);
    first.content = sheet(
        &font,
        &[
            "Page 1 states the bleed, the trim and the art, and no crop: a reader shows the whole",
            "sheet, marks and all, which is what the press wants to see.",
        ],
    )?;
    first.bleed = Some(bleed());
    first.trim = Some(trim());
    first.art = Some(art());
    doc.add_page(first)?;

    let mut second = Page::new(SHEET.0, SHEET.1);
    second.content = sheet(
        &font,
        &[
            "Page 2 is the same drawing and the same boxes, plus a crop set to the trim: a reader",
            "shows the finished page alone. Nothing was removed — what is outside is still here.",
        ],
    )?;
    second.crop = Some(trim());
    second.bleed = Some(bleed());
    second.trim = Some(trim());
    second.art = Some(art());
    doc.add_page(second)?;

    let mut third = Page::a4();
    third.content = turned(&font)?;
    third.turn = PageTurn::Right;
    doc.add_page(third)?;

    let written = doc.to_bytes()?;
    if let Some(parent) = Path::new(&out).parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(&out, &written)?;
    println!(
        "wrote {out}: {} bytes, one drawing on {} pages, each stating its own boxes",
        written.len(),
        doc.page_count()
    );
    Ok(())
}