Fix graph diagram CJK/Unicode character rendering - #49
Conversation
|
Hi there, thanks for your contributions! I'll have a proper look some time today/next week, this seems like a nice addition 👍 |
AlexanderGrooff
left a comment
There was a problem hiding this comment.
Hi there, thanks for contributing. I think this is a great addition, but it doesn't seem quite right just yet.
Your testcase already points this out, the padding between the right border and the text is missing a whitespace so the right border doesn't align nicely.
Here's what it looks like in the browser:
Please have a look at the whitespacing after the label, then we can get this merged 👍
|
@AlexanderGrooff Hi, thanks so much for taking the time to review this — I really appreciate it! So I dug into this alignment issue, and turns out it's a bit of a tricky one. The problem is how web browsers handle CJK (Korean/Japanese/Chinese) characters vs how terminals do. In terminals, CJK characters take up 2 columns (East Asian Width standard), but web browsers just render them as regular single-width characters — even with monospace fonts like Roboto Mono or Menlo. So the output looks perfectly aligned in the terminal, but breaks in the browser. I tried a CSS workaround (wrapping CJK chars in with width: 2ch), but that didn't really work either — the spacing between box-drawing characters and CJK glyphs just Given that this is mainly a CLI tool and the web interface is more of a nice-to-have, I'm not sure it's worth adding a bunch of complexity for this edge case. That said, I completely understand if you'd prefer not to merge this as-is. I'll leave the decision up to you — and if you think it's not ready, I'm happy to close this PR. Thanks again for your time! 👍 |
- Use runewidth.StringWidth() for display width calculation instead of len() which returns byte count - Use runewidth.RuneWidth() to properly position wide characters - Fill subsequent positions with empty string for wide CJK characters that occupy 2 terminal columns - Add Korean language test case (korean_nodes.txt) This fixes double-encoding bug where UTF-8 bytes were incorrectly interpreted as Unicode code points, causing garbled output like "íì¤í¸" instead of "테스트". Affected functions: - drawText(): text rendering - drawBox(): node label rendering - drawSubgraphLabel(): subgraph label rendering - setColumnWidth(): box width calculation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
bddc54a to
aa2bc82
Compare
|
Can't wait any more . I will use your fork |
Summary
runewidthlibrary for proper display width calculationProblem
When rendering graph diagrams with CJK characters (Korean, Japanese, Chinese), the text was corrupted:
Root Cause
len(string)returns byte count, not character countstring[x]returns a byte, not a runestring(byte)interprets the byte as a Unicode code point, causing double-encodingSolution
runewidth.StringWidth()for display width calculationrunewidth.RuneWidth()to properly position wide charactersChanges
cmd/draw.gocmd/mapping_node.gocmd/testdata/.../korean_nodes.txtTest plan
🤖 Generated with Claude Code