# Architecture Comic Lettering is one core with two thin host layers on top and one editor page shared by both. - [The layers](#the-layers) - [Repository layout](#repository-layout) - [The life of a render](#the-life-of-a-render) - [Where state lives](#where-state-lives) - [Host layer: AUTOMATIC1111 / Forge / reForge](#host-layer-automatic1111--forge--reforge) - [Host layer: ComfyUI](#host-layer-comfyui) - [The editor page](#the-editor-page) - [Design decisions](#design-decisions) ## The layers ```mermaid flowchart TB subgraph hosts["Hosts"] A1111["A1111 / Forge / reForge
scripts/comic_lettering_script.py
javascript/comic_lettering.js"] Comfy["ComfyUI
__init__.py, comfy_nodes.py
comfy_web/comic_lettering.js"] end subgraph shared["Shared by both hosts"] UI["Editor page (React)
ui/src, built into ui/dist"] Web["HTTP layer
comic_lettering/web.py"] Core["Core
comic_lettering/core.py, detect.py,
render.py, painting.py, people.py, fonts.py"] end A1111 -- "iframe + postMessage" --> UI Comfy -- "iframe + postMessage" --> UI UI -- "fetch /comic_lettering/*" --> Web A1111 -- "register(app, saver)" --> Web Comfy -- "register_aiohttp(routes, saver)" --> Web A1111 -- "arrange / draw
(automatic mode)" --> Core Comfy -- "arrange / draw
(the node)" --> Core Web --> Core ``` | Layer | Depends on | Knows nothing about | |---|---|---| | **Core** | `numpy`, `scipy`, `Pillow`; optionally `ultralytics` | HTTP, any host, the editor | | **HTTP layer** | the core | which host serves it: every route is a framework-free `answer_*` function, registered once for FastAPI and once for aiohttp | | **Editor page** | the HTTP routes and five `postMessage` types | which host it runs in, apart from one flag (`comfy`) | | **Host layers** | their host's extension API | each other | The rule that keeps it this way: **the core never imports `web`, and nothing in `comic_lettering/` imports a host module.** A host module is imported only inside a function that a host calls. ## Repository layout ``` sd-comic-lettering/ ├── comic_lettering/ the core package (host-independent) │ ├── core.py letter(), arrange(), draw(); placement of every kind of text │ ├── detect.py finding bubbles, erasing letters, edge energy │ ├── render.py fonts to pixels: layout, marks, palettes, moods, bubble shapes │ ├── painting.py tones, speed and focus lines, frames: what is laid on the art under the lettering │ ├── people.py optional character mask (ultralytics) │ ├── fonts.py font discovery: fonts/, fonts/bundled/, system fonts │ ├── web.py settings, sessions and the HTTP routes │ └── __main__.py CLI ├── scripts/comic_lettering_script.py A1111 family: routes, clean copies, automatic mode ├── javascript/comic_lettering.js A1111 family: tool icon, overlay iframe (ES5) ├── style.css A1111 family: sizes the icon, hides one row ├── __init__.py ComfyUI: entry point, routes, saving ├── comfy_nodes.py ComfyUI: the Comic Lettering node ├── comfy_web/comic_lettering.js ComfyUI: Open editor button, widget sync ├── ui/ the editor: Vite + React 19 + TypeScript + Zustand + Tailwind │ ├── src/ sources │ └── dist/ the build, committed ├── fonts/ user fonts (git-ignored) and fonts/bundled/ (shipped) ├── docs/ this documentation └── requirements.txt numpy, scipy, Pillow ``` Both hosts load the same folder. A1111-family hosts run `scripts/*.py` and load `javascript/*.js` and `style.css`; they never import the root `__init__.py`. ComfyUI imports the root `__init__.py` and never looks into `scripts/`. ## The life of a render What happens between a keystroke in the editor and new pixels on screen: ```mermaid sequenceDiagram participant U as User participant P as Editor page (iframe) participant W as web.py participant C as core.py U->>P: leaves a text field / releases a slider / drops an item P->>P: store.render(): skip if inputs equal the last finished render P->>W: POST /comic_lettering/render {session, text, caption, sfx, settings, pins, regions} W->>C: arrange(image, ...) -> scene W->>C: draw(image, scene) -> lettered image W-->>P: {version, status, items, bubbles} P->>W: GET /comic_lettering/image/{session}/result?v={version} W-->>P: PNG P->>U: image, frames and handles from items ``` The core works in two steps on purpose: - **`arrange()`** decides what goes where and returns a *scene*: a JSON-clean dict with every item's text, font size and place in pixels. It draws nothing. - **`draw()`** turns a scene into pixels. The editor needs the scene, not only the picture: frames, handles and the ghost preview while dragging are built from `scene["items"]`. See [CORE.md](CORE.md#the-scene). Bubble detection is the slowest step, and both `arrange()` and `draw()` need it, on every change made in the editor. `detect_bubbles()` therefore keeps the result for the last six images, keyed by a hash of the pixels. ## Where state lives | State | A1111 family | ComfyUI | |---|---|---| | The look (fonts, colors, sliders) | `user_settings.json` in the extension folder, written by `POST /settings` | the node's widgets, so the workflow file | | The three texts | page memory; `user_settings.json` (`_auto` block) only while automatic mode is on | the node's widgets | | Hand placement (`pins`) and areas (`regions`) | page memory, per image; forgotten with the next generation | the node's hidden `layout` widget, as JSON | | Images being lettered | server memory: `web._sessions`, the last 6 | the same | | Clean copies of the last generation | server memory: `web.LAST_CLEAN[tab]` | `web.LAST_CLEAN["node:"]`, the first frame the node saw | Nothing but `user_settings.json` and saved images is ever written to disk. **Sessions.** A session is one image held in server memory under a random id, with its lettered result and cached PNG bytes. The page refers to images only by session id. Six are kept; the least recently used goes first. A page that asks for a dropped session gets `404` with *"The image is gone from memory. Load it again."* **Which image is it.** When the editor opens, the page asks `POST /session/last` with the tab and the index of the image in the host's gallery. The server answers with an `origin` string, `"::"`. The page sends the origin it already holds back as `known`; if it matches and the session is still alive, the server answers `{"same": true}` and the page keeps its slot, with every pin and area in it. A new generation raises the counter, so everything belonging to the old image is dropped. ## Host layer: AUTOMATIC1111 / Forge / reForge **`scripts/comic_lettering_script.py`** is an always-on script (`scripts.AlwaysVisible`). | Hook | What it does | |---|---| | `script_callbacks.on_app_started` | `web.register(app, save_like_host)`: adds the routes to the host's FastAPI app | | `ui()` / `before_component` | Places one hidden Gradio row per tab, `comic_lettering_bar_{tab}`, holding a hidden textbox and the hidden button `comic_lettering_to_gallery_{tab}` | | `process()` | `web.begin_generation(tab)`: forgets the previous generation's clean copies | | `postprocess_image()` | Stores a clean copy of the image in `web.LAST_CLEAN[tab]`; when automatic mode is on, letters `pp.image` in place | `save_like_host()` saves through the host's own `modules.images.save_image`, so the folder, file name pattern, numbering, format and subfolders are the host's, with the suffix `-lettered` and the source image's generation parameters. On "Reload UI" the host runs the script again but keeps imported packages. The script deletes `comic_lettering*` from `sys.modules` first, so a reload also picks up changes in the core. **`javascript/comic_lettering.js`** runs in the Gradio page. It is written in ES5 style (no arrow functions, no template literals), like the host's own scripts. - It adds a tool button to the row under `Generate` in txt2img and img2img, just before Style Grid's button when that extension is present. `dress()` copies the classes and the computed look of a tool button the host made itself, so the icon matches any theme. - A click builds a full-screen overlay with an `