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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939 | //! 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.
//!
//! What the two sheets say is written in the language `HQF_PDF_LANG` names. The
//! names of the inks are not: a colorant is what the plate answers to, the file
//! writes it, and a page that renamed it would name a plate the press has not
//! got. Neither are the sheet counts, the prices or the tints.
//!
//! Usage: `cargo run --example write_spot_color -- tmp/spot.pdf [font.ttf]`
//! `HQF_PDF_LANG=fr cargo run --example write_spot_color`
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,
TableFrame, 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;
#[path = "shared/language.rs"]
mod language;
#[path = "shared/failure.rs"]
mod failure;
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 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;
/// Where the head of a sheet sits, and where the two lines under it do.
const HEAD_BASELINE: f64 = 780.0;
const LEAD_BASELINES: [f64; 2] = [762.0, 748.0];
/// Where the rule at the foot of the second sheet is drawn.
const CLOSING_RULE: f64 = 92.0;
/// The size the closing paragraph is set at, and how far apart its lines sit.
const PROSE_SIZE: f64 = 10.5;
const PROSE_LEADING: f64 = 15.0;
/// Where the crosshairs stand around a sheet whose foot is at `bottom` and
/// which is `height` tall. A page says how many there are, so the count and the
/// word have to agree.
fn crosshairs(bottom: f64, height: f64) -> [(f64, f64); 4] {
[
(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),
]
}
/// Which plates a line of the run list takes.
#[derive(Debug, Clone, Copy)]
enum Plates {
First,
Second,
Both,
}
impl Plates {
/// What this run of plates is called, in the words of a language.
const fn named(self, words: &'static Words) -> &'static str {
match self {
Self::First => words.plates[0],
Self::Second => words.plates[1],
Self::Both => words.plates[2],
}
}
}
/// One line of the run list: how many sheets it runs to, which plates it takes
/// and what it comes to. What is being printed is a word, and it lives with the
/// other words of its language.
const ITEMS: &[(&str, Plates, &str)] = &[
("5 000", Plates::First, "412.00"),
("2 000", Plates::First, "196.00"),
("2 000", Plates::Both, "268.00"),
("3 000", Plates::Second, "184.00"),
("1 000", Plates::Both, "590.00"),
("750", Plates::Both, "445.00"),
("5 000", Plates::First, "388.00"),
("1 500", Plates::First, "242.00"),
("12", Plates::Both, "336.00"),
];
/// What the nine lines come to.
const TOTAL: &str = "3 061.00";
/// The ten tints each ink is shown at. What each is called is read off the tint
/// itself, so no language can label a swatch with a strength it is not laid at.
const TINTS: &[f64] = &[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
/// What one tint is called under its swatch.
fn tint_name(tint: f64) -> String {
format!("{:.0}%", tint * 100.0)
}
/// The two inks the work is set in, and the colorant every plate answers to. A
/// colorant is what the plate is called on the press: the file writes it, and
/// no language renames it.
const BLUE: &str = "Corporate Blue";
const GOLD: &str = "Corporate Gold";
const ALL_PLATES: &str = "All";
/// The tint the panel behind the heading is laid at, which the caption under it
/// spells out in words.
const PANEL_TINT: f64 = 0.15;
/// The tint the shaded lines of the run list are laid at, which the note under
/// it spells out in words.
const SHADED_TINT: f64 = 0.08;
/// The strength the closing paragraph is drawn at, which it says of itself.
const PROSE_TINT: f64 = 0.9;
/// The sizes the two sheets are set in, in points.
const HEAD_SIZE: f64 = 15.0;
const LEAD_SIZE: f64 = 10.0;
const SWATCH_SIZE: f64 = 7.0;
const CAPTION_SIZE: f64 = 10.0;
const SHEET_TITLE_SIZE: f64 = 26.0;
const SHEET_LINE_SIZE: f64 = 11.0;
const MARK_SIZE: f64 = 9.0;
const CELL_SIZE: f64 = 9.0;
const TOTAL_SIZE: f64 = 9.5;
const CLOSING_SIZE: f64 = 8.5;
/// The words the two sheets are written in, one set per language.
///
/// The names of the inks are not among them, nor the sheet counts, the prices
/// or the tints.
#[derive(Debug)]
struct Words {
/// What the first sheet is called.
title: &'static str,
/// The two lines under it, which say what a spot colour is.
lead: [&'static str; 2],
/// What stands after the name of the first ink over its ramp of tints.
first_ramp: &'static str,
/// What stands after the name of the second over its own.
second_ramp: &'static str,
/// What the piece of work set in the two inks is called.
sheet_title: &'static str,
/// The line inside it, which says what is laid over what.
sheet_line: &'static str,
/// The line under it, which says what the crosshairs are for.
marks: &'static str,
/// What the second sheet is called.
list_title: &'static str,
/// The two lines under it, which say how far a carried colour reaches.
list_lead: [&'static str; 2],
/// What heads each column of the run list, left to right.
headings: [&'static str; 4],
/// What each line of the run list is printing, in the order [`ITEMS`] runs
/// them.
items: [&'static str; 9],
/// What each run of plates is called: the first alone, the second alone,
/// then both.
plates: [&'static str; 3],
/// What the last row of the run list is called.
total: &'static str,
/// The three runs of the note under the list, the middle one of which is
/// set in the second ink.
note: [&'static str; 3],
/// The paragraph under it, drawn in one flow.
prose: &'static str,
/// The mark laid across the whole of the run list.
stamp: &'static str,
/// What stands before the name of the first ink in the closing line.
quoted_before: &'static str,
/// What stands between the two names.
quoted_between: &'static str,
/// What stands after the second.
quoted_after: &'static str,
}
impl Words {
/// The words the two sheets are written in, in `language`.
fn of(language: Language) -> &'static Self {
language::pick(&WORDS, language)
}
}
/// The two sheets in English.
const ENGLISH: Words = Words {
title: "Inks the press carries, on plates of their own",
lead: [
"A spot colour is one number, not four: how much of that ink goes down. Where the ink is not",
"to hand, the tint transform the file carries shows it — which is what these swatches are.",
],
first_ramp: ", one plate, ten tints",
second_ramp: ", a second plate",
sheet_title: "Two inks, one sheet",
sheet_line: "Set in the second ink, over a fifteen percent tint of the first.",
marks: "The four crosshairs are set in the colorant every plate answers to, which is what lines them up.",
list_title: "The same two inks, through the layout",
list_lead: [
"A colour the document carries reaches a cell, a row, a rule and a run of styled text the",
"way a colour the device answers for does. Not a drop of process ink goes down below.",
],
headings: ["Item", "Sheets", "Plates", "Price"],
items: [
"Letterhead, A4",
"Letterhead, A5",
"Business cards, both sides",
"Compliments slip",
"Presentation folder",
"Report cover, laminated",
"Window envelope, DL",
"Wallet envelope, C4",
"Exhibition panel",
],
plates: ["First", "Second", "Both"],
total: "Total, nine items, two plates",
note: [
"The heading stands on a full plate of the first ink, ",
"the total on the second",
", and the shaded lines on an eight percent tint of the first. One run of this \
sentence is set in each of the two.",
],
prose: "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.",
stamp: "SPECIMEN",
quoted_before: "Quoted in two inks: ",
quoted_between: " and ",
quoted_after: ", each on a plate of its own.",
};
/// The two sheets in French.
const FRENCH: Words = Words {
title: "Des encres que la presse porte, chacune sur sa plaque",
lead: [
"Un ton direct est un seul nombre, pas quatre : la quantité d'encre qui descend. Là où l'encre",
"manque, la transformation de teinte du fichier la montre — c'est ce que sont ces pastilles.",
],
first_ramp: ", une plaque, dix teintes",
second_ramp: ", une seconde plaque",
sheet_title: "Deux encres, une feuille",
sheet_line: "Seconde encre, sur une teinte à quinze pour cent de la première.",
marks: "Les quatre croix sont tracées dans le colorant auquel chaque plaque répond : c'est ce qui les cale.",
list_title: "Les deux mêmes encres, à travers la mise en page",
list_lead: [
"Une couleur que le document porte atteint une cellule, une rangée, un filet et un passage",
"stylé comme le fait une couleur d'appareil. Pas une goutte d'encre de quadrichromie dessous.",
],
headings: ["Article", "Feuilles", "Plaques", "Prix"],
items: [
"Papier à en-tête, A4",
"Papier à en-tête, A5",
"Cartes de visite, recto verso",
"Carte de compliments",
"Chemise de présentation",
"Couverture de rapport, pelliculée",
"Enveloppe à fenêtre, DL",
"Enveloppe à poche, C4",
"Panneau d'exposition",
],
plates: ["Première", "Seconde", "Les deux"],
total: "Total, neuf articles, deux plaques",
note: [
"Le titre tient sur une plaque pleine de la première encre, ",
"le total sur la seconde",
", et les lignes teintées sur une teinte à huit pour cent de la première. Un \
passage de cette phrase est composé dans chacune des deux.",
],
prose: "Une encre à soi, voilà ce qu'on achète avec une couleur de \
maison : la presse la porte sur une plaque à elle, donc elle descend à la même \
force sur chaque feuille de chaque tirage, ce que quatre encres de \
quadrichromie posées l'une sur l'autre ne peuvent pas promettre. Elle coûte une \
plaque, donc on la dépense sur les marques auxquelles un lecteur reconnaît la \
maison — le nom, le filet dessous, le panneau derrière un titre — et le reste du \
travail est laissé aux encres de quadrichromie. Ce paragraphe est une seule \
coulée de texte, tirée dans la première encre à quatre-vingt-dix pour cent, et \
ses mots ont été mesurés et coupés à la largeur de la liste ci-dessus.",
stamp: "SPÉCIMEN",
quoted_before: "Chiffré en deux encres : ",
quoted_between: " et ",
quoted_after: ", chacune sur sa plaque.",
};
/// 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)];
/// The caption over a ramp of tints: the ink it shows, then what it shows of
/// it.
fn ramp_caption(ink: &str, after: &str) -> String {
format!("{ink}{after}")
}
/// The closing line, which names the two inks the work is quoted in.
fn quoted(words: &Words) -> String {
format!(
"{}{BLUE}{}{GOLD}{}",
words.quoted_before, words.quoted_between, words.quoted_after
)
}
/// 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, 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) in TINT_X.iter().zip(TINTS) {
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, SWATCH_SIZE, *x, y - 11.0, &tint_name(*tint))?;
content.restore_state();
}
content.save_state();
content.set_fill(Rgb::gray(0.2))?;
text(content, font, CAPTION_SIZE, 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,
words: &Words,
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,
SHEET_TITLE_SIZE,
LEFT + 24.0,
bottom + 130.0,
words.sheet_title,
)?;
content.set_fill_components(&[PANEL_TINT])?;
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,
SHEET_LINE_SIZE,
LEFT + 40.0,
bottom + 68.0,
words.sheet_line,
)?;
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 crosshairs(bottom, height) {
registration(content, x, y)?;
}
content.restore_state();
content.save_state();
content.set_fill(Rgb::gray(0.35))?;
text(content, font, MARK_SIZE, LEFT, bottom - 40.0, words.marks)?;
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,
words: &'static Words,
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, [SHADED_TINT]))
.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, CELL_SIZE, label)
.align(align)
.valign(VAlign::Middle)
.color(Rgb::WHITE)
.padding(Padding::symmetric(6.0, 5.0))
};
let [item, sheets, plates, price] = words.headings;
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 (index, (sheets, plates, price)) in ITEMS.iter().enumerate() {
let line = |label: &'static str, align: Align| {
Cell::new(font, CELL_SIZE, label)
.align(align)
.color(Paint::named(blue, [0.95]))
.padding(Padding::symmetric(6.0, 5.0))
};
table.push(
Row::new()
.cell(line(words.items[index], Align::Left))
.cell(line(sheets, Align::Right))
.cell(line(plates.named(words), Align::Left))
.cell(line(price, Align::Right)),
);
}
let total = |label: &str, align: Align| {
Cell::new(font, TOTAL_SIZE, label)
.align(align)
.color(Paint::named(blue, [1.0]))
.padding(Padding::symmetric(6.0, 6.0))
};
table.push(
Row::new()
.cell(total(words.total, 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,
words: &'static Words,
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,
HEAD_SIZE,
LEFT,
HEAD_BASELINE,
words.list_title,
)?;
content.restore_state();
content.save_state();
content.set_fill(Rgb::gray(0.35))?;
for (line, y) in words.list_lead.iter().zip(LEAD_BASELINES) {
text(content, font, LEAD_SIZE, LEFT, y, line)?;
}
content.restore_state();
let table = run_list(font, words, blue, gold)?;
let placed = table.fit(TableFrame::new(LEFT, LIST_TOP, LIST_TOP - 260.0), 0)?;
let height = placed.height();
placed.draw(content)?;
let first_ink = Style::new(font, TOTAL_SIZE).color(Paint::named(blue, [PROSE_TINT]));
let second_ink = Style::new(font, TOTAL_SIZE).color(Paint::named(gold, [1.0]));
let note = RichText::new()
.span(words.note[0], &first_ink)
.span(words.note[1], &second_ink)
.span(words.note[2], &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, PROSE_SIZE)
.color(Paint::named(blue, [PROSE_TINT]))
.leading(PROSE_LEADING);
let prose = flow.break_lines(words.prose, WIDTH);
content.begin_text();
flow.draw(
content,
&prose,
LEFT,
note_top - RichText::height(&lines) - 26.0,
WIDTH,
)?;
content.end_text();
Stamp::new(font, words.stamp)
.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, CLOSING_RULE)?;
content.line_to(LEFT + WIDTH, CLOSING_RULE)?;
content.stroke();
content.set_fill_space(blue);
content.set_fill_components(&[0.55])?;
text(
content,
font,
CLOSING_SIZE,
LEFT,
CLOSING_RULE - 14.0,
"ed(words),
)?;
content.restore_state();
Ok(())
}
fn main() -> std::process::ExitCode {
failure::reported(run())
}
fn run() -> 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("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(BLUE, Cmyk::new(0.92, 0.58, 0.0, 0.06)));
let gold = doc.add_color_space(Separation::ink(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_PLATES,
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,
HEAD_SIZE,
LEFT,
HEAD_BASELINE,
words.title,
)?;
content.save_state();
content.set_fill(Rgb::gray(0.35))?;
for (line, y) in words.lead.iter().zip(LEAD_BASELINES) {
text(&mut content, &font, LEAD_SIZE, LEFT, y, line)?;
}
content.restore_state();
tint_ramp(
&mut content,
&font,
&blue,
640.0,
&ramp_caption(BLUE, words.first_ramp),
)?;
tint_ramp(
&mut content,
&font,
&gold,
530.0,
&ramp_caption(GOLD, words.second_ramp),
)?;
sheet(&mut content, &font, words, &blue, &gold, ®istration_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, words, &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(())
}
#[cfg(test)]
mod tests {
use hqf_pdf::layout::{RichText, Style, TableFrame};
use hqf_pdf::{Cmyk, Document, Font, FontHandle, Paint, Separation, TextFlow};
use super::{
CELL_SIZE, CLOSING_RULE, CLOSING_SIZE, HEAD_SIZE, ITEMS, LEAD_SIZE, LEFT, LIST_TOP,
MARK_SIZE, PANEL_TINT, PROSE_LEADING, PROSE_SIZE, PROSE_TINT, SHADED_TINT, SHEET_LINE_SIZE,
SHEET_TITLE_SIZE, TINTS, TOTAL_SIZE, WIDTH, WORDS, Words, crosshairs, default_font,
language, quoted, ramp_caption, run_list, tint_name,
};
/// The lines two languages are allowed to write the same way. There are
/// none: the names of the inks, the sheet counts and the prices are all
/// held outside the words.
const SPARED: [&str; 0] = [];
/// A4's width, in points, which is the sheet both pages are laid on.
const PAGE_WIDTH: f64 = 595.276;
/// The font every measurement here is taken in, which is the one the
/// example draws with when the command line says nothing.
fn font(document: &mut Document) -> FontHandle {
document.add_font(
Font::parse(std::fs::read(default_font()).expect("the committed font is there"))
.expect("the committed font parses"),
)
}
/// Every line the two sheets draw where they are asked to, each with the
/// size it is set at and the room it has. What is flowed to a width is not
/// here: it is broken, so it cannot run over.
fn lines(words: &'static Words) -> Vec<(String, f64, f64)> {
// The first column of the run list, less what a cell keeps clear on
// each side.
let item_column = WIDTH - 54.0 - 74.0 - 66.0 - 12.0;
let plate_column = 74.0 - 12.0;
// A line that overhangs nothing has only to stay on the sheet; one that
// stands over a block has that block's width.
let sheet = PAGE_WIDTH - LEFT;
let mut measured = vec![
(words.title.to_owned(), HEAD_SIZE, sheet),
(words.list_title.to_owned(), HEAD_SIZE, sheet),
(
ramp_caption(super::BLUE, words.first_ramp),
super::CAPTION_SIZE,
WIDTH,
),
(
ramp_caption(super::GOLD, words.second_ramp),
super::CAPTION_SIZE,
WIDTH,
),
(words.sheet_title.to_owned(), SHEET_TITLE_SIZE, WIDTH - 24.0),
(words.sheet_line.to_owned(), SHEET_LINE_SIZE, WIDTH - 40.0),
(words.marks.to_owned(), MARK_SIZE, WIDTH),
(quoted(words), CLOSING_SIZE, WIDTH),
(words.total.to_owned(), TOTAL_SIZE, item_column),
];
for line in words.lead.iter().chain(&words.list_lead) {
measured.push(((*line).to_owned(), LEAD_SIZE, sheet));
}
for item in words.items {
measured.push((item.to_owned(), CELL_SIZE, item_column));
}
for plate in words.plates {
measured.push((plate.to_owned(), CELL_SIZE, plate_column));
}
for (index, heading) in words.headings.into_iter().enumerate() {
let room = if index == 0 {
item_column
} else {
plate_column
};
measured.push((heading.to_owned(), CELL_SIZE, room));
}
measured
}
#[test]
fn every_language_writes_the_two_sheets_in_its_own_words() {
let untranslated = language::untranslated_lines(&WORDS, &SPARED);
assert!(
untranslated.is_empty(),
"the sheets say these in more than one language: {untranslated:?}"
);
}
/// Every line above is drawn where it is asked for rather than broken to a
/// width, so one longer than its room runs over the column beside it or off
/// the sheet.
#[test]
fn every_language_writes_lines_that_fit_the_room_they_have() {
let mut document = Document::new();
let font = font(&mut document);
for (named, words) in WORDS {
for (line, size, room) in lines(words) {
let measured = font.measure(&line, size);
assert!(
measured <= room,
"the {} sheet draws {line:?} over {measured:.1} points, and \
it has {room:.1}",
named.code()
);
}
}
}
/// The closing paragraph is flowed, so it cannot run over on the right; it
/// can run down over the rule at the foot of the sheet, and nothing else
/// would say so. The list and the note above it are laid out here rather
/// than guessed at: a language whose list stands taller pushes the
/// paragraph down as surely as a longer paragraph does.
#[test]
fn every_language_keeps_the_closing_paragraph_above_the_rule() {
let mut document = Document::new();
let font = font(&mut document);
let blue = document.add_color_space(Separation::ink(
super::BLUE,
Cmyk::new(0.92, 0.58, 0.0, 0.06),
));
let gold = document.add_color_space(Separation::ink(
super::GOLD,
Cmyk::new(0.0, 0.28, 0.95, 0.04),
));
for (named, words) in WORDS {
let table = run_list(&font, words, &blue, &gold).expect("the columns divide the list");
let list = table
.fit(TableFrame::new(LEFT, LIST_TOP, LIST_TOP - 260.0), 0)
.expect("the list is laid out from its first row")
.height();
let first_ink = Style::new(&font, TOTAL_SIZE).color(Paint::named(&blue, [PROSE_TINT]));
let second_ink = Style::new(&font, TOTAL_SIZE).color(Paint::named(&gold, [1.0]));
let note = RichText::new()
.span(words.note[0], &first_ink)
.span(words.note[1], &second_ink)
.span(words.note[2], &first_ink);
let note_lines = note.break_lines(WIDTH);
let flow = TextFlow::new(&font, PROSE_SIZE).leading(PROSE_LEADING);
let broken = flow.break_lines(words.prose, WIDTH);
let top = LIST_TOP - list - 26.0 - RichText::height(¬e_lines) - 26.0;
let lowest = top - flow.height(&broken);
assert!(
lowest >= CLOSING_RULE,
"the {} paragraph reaches down to {lowest:.1}, and the rule is at \
{CLOSING_RULE:.1}",
named.code()
);
}
}
/// Three captions spell a tint out in words, which no language can work out
/// from the number. Changing one of these settings has to send whoever
/// changes it back to every language.
#[test]
fn the_tints_the_captions_spell_out_are_the_tints_that_are_laid_down() {
assert!(
(PANEL_TINT - 0.15).abs() < f64::EPSILON,
"a caption calls the panel behind the heading fifteen percent"
);
assert!(
(SHADED_TINT - 0.08).abs() < f64::EPSILON,
"the note calls the shaded lines eight percent"
);
assert!(
(PROSE_TINT - 0.9).abs() < f64::EPSILON,
"the closing paragraph calls itself ninety percent"
);
}
/// Three more captions spell a count out in words. Each is pinned to the
/// table that produces it.
#[test]
fn the_counts_the_captions_spell_out_are_the_counts_that_are_drawn() {
assert_eq!(TINTS.len(), 10, "a caption calls the ramp ten tints");
assert_eq!(
crosshairs(0.0, 0.0).len(),
4,
"a line under the sheet calls them the four crosshairs"
);
assert_eq!(ITEMS.len(), 9, "the last row calls the list nine items");
}
/// What a swatch is called is read off the tint it is laid at, so the ten
/// of them run from a tenth to the whole plate.
#[test]
fn a_swatch_is_named_by_the_tint_it_is_laid_at() {
let named: Vec<String> = TINTS.iter().map(|tint| tint_name(*tint)).collect();
assert_eq!(named.first().map(String::as_str), Some("10%"));
assert_eq!(named.last().map(String::as_str), Some("100%"));
}
}
|