initial support for different label types

This commit is contained in:
Philipp Klaus 2016-12-17 21:11:58 +01:00
parent c9307a9463
commit c8b3c501f0
2 changed files with 20 additions and 3 deletions

View File

@ -30,6 +30,14 @@ DEFAULT_FONTS = [
{'family': 'DejaVu Serif', 'style': 'Book'}, {'family': 'DejaVu Serif', 'style': 'Book'},
] ]
LABEL_SIZES = [
('62', '62mm endless'),
('29x90', '29mm x 90mm die-cut'),
('62x29', '62mm x 29mm die-cut'),
('17x54', '17mm x 54mm die-cut'),
('17x87', '17mm x 87mm die-cut'),
]
@route('/') @route('/')
def index(): def index():
redirect('/labeldesigner') redirect('/labeldesigner')
@ -42,7 +50,8 @@ def serve_static(filename):
@view('labeldesigner.jinja2') @view('labeldesigner.jinja2')
def labeldesigner(): def labeldesigner():
fonts = sorted(list(FONTS.keys())) fonts = sorted(list(FONTS.keys()))
return {'title': 'Labeldesigner', 'message': '', 'fonts': fonts} label_sizes = LABEL_SIZES
return {'title': 'Labeldesigner', 'message': '', 'fonts': fonts, 'label_sizes': label_sizes}
def get_label_context(request): def get_label_context(request):
""" might raise LookupError() """ """ might raise LookupError() """
@ -89,6 +98,10 @@ def get_label_context(request):
def create_label_im(text, **kwargs): def create_label_im(text, **kwargs):
im_font = ImageFont.truetype(kwargs['font_path'], kwargs['font_size']) im_font = ImageFont.truetype(kwargs['font_path'], kwargs['font_size'])
textsize = im_font.getsize(text) textsize = im_font.getsize(text)
if 'x' in kwargs['label_size']:
# die-cut labels
height = kwargs['height']
else:
height = max(textsize[1] * (text.count('\n')+1), kwargs['height']) height = max(textsize[1] * (text.count('\n')+1), kwargs['height'])
im = Image.new('L', (kwargs['width'], height), 'white') im = Image.new('L', (kwargs['width'], height), 'white')
draw = ImageDraw.Draw(im) draw = ImageDraw.Draw(im)

View File

@ -6,6 +6,9 @@
<div class="row"> <div class="row">
<div class="col-md-4"> <div class="col-md-4">
<fieldset class="form-group"> <fieldset class="form-group">
<select class="form-control" id="labelSize" onChange="preview()">
{% for label_size in label_sizes %}<option value="{{label_size[0]}}">{{label_size[1]}}</option>{% endfor %}
</select>
<label for="labelText">Label Text:</label> <label for="labelText">Label Text:</label>
<textarea rows="4" id="labelText" class="form-control" onChange="preview()" onInput="preview()"></textarea> <textarea rows="4" id="labelText" class="form-control" onChange="preview()" onInput="preview()"></textarea>
<label for="fontFamily">Font Selection:</label> <label for="fontFamily">Font Selection:</label>
@ -58,8 +61,9 @@ function last_url_part() {
text = $('#labelText').val().replace(/\n/g, "%0A"); text = $('#labelText').val().replace(/\n/g, "%0A");
font_family = $('#fontFamily option:selected').text(); font_family = $('#fontFamily option:selected').text();
font_size = $('#fontSize').val(); font_size = $('#fontSize').val();
label_size = $('#labelSize option:selected').val();
if (text == '') text = '%20'; if (text == '') text = '%20';
return text + '?font_family=' + font_family + '&font_size=' + font_size; return text + '?font_family=' + font_family + '&font_size=' + font_size + '&label_size=' + label_size;
} }
function preview() { function preview() {