summaryrefslogtreecommitdiff
path: root/tools/genboxes.js
blob: 356955bcb9a387662f10995ee88ac91cf7ef87dc (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
const fs = require("fs")

const { round, floor, ceil } = Math

let boxes = {}
let mode, name, x, y, w, h, cx, cy, rx, ry

function flush() {
	if (mode === 'rect') {
		boxes[name] = [ x, y, w, h ]
	}
	if (mode === 'circle') {
		x = cx - rx
		y = cy - ry
		w = rx * 2
		h = ry * 2
		boxes[name] = [ x, y, w, h ]
	}
	x = y = w = h = cx = cy = rx = ry = 0
	name = null
}

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")) {
		flush()
		mode = "circle"
		cx = cy = rx = ry = 0
	} else if (line.startsWith('x="'))
		x = round(Number(line.split('"')[1]))
	else if (line.startsWith('y="'))
		y = round(Number(line.split('"')[1]))
	else if (line.startsWith('width="'))
		w = round(Number(line.split('"')[1]))
	else if (line.startsWith('height="'))
		h = round(Number(line.split('"')[1]))
	else if (line.startsWith('cx="'))
		cx = round(Number(line.split('"')[1]))
	else if (line.startsWith('cy="'))
		cy = round(Number(line.split('"')[1]))
	else if (line.startsWith('rx="'))
		rx = round(Number(line.split('"')[1]))
	else if (line.startsWith('ry="'))
		ry = round(Number(line.split('"')[1]))
	else if (line.startsWith('inkscape:label="'))
		name = line.split('"')[1]
}

flush()

console.log("const boxes = {")
for (let key in boxes)
	console.log("\t" + key + ": " + JSON.stringify(boxes[key]) + ",")
console.log("}")