File size: 30,031 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
"""HTTP side of the editor in A1111 / Forge / reForge: settings, sessions and the routes

the React page (ui/) talks to. The core does not import this module; only the host script does.

"""
import base64
import datetime
import io
import json
import math
import os
import re
import sys
import threading
import time
import uuid
from collections import OrderedDict
from typing import Dict, List, Optional, Tuple

from PIL import Image, PngImagePlugin

from .core import PLACEMENTS, arrange, draw, has_alpha, upright
from .fonts import font_names, list_fonts
from .render import BUBBLE_SHAPES, BUBBLE_THEMES, EXTRAS, MOODS, PALETTES, TAILS, Style, symbol_tile
from .render import SYMBOLS as MARK_SHAPES

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SETTINGS_FILE = os.path.join(BASE_DIR, "user_settings.json")
EXTENSION = os.path.basename(BASE_DIR)
SAME_FONT = "same as bubbles"
SFX_FONT = "Bangers"
# Marks that are drawn as shapes; the page offers buttons for them, since a keyboard cannot type them.
SYMBOLS = [
    u"♥", u"♡", u"💕", u"💔", u"💋",
    u"★", u"☆", u"✦", u"✨", u"💥",
    u"💢", u"⚡", u"🔥",
    u"💧", u"💦", u"♨", u"💨",
    u"〃", u"〰", u"🌀", u"💤", u"☁",
    u"♪", u"♫", u"✿", u"🌙", u"☀",
    u"♂", u"♀", u"✖", u"○",
    u"→", u"←", u"↑", u"↓", u"♦", u"♠", u"♣",
    u"😍", u"🙄", u"👅", u"🤤", u"📳", u"🌬", u"✹", u"💓",
]
# The same marks by what they are for, as the page's picker shows them.
MARK_GROUPS = [
    ("Love", [u"♥", u"♡", u"💕", u"💔", u"💋", u"💓"]),
    ("Shine", [u"★", u"☆", u"✦", u"✨", u"💥", u"☀", u"🌙"]),
    ("Feelings and weather", [u"💢", u"⚡", u"🔥", u"💧", u"💦", u"♨", u"💨", u"☁", u"〃", u"〰", u"🌀", u"💤", u"♪", u"♫",
                              u"✿"]),
    ("Signs", [u"♂", u"♀", u"✖", u"○", u"→", u"←", u"↑", u"↓", u"♦", u"♠", u"♣"]),
    ("Faces and motion", [u"😍", u"🙄", u"👅", u"🤤", u"📳", u"🌬", u"✹"]),
]
MARK_MASK_PX = 48  # the side of a mark's mask
_marks_cache = None  # type: Optional[List[Dict[str, str]]]


def _check_groups(shapeless):
    # type: (List[str]) -> None
    """Say so when the groups and the list of marks have drifted apart. Never an error: the

    page still works with the marks that are in both."""
    grouped = set(char for _, chars in MARK_GROUPS for char in chars)
    offered = set(SYMBOLS)
    if grouped == offered and not shapeless:
        return

    def named(chars):
        # Code points, since a console may not be able to print the characters themselves.
        return ", ".join("U+%04X" % ord(char) for char in sorted(chars)) or "none"

    sys.stderr.write("comic_lettering: MARK_GROUPS and SYMBOLS differ. Only in the groups: %s. Only in SYMBOLS: %s. "
                     "Without a shape: %s.\n" % (named(grouped - offered), named(offered - grouped), named(shapeless)))


def _marks():
    # type: () -> List[Dict[str, str]]
    """Every mark the page offers, in the order of MARK_GROUPS: its character, the name of its

    shape, its group, and a mask of the shape exactly as the renderer draws it.



    The mask is a PNG as a data: address, white where the shape is and clear elsewhere, so the

    page can paint it in any color (CSS mask-image goes by the alpha). Made once and kept."""
    global _marks_cache
    if _marks_cache is not None:
        return _marks_cache
    marks = []  # type: List[Dict[str, str]]
    shapeless = []  # type: List[str]
    for group, chars in MARK_GROUPS:
        for char in chars:
            try:
                name = MARK_SHAPES[char]
                tile = symbol_tile(name, MARK_MASK_PX)
            except KeyError:
                shapeless.append(char)
                continue
            picture = Image.new("RGBA", tile.size, (255, 255, 255, 0))
            picture.putalpha(tile)
            buffer = io.BytesIO()
            picture.save(buffer, "PNG", optimize=True)
            marks.append({"symbol": char, "name": name, "group": group,
                          "mask": "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode("ascii")})
    _check_groups(shapeless)
    _marks_cache = marks
    return marks

# The look, remembered between sessions.
DEFAULTS = {
    "placement": "auto", "font": "", "spill": True, "uppercase": True, "erase": True,
    "bubble_size": 9.0, "bubble_min_size": 1.5, "free_size": 5.5, "text_color": "auto",
    "text_color2": "auto", "sfx_color2": "auto",  # bottom of a gradient from text_color / sfx_color; "auto": none
    "gradient_angle": 0.0,                        # of every gradient, degrees: 0 top to bottom, 90 left to right
    "caption_bubble_color": "auto", "caption_bubble_color2": "auto",  # the fill of the caption's bubble, and its gradient
    "caption_tail_angle": 45.0,                   # the tail "custom": 0 points right, 90 down
    "drip": 0.0, "caption_bubble_glow": 0.0,      # drips from the letters; a glow round the caption's bubble
    "bubble_glow": 0.0, "bubble_glow_color": "auto",  # a glow round the bubbles drawn by hand, and its color
    "sfx_glow": 0.0, "sfx_glow_color": "auto",    # a glow round the letters of a sound effect, and its color
    "extras_color": "auto",                       # the color of the marks around a caption or an effect
    "stroke_color": "auto", "stroke_width": 0.12, "shadow": 0.5, "jitter": 0.3, "arc": 0.0,
    "bubble_stroke_color": "auto", "bubble_stroke_width": 0.0,  # 0: no outline, the look of before
    "rotation": 0.0, "soften": 0.4, "grain": 0.0, "opacity": 1.0, "margin": 0.08,
    "white_threshold": 120, "seed": 0,
    "sfx_font": "", "sfx_size": 7.0, "sfx_color": "auto", "sfx_tilt": 15.0, "sfx_opacity": 0.95,
    "sfx_seed": 0,
    "caption_font": SAME_FONT, "bubble_colors": "auto", "caption_colors": "auto",
    "sfx_colors": "auto", "bubble_mood": "calm", "caption_mood": "calm", "sfx_mood": "shout",
    "caption_extras": "none", "sfx_extras": "none", "caption_bubble": "none",
    "caption_bubble_theme": "light", "caption_tail": "none", "size_jitter": 0.0,
    "line_tilt": 0.0, "line_shift": 0.0, "rough": 0.0, "line_spacing": 1.08,
}
# The range of each number: the editor's sliders and the ComfyUI node's inputs have the same,
# and a value outside it (from a hand-edited file or a call to the route) is brought back into it.
LIMITS = {
    "bubble_size": (1.0, 30.0), "bubble_min_size": (0.5, 10.0), "margin": (0.0, 0.3), "free_size": (1.0, 30.0),
    "stroke_width": (0.0, 0.5), "shadow": (0.0, 1.0), "sfx_size": (1.0, 30.0), "sfx_tilt": (0.0, 45.0),
    "sfx_opacity": (0.1, 1.0), "jitter": (0.0, 1.0), "size_jitter": (0.0, 1.0), "rough": (0.0, 1.0),
    "line_tilt": (0.0, 15.0), "line_shift": (0.0, 1.0), "line_spacing": (0.7, 1.6), "arc": (-1.0, 1.0),
    "rotation": (-45.0, 45.0), "opacity": (0.1, 1.0), "soften": (0.0, 3.0), "grain": (0.0, 1.0),
    "bubble_stroke_width": (0.0, 0.3), "white_threshold": (40, 255),
    "gradient_angle": (-180.0, 180.0), "caption_tail_angle": (0.0, 360.0), "drip": (0.0, 1.0), "caption_bubble_glow": (0.0, 1.0),
    "sfx_glow": (0.0, 1.0), "bubble_glow": (0.0, 1.0),
}
# Lists a saved value must belong to; anything else falls back to the default.
CHOICES = {
    "placement": PLACEMENTS, "bubble_colors": list(PALETTES), "caption_colors": list(PALETTES),
    "sfx_colors": list(PALETTES), "bubble_mood": list(MOODS), "caption_mood": list(MOODS),
    "sfx_mood": list(MOODS), "caption_extras": list(EXTRAS), "sfx_extras": list(EXTRAS),
    "caption_bubble": BUBBLE_SHAPES, "caption_bubble_theme": BUBBLE_THEMES,
    "caption_tail": list(TAILS),
}
# What is lettered onto every new image while it is generated, when switched on.
AUTO_DEFAULTS = {"enabled": False, "text": "", "caption": "", "sfx": ""}
AUTO_KEY = "_auto"

_lock = threading.Lock()
_sessions = OrderedDict()  # type: OrderedDict
MAX_SESSIONS = 6
# The images of the last generation before any lettering, per tab ("txt2img", "img2img").
LAST_CLEAN = {}  # type: Dict[str, List[Image.Image]]
# Counts the generations per tab, so the page can tell an image it already has from a new one.
GENERATION = {}  # type: Dict[str, int]


def begin_generation(tab):
    """Forget the images of the previous generation of a tab."""
    LAST_CLEAN[tab] = []
    GENERATION[tab] = GENERATION.get(tab, 0) + 1


# ---------------------------------------------------------------- settings

def clean_values(values):
    """Make every value usable: known fonts, known list entries, the right number types."""
    fonts = font_names()
    if values.get("font") not in fonts:
        values["font"] = fonts[0]
    if values.get("sfx_font") not in fonts:
        values["sfx_font"] = SFX_FONT if SFX_FONT in fonts else fonts[0]
    if values.get("caption_font") not in fonts:
        values["caption_font"] = SAME_FONT
    for key, allowed in CHOICES.items():
        if values.get(key) not in allowed:
            values[key] = DEFAULTS[key]
    for key in ("seed", "sfx_seed", "white_threshold"):
        values[key] = int(values.get(key) or 0)
    for key, (low, high) in LIMITS.items():
        value = values[key]
        if not math.isfinite(value):  # NaN or infinity: JSON can carry both
            value = DEFAULTS[key]
        values[key] = type(DEFAULTS[key])(max(low, min(high, value)))
    return values


def merge_values(given):
    """The defaults overlaid with what is given, as far as the types agree."""
    values = dict(DEFAULTS)
    for key in DEFAULTS:
        if key not in (given or {}):
            continue
        value = given[key]
        if isinstance(DEFAULTS[key], bool):
            if isinstance(value, bool):
                values[key] = value
        elif isinstance(DEFAULTS[key], float):
            if isinstance(value, (int, float)) and not isinstance(value, bool):
                values[key] = float(value)
        elif isinstance(DEFAULTS[key], int):
            if isinstance(value, (int, float)) and not isinstance(value, bool):
                values[key] = int(value)
        elif isinstance(value, type(DEFAULTS[key])) or isinstance(value, str):
            values[key] = value
    return clean_values(values)


def read_layout(text):
    # type: (str) -> Tuple[Dict, List]
    """What was placed and drawn by hand, from JSON text: (pins, regions). Anything that is

    not an object with a "pins" object and a "regions" list gives {} and [] for that part."""
    try:
        data = json.loads(text) if text else {}
    except (TypeError, ValueError):
        data = {}
    if not isinstance(data, dict):
        data = {}
    pins = data.get("pins") if isinstance(data.get("pins"), dict) else {}
    regions = data.get("regions") if isinstance(data.get("regions"), list) else []
    return pins, regions


def _read_file():
    try:
        with io.open(SETTINGS_FILE, encoding="utf-8") as handle:
            saved = json.load(handle)
        return saved if isinstance(saved, dict) else {}
    except (IOError, OSError, ValueError):
        return {}


def load_settings():
    return merge_values(_read_file())


def load_auto():
    auto = dict(AUTO_DEFAULTS)
    saved = _read_file().get(AUTO_KEY)
    if isinstance(saved, dict):
        auto["enabled"] = bool(saved.get("enabled"))
        for key in ("text", "caption", "sfx"):
            if isinstance(saved.get(key), str):
                auto[key] = saved[key]
    return auto


def save_settings(values, auto=None):
    """Write the look and, when given, what is lettered onto new images.



    The file is written beside itself and moved into place, so a crash half way leaves the

    old settings, not half of the new ones (which would read as none). Raises OSError when

    it cannot be written: the page tells the person, and the settings are not saved."""
    with _lock:
        data = dict(merge_values(values))
        data[AUTO_KEY] = auto if auto is not None else load_auto()
        pending = SETTINGS_FILE + ".tmp"
        with io.open(pending, "w", encoding="utf-8") as handle:
            handle.write(json.dumps(data, indent=2, sort_keys=True))
        os.replace(pending, SETTINGS_FILE)


def make_style(s):
    style = Style()
    for key, value in s.items():
        if key in ("placement", "spill", "erase", "white_threshold"):
            continue  # arguments of arrange(), not style options
        if hasattr(style, key):
            setattr(style, key, value)
    if s["caption_font"] == SAME_FONT:
        style.caption_font = ""
    return style


def make_scene(image, text, caption, numbers, s, sfx="", pins=None, regions=None):
    return arrange(image, text, caption=caption, spill=s["spill"], placement=s["placement"],
                   style=make_style(s), erase=s["erase"], white_threshold=s["white_threshold"],
                   numbers=numbers, sfx=sfx, pins=pins, regions=regions)


def describe(info):
    # type: (Dict) -> Tuple[str, bool]
    """What was done with an image: (message, is it a warning)."""
    message = "Bubbles found: %d, filled: %d%s%s." % (
        info.get("bubbles_found", 0), info.get("bubbles_filled", 0),
        ", plus a caption" if info.get("free_text") else "",
        ", sound effects: %d" % info["sfx"] if info.get("sfx") else "")
    for key, label in (("censored", "censored"), ("decorated", "tones, lines and frames")):
        if info.get(key):
            message = message[:-1] + ", %s: %d." % (label, info[key])
    notes = list(info.get("warnings") or [])  # what the core could not do as asked, in its words
    if info.get("characters") is False:
        notes.append("Character detection is off (no ultralytics or no person_yolov8*-seg.pt"
                     " found), so text may land on a character.")
    return " ".join([message] + notes), bool(notes)


def log_warnings(info):
    # type: (Dict) -> None
    """Put the core's warnings on the server's error stream, for the hosts that letter with

    no page to show them on: automatic mode, the ComfyUI node, the command line."""
    for note in info.get("warnings") or []:
        sys.stderr.write("comic_lettering: %s\n" % note)


# ---------------------------------------------------------------- sessions

def new_session(image, name=""):
    """Keep an image to letter; returns the id the page refers to it by."""
    info = image.info.get("parameters") if isinstance(image.info, dict) else None
    session = {"id": uuid.uuid4().hex, "image": image.convert("RGBA" if has_alpha(image) else "RGB"), "result": None,
               "name": name,
               "parameters": info if isinstance(info, str) else None, "version": 0, "png": {},
               "rendering": threading.Lock()}  # one render of this image at a time, see render_session
    with _lock:
        _sessions[session["id"]] = session
        while len(_sessions) > MAX_SESSIONS:
            _sessions.popitem(last=False)
    return session


def get_session(session_id):
    with _lock:
        session = _sessions.get(session_id)
        if session is not None:
            _sessions.move_to_end(session_id)
        return session


def render_session(session, text, caption, sfx, numbers, settings, pins=None, regions=None):
    """Letter the session's image; returns what the page needs to show the result.



    pins, regions: what was placed and drawn by hand on the page, see arrange().

    """
    s = merge_values(settings)
    # Renders of one image take turns, in the order they came. Side by side, a slow older one
    # that finished last would put its picture over the newer one's, under the newer one's version.
    with session["rendering"]:
        scene = make_scene(session["image"], text or "", caption or "", bool(numbers), s, sfx or "", pins, regions)
        result = draw(session["image"], scene)
        with _lock:
            session["result"] = None if scene.get("empty") else result
            session["version"] += 1
            session["png"].pop("result", None)
            version = session["version"]
    message, warning = describe(scene["info"])
    if scene["info"].get("regions"):
        # The count belongs to the first sentence; whatever describe() added comes after it.
        head, dot, notes = message.partition(".")
        message = "%s, areas filled: %d%s%s" % (head, scene["info"]["regions"], dot, notes)
    if scene.get("empty"):
        message, warning = "No text: this is the original image.", False
    return {"version": version, "status": message, "warning": warning, "empty": bool(scene.get("empty")),
            "info": scene["info"], "items": scene["items"], "bubbles": scene.get("bubbles", [])}


def session_png(session, which):
    """PNG bytes of the session's original or lettered image, encoded once."""
    image = session["image"] if which == "original" or session["result"] is None else session["result"]
    key = "original" if image is session["image"] else "result"
    with _lock:
        cached = session["png"].get(key)
    if cached is None:
        buffer = io.BytesIO()
        image.save(buffer, "PNG", compress_level=1)
        cached = buffer.getvalue()
        with _lock:
            session["png"][key] = cached
    return cached


def save_session(session, folder):
    """Write the lettered image (or the original, if nothing is lettered) into a folder."""
    image = session["result"] or session["image"]
    os.makedirs(folder, exist_ok=True)
    stem = re.sub(r"[^A-Za-z0-9_-]+", "_", os.path.splitext(session["name"] or "")[0]).strip("_")[:40]
    name = "lettered-%s%s.png" % (datetime.datetime.now().strftime("%Y%m%d-%H%M%S"), "-" + stem if stem else "")
    path = os.path.normpath(os.path.join(folder, name))
    extra = None
    if session["parameters"]:
        extra = PngImagePlugin.PngInfo()
        extra.add_text("parameters", session["parameters"])
    image.save(path, "PNG", pnginfo=extra)
    return path


def result_image(session_id):
    # type: (str) -> Optional[Image.Image]
    session = get_session(session_id or "")
    if session is None:
        return None
    return session["result"] or session["image"]


# ---------------------------------------------------------------- routes

def options_payload():
    fonts = font_names()
    return {
        "fonts": fonts, "sameFont": SAME_FONT, "palettes": list(PALETTES), "moods": list(MOODS),
        "extras": list(EXTRAS), "shapes": list(BUBBLE_SHAPES), "themes": list(BUBBLE_THEMES),
        "tails": list(TAILS), "placements": list(PLACEMENTS), "symbols": SYMBOLS, "marks": _marks(),
        "defaults": merge_values({}), "settings": load_settings(), "auto": load_auto(),
    }


def ui_html(base=None):
    """The built page with every relative asset address turned into an address of the host.



    base: where the host serves ui/dist from; the A1111 file route by default.

    A fresh ?v= on each call keeps the browser from showing an old build.

    """
    with io.open(os.path.join(BASE_DIR, "ui", "dist", "index.html"), encoding="utf-8") as handle:
        html = handle.read()
    base = base or "/file=extensions/%s/ui/dist" % EXTENSION
    stamp = str(int(time.time()))

    def swap(match):
        return "%s=%s%s/%s?v=%s%s" % (match.group("attr"), match.group("q"), base, match.group("path")[2:],
                                       stamp, match.group("q"))

    return re.sub(r'(?P<attr>\b(?:src|href))=(?P<q>["\'])(?P<path>\./[^"\']+)(?P=q)', swap, html)


MISSING_PAGE = "<p>Comic Lettering: ui/dist is missing. Build the page (npm run build in ui/).</p>"
GONE = "The image is gone from memory. Load it again."

# What each route answers, as (status, payload), free of any web framework: the A1111
# family serves them through FastAPI, ComfyUI through aiohttp.


def answer_settings(body):
    auto = None
    if isinstance(body.get("auto"), dict):
        auto = dict(AUTO_DEFAULTS)
        auto["enabled"] = bool(body["auto"].get("enabled"))
        for key in ("text", "caption", "sfx"):
            if isinstance(body["auto"].get(key), str):
                auto[key] = body["auto"][key]
    try:
        save_settings(body.get("settings") or {}, auto)
    except OSError as error:  # IOError is the same class
        return 500, {"error": "Could not save the settings: %s" % error}
    return 200, {"ok": True}


def answer_upload(data, name):
    if not data:
        return 400, {"error": "no image"}
    try:
        image = Image.open(io.BytesIO(data))
        image.load()
    except Exception:
        return 400, {"error": "not an image"}
    info = image.info
    image = upright(image)
    image.info.update(info)  # the generation text of a PNG is kept, whatever the turn did to the info
    session = new_session(image, name)
    return 200, {"session": session["id"], "width": image.width, "height": image.height}


def answer_last(body):
    """A session from the clean copy of an image of the last generation of a tab (or of a ComfyUI node)."""
    tab = str(body.get("tab") or "txt2img")
    images = LAST_CLEAN.get(tab) or []
    index, count = body.get("index"), body.get("count")
    if not isinstance(index, int) or not isinstance(count, int):
        return 404, {"error": "no such image"}
    # A gallery with more images than were generated starts with their grid.
    index -= max(0, count - len(images))
    if index < 0 or index >= len(images):
        return 404, {"error": "no such image"}
    # The same image as the page already has: it keeps its session and what was done there.
    origin = "%s:%d:%d" % (tab, GENERATION.get(tab, 0), index)
    if body.get("known") == origin and get_session(str(body.get("session") or "")) is not None:
        return 200, {"same": True, "origin": origin}
    image = images[index]
    session = new_session(image, "generation")
    return 200, {"session": session["id"], "width": image.width, "height": image.height, "origin": origin}


def answer_render(body):
    session = get_session(str(body.get("session") or ""))
    if session is None:
        return 404, {"error": GONE}
    try:
        return 200, render_session(session, body.get("text"), body.get("caption"), body.get("sfx"), body.get("numbers"),
                                   body.get("settings") or {}, body.get("pins"), body.get("regions"))
    except Exception as error:  # report it on the page instead of a bare 500
        return 500, {"error": "%s: %s" % (type(error).__name__, error)}


def answer_save(body, saver, tab):
    session = get_session(str(body.get("session") or ""))
    if session is None:
        return 404, {"error": GONE}
    try:
        if saver:
            path = saver(session["result"] or session["image"], session["parameters"], tab)
        else:
            path = save_session(session, os.path.join(BASE_DIR, "saved"))
    except Exception as error:  # the host's own saving can fail in its own ways: say so on the page
        return 500, {"error": "Could not save: %s" % error}
    return 200, {"path": os.path.normpath(str(path))}


def register(app, saver=None):
    """Add the routes to the FastAPI app of an A1111-family host.



    saver(image, parameters, tab) saves an image the way the host saves its own and returns

    the file's path. Without one, images go into a "saved" folder beside this package.

    """
    from fastapi import Request
    from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, Response
    from starlette.concurrency import run_in_threadpool

    def reply(answer):
        return JSONResponse(answer[1], status_code=answer[0])

    @app.get("/comic_lettering/ui")
    async def page():
        try:
            return HTMLResponse(ui_html(), headers={"Cache-Control": "no-store"})
        except (IOError, OSError):
            return HTMLResponse(MISSING_PAGE, status_code=500)

    @app.get("/comic_lettering/options")
    async def options():
        return JSONResponse(await run_in_threadpool(options_payload), headers={"Cache-Control": "no-store"})

    @app.get("/comic_lettering/font")
    async def font(name: str = ""):
        path = list_fonts().get(name)
        if not path or not os.path.isfile(path):
            return reply((404, {"error": "unknown font"}))
        return FileResponse(path, headers={"Cache-Control": "max-age=86400"})

    @app.post("/comic_lettering/settings")
    async def settings(request: Request):
        return reply(await run_in_threadpool(answer_settings, await request.json()))

    @app.post("/comic_lettering/session")
    async def upload(request: Request, name: str = ""):
        return reply(answer_upload(await request.body(), name))

    @app.post("/comic_lettering/session/last")
    async def last(request: Request):
        return reply(answer_last(await request.json()))

    @app.get("/comic_lettering/image/{session_id}/{which}")
    async def image_bytes(session_id: str, which: str):
        session = get_session(session_id)
        if session is None or which not in ("original", "result"):
            return reply((404, {"error": "unknown image"}))
        data = await run_in_threadpool(session_png, session, which)
        return Response(data, media_type="image/png", headers={"Cache-Control": "no-store"})

    @app.post("/comic_lettering/render")
    async def render(request: Request):
        return reply(await run_in_threadpool(answer_render, await request.json()))

    @app.post("/comic_lettering/save")
    async def save(request: Request):
        body = await request.json()
        tab = "img2img" if body.get("tab") == "img2img" else "txt2img"
        return reply(await run_in_threadpool(answer_save, body, saver, tab))


def register_aiohttp(routes, saver=None):
    """Add the same routes to an aiohttp route table: ComfyUI's PromptServer.instance.routes.



    The page's files are served from ui/dist by a route of their own, so they are not in the

    folder ComfyUI loads node scripts from (it would run the page's script on its own page).

    """
    import asyncio
    import mimetypes

    from aiohttp import web as aio

    static = "/comic_lettering/static"
    dist = os.path.realpath(os.path.join(BASE_DIR, "ui", "dist"))

    def reply(answer):
        return aio.json_response(answer[1], status=answer[0])

    async def threaded(function, *args):
        return await asyncio.get_running_loop().run_in_executor(None, function, *args)

    async def body_of(request):
        try:
            body = await request.json()
        except Exception:
            body = None
        return body if isinstance(body, dict) else {}

    @routes.get("/comic_lettering/ui")
    async def page(request):
        try:
            return aio.Response(text=ui_html(static), content_type="text/html", headers={"Cache-Control": "no-store"})
        except (IOError, OSError):
            return aio.Response(text=MISSING_PAGE, content_type="text/html", status=500)

    @routes.get(static + "/{path:.*}")
    async def files(request):
        path = os.path.realpath(os.path.join(dist, request.match_info["path"]))
        if not path.startswith(dist + os.sep) or not os.path.isfile(path):
            return reply((404, {"error": "no such file"}))
        kind = mimetypes.guess_type(path)[0] or "application/octet-stream"
        if path.endswith(".js"):
            kind = "text/javascript"  # Windows sometimes has this as text/plain, and a module then will not load
        with open(path, "rb") as handle:
            return aio.Response(body=handle.read(), content_type=kind, headers={"Cache-Control": "no-cache"})

    @routes.get("/comic_lettering/options")
    async def options(request):
        return aio.json_response(await threaded(options_payload), headers={"Cache-Control": "no-store"})

    @routes.get("/comic_lettering/font")
    async def font(request):
        path = list_fonts().get(request.query.get("name", ""))
        if not path or not os.path.isfile(path):
            return reply((404, {"error": "unknown font"}))
        return aio.FileResponse(path, headers={"Cache-Control": "max-age=86400"})

    @routes.post("/comic_lettering/settings")
    async def settings(request):
        return reply(await threaded(answer_settings, await body_of(request)))

    @routes.post("/comic_lettering/session")
    async def upload(request):
        return reply(answer_upload(await request.read(), request.query.get("name", "")))

    @routes.post("/comic_lettering/session/last")
    async def last(request):
        return reply(answer_last(await body_of(request)))

    @routes.get("/comic_lettering/image/{session_id}/{which}")
    async def image_bytes(request):
        session = get_session(request.match_info["session_id"])
        which = request.match_info["which"]
        if session is None or which not in ("original", "result"):
            return reply((404, {"error": "unknown image"}))
        data = await threaded(session_png, session, which)
        return aio.Response(body=data, content_type="image/png", headers={"Cache-Control": "no-store"})

    @routes.post("/comic_lettering/render")
    async def render(request):
        return reply(await threaded(answer_render, await body_of(request)))

    @routes.post("/comic_lettering/save")
    async def save(request):
        body = await body_of(request)
        return reply(await threaded(answer_save, body, saver, str(body.get("tab") or "")))