@@ -48,11 +48,21 @@ func (g *graph) drawEdge(e *edge) (*drawing, *drawing, *drawing, *drawing, *draw
4848}
4949
5050func (d * drawing ) drawText (start drawingCoord , text string ) {
51- // Increase dimensions if necessary.
52- d .increaseSize (start .x + len (text ), start .y )
53- log .Debug ("Drawing '" , text , "' from " , start , " to " , drawingCoord {x : start .x + len (text ), y : start .y })
54- for x := 0 ; x < len (text ); x ++ {
55- (* d )[x + start .x ][start .y ] = string (text [x ])
51+ // Increase dimensions if necessary. Use the visual width so multibyte
52+ // (e.g. Cyrillic, CJK) runes reserve the correct number of cells.
53+ textWidth := runewidth .StringWidth (text )
54+ d .increaseSize (start .x + textWidth , start .y )
55+ log .Debug ("Drawing '" , text , "' from " , start , " to " , drawingCoord {x : start .x + textWidth , y : start .y })
56+ // Iterate over runes (not bytes) so multibyte characters are placed in a
57+ // single cell instead of being split into invalid byte fragments.
58+ textX := start .x
59+ for _ , r := range text {
60+ runeWidth := Max (runewidth .RuneWidth (r ), 1 )
61+ (* d )[textX ][start .y ] = string (r )
62+ for offset := 1 ; offset < runeWidth ; offset ++ {
63+ (* d )[textX + offset ][start .y ] = ""
64+ }
65+ textX += runeWidth
5666 }
5767}
5868
0 commit comments