updated image generation code [WIP]

This commit is contained in:
weckyy702
2021-04-16 11:46:32 +02:00
parent f4c9c77ad8
commit d8dde66d78
5 changed files with 74 additions and 41 deletions

View File

@@ -1,4 +1,4 @@
"""Iinstruction.py: #TODO"""
"""Iinstruction.py: Instruction classes to turn the lexing results into an image"""
__author__ = "Weckyy702"
@@ -14,7 +14,7 @@ class Iinstruction(metaclass=ABCMeta):
self.instruction_text = instruction_text
@abstractmethod
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float]:
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float, float, float]:
pass
@abstractmethod
@@ -40,9 +40,9 @@ class generic_instruction(Iinstruction):
"""Any instruction that is not a control structure"""
def __init__(self, instruction_text: str) -> None:
Iinstruction.__init__(self, instruction_text)
super().__init__(instruction_text)
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float, float, float]:
return cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, self.getblkheight())
def getblkheight(self) -> float:
@@ -55,14 +55,14 @@ class generic_instruction(Iinstruction):
return self.instruction_text
class if_instruction(Iinstruction):
"""Conditional structure
"""Conditional structurewdas
NOT.
A.
LOOP
"""
def __init__(self, instruction_text: str, true_case: List[Iinstruction], false_case: List[Iinstruction]=None) -> None:
Iinstruction.__init__(self, instruction_text)
super().__init__(instruction_text)
self.true_case = true_case
self.false_case = false_case
@@ -76,7 +76,7 @@ class if_instruction(Iinstruction):
sz = 0.0
if self.false_case:
for inst in self.false_case:
sz += inst.getblkwidth()
sz += inst.getblkheight()
return sz
def get_truewidth(self) -> float:
@@ -100,13 +100,25 @@ class if_instruction(Iinstruction):
return self._getblkheight() + max(self.get_trueheight(), self.get_falseheight())
def getblkwidth(self) -> float:
return max(self._getblkwidth(), self.get_truewidth() + self.get_falsewidth())
text_width, true_width, false_width = self.get_widths()
return max(text_width, true_width+false_width)
def get_widths(self) -> Tuple[float, float, float]:
text_width = self._getblkwidth()
true_width = self.get_truewidth()
false_width = self.get_falsewidth()
true_width = max(text_width/2, true_width)
false_width = max(text_width/2, false_width)
true_width = max(text_width-false_width, true_width)
return text_width, true_width, false_width
def to_image(self, x:int, y:int, x_sz: int) -> Tuple[float]:
true_w = self.get_truewidth()
false_w = self.get_falsewidth()
true_x, true_y, false_x, false_y = cti.draw_if_statement(
_, true_w, false_w = self.get_widths()
true_x, true_y, true_w, false_x, false_y, false_w = cti.draw_if_statement(
self.instruction_text, x, y, true_w, false_w, self.getblkheight()
)
self.draw_true_case(true_x, true_y, true_w)
@@ -116,12 +128,12 @@ class if_instruction(Iinstruction):
def draw_true_case(self, x: float, y:float, x_sz:float):
for instruction in self.true_case:
x, y = instruction.to_image(x, y, x_sz)
x, y = instruction.to_image(x, y, x_sz)[0:2]
def draw_false_case(self, x: float, y:float, x_sz:float):
if self.false_case:
for instruction in self.false_case:
x, y = instruction.to_image(x, y, x_sz)
x, y = instruction.to_image(x, y, x_sz)[0:2]
def __str__(self) -> str:
res = f"if({self.instruction_text}) {'{'}\n"
@@ -138,7 +150,7 @@ class if_instruction(Iinstruction):
class while_instruction_front(Iinstruction):
def __init__(self, condition: str, instructions: List[Iinstruction]) -> None:
Iinstruction.__init__(self, condition)
super().__init__(condition)
self.child_instructions = instructions
def get_children_height(self) -> float:
@@ -150,7 +162,7 @@ class while_instruction_front(Iinstruction):
def get_children_width(self) -> float:
w = 0.0
for inst in self.child_instructions:
w += inst.getblkheight()
w = max(w, inst.getblkheight())
return w
def getblkheight(self) -> float:
@@ -168,7 +180,7 @@ class while_instruction_front(Iinstruction):
def draw_children(self, x:float, y:float, x_sz:float):
for inst in self.child_instructions:
x, y = inst.to_image(x, y, x_sz)
x, y = inst.to_image(x, y, x_sz)[0:2]
return self.get_children_height()
def __str__(self) -> str:
@@ -181,7 +193,7 @@ class while_instruction_front(Iinstruction):
class while_instruction_back(while_instruction_front):
def __init__(self, condition: str, instructions: List[Iinstruction]) -> None:
while_instruction_front.__init__(self, condition, instructions)
super().__init__(condition, instructions)
def to_image(self, x:int, y:int, x_sz: int):
children_x, children_y, children_sz_x = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, self.getblkheight())
@@ -201,7 +213,8 @@ class for_instruction(while_instruction_front):
self.variable_instruction = generic_instruction(variable_def) if variable_def else None
def to_image(self, x: int, y: int, x_sz: int) -> Tuple[float]:
x, y = self.variable_instruction.to_image(x, y, x_sz)
if self.variable_instruction:
x, y, _ = self.variable_instruction.to_image(x, y, x_sz)
return super().to_image(x, y, x_sz)
def getblkheight(self) -> float:

View File

@@ -16,14 +16,17 @@ output_img = None
_bkp_font = ImageFont.truetype("res/fonts/NotoSans-Regular.ttf", 12) # ImageFont.load_default()
#in case set_font does funky stuff, backup the original font
ERROR_TEXT = "Output image was not initialized! Make sure to call NSD_init first"
#in case set_font does funky stuff, backup the original font
font = _bkp_font
def NSD_init(x: float, y: float):
#get input_img
global img, output_img
#img = Image.open(input_dir + file_name + datei_endung)
img = Image.new("RGB", (x, y), "white")
output_img = ImageDraw.Draw(img)
@@ -40,18 +43,18 @@ def set_font(font_filepath: str):
def get_text_size(text: str):
if not font:
raise Exception("Output image was not initialized! Make sure to call NSD_init first")
raise Exception(ERROR_TEXT)
return font.getsize(text)
def draw_debug_tile(x, y, x_sz, y_sz):
from random import randint
output_img.rectangle((x, y) + (x + x_sz, y + y_sz), fill=(randint(0, 255)))
return x, y + y_sz
return x, y + y_sz, x_sz
def draw_generic_instruction(instruction: str, x, y, xsize, ysize) -> Iterable[float]:
if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first")
raise Exception(ERROR_TEXT)
#draw shit
output_img.rectangle((x,y) + (x + xsize, y + ysize), outline=(0), width=1)
@@ -59,16 +62,16 @@ def draw_generic_instruction(instruction: str, x, y, xsize, ysize) -> Iterable[f
#text shit
output_img.multiline_text((x + xsize * .5, y + ysize * .5), instruction, font=font, anchor="mm", align="right", fill=(0))
return x, y + ysize
return x, y + ysize, xsize
def draw_if_statement(condition: str, x: int, y: int, true_w: int, false_w: int, ysize: int):
"""Draw an if statement into the NSD"""
if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first")
raise Exception(ERROR_TEXT)
text_sz = font.getsize(condition)
text_h = text_sz[1] + PADDING_Y
text_w = text_sz[0] + PADDING_X
text_h = text_sz[1] + PADDING_Y
box_w = max(text_w, true_w + false_w)
@@ -85,13 +88,13 @@ def draw_if_statement(condition: str, x: int, y: int, true_w: int, false_w: int,
output_img.text((x + 5, y + text_h), "true", font = font, fill = (0), anchor="ld")
output_img.text((x + box_w - 5, y + text_h), "false", font = font, fill = (0), anchor="rd")
#x, and y of "true" and "false" label
return x, y + text_h, x + true_w, y + text_h
#x, y and width of "true" and "false" label
return x, y + text_h, true_w, x + true_w, y + text_h, false_w
def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int):
if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first")
raise Exception(ERROR_TEXT)
text_y_sz = font.getsize(condition)[1]
@@ -106,13 +109,13 @@ def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int
#the text
output_img.text((x + xsize * .1, y + text_y_sz * .5), condition, font = font, fill = (0), anchor="lm")
#the x, y offset then the x,y draw size (the canvas)
#the x, y offset then the children width
return x + xsize * .1, y + text_y_sz, xsize * .9
def draw_while_loop_back(condition: str, x: int, y: int, xsize: int, ysize: int):
if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first")
raise Exception(ERROR_TEXT)
text_y_sz = get_text_size(condition)[1]