updated image generation code [WIP]
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -8,6 +8,16 @@ import logging
|
||||
from interpreter.function_scope import Function_scope
|
||||
from interpreter._token import Token, Token_type
|
||||
from errors.custom import JavaSyntaxError
|
||||
|
||||
def print_arr(arr):
|
||||
print("[")
|
||||
for elem in arr:
|
||||
if isinstance(elem, List):
|
||||
print_arr(elem)
|
||||
else:
|
||||
print(elem)
|
||||
print("]")
|
||||
|
||||
class Lexer:
|
||||
def __init__(self, tokens: List[Token]) -> None:
|
||||
self._tokens = tokens
|
||||
@@ -149,9 +159,10 @@ class Lexer:
|
||||
def _construct_source_line_from_tokens(self, tokens: List[Token]) -> str:
|
||||
"""TODO: make this function smarter"""
|
||||
line = ""
|
||||
|
||||
for token in tokens:
|
||||
if token.type == Token_type.SEMICOLON:
|
||||
continue
|
||||
break
|
||||
line += token.content + ' '
|
||||
|
||||
return line[:-1] #ignore the space after the last instruction text
|
||||
@@ -209,7 +220,6 @@ class Lexer:
|
||||
return while_instruction_back(condtion_str, loop_instructions)
|
||||
|
||||
def _handle_for_construct(self, tokens: List[Token]):
|
||||
#TODO: change that
|
||||
logging.debug("Found for construct")
|
||||
tokens.extend(self.get_tokens_until([Token_type.LEFT_CURLY]))
|
||||
|
||||
@@ -390,4 +400,4 @@ def _get_for_arguments_from_tokens(tokens: List[Token]) -> Tuple[List[Token], Li
|
||||
break
|
||||
increment_tokens.append(token)
|
||||
|
||||
return variable_tokens, condition_tokens, increment_tokens[:-2]
|
||||
return variable_tokens, condition_tokens, increment_tokens[:-1]
|
||||
@@ -10,7 +10,8 @@ from enum import IntEnum
|
||||
import os.path
|
||||
import secrets
|
||||
|
||||
from interpreter.interpret_source import JavaInterpreter
|
||||
from interpreter.Tokenizer import Tokenizer
|
||||
from interpreter.Lexer import Lexer
|
||||
from interpreter.function_scope import Function_scope
|
||||
from draw.code_to_image_wrapper import NSD_writer
|
||||
import draw.code_to_image as cti
|
||||
@@ -38,12 +39,12 @@ class NassiShneidermanDiagram:
|
||||
|
||||
@staticmethod
|
||||
def _save_scope(scope: Function_scope, output_path: str):
|
||||
y_size = scope.get_height()
|
||||
x_size = scope.get_width()
|
||||
y_size = scope.get_height()
|
||||
with NSD_writer(output_path, x_size, y_size):
|
||||
x, y = 0, 0
|
||||
for instruction in scope:
|
||||
x, y = instruction.to_image(x, y, x_size)
|
||||
x, y = instruction.to_image(x, y, x_size)[0:2]
|
||||
|
||||
@staticmethod
|
||||
def check_conflicts(filepath:str, behavoiur: Overwrite_behaviour):
|
||||
@@ -64,6 +65,7 @@ class NassiShneidermanDiagram:
|
||||
|
||||
filepath = f"{output_path}/{scope.name}"
|
||||
filepath = self.check_conflicts(filepath, on_conflict)
|
||||
|
||||
if filepath is not None:
|
||||
logging.info(f"Saving NSD to {filepath}.png...")
|
||||
|
||||
@@ -81,9 +83,14 @@ class NassiShneidermanDiagram:
|
||||
i+=1
|
||||
|
||||
def load_from_file(self, filepath:str, itp_custom_tags: Optional[Dict[str, List[str]]]):
|
||||
itp = JavaInterpreter(filepath)
|
||||
itp.reset_tags(itp_custom_tags)
|
||||
self.function_scopes = itp.load_instruction_scopes()
|
||||
|
||||
tokenizer = Tokenizer(filepath)
|
||||
|
||||
tokens = tokenizer.get_tokens()
|
||||
|
||||
lexer = Lexer(tokens)
|
||||
|
||||
self.function_scopes = lexer.get_instructions()[:-1]
|
||||
|
||||
if not self.function_scopes:
|
||||
return True
|
||||
|
||||
@@ -19,7 +19,7 @@ class Tokenizer:
|
||||
self.line_number = 1
|
||||
self.column_number = 0
|
||||
|
||||
self.source_text = re.sub("(private)|(public)|(protected)", "", self.source_text)
|
||||
self.source_text = re.sub("(private)|(public)|(protected)|(final)", "", self.source_text)
|
||||
|
||||
self.type_name_pattern = re.compile('(char)|(int)|(void)|(double)|(boolean)|(Pixel)|(String)') #TODO: make this modular
|
||||
|
||||
|
||||
Reference in New Issue
Block a user