File size: 3,123 Bytes
55306cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""A label that points with an arrow instead of a tail, like the notes beside a figure on a page.



Run from the repository folder:  python -m pytest tests

"""
import os
import sys

import numpy as np
from PIL import Image

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from comic_lettering import BUBBLE_SHAPES, Style, letter, web  # noqa: E402
from comic_lettering.render import bubble_shape  # noqa: E402

SIZE = (440, 300)
SKY = (226, 210, 196)
PAPER = (250, 250, 250)


def label(tail, **more):
    # type: (str, **object) -> np.ndarray
    style = Style(caption_bubble="label", caption_tail=tail, free_size=6, caption_colors="custom", text_color="#111111",
                  stroke_width=0.0, shadow=0.0, font="PT Sans", caption_font="PT Sans", **more)
    return np.asarray(letter(Image.new("RGB", SIZE, SKY), caption="BED HEAD", placement="center", style=style)[0]).astype(int)


def changed(picture):
    # type: (np.ndarray) -> np.ndarray
    return np.abs(picture - np.array(SKY)).sum(axis=2) > 30


def test_label_is_a_choice_in_every_list():
    # type: () -> None
    assert "label" in BUBBLE_SHAPES
    assert web.merge_values({"caption_bubble": "label"})["caption_bubble"] == "label"


def extent(tail, tail_angle=0.0):
    # type: (str, float) -> tuple
    """The fill of a label as (left, right, top, bottom) of what is drawn; the masks of every tail share a size."""
    fill = np.asarray(bubble_shape("label", 120, 30, 30, tail, 0, tail_angle)[0]) > 128
    ys, xs = np.nonzero(fill)
    return int(xs.min()), int(xs.max()), int(ys.min()), int(ys.max())


def test_the_arrow_leaves_the_box_on_the_side_it_is_asked_for():
    # type: () -> None
    base = extent("none")
    for tail, side in (("left", 0), ("right", 1), ("up", 2), ("down", 3)):
        shown = extent(tail)
        reach = [base[0] - shown[0], shown[1] - base[1], base[2] - shown[2], shown[3] - base[3]]
        assert reach[side] > 40, (tail, reach)
        assert max(value for index, value in enumerate(reach) if index != side) <= 20, (tail, reach)  # "left" and "right" slant a little downward


def test_the_arrow_has_a_head_wider_than_its_shaft():
    # type: () -> None
    mask = np.asarray(bubble_shape("label", 120, 30, 30, "right", 0)[0]) > 128
    ys, xs = np.nonzero(mask)
    tip = xs.max()
    head, shaft = mask[:, tip - 20].sum(), mask[:, tip - 45].sum()
    assert shaft > 0 and head > shaft * 2, (head, shaft)
    assert head < mask[:, int(xs.min()) + 5].sum(), "the arrow is narrower than the box"


def test_a_label_has_the_look_of_a_box_with_an_outline_and_a_custom_angle_works():
    # type: () -> None
    shown = label("down-right")
    assert (np.abs(shown - np.array(PAPER)).sum(axis=2) < 30).sum() > 1500, "a light fill"
    assert (shown.sum(axis=2) < 120).sum() > 100, "an outline and text"
    right, left = extent("custom", 0.0), extent("custom", 180.0)
    assert right[1] > left[1] + 40 and left[0] < right[0] - 40
    assert np.array_equal(label("left"), label("left")), "the same every time"