aboutsummaryrefslogtreecommitdiff
path: root/fietsboek/pdf.py
blob: 5ac237b5df8368591bb23b314eb4cf55dd119b25 (plain)
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
"""PDF generation for tracks.

This module implements functionality that renders a PDF overview for a track.
The PDF overview consists of a map using OSM tiles, and a table with the
computed metadata.

PDF generation is done using Typst_ via the `Python bindings`_. Typst provides
layouting and good typography without too much effort from our side. The Typst
file is generated from a Jinja2 template, saved to a temporary directory
together with the track image, and then compiled.

.. _Typst: https://typst.app/
.. _Python bindings: https://pypi.org/project/typst/
"""

import html.parser
import importlib.resources
import io
import logging
import tempfile
from itertools import chain
from pathlib import Path

import jinja2
import matplotlib
import typst
from babel.dates import format_datetime
from babel.numbers import format_decimal
from matplotlib import pyplot as plt
from pyramid.i18n import Localizer
from pyramid.i18n import TranslationString as _

from . import trackmap, util
from .config import TileLayerConfig
from .models import Track
from .models.track import TrackWithMetadata
from .views.tileproxy import TileRequester

LOGGER = logging.getLogger(__name__)
TEMP_PREFIX = "fietsboek-typst-"
IMAGE_WIDTH = 900
IMAGE_HEIGHT = 300
# See https://typst.app/docs/reference/syntax/
TO_ESCAPE = {
    "$",
    "#",
    "[",
    "]",
    "*",
    "_",
    "`",
    "<",
    ">",
    "@",
    "=",
    "-",
    "+",
    "/",
    "\\",
}


class HtmlToTypst(html.parser.HTMLParser):
    """A parser that converts HTML to Typst syntax.

    This is adjusted for the HTML output that the markdown converted produces.
    It will not work for arbitrary HTML.
    """

    # pylint: disable=too-many-branches
    def __init__(self, out):
        super().__init__()
        self.out = out

    def handle_data(self, data):
        self.out.write(typst_escape(data))

    def handle_starttag(self, tag, attrs):
        if tag == "strong":
            self.out.write("#strong[")
        elif tag == "em":
            self.out.write("#emph[")
        elif tag == "a":
            href = ""
            for key, val in attrs:
                if key == "href":
                    href = val
            self.out.write(f"#link({typst_string(href)})[")
        elif tag == "ul":
            self.out.write("#list(")
        elif tag == "ol":
            self.out.write("#enum(")
        elif tag == "li":
            self.out.write("[")
        elif tag == "h1":
            self.out.write("#heading(level: 1)[")
        elif tag == "h2":
            self.out.write("#heading(level: 2)[")
        elif tag == "h3":
            self.out.write("#heading(level: 3)[")
        elif tag == "h4":
            self.out.write("#heading(level: 4)[")
        elif tag == "h5":
            self.out.write("#heading(level: 5)[")
        elif tag == "h6":
            self.out.write("#heading(level: 6)[")

    def handle_endtag(self, tag):
        if tag == "p":
            self.out.write("\n\n")
        elif tag == "strong":
            self.out.write("]")
        elif tag == "em":
            self.out.write("]")
        elif tag == "a":
            self.out.write("]")
        elif tag == "ul":
            self.out.write(")")
        elif tag == "ol":
            self.out.write(")")
        elif tag == "li":
            self.out.write("],")
        elif tag in {"h1", "h2", "h3", "h4", "h5", "h6"}:
            self.out.write("]")


def typst_string(value: str) -> str:
    """Serializes a string to a string that can be embedded in Typst source.

    This wraps the value in quotes, and escapes all characters.

    :param value: The value to serialize.
    :return: The serialized string, ready to be embedded.
    """
    return '"' + "".join(f"\\u{{{ord(char):x}}}" for char in value) + '"'


def typst_escape(value: str) -> str:
    """Escapes Typst markup in the given value.

    :param value: The value to escape.
    :return: The value with formatting characters escaped.
    """
    return "".join("\\" + char if char in TO_ESCAPE else char for char in value)


def md_to_typst(value: str) -> str:
    """Convert Markdown-formatted text to Typst source code.

    :param value: The Markdown source to convert.
    :return: The Typst code.
    """
    html_source = util.safe_markdown(value).unescape()
    buffer = io.StringIO()
    parser = HtmlToTypst(buffer)
    parser.feed(html_source)
    return buffer.getvalue()


def draw_map(track: Track, requester: TileRequester, tile_layer: TileLayerConfig, outfile: Path):
    """Renders the track map.

    :param track: The track.
    :param requester: The requester which is used to retrieve tiles.
    :param tile_layer: The OSM tile layer configuration.
    :param outfile: Path to the output file.
    """
    map_image = trackmap.render(
        track.path(),
        tile_layer,
        requester,
        size=(IMAGE_WIDTH, IMAGE_HEIGHT),
    )
    map_image.save(str(outfile))


def draw_height_profile(track: Track, outfile: Path):
    """Renders the height graph.

    :param track: The track.
    :param outfile: The output file.
    """
    x, y = [], []
    cur = 0.0
    last = None
    for point in track.path().points:
        x.append(cur / 1000)
        y.append(point.elevation)
        if last:
            cur += point.distance(last)
        last = point

    matplotlib.use("pdf")
    fig, ax = plt.subplots(1, 1, figsize=(11, 3))
    ax.plot(x, y)
    ax.grid()
    ax.xaxis.set_major_formatter("{x} km")
    ax.yaxis.set_major_formatter("{x} m")
    fig.savefig(outfile, bbox_inches="tight")


def generate(
    track: Track, requester: TileRequester, tile_layer: TileLayerConfig, localizer: Localizer
) -> bytes:
    """Generate a PDF representation for the given track.

    :param track: The track for which to generate a PDF overview.
    :param requester: The tile requester to render the track map.
    :param tile_layer: The tile layer to use for the track map.
    :param localizer: The localizer.
    :return: The PDF bytes.
    """
    # Yes, we could use f-strings, but I don't like embedding the huge
    # expressions below in f-strings:
    # pylint: disable=consider-using-f-string
    # And this is a false positive for typst.compile:
    # pylint: disable=no-member
    env = jinja2.Environment(
        loader=jinja2.PackageLoader("fietsboek", "pdf-assets"),
        autoescape=False,
    )
    env.filters["typst_escape"] = typst_escape
    env.filters["md_to_typst"] = md_to_typst

    twm = TrackWithMetadata(track)
    template = env.get_template("overview.typ")
    translate = localizer.translate
    locale = localizer.locale_name
    placeholders = {
        "title": track.title or track.date.strftime("%Y-%m-%d %H:%M"),
        "people": [person.name for person in chain([track.owner], track.tagged_people)],
        "table": [
            (translate(_("pdf.table.date")), format_datetime(track.date, locale=locale)),
            (
                translate(_("pdf.table.length")),
                "{} km".format(format_decimal(twm.length / 1000, locale=locale)),
            ),
            (
                translate(_("pdf.table.uphill")),
                "{} m".format(format_decimal(twm.uphill, locale=locale)),
            ),
            (
                translate(_("pdf.table.downhill")),
                "{} m".format(format_decimal(twm.downhill, locale=locale)),
            ),
            (translate(_("pdf.table.moving_time")), str(twm.moving_time)),
            (translate(_("pdf.table.stopped_time")), str(twm.stopped_time)),
            (
                translate(_("pdf.table.max_speed")),
                "{} km/h".format(format_decimal(util.mps_to_kph(twm.max_speed), locale=locale)),
            ),
            (
                translate(_("pdf.table.avg_speed")),
                "{} km/h".format(format_decimal(util.mps_to_kph(twm.avg_speed), locale=locale)),
            ),
        ],
        "description": track.description,
    }

    with tempfile.TemporaryDirectory(prefix=TEMP_PREFIX) as temp_dir_name:
        temp_dir = Path(temp_dir_name)
        LOGGER.debug("New PDF generation in %s", temp_dir)

        font_data = importlib.resources.read_binary("fietsboek", "pdf-assets/Nunito.ttf")
        (temp_dir / "Nunito.ttf").write_bytes(font_data)

        draw_map(track, requester, tile_layer, temp_dir / "mapimage.png")
        LOGGER.debug("%s: map drawn", temp_dir)

        draw_height_profile(track, temp_dir / "height_profile.pdf")
        LOGGER.debug("%s: height profile drawn", temp_dir)

        rendered = template.render(placeholders)
        LOGGER.debug("%s: typst template rendered", temp_dir)

        (temp_dir / "overview.typ").write_text(rendered)
        pdf_bytes = typst.compile(
            str(temp_dir / "overview.typ"),
            font_paths=[str(temp_dir)],
        )
        LOGGER.debug("%s: PDF rendering complete", temp_dir)

        return pdf_bytes


__all__ = ["generate"]