render_body.py 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #!/usr/bin/env python3
  2. """
  3. render_body.py — Build the inner-page PDF from tokens.json + content.json.
  4. Usage:
  5. python3 render_body.py --tokens tokens.json --content content.json --out body.pdf
  6. Block types:
  7. h1 h2 h3 Headings (h1 adds a full-width accent rule below)
  8. body Justified prose paragraph
  9. bullet Bullet list item (• prefix)
  10. numbered Auto-numbered list item (resets when interrupted)
  11. callout Highlighted insight box with left accent bar
  12. table Data table with accent header + alternating rows
  13. image Inline image from file path
  14. figure Image with auto-numbered "Figure N:" caption
  15. code Monospace code block with accent left border
  16. math Display math formula via matplotlib mathtext
  17. chart Bar / line / pie chart rendered via matplotlib
  18. flowchart Process diagram rendered via matplotlib
  19. bibliography Numbered reference list
  20. divider Full-width accent rule
  21. caption Small muted text (e.g., under a figure)
  22. pagebreak Force a new page
  23. spacer Vertical whitespace (pt field, default 12)
  24. Exit codes: 0 success, 1 bad args/missing file, 2 missing dep, 3 render error
  25. """
  26. import argparse
  27. import io
  28. import json
  29. import os
  30. import sys
  31. import importlib.util
  32. # ── Dependency bootstrap ───────────────────────────────────────────────────────
  33. def ensure_deps():
  34. missing = [p for p in ("reportlab", "pypdf")
  35. if importlib.util.find_spec(p) is None]
  36. if missing:
  37. import subprocess
  38. subprocess.check_call(
  39. [sys.executable, "-m", "pip", "install",
  40. "--break-system-packages", "-q"] + missing
  41. )
  42. ensure_deps()
  43. from reportlab.platypus import (
  44. BaseDocTemplate, PageTemplate, Frame,
  45. Paragraph, Spacer, Table, TableStyle,
  46. HRFlowable, PageBreak, Flowable, KeepTogether,
  47. Preformatted, Image as RLImage,
  48. )
  49. from reportlab.lib.pagesizes import A4
  50. from reportlab.lib.styles import ParagraphStyle
  51. from reportlab.lib.colors import HexColor
  52. from reportlab.lib.enums import TA_JUSTIFY, TA_CENTER
  53. from reportlab.pdfbase import pdfmetrics
  54. from reportlab.pdfbase.ttfonts import TTFont
  55. # ── Font registration ──────────────────────────────────────────────────────────
  56. def register_fonts(tokens: dict):
  57. """Register TTF fonts from token font_paths if present."""
  58. for name, fpath in tokens.get("font_paths", {}).items():
  59. if os.path.exists(fpath):
  60. try:
  61. pdfmetrics.registerFont(TTFont(name, fpath))
  62. except Exception:
  63. pass
  64. # ══════════════════════════════════════════════════════════════════════════════
  65. # Custom Flowables
  66. # ══════════════════════════════════════════════════════════════════════════════
  67. class CalloutBox(Flowable):
  68. """Highlighted insight box: coloured background + 4px left accent bar."""
  69. def __init__(self, text: str, style, accent: str, bg: str):
  70. super().__init__()
  71. self._para = Paragraph(text, style)
  72. self._accent = HexColor(accent)
  73. self._bg = HexColor(bg)
  74. def wrap(self, aw, ah):
  75. self._w = aw
  76. _, ph = self._para.wrap(aw - 36, ah)
  77. self._h = ph + 22
  78. return aw, self._h
  79. def draw(self):
  80. c = self.canv
  81. c.setFillColor(self._bg)
  82. c.roundRect(0, 0, self._w, self._h, 5, fill=1, stroke=0)
  83. c.setFillColor(self._accent)
  84. c.rect(0, 0, 4, self._h, fill=1, stroke=0)
  85. self._para.drawOn(c, 18, 11)
  86. class BibliographyItem(Flowable):
  87. """Single hanging-indent bibliography entry rendered as [N] text."""
  88. LABEL_W = 28
  89. def __init__(self, ref_id: str, text: str, style, dark: str):
  90. super().__init__()
  91. self._id = ref_id
  92. self._text = text
  93. self._style = style
  94. self._dark = HexColor(dark)
  95. def wrap(self, aw, ah):
  96. self._w = aw
  97. self._para = Paragraph(self._text, self._style)
  98. _, ph = self._para.wrap(aw - self.LABEL_W, ah)
  99. self._h = ph + 4
  100. return aw, self._h
  101. def draw(self):
  102. c = self.canv
  103. c.setFillColor(self._dark)
  104. c.setFont("Helvetica-Bold", 8.5)
  105. c.drawString(0, self._h - 12, f"[{self._id}]")
  106. self._para.drawOn(c, self.LABEL_W, 2)
  107. # ══════════════════════════════════════════════════════════════════════════════
  108. # Page template (header + footer)
  109. # ══════════════════════════════════════════════════════════════════════════════
  110. class BeautifulDoc(BaseDocTemplate):
  111. def __init__(self, path: str, tokens: dict, **kw):
  112. self._t = tokens
  113. super().__init__(path, **kw)
  114. fr = Frame(
  115. self.leftMargin, self.bottomMargin,
  116. self.width, self.height, id="body",
  117. )
  118. tmpl = PageTemplate(id="main", frames=fr, onPage=self._decorate)
  119. self.addPageTemplates([tmpl])
  120. def _decorate(self, canv, doc):
  121. t = self._t
  122. lm = doc.leftMargin
  123. rm = doc.rightMargin
  124. pw = doc.pagesize[0]
  125. ph = doc.pagesize[1]
  126. top = ph - doc.topMargin
  127. canv.saveState()
  128. # Header accent rule
  129. canv.setStrokeColor(HexColor(t["accent"]))
  130. canv.setLineWidth(1.5)
  131. canv.line(lm, top + 12, pw - rm, top + 12)
  132. # Header: title (left) + date (right)
  133. canv.setFillColor(HexColor(t["muted"]))
  134. canv.setFont(t["font_body_rl"], t["size_meta"])
  135. canv.drawString(lm, top + 16, t["title"].upper())
  136. canv.drawRightString(pw - rm, top + 16, t.get("date", ""))
  137. # Footer rule
  138. canv.setStrokeColor(HexColor("#DDDDDD"))
  139. canv.setLineWidth(0.5)
  140. canv.line(lm, doc.bottomMargin - 12, pw - rm, doc.bottomMargin - 12)
  141. # Footer: author (left) + page number (right)
  142. canv.setFillColor(HexColor(t["muted"]))
  143. canv.setFont(t["font_body_rl"], t["size_meta"])
  144. canv.drawString(lm, doc.bottomMargin - 22, t.get("author", ""))
  145. canv.drawRightString(pw - rm, doc.bottomMargin - 22, str(doc.page))
  146. canv.restoreState()
  147. # ══════════════════════════════════════════════════════════════════════════════
  148. # Style factory
  149. # ══════════════════════════════════════════════════════════════════════════════
  150. def make_styles(t: dict) -> dict:
  151. hf = t["font_display_rl"]
  152. bf = t["font_body_rl"]
  153. bfb = t["font_body_b_rl"]
  154. dk = t["body_text"]
  155. d = t["dark"]
  156. mu = t["muted"]
  157. return {
  158. "h1": ParagraphStyle("H1",
  159. fontName=hf, fontSize=t["size_h1"],
  160. leading=t["size_h1"] * 1.3,
  161. textColor=HexColor(d),
  162. spaceBefore=t["section_gap"], spaceAfter=4,
  163. ),
  164. "h2": ParagraphStyle("H2",
  165. fontName=hf, fontSize=t["size_h2"],
  166. leading=t["size_h2"] * 1.4,
  167. textColor=HexColor(d),
  168. spaceBefore=18, spaceAfter=5,
  169. ),
  170. "h3": ParagraphStyle("H3",
  171. fontName=bfb, fontSize=t["size_h3"],
  172. leading=t["size_h3"] * 1.5,
  173. textColor=HexColor(d),
  174. spaceBefore=12, spaceAfter=3,
  175. ),
  176. "body": ParagraphStyle("Body",
  177. fontName=bf, fontSize=t["size_body"],
  178. leading=t["line_gap"],
  179. textColor=HexColor(dk),
  180. spaceAfter=t["para_gap"], alignment=TA_JUSTIFY,
  181. ),
  182. "bullet": ParagraphStyle("Bullet",
  183. fontName=bf, fontSize=t["size_body"],
  184. leading=t["line_gap"] - 1,
  185. textColor=HexColor(dk),
  186. spaceAfter=4, leftIndent=14,
  187. ),
  188. "numbered": ParagraphStyle("Numbered",
  189. fontName=bf, fontSize=t["size_body"],
  190. leading=t["line_gap"] - 1,
  191. textColor=HexColor(dk),
  192. spaceAfter=4, leftIndent=22, firstLineIndent=-22,
  193. ),
  194. "callout": ParagraphStyle("Callout",
  195. fontName=bfb, fontSize=t["size_body"] + 0.5, leading=16,
  196. textColor=HexColor(d),
  197. ),
  198. "caption": ParagraphStyle("Caption",
  199. fontName=bf, fontSize=t["size_caption"], leading=13,
  200. textColor=HexColor(mu), spaceAfter=6,
  201. alignment=TA_CENTER,
  202. ),
  203. "table_header": ParagraphStyle("TblH",
  204. fontName=bfb, fontSize=9.5, leading=13,
  205. textColor=HexColor("#FFFFFF"),
  206. ),
  207. "table_cell": ParagraphStyle("TblC",
  208. fontName=bf, fontSize=9.5, leading=13,
  209. textColor=HexColor(dk),
  210. ),
  211. "code": ParagraphStyle("Code",
  212. fontName="Courier", fontSize=8.5, leading=12.5,
  213. textColor=HexColor(dk),
  214. ),
  215. "code_lang": ParagraphStyle("CodeLang",
  216. fontName="Courier", fontSize=7, leading=10,
  217. textColor=HexColor(mu),
  218. ),
  219. "bib": ParagraphStyle("Bib",
  220. fontName=bf, fontSize=9, leading=14,
  221. textColor=HexColor(dk),
  222. ),
  223. "bib_title": ParagraphStyle("BibTitle",
  224. fontName=hf, fontSize=t["size_h2"],
  225. leading=t["size_h2"] * 1.4,
  226. textColor=HexColor(d),
  227. spaceBefore=t["section_gap"], spaceAfter=8,
  228. ),
  229. "math_fallback": ParagraphStyle("MathFb",
  230. fontName="Courier", fontSize=9, leading=13,
  231. textColor=HexColor(dk),
  232. ),
  233. "eq_label": ParagraphStyle("EqLabel",
  234. fontName="Helvetica", fontSize=9, leading=12,
  235. textColor=HexColor(mu),
  236. ),
  237. }
  238. # ══════════════════════════════════════════════════════════════════════════════
  239. # Shared helpers
  240. # ══════════════════════════════════════════════════════════════════════════════
  241. def _divider(accent: str) -> HRFlowable:
  242. return HRFlowable(
  243. width="100%", thickness=1.2,
  244. color=HexColor(accent),
  245. spaceBefore=14, spaceAfter=14,
  246. )
  247. def _image_from_bytes(png_bytes: bytes, usable_w: float,
  248. max_frac: float = 0.88) -> RLImage:
  249. """Create a scaled RLImage from PNG bytes, bounded to max_frac of usable_w."""
  250. img = RLImage(io.BytesIO(png_bytes))
  251. max_w = usable_w * max_frac
  252. if img.drawWidth > max_w:
  253. scale = max_w / img.drawWidth
  254. img.drawWidth = max_w
  255. img.drawHeight = img.drawHeight * scale
  256. return img
  257. # ══════════════════════════════════════════════════════════════════════════════
  258. # PNG renderers (matplotlib)
  259. # ══════════════════════════════════════════════════════════════════════════════
  260. def _render_math_png(expr: str, dpi: int = 180) -> bytes | None:
  261. """
  262. Render a LaTeX math expression via matplotlib mathtext.
  263. No LaTeX binary required — uses matplotlib's built-in math parser.
  264. Supports: fractions (\\frac), integrals (\\int), sums (\\sum),
  265. Greek letters, sub/superscripts, etc.
  266. """
  267. try:
  268. import matplotlib
  269. matplotlib.use("Agg")
  270. import matplotlib.pyplot as plt
  271. fig = plt.figure(figsize=(8, 1.2))
  272. fig.patch.set_facecolor("white")
  273. ax = fig.add_axes([0, 0, 1, 1])
  274. ax.set_axis_off()
  275. ax.set_facecolor("white")
  276. ax.text(0.5, 0.5, f"${expr}$",
  277. fontsize=16, ha="center", va="center",
  278. transform=ax.transAxes)
  279. buf = io.BytesIO()
  280. fig.savefig(buf, format="png", dpi=dpi, bbox_inches="tight",
  281. facecolor="white", pad_inches=0.1)
  282. plt.close(fig)
  283. buf.seek(0)
  284. return buf.read()
  285. except Exception:
  286. return None
  287. def _render_chart_png(item: dict, accent: str, dpi: int = 150) -> bytes | None:
  288. """
  289. Render bar / line / pie chart to PNG using matplotlib.
  290. Required fields:
  291. chart_type "bar" | "line" | "pie" (default "bar")
  292. labels list of category strings
  293. datasets list of {label?, values: list[number]}
  294. Optional fields:
  295. title chart title
  296. x_label X-axis label
  297. y_label Y-axis label
  298. """
  299. try:
  300. import matplotlib
  301. matplotlib.use("Agg")
  302. import matplotlib.pyplot as plt
  303. import matplotlib.colors as mcolors
  304. import colorsys
  305. import numpy as np
  306. chart_type = item.get("chart_type", "bar")
  307. title_text = item.get("title", "")
  308. labels = item.get("labels", [])
  309. datasets = item.get("datasets", [])
  310. # Derive a consistent palette from the document accent color
  311. r, g, b = mcolors.to_rgb(accent)
  312. h, s, v = colorsys.rgb_to_hsv(r, g, b)
  313. palette = [
  314. colorsys.hsv_to_rgb(
  315. (h + i * 0.13) % 1.0,
  316. max(0.35, s - i * 0.08),
  317. min(0.92, v + i * 0.04),
  318. )
  319. for i in range(max(len(datasets), 1))
  320. ]
  321. fig, ax = plt.subplots(figsize=(7, 3.6), dpi=dpi)
  322. fig.patch.set_facecolor("white")
  323. ax.set_facecolor("white")
  324. if chart_type == "bar":
  325. x = np.arange(len(labels))
  326. n = max(len(datasets), 1)
  327. width = 0.68 / n
  328. for i, ds in enumerate(datasets):
  329. offset = (i - (n - 1) / 2) * width
  330. ax.bar(x + offset, ds.get("values", []), width * 0.88,
  331. label=ds.get("label", f"Series {i+1}"),
  332. color=palette[i % len(palette)], edgecolor="none")
  333. ax.set_xticks(x)
  334. ax.set_xticklabels(labels, fontsize=8.5)
  335. ax.yaxis.grid(True, alpha=0.25, color="#CCCCCC", linewidth=0.7)
  336. ax.set_axisbelow(True)
  337. if item.get("x_label"):
  338. ax.set_xlabel(item["x_label"], fontsize=8.5)
  339. if item.get("y_label"):
  340. ax.set_ylabel(item["y_label"], fontsize=8.5)
  341. elif chart_type == "line":
  342. x = np.arange(len(labels))
  343. for i, ds in enumerate(datasets):
  344. ax.plot(x, ds.get("values", []), marker="o", markersize=3.5,
  345. label=ds.get("label", f"Series {i+1}"),
  346. color=palette[i % len(palette)], linewidth=1.8)
  347. ax.set_xticks(x)
  348. ax.set_xticklabels(labels, fontsize=8.5)
  349. ax.yaxis.grid(True, alpha=0.25, color="#CCCCCC", linewidth=0.7)
  350. ax.set_axisbelow(True)
  351. if item.get("x_label"):
  352. ax.set_xlabel(item["x_label"], fontsize=8.5)
  353. if item.get("y_label"):
  354. ax.set_ylabel(item["y_label"], fontsize=8.5)
  355. elif chart_type == "pie":
  356. vals = datasets[0].get("values", []) if datasets else []
  357. colors = [
  358. colorsys.hsv_to_rgb(
  359. (h + i * 0.11) % 1.0,
  360. max(0.30, s - i * 0.06),
  361. min(0.92, v + i * 0.03),
  362. )
  363. for i in range(len(vals))
  364. ]
  365. ax.pie(vals, labels=labels, colors=colors,
  366. autopct="%1.1f%%", pctdistance=0.82,
  367. wedgeprops=dict(edgecolor="white", linewidth=1.4),
  368. textprops=dict(fontsize=8.5))
  369. # Shared styling
  370. for spine in ax.spines.values():
  371. spine.set_linewidth(0.5)
  372. spine.set_color("#CCCCCC")
  373. ax.tick_params(axis="both", length=0, labelsize=8.5)
  374. if title_text:
  375. ax.set_title(title_text, fontsize=10, pad=8,
  376. color="#333333", fontweight="bold")
  377. if len(datasets) > 1 and chart_type != "pie":
  378. ax.legend(frameon=False, fontsize=8, loc="upper right")
  379. plt.tight_layout(pad=0.4)
  380. buf = io.BytesIO()
  381. fig.savefig(buf, format="png", dpi=dpi, bbox_inches="tight",
  382. facecolor="white", pad_inches=0.06)
  383. plt.close(fig)
  384. buf.seek(0)
  385. return buf.read()
  386. except Exception:
  387. return None
  388. def _render_flowchart_png(item: dict, accent: str, dark: str,
  389. muted: str, dpi: int = 130) -> bytes | None:
  390. """
  391. Render a top-to-bottom flowchart using matplotlib patches and arrows.
  392. Node schema: {id, label, shape?}
  393. shape: "rect" (default) | "diamond" | "oval" | "parallelogram"
  394. Edge schema: {from, to, label?}
  395. Forward edges (to a later node) draw straight arrows.
  396. Back edges (to an earlier node) draw a curved arc to the right.
  397. """
  398. try:
  399. import matplotlib
  400. matplotlib.use("Agg")
  401. import matplotlib.pyplot as plt
  402. import matplotlib.patches as mpatch
  403. from matplotlib.patches import FancyBboxPatch
  404. import matplotlib.colors as mcolors
  405. nodes_list = item.get("nodes", [])
  406. edges = item.get("edges", [])
  407. if not nodes_list:
  408. return None
  409. nodes = {n["id"]: n for n in nodes_list}
  410. order = {n["id"]: i for i, n in enumerate(nodes_list)}
  411. n_nodes = len(nodes_list)
  412. BOX_W = 4.2
  413. BOX_H = 0.58
  414. STEP_Y = 1.25
  415. CX = 5.0
  416. fig_h = max(3.5, n_nodes * STEP_Y + 0.8)
  417. fig, ax = plt.subplots(figsize=(6, fig_h), dpi=dpi)
  418. fig.patch.set_facecolor("white")
  419. ax.set_facecolor("white")
  420. ax.set_xlim(0, 10)
  421. ax.set_ylim(-0.6, n_nodes * STEP_Y + 0.2)
  422. ax.invert_yaxis()
  423. ax.axis("off")
  424. acc_rgb = mcolors.to_rgb(accent)
  425. dark_rgb = mcolors.to_rgb(dark)
  426. muted_rgb = mcolors.to_rgb(muted)
  427. # Node positions (cx, cy) — preserves input order
  428. pos = {nid: (CX, i * STEP_Y) for nid, i in order.items()}
  429. # ── Draw edges (behind nodes) ──────────────────────────────────────────
  430. for edge in edges:
  431. src, dst = edge.get("from"), edge.get("to")
  432. if src not in pos or dst not in pos:
  433. continue
  434. x1, y1 = pos[src]
  435. x2, y2 = pos[dst]
  436. lbl = edge.get("label", "")
  437. src_shape = nodes.get(src, {}).get("shape", "rect")
  438. dst_shape = nodes.get(dst, {}).get("shape", "rect")
  439. dy_src = BOX_H * (0.80 if src_shape == "diamond" else 0.50)
  440. dy_dst = BOX_H * (0.80 if dst_shape == "diamond" else 0.50)
  441. y_start = y1 + dy_src
  442. y_end = y2 - dy_dst
  443. # Forward edge: straight; back-edge: curved arc
  444. conn = "arc3,rad=0.0" if y_end > y_start + 0.01 else "arc3,rad=0.42"
  445. ax.annotate("",
  446. xy=(x2, y_end), xytext=(x1, y_start),
  447. arrowprops=dict(
  448. arrowstyle="-|>", color=muted_rgb,
  449. lw=1.0, mutation_scale=10,
  450. connectionstyle=conn,
  451. ),
  452. )
  453. if lbl:
  454. mid_x = (x1 + x2) / 2 + 0.28
  455. mid_y = (y_start + y_end) / 2
  456. ax.text(mid_x, mid_y, lbl, fontsize=7.5,
  457. color=muted_rgb, ha="left", va="center")
  458. # ── Draw nodes (in front of edges) ────────────────────────────────────
  459. for nid, (cx, cy) in pos.items():
  460. node = nodes[nid]
  461. shape = node.get("shape", "rect")
  462. label = node.get("label", nid)
  463. left = cx - BOX_W / 2
  464. bot = cy - BOX_H / 2
  465. if shape in ("oval", "terminal"):
  466. el = mpatch.Ellipse(
  467. (cx, cy), BOX_W * 0.78, BOX_H * 1.15,
  468. facecolor=acc_rgb, edgecolor=acc_rgb, linewidth=0,
  469. )
  470. ax.add_patch(el)
  471. ax.text(cx, cy, label, ha="center", va="center",
  472. fontsize=8.5, fontweight="bold", color="white")
  473. elif shape == "diamond":
  474. d = BOX_W * 0.44
  475. diamond = plt.Polygon(
  476. [(cx, cy - d * 0.72), (cx + d, cy),
  477. (cx, cy + d * 0.72), (cx - d, cy)],
  478. facecolor="#FFFCF0",
  479. edgecolor=accent, linewidth=1.2,
  480. )
  481. ax.add_patch(diamond)
  482. ax.text(cx, cy, label, ha="center", va="center",
  483. fontsize=8, color=dark_rgb)
  484. elif shape == "parallelogram":
  485. skew = 0.30
  486. para = plt.Polygon(
  487. [(left + skew, bot), (left + BOX_W + skew, bot),
  488. (left + BOX_W, bot + BOX_H), (left, bot + BOX_H)],
  489. facecolor="white",
  490. edgecolor=accent, linewidth=1.2,
  491. )
  492. ax.add_patch(para)
  493. ax.text(cx, cy, label, ha="center", va="center",
  494. fontsize=8.5, color=dark_rgb)
  495. else: # rect (default)
  496. rect = FancyBboxPatch(
  497. (left, bot), BOX_W, BOX_H,
  498. boxstyle="round,pad=0.04",
  499. facecolor="white",
  500. edgecolor=accent, linewidth=1.2,
  501. )
  502. ax.add_patch(rect)
  503. ax.text(cx, cy, label, ha="center", va="center",
  504. fontsize=8.5, color=dark_rgb)
  505. plt.tight_layout(pad=0.2)
  506. buf = io.BytesIO()
  507. fig.savefig(buf, format="png", dpi=dpi, bbox_inches="tight",
  508. facecolor="white", pad_inches=0.08)
  509. plt.close(fig)
  510. buf.seek(0)
  511. return buf.read()
  512. except Exception:
  513. return None
  514. # ══════════════════════════════════════════════════════════════════════════════
  515. # Block renderers
  516. # ══════════════════════════════════════════════════════════════════════════════
  517. def _add_heading(story: list, item: dict, ctx: dict, level: int):
  518. key = f"h{level}"
  519. para = Paragraph(item["text"], ctx["styles"][key])
  520. if level == 1:
  521. story.append(KeepTogether([para, _divider(ctx["acc"])]))
  522. else:
  523. story.append(para)
  524. def _add_body(story: list, item: dict, ctx: dict):
  525. story.append(Paragraph(item["text"], ctx["styles"]["body"]))
  526. def _add_bullet(story: list, item: dict, ctx: dict):
  527. story.append(Paragraph(
  528. f"\u2022\u2002{item['text']}", ctx["styles"]["bullet"]
  529. ))
  530. def _add_numbered(story: list, item: dict, ctx: dict):
  531. ctx["numbered_n"] += 1
  532. story.append(Paragraph(
  533. f"{ctx['numbered_n']}.\u2002{item['text']}",
  534. ctx["styles"]["numbered"],
  535. ))
  536. def _add_callout(story: list, item: dict, ctx: dict):
  537. story.append(Spacer(1, 8))
  538. story.append(CalloutBox(
  539. item["text"], ctx["styles"]["callout"], ctx["acc"], ctx["acc_lt"]
  540. ))
  541. story.append(Spacer(1, 8))
  542. def _add_table(story: list, item: dict, ctx: dict):
  543. t = ctx["tokens"]
  544. styles = ctx["styles"]
  545. usable_w = ctx["usable_w"]
  546. acc = ctx["acc"]
  547. acc_lt = ctx["acc_lt"]
  548. headers = [Paragraph(h, styles["table_header"]) for h in item["headers"]]
  549. rows = [
  550. [Paragraph(str(c), styles["table_cell"]) for c in row]
  551. for row in item.get("rows", [])
  552. ]
  553. n_cols = len(item["headers"])
  554. # Optional col_widths as fractions summing to 1.0
  555. if "col_widths" in item and len(item["col_widths"]) == n_cols:
  556. col_w = [usable_w * f for f in item["col_widths"]]
  557. else:
  558. col_w = [usable_w / n_cols] * n_cols
  559. tbl = Table([headers] + rows, colWidths=col_w)
  560. tbl.setStyle(TableStyle([
  561. ("BACKGROUND", (0, 0), (-1, 0), HexColor(acc)),
  562. ("TEXTCOLOR", (0, 0), (-1, 0), HexColor("#FFFFFF")),
  563. ("FONTNAME", (0, 0), (-1, 0), t["font_body_b_rl"]),
  564. ("FONTSIZE", (0, 0), (-1, 0), 9.5),
  565. ("TOPPADDING", (0, 0), (-1, 0), 7),
  566. ("BOTTOMPADDING", (0, 0), (-1, 0), 7),
  567. ("ROWBACKGROUNDS", (0, 1), (-1, -1),
  568. [HexColor("#FFFFFF"), HexColor(acc_lt)]),
  569. ("FONTNAME", (0, 1), (-1, -1), t["font_body_rl"]),
  570. ("FONTSIZE", (0, 1), (-1, -1), 9.5),
  571. ("TOPPADDING", (0, 1), (-1, -1), 6),
  572. ("BOTTOMPADDING", (0, 1), (-1, -1), 6),
  573. ("LEFTPADDING", (0, 0), (-1, -1), 10),
  574. ("RIGHTPADDING", (0, 0), (-1, -1), 10),
  575. ("BOX", (0, 0), (-1, -1), 0.5, HexColor("#CCCCCC")),
  576. ("LINEBELOW", (0, 0), (-1, 0), 1.2, HexColor(acc)),
  577. ("TEXTCOLOR", (0, 1), (-1, -1), HexColor(t["body_text"])),
  578. ("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
  579. ]))
  580. story.append(tbl)
  581. if item.get("caption"):
  582. story.append(Spacer(1, 4))
  583. story.append(Paragraph(item["caption"], styles["caption"]))
  584. story.append(Spacer(1, 12))
  585. def _add_image(story: list, item: dict, ctx: dict):
  586. path = str(item.get("path", item.get("src", "")))
  587. if not os.path.exists(path):
  588. story.append(Paragraph(
  589. f"[Image not found: {path}]", ctx["styles"]["caption"]
  590. ))
  591. return
  592. try:
  593. img = RLImage(path)
  594. uw = ctx["usable_w"]
  595. if img.drawWidth > uw:
  596. scale = uw / img.drawWidth
  597. img.drawWidth = uw
  598. img.drawHeight = img.drawHeight * scale
  599. story.append(img)
  600. except Exception as e:
  601. story.append(Paragraph(f"[Image error: {e}]", ctx["styles"]["caption"]))
  602. return
  603. if item.get("caption"):
  604. story.append(Spacer(1, 4))
  605. story.append(Paragraph(item["caption"], ctx["styles"]["caption"]))
  606. story.append(Spacer(1, 8))
  607. def _add_figure(story: list, item: dict, ctx: dict):
  608. """Like image but auto-numbers the caption as 'Figure N: ...'."""
  609. ctx["figure_n"] += 1
  610. raw_cap = item.get("caption", "")
  611. caption = f"Figure {ctx['figure_n']}: {raw_cap}" if raw_cap \
  612. else f"Figure {ctx['figure_n']}"
  613. _add_image(story, {**item, "caption": caption}, ctx)
  614. def _add_code(story: list, item: dict, ctx: dict):
  615. acc = ctx["acc"]
  616. acc_lt = ctx["acc_lt"]
  617. mu = ctx["mu"]
  618. uw = ctx["usable_w"]
  619. lang = item.get("language", "")
  620. pre = Preformatted(item.get("text", ""), ctx["styles"]["code"])
  621. tbl = Table([[pre]], colWidths=[uw])
  622. tbl.setStyle(TableStyle([
  623. ("BACKGROUND", (0, 0), (-1, -1), HexColor(acc_lt)),
  624. ("LINEBEFORE", (0, 0), ( 0, -1), 3, HexColor(acc)),
  625. ("BOX", (0, 0), (-1, -1), 0.5, HexColor(mu)),
  626. ("LEFTPADDING", (0, 0), (-1, -1), 14),
  627. ("RIGHTPADDING", (0, 0), (-1, -1), 10),
  628. ("TOPPADDING", (0, 0), (-1, -1), 8),
  629. ("BOTTOMPADDING", (0, 0), (-1, -1), 8),
  630. ]))
  631. story.append(Spacer(1, 6))
  632. if lang:
  633. story.append(Paragraph(lang.upper(), ctx["styles"]["code_lang"]))
  634. story.append(tbl)
  635. story.append(Spacer(1, 6))
  636. def _add_math(story: list, item: dict, ctx: dict):
  637. """
  638. Display math block.
  639. """
  640. acc = ctx["acc"]
  641. acc_lt = ctx["acc_lt"]
  642. uw = ctx["usable_w"]
  643. expr = item.get("text", "").strip()
  644. label = item.get("label", "").strip()
  645. png = _render_math_png(expr)
  646. if png is None:
  647. # Graceful text fallback if matplotlib unavailable
  648. story.append(Spacer(1, 6))
  649. pre = Preformatted(f" {expr}", ctx["styles"]["math_fallback"])
  650. tbl = Table([[pre]], colWidths=[uw])
  651. tbl.setStyle(TableStyle([
  652. ("BACKGROUND", (0, 0), (-1, -1), HexColor(acc_lt)),
  653. ("LEFTPADDING", (0, 0), (-1, -1), 14),
  654. ("RIGHTPADDING", (0, 0), (-1, -1), 14),
  655. ("TOPPADDING", (0, 0), (-1, -1), 8),
  656. ("BOTTOMPADDING", (0, 0), (-1, -1), 8),
  657. ]))
  658. story.append(tbl)
  659. story.append(Spacer(1, 6))
  660. return
  661. img = _image_from_bytes(png, uw, max_frac=0.72)
  662. story.append(Spacer(1, 10))
  663. if label:
  664. label_w = 44
  665. formula_w = uw - label_w
  666. lbl_para = Paragraph(label, ctx["styles"]["eq_label"])
  667. row_tbl = Table([[img, lbl_para]], colWidths=[formula_w, label_w])
  668. row_tbl.setStyle(TableStyle([
  669. ("ALIGN", (0, 0), (0, 0), "CENTER"),
  670. ("ALIGN", (1, 0), (1, 0), "RIGHT"),
  671. ("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
  672. ]))
  673. story.append(row_tbl)
  674. else:
  675. row_tbl = Table([[img]], colWidths=[uw])
  676. row_tbl.setStyle(TableStyle([
  677. ("ALIGN", (0, 0), (-1, -1), "CENTER"),
  678. ]))
  679. story.append(row_tbl)
  680. if item.get("caption"):
  681. story.append(Spacer(1, 4))
  682. story.append(Paragraph(item["caption"], ctx["styles"]["caption"]))
  683. story.append(Spacer(1, 10))
  684. def _add_chart(story: list, item: dict, ctx: dict):
  685. """
  686. Render a chart (bar / line / pie) via matplotlib.
  687. """
  688. uw = ctx["usable_w"]
  689. png = _render_chart_png(item, ctx["acc"])
  690. if png is None:
  691. story.append(Paragraph(
  692. "[Chart: install matplotlib to render — pip install matplotlib]",
  693. ctx["styles"]["caption"],
  694. ))
  695. return
  696. img = _image_from_bytes(png, uw, max_frac=0.95)
  697. story.append(Spacer(1, 8))
  698. row_tbl = Table([[img]], colWidths=[uw])
  699. row_tbl.setStyle(TableStyle([("ALIGN", (0, 0), (-1, -1), "CENTER")]))
  700. story.append(row_tbl)
  701. raw_cap = item.get("caption", "")
  702. use_fig = item.get("figure", True)
  703. if raw_cap or use_fig:
  704. ctx["figure_n"] += 1
  705. prefix = f"Figure {ctx['figure_n']}: " if use_fig else ""
  706. story.append(Spacer(1, 4))
  707. story.append(Paragraph(prefix + raw_cap, ctx["styles"]["caption"]))
  708. story.append(Spacer(1, 10))
  709. def _add_flowchart(story: list, item: dict, ctx: dict):
  710. """
  711. Render a flowchart via matplotlib.
  712. """
  713. uw = ctx["usable_w"]
  714. png = _render_flowchart_png(item, ctx["acc"], ctx["dark"], ctx["mu"])
  715. if png is None:
  716. story.append(Paragraph(
  717. "[Flowchart: install matplotlib to render — pip install matplotlib]",
  718. ctx["styles"]["caption"],
  719. ))
  720. return
  721. img = _image_from_bytes(png, uw, max_frac=0.78)
  722. story.append(Spacer(1, 8))
  723. row_tbl = Table([[img]], colWidths=[uw])
  724. row_tbl.setStyle(TableStyle([("ALIGN", (0, 0), (-1, -1), "CENTER")]))
  725. story.append(row_tbl)
  726. raw_cap = item.get("caption", "")
  727. use_fig = item.get("figure", True)
  728. if raw_cap or use_fig:
  729. ctx["figure_n"] += 1
  730. prefix = f"Figure {ctx['figure_n']}: " if use_fig else ""
  731. story.append(Spacer(1, 4))
  732. story.append(Paragraph(prefix + raw_cap, ctx["styles"]["caption"]))
  733. story.append(Spacer(1, 10))
  734. def _add_bibliography(story: list, item: dict, ctx: dict):
  735. """
  736. Numbered reference list with hanging indent.
  737. """
  738. heading = item.get("title", "References")
  739. if heading:
  740. story.append(KeepTogether([
  741. Paragraph(heading, ctx["styles"]["bib_title"]),
  742. _divider(ctx["acc"]),
  743. ]))
  744. for ref in item.get("items", []):
  745. story.append(Spacer(1, 4))
  746. story.append(BibliographyItem(
  747. str(ref.get("id", "")),
  748. ref.get("text", ""),
  749. ctx["styles"]["bib"],
  750. ctx["dark"],
  751. ))
  752. # ══════════════════════════════════════════════════════════════════════════════
  753. # Story builder
  754. # ══════════════════════════════════════════════════════════════════════════════
  755. # Block types that break a numbered list sequence
  756. _RESETS_NUMBERED = frozenset({
  757. "h1", "h2", "h3", "body", "bullet", "callout", "table",
  758. "image", "figure", "code", "math", "chart", "flowchart",
  759. "bibliography", "divider", "caption", "pagebreak", "spacer",
  760. })
  761. def build_story(content: list, tokens: dict, styles: dict) -> list:
  762. usable_w = A4[0] - tokens["margin_left"] - tokens["margin_right"]
  763. ctx: dict = {
  764. "tokens": tokens,
  765. "styles": styles,
  766. "usable_w": usable_w,
  767. "acc": tokens["accent"],
  768. "acc_lt": tokens["accent_lt"],
  769. "mu": tokens["muted"],
  770. "dark": tokens["dark"],
  771. "figure_n": 0,
  772. "numbered_n": 0,
  773. }
  774. story: list = []
  775. for item in content:
  776. kind = item.get("type", "body")
  777. if kind in _RESETS_NUMBERED:
  778. ctx["numbered_n"] = 0
  779. if kind == "h1": _add_heading(story, item, ctx, 1)
  780. elif kind == "h2": _add_heading(story, item, ctx, 2)
  781. elif kind == "h3": _add_heading(story, item, ctx, 3)
  782. elif kind == "body": _add_body(story, item, ctx)
  783. elif kind == "bullet": _add_bullet(story, item, ctx)
  784. elif kind == "numbered": _add_numbered(story, item, ctx)
  785. elif kind == "callout": _add_callout(story, item, ctx)
  786. elif kind == "table": _add_table(story, item, ctx)
  787. elif kind == "image": _add_image(story, item, ctx)
  788. elif kind == "figure": _add_figure(story, item, ctx)
  789. elif kind == "code": _add_code(story, item, ctx)
  790. elif kind == "math": _add_math(story, item, ctx)
  791. elif kind == "chart": _add_chart(story, item, ctx)
  792. elif kind == "flowchart": _add_flowchart(story, item, ctx)
  793. elif kind == "bibliography": _add_bibliography(story, item, ctx)
  794. elif kind == "divider": story.append(_divider(ctx["acc"]))
  795. elif kind == "caption":
  796. story.append(Paragraph(item["text"], styles["caption"]))
  797. elif kind == "pagebreak": story.append(PageBreak())
  798. elif kind == "spacer": story.append(Spacer(1, item.get("pt", 12)))
  799. return story
  800. # ══════════════════════════════════════════════════════════════════════════════
  801. # Main build
  802. # ══════════════════════════════════════════════════════════════════════════════
  803. def build(tokens: dict, content: list, out_path: str) -> dict:
  804. register_fonts(tokens)
  805. styles = make_styles(tokens)
  806. doc = BeautifulDoc(
  807. out_path, tokens,
  808. pagesize=A4,
  809. leftMargin=tokens["margin_left"],
  810. rightMargin=tokens["margin_right"],
  811. topMargin=tokens["margin_top"],
  812. bottomMargin=tokens["margin_bottom"],
  813. )
  814. doc.build(build_story(content, tokens, styles))
  815. size = os.path.getsize(out_path)
  816. return {"status": "ok", "out": out_path, "size_kb": size // 1024}
  817. # ══════════════════════════════════════════════════════════════════════════════
  818. # CLI
  819. # ══════════════════════════════════════════════════════════════════════════════
  820. def main():
  821. parser = argparse.ArgumentParser(
  822. description="Render body PDF from tokens.json + content.json"
  823. )
  824. parser.add_argument("--tokens", default="tokens.json")
  825. parser.add_argument("--content", default="content.json")
  826. parser.add_argument("--out", default="body.pdf")
  827. args = parser.parse_args()
  828. for fpath in (args.tokens, args.content):
  829. if not os.path.exists(fpath):
  830. print(
  831. json.dumps({"status": "error",
  832. "error": f"File not found: {fpath}"}),
  833. file=sys.stderr,
  834. )
  835. sys.exit(1)
  836. with open(args.tokens, encoding="utf-8") as f:
  837. tokens = json.load(f)
  838. with open(args.content, encoding="utf-8") as f:
  839. content = json.load(f)
  840. try:
  841. result = build(tokens, content, args.out)
  842. print(json.dumps(result))
  843. except Exception as e:
  844. import traceback
  845. print(
  846. json.dumps({
  847. "status": "error",
  848. "error": str(e),
  849. "trace": traceback.format_exc(),
  850. }),
  851. file=sys.stderr,
  852. )
  853. sys.exit(3)
  854. if __name__ == "__main__":
  855. main()