fixed image generation

This commit is contained in:
weckyy702
2021-04-25 14:22:03 +02:00
parent 45cb5c0467
commit 968bb53883
2 changed files with 61 additions and 56 deletions

View File

@@ -2,13 +2,16 @@
__author__ = "plexx, Weckyy702"
from typing import Iterable
from typing import Tuple
from PIL import Image, ImageDraw, ImageFont
import os
import random
PADDING_X = 100
PADDING_X = 50
PADDING_Y = 5
BLOCK_OFFSET_RATIO = .1
datei_endung = ".png"
img = None
@@ -41,99 +44,101 @@ def set_font(font_filepath: str):
raise
def get_text_size(text: str):
def get_text_size(text: str) -> Tuple[int, int]:
if not font:
raise Exception(ERROR_TEXT)
return font.getsize(text)
size = font.getsize(text)
def draw_debug_tile(x, y, x_sz, y_sz):
from random import randint
return (size[0]+PADDING_X, size[1]+PADDING_Y)
output_img.rectangle((x, y) + (x + x_sz, y + y_sz), fill=(randint(0, 255)))
return x, y + y_sz, x_sz
def draw_generic_instruction(instruction: str, x, y, xsize, ysize) -> Iterable[float]:
def draw_debug_tile(x:int, y:int, width:int, height:int) -> int:
fill_color = (random.randint(0, 255),random.randint(0, 255), random.randint(0, 255))
output_img.rectangle((x,y) + (x+width, y+height), outline=(0), fill=fill_color)
return y + height
def draw_generic_instruction(instruction: str, x: int, y: int, width:int, height:int) -> int:
if not output_img:
raise Exception(ERROR_TEXT)
#draw shit
output_img.rectangle((x,y) + (x + xsize, y + ysize), outline=(0), width=1)
output_img.rectangle((x,y) + (x + width, y + height), outline=(0), width=1)
#text shit
output_img.multiline_text((x + xsize * .5, y + ysize * .5), instruction, font=font, anchor="mm", align="right", fill=(0))
output_img.multiline_text((x + width * .5, y + height * .5), instruction, font=font, anchor="mm", align="right", fill=(0))
return x, y + ysize, xsize
return y + height
def draw_if_statement(condition: str, x: int, y: int, true_w: int, false_w: int, ysize: int):
def draw_if_statement(condition: str, x: int, y: int, true_width: int, block_width:int) -> Tuple[int, int, int, int]:
"""Draw an if statement into the NSD"""
if not output_img:
raise Exception(ERROR_TEXT)
text_sz = font.getsize(condition)
text_w = text_sz[0] + PADDING_X
text_h = text_sz[1] + PADDING_Y
text_height = get_text_size(condition)[1]
box_w = max(text_w, true_w + false_w)
#condition
output_img.rectangle((x, y) + (x + block_width, y + text_height), outline=(0), width=1) #Box around condition text
output_img.multiline_text((x + true_width, y + text_height / 2), condition, fill=(0), font=font, anchor="mm", spacing=4, align='right')
output_img.line((x,y) + (x + box_w/2, y + text_h), fill=(0))
output_img.line((x + box_w, y) + (x + box_w/2, y + text_h), fill=(0))
output_img.rectangle((x, y + text_h) + (x + box_w, y + ysize), outline=(0), width=1)
output_img.rectangle((x, y) + (x + box_w, y + text_h), outline=(0), width=1)
output_img.line((x + true_w, y + text_h) + (x + true_w, y + ysize), fill=(0))
#fancy lines for the "true" and "false" labels
output_img.line((x,y) + (x + true_width, y + text_height), fill=(0))
output_img.line((x + block_width, y) + (x + true_width, y + text_height), fill=(0))
# "true" and "false" labels
output_img.text((x + 5, y + text_height), "true", font = font, fill = (0), anchor="ld")
output_img.text((x + block_width - 5, y + text_height), "false", font = font, fill = (0), anchor="rd")
# condition text
output_img.multiline_text((x + box_w / 2, y + text_h / 2), condition, fill=(0), font=font, anchor="mm", spacing=4, align='right')
#x and y of "true" and "false" label
return x, y + text_height, x + true_width, y + text_height
# true / false
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, 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):
def draw_while_loop_front(condition: str, x: int, y: int, block_width: int, block_height: int) -> Tuple[int, int, int]:
if not output_img:
raise Exception(ERROR_TEXT)
text_y_sz = font.getsize(condition)[1]
text_height = get_text_size(condition)[1]
block_offset = int(block_width * BLOCK_OFFSET_RATIO)
#the box
output_img.line((x,y) + (x + xsize, y), fill=(0))
output_img.line((x,y) + (x, y + ysize), 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 + text_y_sz), 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 + text_y_sz), fill=(0))
output_img.line((x,y) + (x + block_width, y), fill=(0))
output_img.line((x,y) + (x, y + block_width), fill=(0))
output_img.line((x + block_offset, y + text_height) + (x + block_width, y + text_height), fill=(0))
output_img.line((x + block_width, y) + (x + block_width, y + text_height), fill=(0))
output_img.line((x, y + block_height) + (x + block_offset, y + block_height ), fill=(0))
output_img.line((x + block_offset, y + block_height) + (x + block_offset, y + text_height), fill=(0))
#the text
output_img.text((x + xsize * .1, y + text_y_sz * .5), condition, font = font, fill = (0), anchor="lm")
output_img.text((x + block_offset, y + text_height * .5), condition, font = font, fill = (0), anchor="lm")
#the x, y offset then the children width
return x + xsize * .1, y + text_y_sz, xsize * .9
return x + block_offset, y + text_height, block_width - block_offset
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, block_width: int, block_height: int):
if not output_img:
raise Exception(ERROR_TEXT)
text_y_sz = get_text_size(condition)[1]
text_height = get_text_size(condition)[1]
#the box
output_img.line((x,y) + (x + xsize * .1, y), fill=0)
output_img.line((x + xsize * .1, y) + (x + xsize * .1, y + ysize - text_y_sz), fill=0)
output_img.line((x + xsize * .1, y + ysize - text_y_sz) + (x + xsize, y + ysize - text_y_sz), fill=0)
output_img.line((x + xsize, y + ysize - text_y_sz) + (x + xsize, y + ysize), fill=0)
output_img.line((x,y + ysize) + (x + xsize, y + ysize), fill=0)
output_img.line((x,y) + (x, y + ysize), fill=0)
output_img.line((x,y) + (x + block_width * .1, y), fill=0)
output_img.line((x + block_width * .1, y) + (x + block_width * .1, y + block_height - text_height), fill=0)
output_img.line((x + block_width * .1, y + block_height - text_height) + (x + block_width, y + block_height - text_height), fill=0)
output_img.line((x + block_width, y + block_height - text_height) + (x + block_width, y + block_height), fill=0)
output_img.line((x,y + block_height) + (x + block_width, y + block_height), fill=0)
output_img.line((x,y) + (x, y + block_height), fill=0)
#the text
output_img.text((x + xsize * .1, y + ysize - text_y_sz * .5), condition, font = font, fill = (0), anchor="lm")
output_img.multiline_text((x + block_width * .1, y + block_height - text_height * .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, xsize * .9
return x + block_width * .1, y, block_width * .9
def NSD_save(filepath: str):
"""Save the created file"""
filepath = filepath.removesuffix(".png")
filepath = filepath.removesuffix(datei_endung)
img.save(filepath + datei_endung ,"PNG")

View File

@@ -211,7 +211,7 @@ class JavaInterpreter:
return generic_instruction(f"declare variable '{var_name}' of type {var_type}"), idx
return generic_instruction(f"declare variable '{var_name}' of type {var_type} with value {var_value}"), idx
def _handle_instruction(self, line: str, idx:int) -> Tuple[Union[Iinstruction, List[Iinstruction]], int]:
def _handle_instruction(self, line: str, idx:int) -> Tuple[Union[instruction, List[instruction]], int]:
if line.startswith("while("):
logging.debug("Found while construct in line: %i", idx+1)
return self._handle_while(line, idx)
@@ -236,8 +236,8 @@ class JavaInterpreter:
logging.debug("Found generic instruction in line %i", idx+1)
return generic_instruction(line), idx
def _get_instructions_in_scope(self, idx: int=0) -> Tuple[List[Iinstruction], int]:
scope: List[Iinstruction] = []
def _get_instructions_in_scope(self, idx: int=0) -> Tuple[List[instruction], int]:
scope: List[instruction] = []
i = idx
while i < len(self._lines):
line = self._lines[i]
@@ -267,7 +267,7 @@ class JavaInterpreter:
fname = groups["name"]
return ftype, fname, fargs
def _get_function_instructions(self, function_header: str) -> List[Iinstruction]:
def _get_function_instructions(self, function_header: str) -> List[instruction]:
idx = self._lines.index(function_header)
brace_offset = self._get_scope_start_offset(idx)
return self._get_instructions_in_scope(idx+brace_offset)[0]