Skip to content

[Bug] mermaid-text: wide (CJK) characters shift box borders #52

Closed

Description

@worldnine

Description

Labels that contain double-width characters (CJK, fullwidth forms) break box alignment: the right border moves one column for every wide character. There seem to be two related causes. Both come down to text being placed in a char grid by one measure (cells) and sized or emitted by another (display width).

  1. Grid and the sequence canvas write by display width but emit every cell. Grid::write_text / write_text_protected (src/layout/grid.rs:1177-1203) and the sequence canvas's put_str (src/render/sequence.rs:113-124) advance by UnicodeWidthChar::width, so the second cell of a wide character stays ' '. Display for Grid (grid.rs:1811), render_inner (grid.rs:777) and the sequence into_string (sequence.rs:126-135) then emit that cell too. 開始 comes out as 開 始. Other drawing passes also leave glyphs in that cell, so a frame border or block fill shows up between the characters ([成═功═], 承░認). I've seen this in flowchart, stateDiagram, sequenceDiagram and architecture-beta.
  2. box_table::put_str advances one cell per character (src/render/box_table.rs:45-49), while box widths are measured in display width (pad_right). I've seen this in classDiagram and erDiagram.

Steps to Reproduce

All on master (186698c); 0.57.0 behaves the same.

printf 'flowchart LR\n  A[開始] --> B[終了]\n' | mermaid-text
┌──────┐     ┌──────┐
│ 開 始  │────▸│ 終 了  │
└──────┘     └──────┘
printf 'sequenceDiagram\n  A->>B: 要件定義書\n  alt 成功\n    B->>A: 承認\n  end\n' | mermaid-text
     ┆ 要 件 定 義 書         ┆
     ────────────────────▸
     ┆                   ┆
╔═[alt]══[成═功═]═══════════════╗
║░░░░┆░承░認░░░░░░░░░░░░░░░┆░░░░║
printf 'classDiagram\n  class 動物 {\n    +名前\n  }\n' | mermaid-text
┌─────────┐
│  動物     │
├─────────┤
│  +名前    │
└─────────┘

Expected Behavior

Each wide character takes exactly two terminal columns and the borders line up:

┌──────┐     ┌──────┐
│ 開始 │────▸│ 終了 │
└──────┘     └──────┘
┌─────────┐
│  動物   │
├─────────┤
│  +名前  │
└─────────┘

Possible fixes

For (1), when building the output string, skip the cell right after a width-2 character. In my tests that alone fixed all four cases (flowchart, sequence including the ═/░ cells, state, architecture-beta). Optionally, write_text could also put a marker in the continuation cell (and protect it in the protected variant) so routing treats it as occupied. A marker alone isn't enough, though, because the sequence frame and fill are drawn into that cell. The get()-based checks I found (nudge.rs, unicode.rs) look for box-drawing glyphs, so a marker shouldn't confuse them, but you know the routing code far better than I do.

For (2), advance put_str by display width the way sequence.rs already does, and give grid_to_string the same skip.

I haven't checked xy_chart.rs and quadrant_chart.rs, which also keep their own Vec<char> canvases. block-beta, requirementDiagram, packet-beta, timeline, journey, gantt and mindmap rendered CJK correctly in my tests.

I'm happy to send a PR if you'd like. Just tell me whether you prefer skip-at-render, a marker cell, or both.

Environment

Field Value
OS macOS 27.2
Terminal Ghostty
Image protocol n/a
Rust version rustc 1.92.0
markdown-reader version n/a (mermaid-text used as a library)
mermaid-text version 0.57.0 from crates.io; also master 186698c
Installation method library dependency; CLI built from source

Additional Context

cosentty, a terminal viewer for Cosense (a wiki service popular in Japan), works around (1) today by post-processing the output for flowchart/sequence/state: it drops the cell after each width-2 character. We'd happily remove that once this is fixed upstream. Class and ER diagrams aren't covered by our workaround.

I used an AI coding assistant for the investigation and drafting; the repros above were run locally.

Activity

  1. leboiko commented on Oct 13, 2026

    @leboiko
    Owner

    Fixed on master by #53. CJK/fullwidth labels now keep contiguous text and aligned borders across shared grids, sequence, class/ER boxes and quadrant charts. Wide continuation cells cannot leak spaces or overwrite neighboring glyphs; combining marks survive clipping boundaries and subsequent writes.

    Thank you @worldnine for the detailed reproductions. Exact terminal-column, boundary and public-helper regressions pass, along with full stable/Rust 1.92 workspace suites and validation gates. No unrelated snapshots changed. This closes the source issue; no release has been published yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions