label_renderer.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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_correction=qrcode.constants.ERROR_CORRECT_M,
  107. box_size=box_size,
  108. border=border,
  109. )
  110. qr.add_data(payload)
  111. qr.make(fit=True)
  112. img = qr.make_image(fill_color="black", back_color="white")
  113. buf = io.BytesIO()
  114. img.save(buf, format="PNG")
  115. return buf.getvalue()
  116. # ── Single-label drawing ─────────────────────────────────────────────────────
  117. def _draw_swatch(c: rl_canvas.Canvas, x: float, y: float, w: float, h: float, data: LabelData) -> None:
  118. """Draw the colour swatch. Multi-colour spools use vertical stripes
  119. (matching the FilamentSwatch convention in the frontend)."""
  120. primary = _color_from_hex(data.rgba)
  121. extras = [_color_from_hex(h) for h in (data.extra_colors or []) if h]
  122. colors = [primary, *extras]
  123. if not colors:
  124. c.setFillColor(HexColor(0x808080))
  125. c.rect(x, y, w, h, stroke=0, fill=1)
  126. return
  127. stripe_w = w / len(colors)
  128. for i, col in enumerate(colors):
  129. c.setFillColor(col)
  130. c.rect(x + i * stripe_w, y, stripe_w, h, stroke=0, fill=1)
  131. # Thin black border so light-colour swatches stay visible on white labels.
  132. c.setStrokeColor(black)
  133. c.setLineWidth(0.3)
  134. c.rect(x, y, w, h, stroke=1, fill=0)
  135. def _draw_qr(c: rl_canvas.Canvas, x: float, y: float, size: float, payload: str) -> None:
  136. """Embed a square QR at (x, y) with edge length ``size`` (in points)."""
  137. png = _qr_png_bytes(payload)
  138. if not png:
  139. return
  140. from reportlab.lib.utils import ImageReader
  141. img = ImageReader(io.BytesIO(png))
  142. c.drawImage(img, x, y, width=size, height=size, mask="auto")
  143. def _truncate_to_width(c: rl_canvas.Canvas, text: str, font: str, size: float, max_w: float) -> str:
  144. """Truncate ``text`` with an ellipsis so it fits within ``max_w`` points."""
  145. if c.stringWidth(text, font, size) <= max_w:
  146. return text
  147. ell = "…"
  148. while text and c.stringWidth(text + ell, font, size) > max_w:
  149. text = text[:-1]
  150. return text + ell if text else ell
  151. def _draw_label(c: rl_canvas.Canvas, x: float, y: float, w: float, h: float, data: LabelData) -> None:
  152. """Render one label inside the box (x, y, w, h). Origin is bottom-left.
  153. Two layouts, picked by available height:
  154. - **Tight** (h < 20 mm): swatch on the left, three lines of text on the
  155. right (brand, material+subtype, big spool ID). No QR — at very small
  156. heights there is not enough horizontal room for swatch + text + QR
  157. without truncating away the user-need fields. Kept as the safety
  158. branch for any future ultra-small preset; the shipped templates all
  159. land in the roomy layout below.
  160. - **Roomy** (h >= 20 mm — AMS holder, box label, Avery sheets): swatch
  161. on the left, QR on the right, multi-line text in the middle column.
  162. Large spool ID anchored at bottom-left under the swatch so it stays
  163. readable at arm's length.
  164. """
  165. pad = 1.2 * mm
  166. inner_x, inner_y = x + pad, y + pad
  167. inner_w = w - 2 * pad
  168. inner_h = h - 2 * pad
  169. # Outer hairline border so labels are easy to cut out from blank stock.
  170. c.setStrokeColor(HexColor(0xCCCCCC))
  171. c.setLineWidth(0.4)
  172. c.rect(x, y, w, h, stroke=1, fill=0)
  173. is_tight = h < 20 * mm
  174. if is_tight:
  175. _draw_label_tight(c, x, y, w, h, inner_x, inner_y, inner_w, inner_h, pad, data)
  176. else:
  177. _draw_label_roomy(c, x, y, w, h, inner_x, inner_y, inner_w, inner_h, pad, data)
  178. def _draw_label_tight(
  179. c: rl_canvas.Canvas,
  180. x: float,
  181. y: float,
  182. w: float,
  183. h: float,
  184. inner_x: float,
  185. inner_y: float,
  186. inner_w: float,
  187. inner_h: float,
  188. pad: float,
  189. data: LabelData,
  190. ) -> None:
  191. """Tight layout (h < 20 mm). Swatch + brand/material/hex/ID, no QR."""
  192. swatch_w = min(inner_h, inner_w * 0.35)
  193. swatch_y = inner_y + (inner_h - swatch_w) / 2
  194. _draw_swatch(c, inner_x, swatch_y, swatch_w, swatch_w, data)
  195. text_x = inner_x + swatch_w + pad
  196. text_w = inner_w - swatch_w - pad
  197. if text_w < 5 * mm:
  198. return # Pathological — even the swatch barely fits.
  199. c.setFillColor(black)
  200. # Top: brand — bumped to bold + larger per the #809 follow-up so it's the
  201. # easiest thing to read on a small AMS holder at arm's length.
  202. brand_size = 6.5
  203. if data.brand:
  204. c.setFont("Helvetica-Bold", brand_size)
  205. brand = _truncate_to_width(c, data.brand, "Helvetica-Bold", brand_size, text_w)
  206. c.drawString(text_x, y + h - pad - brand_size, brand)
  207. # Second line: material + subtype, small
  208. sub_size = 5
  209. sub_line = " ".join(filter(None, [data.material, data.subtype]))
  210. sub_y_baseline = y + h - pad - brand_size - 0.6 - sub_size
  211. if sub_line:
  212. c.setFont("Helvetica", sub_size)
  213. sub_line = _truncate_to_width(c, sub_line, "Helvetica", sub_size, text_w)
  214. c.drawString(text_x, sub_y_baseline, sub_line)
  215. # Third line (when there's room): hex code, tiny — useful when the user
  216. # has multiple near-identical colours in the same material family.
  217. hex_code = _hex_code_label(data.rgba)
  218. if hex_code:
  219. hex_size = 4.5
  220. hex_y = sub_y_baseline - 0.4 - hex_size
  221. # Don't render if it'd collide with the spool ID at the bottom.
  222. if hex_y > inner_y + 13:
  223. c.setFont("Helvetica", hex_size)
  224. c.drawString(text_x, hex_y, hex_code)
  225. # Bottom: BIG spool ID — the killer field at-a-glance.
  226. id_size = 13
  227. c.setFont("Helvetica-Bold", id_size)
  228. id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
  229. c.drawString(text_x, inner_y + 0.5, id_text)
  230. def _draw_label_roomy(
  231. c: rl_canvas.Canvas,
  232. x: float,
  233. y: float,
  234. w: float,
  235. h: float,
  236. inner_x: float,
  237. inner_y: float,
  238. inner_w: float,
  239. inner_h: float,
  240. pad: float,
  241. data: LabelData,
  242. ) -> None:
  243. """Box-label / Avery layout. Swatch left, QR right, text middle."""
  244. # Swatch: full inner height, ~18% of inner width but capped so we never
  245. # eat the text column on extreme aspect ratios.
  246. swatch_w = min(inner_w * 0.18, inner_h, 16 * mm)
  247. swatch_h = inner_h
  248. _draw_swatch(c, inner_x, inner_y, swatch_w, swatch_h, data)
  249. # QR: square, capped at the smaller of (a fraction of width, the inner
  250. # height, or 18 mm — beyond that the QR is overkill for the print size).
  251. qr_size = min(inner_w * 0.20, inner_h, 18 * mm)
  252. qr_x = x + w - pad - qr_size
  253. qr_y = inner_y + (inner_h - qr_size) / 2
  254. _draw_qr(c, qr_x, qr_y, qr_size, data.deeplink_url)
  255. text_x = inner_x + swatch_w + 1.5 * mm
  256. text_w = qr_x - text_x - 1.5 * mm
  257. if text_w < 8 * mm:
  258. return
  259. c.setFillColor(black)
  260. # Build the text rows we want to render, in top→bottom order.
  261. line1 = data.brand or ""
  262. line2 = " · ".join(filter(None, [data.material, data.subtype]))
  263. name = data.name or ""
  264. hex_code = _hex_code_label(data.rgba)
  265. # Layout from the top of the text column.
  266. cursor_y = y + h - pad
  267. # Brand — bumped to bold + larger per the #809 follow-up.
  268. if line1:
  269. size = 8
  270. c.setFont("Helvetica-Bold", size)
  271. text = _truncate_to_width(c, line1, "Helvetica-Bold", size, text_w)
  272. cursor_y -= size
  273. c.drawString(text_x, cursor_y, text)
  274. cursor_y -= 1.2
  275. if line2:
  276. size = 7
  277. c.setFont("Helvetica", size)
  278. text = _truncate_to_width(c, line2, "Helvetica", size, text_w)
  279. cursor_y -= size
  280. c.drawString(text_x, cursor_y, text)
  281. cursor_y -= 1.5
  282. # Hex colour code — useful for telling near-identical material+colour
  283. # spools apart when the swatch is small or the user is colour-blind.
  284. if hex_code:
  285. size = 6.5
  286. c.setFont("Helvetica", size)
  287. cursor_y -= size
  288. c.drawString(text_x, cursor_y, hex_code)
  289. cursor_y -= 1.2
  290. if name and name != line1:
  291. size = 9
  292. c.setFont("Helvetica-Bold", size)
  293. text = _truncate_to_width(c, name, "Helvetica-Bold", size, text_w)
  294. cursor_y -= size
  295. c.drawString(text_x, cursor_y, text)
  296. cursor_y -= 1.2
  297. if data.storage_location:
  298. size = 6.5
  299. c.setFont("Helvetica-Oblique", size)
  300. text = _truncate_to_width(c, data.storage_location, "Helvetica-Oblique", size, text_w)
  301. cursor_y -= size
  302. c.drawString(text_x, cursor_y, text)
  303. # Spool ID — anchored at the bottom of the text column, big and bold.
  304. id_size = 16
  305. c.setFont("Helvetica-Bold", id_size)
  306. id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
  307. c.drawString(text_x, inner_y + 0.5, id_text)
  308. # ── Template entry points ────────────────────────────────────────────────────
  309. # (label_w_mm, label_h_mm) for single-label-per-page templates.
  310. _SINGLE_LABEL_SIZES_MM: dict[str, tuple[float, float]] = {
  311. "ams_holder_74x33": (74.0, 33.0),
  312. "ams_holder_75x55": (75.0, 55.0),
  313. "box_40x30": (40.0, 30.0),
  314. "box_62x29": (62.0, 29.0),
  315. }
  316. # Sheet template parameters: (page_size, label_w_mm, label_h_mm,
  317. # cols, rows, top_margin_mm, left_margin_mm,
  318. # col_gap_mm, row_gap_mm)
  319. _SHEET_TEMPLATES: dict[str, tuple] = {
  320. "avery_5160": (letter, 66.675, 25.4, 3, 10, 12.7, 4.76, 3.175, 0.0),
  321. "avery_l7160": (A4, 63.5, 38.1, 3, 7, 15.15, 7.0, 2.5, 0.0),
  322. }
  323. def _render_single_label_pdf(template: TemplateName, data_list: list[LabelData]) -> bytes:
  324. w_mm, h_mm = _SINGLE_LABEL_SIZES_MM[template]
  325. page_w, page_h = w_mm * mm, h_mm * mm
  326. buf = io.BytesIO()
  327. c = rl_canvas.Canvas(buf, pagesize=(page_w, page_h))
  328. c.setTitle(f"Bambuddy spool labels ({template})")
  329. for data in data_list:
  330. _draw_label(c, 0, 0, page_w, page_h, data)
  331. c.showPage()
  332. c.save()
  333. return buf.getvalue()
  334. def _render_sheet_pdf(template: TemplateName, data_list: list[LabelData]) -> bytes:
  335. page_size, w_mm, h_mm, cols, rows, top_mm, left_mm, col_gap_mm, row_gap_mm = _SHEET_TEMPLATES[template]
  336. page_w, page_h = page_size
  337. label_w = w_mm * mm
  338. label_h = h_mm * mm
  339. top_margin = top_mm * mm
  340. left_margin = left_mm * mm
  341. col_gap = col_gap_mm * mm
  342. row_gap = row_gap_mm * mm
  343. buf = io.BytesIO()
  344. c = rl_canvas.Canvas(buf, pagesize=page_size)
  345. c.setTitle(f"Bambuddy spool labels ({template})")
  346. per_page = cols * rows
  347. for page_start in range(0, len(data_list), per_page):
  348. chunk = data_list[page_start : page_start + per_page]
  349. for idx, data in enumerate(chunk):
  350. row = idx // cols
  351. col = idx % cols
  352. x = left_margin + col * (label_w + col_gap)
  353. y = page_h - top_margin - (row + 1) * label_h - row * row_gap
  354. _draw_label(c, x, y, label_w, label_h, data)
  355. c.showPage()
  356. c.save()
  357. return buf.getvalue()
  358. def render_labels(template: TemplateName, data_list: list[LabelData]) -> bytes:
  359. """Render ``data_list`` to a PDF using the named template. Returns bytes.
  360. Empty ``data_list`` still produces a valid (empty) PDF — callers should
  361. short-circuit beforehand if that's not desired.
  362. """
  363. if template in _SINGLE_LABEL_SIZES_MM:
  364. return _render_single_label_pdf(template, data_list)
  365. if template in _SHEET_TEMPLATES:
  366. return _render_sheet_pdf(template, data_list)
  367. raise ValueError(f"Unknown label template: {template!r}")
  368. __all__ = ["LabelData", "TemplateName", "render_labels"]
  369. # white re-exported for completeness; future templates may need a paper-tone variant.
  370. _ = white