diff --git a/app.py b/app.py index a691a3f..367f0f7 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ from print import * from process_image import * from file_handler import * from colorama import Fore, Style +from fonts import fonts import os text_image_filename = "text_image.png" @@ -62,7 +63,7 @@ templates = { def render_text_template(info=None, info_color=None, scrollDown=None): - return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"], info=info, info_color=info_color, scrollDown=scrollDown) + return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"], info=info, info_color=info_color, scrollDown=scrollDown, fonts=session["fonts"]) @@ -75,7 +76,6 @@ def base(): check_for_new_user(session) return render_template('base.html') - # gets triggered when the user_data folder for the user is created @new_user_handler def on_new_user(session): @@ -87,6 +87,7 @@ def on_new_user(session): session["text1"] = templates["DNH"]["text1"] session["text2"] = templates["DNH"]["text2"] session["cut"] = templates["DNH"]["cut"] + session["fonts"] = fonts() @@ -183,7 +184,30 @@ def text_form(): "pos": int(request.form["pos2"]), } - message, status, img = gen_image(label_width, session["text1"], session["text2"]) + # Clear previously saved font + for font in session["fonts"]: + session["fonts"][font]["selected"] = False + + # If an invalid font has been submitted, just default to the first one + chosen_font = request.form["font"] + if chosen_font in session["fonts"]: + session["fonts"][chosen_font]["selected"] = True + else: + first_font = next(iter(session["fonts"])) + session["fonts"][first_font]["selected"] = True + + # Select the first font that has been marked as selected + chosen_font = None + for font in session["fonts"]: + f = session["fonts"][font] + if f["selected"]: + chosen_font = f + + # If the font is still None, something has gone wrong + if chosen_font is None: + return + + message, status, img = gen_image(label_width, session["text1"], session["text2"], chosen_font) if status == "Error": session["text image path"] = None diff --git a/fonts.py b/fonts.py new file mode 100644 index 0000000..19f4f2b --- /dev/null +++ b/fonts.py @@ -0,0 +1,20 @@ +class Font: + def __init__(self, name: str, path: str, stroke_width: int, default: bool = False) -> None: + self.name = name + self.path = path + self.stroke_width = stroke_width + self.selected = default + + def to_dict(self): + return { + "name": self.name, + "path": self.path, + "selected": self.selected, + "stroke_width": self.stroke_width + } + +def fonts(): + return { + "CYBER": Font("CYBER", "resources/OCRAEXT.TTF", 3, True).to_dict(), + "ComicMono": Font("Comic Sans Mono", "resources/ComicMono.ttf", 0).to_dict() + } diff --git a/gen_image.py b/gen_image.py index ec2d3a1..ba060e2 100644 --- a/gen_image.py +++ b/gen_image.py @@ -3,11 +3,11 @@ from PIL import Image, ImageDraw, ImageFont from colorama import Fore, Style from config import * -font = "resources/ComicMono.ttf" +#font = "resources/ComicMono.ttf" -def gen_image(height, text1, text2): - font1 = ImageFont.truetype(font, size=text1["size"]) - font2 = ImageFont.truetype(font, size=text2["size"]) +def gen_image(height, text1, text2, font): + font1 = ImageFont.truetype(font["path"], size=text1["size"]) + font2 = ImageFont.truetype(font["path"], size=text2["size"]) text1["string"] = text1["string"].replace("\r\n", "\n") text2["string"] = text2["string"].replace("\r\n", "\n") @@ -33,7 +33,7 @@ def gen_image(height, text1, text2): draw = ImageDraw.Draw(img) - draw.text((0, text1["pos"]), text1["string"], font=font1) + draw.text((0, text1["pos"]), text1["string"], font=font1, stroke_width=font["stroke_width"], stroke_fill="black") draw.text((0, text2["pos"]), text2["string"], font=font2) diff --git a/print.py b/print.py index 6f94cea..3da0c10 100644 --- a/print.py +++ b/print.py @@ -6,7 +6,8 @@ import ctypes import io import time -printer = ctypes.CDLL('./epson/library_bridge.so') +#printer = ctypes.CDLL('./epson/library_bridge.so') +printer = None def print_text(text): time.sleep(0.1) diff --git a/resources/LICENSE b/resources/LICENSE-ComicMono similarity index 100% rename from resources/LICENSE rename to resources/LICENSE-ComicMono diff --git a/resources/OCRAEXT.TTF b/resources/OCRAEXT.TTF new file mode 100644 index 0000000..0e77e20 Binary files /dev/null and b/resources/OCRAEXT.TTF differ diff --git a/templates/text.html b/templates/text.html index 02a0f7b..364fdd9 100644 --- a/templates/text.html +++ b/templates/text.html @@ -15,7 +15,7 @@
-

String 1

+

Top text

Text:

Size:
-

String 2

+

Bottom text

Text:

Size:
Y position:
+ style="width: 40px"> + +

Font

+ + + +