label_renderer.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. """PDF spool label rendering.
  2. Five fixed templates:
  3. - ``ams_30x15`` — 30×15 mm single label, fits the popular Makerworld AMS
  4. Filament Label Holder (model 752566). One label per page.
  5. - ``box_40x30`` — 40×30 mm single label, common DK/Brother roll size and a
  6. good fit for filament-bag/storage-bin labels (#809 follow-up). Roomy
  7. layout — swatch, QR, full text column with hex code.
  8. - ``box_62x29`` — 62×29 mm single label, sized for Brother PT/QL and Dymo
  9. generic small labels. One label per page.
  10. - ``avery_5160`` — US Letter sheet, 25.4×66.7 mm × 30 per sheet.
  11. - ``avery_l7160`` — A4 sheet, 38.1×63.5 mm × 21 per sheet.
  12. The renderer is decoupled from the Spool model: callers build a ``LabelData``
  13. list from whatever source (local DB, Spoolman, future) so the same code path
  14. works in both modes.
  15. Layout principle, taken from the issue's user need (`#809`): the **spool ID**
  16. is the most-recognisable field at arm's length and dominates the layout. Other
  17. fields (brand, material, name, storage location) fill remaining space; the QR
  18. code provides the round-trip back to ``/inventory?spool=<id>``.
  19. """
  20. from __future__ import annotations
  21. import io
  22. from dataclasses import dataclass
  23. from typing import Literal
  24. import qrcode
  25. from reportlab.lib.colors import Color, HexColor, black, white
  26. from reportlab.lib.pagesizes import A4, letter
  27. from reportlab.lib.units import mm
  28. from reportlab.pdfgen import canvas as rl_canvas
  29. TemplateName = Literal["ams_30x15", "box_40x30", "box_62x29", "avery_5160", "avery_l7160"]
  30. @dataclass
  31. class LabelData:
  32. """Per-spool data needed to render a label.
  33. Decoupled from the SQLAlchemy model so the same renderer serves the local
  34. inventory and the Spoolman-backed inventory.
  35. """
  36. spool_id: int
  37. name: str
  38. material: str
  39. brand: str | None = None
  40. subtype: str | None = None
  41. rgba: str | None = None # "RRGGBB" or "RRGGBBAA"; None → neutral grey
  42. extra_colors: list[str] | None = None # additional hex colours (no '#')
  43. storage_location: str | None = None
  44. deeplink_url: str = "" # what the QR encodes; caller composes it
  45. # ── Colour helpers ───────────────────────────────────────────────────────────
  46. def _color_from_hex(hex_str: str | None, fallback: Color = HexColor(0x808080)) -> Color:
  47. """Parse an RRGGBB or RRGGBBAA string (no '#') into a ReportLab Color.
  48. Alpha is honoured so multi-colour spools with translucent overlays render
  49. correctly. Falls back to ``fallback`` for None / malformed input rather
  50. than raising — labels should always print.
  51. """
  52. if not hex_str:
  53. return fallback
  54. h = hex_str.lstrip("#").strip()
  55. if len(h) not in (6, 8):
  56. return fallback
  57. try:
  58. r = int(h[0:2], 16) / 255.0
  59. g = int(h[2:4], 16) / 255.0
  60. b = int(h[4:6], 16) / 255.0
  61. a = int(h[6:8], 16) / 255.0 if len(h) == 8 else 1.0
  62. return Color(r, g, b, alpha=a)
  63. except ValueError:
  64. return fallback
  65. def _luminance(color: Color) -> float:
  66. """Perceived luminance of a ReportLab Color (0–1, WCAG-style approximation)."""
  67. return 0.299 * color.red + 0.587 * color.green + 0.114 * color.blue
  68. def _hex_code_label(rgba: str | None) -> str:
  69. """Format ``data.rgba`` as a printable ``#RRGGBB`` string for the label.
  70. Drops the alpha channel (printed labels can't show transparency) and
  71. upper-cases the hex digits to match the colour-picker convention used in
  72. the inventory UI. Returns an empty string for None / malformed input so
  73. the caller can ``if hex_code:`` skip drawing without an exception.
  74. """
  75. if not rgba:
  76. return ""
  77. h = rgba.lstrip("#").strip()
  78. if len(h) not in (6, 8):
  79. return ""
  80. rgb = h[:6]
  81. if not all(c in "0123456789abcdefABCDEF" for c in rgb):
  82. return ""
  83. return f"#{rgb.upper()}"
  84. # ── QR generation ────────────────────────────────────────────────────────────
  85. def _qr_png_bytes(payload: str, *, box_size: int = 4, border: int = 2) -> bytes:
  86. """Render ``payload`` as a tight QR PNG. Empty payload returns empty bytes
  87. so callers can skip drawing without checking ahead of time.
  88. """
  89. if not payload:
  90. return b""
  91. qr = qrcode.QRCode(
  92. version=None,
  93. error_correction=qrcode.constants.ERROR_CORRECT_M,
  94. box_size=box_size,
  95. border=border,
  96. )
  97. qr.add_data(payload)
  98. qr.make(fit=True)
  99. img = qr.make_image(fill_color="black", back_color="white")
  100. buf = io.BytesIO()
  101. img.save(buf, format="PNG")
  102. return buf.getvalue()
  103. # ── Single-label drawing ─────────────────────────────────────────────────────
  104. def _draw_swatch(c: rl_canvas.Canvas, x: float, y: float, w: float, h: float, data: LabelData) -> None:
  105. """Draw the colour swatch. Multi-colour spools use vertical stripes
  106. (matching the FilamentSwatch convention in the frontend)."""
  107. primary = _color_from_hex(data.rgba)
  108. extras = [_color_from_hex(h) for h in (data.extra_colors or []) if h]
  109. colors = [primary, *extras]
  110. if not colors:
  111. c.setFillColor(HexColor(0x808080))
  112. c.rect(x, y, w, h, stroke=0, fill=1)
  113. return
  114. stripe_w = w / len(colors)
  115. for i, col in enumerate(colors):
  116. c.setFillColor(col)
  117. c.rect(x + i * stripe_w, y, stripe_w, h, stroke=0, fill=1)
  118. # Thin black border so light-colour swatches stay visible on white labels.
  119. c.setStrokeColor(black)
  120. c.setLineWidth(0.3)
  121. c.rect(x, y, w, h, stroke=1, fill=0)
  122. def _draw_qr(c: rl_canvas.Canvas, x: float, y: float, size: float, payload: str) -> None:
  123. """Embed a square QR at (x, y) with edge length ``size`` (in points)."""
  124. png = _qr_png_bytes(payload)
  125. if not png:
  126. return
  127. from reportlab.lib.utils import ImageReader
  128. img = ImageReader(io.BytesIO(png))
  129. c.drawImage(img, x, y, width=size, height=size, mask="auto")
  130. def _truncate_to_width(c: rl_canvas.Canvas, text: str, font: str, size: float, max_w: float) -> str:
  131. """Truncate ``text`` with an ellipsis so it fits within ``max_w`` points."""
  132. if c.stringWidth(text, font, size) <= max_w:
  133. return text
  134. ell = "…"
  135. while text and c.stringWidth(text + ell, font, size) > max_w:
  136. text = text[:-1]
  137. return text + ell if text else ell
  138. def _draw_label(c: rl_canvas.Canvas, x: float, y: float, w: float, h: float, data: LabelData) -> None:
  139. """Render one label inside the box (x, y, w, h). Origin is bottom-left.
  140. Two layouts, picked by available height:
  141. - **Tight** (h < 20 mm — AMS holder): swatch on the left, three lines of
  142. text on the right (brand, material+subtype, big spool ID). No QR — at
  143. 30×15 mm there is not enough horizontal room for swatch + text + QR
  144. without truncating away the user-need fields, and the AMS holder is an
  145. at-a-glance identifier where the spool ID is the killer field. The
  146. box-label and Avery templates carry the QR for the other use cases.
  147. - **Roomy** (h >= 20 mm — box label, Avery sheets): swatch on the left,
  148. QR on the right, multi-line text in the middle column. Large spool ID
  149. anchored at bottom-left under the swatch so it stays readable when the
  150. label is on a box on a shelf at arm's length.
  151. """
  152. pad = 1.2 * mm
  153. inner_x, inner_y = x + pad, y + pad
  154. inner_w = w - 2 * pad
  155. inner_h = h - 2 * pad
  156. # Outer hairline border so labels are easy to cut out from blank stock.
  157. c.setStrokeColor(HexColor(0xCCCCCC))
  158. c.setLineWidth(0.4)
  159. c.rect(x, y, w, h, stroke=1, fill=0)
  160. is_tight = h < 20 * mm
  161. if is_tight:
  162. _draw_label_tight(c, x, y, w, h, inner_x, inner_y, inner_w, inner_h, pad, data)
  163. else:
  164. _draw_label_roomy(c, x, y, w, h, inner_x, inner_y, inner_w, inner_h, pad, data)
  165. def _draw_label_tight(
  166. c: rl_canvas.Canvas,
  167. x: float,
  168. y: float,
  169. w: float,
  170. h: float,
  171. inner_x: float,
  172. inner_y: float,
  173. inner_w: float,
  174. inner_h: float,
  175. pad: float,
  176. data: LabelData,
  177. ) -> None:
  178. """AMS-holder layout (e.g. 30×15 mm). Swatch + brand/material/hex/ID, no QR."""
  179. swatch_w = min(inner_h, inner_w * 0.35)
  180. swatch_y = inner_y + (inner_h - swatch_w) / 2
  181. _draw_swatch(c, inner_x, swatch_y, swatch_w, swatch_w, data)
  182. text_x = inner_x + swatch_w + pad
  183. text_w = inner_w - swatch_w - pad
  184. if text_w < 5 * mm:
  185. return # Pathological — even the swatch barely fits.
  186. c.setFillColor(black)
  187. # Top: brand — bumped to bold + larger per the #809 follow-up so it's the
  188. # easiest thing to read on a small AMS holder at arm's length.
  189. brand_size = 6.5
  190. if data.brand:
  191. c.setFont("Helvetica-Bold", brand_size)
  192. brand = _truncate_to_width(c, data.brand, "Helvetica-Bold", brand_size, text_w)
  193. c.drawString(text_x, y + h - pad - brand_size, brand)
  194. # Second line: material + subtype, small
  195. sub_size = 5
  196. sub_line = " ".join(filter(None, [data.material, data.subtype]))
  197. sub_y_baseline = y + h - pad - brand_size - 0.6 - sub_size
  198. if sub_line:
  199. c.setFont("Helvetica", sub_size)
  200. sub_line = _truncate_to_width(c, sub_line, "Helvetica", sub_size, text_w)
  201. c.drawString(text_x, sub_y_baseline, sub_line)
  202. # Third line (when there's room): hex code, tiny — useful when the user
  203. # has multiple near-identical colours in the same material family.
  204. hex_code = _hex_code_label(data.rgba)
  205. if hex_code:
  206. hex_size = 4.5
  207. hex_y = sub_y_baseline - 0.4 - hex_size
  208. # Don't render if it'd collide with the spool ID at the bottom.
  209. if hex_y > inner_y + 13:
  210. c.setFont("Helvetica", hex_size)
  211. c.drawString(text_x, hex_y, hex_code)
  212. # Bottom: BIG spool ID — the killer field at-a-glance.
  213. id_size = 13
  214. c.setFont("Helvetica-Bold", id_size)
  215. id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
  216. c.drawString(text_x, inner_y + 0.5, id_text)
  217. def _draw_label_roomy(
  218. c: rl_canvas.Canvas,
  219. x: float,
  220. y: float,
  221. w: float,
  222. h: float,
  223. inner_x: float,
  224. inner_y: float,
  225. inner_w: float,
  226. inner_h: float,
  227. pad: float,
  228. data: LabelData,
  229. ) -> None:
  230. """Box-label / Avery layout. Swatch left, QR right, text middle."""
  231. # Swatch: full inner height, ~18% of inner width but capped so we never
  232. # eat the text column on extreme aspect ratios.
  233. swatch_w = min(inner_w * 0.18, inner_h, 16 * mm)
  234. swatch_h = inner_h
  235. _draw_swatch(c, inner_x, inner_y, swatch_w, swatch_h, data)
  236. # QR: square, capped at the smaller of (a fraction of width, the inner
  237. # height, or 18 mm — beyond that the QR is overkill for the print size).
  238. qr_size = min(inner_w * 0.20, inner_h, 18 * mm)
  239. qr_x = x + w - pad - qr_size
  240. qr_y = inner_y + (inner_h - qr_size) / 2
  241. _draw_qr(c, qr_x, qr_y, qr_size, data.deeplink_url)
  242. text_x = inner_x + swatch_w + 1.5 * mm
  243. text_w = qr_x - text_x - 1.5 * mm
  244. if text_w < 8 * mm:
  245. return
  246. c.setFillColor(black)
  247. # Build the text rows we want to render, in top→bottom order.
  248. line1 = data.brand or ""
  249. line2 = " · ".join(filter(None, [data.material, data.subtype]))
  250. name = data.name or ""
  251. hex_code = _hex_code_label(data.rgba)
  252. # Layout from the top of the text column.
  253. cursor_y = y + h - pad
  254. # Brand — bumped to bold + larger per the #809 follow-up.
  255. if line1:
  256. size = 8
  257. c.setFont("Helvetica-Bold", size)
  258. text = _truncate_to_width(c, line1, "Helvetica-Bold", size, text_w)
  259. cursor_y -= size
  260. c.drawString(text_x, cursor_y, text)
  261. cursor_y -= 1.2
  262. if line2:
  263. size = 7
  264. c.setFont("Helvetica", size)
  265. text = _truncate_to_width(c, line2, "Helvetica", size, text_w)
  266. cursor_y -= size
  267. c.drawString(text_x, cursor_y, text)
  268. cursor_y -= 1.5
  269. # Hex colour code — useful for telling near-identical material+colour
  270. # spools apart when the swatch is small or the user is colour-blind.
  271. if hex_code:
  272. size = 6.5
  273. c.setFont("Helvetica", size)
  274. cursor_y -= size
  275. c.drawString(text_x, cursor_y, hex_code)
  276. cursor_y -= 1.2
  277. if name and name != line1:
  278. size = 9
  279. c.setFont("Helvetica-Bold", size)
  280. text = _truncate_to_width(c, name, "Helvetica-Bold", size, text_w)
  281. cursor_y -= size
  282. c.drawString(text_x, cursor_y, text)
  283. cursor_y -= 1.2
  284. if data.storage_location:
  285. size = 6.5
  286. c.setFont("Helvetica-Oblique", size)
  287. text = _truncate_to_width(c, data.storage_location, "Helvetica-Oblique", size, text_w)
  288. cursor_y -= size
  289. c.drawString(text_x, cursor_y, text)
  290. # Spool ID — anchored at the bottom of the text column, big and bold.
  291. id_size = 16
  292. c.setFont("Helvetica-Bold", id_size)
  293. id_text = _truncate_to_width(c, f"#{data.spool_id}", "Helvetica-Bold", id_size, text_w)
  294. c.drawString(text_x, inner_y + 0.5, id_text)
  295. # ── Template entry points ────────────────────────────────────────────────────
  296. # (label_w_mm, label_h_mm) for single-label-per-page templates.
  297. _SINGLE_LABEL_SIZES_MM: dict[str, tuple[float, float]] = {
  298. "ams_30x15": (30.0, 15.0),
  299. "box_40x30": (40.0, 30.0),
  300. "box_62x29": (62.0, 29.0),
  301. }
  302. # Sheet template parameters: (page_size, label_w_mm, label_h_mm,
  303. # cols, rows, top_margin_mm, left_margin_mm,
  304. # col_gap_mm, row_gap_mm)
  305. _SHEET_TEMPLATES: dict[str, tuple] = {
  306. "avery_5160": (letter, 66.675, 25.4, 3, 10, 12.7, 4.76, 3.175, 0.0),
  307. "avery_l7160": (A4, 63.5, 38.1, 3, 7, 15.15, 7.0, 2.5, 0.0),
  308. }
  309. def _render_single_label_pdf(template: TemplateName, data_list: list[LabelData]) -> bytes:
  310. w_mm, h_mm = _SINGLE_LABEL_SIZES_MM[template]
  311. page_w, page_h = w_mm * mm, h_mm * mm
  312. buf = io.BytesIO()
  313. c = rl_canvas.Canvas(buf, pagesize=(page_w, page_h))
  314. c.setTitle(f"Bambuddy spool labels ({template})")
  315. for data in data_list:
  316. _draw_label(c, 0, 0, page_w, page_h, data)
  317. c.showPage()
  318. c.save()
  319. return buf.getvalue()
  320. def _render_sheet_pdf(template: TemplateName, data_list: list[LabelData]) -> bytes:
  321. page_size, w_mm, h_mm, cols, rows, top_mm, left_mm, col_gap_mm, row_gap_mm = _SHEET_TEMPLATES[template]
  322. page_w, page_h = page_size
  323. label_w = w_mm * mm
  324. label_h = h_mm * mm
  325. top_margin = top_mm * mm
  326. left_margin = left_mm * mm
  327. col_gap = col_gap_mm * mm
  328. row_gap = row_gap_mm * mm
  329. buf = io.BytesIO()
  330. c = rl_canvas.Canvas(buf, pagesize=page_size)
  331. c.setTitle(f"Bambuddy spool labels ({template})")
  332. per_page = cols * rows
  333. for page_start in range(0, len(data_list), per_page):
  334. chunk = data_list[page_start : page_start + per_page]
  335. for idx, data in enumerate(chunk):
  336. row = idx // cols
  337. col = idx % cols
  338. x = left_margin + col * (label_w + col_gap)
  339. y = page_h - top_margin - (row + 1) * label_h - row * row_gap
  340. _draw_label(c, x, y, label_w, label_h, data)
  341. c.showPage()
  342. c.save()
  343. return buf.getvalue()
  344. def render_labels(template: TemplateName, data_list: list[LabelData]) -> bytes:
  345. """Render ``data_list`` to a PDF using the named template. Returns bytes.
  346. Empty ``data_list`` still produces a valid (empty) PDF — callers should
  347. short-circuit beforehand if that's not desired.
  348. """
  349. if template in _SINGLE_LABEL_SIZES_MM:
  350. return _render_single_label_pdf(template, data_list)
  351. if template in _SHEET_TEMPLATES:
  352. return _render_sheet_pdf(template, data_list)
  353. raise ValueError(f"Unknown label template: {template!r}")
  354. __all__ = ["LabelData", "TemplateName", "render_labels"]
  355. # white re-exported for completeness; future templates may need a paper-tone variant.
  356. _ = white