@project-ava-supernova/core 0.2.183 → 0.2.185

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 (239) hide show
  1. package/dist/agent/agent.d.ts.map +1 -1
  2. package/dist/agent/agent.js +36 -24
  3. package/dist/agent/agent.js.map +1 -1
  4. package/dist/agent/system-prompt.d.ts +6 -0
  5. package/dist/agent/system-prompt.d.ts.map +1 -1
  6. package/dist/agent/system-prompt.js +15 -6
  7. package/dist/agent/system-prompt.js.map +1 -1
  8. package/dist/docs/content/concepts.js +1 -1
  9. package/dist/docs/content/concepts.js.map +1 -1
  10. package/dist/docs/content/features.d.ts.map +1 -1
  11. package/dist/docs/content/features.js +16 -9
  12. package/dist/docs/content/features.js.map +1 -1
  13. package/dist/docs/content/start.d.ts.map +1 -1
  14. package/dist/docs/content/start.js +1 -0
  15. package/dist/docs/content/start.js.map +1 -1
  16. package/dist/docs/data/tools.js +4 -4
  17. package/dist/docs/data/tools.js.map +1 -1
  18. package/dist/docs/i18n/translations.d.ts.map +1 -1
  19. package/dist/docs/i18n/translations.js +1 -1
  20. package/dist/docs/i18n/translations.js.map +1 -1
  21. package/dist/i18n/locales/ar.d.ts.map +1 -1
  22. package/dist/i18n/locales/ar.js +29 -0
  23. package/dist/i18n/locales/ar.js.map +1 -1
  24. package/dist/i18n/locales/de.d.ts.map +1 -1
  25. package/dist/i18n/locales/de.js +29 -0
  26. package/dist/i18n/locales/de.js.map +1 -1
  27. package/dist/i18n/locales/en.d.ts +29 -0
  28. package/dist/i18n/locales/en.d.ts.map +1 -1
  29. package/dist/i18n/locales/en.js +32 -0
  30. package/dist/i18n/locales/en.js.map +1 -1
  31. package/dist/i18n/locales/es.d.ts.map +1 -1
  32. package/dist/i18n/locales/es.js +29 -0
  33. package/dist/i18n/locales/es.js.map +1 -1
  34. package/dist/i18n/locales/fr.d.ts.map +1 -1
  35. package/dist/i18n/locales/fr.js +29 -0
  36. package/dist/i18n/locales/fr.js.map +1 -1
  37. package/dist/i18n/locales/hi.d.ts.map +1 -1
  38. package/dist/i18n/locales/hi.js +29 -0
  39. package/dist/i18n/locales/hi.js.map +1 -1
  40. package/dist/i18n/locales/id.d.ts.map +1 -1
  41. package/dist/i18n/locales/id.js +29 -0
  42. package/dist/i18n/locales/id.js.map +1 -1
  43. package/dist/i18n/locales/it.d.ts.map +1 -1
  44. package/dist/i18n/locales/it.js +29 -0
  45. package/dist/i18n/locales/it.js.map +1 -1
  46. package/dist/i18n/locales/ja.d.ts.map +1 -1
  47. package/dist/i18n/locales/ja.js +29 -0
  48. package/dist/i18n/locales/ja.js.map +1 -1
  49. package/dist/i18n/locales/ko.d.ts.map +1 -1
  50. package/dist/i18n/locales/ko.js +29 -0
  51. package/dist/i18n/locales/ko.js.map +1 -1
  52. package/dist/i18n/locales/nl.d.ts.map +1 -1
  53. package/dist/i18n/locales/nl.js +29 -0
  54. package/dist/i18n/locales/nl.js.map +1 -1
  55. package/dist/i18n/locales/pl.d.ts.map +1 -1
  56. package/dist/i18n/locales/pl.js +29 -0
  57. package/dist/i18n/locales/pl.js.map +1 -1
  58. package/dist/i18n/locales/pt.d.ts.map +1 -1
  59. package/dist/i18n/locales/pt.js +29 -0
  60. package/dist/i18n/locales/pt.js.map +1 -1
  61. package/dist/i18n/locales/ru.d.ts.map +1 -1
  62. package/dist/i18n/locales/ru.js +29 -0
  63. package/dist/i18n/locales/ru.js.map +1 -1
  64. package/dist/i18n/locales/th.d.ts.map +1 -1
  65. package/dist/i18n/locales/th.js +29 -0
  66. package/dist/i18n/locales/th.js.map +1 -1
  67. package/dist/i18n/locales/tr.d.ts.map +1 -1
  68. package/dist/i18n/locales/tr.js +29 -0
  69. package/dist/i18n/locales/tr.js.map +1 -1
  70. package/dist/i18n/locales/uk.d.ts.map +1 -1
  71. package/dist/i18n/locales/uk.js +29 -0
  72. package/dist/i18n/locales/uk.js.map +1 -1
  73. package/dist/i18n/locales/vi.d.ts.map +1 -1
  74. package/dist/i18n/locales/vi.js +29 -0
  75. package/dist/i18n/locales/vi.js.map +1 -1
  76. package/dist/i18n/locales/zh-CN.d.ts.map +1 -1
  77. package/dist/i18n/locales/zh-CN.js +29 -0
  78. package/dist/i18n/locales/zh-CN.js.map +1 -1
  79. package/dist/i18n/locales/zh-TW.d.ts.map +1 -1
  80. package/dist/i18n/locales/zh-TW.js +29 -0
  81. package/dist/i18n/locales/zh-TW.js.map +1 -1
  82. package/dist/index.d.ts +7 -0
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +5 -0
  85. package/dist/index.js.map +1 -1
  86. package/dist/memory-v4/consolidate.d.ts +44 -0
  87. package/dist/memory-v4/consolidate.d.ts.map +1 -0
  88. package/dist/memory-v4/consolidate.js +241 -0
  89. package/dist/memory-v4/consolidate.js.map +1 -0
  90. package/dist/memory-v4/fact-cache.d.ts +38 -0
  91. package/dist/memory-v4/fact-cache.d.ts.map +1 -0
  92. package/dist/memory-v4/fact-cache.js +133 -0
  93. package/dist/memory-v4/fact-cache.js.map +1 -0
  94. package/dist/memory-v4/fact-file.d.ts +30 -0
  95. package/dist/memory-v4/fact-file.d.ts.map +1 -0
  96. package/dist/memory-v4/fact-file.js +133 -0
  97. package/dist/memory-v4/fact-file.js.map +1 -0
  98. package/dist/memory-v4/files.d.ts +30 -0
  99. package/dist/memory-v4/files.d.ts.map +1 -0
  100. package/dist/memory-v4/files.js +115 -0
  101. package/dist/memory-v4/files.js.map +1 -0
  102. package/dist/memory-v4/import-v3.d.ts +38 -0
  103. package/dist/memory-v4/import-v3.d.ts.map +1 -0
  104. package/dist/memory-v4/import-v3.js +148 -0
  105. package/dist/memory-v4/import-v3.js.map +1 -0
  106. package/dist/memory-v4/index.d.ts +20 -0
  107. package/dist/memory-v4/index.d.ts.map +1 -0
  108. package/dist/memory-v4/index.js +14 -0
  109. package/dist/memory-v4/index.js.map +1 -0
  110. package/dist/memory-v4/model.d.ts +26 -0
  111. package/dist/memory-v4/model.d.ts.map +1 -0
  112. package/dist/memory-v4/model.js +49 -0
  113. package/dist/memory-v4/model.js.map +1 -0
  114. package/dist/memory-v4/page.d.ts +57 -0
  115. package/dist/memory-v4/page.d.ts.map +1 -0
  116. package/dist/memory-v4/page.js +150 -0
  117. package/dist/memory-v4/page.js.map +1 -0
  118. package/dist/memory-v4/projects.d.ts +38 -0
  119. package/dist/memory-v4/projects.d.ts.map +1 -0
  120. package/dist/memory-v4/projects.js +135 -0
  121. package/dist/memory-v4/projects.js.map +1 -0
  122. package/dist/memory-v4/promotion.d.ts +17 -0
  123. package/dist/memory-v4/promotion.d.ts.map +1 -0
  124. package/dist/memory-v4/promotion.js +50 -0
  125. package/dist/memory-v4/promotion.js.map +1 -0
  126. package/dist/memory-v4/reader.d.ts +83 -0
  127. package/dist/memory-v4/reader.d.ts.map +1 -0
  128. package/dist/memory-v4/reader.js +270 -0
  129. package/dist/memory-v4/reader.js.map +1 -0
  130. package/dist/memory-v4/search-index.d.ts +43 -0
  131. package/dist/memory-v4/search-index.d.ts.map +1 -0
  132. package/dist/memory-v4/search-index.js +147 -0
  133. package/dist/memory-v4/search-index.js.map +1 -0
  134. package/dist/memory-v4/store.d.ts +168 -0
  135. package/dist/memory-v4/store.d.ts.map +1 -0
  136. package/dist/memory-v4/store.js +549 -0
  137. package/dist/memory-v4/store.js.map +1 -0
  138. package/dist/memory-v4/threads-episodes.d.ts +36 -0
  139. package/dist/memory-v4/threads-episodes.d.ts.map +1 -0
  140. package/dist/memory-v4/threads-episodes.js +119 -0
  141. package/dist/memory-v4/threads-episodes.js.map +1 -0
  142. package/dist/memory-v4/types.d.ts +84 -0
  143. package/dist/memory-v4/types.d.ts.map +1 -0
  144. package/dist/memory-v4/types.js +14 -0
  145. package/dist/memory-v4/types.js.map +1 -0
  146. package/dist/tools/authoring/doc-model.d.ts +2 -11
  147. package/dist/tools/authoring/doc-model.d.ts.map +1 -1
  148. package/dist/tools/authoring/doc-model.js +2 -39
  149. package/dist/tools/authoring/doc-model.js.map +1 -1
  150. package/dist/tools/authoring/document-places.d.ts +19 -0
  151. package/dist/tools/authoring/document-places.d.ts.map +1 -0
  152. package/dist/tools/authoring/document-places.js +24 -0
  153. package/dist/tools/authoring/document-places.js.map +1 -0
  154. package/dist/tools/authoring/export-document.d.ts +10 -0
  155. package/dist/tools/authoring/export-document.d.ts.map +1 -1
  156. package/dist/tools/authoring/export-document.js +26 -0
  157. package/dist/tools/authoring/export-document.js.map +1 -1
  158. package/dist/tools/authoring/font-manifest.d.ts +25 -0
  159. package/dist/tools/authoring/font-manifest.d.ts.map +1 -0
  160. package/dist/tools/authoring/font-manifest.js +35 -0
  161. package/dist/tools/authoring/font-manifest.js.map +1 -0
  162. package/dist/tools/authoring/formula-eval.d.ts +34 -0
  163. package/dist/tools/authoring/formula-eval.d.ts.map +1 -0
  164. package/dist/tools/authoring/formula-eval.js +250 -0
  165. package/dist/tools/authoring/formula-eval.js.map +1 -0
  166. package/dist/tools/authoring/index.d.ts +0 -1
  167. package/dist/tools/authoring/index.d.ts.map +1 -1
  168. package/dist/tools/authoring/index.js +2 -3
  169. package/dist/tools/authoring/index.js.map +1 -1
  170. package/dist/tools/authoring/ods-spreadsheet.d.ts +56 -0
  171. package/dist/tools/authoring/ods-spreadsheet.d.ts.map +1 -0
  172. package/dist/tools/authoring/ods-spreadsheet.js +426 -0
  173. package/dist/tools/authoring/ods-spreadsheet.js.map +1 -0
  174. package/dist/tools/authoring/pdf-fonts.d.ts +53 -0
  175. package/dist/tools/authoring/pdf-fonts.d.ts.map +1 -0
  176. package/dist/tools/authoring/pdf-fonts.js +269 -0
  177. package/dist/tools/authoring/pdf-fonts.js.map +1 -0
  178. package/dist/tools/authoring/read-office.d.ts +29 -0
  179. package/dist/tools/authoring/read-office.d.ts.map +1 -0
  180. package/dist/tools/authoring/read-office.js +48 -0
  181. package/dist/tools/authoring/read-office.js.map +1 -0
  182. package/dist/tools/authoring/render-pdf.d.ts.map +1 -1
  183. package/dist/tools/authoring/render-pdf.js +136 -7
  184. package/dist/tools/authoring/render-pdf.js.map +1 -1
  185. package/dist/tools/authoring/sheet-view.d.ts +32 -0
  186. package/dist/tools/authoring/sheet-view.d.ts.map +1 -0
  187. package/dist/tools/authoring/sheet-view.js +122 -0
  188. package/dist/tools/authoring/sheet-view.js.map +1 -0
  189. package/dist/tools/authoring/spreadsheet.d.ts +69 -0
  190. package/dist/tools/authoring/spreadsheet.d.ts.map +1 -0
  191. package/dist/tools/authoring/spreadsheet.js +216 -0
  192. package/dist/tools/authoring/spreadsheet.js.map +1 -0
  193. package/dist/tools/authoring/templates/builtins.d.ts.map +1 -1
  194. package/dist/tools/authoring/templates/builtins.js +2 -0
  195. package/dist/tools/authoring/templates/builtins.js.map +1 -1
  196. package/dist/tools/authoring/xlsx-charts.d.ts +66 -0
  197. package/dist/tools/authoring/xlsx-charts.d.ts.map +1 -0
  198. package/dist/tools/authoring/xlsx-charts.js +272 -0
  199. package/dist/tools/authoring/xlsx-charts.js.map +1 -0
  200. package/dist/tools/authoring/zip.d.ts +6 -0
  201. package/dist/tools/authoring/zip.d.ts.map +1 -1
  202. package/dist/tools/authoring/zip.js +45 -1
  203. package/dist/tools/authoring/zip.js.map +1 -1
  204. package/dist/tools/document-author.d.ts +4 -0
  205. package/dist/tools/document-author.d.ts.map +1 -1
  206. package/dist/tools/document-author.js +36 -24
  207. package/dist/tools/document-author.js.map +1 -1
  208. package/dist/tools/document-manage.d.ts +28 -25
  209. package/dist/tools/document-manage.d.ts.map +1 -1
  210. package/dist/tools/document-manage.js +298 -586
  211. package/dist/tools/document-manage.js.map +1 -1
  212. package/dist/tools/document-styling.d.ts +0 -26
  213. package/dist/tools/document-styling.d.ts.map +1 -1
  214. package/dist/tools/document-styling.js +0 -224
  215. package/dist/tools/document-styling.js.map +1 -1
  216. package/dist/tools/document-templates.d.ts +3 -18
  217. package/dist/tools/document-templates.d.ts.map +1 -1
  218. package/dist/tools/document-templates.js +4 -94
  219. package/dist/tools/document-templates.js.map +1 -1
  220. package/dist/tools/email-draft.d.ts +3 -6
  221. package/dist/tools/email-draft.d.ts.map +1 -1
  222. package/dist/tools/email-draft.js +29 -123
  223. package/dist/tools/email-draft.js.map +1 -1
  224. package/dist/tools/learning.d.ts.map +1 -1
  225. package/dist/tools/learning.js +9 -3
  226. package/dist/tools/learning.js.map +1 -1
  227. package/dist/tools/memory-recall-v4.d.ts +20 -0
  228. package/dist/tools/memory-recall-v4.d.ts.map +1 -0
  229. package/dist/tools/memory-recall-v4.js +49 -0
  230. package/dist/tools/memory-recall-v4.js.map +1 -0
  231. package/dist/tools/memory-write-v4.d.ts +40 -0
  232. package/dist/tools/memory-write-v4.d.ts.map +1 -0
  233. package/dist/tools/memory-write-v4.js +176 -0
  234. package/dist/tools/memory-write-v4.js.map +1 -0
  235. package/dist/tools/report-generate.d.ts +1 -2
  236. package/dist/tools/report-generate.d.ts.map +1 -1
  237. package/dist/tools/report-generate.js +50 -167
  238. package/dist/tools/report-generate.js.map +1 -1
  239. package/package.json +12 -5
@@ -0,0 +1,269 @@
1
+ /**
2
+ * Fonts for PDFs in every one of Ava's languages (Documents 1.0, 15 Nov 2026).
3
+ *
4
+ * installPdfFonts(doc, text) does two things to a pdfkit document:
5
+ *
6
+ * 1. The standard names ('Helvetica', 'Helvetica-Bold', …) are pointed at
7
+ * Noto Sans, which covers every Latin accent (Polish, Turkish,
8
+ * Vietnamese), Cyrillic and Greek — so every existing call writes those
9
+ * correctly, tables included, with no other change.
10
+ * 2. doc.text is wrapped: text holding Arabic, Devanagari, Thai, Chinese,
11
+ * Japanese or Korean is split into runs and each run is written in the
12
+ * font for its script, then the font is put back.
13
+ *
14
+ * The fonts come from AVA_FONTS_DIR (the extension and the IDE set it to the
15
+ * fonts they ship), else the build cache beside core. Bold Chinese, Japanese
16
+ * and Korean come from ~/.ava/fonts when the person has downloaded them.
17
+ * With no fonts at all the PDF is written as before — the caller can ask
18
+ * missingScripts() and say so plainly rather than hand back a garbled file.
19
+ */
20
+ import { existsSync } from 'node:fs';
21
+ import { mkdir, rename, rm, stat, writeFile } from 'node:fs/promises';
22
+ import { createHash } from 'node:crypto';
23
+ import { homedir } from 'node:os';
24
+ import { dirname, join } from 'node:path';
25
+ import { fileURLToPath } from 'node:url';
26
+ import { OPTIONAL_FONTS } from './font-manifest.js';
27
+ /** The script of one character, or null for a neutral one (space, digit, punctuation). */
28
+ export function scriptOf(ch) {
29
+ const c = ch.codePointAt(0) ?? 0;
30
+ if ((c >= 0x0600 && c <= 0x06ff) || (c >= 0x0750 && c <= 0x077f) || (c >= 0x08a0 && c <= 0x08ff) || (c >= 0xfb50 && c <= 0xfdff) || (c >= 0xfe70 && c <= 0xfeff))
31
+ return 'arabic';
32
+ if ((c >= 0x0900 && c <= 0x097f) || (c >= 0xa8e0 && c <= 0xa8ff))
33
+ return 'devanagari';
34
+ if (c >= 0x0e00 && c <= 0x0e7f)
35
+ return 'thai';
36
+ if ((c >= 0x1100 && c <= 0x11ff) || (c >= 0x2e80 && c <= 0x2fdf) || (c >= 0x3000 && c <= 0x30ff) || (c >= 0x3130 && c <= 0x318f)
37
+ || (c >= 0x3400 && c <= 0x4dbf) || (c >= 0x4e00 && c <= 0x9fff) || (c >= 0xac00 && c <= 0xd7af) || (c >= 0xf900 && c <= 0xfaff)
38
+ || (c >= 0xff00 && c <= 0xffef) || (c >= 0x20000 && c <= 0x2fa1f))
39
+ return 'cjk';
40
+ if (/[\p{L}]/u.test(ch))
41
+ return 'latin';
42
+ return null;
43
+ }
44
+ /** Text in runs of one script each; neutral characters stay with the run they sit in. */
45
+ export function splitByScript(text) {
46
+ const out = [];
47
+ for (const ch of Array.from(text)) {
48
+ const s = scriptOf(ch);
49
+ const last = out[out.length - 1];
50
+ if (!last) {
51
+ out.push({ script: s ?? 'latin', text: ch });
52
+ continue;
53
+ }
54
+ if (s === null || s === last.script) {
55
+ last.text += ch;
56
+ continue;
57
+ }
58
+ // A run that so far holds only neutral characters takes the script of what follows.
59
+ if (scriptOf(Array.from(last.text).find((x) => scriptOf(x) !== null) ?? '') === null) {
60
+ last.script = s;
61
+ last.text += ch;
62
+ continue;
63
+ }
64
+ out.push({ script: s, text: ch });
65
+ }
66
+ return out;
67
+ }
68
+ /** Which Chinese/Japanese/Korean letterforms a document wants. */
69
+ export function cjkFaceFor(text, lang) {
70
+ if (lang) {
71
+ const l = lang.toLowerCase();
72
+ if (l.startsWith('ja'))
73
+ return 'jp';
74
+ if (l.startsWith('ko'))
75
+ return 'kr';
76
+ if (l === 'zh-tw' || l === 'zh-hk' || l.startsWith('zh-hant'))
77
+ return 'tc';
78
+ if (l.startsWith('zh'))
79
+ return 'sc';
80
+ }
81
+ if (/[぀-ヿ]/.test(text))
82
+ return 'jp';
83
+ if (/[가-힯ᄀ-ᇿ]/.test(text))
84
+ return 'kr';
85
+ return 'sc';
86
+ }
87
+ /** Mostly right-to-left text (Arabic): a paragraph that should sit on the right. */
88
+ export function isMostlyRtl(text) {
89
+ let rtl = 0, ltr = 0;
90
+ for (const ch of text) {
91
+ const s = scriptOf(ch);
92
+ if (s === 'arabic')
93
+ rtl++;
94
+ else if (s)
95
+ ltr++;
96
+ }
97
+ return rtl > 0 && rtl >= ltr;
98
+ }
99
+ // ── Where the fonts are ──────────────────────────────────────────────────────
100
+ /** Ava's own folder for downloaded fonts (the optional pack). */
101
+ export function userFontsDir() {
102
+ return join(process.env.AVA_HOME || join(homedir(), '.ava'), 'fonts');
103
+ }
104
+ function bundledFontsDir() {
105
+ const candidates = [];
106
+ if (process.env.AVA_FONTS_DIR)
107
+ candidates.push(process.env.AVA_FONTS_DIR);
108
+ try {
109
+ // Unbundled core (tests, the dev host's sidecar): the build cache beside the package.
110
+ const here = dirname(fileURLToPath(import.meta.url));
111
+ candidates.push(join(here, '..', '..', '..', '.fonts-cache'), join(here, '..', '..', '.fonts-cache'));
112
+ }
113
+ catch { /* bundled as CommonJS: AVA_FONTS_DIR is set by the host */ }
114
+ return candidates.find((d) => existsSync(join(d, 'NotoSans-Regular.ttf'))) ?? null;
115
+ }
116
+ // ── Into a pdfkit document ───────────────────────────────────────────────────
117
+ /**
118
+ * Give a pdfkit document fonts for every script in `allText`. Returns false
119
+ * (and changes nothing) when no fonts are installed.
120
+ */
121
+ export function installPdfFonts(doc, allText, lang) {
122
+ const dir = bundledFontsDir();
123
+ if (!dir)
124
+ return false;
125
+ const f = (name) => join(dir, name);
126
+ // 1. Latin / Cyrillic / Greek / Vietnamese. Registered under their own names
127
+ // and swapped in for the standard ones below: pdfkit caches its built-in
128
+ // 'Helvetica' when the document is created, so re-registering that name
129
+ // is silently ignored for regular text (found in testing, 15 Nov 2026).
130
+ const LATIN = {
131
+ Helvetica: 'ava-latin', 'Helvetica-Bold': 'ava-latin-bold', 'Helvetica-Oblique': 'ava-latin-italic', 'Helvetica-BoldOblique': 'ava-latin-bolditalic',
132
+ };
133
+ doc.registerFont('ava-latin', f('NotoSans-Regular.ttf'));
134
+ doc.registerFont('ava-latin-bold', f('NotoSans-Bold.ttf'));
135
+ doc.registerFont('ava-latin-italic', f('NotoSans-Italic.ttf'));
136
+ doc.registerFont('ava-latin-bolditalic', f('NotoSans-BoldItalic.ttf'));
137
+ // 2. The other scripts, only when the document uses them.
138
+ const needs = new Set(splitByScript(allText).map((r) => r.script));
139
+ const face = cjkFaceFor(allText, lang);
140
+ const boldCjk = join(userFontsDir(), 'NotoSansCJK-Bold.ttc');
141
+ const fonts = {};
142
+ if (needs.has('arabic')) {
143
+ doc.registerFont('ava-arabic', f('NotoSansArabic-Regular.ttf'));
144
+ doc.registerFont('ava-arabic-bold', f('NotoSansArabic-Bold.ttf'));
145
+ fonts.arabic = { regular: 'ava-arabic', bold: 'ava-arabic-bold' };
146
+ }
147
+ if (needs.has('devanagari')) {
148
+ doc.registerFont('ava-deva', f('NotoSansDevanagari-Regular.ttf'));
149
+ doc.registerFont('ava-deva-bold', f('NotoSansDevanagari-Bold.ttf'));
150
+ fonts.devanagari = { regular: 'ava-deva', bold: 'ava-deva-bold' };
151
+ }
152
+ if (needs.has('thai')) {
153
+ doc.registerFont('ava-thai', f('NotoSansThai-Regular.ttf'));
154
+ doc.registerFont('ava-thai-bold', f('NotoSansThai-Bold.ttf'));
155
+ fonts.thai = { regular: 'ava-thai', bold: 'ava-thai-bold' };
156
+ }
157
+ if (needs.has('cjk') && existsSync(f('NotoSansCJK-Regular.ttc'))) {
158
+ doc.registerFont('ava-cjk', f('NotoSansCJK-Regular.ttc'), `NotoSansCJK${face}-Regular`);
159
+ // Real bold only with the optional pack; otherwise bold prints at regular weight.
160
+ if (existsSync(boldCjk))
161
+ doc.registerFont('ava-cjk-bold', boldCjk, `NotoSansCJK${face}-Bold`);
162
+ fonts.cjk = { regular: 'ava-cjk', bold: existsSync(boldCjk) ? 'ava-cjk-bold' : 'ava-cjk' };
163
+ }
164
+ // Track the font each call asks for, so a run can switch away and back;
165
+ // the standard names become Noto Sans.
166
+ const origFont = doc.font.bind(doc);
167
+ let current = 'ava-latin';
168
+ doc.font = (src, ...rest) => {
169
+ if (typeof src === 'string') {
170
+ current = LATIN[src] ?? src;
171
+ return origFont(current, ...rest);
172
+ }
173
+ return origFont(src, ...rest);
174
+ };
175
+ doc.font('Helvetica');
176
+ if (!Object.keys(fonts).length)
177
+ return true;
178
+ const origText = doc.text.bind(doc);
179
+ doc.text = (text, x, y, options) => {
180
+ const str = text == null ? '' : String(text);
181
+ const runs = splitByScript(str);
182
+ if (runs.every((r) => r.script === 'latin' || !fonts[r.script]))
183
+ return origText(text, x, y, options);
184
+ // pdfkit takes (text, options) or (text, x, y, options).
185
+ let opts = options;
186
+ let px = x, py = y;
187
+ if (typeof x === 'object' && x !== null) {
188
+ opts = x;
189
+ px = undefined;
190
+ py = undefined;
191
+ }
192
+ const base = current;
193
+ const bold = /bold/i.test(base);
194
+ runs.forEach((run, i) => {
195
+ const last = i === runs.length - 1;
196
+ const pick = run.script === 'latin' ? base : (fonts[run.script]?.[bold ? 'bold' : 'regular'] ?? base);
197
+ origFont(pick);
198
+ const o = { ...(opts ?? {}), continued: last ? Boolean(opts?.continued) : true };
199
+ if (i === 0 && px !== undefined)
200
+ origText(run.text, px, py, o);
201
+ else
202
+ origText(run.text, o);
203
+ });
204
+ origFont(base);
205
+ current = base;
206
+ return doc;
207
+ };
208
+ return true;
209
+ }
210
+ /** Scripts in the text that this build cannot write into a PDF. */
211
+ export function missingScripts(text) {
212
+ const dir = bundledFontsDir();
213
+ const used = new Set(splitByScript(text).map((r) => r.script));
214
+ if (dir)
215
+ return existsSync(join(dir, 'NotoSansCJK-Regular.ttc')) || !used.has('cjk') ? [] : ['cjk'];
216
+ // No fonts: the built-in ones cover Western European letters only.
217
+ const latinBeyond = Array.from(text).some((ch) => scriptOf(ch) === 'latin' && (ch.codePointAt(0) ?? 0) > 0xff);
218
+ return [...used].filter((s) => s !== 'latin' || latinBeyond);
219
+ }
220
+ export async function optionalFontsStatus() {
221
+ const sizeBytes = OPTIONAL_FONTS.reduce((n, f) => n + f.size, 0);
222
+ for (const f of OPTIONAL_FONTS) {
223
+ try {
224
+ if ((await stat(join(userFontsDir(), f.file))).size !== f.size)
225
+ return { installed: false, sizeBytes };
226
+ }
227
+ catch {
228
+ return { installed: false, sizeBytes };
229
+ }
230
+ }
231
+ return { installed: true, sizeBytes };
232
+ }
233
+ /**
234
+ * Download the optional pack into ~/.ava/fonts. Every file is checked against
235
+ * its SHA-256 before it is kept; a wrong or damaged download is thrown away.
236
+ */
237
+ export async function downloadOptionalFonts(onProgress) {
238
+ const dir = userFontsDir();
239
+ await mkdir(dir, { recursive: true });
240
+ const total = OPTIONAL_FONTS.reduce((n, f) => n + f.size, 0);
241
+ let done = 0;
242
+ for (const f of OPTIONAL_FONTS) {
243
+ const res = await fetch(f.url);
244
+ if (!res.ok || !res.body)
245
+ throw new Error(`The font download failed (${res.status}).`);
246
+ const chunks = [];
247
+ const reader = res.body.getReader();
248
+ for (;;) {
249
+ const { done: end, value } = await reader.read();
250
+ if (end)
251
+ break;
252
+ chunks.push(value);
253
+ done += value.length;
254
+ onProgress?.(Math.min(done, total), total);
255
+ }
256
+ const bytes = Buffer.concat(chunks);
257
+ const sha = createHash('sha256').update(bytes).digest('hex');
258
+ if (bytes.length !== f.size || sha !== f.sha256)
259
+ throw new Error('The downloaded font did not match the expected file, so it was not kept.');
260
+ const tmp = join(dir, `${f.file}.part`);
261
+ await writeFile(tmp, bytes);
262
+ await rename(tmp, join(dir, f.file));
263
+ }
264
+ }
265
+ export async function removeOptionalFonts() {
266
+ for (const f of OPTIONAL_FONTS)
267
+ await rm(join(userFontsDir(), f.file), { force: true });
268
+ }
269
+ //# sourceMappingURL=pdf-fonts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pdf-fonts.js","sourceRoot":"","sources":["../../../src/tools/authoring/pdf-fonts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAOpD,0FAA0F;AAC1F,MAAM,UAAU,QAAQ,CAAC,EAAU;IACjC,MAAM,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACjC,IAAI,CAAC,CAAC,IAAI,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,MAAM,IAAI,CAAC,IAAI,MAAM,CAAC,IAA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QACxC,MAAM,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC5B,MAAM,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IACvC,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,mBAAmB;IACvC,KAAK,MAAM,CAAC,IAAI,cAAc;QAAE,MAAM,EAAE,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC1F,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Text out of a Word or PDF file — ONE place, shared by document_author and
3
+ * document_manage, so a library change is fixed once.
4
+ *
5
+ * Why it exists (14 Nov 2026): reading a PDF had quietly failed everywhere —
6
+ * pdf-parse moved to v2 and both tools still called it the v1 way.
7
+ *
8
+ * The libraries are loaded when needed (they are large), and a failure says
9
+ * what happened in plain words: never "npm install", which means nothing to
10
+ * someone writing a letter.
11
+ */
12
+ export interface OfficeText {
13
+ text: string;
14
+ /** PDF page count, when known. */
15
+ pages?: number;
16
+ }
17
+ /**
18
+ * PDF text via unpdf (15 Nov 2026, his call: "the more professional way").
19
+ * unpdf ships a pdf.js build made for servers and bundles — no browser-only
20
+ * globals, no worker file, no native canvas — so it reads the same in the
21
+ * extension bundle, the IDE sidecar and on the dev host. It replaced
22
+ * pdf-parse, whose v2 needed a DOMMatrix stand-in and a worker file copied
23
+ * beside every bundle.
24
+ */
25
+ export declare function readPdfText(data: Uint8Array): Promise<OfficeText>;
26
+ export declare function readDocxText(path: string): Promise<OfficeText>;
27
+ /** A plain sentence for a reader that failed — what happened, not how to install it. */
28
+ export declare function unreadable(kind: 'Word' | 'PDF', file: string, err: unknown): string;
29
+ //# sourceMappingURL=read-office.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-office.d.ts","sourceRoot":"","sources":["../../../src/tools/authoring/read-office.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC,CAUvE;AAED,wBAAsB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAMpE;AAED,wFAAwF;AACxF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,CAGnF"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Text out of a Word or PDF file — ONE place, shared by document_author and
3
+ * document_manage, so a library change is fixed once.
4
+ *
5
+ * Why it exists (14 Nov 2026): reading a PDF had quietly failed everywhere —
6
+ * pdf-parse moved to v2 and both tools still called it the v1 way.
7
+ *
8
+ * The libraries are loaded when needed (they are large), and a failure says
9
+ * what happened in plain words: never "npm install", which means nothing to
10
+ * someone writing a letter.
11
+ */
12
+ /**
13
+ * PDF text via unpdf (15 Nov 2026, his call: "the more professional way").
14
+ * unpdf ships a pdf.js build made for servers and bundles — no browser-only
15
+ * globals, no worker file, no native canvas — so it reads the same in the
16
+ * extension bundle, the IDE sidecar and on the dev host. It replaced
17
+ * pdf-parse, whose v2 needed a DOMMatrix stand-in and a worker file copied
18
+ * beside every bundle.
19
+ */
20
+ export async function readPdfText(data) {
21
+ const { getDocumentProxy, extractText } = await import('unpdf');
22
+ // A fresh copy: pdf.js takes ownership of (detaches) the buffer it is given.
23
+ const pdf = await getDocumentProxy(new Uint8Array(data));
24
+ try {
25
+ const r = await extractText(pdf, { mergePages: true });
26
+ return { text: r.text, pages: r.totalPages };
27
+ }
28
+ finally {
29
+ try {
30
+ await pdf.destroy?.();
31
+ }
32
+ catch { /* best effort */ }
33
+ }
34
+ }
35
+ export async function readDocxText(path) {
36
+ const mod = await import('mammoth');
37
+ const mammoth = mod.extractRawText ? mod : mod.default;
38
+ if (!mammoth?.extractRawText)
39
+ throw new Error('The Word reader is not available in this build.');
40
+ const r = await mammoth.extractRawText({ path });
41
+ return { text: String(r?.value ?? '') };
42
+ }
43
+ /** A plain sentence for a reader that failed — what happened, not how to install it. */
44
+ export function unreadable(kind, file, err) {
45
+ const why = err instanceof Error ? err.message : String(err);
46
+ return `I could not read the text of this ${kind} file (${file}): ${why}. If it is a scanned image, there is no text in it to read.`;
47
+ }
48
+ //# sourceMappingURL=read-office.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-office.js","sourceRoot":"","sources":["../../../src/tools/authoring/read-office.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAUH;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAAgB;IAChD,MAAM,EAAE,gBAAgB,EAAE,WAAW,EAAE,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC;IAChE,6EAA6E;IAC7E,MAAM,GAAG,GAAG,MAAM,gBAAgB,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,MAAM,WAAW,CAAC,GAAG,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC;IAC/C,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YAAC,MAAO,GAAyC,CAAC,OAAO,EAAE,EAAE,CAAC;QAAC,CAAC;QAAC,MAAM,CAAC,CAAC,iBAAiB,CAAC,CAAC;IACnG,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,IAAY;IAC7C,MAAM,GAAG,GAAQ,MAAM,MAAM,CAAC,SAAS,CAAC,CAAC;IACzC,MAAM,OAAO,GAAG,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC;IACvD,IAAI,CAAC,OAAO,EAAE,cAAc;QAAE,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACjG,MAAM,CAAC,GAAG,MAAM,OAAO,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IACjD,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,EAAE,KAAK,IAAI,EAAE,CAAC,EAAE,CAAC;AAC1C,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,UAAU,CAAC,IAAoB,EAAE,IAAY,EAAE,GAAY;IACzE,MAAM,GAAG,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7D,OAAO,qCAAqC,IAAI,UAAU,IAAI,MAAM,GAAG,6DAA6D,CAAC;AACvI,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"render-pdf.d.ts","sourceRoot":"","sources":["../../../src/tools/authoring/render-pdf.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAiB,QAAQ,EAAY,MAAM,gBAAgB,CAAC;AAWxE,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAWD,wBAAsB,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,CAkDxF"}
1
+ {"version":3,"file":"render-pdf.d.ts","sourceRoot":"","sources":["../../../src/tools/authoring/render-pdf.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAiB,QAAQ,EAAY,MAAM,gBAAgB,CAAC;AAYxE,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAWD,wBAAsB,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,CAoDxF"}
@@ -10,7 +10,8 @@
10
10
  import { readFile } from 'node:fs/promises';
11
11
  import { isAbsolute, resolve } from 'node:path';
12
12
  import { inlineToPlainText } from './doc-model.js';
13
- import { AVA_TITLE_SIZE_PDF, AVA_H1_SIZE_PDF, AVA_H2_SIZE_PDF, AVA_H3_SIZE_PDF, AVA_H4_SIZE_PDF, AVA_BODY_SIZE_PDF, AVA_SUBTITLE_SIZE_PDF, AVA_LINK_COLOR, CALLOUT_COLORS, resolveBrand, getStyleProfile, pdfTable, } from '../document-styling.js';
13
+ import { installPdfFonts, isMostlyRtl } from './pdf-fonts.js';
14
+ import { AVA_TITLE_SIZE_PDF, AVA_H1_SIZE_PDF, AVA_H2_SIZE_PDF, AVA_H3_SIZE_PDF, AVA_H4_SIZE_PDF, AVA_BODY_SIZE_PDF, AVA_SUBTITLE_SIZE_PDF, AVA_LINK_COLOR, CALLOUT_COLORS, resolveBrand, getStyleProfile, } from '../document-styling.js';
14
15
  const H_SIZE_PDF = [0, AVA_H1_SIZE_PDF, AVA_H2_SIZE_PDF, AVA_H3_SIZE_PDF, AVA_H4_SIZE_PDF];
15
16
  export async function renderPdf(model, opts) {
16
17
  const pdfkit = await opts.loadPdf();
@@ -35,6 +36,8 @@ export async function renderPdf(model, opts) {
35
36
  doc.on('data', (c) => chunks.push(c));
36
37
  doc.on('end', () => resolvePromise(Buffer.concat(chunks)));
37
38
  const ctx = { doc, brand, footnoteNumbers, baseDir: opts.baseDir };
39
+ // Fonts for every language the document uses (Noto; see pdf-fonts.ts).
40
+ installPdfFonts(doc, documentText(model));
38
41
  // Title / subtitle / meta.
39
42
  if (model.meta.title) {
40
43
  doc.font('Helvetica-Bold').fontSize(AVA_TITLE_SIZE_PDF).fillColor(`#${brand.headingColor}`).text(model.meta.title, { align: 'center' });
@@ -75,10 +78,7 @@ function renderBlock(block, ctx, images, indent) {
75
78
  break;
76
79
  case 'table':
77
80
  doc.moveDown(0.2);
78
- pdfTable(doc, {
79
- headers: block.headers.map(h => inlineToPlainText(h)),
80
- rows: block.rows.map(r => r.map(c => inlineToPlainText(c))),
81
- });
81
+ renderTable(ctx, block);
82
82
  doc.moveDown(0.4);
83
83
  break;
84
84
  case 'blockquote': {
@@ -136,6 +136,102 @@ function renderList(items, ordered, ctx, images, indent) {
136
136
  });
137
137
  doc.moveDown(0.2);
138
138
  }
139
+ /**
140
+ * A table sized to its content (15 Nov 2026 quality pass: every column used to
141
+ * get the same width in fixed-height rows, so a long description wrapped over
142
+ * the row below, numbers sat on the left and a bold total was not bold).
143
+ * Columns get their natural width, the wide ones share what is left; rows grow
144
+ * to fit; alignment follows the Markdown (numbers right when unmarked); bold
145
+ * cells stay bold; a table that runs off the page carries on with its header.
146
+ */
147
+ function renderTable(ctx, block) {
148
+ const { doc, brand } = ctx;
149
+ const size = AVA_BODY_SIZE_PDF;
150
+ const padX = 6, padY = 5;
151
+ const usable = usableWidth(doc);
152
+ const n = Math.max(block.headers.length, ...block.rows.map((r) => r.length), 1);
153
+ const text = (cells, i) => (cells?.[i] ? inlineToPlainText(cells[i]) : '');
154
+ const isBold = (cells, i) => !!cells?.[i]?.some((x) => x.type === 'strong');
155
+ const numeric = (s) => /^[\s£$€¥₹%+\-.,\d()]+$/.test(s) && /\d/.test(s);
156
+ const align = Array.from({ length: n }, (_, i) => {
157
+ const a = block.align?.[i];
158
+ if (a === 'right' || a === 'center' || a === 'left')
159
+ return a;
160
+ const body = block.rows.map((r) => text(r, i)).filter(Boolean);
161
+ return body.length && body.every(numeric) ? 'right' : 'left';
162
+ });
163
+ // Natural widths, measured in the fonts they will be drawn in.
164
+ doc.fontSize(size);
165
+ const natural = Array.from({ length: n }, (_, i) => {
166
+ doc.font('Helvetica-Bold');
167
+ let w = doc.widthOfString(text(block.headers, i));
168
+ for (const r of block.rows) {
169
+ doc.font(isBold(r, i) ? 'Helvetica-Bold' : 'Helvetica');
170
+ w = Math.max(w, doc.widthOfString(text(r, i)));
171
+ }
172
+ return Math.max(w + padX * 2 + 2, 36);
173
+ });
174
+ const total = natural.reduce((a, b) => a + b, 0);
175
+ let widths;
176
+ if (total <= usable) {
177
+ // Room to spare: share it out by size, so the table spans the page.
178
+ widths = natural.map((w) => w + ((usable - total) * w) / total);
179
+ }
180
+ else {
181
+ // Too wide: narrow columns keep their width, wide ones share the rest.
182
+ const fair = usable / n;
183
+ const small = natural.map((w) => w <= fair);
184
+ const fixed = natural.reduce((a, w, i) => a + (small[i] ? w : 0), 0);
185
+ const bigTotal = natural.reduce((a, w, i) => a + (small[i] ? 0 : w), 0) || 1;
186
+ widths = natural.map((w, i) => (small[i] ? w : ((usable - fixed) * w) / bigTotal));
187
+ }
188
+ const x0 = doc.page.margins.left;
189
+ const bottom = () => doc.page.height - doc.page.margins.bottom;
190
+ const heightOf = (cells, bold) => Math.max(...widths.map((w, i) => {
191
+ doc.font(bold(i) ? 'Helvetica-Bold' : 'Helvetica').fontSize(size);
192
+ return doc.heightOfString(text(cells, i) || ' ', { width: w - padX * 2 });
193
+ })) + padY * 2;
194
+ const drawRow = (cells, y, h, head, shade) => {
195
+ if (head)
196
+ doc.save().rect(x0, y, usable, h).fill(`#${brand.headingColor}`).restore();
197
+ else if (shade)
198
+ doc.save().rect(x0, y, usable, h).fill('#F5F3FF').restore();
199
+ let x = x0;
200
+ widths.forEach((w, i) => {
201
+ doc.font(head || isBold(cells, i) ? 'Helvetica-Bold' : 'Helvetica').fontSize(size).fillColor(head ? '#FFFFFF' : `#${brand.bodyColor}`);
202
+ doc.text(text(cells, i), x + padX, y + padY, { width: w - padX * 2, align: head ? (align[i] === 'right' ? 'right' : 'left') : align[i] });
203
+ x += w;
204
+ });
205
+ if (!head)
206
+ doc.save().strokeColor('#D0D0D0').lineWidth(0.5).moveTo(x0, y + h).lineTo(x0 + usable, y + h).stroke().restore();
207
+ };
208
+ const headH = heightOf(block.headers, () => true);
209
+ let y = doc.y;
210
+ if (y + headH * 2 > bottom()) {
211
+ doc.addPage();
212
+ y = doc.y;
213
+ }
214
+ if (block.headers.length) {
215
+ drawRow(block.headers, y, headH, true, false);
216
+ y += headH;
217
+ }
218
+ block.rows.forEach((row, r) => {
219
+ const h = heightOf(row, (i) => isBold(row, i));
220
+ if (y + h > bottom()) {
221
+ doc.addPage();
222
+ y = doc.y;
223
+ if (block.headers.length) {
224
+ drawRow(block.headers, y, headH, true, false);
225
+ y += headH;
226
+ }
227
+ }
228
+ drawRow(row, y, h, false, r % 2 === 1);
229
+ y += h;
230
+ });
231
+ doc.x = x0;
232
+ doc.y = y + 6;
233
+ doc.fillColor(`#${brand.bodyColor}`);
234
+ }
139
235
  function renderCallout(block, ctx, images, indent) {
140
236
  const { doc } = ctx;
141
237
  const pal = CALLOUT_COLORS[block.variant] ?? CALLOUT_COLORS.note;
@@ -205,6 +301,8 @@ function pdfInline(ctx, inlines, opts) {
205
301
  }
206
302
  const size = opts?.size ?? AVA_BODY_SIZE_PDF;
207
303
  const baseColor = opts?.color ?? ctx.brand.bodyColor;
304
+ // An Arabic paragraph reads from the right.
305
+ const align = opts?.align ?? (isMostlyRtl(segs.map((x) => x.text).join('')) ? 'right' : undefined);
208
306
  segs.forEach((s, i) => {
209
307
  const last = i === segs.length - 1;
210
308
  doc.font(s.code ? 'Courier' : pdfFont(!!opts?.bold || !!s.bold, !!s.italic))
@@ -213,8 +311,8 @@ function pdfInline(ctx, inlines, opts) {
213
311
  const o = { continued: !last };
214
312
  if (opts?.indent != null && i === 0)
215
313
  o.indent = opts.indent;
216
- if (opts?.align && i === 0)
217
- o.align = opts.align;
314
+ if (align && i === 0)
315
+ o.align = align;
218
316
  if (s.link) {
219
317
  o.link = s.link;
220
318
  o.underline = true;
@@ -258,6 +356,37 @@ function flattenInline(nodes, ctx, bold = false, italic = false, link, out = [])
258
356
  return out;
259
357
  }
260
358
  // ── Helpers ──────────────────────────────────────────────────────────────────
359
+ /** All the words in a document, to know which scripts' fonts it needs. */
360
+ function documentText(model) {
361
+ const parts = [model.meta.title ?? '', model.meta.subtitle ?? '', model.meta.author ?? ''];
362
+ const walk = (blocks) => {
363
+ for (const b of blocks) {
364
+ if (b.type === 'heading' || b.type === 'paragraph')
365
+ parts.push(inlineToPlainText(b.inlines));
366
+ else if (b.type === 'table') {
367
+ b.headers.forEach((h) => parts.push(inlineToPlainText(h)));
368
+ b.rows.forEach((r) => r.forEach((c) => parts.push(inlineToPlainText(c))));
369
+ }
370
+ else if (b.type === 'list')
371
+ for (const it of b.items) {
372
+ parts.push(inlineToPlainText(it.inlines));
373
+ if (it.children)
374
+ walk(it.children);
375
+ }
376
+ else if (b.type === 'blockquote' || b.type === 'callout') {
377
+ if (b.type === 'callout' && b.title)
378
+ parts.push(b.title);
379
+ walk(b.blocks);
380
+ }
381
+ else if (b.type === 'code')
382
+ parts.push(b.text);
383
+ }
384
+ };
385
+ walk(model.blocks);
386
+ for (const f of Object.values(model.meta.footnotes ?? {}))
387
+ parts.push(inlineToPlainText(f));
388
+ return parts.join(' ');
389
+ }
261
390
  function pdfFont(bold, italic) {
262
391
  if (bold && italic)
263
392
  return 'Helvetica-BoldOblique';