Skip to content

Fix sequence diagram CJK/Unicode character rendering - #55

Open
ColtWindy wants to merge 3 commits into
AlexanderGrooff:masterfrom
ColtWindy:fix/sequence-cjk-unicode-support
Open

Fix sequence diagram CJK/Unicode character rendering#55
ColtWindy wants to merge 3 commits into
AlexanderGrooff:masterfrom
ColtWindy:fix/sequence-cjk-unicode-support

Conversation

@ColtWindy

@ColtWindy ColtWindy commented Feb 5, 2026

Copy link
Copy Markdown

Summary

Fix sequence diagram rendering for CJK (Chinese, Japanese, Korean) characters.

Before

Boxes misaligned, labels positioned incorrectly:

Screenshot 2026-02-05 at 12 15 11 PM

After

Correct alignment with mixed English/Japanese/Korean/Chinese:

Screenshot 2026-02-05 at 12 15 14 PM

Root Cause

  1. runewidth.StringWidth() treats box-drawing characters (┌, ─, │) as "East Asian Ambiguous" with width=2, but terminals display them as width=1

  2. Label positioning used rune index instead of display width, causing CJK characters (width=2) to shift incorrectly

Solution

  1. Use runewidth.Condition{EastAsianWidth: false} for consistent width calculation
  2. Rewrite label rendering to track display position by character width

Test

Added east_asian_characters.txt with Japanese, Korean, and Chinese text in participant names and message labels.

ColtWindy and others added 3 commits February 5, 2026 11:18
Use a custom runewidth.Condition with EastAsianWidth=false to properly
calculate display widths for both box drawing characters and CJK text.

The issue: runewidth library treats box drawing characters (┌, ─, │) as
East Asian Ambiguous, returning width=2, while they actually display as
width=1 in most terminals. This caused misalignment when CJK characters
(which correctly have width=2) were used in participant names.

Before:
│ 클라이언트 │          │ 서버 │   (10 spaces - too many)

After:
│ 클라이언트 │     │ 서버 │   (5 spaces - correct)

The fix uses EastAsianWidth=false which:
- Box drawing characters: width=1 (correct)
- CJK characters: width=2 (correct)

Also adds Korean language test case (korean_participants.txt).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The previous label rendering used rune array indexing which doesn't
account for display width differences between ASCII and CJK characters.

Problem: Label text was positioned using col++ (rune index), but the
line array used display width positioning for lifelines. This caused
misalignment when CJK characters (display width=2) were used in labels.

Solution: Rewrite label rendering to use display width-based positioning:
- Build the line character by character using display position
- Labels take priority over lifelines (overwrite them as intended)
- Use widthCondition.RuneWidth() to advance position correctly

Before:
     │ 사용자 인증 토큰 검증 요청│   (label ends at wrong position)

After:
     │ 사용자 인증 토큰 검증 요청      (proper display width positioning)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add test coverage for CJK/Unicode character rendering with a test case
that combines English, Japanese, Korean, and Chinese characters.

This verifies header alignment, label positioning, and lifeline
rendering for East Asian wide characters.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
drunkhacker added a commit to drunkhacker/mermaid-ascii that referenced this pull request Feb 11, 2026
Use a custom runewidth.Condition with EastAsianWidth=false to properly
calculate display widths for both box drawing characters and CJK text.

The issue: runewidth library treats box drawing characters (┌, ─, │) as
East Asian Ambiguous, returning width=2, while they actually display as
width=1 in most terminals. This caused misalignment when CJK characters
(which correctly have width=2) were used in participant names.

Changes:
- Add widthCondition with EastAsianWidth=false for consistent width calculation
- Replace runewidth.StringWidth() with widthCondition.StringWidth()
- Rewrite label rendering to use display width-based positioning
- Add east_asian_characters.txt test case with Japanese, Korean, Chinese

Based on upstream PR AlexanderGrooff#55 by ColtWindy, adapted for multi-line label support.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@xlight

xlight commented Apr 9, 2026

Copy link
Copy Markdown

We need this !

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.

2 participants