added variable font

This commit is contained in:
weckyy702
2020-12-27 16:12:22 +01:00
parent 688c4ad0ac
commit 2e70191b04
3 changed files with 23 additions and 14 deletions

View File

@@ -1,11 +1,13 @@
from typing import Iterable from typing import Iterable
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
import os
datei_endung = ".png" datei_endung = ".png"
img = None img = None
output_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): 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") img = Image.new("RGB", (x, y), "white")
output_img = ImageDraw.Draw(img) 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): def get_text_size(text: str):
if not font: if not font:
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")

View File

@@ -4,9 +4,10 @@ from interpreter.NassiShneidermann import NassiShneidermanDiagram
from draw.Iinstruction import * from draw.Iinstruction import *
def nassi(filepath:str, output_path: str, gui): def nassi(filepath:str, output_path: str, gui, font_filepath: str=None):
print('')
NSD = NassiShneidermanDiagram(gui.debug_mode) NSD = NassiShneidermanDiagram(gui.debug_mode)
if font_filepath:
NSD.set_font(font_filepath)
NSD.load_from_file(filepath) NSD.load_from_file(filepath)
NSD.convert_to_image(output_path + "/Nassi-Shneider-Diagramm", 500) NSD.convert_to_image(output_path + "/Nassi-Shneider-Diagramm", 500)

View File

@@ -4,6 +4,7 @@ import logging
from draw.Iinstruction import Iinstruction from draw.Iinstruction import Iinstruction
from interpreter import interpret_source as itp from interpreter import interpret_source as itp
from draw.code_to_image_wrapper import NSD_writer from draw.code_to_image_wrapper import NSD_writer
import draw.code_to_image as cti
class NassiShneidermanDiagram: class NassiShneidermanDiagram:
@@ -19,17 +20,17 @@ class NassiShneidermanDiagram:
logging.basicConfig(level=logLevel) logging.basicConfig(level=logLevel)
def add_instruction(self, instruction: Iinstruction): def set_font(self, font_filepath: str):
self.instructions.append(instruction) cti.set_font(font_filepath)
def get_image_height(self) -> int: def _get_image_height(self) -> int:
h = 0 h = 0
for inst in self.instructions: for inst in self.instructions:
h += inst.getblksize() h += inst.getblksize()
return int(h) return int(h)
def convert_to_image(self, filename: str, x_size: int=200): 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...") logging.info(f"Saving NSD to {filename}.png...")
with NSD_writer(filename, x_size, image_y_sz): with NSD_writer(filename, x_size, image_y_sz):
x, y = 0, 0 x, y = 0, 0
@@ -39,12 +40,7 @@ class NassiShneidermanDiagram:
logging.info("Done!") logging.info("Done!")
def load_from_file(self, filepath:str): def load_from_file(self, filepath:str):
instructions = itp.load_instructions(filepath) self.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)