-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathd2ascii.go
291 lines (247 loc) · 5.37 KB
/
d2ascii.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package d2ascii
import (
"bytes"
"math"
"strings"
"oss.terrastruct.com/d2/d2target"
)
// RenderOpts contains options for ASCII rendering
type RenderOpts struct {
Pad *int64 // Optional padding around the diagram
Scale *float64 // Pixels per ASCII character ratio
}
// Render converts a D2 diagram into ASCII art
func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
if opts == nil {
opts = &RenderOpts{}
}
// Default padding matching d2svg
pad := int(8)
if opts.Pad != nil {
pad = int(*opts.Pad)
}
// Scale for converting diagram coordinates to ASCII grid
// Default: roughly 1 ASCII char = 8x4 pixels
scale := struct{ x, y float64 }{8, 4}
if opts.Scale != nil {
s := *opts.Scale
scale.x = s
scale.y = s / 2 // Maintain aspect ratio
}
// Calculate canvas dimensions
tl, br := diagram.NestedBoundingBox()
width := int(math.Ceil(float64(br.X-tl.X+(pad*2)) / scale.x))
height := int(math.Ceil(float64(br.Y-tl.Y+(pad*2)) / scale.y))
// Create ASCII canvas
canvas := NewCanvas(width, height)
canvas.setScale(scale.x, scale.y)
canvas.setOffset(-int(tl.X), -int(tl.Y))
canvas.setPad(pad)
// Draw shapes
for _, shape := range diagram.Shapes {
err := canvas.drawShape(shape)
if err != nil {
return nil, err
}
}
// Draw connections
for _, conn := range diagram.Connections {
err := canvas.drawConnection(conn)
if err != nil {
return nil, err
}
}
return canvas.Bytes(), nil
}
// Canvas handles the ASCII grid and drawing operations
type Canvas struct {
grid [][]rune
w, h int
// Coordinate transformation
scaleX, scaleY float64
offsetX, offsetY int
pad int
}
func NewCanvas(w, h int) *Canvas {
grid := make([][]rune, h)
for i := range grid {
grid[i] = make([]rune, w)
for j := range grid[i] {
grid[i][j] = ' '
}
}
return &Canvas{
grid: grid,
w: w,
h: h,
}
}
func (c *Canvas) setScale(x, y float64) {
c.scaleX = x
c.scaleY = y
}
func (c *Canvas) setOffset(x, y int) {
c.offsetX = x
c.offsetY = y
}
func (c *Canvas) setPad(pad int) {
c.pad = pad
}
// transformPoint converts diagram coordinates to ASCII grid coordinates
func (c *Canvas) transformPoint(x, y int) (int, int) {
x = int(float64(x+c.offsetX+c.pad) / c.scaleX)
y = int(float64(y+c.offsetY+c.pad) / c.scaleY)
return x, y
}
func (c *Canvas) drawShape(shape d2target.Shape) error {
x, y := c.transformPoint(int(shape.Pos.X), int(shape.Pos.Y))
w := int(float64(shape.Width) / c.scaleX)
h := int(float64(shape.Height) / c.scaleY)
switch shape.Type {
case d2target.ShapeCircle:
return c.drawCircle(x, y, w, h, shape.Label)
case d2target.ShapeSquare:
return c.drawRect(x, y, w, h, shape.Label)
// Add more shape types as needed
default:
return c.drawRect(x, y, w, h, shape.Label)
}
}
func (c *Canvas) drawRect(x, y, w, h int, label string) error {
// Draw corners
c.set(x, y, '+')
c.set(x+w, y, '+')
c.set(x, y+h, '+')
c.set(x+w, y+h, '+')
// Draw horizontal edges
for i := x + 1; i < x+w; i++ {
c.set(i, y, '-')
c.set(i, y+h, '-')
}
// Draw vertical edges
for i := y + 1; i < y+h; i++ {
c.set(x, i, '|')
c.set(x+w, i, '|')
}
// Draw label
if label != "" {
c.drawCenteredText(x+1, y+1, w-1, h-1, label)
}
return nil
}
func (c *Canvas) drawCircle(x, y, w, h int, label string) error {
// Approximate circle with ASCII characters
c.set(x+w/2, y, '.')
c.set(x+w/2, y+h, '\'')
c.set(x, y+h/2, '(')
c.set(x+w, y+h/2, ')')
if label != "" {
c.drawCenteredText(x+1, y+1, w-1, h-1, label)
}
return nil
}
func (c *Canvas) drawConnection(conn d2target.Connection) error {
// Draw a simple line between points for now
points := make([]struct{ x, y int }, len(conn.Route))
for i, p := range conn.Route {
points[i].x, points[i].y = c.transformPoint(int(p.X), int(p.Y))
}
for i := 0; i < len(points)-1; i++ {
c.drawLine(points[i].x, points[i].y, points[i+1].x, points[i+1].y)
}
return nil
}
func (c *Canvas) drawLine(x1, y1, x2, y2 int) {
// Draw horizontal line
if y1 == y2 {
for x := min(x1, x2); x <= max(x1, x2); x++ {
c.set(x, y1, '-')
}
return
}
// Draw vertical line
if x1 == x2 {
for y := min(y1, y2); y <= max(y1, y2); y++ {
c.set(x1, y, '|')
}
return
}
// Draw diagonal line
dx := abs(x2 - x1)
dy := abs(y2 - y1)
steep := dy > dx
if steep {
x1, y1 = y1, x1
x2, y2 = y2, x2
}
if x1 > x2 {
x1, x2 = x2, x1
y1, y2 = y2, y1
}
dx = x2 - x1
dy = abs(y2 - y1)
err := dx / 2
ystep := 1
if y1 >= y2 {
ystep = -1
}
for ; x1 <= x2; x1++ {
if steep {
c.set(y1, x1, '/')
} else {
c.set(x1, y1, '/')
}
err -= dy
if err < 0 {
y1 += ystep
err += dx
}
}
}
func (c *Canvas) drawCenteredText(x, y, w, h int, text string) {
lines := strings.Split(text, "\n")
startY := y + (h-len(lines))/2
for i, line := range lines {
if startY+i >= c.h {
break
}
startX := x + (w-len(line))/2
for j, ch := range line {
if startX+j >= c.w {
break
}
c.set(startX+j, startY+i, ch)
}
}
}
func (c *Canvas) set(x, y int, ch rune) {
if x >= 0 && x < c.w && y >= 0 && y < c.h {
c.grid[y][x] = ch
}
}
func (c *Canvas) Bytes() []byte {
var buf bytes.Buffer
for _, row := range c.grid {
buf.WriteString(string(row))
buf.WriteByte('\n')
}
return buf.Bytes()
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}