zcode-app-cli 3.8.1-15 → 3.9.1-16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/bin/zcode.js +92 -47
  2. package/package.json +2 -2
  3. package/vendor/extraction.json +131 -5
  4. package/vendor/node_modules/@zcode/tui/dist/index.js +35 -4
  5. package/vendor/packages/browser-use-plugin/.zcode-plugin/plugin.json +1 -1
  6. package/vendor/packages/browser-use-plugin/dist/mcp/server.js +12880 -10408
  7. package/vendor/packages/browser-use-plugin/package.json +1 -1
  8. package/vendor/packages/document-skills-plugin/.mcp.json +13 -0
  9. package/vendor/packages/document-skills-plugin/.zcode-plugin/plugin.json +19 -6
  10. package/vendor/packages/document-skills-plugin/package.json +3 -3
  11. package/vendor/packages/document-skills-plugin/skills/docx/SKILL.md +6 -2
  12. package/vendor/packages/document-skills-plugin/skills/docx/env_setup/env_check.sh +0 -0
  13. package/vendor/packages/document-skills-plugin/skills/docx/env_setup/setup_mac_linux.sh +0 -0
  14. package/vendor/packages/document-skills-plugin/skills/docx/setup.sh +0 -0
  15. package/vendor/packages/document-skills-plugin/skills/pdf/SKILL.md +39 -20
  16. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/academic.md +39 -47
  17. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/creative.md +11 -3
  18. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/poster.md +3 -0
  19. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/process.md +3 -0
  20. package/vendor/packages/document-skills-plugin/skills/pdf/briefs/report.md +41 -62
  21. package/vendor/packages/document-skills-plugin/skills/pdf/env_setup/env_check.sh +23 -15
  22. package/vendor/packages/document-skills-plugin/skills/pdf/env_setup/setup.md +37 -19
  23. package/vendor/packages/document-skills-plugin/skills/pdf/env_setup/setup_mac_linux.sh +0 -0
  24. package/vendor/packages/document-skills-plugin/skills/pdf/env_setup/setup_windows.ps1 +17 -19
  25. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/cover_render.py +487 -0
  26. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/html2poster.js +43 -11
  27. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/pdf.py +145 -50
  28. package/vendor/packages/document-skills-plugin/skills/pdf/scripts/setup.sh +23 -21
  29. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover-backgrounds.md +1 -1
  30. package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover.md +139 -1347
  31. package/vendor/packages/document-skills-plugin/skills/pptx/LICENSE.txt +13 -0
  32. package/vendor/packages/document-skills-plugin/skills/pptx/SKILL.md +146 -179
  33. package/vendor/packages/document-skills-plugin/skills/xlsx/SKILL.md +6 -2
  34. package/vendor/packages/document-skills-plugin/skills/xlsx/env_setup/env_check.sh +0 -0
  35. package/vendor/packages/document-skills-plugin/skills/xlsx/env_setup/setup_mac_linux.sh +0 -0
  36. package/vendor/packages/document-skills-plugin/skills/xlsx/setup.sh +0 -0
  37. package/vendor/packages/zcode-cua-plugin/.zcode-plugin/plugin.json +19 -0
  38. package/vendor/packages/zcode-cua-plugin/dist/mcp/server.js +68498 -0
  39. package/vendor/packages/zcode-cua-plugin/node_modules/@img/colour/color.cjs +1596 -0
  40. package/vendor/packages/zcode-cua-plugin/node_modules/@img/colour/index.cjs +1 -0
  41. package/vendor/packages/zcode-cua-plugin/node_modules/@img/colour/package.json +58 -0
  42. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-libvips-linux-x64/lib/glib-2.0/include/glibconfig.h +221 -0
  43. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-libvips-linux-x64/lib/index.js +1 -0
  44. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-libvips-linux-x64/lib/libvips-cpp.so.8.17.3 +0 -0
  45. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-libvips-linux-x64/package.json +42 -0
  46. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-libvips-linux-x64/versions.json +30 -0
  47. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-linux-x64/LICENSE +191 -0
  48. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-linux-x64/lib/sharp-linux-x64.node +0 -0
  49. package/vendor/packages/zcode-cua-plugin/node_modules/@img/sharp-linux-x64/package.json +46 -0
  50. package/vendor/packages/zcode-cua-plugin/node_modules/detect-libc/LICENSE +201 -0
  51. package/vendor/packages/zcode-cua-plugin/node_modules/detect-libc/lib/detect-libc.js +313 -0
  52. package/vendor/packages/zcode-cua-plugin/node_modules/detect-libc/lib/elf.js +39 -0
  53. package/vendor/packages/zcode-cua-plugin/node_modules/detect-libc/lib/filesystem.js +51 -0
  54. package/vendor/packages/zcode-cua-plugin/node_modules/detect-libc/lib/process.js +24 -0
  55. package/vendor/packages/zcode-cua-plugin/node_modules/detect-libc/package.json +44 -0
  56. package/vendor/packages/zcode-cua-plugin/node_modules/semver/LICENSE +15 -0
  57. package/vendor/packages/zcode-cua-plugin/node_modules/semver/bin/semver.js +191 -0
  58. package/vendor/packages/zcode-cua-plugin/node_modules/semver/classes/comparator.js +143 -0
  59. package/vendor/packages/zcode-cua-plugin/node_modules/semver/classes/index.js +7 -0
  60. package/vendor/packages/zcode-cua-plugin/node_modules/semver/classes/range.js +557 -0
  61. package/vendor/packages/zcode-cua-plugin/node_modules/semver/classes/semver.js +333 -0
  62. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/clean.js +8 -0
  63. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/cmp.js +54 -0
  64. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/coerce.js +62 -0
  65. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/compare-build.js +9 -0
  66. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/compare-loose.js +5 -0
  67. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/compare.js +7 -0
  68. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/diff.js +60 -0
  69. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/eq.js +5 -0
  70. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/gt.js +5 -0
  71. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/gte.js +5 -0
  72. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/inc.js +21 -0
  73. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/lt.js +5 -0
  74. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/lte.js +5 -0
  75. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/major.js +5 -0
  76. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/minor.js +5 -0
  77. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/neq.js +5 -0
  78. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/parse.js +18 -0
  79. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/patch.js +5 -0
  80. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/prerelease.js +8 -0
  81. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/rcompare.js +5 -0
  82. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/rsort.js +5 -0
  83. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/satisfies.js +12 -0
  84. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/sort.js +5 -0
  85. package/vendor/packages/zcode-cua-plugin/node_modules/semver/functions/valid.js +8 -0
  86. package/vendor/packages/zcode-cua-plugin/node_modules/semver/index.js +91 -0
  87. package/vendor/packages/zcode-cua-plugin/node_modules/semver/internal/constants.js +37 -0
  88. package/vendor/packages/zcode-cua-plugin/node_modules/semver/internal/debug.js +11 -0
  89. package/vendor/packages/zcode-cua-plugin/node_modules/semver/internal/identifiers.js +29 -0
  90. package/vendor/packages/zcode-cua-plugin/node_modules/semver/internal/lrucache.js +42 -0
  91. package/vendor/packages/zcode-cua-plugin/node_modules/semver/internal/parse-options.js +17 -0
  92. package/vendor/packages/zcode-cua-plugin/node_modules/semver/internal/re.js +223 -0
  93. package/vendor/packages/zcode-cua-plugin/node_modules/semver/package.json +78 -0
  94. package/vendor/packages/zcode-cua-plugin/node_modules/semver/preload.js +4 -0
  95. package/vendor/packages/zcode-cua-plugin/node_modules/semver/range.bnf +16 -0
  96. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/gtr.js +6 -0
  97. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/intersects.js +9 -0
  98. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/ltr.js +6 -0
  99. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/max-satisfying.js +27 -0
  100. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/min-satisfying.js +26 -0
  101. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/min-version.js +63 -0
  102. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/outside.js +82 -0
  103. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/simplify.js +49 -0
  104. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/subset.js +249 -0
  105. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/to-comparators.js +10 -0
  106. package/vendor/packages/zcode-cua-plugin/node_modules/semver/ranges/valid.js +13 -0
  107. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/LICENSE +191 -0
  108. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/install/build.js +38 -0
  109. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/install/check.js +14 -0
  110. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/channel.js +177 -0
  111. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/colour.js +195 -0
  112. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/composite.js +212 -0
  113. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/constructor.js +499 -0
  114. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/index.js +16 -0
  115. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/input.js +809 -0
  116. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/is.js +143 -0
  117. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/libvips.js +207 -0
  118. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/operation.js +1016 -0
  119. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/output.js +1666 -0
  120. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/resize.js +595 -0
  121. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/sharp.js +121 -0
  122. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/lib/utility.js +291 -0
  123. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/package.json +202 -0
  124. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/binding.gyp +298 -0
  125. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/common.cc +1130 -0
  126. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/common.h +402 -0
  127. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/metadata.cc +346 -0
  128. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/metadata.h +90 -0
  129. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/operations.cc +499 -0
  130. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/operations.h +137 -0
  131. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/pipeline.cc +1814 -0
  132. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/pipeline.h +408 -0
  133. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/sharp.cc +43 -0
  134. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/stats.cc +186 -0
  135. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/stats.h +62 -0
  136. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/utilities.cc +288 -0
  137. package/vendor/packages/zcode-cua-plugin/node_modules/sharp/src/utilities.h +22 -0
  138. package/vendor/packages/zcode-cua-plugin/package.json +31 -0
  139. package/vendor/packages/zcode-cua-plugin/scripts/build-mcp.mjs +58 -0
  140. package/vendor/packages/zcode-cua-plugin/scripts/bump-zcode-cua-producer.mjs +291 -0
  141. package/vendor/packages/zcode-cua-plugin/scripts/check-cua-baseline.mjs +155 -0
  142. package/vendor/packages/zcode-cua-plugin/scripts/check-version-coherence.mjs +478 -0
  143. package/vendor/packages/zcode-cua-plugin/scripts/sync-cache.mjs +100 -0
  144. package/vendor/packages/zcode-cua-plugin/scripts/sync-skill-from-zcode-cua.mjs +115 -0
  145. package/vendor/packages/zcode-cua-plugin/skills/zcode-computer-use/SKILL.md +129 -0
  146. package/vendor/zcode.cjs +752 -743
  147. package/zcode-runtime.lock.json +3 -3
  148. package/vendor/packages/document-skills-plugin/skills/pptx/assets/font_list.txt +0 -80
  149. package/vendor/packages/document-skills-plugin/skills/pptx/scripts/general_judge.py +0 -529
@@ -0,0 +1,487 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ cover_render.py — ReportLab cover generator for the PDF skill.
4
+
5
+ Renders a single-page A4 cover PDF (to be merged as page 1 of the body PDF via
6
+ pypdf). Pure ReportLab — no HTML/Playwright/Chromium required.
7
+
8
+ Nine templates (renumbered 01-09):
9
+ 01 HUD Data Terminal (light) — report/tech
10
+ 02 Corporate Editorial (light) — annual/financial
11
+ 03 Monolith (light) — white paper/proposal/gov
12
+ 04 Museum Minimal (light) — portfolio/gallery/luxury
13
+ 05 Solid Sidebar (light) — institutional/bidding/legal
14
+ 06 Academic Vertical (dark) — arXiv/preprint/tech report
15
+ 07 Academic Symmetric (dark) — IEEE/ACM/thesis
16
+ 08 Academic Journal (dark) — CJK journal/thesis w/ keywords
17
+ 09 Institutional (white + black frame) — thesis proposal/gov
18
+
19
+ Fonts: auto-detected from the host machine, chosen by cover style
20
+ (sans for 01-05, serif for 06-09; kai optional). CFF/PostScript-outline fonts
21
+ (PingFang, Hiragino, Noto CJK OTC, Source Han) are skipped — ReportLab's TTFont
22
+ cannot embed them. Missing heavy weights are simulated with stroke faux-bold.
23
+
24
+ CLI:
25
+ python3 cover_render.py <template 01-09> --out cover.pdf [--content content.json]
26
+ python3 cover_render.py --demo # renders all 9 with sample content
27
+ """
28
+ import os, sys, json, argparse, subprocess
29
+ from reportlab.pdfgen import canvas
30
+ from reportlab.lib.colors import HexColor, Color
31
+ from reportlab.pdfbase import pdfmetrics
32
+ from reportlab.pdfbase.ttfonts import TTFont
33
+ from reportlab.pdfbase.pdfmetrics import stringWidth
34
+ from reportlab.platypus import Paragraph
35
+ from reportlab.lib.styles import ParagraphStyle
36
+ from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER
37
+
38
+ # ── Page geometry (A4 pt); HTML design canvas was 794x1123 px ──
39
+ W, H = 595.28, 841.89
40
+ PXW, PXH = 794.0, 1123.0
41
+ def X(px): return px * W / PXW
42
+ def LW(px): return px * W / PXW
43
+ def VH(px): return px * H / PXH
44
+ def YT(px): return H - px * H / PXH
45
+ def FSpx(px): return px * W / PXW
46
+
47
+ # ── Default palette (override via palette dict) ──
48
+ DEFAULT_PALETTE = {
49
+ "primary": "#1e3a5f", "secondary": "#2d5f8a", "text": "#1a1a2e",
50
+ "muted": "#6b7280", "bg": "#ffffff",
51
+ # academic dark
52
+ "ac_bg": "#162032", "ac_accent": "#4A90C4", "ac_gold": "#8B7E5A",
53
+ "ac_text": "#FFFFFF", "ac_muted": "#90A8C0", "ac_foot": "#607080",
54
+ }
55
+
56
+ # ═══════════════════════════════════════════════════════════════════
57
+ # Font detection — pick embeddable (TrueType-outline) CJK fonts by role
58
+ # ═══════════════════════════════════════════════════════════════════
59
+ _FONT_CANDIDATES = {
60
+ # role: [(regname, [path candidates], subfontIndex)]
61
+ "sans": [
62
+ ("SimHei", ["/usr/share/fonts/truetype/chinese/SimHei.ttf",
63
+ "~/Library/Fonts/SimHei.ttf", "C:/Windows/Fonts/simhei.ttf"], None),
64
+ ("MSYaHei", ["/usr/share/fonts/truetype/chinese/msyh.ttf",
65
+ "/usr/share/fonts/truetype/chinese/msyh.ttc",
66
+ "C:/Windows/Fonts/msyh.ttc", "C:/Windows/Fonts/msyh.ttf"], 0),
67
+ ("HeitiSC", ["/System/Library/Fonts/STHeiti Medium.ttc"], 1),
68
+ ("WenQuanYiZenHei", ["/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc"], 0),
69
+ ("ArialUnicode", ["/Library/Fonts/Arial Unicode.ttf",
70
+ "/System/Library/Fonts/Supplemental/Arial Unicode.ttf"], None),
71
+ ],
72
+ "serif": [
73
+ ("SongtiSC", ["/System/Library/Fonts/Supplemental/Songti.ttc"], 0), # Black
74
+ ("SimSun", ["/usr/share/fonts/truetype/chinese/simsun.ttc",
75
+ "~/Library/Fonts/SimSun.ttf", "C:/Windows/Fonts/simsun.ttc"], 0),
76
+ ("STSong", ["/System/Library/Fonts/Supplemental/Songti.ttc"], 4),
77
+ ("ARPLUMing", ["/usr/share/fonts/truetype/arphic/uming.ttc"], 0),
78
+ ],
79
+ "kai": [
80
+ ("KaiTi", ["/usr/share/fonts/truetype/chinese/simkai.ttf",
81
+ "~/Library/Fonts/SimKai.ttf", "C:/Windows/Fonts/simkai.ttf"], None),
82
+ ("KaitiSC", ["/System/Library/Fonts/Supplemental/Kaiti.ttc"], 2),
83
+ ],
84
+ "latin": [
85
+ ("TNR", ["/usr/share/fonts/truetype/english/Times-New-Roman.ttf",
86
+ "~/Library/Fonts/Times-New-Roman.ttf",
87
+ "/System/Library/Fonts/Supplemental/Times New Roman.ttf",
88
+ "C:/Windows/Fonts/times.ttf"], None),
89
+ ],
90
+ }
91
+
92
+ def _try_register(name, path, idx):
93
+ path = os.path.expanduser(path)
94
+ if not os.path.exists(path):
95
+ return False
96
+ try:
97
+ pdfmetrics.registerFont(TTFont(name, path) if idx is None
98
+ else TTFont(name, path, subfontIndex=idx))
99
+ return True # succeeds only for embeddable TrueType-outline fonts
100
+ except Exception:
101
+ return False # CFF/PostScript-outline (PingFang/Hiragino/Noto CJK) land here
102
+
103
+ def _fc_fallback(role):
104
+ """Last resort: query fontconfig for a zh font and try to embed it."""
105
+ try:
106
+ pat = ":lang=zh:spacing=100" if role == "sans" else ":lang=zh"
107
+ out = subprocess.run(["fc-list", pat, "file"], capture_output=True,
108
+ text=True, timeout=5).stdout
109
+ except Exception:
110
+ return None
111
+ for line in out.splitlines():
112
+ p = line.split(":")[0].strip()
113
+ if p.lower().endswith((".ttf", ".ttc")):
114
+ nm = "FC_%s_%d" % (role, abs(hash(p)) % 10000)
115
+ if _try_register(nm, p, 0):
116
+ return nm
117
+ return None
118
+
119
+ def detect_fonts(overrides=None):
120
+ """Return {sans, serif, kai, latin} of registered, embeddable font names.
121
+ `overrides` may map any role -> (regname, path, idx) to force a choice."""
122
+ picks = {}
123
+ overrides = overrides or {}
124
+ for role, cands in _FONT_CANDIDATES.items():
125
+ chosen = None
126
+ if role in overrides:
127
+ nm, p, i = overrides[role]
128
+ if _try_register(nm, p, i):
129
+ chosen = nm
130
+ if not chosen:
131
+ for nm, paths, idx in cands:
132
+ for p in paths:
133
+ if _try_register("F_%s_%s" % (role, nm), p, idx):
134
+ chosen = "F_%s_%s" % (role, nm); break
135
+ if chosen: break
136
+ if not chosen and role in ("sans", "serif"):
137
+ chosen = _fc_fallback(role)
138
+ picks[role] = chosen
139
+ # graceful fallbacks
140
+ if not picks.get("sans"): picks["sans"] = picks.get("serif")
141
+ if not picks.get("serif"): picks["serif"] = picks.get("sans")
142
+ if not picks.get("latin"): picks["latin"] = picks.get("sans")
143
+ if not picks.get("kai"): picks["kai"] = picks.get("serif") or picks.get("sans")
144
+ if not picks.get("sans"):
145
+ raise RuntimeError("No embeddable CJK font found on this machine. "
146
+ "Install one of: SimHei, Microsoft YaHei, Songti/SimSun, "
147
+ "Heiti SC, WenQuanYi Zen Hei.")
148
+ return picks
149
+
150
+ # ═══════════════════════════════════════════════════════════════════
151
+ # primitives
152
+ # ═══════════════════════════════════════════════════════════════════
153
+ def _c(hexv): return HexColor(hexv)
154
+
155
+ def fill_rect(c, x_px, top_px, w_px, h_px, color, alpha=1.0):
156
+ c.saveState(); c.setFillColor(color); c.setFillAlpha(alpha)
157
+ c.rect(X(x_px), YT(top_px) - VH(h_px), LW(w_px), VH(h_px), stroke=0, fill=1)
158
+ c.restoreState()
159
+
160
+ def hline(c, x_px, top_px, w_px, thick_px, color, alpha=1.0):
161
+ c.saveState(); c.setStrokeColor(color); c.setStrokeAlpha(alpha); c.setLineWidth(LW(thick_px))
162
+ c.line(X(x_px), YT(top_px), X(x_px + w_px), YT(top_px)); c.restoreState()
163
+
164
+ def vline(c, x_px, top_px, h_px, thick_px, color, alpha=1.0):
165
+ c.saveState(); c.setStrokeColor(color); c.setStrokeAlpha(alpha); c.setLineWidth(LW(thick_px))
166
+ c.line(X(x_px), YT(top_px), X(x_px), YT(top_px + h_px)); c.restoreState()
167
+
168
+ def _style(font, size, color, leading=None, align=TA_LEFT):
169
+ return ParagraphStyle('s', fontName=font, fontSize=size, leading=leading or size * 1.2,
170
+ textColor=color, alignment=align, wordWrap='CJK')
171
+
172
+ def para(c, text, x_px, top_px, w_px, font, size, color, line_h=1.2, align=TA_LEFT, fauxbold=0.0):
173
+ st = _style(font, size, color, leading=size * line_h, align=align)
174
+ w = LW(w_px); x = X(x_px)
175
+ p = Paragraph(text, st); _, h = p.wrap(w, H); y = YT(top_px) - h
176
+ if fauxbold > 0:
177
+ for dx, dy in [(0, 0), (fauxbold, 0), (0, fauxbold), (fauxbold, fauxbold), (fauxbold / 2, fauxbold / 2)]:
178
+ pp = Paragraph(text, st); pp.wrap(w, H); pp.drawOn(c, x + dx, y - dy)
179
+ else:
180
+ p.drawOn(c, x, y)
181
+ return h
182
+
183
+ def line_text(c, text, x_px, top_px, font, size, color, ls_px=0.0, align=TA_LEFT,
184
+ right_px=None, ascent=0.82, alpha=1.0):
185
+ c.saveState(); c.setFillColor(color); c.setFillAlpha(alpha)
186
+ ls = LW(ls_px); baseline = YT(top_px) - size * ascent
187
+ if align == TA_RIGHT and right_px is not None:
188
+ w = stringWidth(text, font, size) + ls * max(0, len(text) - 1); x = X(right_px) - w
189
+ else:
190
+ x = X(x_px)
191
+ to = c.beginText(x, baseline); to.setFont(font, size)
192
+ if ls: to.setCharSpace(ls)
193
+ to.textOut(text); c.drawText(to); c.restoreState()
194
+
195
+ def center_line(c, text, top_px, font, size, color, ls_px=0.0, fauxbold=0.0, line_h=1.3):
196
+ ls = LW(ls_px); w = stringWidth(text, font, size) + ls * max(0, len(text) - 1)
197
+ x = W / 2 - w / 2; baseline = YT(top_px) - size * 0.82
198
+ c.saveState(); c.setFillColor(color)
199
+ def draw(dx, dy):
200
+ to = c.beginText(x + dx, baseline - dy); to.setFont(font, size)
201
+ if ls: to.setCharSpace(ls)
202
+ to.textOut(text); c.drawText(to)
203
+ if fauxbold > 0:
204
+ for dx, dy in [(0, 0), (fauxbold, 0), (0, fauxbold), (fauxbold, fauxbold), (fauxbold / 2, fauxbold / 2)]:
205
+ draw(dx, dy)
206
+ else:
207
+ draw(0, 0)
208
+ c.restoreState()
209
+ return size * line_h
210
+
211
+ # ═══════════════════════════════════════════════════════════════════
212
+ # templates (F = detected font names, P = palette colors)
213
+ # ═══════════════════════════════════════════════════════════════════
214
+ def _t01_hud(c, ct, F, P):
215
+ pri = _c(P["primary"]); txt = _c(P["text"]); mut = _c(P["muted"])
216
+ c.saveState(); c.setStrokeColor(pri); c.setStrokeAlpha(0.05); c.setLineWidth(LW(0.5))
217
+ x = 0
218
+ while x <= PXW: c.line(X(x), 0, X(x), H); x += 50
219
+ y = 0
220
+ while y <= PXH: c.line(0, YT(y), W, YT(y)); y += 50
221
+ c.restoreState()
222
+ xc = 0.12 * PXW + 30
223
+ vline(c, 0.12 * PXW, 0.10 * PXH, 0.80 * PXH, 6, pri)
224
+ hline(c, xc, 0.72 * PXH - 12, 0.4 * PXW, 1, pri, alpha=0.4)
225
+ line_text(c, ct["kicker"], xc, 0.15 * PXH, F["sans"], 16, mut, ls_px=3)
226
+ para(c, ct["hero"], xc, 0.28 * PXH, 0.7 * PXW, F["sans"], 66, pri, line_h=1.15, fauxbold=1.4)
227
+ para(c, ct["summary"], xc, 0.48 * PXH, 0.6 * PXW, F["sans"], 17, txt, line_h=1.6)
228
+ para(c, ct["meta"], xc, 0.74 * PXH, 0.7 * PXW, F["sans"], 17, txt, line_h=1.7)
229
+
230
+ def _t02_corporate(c, ct, F, P):
231
+ pri = _c(P["primary"]); txt = _c(P["text"]); mut = _c(P["muted"])
232
+ c.saveState(); c.setFillColor(pri); c.setFillAlpha(0.05)
233
+ yr = ct.get("year", "")
234
+ w = stringWidth(yr, F["sans"], 180)
235
+ c.drawString((W - X(20)) - w, YT(0.15 * PXH) - 180 * 0.80, yr); c.restoreState()
236
+ fill_rect(c, 0, 0, PXW, 15, pri)
237
+ fill_rect(c, 0.88 * PXW, 0.75 * PXH, 4, 0.13 * PXH, pri)
238
+ line_text(c, ct["kicker"], 0.12 * PXW, 0.15 * PXH, F["sans"], 16, mut, ls_px=3)
239
+ para(c, ct["hero"], 0.12 * PXW, 0.15 * PXH + 44, 0.75 * PXW, F["sans"], 60, pri, line_h=1.15, fauxbold=1.3)
240
+ para(c, ct["summary"], 0.12 * PXW, 0.50 * PXH, 0.5 * PXW, F["sans"], 17, txt, line_h=1.6)
241
+ para(c, ct["meta"], 0.15 * PXW, 0.70 * PXH, (0.88 * PXW - 20) - 0.15 * PXW,
242
+ F["sans"], 17, txt, line_h=1.8, align=TA_RIGHT)
243
+
244
+ def _t03_monolith(c, ct, F, P):
245
+ pri = _c(P["primary"]); txt = _c(P["text"]); mut = _c(P["muted"]); xc = 0.12 * PXW
246
+ c.saveState(); c.setFillColor(pri); c.setFillAlpha(0.04)
247
+ c.translate(X(0.85 * PXW), H / 2); c.rotate(-90)
248
+ word = ct.get("word", "REPORT")
249
+ tw = stringWidth(word, F["sans"], 170) + LW(8) * (len(word) - 1)
250
+ to = c.beginText(-tw / 2, -170 * 0.35); to.setFont(F["sans"], 170); to.setCharSpace(LW(8))
251
+ to.textOut(word); c.drawText(to); c.restoreState()
252
+ fill_rect(c, xc, 0.15 * PXH, 50, 5, pri)
253
+ vline(c, xc - 12, 0.70 * PXH, 0.12 * PXH, 2, pri, alpha=0.5)
254
+ line_text(c, ct["kicker"], xc, 0.20 * PXH, F["sans"], 16, mut, ls_px=3)
255
+ para(c, ct["hero"], xc, 0.28 * PXH, 0.7 * PXW, F["sans"], 60, pri, line_h=1.15, fauxbold=1.3)
256
+ para(c, ct["summary"], xc, 0.45 * PXH, 0.55 * PXW, F["sans"], 17, txt, line_h=1.6)
257
+ para(c, ct["meta"], xc, 0.70 * PXH, 0.7 * PXW, F["sans"], 18, txt, line_h=2.0)
258
+ if ct.get("footer"):
259
+ line_text(c, ct["footer"], 0, 0.90 * PXH, F["sans"], 16, mut, ls_px=1,
260
+ align=TA_RIGHT, right_px=0.88 * PXW, alpha=0.85)
261
+
262
+ def _t04_museum(c, ct, F, P):
263
+ pri = _c(P["primary"]); txt = _c(P["text"]); mut = _c(P["muted"])
264
+ M = 0.08 * PXW; arm = 30; lw = 2; op = 0.6
265
+ def mk(hx, hy, vx, vy):
266
+ fill_rect(c, hx, hy, arm, lw, pri, alpha=op); fill_rect(c, vx, vy, lw, arm, pri, alpha=op)
267
+ mk(M, M, M, M); mk(PXW - M - arm, M, PXW - M - lw, M)
268
+ mk(M, PXH - M - lw, M, PXH - M - arm); mk(PXW - M - arm, PXH - M - lw, PXW - M - lw, PXH - M - arm)
269
+ styles = [
270
+ (ct["kicker"], F["sans"], 16, mut, 1.3, 24, 0.84 * PXW, 0.0),
271
+ (ct["hero"], F["sans"], 56, pri, 1.15, 20, 0.84 * PXW, 1.2),
272
+ (ct["summary"], F["sans"], 17, txt, 1.6, 40, 0.6 * PXW, 0.0),
273
+ (ct["meta"], F["sans"], 16, mut, 1.3, 0, 0.84 * PXW, 0.0),
274
+ ]
275
+ paras = []; total = 0
276
+ for tx, fn, sz, col, lh, gap, wpx, fb in styles:
277
+ st = _style(fn, sz, col, leading=sz * lh, align=TA_CENTER)
278
+ p = Paragraph(tx, st); _, hh = p.wrap(LW(wpx), H)
279
+ paras.append((tx, st, hh, gap, wpx, fb)); total += hh + VH(gap)
280
+ y = H / 2 + total / 2
281
+ for tx, st, hh, gap, wpx, fb in paras:
282
+ x0 = (W - LW(wpx)) / 2
283
+ if fb > 0:
284
+ for dx, dy in [(0, 0), (fb, 0), (0, fb), (fb, fb), (fb / 2, fb / 2)]:
285
+ pp = Paragraph(tx, st); pp.wrap(LW(wpx), H); pp.drawOn(c, x0 + dx, y - hh - dy)
286
+ else:
287
+ pp = Paragraph(tx, st); pp.wrap(LW(wpx), H); pp.drawOn(c, x0, y - hh)
288
+ y -= hh + VH(gap)
289
+
290
+ def _t05_sidebar(c, ct, F, P):
291
+ pri = _c(P["primary"]); txt = _c(P["text"]); mut = _c(P["muted"]); white = _c("#ffffff")
292
+ sb = 0.1 * PXW; le = sb + 40
293
+ fill_rect(c, 0, 0, sb, PXH, pri)
294
+ c.saveState(); pth = c.beginPath(); pth.rect(0, 0, X(sb), H); c.clipPath(pth, stroke=0)
295
+ c.setFillColor(white); c.setFillAlpha(0.15)
296
+ c.translate(X(sb / 2), H / 2); c.rotate(-90)
297
+ wm = ct.get("year", ct.get("word", ""))
298
+ tw = stringWidth(wm, F["sans"], FSpx(52)) + LW(14) * (len(wm) - 1)
299
+ to = c.beginText(-tw / 2, -FSpx(52) * 0.35); to.setFont(F["sans"], FSpx(52)); to.setCharSpace(LW(14))
300
+ to.textOut(wm); c.drawText(to); c.restoreState()
301
+ hline(c, le, 0.90 * PXH, 0.90 * PXW - le, 1, pri, alpha=0.3)
302
+ blk_w = 0.90 * PXW - le
303
+ items = [
304
+ (ct["kicker"], F["sans"], 16, mut, 1.3, 18, 0.0),
305
+ (ct["hero"], F["sans"], 58, pri, 1.15, 24, 1.25),
306
+ (ct["summary"], F["sans"], 17, txt, 1.6, 30, 0.0),
307
+ (ct["meta"], F["sans"], 18, txt, 1.8, 0, 0.0),
308
+ ]
309
+ paras = []; total = 0
310
+ for tx, fn, sz, col, lh, gap, fb in items:
311
+ st = _style(fn, sz, col, leading=sz * lh, align=TA_LEFT)
312
+ pp = Paragraph(tx, st); _, hh = pp.wrap(LW(blk_w), H)
313
+ paras.append((tx, st, hh, gap, fb)); total += hh + VH(gap)
314
+ y = H / 2 + total / 2
315
+ for tx, st, hh, gap, fb in paras:
316
+ if fb > 0:
317
+ for dx, dy in [(0, 0), (fb, 0), (0, fb), (fb, fb), (fb / 2, fb / 2)]:
318
+ pp = Paragraph(tx, st); pp.wrap(LW(blk_w), H); pp.drawOn(c, X(le) + dx, y - hh - dy)
319
+ else:
320
+ pp = Paragraph(tx, st); pp.wrap(LW(blk_w), H); pp.drawOn(c, X(le), y - hh)
321
+ y -= hh + VH(gap)
322
+ if ct.get("footer_left"):
323
+ line_text(c, ct["footer_left"], le, 0.90 * PXH - 26, F["sans"], 16, mut, alpha=0.9)
324
+ if ct.get("footer_right"):
325
+ line_text(c, ct["footer_right"], 0, 0.90 * PXH - 26, F["sans"], 16, mut,
326
+ ls_px=1, align=TA_RIGHT, right_px=0.90 * PXW, alpha=0.85)
327
+
328
+ def _dark_bg(c, color): c.setFillColor(color); c.rect(0, 0, W, H, stroke=0, fill=1)
329
+
330
+ def _t06_academic_vertical(c, ct, F, P):
331
+ bg = _c(P["ac_bg"]); gold = _c(P["ac_gold"]); tx = _c(P["ac_text"]); mut = _c(P["ac_muted"]); foot = _c(P["ac_foot"])
332
+ _dark_bg(c, bg)
333
+ vline(c, 57, 76, PXH - 152, 2.5, gold)
334
+ hline(c, 83, PXH - 132, PXW - 83 - 76, 0.5, gold)
335
+ line_text(c, ct.get("label", ""), 83, 132, F["sans"], 9, gold, ls_px=3)
336
+ para(c, ct["title"], 83, 228, 600, F["serif"], 30, tx, line_h=1.3)
337
+ if ct.get("subtitle"): para(c, ct["subtitle"], 83, 560, 520, F["sans"], 12, mut, line_h=1.5)
338
+ para(c, ct.get("authors", ""), 83, 700, 400, F["sans"], 12, tx, line_h=1.4)
339
+ if ct.get("institution"): para(c, ct["institution"], 83, 740, 400, F["sans"], 10, mut, line_h=1.4)
340
+ if ct.get("footer_left"): line_text(c, ct["footer_left"], 83, PXH - 76 - 9, F["sans"], 9, foot)
341
+ if ct.get("footer_right"): line_text(c, ct["footer_right"], 0, PXH - 76 - 9, F["sans"], 9, foot,
342
+ align=TA_RIGHT, right_px=PXW - 76)
343
+
344
+ def _centered_dark(c, ct, F, P, divider_px, keywords):
345
+ bg = _c(P["ac_bg"]); acc = _c(P["ac_accent"]); tx = _c(P["ac_text"]); mut = _c(P["ac_muted"])
346
+ _dark_bg(c, bg)
347
+ hline(c, 114, 114, PXW - 228, 2, acc); hline(c, 114, PXH - 114, PXW - 228, 2, acc)
348
+ inner = PXW - 228
349
+ blocks = [(ct.get("label", ""), F["sans"], 9, acc, 1.2, 40),
350
+ (ct["title"], F["serif"], 30 if keywords else 28, tx, 1.3, 20 if keywords else 24),
351
+ (ct.get("subtitle", ""), F["sans"], 13, mut, 1.5, 40)]
352
+ paras = []; total = 0
353
+ for tx0, fn, sz, col, lh, gap in blocks:
354
+ pp = Paragraph(tx0, _style(fn, sz, col, leading=sz * lh, align=TA_CENTER))
355
+ _, hh = pp.wrap(LW(inner), H); paras.append((pp, hh, gap)); total += hh + VH(gap)
356
+ if keywords:
357
+ tail = ct.get("keywords_lines", []); tail_h = len(tail) * 11 * 1.8
358
+ else:
359
+ tail_h = 12 * 1.2 + VH(12) + 10 * 1.2
360
+ total += VH(0.5) + VH(40) + tail_h
361
+ y = H / 2 + total / 2
362
+ for pp, hh, gap in paras:
363
+ pp.drawOn(c, (W - LW(inner)) / 2, y - hh); y -= hh + VH(gap)
364
+ c.saveState(); c.setStrokeColor(acc); c.setLineWidth(LW(0.5))
365
+ c.line(W / 2 - LW(divider_px) / 2, y, W / 2 + LW(divider_px) / 2, y); c.restoreState()
366
+ y -= VH(40)
367
+ if keywords:
368
+ for ln in ct.get("keywords_lines", []):
369
+ pp = Paragraph(ln, _style(F["sans"], 11, mut, leading=11 * 1.8, align=TA_CENTER))
370
+ _, hh = pp.wrap(LW(inner), H); pp.drawOn(c, (W - LW(inner)) / 2, y - hh); y -= hh
371
+ else:
372
+ pp = Paragraph(ct.get("authors", ""), _style(F["sans"], 12, tx, leading=12 * 1.2, align=TA_CENTER))
373
+ _, hh = pp.wrap(LW(inner), H); pp.drawOn(c, (W - LW(inner)) / 2, y - hh); y -= hh + VH(12)
374
+ pp = Paragraph(ct.get("institution", ""), _style(F["sans"], 10, mut, leading=10 * 1.4, align=TA_CENTER))
375
+ _, hh = pp.wrap(LW(inner), H); pp.drawOn(c, (W - LW(inner)) / 2, y - hh)
376
+ foot = " · ".join([s for s in [ct.get("footer_left"), ct.get("footer_right")] if s])
377
+ if foot:
378
+ pp = Paragraph(foot, _style(F["sans"], 9, mut, leading=9 * 1.2, align=TA_CENTER))
379
+ pp.wrap(LW(inner), H); pp.drawOn(c, (W - LW(inner)) / 2, YT(PXH - 57))
380
+
381
+ def _t07_academic_symmetric(c, ct, F, P): _centered_dark(c, ct, F, P, 114, keywords=False)
382
+ def _t08_academic_journal(c, ct, F, P): _centered_dark(c, ct, F, P, 152, keywords=True)
383
+
384
+ def _t09_institutional(c, ct, F, P):
385
+ black = _c("#1a1a1a"); linec = _c("#333333"); dmut = _c("#4a4a4a")
386
+ # serif family: use kai if requested, else serif
387
+ serif = F["kai"] if ct.get("style") == "kai" else F["serif"]
388
+ c.saveState(); c.setStrokeColor(black); c.setLineWidth(LW(2.5))
389
+ c.rect(X(40), YT(PXH - 56), X(PXW - 40) - X(40), YT(56) - YT(PXH - 56), stroke=1, fill=0); c.restoreState()
390
+ cont_l = 90; cont_w = (PXW - 40 - 50) - 90; cont_bottom = PXH - 56 - 60
391
+ y = 56 + 60
392
+ y += center_line(c, ct["institution"], y, serif, 30, black, ls_px=6) + 30
393
+ dw = 0.70 * cont_w
394
+ hline(c, PXW / 2 - dw / 2, y, dw, 2, black); y += 2 + 40
395
+ if ct.get("doc_type"):
396
+ y += center_line(c, ct["doc_type"], y, F["sans"], 22, black, ls_px=4) + 50
397
+ y += para(c, ct["title"], (PXW - 520) / 2, y, 520, serif, 24, black, line_h=1.4, align=TA_CENTER) + 110
398
+ fld_w = 400; fld_x = (PXW - fld_w) / 2; label_w = 90
399
+ for lbl, val in ct.get("fields", []):
400
+ line_text(c, lbl, fld_x, y, F["sans"], 14, black, ls_px=2)
401
+ vx = fld_x + label_w + 12; vw = fld_w - label_w - 12
402
+ pp = Paragraph(val, _style(F["sans"], 14, black, leading=14 * 1.2, align=TA_CENTER))
403
+ _, hh = pp.wrap(LW(vw), H); pp.drawOn(c, X(vx), YT(y) - hh)
404
+ hline(c, vx, y + 26, vw, 1, linec); y += 58
405
+ if ct.get("date"):
406
+ center_line(c, ct["date"], cont_bottom - 26, F["sans"], 14, dmut, ls_px=2)
407
+
408
+ TEMPLATES = {
409
+ "01": (_t01_hud, "bg"), "02": (_t02_corporate, "bg"), "03": (_t03_monolith, "bg"),
410
+ "04": (_t04_museum, "bg"), "05": (_t05_sidebar, "bg"),
411
+ "06": (_t06_academic_vertical, "ac_bg"), "07": (_t07_academic_symmetric, "ac_bg"),
412
+ "08": (_t08_academic_journal, "ac_bg"), "09": (_t09_institutional, "bg"),
413
+ }
414
+
415
+ # ═══════════════════════════════════════════════════════════════════
416
+ # public API
417
+ # ═══════════════════════════════════════════════════════════════════
418
+ def render_cover(template, content, out_pdf, palette=None, fonts=None, font_overrides=None):
419
+ """Render one cover to a single-page A4 PDF.
420
+ template: "01".."09"; content: dict (keys depend on template family);
421
+ palette: optional dict overriding DEFAULT_PALETTE; fonts: optional pre-detected
422
+ dict from detect_fonts(); font_overrides: role->(name,path,idx)."""
423
+ template = str(template).zfill(2)
424
+ if template not in TEMPLATES:
425
+ raise ValueError("unknown template %r (expected 01-09)" % template)
426
+ P = dict(DEFAULT_PALETTE); P.update(palette or {})
427
+ F = fonts or detect_fonts(font_overrides)
428
+ fn, bgkey = TEMPLATES[template]
429
+ c = canvas.Canvas(out_pdf, pagesize=(W, H))
430
+ c.setFillColor(_c(P[bgkey])); c.rect(0, 0, W, H, stroke=0, fill=1)
431
+ fn(c, content, F, P)
432
+ c.showPage(); c.save()
433
+ return out_pdf
434
+
435
+ # ── demo sample content ──
436
+ _DEMO_REPORT = {
437
+ "kicker": "2025 年度可持续发展报告 · ANNUAL ESG REPORT", "hero": "青松环境科技",
438
+ "summary": "本报告系统披露青松环境科技在 2025 财年的 ESG 实践、碳中和路径与核心运营数据,涵盖 Green Energy、循环经济与社会责任等关键议题。",
439
+ "meta": "发布单位:青松环境科技集团 · 2026 年 3 月",
440
+ "footer": "QINGSONG GREENTECH · DOC 2026-ESG-001",
441
+ "footer_left": "青松环境科技集团 · 2026 年 3 月", "footer_right": "DOC 2026-ESG-001",
442
+ "year": "2026", "word": "REPORT",
443
+ }
444
+ _DEMO_ACADEMIC = {
445
+ "label": "博士学位论文 · DOCTORAL DISSERTATION",
446
+ "title": "面向城市交通的深度强化学习信号协同优化方法研究",
447
+ "subtitle": "A Study on Deep Reinforcement Learning for Urban Traffic Signal Coordination",
448
+ "authors": "张明远", "institution": "清华大学 计算机科学与技术系",
449
+ "keywords_lines": ["深度强化学习 · 交通信号控制", "多智能体协同 · 城市交通仿真"],
450
+ "footer_left": "Journal of Intelligent Transportation", "footer_right": "2026 年 3 月",
451
+ }
452
+ _DEMO_INST = {
453
+ "institution": "清华大学", "doc_type": "博士学位论文开题报告",
454
+ "title": "面向城市交通的深度强化学习信号协同优化方法研究",
455
+ "fields": [["姓  名", "张明远"], ["学  号", "2021210001"], ["导  师", "李国华 教授"],
456
+ ["院  系", "计算机科学与技术系"], ["专  业", "计算机科学与技术"]],
457
+ "date": "2026 年 3 月",
458
+ }
459
+ def _demo_content(t):
460
+ if t in ("06", "07", "08"): return _DEMO_ACADEMIC
461
+ if t == "09": return _DEMO_INST
462
+ return _DEMO_REPORT
463
+
464
+ def main(argv=None):
465
+ ap = argparse.ArgumentParser(description="ReportLab cover generator (templates 01-09)")
466
+ ap.add_argument("template", nargs="?", help="01-09")
467
+ ap.add_argument("--out", default="cover.pdf")
468
+ ap.add_argument("--content", help="path to JSON content file")
469
+ ap.add_argument("--demo", action="store_true", help="render all 9 with sample content")
470
+ ap.add_argument("--outdir", default=".")
471
+ a = ap.parse_args(argv)
472
+ F = detect_fonts()
473
+ print("Detected fonts:", {k: v for k, v in F.items()})
474
+ if a.demo:
475
+ for t in TEMPLATES:
476
+ out = os.path.join(a.outdir, "cover_%s.pdf" % t)
477
+ render_cover(t, _demo_content(t), out, fonts=F); print("wrote", out)
478
+ return
479
+ if not a.template:
480
+ ap.error("template (01-09) or --demo required")
481
+ content = _demo_content(str(a.template).zfill(2))
482
+ if a.content:
483
+ with open(a.content, encoding="utf-8") as f: content = json.load(f)
484
+ render_cover(a.template, content, a.out, fonts=F); print("wrote", a.out)
485
+
486
+ if __name__ == "__main__":
487
+ main()
@@ -33,7 +33,42 @@
33
33
 
34
34
  const fs = require('fs');
35
35
  const path = require('path');
36
- const { spawnSync } = require('child_process');
36
+ const { execSync, spawnSync } = require('child_process');
37
+
38
+ // ── Playwright resolution ──
39
+
40
+ function loadPlaywright() {
41
+ for (const name of ['playwright', 'playwright-core']) {
42
+ try { return require(name); } catch (_) {}
43
+ }
44
+
45
+ // Node never searches npm's global dir, and when node itself was installed
46
+ // into a private prefix, playwright is usually installed right next to it.
47
+ // Resolve both by hand instead of reporting "not installed".
48
+ const Module = require('module');
49
+ const roots = new Set();
50
+ if (process.env.PLAYWRIGHT_PATH) roots.add(process.env.PLAYWRIGHT_PATH);
51
+ if (process.env.NODE_PATH) {
52
+ process.env.NODE_PATH.split(path.delimiter).filter(Boolean).forEach(p => roots.add(p));
53
+ }
54
+ // <prefix>/bin/node -> <prefix>/node_modules
55
+ roots.add(path.join(path.dirname(process.execPath), '..', 'node_modules'));
56
+ try {
57
+ const g = execSync('npm root -g', { stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim();
58
+ if (g) roots.add(g);
59
+ } catch (_) {}
60
+
61
+ for (const base of roots) {
62
+ for (const name of ['playwright', 'playwright-core']) {
63
+ const dir = path.join(base, name);
64
+ if (!fs.existsSync(path.join(dir, 'package.json'))) continue;
65
+ // Require by absolute path — createRequire() would still walk the
66
+ // node_modules chain and miss roots not named node_modules.
67
+ try { return Module.createRequire(__filename)(dir); } catch (_) {}
68
+ }
69
+ }
70
+ return null;
71
+ }
37
72
 
38
73
  // ── Chromium resolution (shared logic with html2pdf-next.js) ──
39
74
 
@@ -113,16 +148,12 @@ async function main() {
113
148
  console.log(` Width: ${width}`);
114
149
 
115
150
  // Load Playwright
116
- let playwright;
117
- try {
118
- playwright = require('playwright');
119
- } catch {
120
- try {
121
- playwright = require('playwright-core');
122
- } catch {
123
- console.error('Error: playwright or playwright-core not installed.');
124
- process.exit(1);
125
- }
151
+ const playwright = loadPlaywright();
152
+ if (!playwright) {
153
+ console.error('Error: playwright or playwright-core not installed.');
154
+ console.error(` Searched: require paths, PLAYWRIGHT_PATH, NODE_PATH,`);
155
+ console.error(` ${path.join(path.dirname(process.execPath), '..', 'node_modules')}, npm root -g`);
156
+ process.exit(1);
126
157
  }
127
158
 
128
159
  const { chromium } = playwright;
@@ -254,3 +285,4 @@ main().catch(err => {
254
285
  console.error(`\n✗ Fatal: ${err.message}`);
255
286
  process.exit(1);
256
287
  });
288
+