fixed alignement issues in the image generation

This commit is contained in:
weckyy702
2020-12-26 18:20:42 +01:00
parent 03bff02fdf
commit dc7e93741d
4 changed files with 23 additions and 18 deletions

View File

@@ -5,7 +5,7 @@ datei_endung = ".png"
img = None
output_img = None
font = None
font = ImageFont.truetype("res/fonts/SpaceGrotesk-Light.ttf", 12)
def NSD_init(x: float, y: float):
@@ -14,8 +14,6 @@ def NSD_init(x: float, y: float):
#img = Image.open(input_dir + file_name + datei_endung)
img = Image.new("RGB", (x, y), "white")
output_img = ImageDraw.Draw(img)
#font = ImageFont.load_default()
font = ImageFont.truetype("res/fonts/SpaceGrotesk-Light.ttf", 12)
def get_text_size(text: str):
if not font:
@@ -36,7 +34,7 @@ def draw_generic_instruction(instruction: str, x, y, xsize, ysize) -> Iterable[f
output_img.rectangle((x,y) + (x + xsize, y + ysize), outline=(0), width=1)
#text shit
output_img.multiline_text((x + 5, y + ysize * .5), instruction, font=font, anchor="lm", align="right", fill=(0))
output_img.multiline_text((x + xsize * .5, y + ysize * .5), instruction, font=font, anchor="mm", align="right", fill=(0))
return x, y + ysize
@@ -57,7 +55,7 @@ def draw_if_statement(condition: str, x: int, y: int, xsize: int, ysize: int):
output_img.line((x + xsize / 2, y + text_y_size) + (x + xsize / 2, y + ysize), fill=(0))
# condition text
output_img.multiline_text((x + xsize / 2, y + text_y_size), condition, fill=(0), font=font, anchor="md", spacing=4, align='right')
output_img.multiline_text((x + xsize / 2, y + text_y_size / 2), condition, fill=(0), font=font, anchor="mm", spacing=4, align='right')
# true / false
output_img.text((x + 5, y + text_y_size), "true", font = font, fill = (0), anchor="ld")
@@ -82,7 +80,7 @@ def draw_while_loop_front(condition: str, x: int, y: int, xsize: int, ysize: int
output_img.line((x + xsize * .1, y + ysize) + (x + xsize * .1, y + text_y_sz), fill=(0))
#the text
output_img.text((x + xsize * .1, y + text_y_sz * 0.5), condition, font = font, fill = (0), anchor="lm")
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)
return x + xsize * .1, y + text_y_sz, xsize * .9

View File

@@ -6,7 +6,7 @@ from draw.Iinstruction import *
def nassi(filepath:str, output_path: str, gui):
print('')
NSD = NassiShneidermanDiagram(gui=gui)
NSD = NassiShneidermanDiagram(gui.debug_mode)
NSD.load_from_file(filepath)
NSD.convert_to_image(output_path + "/Nassi-Shneider-Diagramm", 500)

View File

@@ -8,9 +8,9 @@ from draw import code_to_image as cti
class NassiShneidermanDiagram:
def __init__(self, gui):
self.instructions: dict[str, Iinstruction] = {}
self.init_logging(gui.debug_mode)
def __init__(self, do_debug: bool):
self.instructions: List[Iinstruction] = []
self.init_logging(do_debug)
def init_logging(self, debug: bool):
logLevel = logging.INFO
@@ -20,16 +20,23 @@ class NassiShneidermanDiagram:
logging.basicConfig(level=logLevel)
def add_instruction(self, instruction: Iinstruction):
instruction_key = "instruction#" + str(len(self.instructions))
self.instructions[instruction_key] = instruction
logging.debug("added instruction %s : %s", instruction_key, instruction.instruction_text)
self.instructions.append(instruction)
def get_image_height(self) -> int:
h = 0
for inst in self.instructions:
h += inst.getblksize()
return int(h)
def convert_to_image(self, filename: str, x_size: int=200):
logging.info(f"Saving NSD to {filename}.png")
cti.NSD_init(x_size, 5000)
x, y, x_sz = 0, 0, x_size
for _k, instruction in self.instructions.items():
x, y = instruction.to_image(x, y, x_sz)
image_y_sz = self.get_image_height()
cti.NSD_init(x_size, image_y_sz)
x, y = 0, 0
for instruction in self.instructions:
x, y = instruction.to_image(x, y, x_size)
cti.NSD_save(filename)
def load_from_file(self, filepath:str):