"""Black torn balloons, marks in a color of your own, the scream mood and two lettering fonts. 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, BUBBLE_THEMES, MOODS, Style, arrange, draw, letter, web # noqa: E402 from comic_lettering.core import BLACK_BODY, _regions # noqa: E402 from comic_lettering.fonts import list_fonts, missing_glyphs # noqa: E402 SIZE = (400, 300) GRAY = (120, 120, 120) PINK = "#ff8fc7" MIN_PIXELS = 40 def base(): # type: () -> Image.Image return Image.new("RGB", SIZE, GRAY) def black_balloon(shape="torn", **more): # type: (str, **object) -> np.ndarray style = Style(caption_bubble=shape, caption_bubble_theme="black", caption_colors="custom", text_color="#ffffff", **more) return np.asarray(letter(base(), caption="THAT'S IT!\nDIG IN!", placement="center", style=style)[0]).astype(int) def count(picture, color, reach=12): # type: (np.ndarray, tuple, int) -> int return int((np.abs(picture - np.array(color)).sum(axis=2) < reach).sum()) def test_torn_is_a_shape_and_the_black_theme_a_choice(): # type: () -> None assert "torn" in BUBBLE_SHAPES and "black" in BUBBLE_THEMES and "scream" in MOODS values = web.merge_values({"caption_bubble": "torn", "caption_bubble_theme": "black", "caption_bubble_mood": "calm"}) assert values["caption_bubble"] == "torn" and values["caption_bubble_theme"] == "black" assert web.merge_values({"caption_mood": "scream"})["caption_mood"] == "scream" def test_a_black_balloon_is_black_with_light_letters_and_no_light_outline(): # type: () -> None picture = black_balloon() assert count(picture, BLACK_BODY) > 1500, "a black body" assert count(picture, (255, 255, 255), 60) > MIN_PIXELS, "white letters" ring = picture[(np.abs(picture - np.array(BLACK_BODY)).sum(axis=2) < 12)] assert ring.size > 0 # No light outline round it: the pixels beside the black body are the art's, not a white line. dark = (np.abs(picture - np.array(BLACK_BODY)).sum(axis=2) < 12) edge = np.zeros_like(dark) edge[1:-1, 1:-1] = ~dark[1:-1, 1:-1] & (dark[:-2, 1:-1] | dark[2:, 1:-1] | dark[1:-1, :-2] | dark[1:-1, 2:]) next_to_it = picture[edge & (picture.sum(axis=2) > 700)] assert len(next_to_it) < 0.02 * edge.sum(), "a white outline round the balloon" def test_a_torn_balloon_has_a_longer_edge_than_a_box(): # type: () -> None def travel(picture): # How far the left edge, row by row, and the top edge, column by column, travel in all: a straight side hardly moves. dark = (np.abs(picture - np.array(BLACK_BODY)).sum(axis=2) < 12) lefts = [int(np.argmax(row)) for row in dark if row.sum() > 30] tops = [int(np.argmax(column)) for column in dark.T if column.sum() > 30] return sum(abs(a - b) for a, b in zip(lefts, lefts[1:])) + sum(abs(a - b) for a, b in zip(tops, tops[1:])) assert travel(black_balloon("torn")) > 2 * travel(black_balloon("box")) assert not np.array_equal(black_balloon("torn"), black_balloon("torn", seed=3)), "the tearing follows the seed" def test_marks_take_a_color_of_your_own(): # type: () -> None look = dict(caption_bubble="jagged", caption_extras="hearts", free_size=9) plain = np.asarray(letter(base(), caption="GLKH!", placement="center", style=Style(**look))[0]).astype(int) pink = np.asarray(letter(base(), caption="GLKH!", placement="center", style=Style(extras_color=PINK, **look))[0]).astype(int) target = (255, 143, 199) assert count(pink, target) > MIN_PIXELS and count(plain, target) == 0 auto = np.asarray(letter(base(), caption="GLKH!", placement="center", style=Style(extras_color="auto", **look))[0]).astype(int) assert np.array_equal(plain, auto), "auto is the picture of before" sound = np.asarray(letter(base(), sfx="BAM", style=Style(sfx_extras="sparkles", extras_color=PINK))[0]).astype(int) assert count(sound, target) > 0 or count(sound, target, 40) > 0, "the marks of an effect too" def test_the_scream_mood_is_a_preset_of_its_own(): # type: () -> None calm = np.asarray(letter(base(), caption="HERRK!!", placement="center", style=Style(caption_mood="calm", free_size=12))[0]) scream = np.asarray(letter(base(), caption="HERRK!!", placement="center", style=Style(caption_mood="scream", free_size=12))[0]) assert not np.array_equal(calm, scream) for key in ("jitter", "size_jitter", "line_tilt", "line_shift", "rough"): assert MOODS["scream"][key] > 0 def test_a_painted_area_can_be_black_with_no_outline(): # type: () -> None square = [[100, 80], [300, 80], [300, 220], [100, 220]] regions = [{"id": "a", "kind": "text", "fill": "black", "text": "DIG IN!", "shapes": [{"type": "polygon", "points": square}]}] assert _regions(regions)[0]["fill"] == "black" scene = arrange(base(), regions=regions) shown = np.asarray(draw(base(), scene)).astype(int) assert count(shown, BLACK_BODY) > 15000 assert count(shown, (255, 255, 255), 60) > MIN_PIXELS, "light letters on it" around = shown[70:78, 150:250] assert (around.sum(axis=2) < 700).all(), "no white outline round it" def test_the_two_lettering_fonts_ship_with_latin_and_a_license(): # type: () -> None fonts = list_fonts() for name in ("Knewave", "Sedgwick Ave Display"): assert name in fonts, name assert missing_glyphs(name, u"SHUT UP! I'M FREE? 0123456789") == "", name