less copy protection, more size visualization
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

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