diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-04-27 15:54:04 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-04-27 15:54:04 +0200 |
commit | 3b7c6a085a7e8d2b91ce063349ed9829eee72385 (patch) | |
tree | 5da537e506a754bc490a0e4d7b3b2cd3e09bb19a /maps/mappdf.py | |
parent | 4d58ddd03b22dab33f37d1916fc94e6d6a875c87 (diff) |
add mappdf script
Diffstat (limited to 'maps/mappdf.py')
-rwxr-xr-x | maps/mappdf.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/maps/mappdf.py b/maps/mappdf.py new file mode 100755 index 0000000..4f4a45d --- /dev/null +++ b/maps/mappdf.py @@ -0,0 +1,56 @@ +import sys +from struct import unpack +import svgutils.transform as sg +import svgutils.compose as sc + +SVG_TILE_SIZE = 24 +DPI = 96 +INCH = 2.54 +ROBOT_SIZE = 9.5 +ROBOT_SIZE_UNIT = "cm" + +RESIZE_FACTOR = ROBOT_SIZE / (SVG_TILE_SIZE / DPI * INCH) + +def read_tiles(): + tiles = [] + for x in range(16): + tile = open(f"./tiles/{x:x}.svg", "r") + tiles.append(tile.read()) + tile.close() + return tiles + +def mappdf(filename): + file = open(filename, 'rb') + contents = file.read() + file.close() + tiles = read_tiles() + + width, height = unpack('>HH', contents[0:4]) + + target = sg.SVGFigure() + target.set_size((f"{width * SVG_TILE_SIZE}px", f"{height * SVG_TILE_SIZE}px")) + + for y in range(height): + for x in range(width): + nibble = x + y * width + byte = int(nibble / 2) + shift = byte * 2 - nibble + 1 + num = (contents[4 + byte] & (0xf << 4 * shift)) >> 4 * shift + + tile = sg.fromstring(tiles[num]).getroot() + tile.moveto(x * SVG_TILE_SIZE, y * SVG_TILE_SIZE) + target.append(tile) + + target.save(f"{filename}.svg") + target_scaled = sc.SVG(f"{filename}.svg") + target_scaled.scale(RESIZE_FACTOR) + target = sc.Figure(width * SVG_TILE_SIZE * RESIZE_FACTOR, height * SVG_TILE_SIZE * RESIZE_FACTOR, target_scaled) + target.save(f"{filename}.svg") + print(f"inkscape --export-pdf={filename}.pdf {filename}.svg") + + +if __name__ == "__main__": + if len(sys.argv) > 1: + mappdf(sys.argv[1]) + else: + exit(1) |