Skip to content

Commit f120c01

Browse files
committed
Shorten the keys in text object
Signed-off-by: DL6ER <[email protected]>
1 parent 1f98aeb commit f120c01

5 files changed

Lines changed: 150 additions & 135 deletions

File tree

app/labeldesigner/label.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ def _draw_text(self, img = None, bboxes = [], text_offset = (0, 0)):
397397
# Iterate over lines of text
398398
for i, line in enumerate(self.text):
399399
# Calculate spacing
400-
spacing = int(int(line['font_size'])*((int(line['line_spacing']) - 100) / 100)) if 'line_spacing' in line else 0
400+
spacing = int(int(line['size'])*((int(line['line_spacing']) - 100) / 100)) if 'line_spacing' in line else 0
401401

402402
# Get font
403-
font = self._get_font(line['font_path'], line['font_size'])
403+
font = self._get_font(line['path'], line['size'])
404404

405405
# Determine anchors
406406
anchor = None
@@ -419,12 +419,12 @@ def _draw_text(self, img = None, bboxes = [], text_offset = (0, 0)):
419419
else:
420420
raise ValueError(f"Unsupported alignment: {align}")
421421

422-
red_font = 'font_color' in line and line['font_color'] == 'red'
422+
red_font = 'color' in line and line['color'] == 'red'
423423
# if red_font and not self._red_support:
424424
# raise ValueError("Red font is not supported on this label")
425425
color = (255, 0, 0) if red_font else (0, 0, 0)
426426

427-
if do_draw and 'font_inverted' in line and line['font_inverted']:
427+
if do_draw and 'inverted' in line and line['inverted']:
428428
# Draw a filled rectangle
429429
center_x = 0
430430
if anchor == "lt":
@@ -439,7 +439,7 @@ def _draw_text(self, img = None, bboxes = [], text_offset = (0, 0)):
439439
elif anchor == "rt":
440440
max_bbox_x = text_offset[0] + max(bbox[0][2] for bbox in bboxes)
441441
min_bbox_x = max_bbox_x - (bboxes[i][0][2] - bboxes[i][0][0])
442-
shift = 0.1 * int(line['font_size'])
442+
shift = 0.1 * int(line['size'])
443443
y_min = bboxes[i][0][1] + text_offset[1] - shift
444444
y_max = bboxes[i][0][3] + text_offset[1] - shift
445445
draw.rectangle((min_bbox_x, y_min, max_bbox_x, y_max), fill=color)
@@ -487,5 +487,5 @@ def _compute_bbox(self, bboxes):
487487
max_width = max(bbox[0][2] for bbox in bboxes)
488488
return (bboxes[0][0][0], bboxes[0][0][1], max_width, bboxes[-1][0][3])
489489

490-
def _get_font(self, font_path, font_size):
491-
return ImageFont.truetype(font_path, int(font_size))
490+
def _get_font(self, font_path, size):
491+
return ImageFont.truetype(font_path, int(size))

app/labeldesigner/routes.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,18 @@ def get_label_dimensions(label_size):
203203

204204
def get_font_path(line: dict):
205205
try:
206-
font_family_name = line.get('font_family')
207-
font_style_name = line.get('font_style')
208-
if font_family_name not in FONTS.fonts:
209-
raise LookupError("Unknown font family: %s" % font_family_name)
210-
if font_style_name not in FONTS.fonts[font_family_name]:
211-
font_style_name = current_app.config['LABEL_DEFAULT_FONT_STYLE']
212-
if font_style_name not in FONTS.fonts[font_family_name]:
206+
family_name = line.get('family')
207+
style_name = line.get('style')
208+
if family_name not in FONTS.fonts:
209+
raise LookupError("Unknown font family: %s" % family_name)
210+
if style_name not in FONTS.fonts[family_name]:
211+
style_name = current_app.config['LABEL_DEFAULT_FONT_STYLE']
212+
if style_name not in FONTS.fonts[family_name]:
213213
raise LookupError("Unknown font style: %s for font %s" %
214-
(font_style_name, font_family_name))
215-
font_path = FONTS.fonts[font_family_name][font_style_name]
214+
(style_name, family_name))
215+
return FONTS.fonts[family_name][style_name]
216216
except KeyError:
217-
raise LookupError("Couln't find the font & style")
218-
return font_path
217+
raise LookupError("Couldn't find the requested font + style")
219218

220219
def get_uploaded_image(image):
221220
name, ext = os.path.splitext(image.filename)
@@ -277,13 +276,15 @@ def get_uploaded_image(image):
277276

278277
# For each line in text, we determine and add the font path
279278
for line in context['text']:
280-
if 'font_family' not in line:
281-
line['font_family'] = current_app.config['LABEL_DEFAULT_FONT_FAMILY']
282-
if 'font_style' not in line:
283-
line['font_style'] = current_app.config['LABEL_DEFAULT_FONT_STYLE']
284-
if 'font_size' not in line:
279+
if 'family' not in line:
280+
line['family'] = current_app.config['LABEL_DEFAULT_FONT_FAMILY']
281+
if 'style' not in line:
282+
line['style'] = current_app.config['LABEL_DEFAULT_FONT_STYLE']
283+
if 'size' not in line or not line['size'].isdigit():
285284
raise ValueError("Font size is required")
286-
line['font_path'] = get_font_path(line)
285+
if int(line['size']) < 1:
286+
raise ValueError("Font size must be at least 1")
287+
line['path'] = get_font_path(line)
287288

288289
# Reject extraordinary long texts
289290
if len(line['text']) > 10_000:

app/labeldesigner/templates/labeldesigner.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@
2929
<div id="collapse1" class="collapse" aria-labelledby="heading1" data-parent="#accordion">
3030
<div class="card-body">
3131
<label for="labelSize" style="margin-bottom: 0">Label Size:</label>
32-
<select class="form-control" id="labelSize" onChange="preview()">
33-
{% for label_size in label_sizes %}<option value="{{label_size[0]}}" data-x="{{label_size[3][0]}}" data-y="{{label_size[3][1]}}" data-round="{{label_size[2]}}" {% if default_label_size == label_size[0] %}selected{% endif %}>{{label_size[1]}}</option>{% endfor %}
32+
<select class="form-control" id="labelSize" onChange="preview()" data-default="{{default_label_size}}">
33+
{% for label_size in label_sizes %}<option value="{{label_size[0]}}" data-x="{{label_size[3][0]}}"
34+
data-y="{{label_size[3][1]}}" data-round="{{label_size[2]}}" {% if default_label_size==label_size[0]
35+
%}selected{% endif %}>{{label_size[1]}}</option>{% endfor %}
3436
</select>
3537
<div class="form-text text-muted">Detected label: <span id="label-width">???</span> x <span id="label-height">???</span></div>
3638
<div id="labelMismatch" class="form-text text-danger" style="display: none;">Label size mismatch detected!</div>
3739

3840
<label for="orientation" class="control-label input-group" style="margin-top: 10px; margin-bottom: 0">Label Orientation:</label>
3941
<div class="btn-group btn-group-toggle btn-block" data-toggle="buttons">
4042
<label class="btn btn-secondary {% if default_orientation == 'standard' %}active{% endif %}" id="orientation_standard">
41-
<input type="radio" name="orientation" onchange="preview()" value="standard" aria-label="Standard" {% if default_orientation == 'standard' %}checked{% endif %}>
43+
<input type="radio" name="orientation" onchange="preview()" value="standard" aria-label="Standard" {% if
44+
default_orientation=='standard' %}checked data-default="1" {% endif %}>
4245
<span class="fas fa-ruler-horizontal" aria-hidden="true"> Standard
4346
</label>
4447
<label class="btn btn-secondary {% if default_orientation == 'rotated' %}active{% endif %}" id="orientation_rotated">
45-
<input type="radio" name="orientation" onchange="preview()" value="rotated" aria-label="Rotated" {% if default_orientation == 'rotated' %}checked{% endif %}>
48+
<input type="radio" name="orientation" onchange="preview()" value="rotated" aria-label="Rotated" {% if
49+
default_orientation=='rotated' %}checked data-default="1" {% endif %}>
4650
<span class="fas fa-ruler-vertical" aria-hidden="true"> Rotated
4751
</label>
4852
</div>
@@ -119,7 +123,7 @@
119123
Color:</label>
120124
<div class="btn-group btn-group-toggle btn-block" data-toggle="buttons">
121125
<label class="btn btn-dark" id="print_color_black">
122-
<input type="radio" name="fontColor" onchange="preview()" value="black" aria-label="Black">Black
126+
<input type="radio" name="fontColor" onchange="preview()" value="black" aria-label="Black" data-default="1">Black
123127
</label>
124128
<label class="btn btn-danger" id="print_color_red">
125129
<input type="radio" name="fontColor" onchange="preview()" value="red" aria-label="Red">Red
@@ -297,7 +301,7 @@
297301
<label for="printType" class="control-label input-group" style="margin-bottom: 0">Print Type:</label>
298302
<div class="btn-group btn-group-toggle btn-block" data-toggle="buttons">
299303
<label class="btn btn-secondary active" id="printTypeText">
300-
<input type="radio" name="printType" onchange="preview()" value="text" aria-label="Text" checked>
304+
<input type="radio" name="printType" onchange="preview()" value="text" aria-label="Text" data-default="1" checked>
301305
<span class="fas fa-font" aria-hidden="true"></span><br>Text
302306
</label>
303307
<label class="btn btn-secondary" id="printTypeQrCode">

app/static/js/main.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ function setFontSettingsPerLine() {
1818

1919
// Default font settings from the current UI controls
2020
var currentFont = {
21-
font_family: $('#fontFamily option:selected').text(),
22-
font_style: $('#fontStyle option:selected').text(),
23-
font_size: $('#fontSize').val(),
24-
font_inverted: $('#fontInverted').is(':checked'),
21+
family: $('#fontFamily option:selected').text(),
22+
style: $('#fontStyle option:selected').text(),
23+
size: $('#fontSize').val(),
24+
inverted: $('#fontInverted').is(':checked'),
2525
align: $('input[name=fontAlign]:checked').val(),
2626
line_spacing: $('input[name=lineSpacing]:checked').val(),
27-
font_color: $('#fontColor').val()
27+
color: $('input[name=fontColor]:checked').val()
2828
};
2929

3030
// Create lines in the <option> with id #lineSelect
@@ -92,29 +92,29 @@ $(document).ready(function () {
9292
if (isNaN(idx) || !fontSettingsPerLine || !fontSettingsPerLine[idx]) return;
9393
var fs = fontSettingsPerLine[idx];
9494
// Only set font family and get styles if font family is changed
95-
if (fs.font_family !== $('#fontFamily option:selected').text()) {
95+
if (fs.family !== $('#fontFamily option:selected').text()) {
9696
// Set font family
97-
$('#fontFamily').val(fs.font_family);
97+
$('#fontFamily').val(fs.family);
9898
// Set font style
99-
updateStyles(fs.font_style);
99+
updateStyles(fs.style);
100100
}
101101
else {
102102
// Only set font style
103-
$('#fontStyle').val(fs.font_style);
103+
$('#fontStyle').val(fs.style);
104104
}
105105
// Set font size
106-
$('#fontSize').val(fs.font_size);
106+
$('#fontSize').val(fs.size);
107107
// Set alignment
108108
$('input[name=fontAlign]').prop('checked', false).parent().removeClass('active');
109109
$('input[name=fontAlign][value="' + fs.align + '"]').prop('checked', true).parent().addClass('active');
110110
// Set line spacing
111111
$('input[name=lineSpacing]').prop('checked', false).parent().removeClass('active');
112112
$('input[name=lineSpacing][value="' + fs.line_spacing + '"]').prop('checked', true).parent().addClass('active');
113113
// Set font inversion
114-
$('#fontInverted').prop('checked', fs.font_inverted);
114+
$('#fontInverted').prop('checked', fs.inverted);
115115
// Set font color
116116
$('input[name=fontColor]').prop('checked', false).parent().removeClass('active');
117-
$('input[name=fontColor][value="' + fs.font_color + '"]').prop('checked', true).parent().addClass('active');
117+
$('input[name=fontColor][value="' + fs.color + '"]').prop('checked', true).parent().addClass('active');
118118
});
119119

120120
// When the user changes the caret/selection in the textarea, update #lineSelect and font controls
@@ -201,6 +201,7 @@ function updateStyles(style = null) {
201201
}
202202
});
203203
styleSelect.trigger("change");
204+
preview();
204205
}
205206
});
206207
}
@@ -503,7 +504,7 @@ function restoreAllSettingsFromLocalStorage() {
503504
if (this.type === 'checkbox' || this.type === 'radio') {
504505
$(this).prop('checked', !!data[key]);
505506
if (this.type === 'radio') {
506-
if ($(this).val() == data[key]) $(this).prop('checked', true);
507+
$(this).prop('checked', $(this).val() == data[key]);
507508
}
508509
} else {
509510
$(this).val(data[key]);
@@ -564,11 +565,15 @@ function resetSettings() {
564565
function set_all_inputs_default(force = false) {
565566
// Iterate over those <input> that have a data-default propery and set the value if empty
566567
$('input[data-default], select[data-default], textarea[data-default]').each(function() {
567-
if (!$(this).val() || force) {
568+
if (this.type === 'checkbox' || this.type === 'radio') {
569+
$(this).prop('checked', $(this).data('default') == 1 || $(this).data('default') === true);
570+
}
571+
else if (this.type === 'select-one' || this.type === 'number') {
568572
$(this).val($(this).data('default'));
573+
569574
}
570-
if (this.type === 'checkbox') {
571-
$(this).prop('checked', $(this).data('default') == 1 || $(this).data('default') === true);
575+
else if (!$(this).val() || force) {
576+
$(this).val($(this).data('default'));
572577
}
573578
});
574579
}
@@ -582,6 +587,7 @@ window.onload = function () {
582587

583588
// Restore settings on load
584589
restoreAllSettingsFromLocalStorage();
590+
585591
// Save on change
586592
$(document).on('change input', 'input, select, textarea', function() {
587593
saveAllSettingsToLocalStorage();

0 commit comments

Comments
 (0)