Metadata-Version: 2.4
Name: speech-bubble-mcp
Version: 0.2.0
Summary: Lettering for comics and manga videos: speech bubbles, thought balloons and captions on stills or video, as an MCP server.
Author: Tobias Fong
License-Expression: MIT
Project-URL: Homepage, https://tobiasfong.github.io/projects/speech-bubble.html
Project-URL: Repository, https://github.com/tobiasfong/webcomic-toolkit/tree/master/servers/speech-bubble-mcp
Project-URL: Changelog, https://github.com/tobiasfong/webcomic-toolkit/blob/master/servers/speech-bubble-mcp/CHANGELOG.md
Keywords: mcp,comics,manga,lettering,speech-bubbles,webcomic
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Multimedia :: Graphics
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: mcp<2,>=1.2.0
Requires-Dist: Pillow>=10.0.0
Requires-Dist: numpy>=1.24.0

# speech-bubble-mcp

<!-- mcp-name: io.github.tobiasfong/speech-bubble -->

Lettering for comics and manga videos, as an MCP server: speech bubbles,
thought bubbles, caption boxes, shouts and whispers, drawn onto a still page or
animated over a video shot. Runs on the CPU, with no model, no network and no
API key.

The connected AI harness does the thinking -- which line goes in which shot,
where the speaker stands, any translation -- and this server does the
mechanical part: fitting the type, drawing the balloon, aiming the tail and
timing the fades.

| | |
|---|---|
| GPU | none |
| Python | 3.10+ |
| Video | any ffmpeg (decode and encode only, no filters needed) |
| Languages | English, Japanese (vertical or horizontal), Chinese, Korean |

## Install

### From PyPI (simplest)

With [uv](https://docs.astral.sh/uv/) installed, there is nothing to clone:

```bash
claude mcp add -s user speech-bubble -e SPEECH_BUBBLE_FFMPEG=/path/to/ffmpeg -- uvx speech-bubble-mcp
```

or `pip install speech-bubble-mcp` and register the `speech-bubble-mcp`
command. Installed this way, styles, saved lettering and output go to
`~/speech-bubble-mcp/`. The layout page is the `speech-bubble-layout` command.

### From the repo

```bash
git clone https://github.com/tobiasfong/webcomic-toolkit
cd webcomic-toolkit/servers/speech-bubble-mcp
python -m venv .venv
.venv/Scripts/python.exe -m pip install -r requirements.txt   # Windows
# .venv/bin/python -m pip install -r requirements.txt         # macOS / Linux
```

Register it with your harness, for example Claude Code:

```bash
claude mcp add -s user speech-bubble -e SPEECH_BUBBLE_FFMPEG=/path/to/ffmpeg -- \
  /path/to/speech-bubble-mcp/.venv/Scripts/python.exe /path/to/speech-bubble-mcp/server.py
```

Check it with `check_status`: it reports the ffmpeg it found, a font for each
language, and where it writes.

## Tools

| tool | what it does |
|---|---|
| `set_project_style` | one project's fonts, type size, colors, uppercase, vertical Japanese, reading order, thought style |
| `get_project_style` | the saved and the effective style |
| `parse_script` | `NAME: line`, `NAME (thought): line`, `CAPTION: line` script -> bubble entries, words untouched |
| `fit_text` | the line breaks and size a line would get, without drawing -- checks a translation fits |
| `letter_image` | letters a still: the lettered PNG, the bubbles alone on a transparent layer, and a saved spec |
| `letter_video` | animates bubbles over a clip or a still; `preview_at` renders single frames first |
| `list_lettering`, `get_lettering` | saved specs; re-render one in another language by name |
| `check_status` | ffmpeg, fonts, output folders |

## Placing bubbles by hand: the layout page

Auto-placement cannot tell a face from calm art, and a bubble over a face is the
first thing an author rejects. For a whole episode, `layout_server.py` puts every
shot on one local page, each on the frame its bubbles are held on, lettered by
the same drawing code the encode uses:

- drag a bubble to move it (`at`)
- drag the bar on its right edge to resize it; the text re-wraps (`max_width`)
- drag the dot to aim its tail (`tail`)

Every drop re-renders that shot and saves to the layout file at once. Words are
never edited on the page.

```
python layout_server.py <layout.json> [port]      # http://127.0.0.1:8766/
speech-bubble-layout <layout.json> [port]         # the same, when installed from PyPI
```

The layout file is `{"project", "lang", "shots": [{"id", "frame", "script",
"bubbles": [...]}]}`, with `frame` the path of that shot's hold frame. Build it
with recommended placements (below each speaker, captions top-left), let the
author drag, then letter each shot from it with `letter_video`.

The page doubles as a frame check: a shot's last frame is held on screen for
seconds once lettered, and a torn final frame from a video model, invisible in
motion, is plain as a still. A replaced hold frame shows on the next reload.

## A bubble

```json
{"id": "l3",
 "kind": "speech",
 "text": {"en": "Wait. You think I came here for the money?", "zh": "等等。你以为我是为了钱来的？"},
 "at": [0.30, 0.25],
 "tail": [0.42, 0.55],
 "start": 1.2, "in": "fade"}
```

- `kind`: `speech`, `thought`, `caption`, `shout`, `whisper`. A project's
  `thought_style` draws thoughts as a `cloud` (bumps and a trail of dots, the
  default) or a manga `flash` (a white body ringed with radiating lines, no
  outline and no tail).
- `at`: the bubble's center as fractions of the frame, or a name (`top-left`,
  `top`, `top-right`, `left`, `center`, `right`, `bottom-left`, `bottom`,
  `bottom-right`). Omit it to auto-place on calm art near the speaker.
- `tail`: the speaker's mouth or head, as fractions, or `left`, `right`,
  `center`, `far-left`, `far-right`. Omit it for no tail.
- Video only: `start`, `end` (seconds; default is one after another by reading
  time -- 24 English characters or 7 CJK characters a second, at least 1.2 s
  -- staying to the end of the shot), `read` (seconds, to override one
  bubble's reading time), `in` (`fade`, `slide-up`,
  `slide-down`, `slide-left`, `slide-right`, `pop`, `none`), `out` (`none` or
  `fade`).
- Optional: `scale` (a shout uses 1.15), `font`, `max_width`, `vertical` /
  `horizontal` to force Japanese one way.

An explicit newline in `text` is always kept: that is how a line is broken at
the phrase the writer wants.

## Where things go

- Lettered images: `../character-panel-mcp/output/<project>/lettered/`
- Lettered videos: `../anime-production-mcp/output/<project>/lettered/`, with
  each bubble as a transparent PNG in `<name>_<lang>_bubbles/`
- Specs and styles: `state/` (gitignored -- dialogue carries character names)

Installed on its own, without those sibling servers, it writes to `output/`.
`SPEECH_BUBBLE_STATE`, `SPEECH_BUBBLE_IMAGE_OUT` and `SPEECH_BUBBLE_VIDEO_OUT`
override each location.

## How it works

- **The bubble follows the text.** One type size for a whole project; the
  balloon is fitted around the words, never the words squeezed into a balloon.
  Line breaks are chosen by trying every line count and keeping the tightest
  ellipse, which gives the short-long-short shape of hand lettering.
- **Drawn twice, outlined once.** Each shape is a set of primitives drawn grown
  by the stroke in the outline color, then at size in the fill color, so a
  tail, a cloud's bumps or a burst's spikes share one clean outline. Drawn at
  3x and downsampled.
- **Video frames are composited here, in Pillow.** ffmpeg only decodes and
  encodes, so a reduced ffmpeg build works. The preview and the encode call the
  same function, so an approved preview is what renders.

## Troubleshooting

- **`No ffmpeg found`** -- only video needs it. Set `SPEECH_BUBBLE_FFMPEG`.
- **Tofu boxes instead of characters** -- the font lacks that script. Set one
  per language with `set_project_style(fonts={"zh": "..."})`.
- **`Bubble has no 'ja' text`** -- the spec has no line in that language yet;
  translations are added to `text` as a per-language map.
- **A bubble runs off the frame** -- allowed, and reported as `off_frame`.
  Pin it with `at` if it should not.

## Tests

```bash
.venv/Scripts/python.exe test_tools.py
```

Synthetic art in a temporary folder; video checks need ffmpeg. They compare
rendered frames against the source and against a still lettered by the other
code path, so they check what was encoded, not what was planned.
