write_eval_watermark.py

The Python file of the “The watermark a trial copy carries” example. What you get before you pay anything: a watermark across every page, and the same pages under it, laid out exactly as a paid copy lays them out. This is also the document that says what a licence key changes, and why every figure on this site is measured with one.

Python 109 lines

What this example is for

You want to see what this engine makes before you pay anybody anything. A trial copy lays out the same pages as a paid copy — the same layout, the same tables, the same tables carried on to the next page, the same files made for keeping — and writes a watermark corner to corner across every page. Look at the picture beside this text before you open anything: the watermark climbs across the page, a smaller line under it says what a licence key buys, and underneath sits an ordinary report — a heading, a name, two sentences — laid out exactly as a paid copy lays it. The document below is that trial copy, exactly as it came out.

What this example shows

  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
"""Shows what an evaluation copy looks like: a document written with no licence key,
which the library stamps with a watermark on every page.

The Python twin of the `write_eval_watermark` example in Rust. A licensed document
is what most examples show; this one is the opposite. No key is set, so the library
writes an evaluation copy and marks every page as one. Set a licence key and the
mark is gone.

The page under the mark is held in `Words`, once per language, and `HQF_PDF_LANG`
picks which set is drawn. The watermark itself is the library's, not the page's, and
does not follow the page's language.

Usage: python examples/write_eval_watermark.py [out.pdf] [font.ttf]
       HQF_PDF_LANG=fr python examples/write_eval_watermark.py
"""

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

import _language
import _out

import hqf_pdf

# The size each line is set at, and the baseline it sits on, in the order they are laid
# down.
PLACES = (
    (20.0, 760.0),
    (11.0, 730.0),
    (11.0, 700.0),
    (11.0, 684.0),
    (10.0, 120.0),
    (10.0, 106.0),
)


@dataclass(frozen=True)
class Words:
    """Every word the page draws, in one language.

    The watermark is not here: the library writes it, and it says the same thing
    whatever language the page under it is in.
    """

    # A short document laid under the watermark, and under it a note that says what the
    # watermark is, in the order the lines are laid down.
    lines: tuple[str, str, str, str, str, str]


# The page in English.
ENGLISH = Words(
    lines=(
        "Quarterly report",
        "Prepared for HQF Development",
        "Revenue held steady across the three months, and costs fell as the new",
        "rendering pipeline replaced the tools it was built to retire.",
        "This is an evaluation copy: the library stamps every page with the",
        "watermark above until a licence key is set. Set one and it is gone.",
    ),
)

# The page in French.
FRENCH = Words(
    lines=(
        "Rapport trimestriel",
        "Établi pour HQF Development",
        "Le chiffre d'affaires est resté stable sur les trois mois, et les",
        "coûts ont baissé quand la nouvelle chaîne de rendu a pris la suite.",
        "Ceci est une copie d'évaluation : la bibliothèque marque chaque page",
        "du filigrane ci-dessus tant qu'aucune clé n'est posée. Posez-en une.",
    ),
)


# Every language the example is written in. A language is added by writing its own set
# of words and naming it here.
WORDS = {_language.ENGLISH: ENGLISH, _language.FRENCH: FRENCH}


def main() -> None:
    language = _language.from_environment()
    words = _language.words_of(WORDS, language)

    # A named file is written as named; the default one carries the language, so the two
    # languages do not overwrite each other in `tmp/`.
    out = _out.output_path(Path(_language.file_name("eval.pdf", language)).stem)

    document = hqf_pdf.Document()
    # No key is set: the document is an evaluation copy, and the library marks every
    # page of it with a watermark.
    document.set_license(hqf_pdf.License.evaluation())
    font = document.add_font(hqf_pdf.Font.from_path(_out.font_path()))

    content = hqf_pdf.Content()
    for line, (size, top) in zip(words.lines, PLACES):
        content.draw_text(font, size, 72.0, top, line)

    page = hqf_pdf.Page.a4()
    page.set_content(content)
    document.add_page(page)

    written = document.write(out)
    print(f"wrote {out}: {written} bytes")


if __name__ == "__main__":
    main()