@@ -2,7 +2,6 @@ package d2ascii
2
2
3
3
import (
4
4
"bytes"
5
- "fmt"
6
5
"math"
7
6
"strings"
8
7
@@ -47,25 +46,24 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
47
46
canvas .setOffset (- int (tl .X ), - int (tl .Y ))
48
47
canvas .setPad (pad )
49
48
50
- // Draw shapes
51
- for _ , shape := range diagram .Shapes {
52
- err := canvas .drawShape ( shape )
49
+ // Draw connections
50
+ for _ , conn := range diagram .Connections {
51
+ err := canvas .drawConnection ( conn )
53
52
if err != nil {
54
53
return nil , err
55
54
}
56
55
}
57
56
58
- // Draw connections
59
- for _ , conn := range diagram .Connections {
60
- err := canvas .drawConnection ( conn )
57
+ // Draw shapes
58
+ for _ , shape := range diagram .Shapes {
59
+ err := canvas .drawShape ( shape )
61
60
if err != nil {
62
61
return nil , err
63
62
}
64
63
}
65
64
66
- height = canvas .AutoHeight ()
67
- fmt .Println (canvas .h , height )
68
- canvas .ReScale (canvas .w , height )
65
+ // TODO: preserve arrow head
66
+ // canvas.ReScale(canvas.w, canvas.AutoHeight())
69
67
return canvas .TrimBytes (), nil
70
68
}
71
69
@@ -182,16 +180,48 @@ func (c *Canvas) drawCircle(x, y, w, h int, label string) error {
182
180
}
183
181
184
182
func (c * Canvas ) drawConnection (conn d2target.Connection ) error {
185
- // Draw a simple line between points for now
186
183
points := make ([]struct { x , y int }, len (conn .Route ))
187
184
for i , p := range conn .Route {
188
185
points [i ].x , points [i ].y = c .transformPoint (int (p .X ), int (p .Y ))
189
186
}
190
187
191
- for i := 0 ; i < len (points )- 1 ; i ++ {
188
+ // Draw lines between points
189
+ for i := range len (points ) - 1 {
192
190
c .drawLine (points [i ].x , points [i ].y , points [i + 1 ].x , points [i + 1 ].y )
193
191
}
194
192
193
+ // Draw destination arrow if needed
194
+ if len (points ) >= 2 && conn .DstArrow != d2target .NoArrowhead {
195
+ end , prev := points [len (points )- 1 ], points [len (points )- 2 ]
196
+ // Calculate angle for arrow direction
197
+ dx := float64 (end .x - prev .x )
198
+ dy := float64 (end .y - prev .y )
199
+ angle := math .Atan2 (dy , dx )
200
+
201
+ // Determine arrow character and offsets based on angle
202
+ var arrow rune
203
+ var mx , my int
204
+ switch {
205
+ case angle > - math .Pi / 4 && angle <= math .Pi / 4 :
206
+ arrow = '>'
207
+ mx = - 1 // Move left
208
+ my = 0
209
+ case angle > math .Pi / 4 && angle <= 3 * math .Pi / 4 :
210
+ arrow = 'v'
211
+ mx = 0
212
+ my = - 1 // Move up
213
+ case angle > 3 * math .Pi / 4 || angle <= - 3 * math .Pi / 4 :
214
+ arrow = '<'
215
+ mx = 1 // Move right
216
+ my = 0
217
+ default : // -3*math.Pi/4 < angle <= -math.Pi/4 (upward)
218
+ arrow = '^'
219
+ mx = 0
220
+ my = 1 // Move down
221
+ }
222
+ c .set (end .x + mx , end .y + my , arrow )
223
+ }
224
+
195
225
return nil
196
226
}
197
227
0 commit comments