cff-displays/regio_generator.py

54 lines
1.4 KiB
Python
Raw Permalink Normal View History

2024-04-09 14:36:22 +00:00
import re
WIDTH = 8.677
def generate_from_num(num):
2024-04-10 16:42:32 +00:00
re_path = f"icons/re-{num}.svg"
r_path = f"icons/r-{num}.svg"
generate(re_path, r_path)
2024-04-09 14:36:22 +00:00
2024-04-10 16:42:32 +00:00
re_path = f"icons/re-{num}-negative.svg"
r_path = f"icons/r-{num}-negative.svg"
generate(re_path, r_path)
2024-04-09 14:36:22 +00:00
def generate(re_path, r_path):
2024-04-10 16:42:32 +00:00
with open(re_path, "r") as re_f:
re_svg = re_f.read()
r_svg = convert_svg(re_svg)
with open(r_path, "w") as r_f:
r_f.write(r_svg)
2024-04-09 14:36:22 +00:00
def convert_svg(re_svg):
2024-04-10 16:42:32 +00:00
# Remove E
path_match = list(re.finditer("path d=\"(.*?)\"", re_svg))[1]
path_pos = path_match.span()
path_pos = (path_pos[0] + 8, path_pos[1] - 1)
path = path_match.group(1)
E_pos = list(re.finditer("M.*?Z", path))[1].span()
path = path[:E_pos[0]] + path[E_pos[1]:]
# Move digits left
R, path = path.split("Z", 1)
digits = list(re.finditer("[MHL](\d+(\.\d+)?)", path))[::-1]
for digit in digits:
pos = digit.span()
x = float(digit.group(1))
x -= WIDTH
path = path[:pos[0]+1] + str(x) + path[pos[0] + len(digit.group(1)):]
path = R + "Z" + path
re_svg = re_svg[:path_pos[0]] + path + re_svg[path_pos[1]:]
return re_svg
2024-04-09 14:36:22 +00:00
if __name__ == "__main__":
2024-04-10 16:42:32 +00:00
for i in range(1, 100):
generate_from_num(i)
2024-04-09 14:36:22 +00:00
2024-04-10 16:42:32 +00:00
generate("icons/re.svg", "icons/r.svg")
generate("icons/re-negative.svg", "icons/r-negative.svg")