summaryrefslogtreecommitdiff
path: root/tools/genlayout.py
blob: 8782af38be222070bb3a7fec3eceb3a0f7a2c991 (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
from bs4 import BeautifulSoup

SCALE = 1.0

def readsvg(filename):
    with open(filename) as fp:
        soup = BeautifulSoup(fp, features="xml")

    result = []
    for group in ['Regions', 'States']:
        boxes = soup.find('g', id=group)
        for box in boxes.find_all('g', recursive=False):
            name = box.attrs['id']
            rect = box.find('rect')
            x = float(rect.attrs['x'])
            y = float(rect.attrs['y'])
            w = float(rect.attrs['width'])
            h = float(rect.attrs['height'])
            xc = round((x+w/2.0)*SCALE)
            yc = round((y+h/2.0)*SCALE)
            result.append([name, xc, yc])

    return result

def print_list(data):
    print("const LAYOUT = {")
    for (name, x, y) in data:
        print(f'\t"{name}": [{x}, {y}],')
    print("}")

result = readsvg("tools/layout.svg")
print_list(result)