less copy protection, more size visualization
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

144 lignes
6.6 KiB

  1. import sys
  2. import re
  3. import json
  4. import os
  5. import subprocess
  6. import pathlib
  7. from xml.dom import minidom
  8. # an affront to god
  9. def combine(base_path, dark_path, medium_path, light_path, output_path):
  10. base = open(base_path, "r", encoding="utf-8").read()
  11. dark = open(dark_path, "r", encoding="utf-8").read()
  12. medium = open(medium_path, "r", encoding="utf-8").read()
  13. light = open(light_path, "r", encoding="utf-8").read()
  14. base_data = re.search("<g.*", base, flags=re.DOTALL)[0][:-7]
  15. dark_data = re.search("<g.*", dark, flags=re.DOTALL)[0][:-7]
  16. medium_data = re.search("<g.*", medium, flags=re.DOTALL)[0][:-7]
  17. light_data = light.replace("</metadata>", "</metadata>" + base_data + "\n" + dark_data + "\n" + medium_data)
  18. with open(output_path, "w", encoding="utf-8") as f:
  19. f.write(light_data)
  20. return subprocess.Popen([INKSCAPE, "--without-gui", "--export-plain-svg=" + output_path, "--export-area-drawing", output_path], shell=False)
  21. configdir = pathlib.Path(__file__).parent
  22. configpath = configdir.joinpath("config.json")
  23. config = json.load(open(configpath, encoding="utf-8"))
  24. workdir = pathlib.Path(config["work-directory"])
  25. print(sys.argv)
  26. sourcedir = workdir.joinpath(sys.argv[1])
  27. macrodir = pathlib.Path(config["macrovision-directory"])
  28. print(sourcedir, macrodir)
  29. side_strings = []
  30. POTRACE = config["potrace"]
  31. INKSCAPE = config["inkscape"]
  32. output = {}
  33. with open(sourcedir.joinpath("data.json"), "r", encoding="utf-8") as file:
  34. all_data = json.load(file)
  35. group_name = all_data["name"]
  36. category = all_data["kind"]
  37. outputdir = macrodir.joinpath("media").joinpath(category).joinpath(group_name)
  38. os.makedirs(outputdir, exist_ok=True)
  39. for data in all_data["forms"]:
  40. name = data["name"]
  41. for view in data["views"]:
  42. view_name = view["name"]
  43. input = sourcedir.joinpath(name + "-" + view_name + ".png").__str__()
  44. input_noline_raw = sourcedir.joinpath(name + "-" + view_name + "-" + "noline.png").__str__()
  45. result = outputdir.joinpath(name + "-" + view_name + ".svg").__str__()
  46. print(result)
  47. if os.path.exists(result) and os.path.getmtime(input) < os.path.getmtime(result):
  48. print("Skipping ", input)
  49. continue
  50. input_base = sourcedir.joinpath(name + "-" + view_name + "-base.bmp").__str__()
  51. input_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.bmp").__str__()
  52. input_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.bmp").__str__()
  53. input_light = sourcedir.joinpath(name + "-" + view_name + "-light.bmp").__str__()
  54. input_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.bmp").__str__()
  55. procs = []
  56. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RGB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-flatten", input_base], shell=False))
  57. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "GB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-negate", "-flatten", input_dark], shell=False))
  58. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RB", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-negate", "-flatten", input_medium], shell=False))
  59. procs.append(subprocess.Popen(["magick", "convert", input, "-channel", "RG", "-evaluate", "set", "0", "-channel", "RGB", "-modulate", "1000,0", "-background", "#FFFFFF", "-negate", "-flatten", input_light], shell=False))
  60. # to correct for extra height from lines
  61. procs.append(subprocess.Popen(["magick", "convert", input_noline_raw, "-channel", "RGB", "-evaluate", "set", "0", "-background", "#FFFFFF", "-flatten", input_noline], shell=False))
  62. [proc.wait() for proc in procs]
  63. output_base = sourcedir.joinpath(name + "-" + view_name + "-base.svg").__str__()
  64. output_dark = sourcedir.joinpath(name + "-" + view_name + "-dark.svg").__str__()
  65. output_medium = sourcedir.joinpath(name + "-" + view_name + "-medium.svg").__str__()
  66. output_light = sourcedir.joinpath(name + "-" + view_name + "-light.svg").__str__()
  67. output_noline = sourcedir.joinpath(name + "-" + view_name + "-noline.svg").__str__()
  68. procs = []
  69. procs.append(subprocess.Popen([POTRACE, input_base, "-b", "svg", "-o", output_base], shell=False))
  70. procs.append(subprocess.Popen([POTRACE, input_dark, "-b", "svg", "-C", "#1a1a1a", "-o", output_dark], shell=False))
  71. procs.append(subprocess.Popen([POTRACE, input_medium, "-b", "svg", "-C", "#333333", "-o", output_medium], shell=False))
  72. procs.append(subprocess.Popen([POTRACE, input_light, "-b", "svg", "-C", "#4d4d4d", "-o", output_light], shell=False))
  73. procs.append(subprocess.Popen([POTRACE, input_noline, "-b", "svg", "-C", "#333333", "-o", output_noline], shell=False))
  74. [proc.wait() for proc in procs]
  75. procs = []
  76. noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__()
  77. procs.append(combine(output_base, output_dark, output_medium, output_light, result))
  78. procs.append(combine(output_noline, output_noline, output_noline, output_noline, noline_result))
  79. [proc.wait() for proc in procs]
  80. # we now learn how much height was added by the lineart!
  81. original_xml = minidom.parse(open(result))
  82. noline_xml = minidom.parse(open(noline_result))
  83. original_height = float(original_xml.childNodes[0].attributes["height"].value[:-2])
  84. noline_height = float(noline_xml.childNodes[0].attributes["height"].value[:-2])
  85. delta = original_height - noline_height
  86. height = original_height
  87. bottom = height - (height - delta / 2)
  88. top = height - delta / 2
  89. view["extra"] = (height - bottom) / (top - bottom)
  90. view["bottom"] = bottom / height
  91. # now we add the data
  92. file_path = macrodir.joinpath("presets").joinpath(category + ".js")
  93. with open(file_path, "r", encoding="utf-8") as file:
  94. lines = file.readlines()
  95. found = False
  96. with open(file_path, "w", encoding="utf-8") as file:
  97. for line in lines:
  98. if f"/* ***{group_name}*** */" in line:
  99. found = True
  100. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  101. elif "/* ***INSERT HERE*** */" in line and not found:
  102. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  103. file.write(line)
  104. else:
  105. file.write(line)