forked from stathissideris/ditaa
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiagramshape.go
208 lines (191 loc) · 5.49 KB
/
diagramshape.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
package main
import (
"fmt"
"github.com/akavel/ditaa/graphical"
)
func NewSmallLine(grid *TextGrid, c Cell, gg graphical.Grid) *graphical.Shape {
cc := graphical.Cell(c)
switch {
case grid.IsHorizontalLine(c):
return graphical.NewShape(
graphical.Point{X: gg.CellMinX(cc), Y: gg.CellMidY(cc)},
graphical.Point{X: gg.CellMaxX(cc) - 1, Y: gg.CellMidY(cc)},
)
case grid.IsVerticalLine(c):
return graphical.NewShape(
graphical.Point{X: gg.CellMidX(cc), Y: gg.CellMinY(cc)},
graphical.Point{X: gg.CellMidX(cc), Y: gg.CellMaxY(cc) - 1},
)
}
return nil
}
func ConnectEndsToAnchors(s *graphical.Shape, grid *TextGrid, gg graphical.Grid) {
if s.Closed {
return
}
n := len(s.Points)
// println(n)
for _, line := range []struct{ end, next *graphical.Point }{
{&s.Points[0], &s.Points[1]},
{&s.Points[n-1], &s.Points[n-2]},
} {
var x, y float64
switch {
case line.next.NorthOf(*line.end):
x, y = line.end.X, line.end.Y+float64(gg.CellH)
case line.next.SouthOf(*line.end):
x, y = line.end.X, line.end.Y-float64(gg.CellH)
case line.next.WestOf(*line.end):
x, y = line.end.X+float64(gg.CellW), line.end.Y
case line.next.EastOf(*line.end):
x, y = line.end.X-float64(gg.CellW), line.end.Y
}
anchor := gg.CellFor(graphical.Point{X: x, Y: y})
c := Cell(anchor)
if grid.IsArrowhead(c) || grid.IsCorner(c) || grid.IsIntersection(c) {
line.end.X, line.end.Y = gg.CellMidX(anchor), gg.CellMidY(anchor)
line.end.Locked = true
}
}
}
func createOpenFromBoundaryCells(grid *TextGrid, cells *CellSet, gg graphical.Grid, allCornersRound bool) []graphical.Shape {
if cells.Type(grid) != SET_OPEN {
panic("CellSet is closed and cannot be handled by this method")
}
if len(cells.Set) == 0 {
return []graphical.Shape{}
}
shapes := []graphical.Shape{}
workGrid := NewTextGrid(grid.Width(), grid.Height())
CopySelectedCells(workGrid, cells, grid)
// if DEBUG {
// fmt.Println("Making composite shape from grid:")
// workGrid.printDebug()
// }
visited := NewCellSet()
for c := range cells.Set {
// fmt.Println("cell", c)
if workGrid.IsLinesEnd(c) {
// fmt.Println("- is lines end")
nextCells := workGrid.FollowCell(c, nil)
// fmt.Println("- nextCells", nextCells)
shapes = append(shapes, growEdgesFromCell(workGrid, gg, allCornersRound, nextCells.SomeCell(), c, visited)...)
break
}
}
//dashed shapes should "infect" the rest of the shapes
dashedShapeExists := false
for _, s := range shapes {
if s.Dashed {
dashedShapeExists = true
break
}
}
if dashedShapeExists {
for i := range shapes {
shapes[i].Dashed = true
}
}
return shapes
}
// func callfromline() int {
// _, _, line, _ := runtime.Caller(1)
// return line
// }
func growEdgesFromCell(grid *TextGrid, gg graphical.Grid, allCornersRound bool, c, prev Cell, visited *CellSet) []graphical.Shape {
result := []graphical.Shape{}
visited.Add(prev)
shape := graphical.NewShape(makePointForCell(prev, grid, gg, allCornersRound))
// if DEBUG {
// fmt.Printf("point at %s (call from line: %d)", prev, callfromline())
// }
if grid.CellContainsDashedLineChar(prev) {
shape.Dashed = true
}
for finished := false; !finished; {
visited.Add(c)
if grid.IsPointCell(c) {
shape.Points = append(shape.Points, makePointForCell(c, grid, gg, allCornersRound))
}
if grid.CellContainsDashedLineChar(c) {
shape.Dashed = true
}
if grid.IsLinesEnd(c) {
finished = true
}
nextCells := grid.FollowCell(c, &prev)
if len(nextCells.Set) == 1 {
prev = c
c = nextCells.SomeCell()
} else { // 3- or 4- way intersection
finished = true
for nextCell := range nextCells.Set {
result = append(result, growEdgesFromCell(grid, gg, allCornersRound, nextCell, c, visited)...)
}
}
}
result = append(result, *shape)
return result
}
func makePointForCell(c Cell, grid *TextGrid, gg graphical.Grid, allCornersRound bool) graphical.Point {
var typ graphical.PointType
switch {
case grid.IsCorner(c) && allCornersRound:
typ = graphical.POINT_ROUND
case grid.IsNormalCorner(c):
typ = graphical.POINT_NORMAL
case grid.IsRoundCorner(c):
typ = graphical.POINT_ROUND
case grid.IsLinesEnd(c) || grid.IsIntersection(c):
typ = graphical.POINT_NORMAL
default:
panic(fmt.Sprint("Cannot make point for cell", c))
}
return graphical.Point{
X: gg.CellMidX(graphical.Cell(c)),
Y: gg.CellMidY(graphical.Cell(c)),
Type: typ,
}
}
func createArrowhead(grid *TextGrid, c Cell, gg graphical.Grid) *graphical.Shape {
if !grid.IsArrowhead(c) {
return nil
}
BLACK := graphical.Color{A: 255}
s := graphical.Shape{
Closed: true,
FillColor: &BLACK,
StrokeColor: BLACK,
Type: graphical.TYPE_ARROWHEAD,
}
cc := graphical.Cell(c)
switch {
case grid.IsNorthArrowhead(c):
s.Points = []graphical.Point{
{X: gg.CellMidX(cc), Y: gg.CellMinY(cc)},
{X: gg.CellMinX(cc), Y: gg.CellMaxY(cc)},
{X: gg.CellMaxX(cc), Y: gg.CellMaxY(cc)},
}
case grid.IsSouthArrowhead(c):
s.Points = []graphical.Point{
{X: gg.CellMinX(cc), Y: gg.CellMinY(cc)},
{X: gg.CellMidX(cc), Y: gg.CellMaxY(cc)},
{X: gg.CellMaxX(cc), Y: gg.CellMinY(cc)},
}
case grid.IsWestArrowhead(c):
s.Points = []graphical.Point{
{X: gg.CellMaxX(cc), Y: gg.CellMinY(cc)},
{X: gg.CellMinX(cc), Y: gg.CellMidY(cc)},
{X: gg.CellMaxX(cc), Y: gg.CellMaxY(cc)},
}
case grid.IsEastArrowhead(c):
s.Points = []graphical.Point{
{X: gg.CellMinX(cc), Y: gg.CellMinY(cc)},
{X: gg.CellMaxX(cc), Y: gg.CellMidY(cc)},
{X: gg.CellMinX(cc), Y: gg.CellMaxY(cc)},
}
default:
return nil
}
return &s
}