bugfix: wasn't working at all

This commit is contained in:
2025-06-11 17:12:03 -07:00
parent 2b328d805e
commit e1639ff8ba
3 changed files with 39 additions and 31 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
partdb-labeler.bat
partdb_labeler.egg-info
build
*/__pycache__/*

View File

@@ -52,25 +52,25 @@ def esc(s):
# render a line of text at coordinates
# return coordinates of next line
def textline(s, loc, fontnum):
def textline(z, res, s, loc, fontnum):
z.output(f"A{loc[0]},{loc[1]},0,{fontnum},1,1,N,\"{esc(filter(subst(s), "cp437"))}\"\n")
return (loc[0], loc[1]+font_metrics[fontnum][1])
return (loc[0], loc[1]+font_metrics(res)[fontnum][1])
# wrap text in a bounding box at coordinates
# return coordinates of next line and any unused text
def textbox(s, loc, bbox, fontnum):
wrapped=textwrap.wrap(filter(subst(s), "cp437"), width=floor(bbox[0]/font_metrics[fontnum][0]))
def textbox(z, res, s, loc, bbox, fontnum):
wrapped=textwrap.wrap(filter(subst(s), "cp437"), width=floor(bbox[0]/font_metrics(res)[fontnum][0]))
line=0
while line*font_metrics[fontnum][1]<bbox[1] and line<len(wrapped):
loc=textline(wrapped[line], loc, fontnum)
while line*font_metrics(res)[fontnum][1]<bbox[1] and line<len(wrapped):
loc=textline(z, res, wrapped[line], loc, fontnum)
line=line+1
return loc, " ".join(wrapped[line:])
# render a QR code at coordinates
# return size (single value, since QR codes are square)
def qr(s, loc, mul, brdr):
def qr(z, s, loc, mul, brdr):
qr = qrcode.QRCode(
box_size=mul,
border=brdr,
@@ -83,6 +83,26 @@ def qr(s, loc, mul, brdr):
z.output(f"GW10,10,{ceil(padded.width/8)},{padded.height},{padded.tobytes().decode("cp437")}\n")
return img.height
# width and height for built-in monospace fonts (includes whitespace)
def font_metrics(res):
m={}
if res==203:
m[1]=(10,14)
m[2]=(12,18)
m[3]=(14,22)
m[4]=(16,26)
m[5]=(34,50)
if res==300:
m[1]=(14,22)
m[2]=(18,30)
m[3]=(22,38)
m[4]=(26,46)
m[5]=(50,82)
return m
# entrypoint
def cli():
parser=argparse.ArgumentParser()
parser.add_argument("id", help="part ID", type=int)
@@ -121,22 +141,6 @@ def cli():
if res!=203 and res!=300:
raise ValueError("valid resolution options are 203 and 300")
# width and height for built-in monospace fonts (includes whitespace)
font_metrics={}
if res==203:
font_metrics[1]=(10,14)
font_metrics[2]=(12,18)
font_metrics[3]=(14,22)
font_metrics[4]=(16,26)
font_metrics[5]=(34,50)
if res==300:
font_metrics[1]=(14,22)
font_metrics[2]=(18,30)
font_metrics[3]=(22,38)
font_metrics[4]=(26,46)
font_metrics[5]=(50,82)
# look up the part
url=f"{base_url}/api/parts/{id}"
@@ -159,19 +163,22 @@ def cli():
z.output("N\n")
if res==300:
qr_size=qr(f"{base_url}/en/part/{id}", (10, 10), 6, 3)
qr_size=qr(z, f"{base_url}/en/part/{id}", (10, 10), 6, 3)
else:
qr_size=qr(f"{base_url}/en/part/{id}", (10, 10), 4, 2)
qr_size=qr(z, f"{base_url}/en/part/{id}", (10, 10), 4, 2)
loc=(15+qr_size, 20)
loc=textline(part["ipn"], loc, 5)
loc=textline(z, res, part["ipn"], loc, 5)
loc, excess=textbox(f"{part["manufacturer"]["name"]} {part["manufacturer_product_number"]}", loc, (label_width-loc[0]-10, label_height-loc[1]-10), 2)
loc, excess=textbox(z, res, f"{part["manufacturer"]["name"]} {part["manufacturer_product_number"]}", loc, (label_width-loc[0]-10, label_height-loc[1]-10), 2)
avail_y=floor((20+qr_size-loc[1])/font_metrics[2][1])*font_metrics[2][1]
loc, excess=textbox(part["description"], loc, (label_width-loc[0]-10, avail_y), 2)
avail_y=floor((20+qr_size-loc[1])/font_metrics(res)[2][1])*font_metrics(res)[2][1]
loc, excess=textbox(z, res, part["description"], loc, (label_width-loc[0]-10, avail_y), 2)
if excess!="":
loc=(10, loc[1])
loc, excess=textbox(excess, loc, (label_width-20, label_height-loc[1]), 2)
loc, excess=textbox(z, res, excess, loc, (label_width-20, label_height-loc[1]), 2)
z.output("P1\n")
if __name__ == "__main__":
cli()

View File

@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "partdb_labeler"
version = "0.2.3"
version = "0.2.4"
authors = [
{name = "Scott Alfter", email = "scott@alfter.us"},
]