Un contrat de prestation de deux pages

Un contrat de prestation sur deux pages, terminé par des champs de signature, de nom et de date que le lecteur remplit.

Rust write_contract.rs 819 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
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
//! Draws a services agreement two parties sign in a reader: a letterhead, the
//! parties, numbered clauses set as flowing paragraphs, and a signature block
//! with a signature field, a printed name and a date for each side.
//!
//! The clauses are prose, so the page is laid out by measuring: each clause is
//! broken to the column, and a clause that would not fit under what is already
//! on the page starts the next one instead, heading and body together. The
//! number of pages is whatever that comes to, and the footer of every page says
//! which of them it is.
//!
//! The fields at the foot are what makes the page an agreement rather than a
//! picture of one: a reader signs the signature field, types the name and the
//! date into the two boxes under it, and every field carries a border so the
//! boxes to click are plain to see.
//!
//! Every word the page draws is held in `Words`, once per language, and
//! `HQF_PDF_LANG` picks which one is drawn. The parties' names and addresses,
//! their registration numbers, the agreement's number, the day rate and the
//! names the form fields answer to are the same in both, and the French runs to
//! its own number of pages.
//!
//! Usage: `cargo run --example write_contract -- tmp/contract.pdf [font.ttf]`
//!        `HQF_PDF_LANG=fr cargo run --example write_contract -- tmp/contrat.pdf`

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

use hqf_pdf::content::Content;
use hqf_pdf::cos::Name;
use hqf_pdf::layout::{Align, RichText, Style, TextFlow};
use hqf_pdf::{
    Document, Field, Font, FontHandle, FormField, Image, ImageHandle, Page, Rgb, SignatureField,
};

#[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 logo the letterhead carries: the committed fixture, so the example runs
/// on any machine. It stands in for the provider's mark.
fn logo_path() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("images")
        .join("hqf_development.png")
}

/// A4, in points.
const PAGE_WIDTH: f64 = 595.276;
const PAGE_HEIGHT: f64 = 841.89;
/// The page's left edge and, mirrored, its right.
const LEFT: f64 = 56.0;
const RIGHT: f64 = PAGE_WIDTH - LEFT;
/// The width every paragraph is broken to.
const BODY_WIDTH: f64 = PAGE_WIDTH - 2.0 * LEFT;

/// The ink the headings and rules are drawn in, and the grey of the small
/// print.
const ACCENT: Rgb = Rgb {
    r: 0.11,
    g: 0.33,
    b: 0.55,
};
const MUTED: Rgb = Rgb {
    r: 0.42,
    g: 0.42,
    b: 0.45,
};
const INK: Rgb = Rgb {
    r: 0.1,
    g: 0.1,
    b: 0.12,
};

/// The size the clause bodies are set at, and the distance from one of their
/// baselines to the next.
const BODY_SIZE: f64 = 9.5;
const LEADING: f64 = 13.5;
/// The drop from a clause heading's baseline to the top of its body, and the
/// gap left under a clause before the next one starts.
const HEADING_DROP: f64 = 5.0;
const CLAUSE_GAP: f64 = 19.0;

/// Where the clauses start on the pages after the first. On the first they
/// start under the preamble, wherever the preamble ends.
const NEXT_TOP: f64 = 782.0;
/// The lowest a clause or a block may reach before it moves to the next page.
const BOTTOM: f64 = 100.0;
/// The room the signature block needs under the last clause.
const SIGNATURE_HEIGHT: f64 = 250.0;

/// The colour and width every field's border is stroked in.
const BORDER: Rgb = Rgb::gray(0.55);
const BORDER_WIDTH: f64 = 0.75;

/// The width of a signature column, and the left edge of the second one.
const COLUMN_WIDTH: f64 = 224.0;
const COLUMN_TWO: f64 = 315.276;

/// What the agreement is called.
const NUMBER: &str = "HQF-2026-0087";

/// How many clauses the agreement is made of.
const CLAUSE_COUNT: usize = 9;

/// The party that supplies the services and the party that buys them: the name,
/// the two lines under it, and the tax number the third line carries.
const PROVIDER_NAME: &str = "HQF Development";
const PROVIDER_LINES: [&str; 2] = [
    "42 lot les Genêts, 13480 Calas, France",
    "SIRET 752 492 777 00012 · APE 6202A",
];
const PROVIDER_VAT: &str = "FR59 752 492 777";
const CLIENT_NAME: &str = "Northwind Trading SARL";
const CLIENT_LINES: [&str; 2] = [
    "18 quai du Port, 13002 Marseille, France",
    "RCS Marseille 812 665 431",
];
const CLIENT_VAT: &str = "FR41 812 665 431";

/// Who signs, in the order their columns are drawn, and the name the column's
/// fields answer to. A field name is what a form-filling program reads, not
/// something the page draws, so it is the same in every language.
const SIGNATORIES: [(&str, &str, f64); 2] = [
    ("Olivier Pons", "Provider", LEFT),
    ("Claire Lemoine", "Client", COLUMN_TWO),
];

/// Every word the page draws, in one language.
///
/// What is not language stays out of it: the parties' names and addresses,
/// their registration numbers, the agreement's number, the day rate and the
/// names the form fields answer to are drawn from constants of their own and
/// read the same in every language.
#[derive(Debug)]
struct Words {
    /// The words the document's title is built from, and the word set large at
    /// the head of the first page.
    title: &'static str,
    heading: &'static str,
    /// What stands before the agreement's number.
    number_label: &'static str,
    /// What stands before the day it was signed, and that day.
    signed_label: &'static str,
    agreement_date: &'static str,
    /// What stands before the day it runs from, and that day.
    effective_label: &'static str,
    effective_date: &'static str,
    /// The heading over each party.
    provider_heading: &'static str,
    client_heading: &'static str,
    /// The word before a tax number, wherever one is set.
    vat: &'static str,
    /// The sentence that binds the parties, cut where the two dates go into it.
    preamble_first: &'static str,
    preamble_second: &'static str,
    preamble_after: &'static str,
    /// The clauses the agreement is made of: a heading, and the paragraph under
    /// it.
    clauses: [(&'static str, &'static str); CLAUSE_COUNT],
    /// The heading over the signature block, and the sentence under it, cut
    /// where the three things a reader fills in are named.
    signatures: &'static str,
    note_before: &'static str,
    note_marked: &'static str,
    note_after: &'static str,
    /// The heading over each signature column, and what each signatory does.
    signatory_headings: [&'static str; 2],
    signatory_roles: [&'static str; 2],
    /// What stands under each of a column's three boxes.
    signature_label: &'static str,
    printed_name_label: &'static str,
    date_label: &'static str,
    /// The words the page number is built from, at the foot of every page.
    page_before: &'static str,
    page_word: &'static str,
    page_of: &'static str,
}

impl Words {
    /// The words the agreement is written in, in `language`.
    fn of(language: Language) -> &'static Self {
        language::pick(&WORDS, language)
    }
}

/// The agreement in English.
const ENGLISH: Words = Words {
    title: "Services agreement",
    heading: "AGREEMENT",
    number_label: "Agreement No.",
    signed_label: "Signed",
    agreement_date: "20 July 2026",
    effective_label: "Effective",
    effective_date: "1 September 2026",
    provider_heading: "SERVICE PROVIDER",
    client_heading: "CLIENT",
    vat: "VAT",
    preamble_first: "This agreement is made on",
    preamble_second: "between the parties named above, and takes effect on",
    preamble_after: ". It sets out the terms on which the Provider supplies \
         software development and support services to the Client. Each clause \
         below binds both parties from the date the last of them signs.",
    clauses: [
        (
            "Scope of services",
            "The Provider designs, builds and maintains the software described in \
             the statement of work annexed to this agreement, together with any \
             further work the parties agree to in writing. A change of scope is \
             agreed in writing before it is carried out, and the Provider may \
             decline a change that cannot be delivered within the agreed term.",
        ),
        (
            "Term and commencement",
            "This agreement begins on the effective date shown above and runs for \
             twelve months. It renews for successive periods of twelve months \
             unless either party gives written notice of at least sixty days \
             before the end of the period then running.",
        ),
        (
            "Fees and payment",
            "The Client pays the Provider 620.00 EUR for each day of work, before \
             value added tax. The Provider invoices monthly in arrears and each \
             invoice falls due thirty days after its issue date. A sum still \
             unpaid on the due date carries interest at the statutory rate, and no \
             reminder has to be sent for that interest to run.",
        ),
        (
            "Intellectual property",
            "Once an invoice has been paid in full, the Client owns the source \
             code and the documents written for it under this agreement. The \
             Provider keeps the tools, libraries and generic components it brought \
             to the work or wrote for general use, and grants the Client a \
             perpetual non-exclusive licence to use them as part of the delivered \
             software.",
        ),
        (
            "Confidentiality",
            "Each party keeps in confidence what it learns of the other's \
             business, customers and technology, and uses it only to perform this \
             agreement. That duty outlives the agreement by five years. It does \
             not cover what is already public through no fault of the party that \
             received it, nor what a court or a regulator requires be disclosed.",
        ),
        (
            "Warranties and liability",
            "The Provider warrants that the work is performed with the skill and \
             care of a competent professional, and corrects at its own cost any \
             defect reported within ninety days of delivery. Neither party is \
             liable for loss of profit, loss of data or any indirect loss, and the \
             liability of each party under this agreement is capped at the fees \
             paid over the twelve months before the claim arose.",
        ),
        (
            "Termination",
            "Either party may end this agreement on sixty days' written notice, \
             and either may end it at once if the other commits a material breach \
             that is not put right within thirty days of being asked in writing to \
             put it right. On termination the Client pays for the work done up to \
             that date and the Provider hands over the deliverables in the state \
             they are then in.",
        ),
        (
            "Notices",
            "Notice under this agreement is given in writing to the address shown \
             for the party at the head of this document, and takes effect on the \
             day it is delivered. A party that moves tells the other within \
             fifteen days.",
        ),
        (
            "Entire agreement and governing law",
            "This document, with the statement of work annexed to it, is the whole \
             of what the parties have agreed on this subject and replaces every \
             earlier understanding of it. The agreement is governed by French law, \
             and any dispute the parties cannot settle between them goes to the \
             courts of Aix-en-Provence, France.",
        ),
    ],
    signatures: "SIGNATURES",
    note_before: "Each party signs in the boxes below: a ",
    note_marked: "signature, a printed name and a date",
    note_after: ", filled in from a reader. The agreement takes effect on the \
         later of the two dates written here, and a copy of the signed \
         document goes to each party.",
    signatory_headings: ["FOR THE PROVIDER", "FOR THE CLIENT"],
    signatory_roles: ["Director", "Managing Director"],
    signature_label: "Signature",
    printed_name_label: "Printed name",
    date_label: "Date (DD/MM/YYYY)",
    page_before: "Agreement",
    page_word: "page",
    page_of: "of",
};

/// The agreement in French.
const FRENCH: Words = Words {
    title: "Contrat de prestation de services",
    heading: "CONTRAT",
    number_label: "Contrat n°",
    signed_label: "Signé le",
    agreement_date: "20 juillet 2026",
    effective_label: "Prise d'effet",
    effective_date: "1er septembre 2026",
    provider_heading: "PRESTATAIRE",
    client_heading: "CLIENT",
    vat: "TVA",
    preamble_first: "Le présent contrat est conclu le",
    preamble_second: "entre les parties désignées ci-dessus et prend effet le",
    preamble_after: ". Il fixe les conditions dans lesquelles le Prestataire \
         fournit au Client des prestations de développement et de maintenance \
         logicielle. Chaque article ci-dessous engage les deux parties à \
         compter de la date de la dernière signature.",
    clauses: [
        (
            "Objet du contrat",
            "Le Prestataire conçoit, développe et maintient le logiciel décrit \
             dans le cahier des charges annexé au présent contrat, ainsi que \
             toute prestation complémentaire convenue par écrit entre les \
             parties. Toute modification de l'objet est convenue par écrit avant \
             son exécution, et le Prestataire peut refuser une modification qui \
             ne peut être livrée dans le délai convenu.",
        ),
        (
            "Durée et prise d'effet",
            "Le présent contrat prend effet à la date indiquée ci-dessus et court \
             pour douze mois. Il se renouvelle par périodes successives de douze \
             mois, sauf dénonciation écrite de l'une des parties adressée au \
             moins soixante jours avant le terme de la période en cours.",
        ),
        (
            "Rémunération",
            "Le Client verse au Prestataire 620.00 EUR par journée de travail, \
             hors taxes. Le Prestataire facture chaque mois à terme échu et \
             chaque facture est payable trente jours après sa date d'émission. \
             Toute somme impayée à l'échéance porte intérêt au taux légal, sans \
             qu'une mise en demeure soit nécessaire.",
        ),
        (
            "Propriété intellectuelle",
            "Dès le paiement intégral de la facture correspondante, le Client \
             devient propriétaire du code source et des documents rédigés pour \
             lui au titre du présent contrat. Le Prestataire conserve les outils, \
             bibliothèques et composants génériques qu'il a apportés ou écrits \
             pour un usage général, et concède au Client une licence perpétuelle \
             et non exclusive de les utiliser au sein du logiciel livré.",
        ),
        (
            "Confidentialité",
            "Chaque partie tient confidentiel ce qu'elle apprend de l'activité, \
             de la clientèle et de la technologie de l'autre, et ne l'utilise que \
             pour l'exécution du présent contrat. Cette obligation survit cinq \
             ans au contrat. Elle ne couvre ni ce qui est déjà public sans faute \
             de la partie qui l'a reçu, ni ce dont un tribunal ou une autorité \
             exige la communication.",
        ),
        (
            "Garanties et responsabilité",
            "Le Prestataire garantit que les travaux sont exécutés avec le soin \
             et la compétence d'un professionnel avisé, et corrige à ses frais \
             tout défaut signalé dans les quatre-vingt-dix jours suivant la \
             livraison. Aucune des parties ne répond de la perte d'exploitation, \
             de la perte de données ni d'aucun dommage indirect, et la \
             responsabilité de chaque partie au titre du présent contrat est \
             plafonnée aux sommes versées au cours des douze mois précédant le \
             fait générateur.",
        ),
        (
            "Résiliation",
            "Chaque partie peut résilier le présent contrat moyennant un préavis \
             écrit de soixante jours, et chacune peut y mettre fin immédiatement \
             si l'autre commet un manquement grave auquel elle ne remédie pas \
             dans les trente jours d'une mise en demeure écrite. À la \
             résiliation, le Client règle les travaux exécutés jusqu'à cette date \
             et le Prestataire remet les livrables en l'état où ils se trouvent.",
        ),
        (
            "Notifications",
            "Toute notification au titre du présent contrat est faite par écrit à \
             l'adresse indiquée pour la partie en tête du présent document, et \
             prend effet le jour de sa remise. La partie qui change d'adresse en \
             informe l'autre dans les quinze jours.",
        ),
        (
            "Intégralité du contrat et loi applicable",
            "Le présent document, avec le cahier des charges qui lui est annexé, \
             constitue l'intégralité de l'accord des parties sur son objet et \
             remplace tout accord antérieur portant sur celui-ci. Le contrat est \
             régi par la loi française, et tout litige que les parties ne peuvent \
             régler entre elles relève des tribunaux d'Aix-en-Provence, France.",
        ),
    ],
    signatures: "SIGNATURES",
    note_before: "Chaque partie signe dans les cases ci-dessous : une ",
    note_marked: "signature, un nom en clair et une date",
    note_after: ", saisis depuis un lecteur. Le contrat prend effet à la plus \
         tardive des deux dates portées ici, et un exemplaire signé est remis à \
         chaque partie.",
    signatory_headings: ["POUR LE PRESTATAIRE", "POUR LE CLIENT"],
    signatory_roles: ["Gérant", "Directrice générale"],
    signature_label: "Signature",
    printed_name_label: "Nom en clair",
    date_label: "Date (JJ/MM/AAAA)",
    page_before: "Contrat",
    page_word: "page",
    page_of: "sur",
};

/// 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 a line of text, left-aligned, in a colour of its own.
fn text(content: &mut Content, font: &FontHandle, size: f64, x: f64, y: f64, color: Rgb, s: &str) {
    let _ = content.set_fill(color);
    content.begin_text();
    let _ = content.set_font(font.name(), size);
    let _ = content.text_origin(x, y);
    content.show_glyphs(&font.glyphs(s));
    content.end_text();
}

/// Draws a line of text whose right edge sits at `right`.
fn text_right(
    content: &mut Content,
    font: &FontHandle,
    size: f64,
    right: f64,
    y: f64,
    color: Rgb,
    s: &str,
) {
    text(
        content,
        font,
        size,
        right - font.measure(s, size),
        y,
        color,
        s,
    );
}

/// A horizontal rule from `LEFT` to `RIGHT` at height `y`.
fn rule(content: &mut Content, y: f64, width: f64, color: Rgb) -> Result<(), hqf_pdf::Error> {
    content.set_stroke(color)?;
    content.set_line_width(width)?;
    content.move_to(LEFT, y)?;
    content.line_to(RIGHT, y)?;
    content.stroke();
    Ok(())
}

/// Draws the letterhead: the mark, the provider's name, what the page is, and
/// the agreement's number and dates, closed off by a rule.
fn letterhead(
    content: &mut Content,
    font: &FontHandle,
    logo: &ImageHandle,
    words: &Words,
) -> Result<(), hqf_pdf::Error> {
    let (logo_w, logo_h) = logo.fit_within(84.0, 84.0)?;
    content.draw_image(logo, LEFT, 826.5 - logo_h, logo_w, logo_h)?;

    text(
        content,
        font,
        8.5,
        LEFT,
        768.0,
        MUTED,
        "42 lot les Genêts, 13480 Calas, France",
    );
    text(content, font, 8.5, LEFT, 756.0, MUTED, "www.hqf.fr");

    text_right(content, font, 26.0, RIGHT, 796.0, ACCENT, words.heading);
    text_right(
        content,
        font,
        9.5,
        RIGHT,
        770.0,
        INK,
        &format!("{}  {NUMBER}", words.number_label),
    );
    text_right(
        content,
        font,
        9.5,
        RIGHT,
        757.0,
        MUTED,
        &format!("{}  {}", words.signed_label, words.agreement_date),
    );
    text_right(
        content,
        font,
        9.5,
        RIGHT,
        744.0,
        MUTED,
        &format!("{}  {}", words.effective_label, words.effective_date),
    );

    rule(content, 730.0, 1.2, ACCENT)
}

/// Draws the two parties, side by side, and the sentence that binds them.
/// Answers with the height the first clause may start at, which is under
/// however many lines the sentence came to.
fn parties(content: &mut Content, font: &FontHandle, words: &Words) -> Result<f64, hqf_pdf::Error> {
    let columns = [
        (
            words.provider_heading,
            PROVIDER_NAME,
            PROVIDER_LINES,
            PROVIDER_VAT,
            LEFT,
        ),
        (
            words.client_heading,
            CLIENT_NAME,
            CLIENT_LINES,
            CLIENT_VAT,
            COLUMN_TWO,
        ),
    ];

    for (heading, name, lines, vat, x) in columns {
        text(content, font, 8.0, x, 712.0, MUTED, heading);
        text(content, font, 11.5, x, 696.0, INK, name);
        let mut y = 681.0;
        for line in lines {
            text(content, font, 8.5, x, y, MUTED, line);
            y -= 11.5;
        }
        text(
            content,
            font,
            8.5,
            x,
            y,
            MUTED,
            &format!("{} {vat}", words.vat),
        );
    }

    // The date the agreement runs from is underlined mid-sentence, which is
    // what rich text is for.
    let body = Style::new(font, BODY_SIZE);
    let marked = Style::new(font, BODY_SIZE).underline();
    let preamble = RichText::new()
        .align(Align::Justify)
        .push(
            format!(
                "{} {} {} ",
                words.preamble_first, words.agreement_date, words.preamble_second
            ),
            &body,
        )
        .push(words.effective_date, &marked)
        .push(words.preamble_after, &body);

    let preamble_top = 632.0;
    let lines = preamble.break_lines(BODY_WIDTH);
    content.set_fill(INK)?;
    preamble.draw(content, &lines, LEFT, preamble_top, BODY_WIDTH)?;

    Ok(preamble_top - RichText::height(&lines) - 24.0)
}

/// Draws the signature block from `top` down: a heading, a sentence saying what
/// a reader does with it, and a column for each party carrying a signature
/// field, a box for the printed name and a box for the date. The fields are
/// pushed onto `fields`, for the page they land on.
fn signatures(
    content: &mut Content,
    font: &FontHandle,
    words: &Words,
    top: f64,
    fields: &mut Vec<Field>,
) -> Result<(), hqf_pdf::Error> {
    text(content, font, 10.5, LEFT, top, ACCENT, words.signatures);

    let body = Style::new(font, 9.0);
    let marked = Style::new(font, 9.0).underline();
    let note = RichText::new()
        .align(Align::Justify)
        .push(words.note_before, &body)
        .push(words.note_marked, &marked)
        .push(words.note_after, &body);
    let note_top = top - HEADING_DROP;
    let lines = note.break_lines(BODY_WIDTH);
    content.set_fill(INK)?;
    note.draw(content, &lines, LEFT, note_top, BODY_WIDTH)?;

    let block_top = note_top - RichText::height(&lines) - 28.0;
    let columns = SIGNATORIES
        .iter()
        .zip(words.signatory_headings)
        .zip(words.signatory_roles);
    for (((name, field_name, x), heading), role) in columns {
        text(content, font, 8.0, *x, block_top, MUTED, heading);
        text(
            content,
            font,
            9.5,
            *x,
            block_top - 14.0,
            INK,
            &format!("{name} — {role}"),
        );

        let signature_bottom = block_top - 84.0;
        fields.push(
            SignatureField::new(
                format!("{field_name} signature"),
                *x,
                signature_bottom,
                COLUMN_WIDTH,
                48.0,
            )
            .border(BORDER, BORDER_WIDTH)
            .into(),
        );
        text(
            content,
            font,
            7.5,
            *x,
            signature_bottom - 10.0,
            MUTED,
            words.signature_label,
        );

        // The printed name and the date are text fields, so what a reader types
        // is read back as text and not as a picture of a hand.
        for (label, field_label, bottom) in [
            (
                words.printed_name_label,
                "printed name",
                signature_bottom - 42.0,
            ),
            (words.date_label, "date", signature_bottom - 84.0),
        ] {
            fields.push(
                FormField::new(
                    format!("{field_name} {field_label}"),
                    *x,
                    bottom,
                    COLUMN_WIDTH,
                    18.0,
                    font.name(),
                    10.0,
                )
                .border(BORDER, BORDER_WIDTH)
                .into(),
            );
            text(content, font, 7.5, *x, bottom - 10.0, MUTED, label);
        }
    }

    Ok(())
}

/// Draws the foot of one page: a rule, what the law asks the provider to carry,
/// and which page of how many this is.
fn footer(
    content: &mut Content,
    font: &FontHandle,
    words: &Words,
    number: usize,
    total: usize,
) -> Result<(), hqf_pdf::Error> {
    rule(content, 78.0, 0.5, Rgb::gray(0.8))?;

    let mentions = format!(
        "HQF Development · 42 lot les Genêts, 13480 Calas, France · \
        SIRET 752 492 777 00012 · APE 6202A · {} {PROVIDER_VAT}",
        words.vat
    );
    let mentions_x = LEFT + (BODY_WIDTH - font.measure(&mentions, 7.0)) / 2.0;
    text(content, font, 7.0, mentions_x, 66.0, MUTED, &mentions);

    let label = format!(
        "{} {NUMBER} — {} {number} {} {total}",
        words.page_before, words.page_word, words.page_of
    );
    let label_x = LEFT + (BODY_WIDTH - font.measure(&label, 7.0)) / 2.0;
    text(content, font, 7.0, label_x, 55.0, MUTED, &label);
    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 path = args
        .next()
        .unwrap_or_else(|| language.file_name(&out::default_path("contract")));
    let font_path = args.next().map_or_else(default_font, PathBuf::from);

    let mut doc = Document::new();
    doc.set_license(licence::licensed());
    doc.set_info(Name::new("Title"), &format!("{} {NUMBER}", words.title));

    let font = doc.add_font(Font::parse(fs::read(&font_path)?)?);
    let logo = doc.add_image(Image::parse(fs::read(logo_path())?)?);

    let mut content = Content::new();
    letterhead(&mut content, &font, &logo, words)?;
    let mut top = parties(&mut content, &font, words)?;

    // Every clause is measured before it is drawn, and one that would run past
    // the foot of the page starts the next one whole: a heading is never left
    // behind on a page away from the paragraph it introduces.
    let mut finished: Vec<Content> = Vec::new();
    let flow = TextFlow::new(&font, BODY_SIZE)
        .align(Align::Justify)
        .leading(LEADING);

    for (index, (heading, clause)) in words.clauses.iter().enumerate() {
        let lines = flow.break_lines(clause, BODY_WIDTH);
        let height = flow.height(&lines);
        if top - (HEADING_DROP + height + CLAUSE_GAP) < BOTTOM {
            finished.push(std::mem::replace(&mut content, Content::new()));
            top = NEXT_TOP;
        }

        let numbered = format!("{}. {heading}", index + 1);
        text(&mut content, &font, 10.5, LEFT, top, ACCENT, &numbered);

        let body_top = top - HEADING_DROP;
        content.set_fill(INK)?;
        content.begin_text();
        flow.draw(&mut content, &lines, LEFT, body_top, BODY_WIDTH)?;
        content.end_text();

        top = body_top - height - CLAUSE_GAP;
    }

    if top - SIGNATURE_HEIGHT < BOTTOM {
        finished.push(std::mem::replace(&mut content, Content::new()));
        top = NEXT_TOP;
    }
    let mut fields = Vec::new();
    signatures(&mut content, &font, words, top - 10.0, &mut fields)?;
    finished.push(content);

    // The footer says which page of how many, so it is drawn once the count is
    // known, and the fields go on the page the signature block landed on.
    let total = finished.len();
    for (index, mut content) in finished.into_iter().enumerate() {
        footer(&mut content, &font, words, index + 1, total)?;

        let mut page = Page::new(PAGE_WIDTH, PAGE_HEIGHT);
        page.content = content.into_bytes();
        if index + 1 == total {
            page.fields = std::mem::take(&mut fields);
        }
        doc.add_page(page)?;
    }

    let bytes = doc.to_bytes()?;
    if let Some(parent) = Path::new(&path).parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(&path, &bytes)?;

    println!("wrote {path}: {} bytes, {total} pages", bytes.len());
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{WORDS, language};

    /// The lines two languages are allowed to write the same way, each with
    /// what makes the two the same word.
    const SPARED: [&str; 4] = [
        // The party that buys the services is a client in both.
        r#"client_heading: "CLIENT""#,
        // A block of signatures is headed by the same word in both.
        r#"signatures: "SIGNATURES""#,
        // What a hand puts in the first box is a signature in both.
        r#"signature_label: "Signature""#,
        // A page is a page in both.
        r#"page_word: "page""#,
    ];

    #[test]
    fn every_language_draws_the_agreement_in_its_own_words() {
        let untranslated = language::untranslated_lines(&WORDS, &SPARED);

        assert!(
            untranslated.is_empty(),
            "the agreement says these in more than one language: {untranslated:?}"
        );
    }
}