Inks on plates of their own

Two inks the press carries on a plate of their own, each at ten strengths, a piece of work set in both, and the crosses used to line the plates up — drawn in an ink every plate answers to.

Rust write_spot_color.rs 514 lines
  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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Inks the press carries on plates of their own, at every tint.
//!
//! A spot colour is not mixed out of the process inks: it is an ink of its own,
//! and a colour in its space is one number — how much of it goes down. What
//! that number looks like where the ink is not to hand, on a screen or on a
//! press that does not carry it, is what the tint transform says.
//!
//! The second page sets a run list in those same two inks and in nothing else:
//! the heading on a full-strength plate, every other line on a tint of it, the
//! total on the second ink, the rules in both, and a closing paragraph whose
//! runs each name an ink. Everything that paints takes a colour the document
//! carries exactly where it takes a device one — a cell's background, a cell's
//! text, a row, a table's rules, a run of styled text, a stamp.
//!
//! Usage: `cargo run --example write_spot_color -- tmp/spot.pdf [font.ttf]`

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

use hqf_pdf::content::Content;
use hqf_pdf::layout::{
    Area, Cell, ColumnWidth, Columns, Padding, RichText, Row, Rule, Stamp, Stroke, Style, Table,
    VAlign,
};
use hqf_pdf::{
    Align, Cmyk, ColorSpaceHandle, DeviceSpace, Document, Exponential, Font, FontHandle, Page,
    Paint, Rgb, Separation, TextFlow,
};

#[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")
}

/// The left edge of everything, and the width of the sheet the page shows.
const LEFT: f64 = 70.0;
/// See [`LEFT`].
const WIDTH: f64 = 445.0;

/// The left edge of each tint swatch, and the size of one.
const TINT_X: &[f64] = &[
    70.0, 115.0, 160.0, 205.0, 250.0, 295.0, 340.0, 385.0, 430.0, 475.0,
];
/// See [`TINT_X`].
const TINT_SIZE: (f64, f64) = (40.0, 56.0);

/// The top of the run list on the second page.
const LIST_TOP: f64 = 700.0;

/// One line of the run list: what is being printed, how many sheets, which
/// plates it takes and what it comes to.
const ITEMS: &[[&str; 4]] = &[
    ["Letterhead, A4", "5 000", "First", "412.00"],
    ["Letterhead, A5", "2 000", "First", "196.00"],
    ["Business cards, both sides", "2 000", "Both", "268.00"],
    ["Compliments slip", "3 000", "Second", "184.00"],
    ["Presentation folder", "1 000", "Both", "590.00"],
    ["Report cover, laminated", "750", "Both", "445.00"],
    ["Window envelope, DL", "5 000", "First", "388.00"],
    ["Wallet envelope, C4", "1 500", "First", "242.00"],
    ["Exhibition panel", "12", "Both", "336.00"],
];

/// What the nine lines come to.
const TOTAL: &str = "3 061.00";

/// The prose under the run list, coloured by the flow that draws it.
const PROSE: &str = "An ink of its own is what a house colour is bought as: the \
    press carries it on a plate of its own, so it goes down at the same strength \
    on every sheet of every run, which four process inks laid over each other \
    cannot promise. It costs a plate, so it is spent on the marks a reader \
    recognises the house by — the name, the rule under it, the panel behind a \
    heading — and the rest of the work is left to the process inks. This \
    paragraph is one flow of text, drawn in the first ink at ninety percent, and \
    the words of it were measured and broken to the width of the list above.";

/// The ten tints each ink is shown at, and what each of them is called.
const TINTS: &[f64] = &[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
/// See [`TINTS`].
const TINT_NAMES: &[&str] = &[
    "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%",
];

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

/// Draws the ten tints of one ink, each labelled with how much goes down.
fn tint_ramp(
    content: &mut Content,
    font: &FontHandle,
    ink: &ColorSpaceHandle,
    y: f64,
    caption: &str,
) -> Result<(), hqf_pdf::Error> {
    let (width, height) = TINT_SIZE;

    for ((x, tint), name) in TINT_X.iter().zip(TINTS).zip(TINT_NAMES) {
        content.save_state();
        content.set_fill_space(ink);
        content.set_fill_components(&[*tint])?;
        content.rect(*x, y, width, height)?.fill();
        content.restore_state();

        content.save_state();
        content.set_fill(Rgb::gray(0.35))?;
        text(content, font, 7.0, *x, y - 11.0, name)?;
        content.restore_state();
    }

    content.save_state();
    content.set_fill(Rgb::gray(0.2))?;
    text(content, font, 10.0, LEFT, y + height + 8.0, caption)?;
    content.restore_state();
    Ok(())
}

/// Draws a printer's crosshair centred on `(x, y)`, in whatever space is in
/// force.
fn registration(content: &mut Content, x: f64, y: f64) -> Result<(), hqf_pdf::Error> {
    let arm = 7.0;
    content.move_to(x - arm, y)?;
    content.line_to(x + arm, y)?;
    content.move_to(x, y - arm)?;
    content.line_to(x, y + arm)?;
    content.stroke();

    content.rect(x - 4.0, y - 4.0, 8.0, 8.0)?.stroke();
    Ok(())
}

/// Draws a piece of work set in the two inks, with the marks a printer lines
/// the plates up by around it.
fn sheet(
    content: &mut Content,
    font: &FontHandle,
    blue: &ColorSpaceHandle,
    gold: &ColorSpaceHandle,
    registration_ink: &ColorSpaceHandle,
) -> Result<(), hqf_pdf::Error> {
    let (bottom, height) = (250.0, 190.0);
    content.save_state();
    content.set_stroke(Rgb::gray(0.75))?;
    content.set_line_width(0.5)?;
    content.rect(LEFT, bottom, WIDTH, height)?.stroke();
    content.restore_state();

    content.save_state();
    content.set_fill_space(blue);
    content.set_fill_components(&[1.0])?;
    text(
        content,
        font,
        26.0,
        LEFT + 24.0,
        bottom + 130.0,
        "Two inks, one sheet",
    )?;
    content.set_fill_components(&[0.15])?;
    content
        .rect(LEFT + 24.0, bottom + 40.0, WIDTH - 48.0, 66.0)?
        .fill();
    content.restore_state();

    content.save_state();
    content.set_fill_space(gold);
    content.set_fill_components(&[1.0])?;
    content
        .rect(LEFT + 24.0, bottom + 118.0, 180.0, 3.0)?
        .fill();
    text(
        content,
        font,
        11.0,
        LEFT + 40.0,
        bottom + 68.0,
        "Set in the second ink, over a fifteen percent tint of the first.",
    )?;
    content.restore_state();

    content.save_state();
    content.set_stroke_space(registration_ink);
    content.set_stroke_components(&[1.0])?;
    content.set_line_width(0.5)?;
    for (x, y) in [
        (LEFT - 16.0, bottom - 16.0),
        (LEFT + WIDTH + 16.0, bottom - 16.0),
        (LEFT - 16.0, bottom + height + 16.0),
        (LEFT + WIDTH + 16.0, bottom + height + 16.0),
    ] {
        registration(content, x, y)?;
    }
    content.restore_state();

    content.save_state();
    content.set_fill(Rgb::gray(0.35))?;
    text(
        content,
        font,
        9.0,
        LEFT,
        bottom - 40.0,
        "The four crosshairs are set in the colorant every plate answers to, which is what lines them up.",
    )?;
    content.restore_state();

    Ok(())
}

/// The run list, set in the two inks and in nothing else: the heading on a
/// full-strength plate of the first, every other line on an eight percent tint
/// of it, the total on the second, and every rule in one or the other.
fn run_list<'a>(
    font: &'a FontHandle,
    blue: &ColorSpaceHandle,
    gold: &ColorSpaceHandle,
) -> Result<Table<'a>, hqf_pdf::Error> {
    let columns = Columns::new(
        vec![
            ColumnWidth::Fraction(1.0),
            ColumnWidth::Points(54.0),
            ColumnWidth::Points(74.0),
            ColumnWidth::Points(66.0),
        ],
        WIDTH,
    )?;
    let mut table = Table::new(columns);
    table
        .fill(Area::EvenRows, Paint::named(blue, [0.08]))
        .fill(Area::Header, Paint::named(blue, [1.0]))
        .rule(Rule::Frame, Stroke::new(0.8, Paint::named(blue, [0.55])))
        .rule(
            Rule::HorizontalOther,
            Stroke::new(0.3, Paint::named(blue, [0.3])),
        )
        .rule(
            Rule::Horizontal(1),
            Stroke::new(1.2, Paint::named(gold, [1.0])),
        )
        .header(1);

    let heading = |label: &'static str, align: Align| {
        Cell::new(font, 9.0, label)
            .align(align)
            .valign(VAlign::Middle)
            .color(Rgb::WHITE)
            .padding(Padding::symmetric(6.0, 5.0))
    };
    table.push(
        Row::new()
            .cell(heading("Item", Align::Left))
            .cell(heading("Sheets", Align::Right))
            .cell(heading("Plates", Align::Left))
            .cell(heading("Price", Align::Right)),
    );

    for item in ITEMS {
        let line = |column: usize, align: Align| {
            Cell::new(font, 9.0, item[column])
                .align(align)
                .color(Paint::named(blue, [0.95]))
                .padding(Padding::symmetric(6.0, 5.0))
        };
        table.push(
            Row::new()
                .cell(line(0, Align::Left))
                .cell(line(1, Align::Right))
                .cell(line(2, Align::Left))
                .cell(line(3, Align::Right)),
        );
    }

    let total = |label: &str, align: Align| {
        Cell::new(font, 9.5, label)
            .align(align)
            .color(Paint::named(blue, [1.0]))
            .padding(Padding::symmetric(6.0, 6.0))
    };
    table.push(
        Row::new()
            .cell(total("Total, nine items, two plates", Align::Left))
            .cell(total("", Align::Right))
            .cell(total("", Align::Left))
            .cell(total(TOTAL, Align::Right))
            .fill(Paint::named(gold, [0.28])),
    );

    Ok(table)
}

/// Draws the run list, the note whose two runs each stand in the ink they name,
/// and a specimen mark laid across the whole of it.
fn run_sheet(
    content: &mut Content,
    font: &FontHandle,
    blue: &ColorSpaceHandle,
    gold: &ColorSpaceHandle,
) -> Result<(), hqf_pdf::Error> {
    content.save_state();
    content.set_fill_space(blue);
    content.set_fill_components(&[1.0])?;
    text(
        content,
        font,
        15.0,
        LEFT,
        780.0,
        "The same two inks, through the layout",
    )?;
    content.restore_state();

    content.save_state();
    content.set_fill(Rgb::gray(0.35))?;
    text(
        content,
        font,
        10.0,
        LEFT,
        762.0,
        "A colour the document carries reaches a cell, a row, a rule and a run of styled text the",
    )?;
    text(
        content,
        font,
        10.0,
        LEFT,
        748.0,
        "way a colour the device answers for does. Not a drop of process ink goes down below.",
    )?;
    content.restore_state();

    let table = run_list(font, blue, gold)?;
    let placed = table.fit(LEFT, LIST_TOP, LIST_TOP - 260.0, 0)?;
    let height = placed.height();
    placed.draw(content)?;

    let first_ink = Style::new(font, 9.5).color(Paint::named(blue, [0.9]));
    let second_ink = Style::new(font, 9.5).color(Paint::named(gold, [1.0]));
    let note = RichText::new()
        .push(
            "The heading stands on a full plate of the first ink, ",
            &first_ink,
        )
        .push("the total on the second", &second_ink)
        .push(
            ", and the shaded lines on an eight percent tint of the first. One run of this \
             sentence is set in each of the two.",
            &first_ink,
        );
    let lines = note.break_lines(WIDTH);
    let note_top = LIST_TOP - height - 26.0;
    note.draw(content, &lines, LEFT, note_top, WIDTH)?;

    let flow = TextFlow::new(font, 10.5)
        .color(Paint::named(blue, [0.9]))
        .leading(15.0);
    let prose = flow.break_lines(PROSE, WIDTH);
    content.begin_text();
    flow.draw(
        content,
        &prose,
        LEFT,
        note_top - RichText::height(&lines) - 26.0,
        WIDTH,
    )?;
    content.end_text();

    Stamp::new(font, "SPECIMEN")
        .color(Paint::named(gold, [0.22]))
        .draw(content, LEFT, LIST_TOP - height, WIDTH, height)?;

    content.save_state();
    content.set_stroke_space(gold);
    content.set_stroke_components(&[1.0])?;
    content.set_line_width(0.8)?;
    content.move_to(LEFT, 92.0)?;
    content.line_to(LEFT + WIDTH, 92.0)?;
    content.stroke();
    content.set_fill_space(blue);
    content.set_fill_components(&[0.55])?;
    text(
        content,
        font,
        8.5,
        LEFT,
        78.0,
        "Quoted in two inks: Corporate Blue and Corporate Gold, each on a plate of its own.",
    )?;
    content.restore_state();
    Ok(())
}

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("spot"));
    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 blue = doc.add_color_space(Separation::ink(
        "Corporate Blue",
        Cmyk::new(0.92, 0.58, 0.0, 0.06),
    ));
    let gold = doc.add_color_space(Separation::ink(
        "Corporate Gold",
        Cmyk::new(0.0, 0.28, 0.95, 0.04),
    ));

    // The colorant every plate answers to: a mark set in it goes down on all of
    // them, which is how a printer sees whether they line up. It is shown as
    // black where no press is involved, so the tint transform runs from the
    // paper to the ink.
    let registration_ink = doc.add_color_space(Separation::new(
        "All",
        DeviceSpace::Gray,
        Exponential::new([0.0, 1.0], 1.0)
            .endpoints(vec![1.0], vec![0.0])
            .range(vec![[0.0, 1.0]]),
    ));

    let mut content = Content::new();
    text(
        &mut content,
        &font,
        15.0,
        LEFT,
        780.0,
        "Inks the press carries, on plates of their own",
    )?;
    content.save_state();
    content.set_fill(Rgb::gray(0.35))?;
    text(
        &mut content,
        &font,
        10.0,
        LEFT,
        762.0,
        "A spot colour is one number, not four: how much of that ink goes down. Where the ink is not",
    )?;
    text(
        &mut content,
        &font,
        10.0,
        LEFT,
        748.0,
        "to hand, the tint transform the file carries shows it — which is what these swatches are.",
    )?;
    content.restore_state();

    tint_ramp(
        &mut content,
        &font,
        &blue,
        640.0,
        "Corporate Blue, one plate, ten tints",
    )?;
    tint_ramp(
        &mut content,
        &font,
        &gold,
        530.0,
        "Corporate Gold, a second plate",
    )?;

    sheet(&mut content, &font, &blue, &gold, &registration_ink)?;

    let mut page = Page::a4();
    page.content = content.into_bytes();
    doc.add_page(page)?;

    let mut second = Content::new();
    run_sheet(&mut second, &font, &blue, &gold)?;
    let mut page = Page::a4();
    page.content = second.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, 2 pages, two inks at {} tints each, {} priced lines",
        bytes.len(),
        TINTS.len(),
        ITEMS.len()
    );
    Ok(())
}