New Notes template
This commit is contained in:
64
templates.py
64
templates.py
@@ -227,10 +227,74 @@ def new_label_large_template(variables, width_pixels=991, height_pixels=413, fil
|
|||||||
|
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
def notes_1_template(variables, width_pixels=991, height_pixels=413, filename=None):
|
||||||
|
"""Notes template with animal/date header and wrapped notes"""
|
||||||
|
image = Image.new('RGB', (width_pixels, height_pixels), 'white')
|
||||||
|
draw = ImageDraw.Draw(image)
|
||||||
|
|
||||||
|
try:
|
||||||
|
font_header = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 36)
|
||||||
|
font_notes = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 32)
|
||||||
|
font_small = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 28)
|
||||||
|
except:
|
||||||
|
font_header = ImageFont.load_default()
|
||||||
|
font_notes = ImageFont.load_default()
|
||||||
|
font_small = ImageFont.load_default()
|
||||||
|
|
||||||
|
margin = 10
|
||||||
|
top = margin
|
||||||
|
left_col = margin
|
||||||
|
right_margin = margin
|
||||||
|
line_height = 46
|
||||||
|
|
||||||
|
animal = variables.get('animal', '')
|
||||||
|
note_date = variables.get('date', '')
|
||||||
|
notes = variables.get('notes', '')
|
||||||
|
|
||||||
|
# Animal (left) and date (right) on the same line
|
||||||
|
draw.text((left_col, top), animal, fill='black', font=font_header)
|
||||||
|
bbox = draw.textbbox((0, 0), note_date, font=font_small)
|
||||||
|
date_width = bbox[2] - bbox[0]
|
||||||
|
draw.text((width_pixels - right_margin - date_width, top + 4), note_date, fill='black', font=font_small)
|
||||||
|
|
||||||
|
# Wrapped notes below the header
|
||||||
|
notes_top = top + line_height + 10
|
||||||
|
max_width = width_pixels - (left_col + right_margin)
|
||||||
|
|
||||||
|
def wrap_text(text, font, max_px):
|
||||||
|
lines = []
|
||||||
|
for raw_line in text.splitlines():
|
||||||
|
words = raw_line.split()
|
||||||
|
if not words:
|
||||||
|
lines.append('')
|
||||||
|
continue
|
||||||
|
line = []
|
||||||
|
for word in words:
|
||||||
|
test_line = ' '.join(line + [word])
|
||||||
|
bbox = draw.textbbox((0, 0), test_line, font=font)
|
||||||
|
test_width = bbox[2] - bbox[0]
|
||||||
|
if test_width <= max_px or not line:
|
||||||
|
line.append(word)
|
||||||
|
else:
|
||||||
|
lines.append(' '.join(line))
|
||||||
|
line = [word]
|
||||||
|
if line:
|
||||||
|
lines.append(' '.join(line))
|
||||||
|
return lines
|
||||||
|
|
||||||
|
for i, line in enumerate(wrap_text(notes, font_notes, max_width)):
|
||||||
|
draw.text((left_col, notes_top + i * (line_height - 6)), line, fill='black', font=font_notes)
|
||||||
|
|
||||||
|
if filename:
|
||||||
|
image.save(filename)
|
||||||
|
|
||||||
|
return image
|
||||||
|
|
||||||
# Template registry
|
# Template registry
|
||||||
TEMPLATES = {
|
TEMPLATES = {
|
||||||
'vet_label': vet_label_template,
|
'vet_label': vet_label_template,
|
||||||
'new_label': new_label_template,
|
'new_label': new_label_template,
|
||||||
'new_label_large': new_label_large_template,
|
'new_label_large': new_label_large_template,
|
||||||
|
'notes_1': notes_1_template,
|
||||||
# Add more templates here, e.g., 'vet_label_small': vet_label_small_template
|
# Add more templates here, e.g., 'vet_label_small': vet_label_small_template
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user