added variable font
This commit is contained in:
@@ -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")
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
@@ -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:
|
||||||
@@ -18,18 +19,18 @@ class NassiShneidermanDiagram:
|
|||||||
logLevel = logging.DEBUG
|
logLevel = logging.DEBUG
|
||||||
|
|
||||||
logging.basicConfig(level=logLevel)
|
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
|
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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user