import re

WIDTH = 8.677

def generate_from_num(num):
    re_path = f"icons/re-{num}.svg"
    r_path = f"icons/r-{num}.svg"
    generate(re_path, r_path)

    re_path = f"icons/re-{num}-negative.svg"
    r_path = f"icons/r-{num}-negative.svg"
    generate(re_path, r_path)

def generate(re_path, r_path):
    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)

def convert_svg(re_svg):
    # 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

if __name__ == "__main__":
    for i in range(1, 100):
        generate_from_num(i)

    generate("icons/re.svg", "icons/r.svg")
    generate("icons/re-negative.svg", "icons/r-negative.svg")