less copy protection, more size visualization
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

146 строки
6.7 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. alpha = str(all_data["trace_alpha"])
  70. procs.append(subprocess.Popen([POTRACE, input_base, "-b", "svg", "-a", alpha, "-o", output_base], shell=False))
  71. procs.append(subprocess.Popen([POTRACE, input_dark, "-b", "svg", "-a", alpha, "-C", "#1a1a1a", "-o", output_dark], shell=False))
  72. procs.append(subprocess.Popen([POTRACE, input_medium, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_medium], shell=False))
  73. procs.append(subprocess.Popen([POTRACE, input_light, "-b", "svg", "-a", alpha, "-C", "#4d4d4d", "-o", output_light], shell=False))
  74. procs.append(subprocess.Popen([POTRACE, input_noline, "-b", "svg", "-a", alpha, "-C", "#333333", "-o", output_noline], shell=False))
  75. [proc.wait() for proc in procs]
  76. procs = []
  77. noline_result = sourcedir.joinpath(name + "-" + view_name + "-noline_processed.svg").__str__()
  78. procs.append(combine(output_base, output_dark, output_medium, output_light, result))
  79. procs.append(combine(output_noline, output_noline, output_noline, output_noline, noline_result))
  80. [proc.wait() for proc in procs]
  81. # we now learn how much height was added by the lineart!
  82. original_xml = minidom.parse(open(result))
  83. noline_xml = minidom.parse(open(noline_result))
  84. original_height = float(original_xml.childNodes[0].attributes["height"].value[:-2])
  85. noline_height = float(noline_xml.childNodes[0].attributes["height"].value[:-2])
  86. delta = original_height - noline_height
  87. height = original_height
  88. bottom = height - (height - delta / 2)
  89. top = height - delta / 2
  90. view["extra"] = (height - bottom) / (top - bottom)
  91. view["bottom"] = bottom / height
  92. # now we add the data
  93. file_path = macrodir.joinpath("presets").joinpath(category + ".js")
  94. with open(file_path, "r", encoding="utf-8") as file:
  95. lines = file.readlines()
  96. found = False
  97. with open(file_path, "w", encoding="utf-8") as file:
  98. for line in lines:
  99. if f"/* ***{group_name}*** */" in line:
  100. found = True
  101. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  102. elif "/* ***INSERT HERE*** */" in line and not found:
  103. file.write(f" /* ***{group_name}*** */ results.push(makeModel({json.dumps(all_data)}));\n")
  104. file.write(line)
  105. else:
  106. file.write(line)