File size: 4,583 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""A dripping bubble's tail of drops, a glow round drawn bubbles, a dashed tremor and a thick cloud of breath.



Run from the repository folder:  python -m pytest tests

"""
import os
import sys

import numpy as np
from PIL import Image
from scipy import ndimage as ndi

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

from comic_lettering import Style, arrange, draw, web  # noqa: E402
from comic_lettering.painting import LINES_LOOKS  # noqa: E402
from comic_lettering.render import SYMBOLS, bubble_shape, symbol_tile  # noqa: E402

SIZE = (440, 360)
NIGHT = (60, 50, 70)
SKY = (226, 210, 196)
BOX = [[60, 40], [220, 40], [220, 110], [60, 110]]


def test_the_new_choices_and_settings():
    # type: () -> None
    assert "dashes" in LINES_LOOKS
    assert SYMBOLS[u"☁"] == "breath" and u"☁" in web.SYMBOLS
    values = web.merge_values({"bubble_glow": 5, "bubble_glow_color": "#ff3a8c"})
    assert values["bubble_glow"] == 1.0 and values["bubble_glow_color"] == "#ff3a8c"
    assert web.merge_values({})["bubble_glow"] == 0.0


def test_a_dripping_bubble_lets_its_tail_fall_as_separate_drops():
    # type: () -> None
    plain = np.asarray(bubble_shape("drip", 140, 40, 30, "none", 0)[0]) > 128
    tailed = np.asarray(bubble_shape("drip", 140, 40, 30, "right", 0)[0]) > 128
    extra = tailed & ~ndi.binary_dilation(plain, iterations=2)
    assert extra.sum() > 100
    _, pieces = ndi.label(extra)
    assert pieces >= 2, "drops, not one solid tail"
    ys, xs = np.nonzero(extra)
    assert xs.min() > np.nonzero(plain)[1].max() - 20, "on the side the tail points to"
    other = np.asarray(bubble_shape("oval", 140, 40, 30, "right", 0)[0]) > 128
    _, solid = ndi.label(other & ~ndi.binary_dilation(np.asarray(bubble_shape("oval", 140, 40, 30, "none", 0)[0]) > 128, iterations=2))
    assert solid == 1, "other shapes keep their pointed tail"


def glowing(strength, color="#ff3a8c", fill="light"):
    # type: (float, str, str) -> np.ndarray
    image = Image.new("RGB", SIZE, NIGHT)
    regions = [{"id": "a", "kind": "text", "fill": fill, "text": "HEY!", "shapes": [{"type": "polygon", "points": BOX}]}]
    return np.asarray(draw(image, arrange(image, regions=regions, style=Style(bubble_glow=strength, bubble_glow_color=color)))).astype(int)


def test_a_drawn_bubble_glows_in_the_color_asked_for():
    # type: () -> None
    none, pink, more = glowing(0.0), glowing(0.8), glowing(0.3)
    assert np.array_equal(none, glowing(0.0, "#00ff00")), "nothing without a strength"
    halo = np.abs(pink - none).sum(axis=2) > 30
    assert halo.sum() > 800
    assert (pink[halo][:, 0] > pink[halo][:, 1] + 40).mean() > 0.6, "pink"
    assert halo.sum() > (np.abs(more - none).sum(axis=2) > 30).sum(), "reaches further when stronger"
    inside = (slice(60, 90), slice(80, 200))
    assert np.array_equal(pink[inside][:, :5], none[inside][:, :5]), "the bubble itself is untouched"
    auto = glowing(0.8, "auto", "blue")
    ring = np.abs(auto - glowing(0.0, "auto", "blue")).sum(axis=2) > 30
    assert (auto[ring][:, 2] > auto[ring][:, 0]).mean() > 0.6, "a blue bubble glows blue by itself"


def test_a_dashed_tremor_runs_beside_the_stroke_in_straight_dashes():
    # type: () -> None
    path = [[100 + 12 * t, 60 + 14 * t] for t in range(21)]

    def lines(look):
        image = Image.new("RGB", SIZE, SKY)
        regions = [{"id": "t", "kind": "lines", "look": look, "shapes": [{"type": "stroke", "points": path, "width": 26}]}]
        return np.abs(np.asarray(draw(image, arrange(image, regions=regions))).astype(int) - np.array(SKY)).sum(axis=2) > 60
    dashes, waves = lines("dashes"), lines("tremor")
    assert dashes.sum() > 40 and not np.array_equal(dashes, waves)
    _, pieces = ndi.label(dashes)
    assert pieces >= 8, "many short dashes"
    ys, xs = np.nonzero(dashes)
    points = np.array(path, dtype=float)
    near = np.array([np.min(np.hypot(points[:, 0] - x, points[:, 1] - y)) for x, y in zip(xs[::5], ys[::5])])
    assert near.max() < 26 * 1.5 and (near > 5).mean() > 0.8, "beside the stroke, not on it"
    assert np.array_equal(dashes, lines("dashes")), "the same every time"


def test_the_breath_cloud_is_fuller_than_the_puff():
    # type: () -> None
    breath = np.asarray(symbol_tile("breath", 96)) > 128
    puff = np.asarray(symbol_tile("puff", 96)) > 128
    assert breath.sum() > puff.sum() * 1.3
    _, pieces = ndi.label(breath)
    assert pieces <= 3, "one big cloud and a trail of a puff or two"