started reworking image generation

This commit is contained in:
weckyy702
2020-12-26 17:45:44 +01:00
parent 2af5d22495
commit 9d748c289d
4 changed files with 119 additions and 84 deletions

View File

@@ -11,9 +11,16 @@ class Iinstruction:
self.instruction_text = instruction_text self.instruction_text = instruction_text
@abstractmethod @abstractmethod
def to_image(self, x:float, y:float, x_sz: float, y_sz: float) -> Iterable[float]: def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
pass pass
@abstractmethod
def getblksize(self) -> float:
return self._getblksize()
def _getblksize(self) -> float:
return cti.get_text_size(self.instruction_text)[1]
class generic_instruction(Iinstruction): class generic_instruction(Iinstruction):
"""Any instruction that is not a control structure""" """Any instruction that is not a control structure"""
@@ -21,9 +28,8 @@ class generic_instruction(Iinstruction):
def __init__(self, instruction_text: str) -> None: def __init__(self, instruction_text: str) -> None:
Iinstruction.__init__(self, instruction_text) Iinstruction.__init__(self, instruction_text)
def to_image(self, x:int, y:int, x_sz: int, y_sz: int) -> Iterable[float]: def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
new_x, new_y = cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, y_sz) return cti.draw_generic_instruction(self.instruction_text, x, y, x_sz, self.getblksize())
return new_x, new_y
@@ -39,30 +45,39 @@ class if_instruction(Iinstruction):
self.true_case = true_case self.true_case = true_case
self.false_case = false_case self.false_case = false_case
def to_image(self, x:int, y:int, x_sz: int, y_sz: int) -> Iterable[float]: def get_truesize(self) -> float:
true_x, true_y, true_sz_x, true_sz_y, false_x, false_y, false_sz_x, false_sz_y = cti.draw_if_statement( sz = 0.0
self.instruction_text, for inst in self.true_case:
x, y, x_sz, y_sz) sz += inst.getblksize()
return sz
true_blk_sz = self.draw_true_case(true_x, true_y, true_sz_x, true_sz_y) def get_falsesize(self) -> float:
false_blk_sz = self.draw_false_case(false_x, false_y, false_sz_x, false_sz_y) sz = 0.0
blk_sz = max(true_blk_sz, false_blk_sz) if self.false_case:
return x, true_y + blk_sz for inst in self.false_case:
sz += inst.getblksize()
def draw_true_case(self, x: float, y:float, x_sz:float, y_sz:float) -> float: return sz
start_y = y
y_sz /= len(self.true_case) def getblksize(self) -> float:
for instruction in self.true_case: return self._getblksize() + max(self.get_truesize(), self.get_falsesize())
x, y = instruction.to_image(x, y, x_sz, y_sz)
return y - start_y def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
true_x, true_y, true_sz_x, _, false_x, false_y, false_sz_x, _ = cti.draw_if_statement(
def draw_false_case(self, x: float, y:float, x_sz:float, y_sz:float) -> float: self.instruction_text, x, y, x_sz, self.getblksize()
start_y = y )
self.draw_true_case(true_x, true_y, true_sz_x)
self.draw_false_case(false_x, false_y, false_sz_x)
blk_size = self.getblksize()
return x, y + blk_size
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)
def draw_false_case(self, x: float, y:float, x_sz:float):
if self.false_case: if self.false_case:
y_sz /= len(self.false_case)
for instruction in self.false_case: for instruction in self.false_case:
x, y = instruction.to_image(x, y, x_sz, y_sz) x, y = instruction.to_image(x, y, x_sz)
return y - start_y
#TODO #TODO
# class switch_instruction(Iinstruction): # class switch_instruction(Iinstruction):
@@ -88,33 +103,40 @@ class while_instruction_front(Iinstruction):
Iinstruction.__init__(self, condition) Iinstruction.__init__(self, condition)
self.child_instructions = instructions self.child_instructions = instructions
def to_image(self, x:int, y:int, x_sz: int, y_sz: int): def get_children_size(self) -> float:
children_x, children_y, children_sz_x, children_sz_y = cti.draw_while_loop_front(self.instruction_text, x, y, x_sz, y_sz) children_sz = 0
blk_size = self.draw_children(children_x, children_y, children_sz_x, children_sz_y) for inst in self.child_instructions:
return x, y + blk_size children_sz += inst.getblksize()
return children_sz
def draw_children(self, x:float, y:float, x_sz:float, y_sz:float) -> float: def getblksize(self) -> float:
y_sz /= len(self.child_instructions) return self._getblksize() + self.get_children_size()
for instruction in self.child_instructions:
x, y = instruction.to_image(x, y, x_sz, y_sz)
return y_sz
def to_image(self, x:int, y:int, x_sz: int) -> Iterable[float]:
children_x, children_y, children_sz_x = cti.draw_while_loop_front(self.instruction_text, x, y, x_sz, self.getblksize())
self.draw_children(children_x, children_y, children_sz_x)
return x, y + self.getblksize()
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)
return self.get_children_size()
class while_instruction_back(while_instruction_front): class while_instruction_back(while_instruction_front):
def __init__(self, condition: str, instructions: List[Iinstruction]) -> None: def __init__(self, condition: str, instructions: List[Iinstruction]) -> None:
while_instruction_front.__init__(self, condition, instructions) while_instruction_front.__init__(self, condition, instructions)
def to_image(self, x:int, y:int, x_sz: int, y_sz: int): def to_image(self, x:int, y:int, x_sz: int):
children_x, children_y, children_sz_x, children_sz_y = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, y_sz) children_x, children_y, children_sz_x, children_sz_y = cti.draw_while_loop_back(self.instruction_text, x, y, x_sz, y_sz)
blk_size = self.draw_children(children_x, children_y, children_sz_x, children_sz_y) blk_size = self.draw_children(children_x, children_y, children_sz_x)
return x, y + blk_size return x
def draw_children(self, x:float, y:float, x_sz:float, y_sz:float) -> float: def draw_children(self, x:float, y:float, x_sz:float):
y_sz /= len(self.child_instructions)
for instruction in self.child_instructions: for instruction in self.child_instructions:
x, y = instruction.to_image(x, y, x_sz, y_sz) x, y = instruction.to_image(x, y, x_sz)
return y_sz
if __name__ == "__main__": if __name__ == "__main__":
@@ -130,5 +152,5 @@ if __name__ == "__main__":
if_instruction("shouldNiet()", [ generic_instruction("hiet()") ], [generic_instruction("hiet()")]), if_instruction("shouldNiet()", [ generic_instruction("hiet()") ], [generic_instruction("hiet()")]),
]) ])
cti.NSD_init(500, 500) cti.NSD_init(500, 500)
test.to_image(0, 0, 500, 500) test.to_image(0, 0, 500)
cti.NSD_save("Iinstruction") cti.NSD_save("Iinstruction")

View File

@@ -16,15 +16,26 @@ def NSD_init(x: float, y: float):
img = Image.new("RGB", (x, y), "white") img = Image.new("RGB", (x, y), "white")
output_img = ImageDraw.Draw(img) output_img = ImageDraw.Draw(img)
#font = ImageFont.load_default() #font = ImageFont.load_default()
font = ImageFont.truetype("res/fonts/SpaceGrotesk-Light.ttf", 12) font = ImageFont.truetype("res/fonts/SpaceGrotesk-Light.ttf", 14)
def get_text_size(text: str):
if not font:
raise Exception("Output image was not initialized! Make sure to call NSD_init first")
return font.getsize(text, direction="ltr")
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
def draw_generic_instruction(instruction: str, x, y, xsize, ysize) -> Iterable[float]: def draw_generic_instruction(instruction: str, x, y, xsize, ysize) -> Iterable[float]:
if not output_img: if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first") raise Exception("Output image was not initialized! Make sure to call NSD_init first")
#size shit #size shit
#text_y_size = font.getsize(instruction, direction="ltr")[1] text_y_size = font.getsize(instruction, direction="ltr")[1]
#ysize = max(text_y_size, ysize) # ensure it is alway at least big enought to fit the text ysize = max(text_y_size, ysize) # ensure the bounding box is alway at least big enought to fit the text
#draw shit #draw shit
output_img.rectangle((x,y) + (x + xsize, y + ysize), outline=(0), width=1) output_img.rectangle((x,y) + (x + xsize, y + ysize), outline=(0), width=1)
@@ -42,42 +53,44 @@ def draw_if_statement(condition: str, x: int, y: int, xsize: int, ysize: int):
if not output_img: if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first") raise Exception("Output image was not initialized! Make sure to call NSD_init first")
output_img.line((x,y) + (x + xsize / 2, y + ysize / 4), fill=(0)) text_y_size = font.getsize(condition)[1]
output_img.line((x + xsize, y) + (x + xsize / 2, y + ysize / 4), fill=(0))
output_img.rectangle((x, y + ysize / 4) + (x + xsize, y + ysize), outline=(0), width=1) output_img.line((x,y) + (x + xsize / 2, y + text_y_size), fill=(0))
output_img.rectangle((x, y) + (x + xsize, y + ysize / 4), outline=(0), width=1) output_img.line((x + xsize, y) + (x + xsize / 2, y + text_y_size), fill=(0))
output_img.line((x + xsize / 2, y + ysize / 4) + (x + xsize / 2, y + ysize), fill=(0)) output_img.rectangle((x, y + text_y_size) + (x + xsize, y + ysize), outline=(0), width=1)
output_img.rectangle((x, y) + (x + xsize, y + text_y_size), outline=(0), width=1)
output_img.line((x + xsize / 2, y + text_y_size) + (x + xsize / 2, y + ysize), fill=(0))
# condition text # condition text
output_img.multiline_text((x + xsize / 2, y + ysize * .05 ), condition, fill=(0), font=font, anchor="mm", spacing=4, align='right') output_img.multiline_text((x + xsize / 2, y + text_y_size), condition, fill=(0), font=font, anchor="md", spacing=4, align='right')
# true / false # true / false
output_img.text((x + 5, y + ysize * .1875), "true", font = font, fill = (0), anchor="lm") output_img.text((x + 5, y + text_y_size), "true", font = font, fill = (0), anchor="ld")
output_img.text((x + xsize - 5, y + ysize * .1875), "false", font = font, fill = (0), anchor="rm") output_img.text((x + xsize - 5, y + text_y_size), "false", font = font, fill = (0), anchor="rd")
#first x,y,xsize,ysize of first box then of second first true and then false #first x,y,xsize,ysize of first box then of second first true and then false
return x, y + ysize / 4, xsize / 2, ysize * .75, x + xsize / 2, y + ysize / 4, xsize / 2, ysize * .75 return x, y + text_y_size, xsize / 2, ysize - text_y_size, x + xsize / 2, y + text_y_size, xsize / 2, ysize - text_y_size
def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int): def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int):
if not output_img: if not output_img:
raise Exception("Output image was not initialized! Make sure to call NSD_init first") raise Exception("Output image was not initialized! Make sure to call NSD_init first")
#ole #TODO text_y_sz = font.getsize(condition)[1]
#the box #the box
output_img.line((x,y) + (x + xsize, y), fill=(0)) output_img.line((x,y) + (x + xsize, y), fill=(0))
output_img.line((x,y) + (x, y + ysize), fill=(0)) output_img.line((x,y) + (x, y + ysize), fill=(0))
output_img.line((x + xsize * .1, y + ysize * .1) + (x + xsize, y + ysize * .1), fill=(0)) output_img.line((x + xsize * .1, y + text_y_sz) + (x + xsize, y + text_y_sz), fill=(0))
output_img.line((x + xsize, y) + (x + xsize, y + ysize * .1), fill=(0)) output_img.line((x + xsize, y) + (x + xsize, y + text_y_sz), fill=(0))
output_img.line((x, y + ysize) + (x + xsize * .1, y + ysize ), fill=(0)) output_img.line((x, y + ysize) + (x + xsize * .1, y + ysize ), fill=(0))
output_img.line((x + xsize * .1, y + ysize) + (x + xsize * .1, y + ysize * .1), fill=(0)) output_img.line((x + xsize * .1, y + ysize) + (x + xsize * .1, y + text_y_sz), fill=(0))
#the text #the text
output_img.text((x + xsize * .1, y + ysize * .025), condition, font = font, fill = (0), anchor="lm") output_img.text((x + xsize * .1, y + text_y_sz * 0.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 x,y draw size (the canvas)
return x + xsize * .1, y + ysize * .1, xsize * .9, ysize * .9 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): def draw_while_loop_back(condition: str, x: int, y: int, xsize: int, ysize: int):

View File

@@ -29,7 +29,7 @@ class NassiShneidermanDiagram:
cti.NSD_init(x_size, 5000) cti.NSD_init(x_size, 5000)
x, y, x_sz = 0, 0, x_size x, y, x_sz = 0, 0, x_size
for _k, instruction in self.instructions.items(): for _k, instruction in self.instructions.items():
x, y = instruction.to_image(x, y, x_sz, 800) x, y = instruction.to_image(x, y, x_sz)
cti.NSD_save(filename) cti.NSD_save(filename)
def load_from_file(self, filepath:str): def load_from_file(self, filepath:str):

View File

@@ -1,29 +1,29 @@
fahre1(); // fahre1();
fahre2(); // fahre2();
while(shouldNiet()) // while(shouldNiet())
{ // {
niet4(); // niet4();
niet5(); // niet5();
if(if6) // if(if6)
{ // {
niet7(); // niet7();
niet8(); // niet8();
} else // } else
{ // {
niet10(); // niet10();
niet11(); // niet11();
} // }
niet13(); // niet13();
} // }
niet15(); // niet15();
niet16(); // niet16();
do{ // do{
niet21(); // niet21();
niet22(); // niet22();
}while(bool23); // }while(bool23);
drehe("links"); drehe("links");
while(huegelVorhanden("rechts")) while(huegelVorhanden("rechts"))