From 2e70191b0495a3ff65f6e0366c618b8497b4626a Mon Sep 17 00:00:00 2001 From: weckyy702 Date: Sun, 27 Dec 2020 16:12:22 +0100 Subject: [PATCH] added variable font --- draw/code_to_image.py | 14 +++++++++++++- gui/utils.py | 5 +++-- interpreter/NassiShneidermann.py | 18 +++++++----------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/draw/code_to_image.py b/draw/code_to_image.py index 0f1c95c..6ccd2b2 100644 --- a/draw/code_to_image.py +++ b/draw/code_to_image.py @@ -1,11 +1,13 @@ from typing import Iterable from PIL import Image, ImageDraw, ImageFont +import os datei_endung = ".png" img = None output_img = None -font = ImageFont.truetype("res/fonts/NotoSans-Regular.ttf", 12) +_bkp_font = ImageFont.truetype("res/fonts/NotoSans-Regular.ttf", 12) #in case set_font does funky stuff, backup the original font +font = _bkp_font def NSD_init(x: float, y: float): @@ -15,6 +17,16 @@ def NSD_init(x: float, y: float): img = Image.new("RGB", (x, y), "white") output_img = ImageDraw.Draw(img) +def set_font(font_filepath: str): + if not os.path.exists(font_filepath): + raise FileNotFoundError("") + try: + font = ImageFont.truetype(font_filepath) + except: + font = _bkp_font + raise + + def get_text_size(text: str): if not font: raise Exception("Output image was not initialized! Make sure to call NSD_init first") diff --git a/gui/utils.py b/gui/utils.py index e86f913..9ba6198 100644 --- a/gui/utils.py +++ b/gui/utils.py @@ -4,9 +4,10 @@ from interpreter.NassiShneidermann import NassiShneidermanDiagram from draw.Iinstruction import * -def nassi(filepath:str, output_path: str, gui): - print('') +def nassi(filepath:str, output_path: str, gui, font_filepath: str=None): NSD = NassiShneidermanDiagram(gui.debug_mode) + if font_filepath: + NSD.set_font(font_filepath) NSD.load_from_file(filepath) NSD.convert_to_image(output_path + "/Nassi-Shneider-Diagramm", 500) diff --git a/interpreter/NassiShneidermann.py b/interpreter/NassiShneidermann.py index 5c2cecf..48b9cb6 100644 --- a/interpreter/NassiShneidermann.py +++ b/interpreter/NassiShneidermann.py @@ -4,6 +4,7 @@ import logging from draw.Iinstruction import Iinstruction from interpreter import interpret_source as itp from draw.code_to_image_wrapper import NSD_writer +import draw.code_to_image as cti class NassiShneidermanDiagram: @@ -18,18 +19,18 @@ class NassiShneidermanDiagram: logLevel = logging.DEBUG logging.basicConfig(level=logLevel) - - def add_instruction(self, instruction: Iinstruction): - self.instructions.append(instruction) - def get_image_height(self) -> int: + def set_font(self, font_filepath: str): + cti.set_font(font_filepath) + + 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): - image_y_sz = self.get_image_height() + image_y_sz = self._get_image_height() logging.info(f"Saving NSD to {filename}.png...") with NSD_writer(filename, x_size, image_y_sz): x, y = 0, 0 @@ -39,12 +40,7 @@ class NassiShneidermanDiagram: logging.info("Done!") def load_from_file(self, filepath:str): - instructions = itp.load_instructions(filepath) - self.add_instructions_from_scope(instructions) - - def add_instructions_from_scope(self, scope: List[Iinstruction]): - for inst in scope: - self.add_instruction(inst) + self.instructions = itp.load_instructions(filepath)