From b201c6d8c7e9f336ac0a8730b714e5816977f545 Mon Sep 17 00:00:00 2001 From: Christoffer Viken Date: Sat, 25 Mar 2017 15:59:47 +0000 Subject: [PATCH] Aaand font paths --- printer_helpers.py | 51 +++++++++++++++++++++++++++++++++++ text_interface/printermenu.py | 14 +++++++--- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/printer_helpers.py b/printer_helpers.py index 764960d..2c44edf 100644 --- a/printer_helpers.py +++ b/printer_helpers.py @@ -1,13 +1,64 @@ import os import barcode +import datetime +from PIL import Image +from PIL import ImageDraw +from PIL import ImageFont from brother_ql import BrotherQLRaster from brother_ql import create_label from brother_ql.backends import backend_factory +from brother_ql.devicedependent import label_type_specs from barcode_helpers import BrotherLabelWriter +def print_name_label(text, margin=10, rotate=False, label_type="62", printer_type="QL-700",): + if not rotate: + width, height = label_type_specs[label_type]['dots_printable'] + else: + height, width = label_type_specs[label_type]['dots_printable'] + + font_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Stranger back in the Night.ttf") + fs = 2000 + tw, th = width, height + if width == 0: + while th + 2*margin > height: + font = ImageFont.truetype(font_path, fs) + tw, th = font.getsize(text) + fs -= 1 + width = tw+2*margin + elif height == 0: + while tw + 2*margin > width: + font = ImageFont.truetype(font_path, fs) + tw, th = font.getsize(text) + fs -= 1 + height = th+2*margin + else: + while tw + 2*margin > width or th + 2*margin > height: + print(fs) + font = ImageFont.truetype(font_path, fs) + tw, th = font.getsize(text) + fs -= 1 + + xp = (width//2)-(tw//2) + yp = (height//2)-(th//2) + + im = Image.new("RGB", (width, height), (255, 255, 255)) + dr = ImageDraw.Draw(im) + + dr.text((xp, yp), text, fill=(0, 0, 0), font=font) + now = datetime.datetime.now() + date = now.strftime("%Y-%m-%d") + dr.text((0, 0), date, fill=(0, 0, 0)) + + base_path = os.path.dirname(os.path.realpath(__file__)) + fn = os.path.join(base_path, "bar_codes", text + ".png") + + im.save(fn, "PNG") + print_image(fn, printer_type, label_type) + + def print_bar_code(barcode_value, barcode_text, barcode_type="ean13", rotate=False, printer_type="QL-700", label_type="62"): bar_coder = barcode.get_barcode_class(barcode_type) diff --git a/text_interface/printermenu.py b/text_interface/printermenu.py index 1e1b32d..5ce0b75 100644 --- a/text_interface/printermenu.py +++ b/text_interface/printermenu.py @@ -1,4 +1,5 @@ -from printer_helpers import print_bar_code +from db import Product, User +from printer_helpers import print_bar_code, print_name_label from text_interface.helpermenus import Menu import conf @@ -14,7 +15,12 @@ Put it up somewhere in the vicinity. def _execute(self): self.print_header() - product = self.input_product('Prodct> ') - print_bar_code(product.bar_code, product.name, barcode_type="ean13", rotate=conf.label_rotate, - printer_type="QL-700", label_type=conf.label_type) + thing = self.input_thing('Prodct/User> ') + + if isinstance(thing, Product): + print_bar_code(thing.bar_code, thing.name, barcode_type="ean13", rotate=conf.label_rotate, + printer_type="QL-700", label_type=conf.label_type) + elif isinstance(thing, User): + print_name_label(text=thing.name, label_type=conf.label_type, rotate=conf.label_rotate, + printer_type="QL-700")