Skip to content

Fix Cavena890 Chinese write path so text and italics round-trip#12594

Merged
niksedk merged 1 commit into
mainfrom
fix/cavena890-chinese-write-path
Jul 18, 2026
Merged

Fix Cavena890 Chinese write path so text and italics round-trip#12594
niksedk merged 1 commit into
mainfrom
fix/cavena890-chinese-write-path

Conversation

@niksedk

@niksedk niksedk commented Jul 18, 2026

Copy link
Copy Markdown
Member

Follow-up to the #12580 review, where the Chinese index++ commit was dropped because the whole Chinese write path was found to be unable to round-trip. This fixes the path properly.

The problem: the Chinese text field is UTF-16BE — FixText trims the 0x00/0x7F fillers, forces an even length, and decodes the field with Encoding.GetEncoding(1201) as byte pairs. The writer, however, emitted only the high byte of each character (encoding.GetBytes(new[] { current })[0]) and wrote lone italic-marker bytes without advancing the index (so the next character overwrote them). No Chinese text, let alone italics, could survive a save/load cycle.

Writer (GetTextAsBytes): every character now writes its full UTF-16BE pair, and the italic markers are written as in-band 0x00 0x88 / 0x00 0x98 pairs so the byte pairing stays aligned. All writes are bounds-checked against the 51-byte field (silent truncation, as before).

Reader (FixText, Chinese branch): the marker/filler comparisons used Encoding.Default.GetString(..), which is platform dependent — on .NET Core Encoding.Default is UTF-8, where 0x88/0x98/0xBE each decode to U+FFFD, meaning any undecodable byte in a real file was replaced with <i>. The comparisons now match the actual decoded chars (U+0088, U+0098, U+007F, U+00BE) explicitly and deterministically.

Compatibility notes:

  • The UTF-16BE pair layout for text is certain — it's what the reader (which handles real-world files) has always decoded.
  • The 0x00 0x88 marker-pair layout is the natural in-band encoding under that pairing, but I could not verify it against files produced by real Cavena tools (no sample .890 files in the repo). Since the old marker matching couldn't fire deterministically on any platform, no working real-file italic behavior is lost either way.

Tests: save→load round-trip through the public Save/LoadSubtitle API for plain Chinese text, a two-line paragraph, and an italicized line. Both tests fail against the old code. All 531 libse and 237 seconv tests pass.

🤖 Generated with Claude Code

The Chinese text field is UTF-16BE - the reader trims 0x00/0x7F fillers,
forces an even length, and decodes the field as byte pairs. The writer
however emitted only the high byte of each character
(encoding.GetBytes(...)[0]) and wrote lone italic-marker bytes without
advancing the index, so no Chinese text or formatting could survive a
save/load cycle.

Writer: emit the full UTF-16BE pair for every character, and write the
italic markers as in-band 0x00 0x88 / 0x00 0x98 pairs so the byte
pairing stays aligned; all writes are bounds-checked against the 51-byte
field.

Reader: match the decoded marker/filler chars explicitly (U+0088, U+0098,
U+007F, U+00BE) instead of via Encoding.Default.GetString(..), which is
platform dependent - on .NET Core (UTF-8) 0x88/0x98/0xBE all decode to
U+FFFD, so any undecodable byte in a file was turned into "<i>".

Round-trip tests added (plain text, two lines, italics); both fail
against the old code.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@niksedk
niksedk merged commit 5aaaa4a into main Jul 18, 2026
1 of 3 checks passed
@niksedk
niksedk deleted the fix/cavena890-chinese-write-path branch July 18, 2026 09:26
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