@pao-at-office/reports-web 1.3.0 → 1.3.1

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.
@@ -0,0 +1,315 @@
1
+ /**
2
+ * 書体の寸法の表(1.3.0、2026-11-13)。基準の実装 Pao.Reports.Maui の KnownFamilyMetrics(Engine/Fonts/known-family-metrics.txt)と
3
+ * 同じ出所の数を、tools/fonts/build_known_family_metrics_ts.py で写した物(knownFamilyMetrics.generated.ts)。Web だけの手書きの数にしない。
4
+ * 端末の書体(同梱の Noto)に依らず、配置(行送り・基線・字の高さの画素数・字形の有無)は「指定した書体」のこの寸法で決める。
5
+ */
6
+ import { knownFamilyEntries } from './knownFamilyMetrics.generated.js';
7
+ /**
8
+ * 決まり YEN:見た目だけ「\」を ¥ に置き換えた文字(textLayout.displayText)の配置の寸法の書体名に付ける印。
9
+ * この印の付いた書体名の寸法(findKnownFamily)は、表示の ¥(U+00A5)を入力の「\」(U+005C)の字として数える(送り・ヒンティング後の送り・字の組の詰め・
10
+ * 縦の送り・字形の有無)。MS Pゴシック・MS P明朝・MS UI Gothic は「\」の送りが ¥ と違う(1032 対 1208 ほか)ので、Reports.net と同じ「\」の送りで並べるため。
11
+ * 物体の入力に ¥ が無いとき(表示の ¥ が全部入力の「\」)だけ使う(textLayout.sourceFamily)。
12
+ */
13
+ export const YEN_METRICS_MARK = '\u0000yen';
14
+ /** 決まり YEN:配置で数える字(印の付いた書体名なら表示の ¥ を「\」に戻す)。 */
15
+ export function metricCodePoint(family, codePoint) {
16
+ return codePoint === 0xa5 && family?.endsWith(YEN_METRICS_MARK) ? 0x5c : codePoint;
17
+ }
18
+ const yenMetrics = new WeakMap();
19
+ function yenMetricsOf(base) {
20
+ let found = yenMetrics.get(base);
21
+ if (!found) {
22
+ const m = (codePoint) => codePoint === 0xa5 ? 0x5c : codePoint;
23
+ found = { ...base,
24
+ advanceUnits: (codePoint, bold, italic) => base.advanceUnits(m(codePoint), bold, italic),
25
+ hintedPixels: (codePoint, emPixels, bold, italic) => base.hintedPixels(m(codePoint), emPixels, bold, italic),
26
+ kernUnits: (first, second, bold, italic) => base.kernUnits(m(first), m(second), bold, italic),
27
+ verticalAdvanceUnits: (codePoint, bold) => base.verticalAdvanceUnits(m(codePoint), bold),
28
+ verticalHintedPixels: (codePoint, emPixels, bold) => base.verticalHintedPixels(m(codePoint), emPixels, bold),
29
+ hasGlyph: codePoint => base.hasGlyph(m(codePoint)),
30
+ };
31
+ yenMetrics.set(base, found);
32
+ }
33
+ return found;
34
+ }
35
+ /** 書体名を表の鍵の形にする(Maui GdiFallbackMetrics.FamilyKey と同じ:NFKC、小文字、空白・「_」「-」を除く)。 */
36
+ export function knownFamilyKey(family) {
37
+ return String(family ?? '').normalize('NFKC').toLowerCase().replace(/[\s_-]/g, '');
38
+ }
39
+ /** 字の高さの画素数(VDMX が無い書体の式。Maui LayoutRules.FontHeightPixels と同じ)。 */
40
+ /**
41
+ * 決まり R12:横倒しの字の「字の高さ」の画素数。xMinEm・xMaxEm=書体全体の外接(head)の左・右(em)、emPixels=1 em の画素数(丸めない)。
42
+ * C(xMax×p)+C(−xMin×p)、C(v)=ceil(v を 1/16 画素に切り捨てた値)。右と左を別々に切り捨ててから切り上げる(1 つの数として丸めると境目がずれる)。
43
+ * Maui LayoutRules.SidewaysCellPixels と同じ(GDI+ 直:16 書体 × 1 em=6〜110 画素で確かめた)。glyphRules からも出す。
44
+ */
45
+ export function sidewaysCellPixels(xMinEm, xMaxEm, emPixels) {
46
+ const c = (v) => Math.ceil(Math.floor(v * 16 + 1e-9) / 16 - 1e-12);
47
+ return c(xMaxEm * emPixels) + c(-xMinEm * emPixels);
48
+ }
49
+ export function fontHeightPixelsFromMetrics(ascentEm, descentEm, emPixels) {
50
+ return Math.floor(ascentEm * emPixels + .5) + Math.floor(descentEm * emPixels + .5);
51
+ }
52
+ const signed36 = (value) => value.startsWith('-') ? -parseInt(value.slice(1), 36) : parseInt(value, 36);
53
+ function runsInto(line, target) {
54
+ for (const run of line.split(',')) {
55
+ if (!run)
56
+ continue;
57
+ const [start, count, value] = run.split('.').map(v => parseInt(v, 36));
58
+ for (let i = 0; i < count; i++)
59
+ target.set(start + i, value);
60
+ }
61
+ }
62
+ function readGlyphTables(entry) {
63
+ const advances = new Map(), bold = new Map(), parts = entry.advance.split('|');
64
+ const common = parts[0] ? Number(parts[0]) : 0;
65
+ if (parts[0]) {
66
+ if (parts.length >= 2)
67
+ runsInto(parts[1], advances);
68
+ runsInto(entry.bold, bold);
69
+ }
70
+ const hintedKeys = new Map(), hintedDeltas = new Map();
71
+ for (const group of entry.hinted.split(';')) {
72
+ if (!group)
73
+ continue;
74
+ const colon = group.indexOf(':'), bar = group.indexOf('|'), key = Number(group.slice(0, colon)), defaults = group.slice(colon + 1, bar).split('.');
75
+ hintedKeys.set(key, [signed36(defaults[0]), signed36(defaults[1])]);
76
+ const deltas = new Map();
77
+ hintedDeltas.set(key, deltas);
78
+ for (const item of group.slice(bar + 1).split(',')) {
79
+ if (!item)
80
+ continue;
81
+ const q = item.split('.'), start = parseInt(q[0], 36), count = q.length === 3 ? parseInt(q[1], 36) : 1, delta = signed36(q[q.length - 1]);
82
+ for (let c = 0; c < count; c++)
83
+ deltas.set(start + c, delta);
84
+ }
85
+ }
86
+ const kern = new Map();
87
+ for (const group of entry.kern.split(';')) {
88
+ if (!group)
89
+ continue;
90
+ const colon = group.indexOf(':'), first = parseInt(group.slice(0, colon), 36), inner = new Map();
91
+ kern.set(first, inner);
92
+ for (const item of group.slice(colon + 1).split(',')) {
93
+ if (!item)
94
+ continue;
95
+ const dot = item.indexOf('.');
96
+ inner.set(parseInt(item.slice(0, dot), 36), signed36(item.slice(dot + 1)));
97
+ }
98
+ }
99
+ const vertical = new Map();
100
+ runsInto(entry.vertical, vertical);
101
+ return { common, advances, bold, hintedKeys, hintedDeltas, kern, vertical };
102
+ }
103
+ function build(entry) {
104
+ const vdmx = new Map();
105
+ for (const pair of entry.vdmx.split(',').filter(Boolean)) {
106
+ const [ppem, height] = pair.split(':').map(Number);
107
+ vdmx.set(ppem, height);
108
+ }
109
+ const runs = entry.glyphRuns.split(',').filter(Boolean).map(run => { const [start, count] = run.split('.').map(v => parseInt(v, 36)); return [start, start + count]; });
110
+ const ascentEm = entry.ascent / 2048, descentEm = entry.descent / 2048, leadingEm = entry.leading / 2048;
111
+ const headBox = entry.headBox.split(' ').filter(Boolean).map(Number);
112
+ const [headXMin, headXMax, headUnitsPerEm] = headBox.length === 3 ? headBox : [0, 0, 0];
113
+ let tables;
114
+ const glyph = () => tables ??= readGlyphTables(entry);
115
+ const flags = entry.styleFlags.trim().split(' '), hasItalicFace = flags.length === 2 && flags[0] === '1', hasBoldItalicFace = flags.length === 2 && flags[1] === '1';
116
+ const styleFace = (bold, italic) => bold && italic && hasBoldItalicFace ? 'J' : italic && !bold && hasItalicFace ? 'I' : bold && entry.hasBoldFace ? 'B' : 'R';
117
+ let styleAdvances;
118
+ const styleKerns = new Map();
119
+ const advanceUnits = (codePoint, bold, italic = false) => {
120
+ if (codePoint < 0x20)
121
+ return 0;
122
+ const t = glyph();
123
+ if (italic) {
124
+ const face = styleFace(bold, true);
125
+ if (face === 'I' || face === 'J') {
126
+ if (!styleAdvances) {
127
+ styleAdvances = { I: new Map(), J: new Map() };
128
+ runsInto(entry.italic, styleAdvances.I);
129
+ runsInto(entry.boldItalic, styleAdvances.J);
130
+ }
131
+ const v = styleAdvances[face].get(codePoint);
132
+ if (v !== undefined)
133
+ return v;
134
+ bold = false;
135
+ }
136
+ }
137
+ if (bold) {
138
+ const b = t.bold.get(codePoint);
139
+ if (b !== undefined)
140
+ return b;
141
+ }
142
+ return t.advances.get(codePoint) ?? (t.common > 0 ? t.common : -1);
143
+ };
144
+ return {
145
+ english: entry.english, ascentEm, descentEm, leadingEm, lineEm: ascentEm + descentEm + leadingEm,
146
+ fixed: entry.fixed, hasBoldFace: entry.hasBoldFace, syntheticBoldUnits: entry.syntheticBold, syntheticBoldBefore: entry.syntheticBoldBefore,
147
+ // 1.3.0(2026-11-14):字の高さ=max(VDMX の四捨五入した画素数の行, round(上×p)+round(下×p))。VDMX は文字セット 1 の組だけ(表の V 行)。
148
+ // Maui KnownFamilyMetrics.FontHeightPixels と同じ。以前は VDMX の行があればそのまま使い、式より小さい行(Verdana 10pt の 600dpi など)で小さな字と数えた。
149
+ fontHeightPixels: emPixels => {
150
+ const formula = fontHeightPixelsFromMetrics(ascentEm, descentEm, emPixels), height = vdmx.get(Math.floor(emPixels + .5));
151
+ return height === undefined ? formula : Math.max(height, formula);
152
+ },
153
+ advanceUnits,
154
+ styleFace,
155
+ hintedPixels: (codePoint, emPixels, bold, italic = false) => {
156
+ // 決まり ST:太字・斜体の面は差を持たない(round(u×P÷2048))。
157
+ const styled = bold || italic && (styleFace(bold, true) === 'I' || styleFace(bold, true) === 'J');
158
+ const units = advanceUnits(codePoint, bold, italic);
159
+ if (units < 0)
160
+ return -1;
161
+ const t = glyph();
162
+ let key = Math.floor(emPixels * 12 + .5), p;
163
+ if (Math.abs(emPixels * 12 - key) < 1e-3 && t.hintedKeys.has(key))
164
+ p = key / 12;
165
+ else {
166
+ const whole = Math.ceil(emPixels - .5 - 1e-4);
167
+ key = whole * 12;
168
+ p = whole;
169
+ }
170
+ const value = Math.floor(units * p / 2048 + .5);
171
+ const defaults = styled ? undefined : t.hintedKeys.get(key);
172
+ if (!defaults)
173
+ return value;
174
+ const delta = t.hintedDeltas.get(key)?.get(codePoint);
175
+ if (delta !== undefined)
176
+ return value + delta;
177
+ return value + (t.common > 0 && units === t.common ? defaults[1] : defaults[0]);
178
+ },
179
+ kernUnits: (first, second, bold = false, italic = false) => {
180
+ const face = styleFace(bold, italic);
181
+ if (face === 'R')
182
+ return glyph().kern.get(first)?.get(second) ?? 0;
183
+ let table = styleKerns.get(face);
184
+ if (!table) {
185
+ // KB・KI・KJ 行:「+」で始まる行は K 行と違う組だけ(0=詰めない)、ほかは全部の組。
186
+ const line = face === 'B' ? entry.kernBold : face === 'I' ? entry.kernItalic : entry.kernBoldItalic;
187
+ table = new Map();
188
+ if (line.startsWith('+'))
189
+ for (const [a, inner] of glyph().kern)
190
+ table.set(a, new Map(inner));
191
+ for (const group of (line.startsWith('+') ? line.slice(1) : line).split(';')) {
192
+ if (!group)
193
+ continue;
194
+ const colon = group.indexOf(':'), a = parseInt(group.slice(0, colon), 36);
195
+ let inner = table.get(a);
196
+ if (!inner) {
197
+ inner = new Map();
198
+ table.set(a, inner);
199
+ }
200
+ for (const item of group.slice(colon + 1).split(',')) {
201
+ if (!item)
202
+ continue;
203
+ const dot = item.indexOf('.');
204
+ inner.set(parseInt(item.slice(0, dot), 36), signed36(item.slice(dot + 1)));
205
+ }
206
+ }
207
+ styleKerns.set(face, table);
208
+ }
209
+ return table.get(first)?.get(second) ?? 0;
210
+ },
211
+ verticalOriginEm: entry.verticalOrigin > 0 ? entry.verticalOrigin / 2048 : Math.min(ascentEm, .88),
212
+ verticalAdvanceUnits: (codePoint, bold) => (bold ? undefined : glyph().vertical.get(codePoint)) ?? advanceUnits(codePoint, bold),
213
+ verticalHintedPixels(codePoint, emPixels, bold) {
214
+ if (bold)
215
+ return this.hintedPixels(codePoint, emPixels, bold);
216
+ const units = this.verticalAdvanceUnits(codePoint, false);
217
+ if (units < 0)
218
+ return -1;
219
+ const whole = Math.ceil(emPixels - .5 - 1e-4), n = Math.ceil(units * whole / 32 - .5 - 1e-9);
220
+ return (n + 31) >> 6;
221
+ },
222
+ sidewaysCellPixels: emPixels => headUnitsPerEm > 0 ? sidewaysCellPixels(headXMin / headUnitsPerEm, headXMax / headUnitsPerEm, emPixels) : 0,
223
+ unitsPerEm: headUnitsPerEm > 0 ? headUnitsPerEm : 2048,
224
+ yenBackslash: entry.yenBackslash,
225
+ hasGlyph: codePoint => {
226
+ if (codePoint < 0x20 || entry.allGlyphs)
227
+ return true;
228
+ if (codePoint >= 0x2e80)
229
+ return false;
230
+ let low = 0, high = runs.length - 1;
231
+ while (low <= high) {
232
+ const mid = (low + high) >> 1, [start, end] = runs[mid];
233
+ if (codePoint < start)
234
+ high = mid - 1;
235
+ else if (codePoint >= end)
236
+ low = mid + 1;
237
+ else
238
+ return true;
239
+ }
240
+ return false;
241
+ },
242
+ };
243
+ }
244
+ const LINKED_WESTERN = new Set(['arial', 'timesnewroman', 'couriernew', 'microsoftsansserif', 'tahoma', 'verdana', 'segoeui']);
245
+ export function linkedFallback(known) {
246
+ if (!known)
247
+ return undefined;
248
+ const key = knownFamilyKey(known.english);
249
+ if (!LINKED_WESTERN.has(key))
250
+ return undefined;
251
+ if (key === 'segoeui') {
252
+ const metrics = findKnownFamily('Meiryo UI');
253
+ return metrics && { metrics, scale: 2724 / 2601 };
254
+ }
255
+ const metrics = findKnownFamily('MS UI Gothic');
256
+ return metrics && { metrics, scale: 1.1 };
257
+ }
258
+ /**
259
+ * 決まり FB1:折り返し(行・列の割り付け)に数える代わりの字の+3% の送り(指定した書体の 1/2048 em の整数)=floor((u+(3u+50)÷100 の整数部)×s)。
260
+ * 太字でも合成太字の量を足さない。立てて置く字は縦の送り。切り捨ては FB1V の短い枠(11mm に 12pt の「国」2 字=2×2319.9 単位が入る)で測った形
261
+ * (GDI+ が指定した書体の単位に直すときの切り捨てと見ている。推測)。字の位置は切り捨てない送り(glyphRules.placement)。
262
+ */
263
+ export function linkedNominalUnits(link, codePoint, upright = false) {
264
+ const u = upright ? link.metrics.verticalAdvanceUnits(codePoint, false) : link.metrics.advanceUnits(codePoint, false);
265
+ return Math.floor((u + Math.floor((u * 3 + 50) / 100)) * link.scale + 1e-9);
266
+ }
267
+ let table;
268
+ const lookups = new Map();
269
+ /** 名前が表にある書体の寸法。無ければ undefined。 */
270
+ export function findKnownFamily(family) {
271
+ if (!table) {
272
+ table = new Map();
273
+ for (const entry of knownFamilyEntries) {
274
+ const metrics = build(entry);
275
+ for (const key of entry.keys)
276
+ table.set(knownFamilyKey(key), metrics);
277
+ table.set(knownFamilyKey(entry.english), metrics);
278
+ }
279
+ }
280
+ // 性能(2026-11-18 REVIEW-WJ 軽い 9):字ごとに呼ばれる(textLayout.linkedCharacterUnits・linkedGlyphScale)ので、書体名ごとの引き当て
281
+ // (NFKC と正規表現の knownFamilyKey)を覚える。表は作った後は変わらないので、答えは同じ。
282
+ const name = family ?? '';
283
+ if (name.endsWith(YEN_METRICS_MARK)) {
284
+ const base = findKnownFamily(name.slice(0, -YEN_METRICS_MARK.length));
285
+ return base && yenMetricsOf(base);
286
+ }
287
+ if (lookups.has(name))
288
+ return lookups.get(name);
289
+ const key = knownFamilyKey(family);
290
+ const found = key ? table.get(key) : undefined;
291
+ if (lookups.size >= 256)
292
+ lookups.clear();
293
+ lookups.set(name, found);
294
+ return found;
295
+ }
296
+ /** 決まり BD1:合成太字を足さない字(空白。字形の輪郭が無い)。Java Character.isWhitespace・isSpaceChar、Python str.isspace と同じ範囲の主な字。 */
297
+ export function isBoldBlank(codePoint) { return codePoint <= 0xffff && /\s/u.test(String.fromCodePoint(codePoint)); }
298
+ /** 決まり BD1:太字を GDI+ が合成する(太字の面が無く、合成太字の量がある)書体か。 */
299
+ export function syntheticBold(known, bold) { return bold && !known.hasBoldFace && (known.syntheticBoldUnits > 0 || known.syntheticBoldBefore > 0); }
300
+ /** 決まり BD1:この字に足す合成太字の前の量(空白は足さない)。 */
301
+ export function boldBefore(known, codePoint, bold) { return syntheticBold(known, bold) && !isBoldBlank(codePoint) ? known.syntheticBoldBefore : 0; }
302
+ /** 決まり BD1:この字に足す合成太字の後の量(MS の書体の 5。空白は U+0020 だけ足し、ほかの空白は足さない。GDI+ 直、em 4096 画素で測った)。 */
303
+ export function boldAfter(known, codePoint, bold) { return syntheticBold(known, bold) && (!isBoldBlank(codePoint) || codePoint === 0x20) ? known.syntheticBoldUnits : 0; }
304
+ /** 決まり BD1:+3% の送り(1/2048 em)。合成太字は、送りに前の量を足してから+3%、その後に後の量を足す(Java boldNominalUnits と同じ)。 */
305
+ export function boldNominalUnits(known, codePoint, units, bold) {
306
+ const b = units + boldBefore(known, codePoint, bold);
307
+ return b + Math.floor((b * 3 + 50) / 100) + boldAfter(known, codePoint, bold);
308
+ }
309
+ /** 決まり BD1:合成太字の横書きのヒンティング後の送り=標準の送り+1 画素(合成の量を足さない字は足さない)。 */
310
+ export function boldHintedPixels(known, codePoint, emPixels, bold, italic = false) {
311
+ if (!boldBefore(known, codePoint, bold) && !boldAfter(known, codePoint, bold))
312
+ return known.hintedPixels(codePoint, emPixels, bold, italic);
313
+ const value = known.hintedPixels(codePoint, emPixels, false);
314
+ return value < 0 ? value : value + 1;
315
+ }
package/package.json CHANGED
@@ -1,57 +1,58 @@
1
- {
2
- "name": "@pao-at-office/reports-web",
3
- "version": "1.3.0",
4
- "description": "Reports.Web for Node.js: build PREPEJ print data from report definitions (.prepdj) and render PDF with the Reports.Web engine (C++ WebAssembly). Japanese business forms such as invoices, estimates and labels.",
5
- "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.js"
12
- }
13
- },
14
- "files": [
15
- "dist/*.js",
16
- "dist/*.d.ts",
17
- "README.md",
18
- "LICENSE.md"
19
- ],
20
- "engines": {
21
- "node": ">=18"
22
- },
23
- "scripts": {
24
- "build": "tsc",
25
- "test": "npm run build && node --test test/engine.test.mjs"
26
- },
27
- "keywords": [
28
- "reports",
29
- "report",
30
- "pdf",
31
- "pdf-generation",
32
- "report-designer",
33
- "invoice",
34
- "estimate",
35
- "label",
36
- "business-forms",
37
- "webassembly",
38
- "wasm",
39
- "prepej",
40
- "prepdj",
41
- "japanese",
42
- "帳票",
43
- "Reports.Web",
44
- "Reports Web"
45
- ],
46
- "homepage": "https://www.pao.ac/reports.web/",
47
- "bugs": {
48
- "url": "https://www.pao.ac/reports.web/",
49
- "email": "info@pao.ac"
50
- },
51
- "author": "Pao@Office <info@pao.ac> (https://www.pao.ac/)",
52
- "license": "SEE LICENSE IN LICENSE.md",
53
- "devDependencies": {
54
- "@types/node": "22.18.0",
55
- "typescript": "5.9.2"
56
- }
57
- }
1
+ {
2
+ "name": "@pao-at-office/reports-web",
3
+ "version": "1.3.1",
4
+ "description": "Reports.Web for Node.js: build PREPEJ print data from report definitions (.prepdj) and render PDF with the Reports.Web engine (C++ WebAssembly). Japanese business forms such as invoices, estimates and labels.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": { "reports-web": "./dist/cli.js" },
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist/*.js",
17
+ "dist/*.d.ts",
18
+ "README.md",
19
+ "LICENSE.md"
20
+ ],
21
+ "engines": {
22
+ "node": ">=18"
23
+ },
24
+ "scripts": {
25
+ "build": "node scripts/prepare-lint.mjs && tsc",
26
+ "test": "npm run build && node --test --test-concurrency=2 test/*.test.mjs"
27
+ },
28
+ "keywords": [
29
+ "reports",
30
+ "report",
31
+ "pdf",
32
+ "pdf-generation",
33
+ "report-designer",
34
+ "invoice",
35
+ "estimate",
36
+ "label",
37
+ "business-forms",
38
+ "webassembly",
39
+ "wasm",
40
+ "prepej",
41
+ "prepdj",
42
+ "japanese",
43
+ "帳票",
44
+ "Reports.Web",
45
+ "Reports.net"
46
+ ],
47
+ "homepage": "https://www.pao.ac/reports.web/",
48
+ "bugs": {
49
+ "url": "https://www.pao.ac/reports.web/",
50
+ "email": "info@pao.ac"
51
+ },
52
+ "author": "Pao@Office <info@pao.ac> (https://www.pao.ac/)",
53
+ "license": "SEE LICENSE IN LICENSE.md",
54
+ "devDependencies": {
55
+ "@types/node": "22.18.0",
56
+ "typescript": "5.9.2"
57
+ }
58
+ }