summaryrefslogtreecommitdiff
path: root/tools/parse-layout.js
blob: 6f77adf6bdfec109cd8b959df7770b76eb049da0 (plain)
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
"use strict"

const fs = require("fs")

let points = {}
let boxes = {}
let edges = {}
let mode, group, name, x, y, w, h, cx, cy, rx, ry, x2, y2
let labels = []
let labels_uniq = {}

function add_point(x, y) {
	if (!(group in points))
		points[group] = []
	points[group].push({x,y,name})
}

function add_rect(x, y, w, h) {
	if (!(group in boxes))
		boxes[group] = []
	boxes[group].push({x,y,w,h,name})
}

function add_edge(x1, y1, x2, y2) {
	if (!(group in edges))
		edges[group] = []
	edges[group].push({x1,y1,x2,y2,name})
}

function flush() {
	if (mode === 'path') {
		add_edge(x, y, x2, y2)
	}
	if (mode === 'rect') {
		add_rect(x, y, w, h)
		add_point( x + w/2, y + h/2 )
	}
	if (mode === 'circle') {
		add_rect(cx - rx, cy - ry, rx * 2, ry * 2)
		add_point( cx, cy )
	}
	x = y = x2 = y2 = w = h = cx = cy = rx = ry = 0
	name = null
}

function parse_path_data(path) {
	let cx = 0
	let cy = 0
	let abs = 0
	for (let i = 0; i < path.length;) {
		switch (path[i]) {
		case 'M':
			x2 = x = cx = Number(path[i+1])
			y2 = y = cy = Number(path[i+2])
			i += 3
			abs = true
			break
		case 'm':
			x2 = x = cx = cx + Number(path[i+1])
			y2 = y = cy = cy + Number(path[i+2])
			i += 3
			abs = false
			break
		case 'C':
			x2 = cx = Number(path[i+5])
			y2 = cy = Number(path[i+6])
			i += 7
			abs = true
			break
		case 'L':
			i += 1
			abs = true
			break
		case 'H':
			x2 = cx = Number(path[i+1])
			i += 2
			abs = true
			break
		case 'V':
			y2 = cy = Number(path[i+1])
			i += 2
			abs = true
			break
		case 'c':
			x2 = cx = cx + Number(path[i+5])
			y2 = cy = cy + Number(path[i+6])
			i += 7
			abs = false
			break
		case 'l':
			i += 1
			abs = false
			break
		case 'h':
			x2 = cx = cx + Number(path[i+1])
			i += 2
			abs = false
			break
		case 'v':
			y2 = cy = cy + Number(path[i+1])
			i += 2
			abs = false
			break
		default:
			if (abs) {
				x2 = cx = Number(path[i+0])
				y2 = cy = Number(path[i+1])
			} else {
				x2 = cx = cx + Number(path[i+0])
				y2 = cy = cy + Number(path[i+1])
			}
			i += 2
			break
		}
	}
}

for (let line of fs.readFileSync("tools/layout.svg", "utf-8").split("\n")) {
	line = line.trim()
	if (line.startsWith("<rect")) {
		flush()
		mode = "rect"
		x = y = w = h = 0
	}
	else if (line.startsWith("<ellipse") || line.startsWith("<circle")) {
		flush()
		mode = "circle"
		cx = cy = rx = ry = 0
	}
	else if (line.startsWith("<path")) {
		flush()
		mode = "path"
	}
	else if (line.startsWith("<g")) {
		flush()
		mode = "g"
	}
	else if (line.startsWith('x="'))
		x = (Number(line.split('"')[1]))
	else if (line.startsWith('y="'))
		y = (Number(line.split('"')[1]))
	else if (line.startsWith('width="'))
		w = (Number(line.split('"')[1]))
	else if (line.startsWith('height="'))
		h = (Number(line.split('"')[1]))
	else if (line.startsWith('cx="'))
		cx = (Number(line.split('"')[1]))
	else if (line.startsWith('cy="'))
		cy = (Number(line.split('"')[1]))
	else if (line.startsWith('r="'))
		rx = ry = (Number(line.split('"')[1]))
	else if (line.startsWith('rx="'))
		rx = (Number(line.split('"')[1]))
	else if (line.startsWith('ry="'))
		ry = (Number(line.split('"')[1]))
	else if (line.startsWith('inkscape:label="') && mode === "g")
		group = line.split('"')[1]
	else if (line.startsWith('inkscape:label="') && mode !== "g")
		name = line.split('"')[1]
	else if (line.startsWith('d="'))
		parse_path_data(line.split('"')[1].split(/[ ,]/))
	if (line.includes("</tspan>")) {
		let name = line.replace(/^[^>]*>/, "").replace(/<\/tspan.*/, "")
		if (name in labels_uniq)
			console.log("DUPLICATE LABEL", name)
		labels.push({x, y, name})
		labels_uniq[name] = 1
	}
}

flush()

function find_label(x, y, limit) {
	let nd = Infinity, nn = null

	for (let n of labels) {
		let d = Math.hypot(n.x - x, n.y - y)
		if (d < nd) {
			nd = d
			nn = n
		}
	}

	if (!nn || nd > limit) {
		console.log("LABEL NOT FOUND", x, y)
		return null
	}

	return nn.name
}

for (let group in points) {
	for (let item of points[group]) {
		if (item.name === null) {
			item.name = find_label(item.x, item.y, 72)
		}
	}
}

for (let group in boxes) {
	for (let item of boxes[group]) {
		if (item.name === null) {
			item.name = find_label(item.x+item.w/2, item.y+item.w/2, 72)
		}
	}
}

for (let group in edges) {
	for (let item of edges[group]) {
		item.name1 = find_label(item.x1, item.y1, 72)
		item.name2 = find_label(item.x2, item.y2, 72)
	}
}

fs.writeFileSync("tools/layout.json", JSON.stringify({boxes,points,edges,labels}, null, 4))