sketch-a-day/2019/sketch_190421a/dbn_generate_letters.py

64 wiersze
2.3 KiB
Python

"""
Alexandre B A Villares
https://abav.lugaralgum.com/sketch-a-day
This script generates dbn_letterss.py
Converting some of Maeda's Design by Number
dbnletters.dbn code -> Processing
"""
def convert_dbn_source_letters(file_path):
with open("dbn_letters.py", 'w') as out:
out.write('"""\n')
out.write("Alexandre B A Villares\n")
out.write("https://abav.lugaralgum.com/sketch-a-day\n")
out.write("This code was generated by dbn_generate_letters.py\n")
out.write("Converting some of Maeda's Design by Number\n")
out.write('dbnletters.dbn code -> Processing\n"""\n')
out.write("dbn_letter = {} # Dict of functions\n")
out.write("\n")
with open(file_path, "r") as f:
dbn_source = f.readlines()
inside_block = False
command_name = ""
command_block = []
for ln in dbn_source:
if ln.count("command"):
command_name = ln[14:15]
elif ln.count("{"):
inside_block = True
elif ln.count("}"):
if command_name in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
def_dbn_letter(command_block, command_name)
command_block = [] # empty block
inside_block = False
elif inside_block:
command_block.append(ln.lstrip())
def def_dbn_letter(dbn_block, key_):
p_block = []
for dbn_line in dbn_block:
if dbn_line:
# for debug
p_block.append(" if debug_poly: stroke(random(256),200, 200)\n")
p_block.append(" " + dbn_line
.replace("line ", "line(")
.replace(" ", ",")
.replace("//", "#")
.strip()
+ ")\n")
with open("dbn_letters.py", 'a') as out:
out.write("# " + key_ + "\n")
out.write("def dbn_letter" + key_ + "(h, v, debug_poly=False):\n")
out.write(" pushMatrix()\n")
out.write(" scale(1, -1)\n")
for py_processing_line in p_block:
out.write(py_processing_line)
out.write(" popMatrix()\n")
out.write("dbn_letter['" + key_ + "'] = dbn_letter" + key_ + "\n")
out.write("dbn_letter[" + str(ord(key_) - 64)
+ "] = dbn_letter" + key_ + "\n")