Skip to content

fix(subtitles): trim the grid padding off teletext rows at interior line breaks (#233) - #258

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/issue233-teletext-row-padding
Jul 30, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/issue233-teletext-row-padding

Conversation

@superuser404notfound

@superuser404notfound superuser404notfound commented Jul 30, 2026 •

Copy link
Copy Markdown
Owner

Reported by tresby on #233. A two-row teletext caption reached the host as row one<pad>\n<pad>row two, so a renderer that centres each line put row two off centre and drew the background box wider than its text.

Where it comes from

On a page libzvbi does not flag as a subtitle page, gen_sub_ass writes each row at full column width (decode_string(page, row, &buf, 0, page->columns, ...), one \h per grid space) and appends " \N" after it. Two passes handled whitespace and neither reached the inside of a line break:

  • edgeTrimmed trims the leading and trailing edge of the whole run sequence, so the first row's leading pad and the last row's trailing pad only.
  • collapseInteriorBlankLines folds what sits BETWEEN two newlines. Against a single retained newline it drops nothing, and it never looks at the characters before one at all.

The fix

trimGridRows takes the padding off both ends of every row. Like the fold it works on the flattened character sequence and re-splits along the original run boundaries, since a row's pad carries the spacing attribute that changes colour and therefore lands in a whitespace-only run of its own, which a per-run walk steps over. Both passes share one rewritingFlattened helper now.

The gate, and why the fold moves behind it too

Both passes only correct a raw grid dump, which is exactly the page that arrives without an \an. The equivalence holds by construction: gen_sub_ass emits {\anN} on the first row of a flagged page that holds any character, and a flagged page holding none produces no cue at all.

On a curated page both of the things these passes remove are decisions rather than noise:

curated page raw grid dump
blank lines empty_lines / 2 fillers, vertical fine-positioning inside the block every skipped grid row, emitted
residual padding relative indentation carrying the chosen alignment (leading = min_leading left, trailing = min_trailing right, both for the non-centrable centre case) the grid, at full column width

So a curated body is passed through whole now, including through the fold, which had been folding those fillers away since #107. That half is unreported and there is no flagged-page source here to shoot it against, but leaving the fold unconditional while gating the trim would give two passes with the same premise two different answers.

Not taken

Deriving the horizontal anchor the way can_align_left / _right / _center do, which the trim does make measurable. Where that analysis leaves more than one of the three viable it resolves the tie from last_ass_alignment, the answer given to the previous cue. A stateless parser has to fall back to centre there, so an unambiguous left-aligned cue followed by an ambiguous one would jump, which is the same blink the vertical derivation already refuses. The comment on gridPlacement now says that instead of the stale reason.

Tests

13 new in Issue233TeletextRowPaddingTests, built on a gridRow helper that reproduces ffmpeg's emission shape rather than a hand-written string: the reported two-row case, the separator space, padding that lands in its own coloured run, indentation inside a row surviving, colours surviving, and the curated page keeping both its indentation and its fillers. Full suite green, 1328 Swift Testing + 392 XCTest (1 skipped).

Closes nothing yet, the reporter retest on page 801 is what settles it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N

…ine breaks (#233)

A teletext row that libzvbi does not curate arrives at full column width. On a
page it does not flag as a subtitle page, `gen_sub_ass` writes each row with
`decode_string(page, row, &buf, 0, page->columns, ...)`, one `\h` per grid space,
and appends `" \N"` after it. Two passes handled whitespace and neither reached
the inside of a line break:

- `edgeTrimmed` trims the leading and trailing edge of the whole run sequence,
  so the first row's leading pad and the last row's trailing pad only.
- `collapseInteriorBlankLines` folds what sits BETWEEN two newlines. Against a
  single retained newline it drops nothing, and it never looks at the characters
  before one at all.

So a two-row caption reached the host as `row one<pad>\n<pad>row two`. A renderer
that centres each line puts row two visibly off centre and draws the cue's
background box wider than its text.

`trimGridRows` now takes the padding off both ends of every row. Like the fold it
works on the flattened character sequence and re-splits along the original run
boundaries, because a row's pad carries the spacing attribute that changes colour
and therefore lands in a whitespace-only run of its own, which a per-run walk
steps right over. Both share one `rewritingFlattened` helper now.

The pass is gated, and the fold is gated with it. Both only correct a raw grid
dump, which is exactly the page that arrives without an `\an`: `gen_sub_ass`
emits `{\anN}` on the first row of a flagged page holding any character, and a
flagged page holding none produces no cue, so an arriving `\an` and a curated
body are equivalent. On a curated page both of the things these passes remove are
decisions instead of noise. The `empty_lines / 2` fillers are its vertical
fine-positioning inside the block, and the residual padding is the relative
indentation that carries the horizontal alignment it picked: `leading =
min_leading` for left, `trailing = min_trailing` for right, both for its
non-centrable centre case. A curated body is passed through whole now, including
that fold, which had been folding those fillers away since #107.

Not taken, though the trim makes it measurable: deriving the horizontal anchor
the way `can_align_left` / `_right` / `_center` do. Where that analysis leaves
more than one of the three viable it resolves the tie from `last_ass_alignment`,
the answer given to the previous cue. A stateless parser has to fall back to
centre there, so an unambiguous left-aligned cue followed by an ambiguous one
would jump, which is the same blink the vertical derivation already refuses.

Reported by tresby, who had solved it row-wise in a proxy implementation and
supplied the failing sequence.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N
@superuser404notfound
superuser404notfound merged commit 43fef71 into main Jul 30, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/issue233-teletext-row-padding branch July 30, 2026 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant