label_renderer.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. """PDF spool label rendering.
  2. Six fixed templates:
  3. - ``ams_holder_74x33`` — 74×33 mm single label, matches the printable label
  4. STL bundled with the Makerworld AMS Filament Label Holder (model 752566).
  5. Smaller variant — the visible window in the holder. One label per page.
  6. - ``ams_holder_75x55`` — 75×55 mm single label, fits the cardstock-insert
  7. variant of the same holder. Roomier — swatch + QR + full text column.
  8. - ``box_40x30`` — 40×30 mm single label, common DK/Brother roll size and a
  9. good fit for filament-bag/storage-bin labels (#809 follow-up). Roomy
  10. layout — swatch, QR, full text column with hex code.
  11. - ``box_62x29`` — 62×29 mm single label, sized for Brother PT/QL and Dymo
  12. generic small labels. One label per page.
  13. - ``avery_5160`` — US Letter sheet, 25.4×66.7 mm × 30 per sheet.
  14. - ``avery_l7160`` — A4 sheet, 38.1×63.5 mm × 21 per sheet.
  15. The legacy ``ams_30x15`` preset (#809) was incorrect — the original 30×15 mm
  16. dimension didn't fit any documented variant of model 752566. Replaced by the
  17. two ``ams_holder_*`` presets above (#1426).
  18. The renderer is decoupled from the Spool model: callers build a ``LabelData``
  19. list from whatever source (local DB, Spoolman, future) so the same code path
  20. works in both modes.
  21. Layout principle, taken from the issue's user need (`#809`): the **spool ID**
  22. is the most-recognisable field at arm's length and dominates the layout. Other
  23. fields (brand, material, name, storage location) fill remaining space; the QR
  24. code provides the round-trip back to ``/inventory?spool=<id>``.
  25. """
  26. from __future__ import annotations
  27. import io
  28. from dataclasses import dataclass
  29. from typing import Literal
  30. import qrcode
  31. from reportlab.lib.colors import Color, HexColor, black, white
  32. from reportlab.lib.pagesizes import A4, letter
  33. from reportlab.lib.units import mm
  34. from reportlab.pdfgen import canvas as rl_canvas
  35. TemplateName = Literal[
  36. "ams_holder_74x33",
  37. "ams_holder_75x55",
  38. "box_40x30",
  39. "box_62x29",
  40. "avery_5160",
  41. "avery_l7160",
  42. ]
  43. @dataclass
  44. class LabelData:
  45. """Per-spool data needed to render a label.
  46. Decoupled from the SQLAlchemy model so the same renderer serves the local
  47. inventory and the Spoolman-backed inventory.
  48. """
  49. spool_id: int
  50. name: str
  51. material: str
  52. brand: str | None = None
  53. subtype: str | None = None
  54. rgba: str | None = None # "RRGGBB" or "RRGGBBAA"; None → neutral grey
  55. extra_colors: list[str] | None = None # additional hex colours (no '#')
  56. storage_location: str | None = None
  57. deeplink_url: str = "" # what the QR encodes; caller composes it
  58. # ── Colour helpers ───────────────────────────────────────────────────────────
  59. def _color_from_hex(hex_str: str | None, fallback: Color = HexColor(0x808080)) -> Color:
  60. """Parse an RRGGBB or RRGGBBAA string (no '#') into a ReportLab Color.
  61. Alpha is honoured so multi-colour spools with translucent overlays render
  62. correctly. Falls back to ``fallback`` for None / malformed input rather
  63. than raising — labels should always print.
  64. """
  65. if not hex_str:
  66. return fallback
  67. h = hex_str.lstrip("#").strip()
  68. if len(h) not in (6, 8):
  69. return fallback
  70. try:
  71. r = int(h[0:2], 16) / 255.0
  72. g = int(h[2:4], 16) / 255.0
  73. b = int(h[4:6], 16) / 255.0
  74. a = int(h[6:8], 16) / 255.0 if len(h) == 8 else 1.0
  75. return Color(r, g, b, alpha=a)
  76. except ValueError:
  77. return fallback
  78. def _luminance(color: Color) -> float:
  79. """Perceived luminance of a ReportLab Color (0–1, WCAG-style approximation)."""
  80. return 0.299 * color.red + 0.587 * color.green + 0.114 * color.blue
  81. def _hex_code_label(rgba: str | None) -> str:
  82. """Format ``data.rgba`` as a printable ``#RRGGBB`` string for the label.
  83. Drops the alpha channel (printed labels can't show transparency) and
  84. upper-cases the hex digits to match the colour-picker convention used in
  85. the inventory UI. Returns an empty string for None / malformed input so
  86. the caller can ``if hex_code:`` skip drawing without an exception.
  87. """
  88. if not rgba:
  89. return ""
  90. h = rgba.lstrip("#").strip()
  91. if len(h) not in (6, 8):
  92. return ""
  93. rgb = h[:6]
  94. if not all(c in "0123456789abcdefABCDEF" for c in rgb):
  95. return ""
  96. return f"#{rgb.upper()}"
  97. # ── QR generation ────────────────────────────────────────────────────────────
  98. def _qr_png_bytes(payload: str, *, box_size: int = 4, border: int = 2) -> bytes:
  99. """Render ``payload`` as a tight QR PNG. Empty payload returns empty bytes
  100. so callers can skip drawing without checking ahead of time.
  101. """
  102. if not payload:
  103. return b""
  104. qr = qrcode.QRCode(
  105. version=None,
  106. # ERROR_CORRECT_L (7% recovery) rather than M (15%): a label QR only
  107. # needs to survive being scanned off clean stock, not physical damage,
  108. # and L encodes the same payload in a lower version (fewer, chunkier
  109. # modules). That extra module size is what makes the code printable on
  110. # low-resolution 203 dpi thermal printers, where M-level density bled
  111. # the modules together on small labels (#1870).
  112. error_correction=qrcode.constants.ERROR_CORRECT_L,
  113. box_size=box_size,
  114. border=border,
  115. )
  116. qr.add_data(payload)
  117. qr.make(fit=True)
  118. img = qr.make_image(fill_color="black", back_color="white")
  119. buf = io.BytesIO()
  120. img.save(buf, format="PNG")
  121. return buf.getvalue()
  122. # ── Single-label drawing ─────────────────────────────────────────────────────
  123. def _draw_swatch(c: rl_canvas.Canvas, x: float, y: float, w: float, h: float, data: LabelData) -> None:
  124. """Draw the colour swatch. Multi-colour spools use vertical stripes
  125. (matching the FilamentSwatch convention in the frontend)."""
  126. c.saveState()
  127. try:
  128. primary = _color_from_hex(data.rgba)
  129. extras = [_color_from_hex(h) for h in (data.extra_colors or []) if h]
  130. colors = [primary, *extras]
  131. if not colors:
  132. c.setFillColor(HexColor(0x808080))
  133. c.rect(x, y, w, h, stroke=0, fill=1)
  134. return
  135. stripe_w = w / len(colors)
  136. for i, col in enumerate(colors):
  137. c.setFillColor(col)
  138. c.rect(x + i * stripe_w, y, stripe_w, h, stroke=0, fill=1)
  139. # Thin black border so light-colour swatches stay visible on white labels.
  140. c.setStrokeColor(black)
  141. c.setLineWidth(0.3)
  142. c.rect(x, y, w, h, stroke=1, fill=0)
  143. finally:
  144. c.restoreState()
  145. def _roomy_qr_size(inner_w: float, inner_h: float) -> float:
  146. """QR edge length (points) for the roomy layout.
  147. Historically a flat 20% of inner width, which on the narrowest label
  148. (box_40x30, ~37.6 mm inner) rendered a ~7.5 mm QR — at 203 dpi each module
  149. fell below ~2 dots and the code bled into itself on thermal printers
  150. (#1870). A 12 mm floor keeps small labels scannable; the code is still
  151. capped by the inner height, an 18 mm absolute max, and ~45% of inner width
  152. so it can't crowd out the text column on an ultra-narrow label.
  153. """
  154. return min(max(inner_w * 0.20, 12 * mm), inner_h, 18 * mm, inner_w * 0.45)
  155. def _draw_qr(c: rl_canvas.Canvas, x: float, y: float, size: float, payload: str) -> None:
  156. """Embed a square QR at (x, y) with edge length ``size`` (in points)."""
  157. png = _qr_png_bytes(payload)
  158. if not png:
  159. return
  160. from reportlab.lib.utils import ImageReader
  161. img = ImageReader(io.BytesIO(png))
  162. c.drawImage(img, x, y, width=size, height=size, mask="auto")
  163. def _truncate_to_width(c: rl_canvas.Canvas, text: str, font: str, size: float, max_w: float) -> str:
  164. """Truncate ``text`` with an ellipsis so it fits within ``max_w`` points."""
  165. if c.stringWidth(text, font, size) <= max_w:
  166. return text
  167. ell = "…"
  168. while text and c.stringWidth(text + ell, font, size) > max_w:
  169. text = text[:-1]
  170. return text + ell if text else ell
  171. def _draw_label(
  172. c: rl_canvas.Canvas, x: float, y: float, w: float, h: float, data: LabelData, monochrome: bool = False
  173. ) -> None:
  174. """Render one label inside the box (x, y, w, h). Origin is bottom-left.
  175. Two layouts, picked by available height:
  176. - **Tight** (h < 20 mm): swatch on the left, three lines of text on the
  177. right (brand, material+subtype, big spool ID). No QR — at very small
  178. heights there is not enough horizontal room for swatch + text + QR
  179. without truncating away the user-need fields. Kept as the safety
  180. branch for any future ultra-small preset; the shipped templates all
  181. land in the roomy layout below.
  182. - **Roomy** (h >= 20 mm — AMS holder, box label, Avery sheets): swatch
  183. on the left, QR on the right, multi-line text in the middle column.
  184. Large spool ID anchored at bottom-left under the swatch so it stays
  185. readable at arm's length.
  186. """
  187. pad = 1.2 * mm
  188. inner_x, inner_y = x + pad, y + pad
  189. inner_w = w - 2 * pad
  190. inner_h = h - 2 * pad
  191. # Outer hairline border so labels are easy to cut out from blank stock.
  192. c.setStrokeColor(HexColor(0xCCCCCC))
  193. c.setLineWidth(0.4)
  194. c.rect(x, y, w, h, stroke=1, fill=0)
  195. is_tight = h < 20 * mm
  196. if is_tight:
  197. _draw_label_tight(c, x, y, w, h, inner_x, inner_y, inner_w, inner_h, pad, data, monochrome)
  198. else:
  199. _draw_label_roomy(c, x, y, w, h, inner_x, inner_y, inner_w, inner_h, pad, data, monochrome)
  200. def _draw_label_tight(
  201. c: rl_canvas.Canvas,
  202. x: float,
  203. y: float,
  204. w: float,
  205. h: float,
  206. inner_x: float,
  207. inner_y: float,
  208. inner_w: float,
  209. inner_h: float,
  210. pad: float,
  211. data: LabelData,
  212. monochrome: bool = False,
  213. ) -> None:
  214. """Tight layout (h < 20 mm). Swatch + brand/material/hex/ID, no QR."""
  215. # Monochrome: drop the colour swatch (see _draw_label_roomy) and give the
  216. # width to the text column (#1870).
  217. if monochrome:
  218. swatch_w = 0.0
  219. else:
  220. swatch_w = min(inner_h, inner_w * 0.35)
  221. swatch_y = inner_y + (inner_h - swatch_w) / 2
  222. _draw_swatch(c, inner_x, swatch_y, swatch_w, swatch_w, data)
  223. text_x = inner_x + swatch_w + pad
  224. text_w = inner_w - swatch_w - pad
  225. if text_w < 5 * mm:
  226. return # Pathological — even the swatch barely fits.
  227. c.setFillColor(black)
  228. # Top: brand — bumped to bold + larger per the #809 follow-up so it's the
  229. # easiest thing to read on a small AMS holder at arm's length.
  230. brand_size = 6.5
  231. if data.brand:
  232. c.setFont("Helvetica-Bold", brand_size)
  233. brand = _truncate_to_width(c, data.brand, "Helvetica-Bold", brand_size, text_w)
  234. c.drawString(text_x, y + h - pad - brand_size, brand)
  235. # Second line: material + subtype, small
  236. sub_size = 5
  237. sub_line = " ".join(filter(None, [data.material, data.subtype]))
  238. sub_y_baseline = y + h - pad - brand_size - 0.6 - sub_size
  239. if sub_line:
  240. c.setFont("Helvetica", sub_size)
  241. sub_line = _truncate_to_width(c, sub_line, "Helvetica", sub_size, text_w)
  242. c.drawString(text_x, sub_y_baseline, sub_line)
  243. # Third line (when there's room): hex code, tiny — useful when the user
  244. # has multiple near-identical colours in the same material family.
  245. hex_code = _hex_code_label(data.rgba)
  246. if hex_code:
  247. hex_size = 4.5
  248. hex_y = sub_y_baseline - 0.4 - hex_size
  249. # Don't render if it'd collide with the spool ID at the bottom.
  250. if hex_y > inner_y + 13:
  251. c.setFont("Helvetica", hex_size)
  252. c.drawString(text_x, hex_y, hex_code)
  253. # Bottom: BIG spool ID — the killer field at-a-glance.
  254. id_size = 13
  255. c.setFont("Helvetica-Bold", id_size)
  256. id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
  257. c.drawString(text_x, inner_y + 0.5, id_text)
  258. def _draw_label_roomy(
  259. c: rl_canvas.Canvas,
  260. x: float,
  261. y: float,
  262. w: float,
  263. h: float,
  264. inner_x: float,
  265. inner_y: float,
  266. inner_w: float,
  267. inner_h: float,
  268. pad: float,
  269. data: LabelData,
  270. monochrome: bool = False,
  271. ) -> None:
  272. """Box-label / Avery layout. Swatch left, QR right, text middle."""
  273. # Swatch: full inner height, ~18% of inner width but capped so we never
  274. # eat the text column on extreme aspect ratios. Omitted entirely in
  275. # monochrome mode — on a B&W thermal printer a colour block prints as a
  276. # muddy grey that conveys nothing, so we reclaim the space for text and
  277. # rely on the hex-code line to carry the colour (#1870, requested by
  278. # @Geoff-S). The hex code already renders below whenever rgba is set.
  279. if monochrome:
  280. swatch_w = 0.0
  281. else:
  282. swatch_w = min(inner_w * 0.18, inner_h, 16 * mm)
  283. _draw_swatch(c, inner_x, inner_y, swatch_w, inner_h, data)
  284. qr_size = _roomy_qr_size(inner_w, inner_h)
  285. qr_x = x + w - pad - qr_size
  286. qr_y = inner_y + (inner_h - qr_size) / 2
  287. _draw_qr(c, qr_x, qr_y, qr_size, data.deeplink_url)
  288. text_x = inner_x + swatch_w + 1.5 * mm
  289. text_w = qr_x - text_x - 1.5 * mm
  290. if text_w < 8 * mm:
  291. return
  292. c.setFillColor(black)
  293. # Build the text rows we want to render, in top→bottom order.
  294. line1 = data.brand or ""
  295. line2 = " · ".join(filter(None, [data.material, data.subtype]))
  296. name = data.name or ""
  297. hex_code = _hex_code_label(data.rgba)
  298. # Layout from the top of the text column.
  299. cursor_y = y + h - pad
  300. # Brand — bumped to bold + larger per the #809 follow-up.
  301. if line1:
  302. size = 8
  303. c.setFont("Helvetica-Bold", size)
  304. text = _truncate_to_width(c, line1, "Helvetica-Bold", size, text_w)
  305. cursor_y -= size
  306. c.drawString(text_x, cursor_y, text)
  307. cursor_y -= 1.2
  308. if line2:
  309. size = 7
  310. c.setFont("Helvetica", size)
  311. text = _truncate_to_width(c, line2, "Helvetica", size, text_w)
  312. cursor_y -= size
  313. c.drawString(text_x, cursor_y, text)
  314. cursor_y -= 1.5
  315. # Hex colour code — useful for telling near-identical material+colour
  316. # spools apart when the swatch is small or the user is colour-blind.
  317. if hex_code:
  318. size = 6.5
  319. c.setFont("Helvetica", size)
  320. cursor_y -= size
  321. c.drawString(text_x, cursor_y, hex_code)
  322. cursor_y -= 1.2
  323. if name and name != line1:
  324. size = 9
  325. c.setFont("Helvetica-Bold", size)
  326. text = _truncate_to_width(c, name, "Helvetica-Bold", size, text_w)
  327. cursor_y -= size
  328. c.drawString(text_x, cursor_y, text)
  329. cursor_y -= 1.2
  330. if data.storage_location:
  331. size = 6.5
  332. c.setFont("Helvetica-Oblique", size)
  333. text = _truncate_to_width(c, data.storage_location, "Helvetica-Oblique", size, text_w)
  334. cursor_y -= size
  335. c.drawString(text_x, cursor_y, text)
  336. # Spool ID — anchored at the bottom of the text column, big and bold.
  337. id_size = 16
  338. c.setFont("Helvetica-Bold", id_size)
  339. id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
  340. c.drawString(text_x, inner_y + 0.5, id_text)
  341. # ── Template entry points ────────────────────────────────────────────────────
  342. # (label_w_mm, label_h_mm) for single-label-per-page templates.
  343. _SINGLE_LABEL_SIZES_MM: dict[str, tuple[float, float]] = {
  344. "ams_holder_74x33": (74.0, 33.0),
  345. "ams_holder_75x55": (75.0, 55.0),
  346. "box_40x30": (40.0, 30.0),
  347. "box_62x29": (62.0, 29.0),
  348. }
  349. # Sheet template parameters: (page_size, label_w_mm, label_h_mm,
  350. # cols, rows, top_margin_mm, left_margin_mm,
  351. # col_gap_mm, row_gap_mm)
  352. _SHEET_TEMPLATES: dict[str, tuple] = {
  353. "avery_5160": (letter, 66.675, 25.4, 3, 10, 12.7, 4.76, 3.175, 0.0),
  354. "avery_l7160": (A4, 63.5, 38.1, 3, 7, 15.15, 7.0, 2.5, 0.0),
  355. }
  356. def _render_single_label_pdf(template: TemplateName, data_list: list[LabelData], monochrome: bool = False) -> bytes:
  357. w_mm, h_mm = _SINGLE_LABEL_SIZES_MM[template]
  358. page_w, page_h = w_mm * mm, h_mm * mm
  359. buf = io.BytesIO()
  360. c = rl_canvas.Canvas(buf, pagesize=(page_w, page_h))
  361. c.setTitle(f"Bambuddy spool labels ({template})")
  362. for data in data_list:
  363. _draw_label(c, 0, 0, page_w, page_h, data, monochrome)
  364. c.showPage()
  365. c.save()
  366. return buf.getvalue()
  367. def get_sheet_capacity(template: TemplateName) -> int | None:
  368. """Return the number of slots on a sheet template, or ``None`` for roll labels."""
  369. layout = _SHEET_TEMPLATES.get(template)
  370. if layout is None:
  371. return None
  372. return layout[3] * layout[4]
  373. def _render_sheet_pdf(
  374. template: TemplateName,
  375. data_list: list[LabelData],
  376. monochrome: bool,
  377. starting_position: int,
  378. ) -> bytes:
  379. page_size, w_mm, h_mm, cols, rows, top_mm, left_mm, col_gap_mm, row_gap_mm = _SHEET_TEMPLATES[template]
  380. page_w, page_h = page_size
  381. label_w = w_mm * mm
  382. label_h = h_mm * mm
  383. top_margin = top_mm * mm
  384. left_margin = left_mm * mm
  385. col_gap = col_gap_mm * mm
  386. row_gap = row_gap_mm * mm
  387. buf = io.BytesIO()
  388. c = rl_canvas.Canvas(buf, pagesize=page_size)
  389. c.setTitle(f"Bambuddy spool labels ({template})")
  390. per_page = cols * rows
  391. if starting_position < 1 or starting_position > per_page:
  392. raise ValueError(f"Starting position must be between 1 and {per_page} for {template}")
  393. data_index = 0
  394. page_number = 0
  395. while data_index < len(data_list):
  396. slot_offset = starting_position - 1 if page_number == 0 else 0
  397. page_capacity = per_page - slot_offset
  398. chunk = data_list[data_index : data_index + page_capacity]
  399. for idx, data in enumerate(chunk):
  400. slot_index = slot_offset + idx
  401. row = slot_index // cols
  402. col = slot_index % cols
  403. x = left_margin + col * (label_w + col_gap)
  404. y = page_h - top_margin - (row + 1) * label_h - row * row_gap
  405. _draw_label(c, x, y, label_w, label_h, data, monochrome)
  406. c.showPage()
  407. data_index += len(chunk)
  408. page_number += 1
  409. c.save()
  410. return buf.getvalue()
  411. def render_labels(
  412. template: TemplateName,
  413. data_list: list[LabelData],
  414. *,
  415. monochrome: bool = False,
  416. starting_position: int = 1,
  417. ) -> bytes:
  418. """Render ``data_list`` to a PDF using the named template. Returns bytes.
  419. Empty ``data_list`` still produces a valid (empty) PDF — callers should
  420. short-circuit beforehand if that's not desired.
  421. ``monochrome`` drops the colour swatch (which prints as a useless grey block
  422. on black-and-white thermal printers) and reclaims the space for text; the
  423. hex-code line still carries the colour. See #1870.
  424. ``starting_position`` is one-based and applies only to the first page of a
  425. sheet template. Later pages always begin at the first slot.
  426. """
  427. if template in _SINGLE_LABEL_SIZES_MM:
  428. if starting_position != 1:
  429. raise ValueError("Starting position is only supported for sheet label templates")
  430. return _render_single_label_pdf(template, data_list, monochrome)
  431. if template in _SHEET_TEMPLATES:
  432. return _render_sheet_pdf(template, data_list, monochrome, starting_position)
  433. raise ValueError(f"Unknown label template: {template!r}")
  434. __all__ = ["LabelData", "TemplateName", "get_sheet_capacity", "render_labels"]
  435. # white re-exported for completeness; future templates may need a paper-tone variant.
  436. _ = white