File size: 7,439 Bytes
2a261b0 | 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 | # Architecture
## Layers
| Layer | Location | Notes |
|---|---|---|
| Core | `text_fix/core.py`, `text_fix/fonts.py` | Finds, erases and writes text. numpy, OpenCV, Pillow. Knows nothing about the host. |
| Server | `text_fix/web.py` | The images open in the editor, and the routes the editor calls. The same functions behind FastAPI (A1111 family) and aiohttp (ComfyUI). |
| Editor | `ui/` | React, TypeScript, Tailwind, shadcn-style parts on Radix, zustand. Built into `ui/dist`, which is kept in the repository. |
| A1111 / Forge / reForge | `scripts/text_fix_script.py` | Registers the routes; saves the way the host saves; runs the model pass; a hidden button that carries the result into a gallery. |
| Host page script | `javascript/text_fix.js`, `style.css` | The icon under `Generate`, the button under the gallery, the window the editor opens in. ES5 style. |
| ComfyUI | `__init__.py`, `comfy_nodes.py`, `comfy_web/text_fix.js` | The node, its `Open editor` button, and the routes. A1111-family hosts never import these. |
Everything the two hosts share is in the core and the server. A host layer turns its own image type into an RGB `uint8` array, calls them and turns the answer back.
## The editor and the server
The editor is a page served by the extension at `/text_fix/ui` and shown in an iframe over the host's page. The host page and the editor talk by `postMessage` (`ui/src/bridge.ts`): the host hands over the image (A1111 family) or the node and what it keeps (ComfyUI); the editor asks to close, to send its result to a gallery, or to write its state into the node.
The server keeps each image open in the editor as a `Session`: the image, the lines found on it, the selections worked out so far, the last result, and the images `Use as the image` replaced, for `Undo`.
A selection never travels as pixels. The editor holds a recipe for it: one of the found lines (`{"found": 2}`), a click (`{"at": [x, y], "reach": 0.3}`) or several lines taken as one block (`{"join": [recipe, ...]}`); then the clicks that extended it (`"extend"`), the place for the new text set with the handles (`"shape": {"corners", "bend"}`), the brush strokes (`"paint"`), a path drawn for the new text (`"path"`, which makes a `PathSelection`) and a hand warp (`"warp"`: 3 x 4 moves in pixels). A selection keeps the height and the area of its old letters apart from the place for the new text (`letters_height`, `letters_area`): erasing goes by the letters, writing by the place. The server turns a recipe into a `Selection` of its image and keeps the answer, so asking again is cheap. On the same image a recipe means the same thing every time; that is what lets a ComfyUI node keep its selections as a few bytes of JSON in a widget and work them out again on the next run.
The preview is the core's `replace` run on the server for every change, a moment after the last one; it takes tens of milliseconds. The model pass is the host's (`Host.blend`): img2img inpainting in the A1111 family, the workflow's own nodes in ComfyUI.
## The core, step by step
### 1. Marks (`_passes`)
Letters are thin marks that stand out from what is around them. The image is searched four ways: light on dark and dark on light (morphological top-hat and black-hat), at two stroke widths. A mark counts when it is strong next to the strongest thing near it, so faint text beside a bright window is still seen.
### 2. A line of text (`_line_of`)
Starting from one mark, its neighbors are added while they are of its height and its color and stand side by side. The result is one line.
### 3. Letters or gaps (`_contrast`)
Between light letters the dark gaps are marks too, and just as regular. What tells them apart is the surroundings: the gaps have the color of what lies right around the line, the letters do not. The comparison uses a narrow band around the line, because text often sits on a plate or a patch of its own.
### 4. From a click (`locate`)
The mark nearest to the click is the seed. All four passes give an answer; the one that stands out most wins. The searched window grows when the line runs out of it. `add_to` joins the answer to an earlier selection.
### 5. Without a click (`find_all`)
Every mark is tried as a seed. A line is kept when it looks like text by itself: at least three marks, equal heights, equal stroke thickness, not solid blocks and not thin outline fragments, clearly unlike its surroundings. Lines found by more than one pass are kept once.
### 6. The selection (`Selection`)
A selection holds the pixels of the letters, their color, the direction of the line, and its upper and lower edge as polynomials of the place along the line: constants for straight text, lines for text that grows, parabolas for text on a bent surface.
### 7. Erasing (`erase`, `_fill`)
The letters, their soft edges and their shaded parts make a hole. It is filled from the pixels around it, each counted by how much it looks like the surface the text was on, near pixels first. A dark seam next to the text adds nothing to the fill.
### 8. Writing (`write`, `_text_tile`, `_glyphs`)
The text is drawn flat, four times larger than needed, capitals as tall as the old letters. Its strokes are thickened or thinned to match the old ones; it is spread towards the old width or squeezed into it. Then every pixel near the text looks up its value in that flat tile through the selection's edges, which bends the text into place. The ink is the old color plus the light of the surface under it.
Along a path (`PathSelection`, `_path_coords`), the place in the tile is the distance along the path to the nearest point on it, and the distance from it. A hand warp and `relief` (`_displace`) then move those places: the warp's grid of moves is spread over the text, the relief takes a displacement map from the surface's brightness, and each pixel looks up where it was moved from.
The look is laid on in order: the shadow of the letters and their outline (`warpAffine` of the shape, blurred), the outline (the letters dilated), then the letters. A second color splits each line at its middle by where the pixel sits in its line. Materials (`_effect_shape`, `_effect_color`) change the coverage (a stencil's bridges and wear) or the colors (embroidery's stitches, neon's core and glow, engraving's shaded cut).
### 9. Font matching (`match_fonts`)
The old letters are sampled into a straight line 64 pixels high, taking out the slant and bend. From it come five numbers: stroke thickness, the ratio of thick to thin strokes, letter width, how much of its box a letter fills, and slant. The same numbers are measured once on each font's `HAMBURGEFONSTIV`, and the fonts are ranked by a weighted distance.
### 10. The model pass
Not part of the core. The A1111-family script runs img2img inpainting over `blend_mask(selection)` at a low strength. The ComfyUI node returns the mask and leaves the pass to the workflow.
## Data that crosses the layers
- `Selection` objects live in the host's session state between clicks. Hosts may hand back copies, so two selections are compared by where they lie (`same`), never by identity.
- `Look` carries every setting of how the text is drawn.
## What is planned
See [ROADMAP.md](../ROADMAP.md). The core grows with it; the editor gets the tools for picking and shaping text that the core gains.
|