Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions labelprinterkit/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
provided for you.
"""
from typing import Tuple
import logging

from PIL import Image


logger = logging.getLogger(__name__)


def _coord_add(tup1, tup2):
"""add two tuples of size two"""
return (tup1[0] + tup2[0], tup1[1] + tup2[1])
Expand Down Expand Up @@ -73,12 +77,45 @@ def render(self, width=None, height=None) -> Image:
pos[1] += max(i.size[1] for i in line)

xdim, ydim = img.size
print("presize", xdim, ydim, height)
logger.debug("presize %d %d %d", xdim, ydim, height)
xdim = round((height / ydim) * xdim)

print("calcsize", xdim, ydim)
logger.debug("calcsize %d %d", xdim, ydim)
img = img.resize((xdim, height))

return img

# print("".join(f"{x:08b}".replace("0", " ") for x in bytes(i)))

class CenteredLabel(Label):
def render(self, width=None, height=None) -> Image:
"""render the Label.

Args:
width: Width request
height: Height request
"""
size = self.size
img = Image.new("1", size, "white")

pos = [0, 0]

for line in self._rendered_items:
line_width = sum(item.size[0] for item in line)
# to center, offset by half of different between line width and total width
pos[0] = (size[0] - line_width) // 2
for item in line:
box = (*pos, *_coord_add(item.size, pos))
img.paste(item, box=box)
pos[0] += item.size[0]

pos[1] += max(i.size[1] for i in line)

xdim, ydim = img.size
logger.debug("presize %d %d %d", xdim, ydim, height)
xdim = round((height / ydim) * xdim)

logger.debug("calcsize %d %d", xdim, ydim)
img = img.resize((xdim, height))

return img