Skip to content

CJK (Chinese, Japanese, Korean) and Emoji character width issues in ASCII rendering #13

@zenyr

Description

@zenyr

Summary

ASCII rendering has width calculation issues with CJK characters (full-width, 2-byte) and emoji, causing misaligned boxes and potential text overflow.

Problem Description

Issue 1: CJK Character Width Miscalculation

CJK characters (Chinese, Japanese, Korean) are full-width characters that occupy 2 columns in terminal/monospace displays. However, the ASCII renderer treats them as single-width characters.

Reproduction:

import { renderMermaidAscii } from 'beautiful-mermaid'

// Korean example
const svg = renderMermaidAscii(`graph TD
  A[시작] --> B{결정}
  B -->|예| C[처리]`)

console.log(svg)

Actual Output:

┌───┐           
│   │           
│ 시작 │  ← Box too narrow for CJK
│   │           
└─┬─┘           

Issues:

  • CJK text appears cramped in boxes
  • Boxes don't properly accommodate full-width characters
  • Long CJK text may overflow boundaries

Issue 2: Emoji Width Inconsistency

Emoji characters also have variable width that isn't properly handled.

Expected Behavior

CJK characters should be counted as 2 columns for proper box sizing:

# Expected for "시작" (2 CJK chars = 4 columns)
┌────────┐
│        │
│  시작  │
│        │
└────┬───┘

Environment

  • Package: beautiful-mermaid v0.1.3
  • Runtime: Bun/Node.js
  • Affected: Korean, Chinese, Japanese, Emoji

Possible Solutions

  1. Use string-width or wcwidth library for proper Unicode width calculation
  2. Detect CJK Unicode ranges and count as width 2:
    • Chinese: \u4e00-\u9fff
    • Hiragana: \u3040-\u309f
    • Katakana: \u30a0-\u30ff
    • Korean: \uac00-\ud7af
  3. Document limitation and recommend SVG output for CJK/Emoji

Impact

Affects users working with CJK languages or emoji in ASCII/terminal mode.

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