File size: 3,443 Bytes
f5403c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c38786
 
f5403c7
 
 
 
 
 
 
 
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
from __future__ import annotations

from pathlib import Path

from PIL import Image, ImageDraw, ImageFont

from .config import AgePreset
from .generator import DotLayout


def _load_font(size: int) -> ImageFont.ImageFont:
    try:
        return ImageFont.truetype("DejaVuSans.ttf", size=size)
    except OSError:
        return ImageFont.load_default()


def _fit_canvas(source: Image.Image, target_size, padding=220):
    source = source.convert("L")
    canvas = Image.new("RGB", target_size, "white")
    inner_width = target_size[0] - (padding * 2)
    inner_height = target_size[1] - (padding * 2)
    fitted = source.copy()
    fitted.thumbnail((inner_width, inner_height))
    x = (target_size[0] - fitted.width) // 2
    y = (target_size[1] - fitted.height) // 2
    return canvas, fitted, (x, y)


def render_activity_page(
    source: Image.Image,
    layout: DotLayout,
    preset: AgePreset,
    title: str,
    output_path: Path,
    include_reference: bool,
    page_pixel_size: tuple[int, int],
) -> Path:
    canvas, fitted, origin = _fit_canvas(source, target_size=page_pixel_size)
    draw = ImageDraw.Draw(canvas)
    font = _load_font(preset.font_size)
    title_font = _load_font(42)

    if include_reference:
        faint = fitted.convert("RGB")
        faint = Image.eval(faint, lambda px: 245 if px > 180 else 210)
        canvas.paste(faint, origin)

    sx = fitted.width / layout.source_size[0]
    sy = fitted.height / layout.source_size[1]

    draw.text((160, 100), title, fill="black", font=title_font)
    draw.text((160, 150), "Ligue os pontos em ordem.", fill="black", font=font)

    for index, (x, y) in enumerate(layout.points, start=1):
        px = int(origin[0] + x * sx)
        py = int(origin[1] + y * sy)
        radius = preset.dot_radius
        draw.ellipse((px - radius, py - radius, px + radius, py + radius), fill="white", outline="black", width=2)
        tx = px + radius + 4
        ty = py - preset.font_size
        draw.text((tx, ty), str(index), fill="black", font=font)

    canvas.save(output_path, dpi=(300, 300))
    return output_path


def render_answer_page(
    source: Image.Image,
    layout: DotLayout,
    preset: AgePreset,
    title: str,
    output_path: Path,
    page_pixel_size: tuple[int, int],
) -> Path:
    canvas, fitted, origin = _fit_canvas(source, target_size=page_pixel_size)
    draw = ImageDraw.Draw(canvas)
    font = _load_font(preset.font_size)
    title_font = _load_font(42)

    outline = fitted.convert("RGB")
    outline = Image.eval(outline, lambda px: 255 if px > 210 else 225)
    canvas.paste(outline, origin)

    sx = fitted.width / layout.source_size[0]
    sy = fitted.height / layout.source_size[1]
    scaled_points = [(int(origin[0] + x * sx), int(origin[1] + y * sy)) for x, y in layout.points]

    draw.text((160, 100), f"{title} - Resposta", fill="black", font=title_font)
    if len(scaled_points) > 1:
        line_points = scaled_points + [scaled_points[0]] if layout.closed else scaled_points
        draw.line(line_points, fill="black", width=4, joint="curve")

    for index, (px, py) in enumerate(scaled_points, start=1):
        radius = max(5, preset.dot_radius - 2)
        draw.ellipse((px - radius, py - radius, px + radius, py + radius), fill="black")
        draw.text((px + radius + 3, py - preset.font_size), str(index), fill="black", font=font)

    canvas.save(output_path, dpi=(300, 300))
    return output_path