@aglyn/shared-util-tools 1.0.0-beta.289 → 1.0.0-beta.290
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.
- package/README.md +4 -1
- package/package.json +2 -2
- package/src/lib/pdf/pdf-file.d.ts +65 -0
- package/src/lib/pdf/pdf-file.js +170 -0
- package/src/lib/pdf/pdf-file.js.map +1 -0
- package/src/lib/pdf/pdf-text.d.ts +42 -0
- package/src/lib/pdf/pdf-text.js +743 -0
- package/src/lib/pdf/pdf-text.js.map +1 -0
- package/src/lib/pdf/pdf-writer.d.ts +173 -0
- package/src/lib/pdf/pdf-writer.js +348 -0
- package/src/lib/pdf/pdf-writer.js.map +1 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ /*==========================================
|
|
17
|
+
* TEXT IN A HAND-WRITTEN PDF: the standard Helvetica faces in WinAnsi.
|
|
18
|
+
*
|
|
19
|
+
* The standard 14 fonts need no embedding, and every reader draws them, but
|
|
20
|
+
* they hold one byte per character: WinAnsi, which is Latin-1 plus a row of
|
|
21
|
+
* typographic marks (curly quotes, dashes, the bullet, the euro). Everything
|
|
22
|
+
* here turns arbitrary text into that byte set FIRST, so a string that
|
|
23
|
+
* reaches a content stream can never carry a byte the font has no glyph for:
|
|
24
|
+
*
|
|
25
|
+
* - a character WinAnsi holds is kept as it is;
|
|
26
|
+
* - a known look-alike is swapped in (`−` → `-`, `→` → `->`, `ł` → `l`,
|
|
27
|
+
* odd spaces → a space, zero-width marks → nothing);
|
|
28
|
+
* - an accented letter outside Latin-1 loses its accent (`ă` → `a`) and a
|
|
29
|
+
* compatibility form its styling (`fi` → `fi`, `A` → `A`);
|
|
30
|
+
* - emoji, pictographs and stray combining marks are dropped;
|
|
31
|
+
* - anything else (another script) prints `?`.
|
|
32
|
+
*
|
|
33
|
+
* Widths are Adobe's published metrics for Helvetica and Helvetica-Bold
|
|
34
|
+
* (Helvetica-Oblique shares Helvetica's), in thousandths of the font size,
|
|
35
|
+
* so text measured here right-aligns where a reader draws it.
|
|
36
|
+
*
|
|
37
|
+
* Reached as `@aglyn/shared-util-tools/pdf/pdf-text`; not re-exported from
|
|
38
|
+
* the package root.
|
|
39
|
+
*=========================================*/ /** The three faces a document may set text in. */ /** Unicode code points WinAnsi holds at 0x80–0x9F, and their bytes. */ const WIN_ANSI_HIGH = {
|
|
40
|
+
0x20ac: 0x80,
|
|
41
|
+
0x201a: 0x82,
|
|
42
|
+
0x0192: 0x83,
|
|
43
|
+
0x201e: 0x84,
|
|
44
|
+
0x2026: 0x85,
|
|
45
|
+
0x2020: 0x86,
|
|
46
|
+
0x2021: 0x87,
|
|
47
|
+
0x02c6: 0x88,
|
|
48
|
+
0x2030: 0x89,
|
|
49
|
+
0x0160: 0x8a,
|
|
50
|
+
0x2039: 0x8b,
|
|
51
|
+
0x0152: 0x8c,
|
|
52
|
+
0x017d: 0x8e,
|
|
53
|
+
0x2018: 0x91,
|
|
54
|
+
0x2019: 0x92,
|
|
55
|
+
0x201c: 0x93,
|
|
56
|
+
0x201d: 0x94,
|
|
57
|
+
0x2022: 0x95,
|
|
58
|
+
0x2013: 0x96,
|
|
59
|
+
0x2014: 0x97,
|
|
60
|
+
0x02dc: 0x98,
|
|
61
|
+
0x2122: 0x99,
|
|
62
|
+
0x0161: 0x9a,
|
|
63
|
+
0x203a: 0x9b,
|
|
64
|
+
0x0153: 0x9c,
|
|
65
|
+
0x017e: 0x9e,
|
|
66
|
+
0x0178: 0x9f
|
|
67
|
+
};
|
|
68
|
+
/** Characters WinAnsi lacks, read as the nearest text it has. */ const STAND_INS = {
|
|
69
|
+
'\t': ' ',
|
|
70
|
+
' ': ' ',
|
|
71
|
+
' ': ' ',
|
|
72
|
+
' ': ' ',
|
|
73
|
+
' ': ' ',
|
|
74
|
+
' ': ' ',
|
|
75
|
+
' ': ' ',
|
|
76
|
+
' ': ' ',
|
|
77
|
+
' ': ' ',
|
|
78
|
+
' ': ' ',
|
|
79
|
+
' ': ' ',
|
|
80
|
+
' ': ' ',
|
|
81
|
+
' ': ' ',
|
|
82
|
+
' ': ' ',
|
|
83
|
+
' ': ' ',
|
|
84
|
+
'': '',
|
|
85
|
+
'': '',
|
|
86
|
+
'': '',
|
|
87
|
+
'': '',
|
|
88
|
+
'': '',
|
|
89
|
+
'‐': '-',
|
|
90
|
+
'‑': '-',
|
|
91
|
+
'‒': '–',
|
|
92
|
+
'―': '—',
|
|
93
|
+
'−': '-',
|
|
94
|
+
'⁃': '-',
|
|
95
|
+
'′': "'",
|
|
96
|
+
'″': '"',
|
|
97
|
+
'‵': "'",
|
|
98
|
+
'‛': "'",
|
|
99
|
+
'‟': '"',
|
|
100
|
+
'ʼ': '’',
|
|
101
|
+
'ʹ': "'",
|
|
102
|
+
'‣': '•',
|
|
103
|
+
'●': '•',
|
|
104
|
+
'▪': '•',
|
|
105
|
+
'◦': 'o',
|
|
106
|
+
'○': 'o',
|
|
107
|
+
'∙': '·',
|
|
108
|
+
'⁄': '/',
|
|
109
|
+
'←': '<-',
|
|
110
|
+
'→': '->',
|
|
111
|
+
'↔': '<->',
|
|
112
|
+
'⇒': '=>',
|
|
113
|
+
'≤': '<=',
|
|
114
|
+
'≥': '>=',
|
|
115
|
+
'≠': '!=',
|
|
116
|
+
'≈': '~',
|
|
117
|
+
'№': 'No.',
|
|
118
|
+
'⅓': '1/3',
|
|
119
|
+
'⅔': '2/3',
|
|
120
|
+
'⅛': '1/8',
|
|
121
|
+
'⅜': '3/8',
|
|
122
|
+
'⅝': '5/8',
|
|
123
|
+
'⅞': '7/8',
|
|
124
|
+
'₹': 'Rs',
|
|
125
|
+
'₩': 'W',
|
|
126
|
+
'₪': 'NIS',
|
|
127
|
+
'₽': 'RUB',
|
|
128
|
+
'₱': 'PHP',
|
|
129
|
+
'₺': 'TL',
|
|
130
|
+
'₫': 'd',
|
|
131
|
+
'₦': 'N',
|
|
132
|
+
'₴': 'UAH',
|
|
133
|
+
'Ł': 'L',
|
|
134
|
+
'ł': 'l',
|
|
135
|
+
'Đ': 'D',
|
|
136
|
+
'đ': 'd',
|
|
137
|
+
'Ħ': 'H',
|
|
138
|
+
'ħ': 'h',
|
|
139
|
+
'ı': 'i',
|
|
140
|
+
'Ŀ': 'L',
|
|
141
|
+
'ŀ': 'l',
|
|
142
|
+
'Ŋ': 'N',
|
|
143
|
+
'ŋ': 'n',
|
|
144
|
+
'Ŧ': 'T',
|
|
145
|
+
'ŧ': 't',
|
|
146
|
+
'ſ': 's',
|
|
147
|
+
'IJ': 'IJ',
|
|
148
|
+
'ij': 'ij',
|
|
149
|
+
'ȷ': 'j',
|
|
150
|
+
'Ə': 'E',
|
|
151
|
+
'ə': 'e'
|
|
152
|
+
};
|
|
153
|
+
/** Whether WinAnsi holds this one character. */ function encodable(code) {
|
|
154
|
+
return code >= 32 && code <= 126 || code >= 0xa0 && code <= 0xff || WIN_ANSI_HIGH[code] !== undefined;
|
|
155
|
+
}
|
|
156
|
+
/** Marks with no glyph of their own in a text font: combining accents, variation selectors, emoji, private use. */ function dropped(code) {
|
|
157
|
+
return code >= 0x0300 && code <= 0x036f || code >= 0x1ab0 && code <= 0x1aff || code >= 0x20d0 && code <= 0x20ff || code >= 0xfe00 && code <= 0xfe0f || code >= 0xfe20 && code <= 0xfe2f || code >= 0x2600 && code <= 0x27bf || code >= 0x2b00 && code <= 0x2bff || code >= 0xe000 && code <= 0xf8ff || code >= 0x1f000 || code < 32 || code >= 0x7f && code < 0xa0;
|
|
158
|
+
}
|
|
159
|
+
/** One character as text WinAnsi holds, without the decomposition fallback. */ function direct(char) {
|
|
160
|
+
var _char_codePointAt;
|
|
161
|
+
const code = (_char_codePointAt = char.codePointAt(0)) != null ? _char_codePointAt : 0;
|
|
162
|
+
if (encodable(code)) return char;
|
|
163
|
+
const standIn = STAND_INS[char];
|
|
164
|
+
if (standIn !== undefined) return standIn;
|
|
165
|
+
if (dropped(code)) return '';
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* `text` as characters WinAnsi holds, line breaks kept (`\r\n` and `\r`
|
|
170
|
+
* become `\n`). Text that is already WinAnsi comes back unchanged.
|
|
171
|
+
*/ export function toWinAnsiText(text) {
|
|
172
|
+
const source = String(text != null ? text : '').replace(/\r\n?/g, '\n').normalize('NFC');
|
|
173
|
+
let out = '';
|
|
174
|
+
for (const char of source){
|
|
175
|
+
if (char === '\n') {
|
|
176
|
+
out += char;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const kept = direct(char);
|
|
180
|
+
if (kept !== null) {
|
|
181
|
+
out += kept;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
// An accented letter or a compatibility form: its parts, accents dropped.
|
|
185
|
+
let parts = '';
|
|
186
|
+
let readable = true;
|
|
187
|
+
for (const part of char.normalize('NFKD')){
|
|
188
|
+
const mapped = direct(part);
|
|
189
|
+
if (mapped === null) {
|
|
190
|
+
readable = false;
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
parts += mapped;
|
|
194
|
+
}
|
|
195
|
+
out += readable && parts ? parts : '?';
|
|
196
|
+
}
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
/** One character's WinAnsi byte, `?` (63) for a character WinAnsi does not hold. */ export function winAnsiByte(char) {
|
|
200
|
+
var _char_codePointAt, _WIN_ANSI_HIGH_code;
|
|
201
|
+
const code = (_char_codePointAt = char.codePointAt(0)) != null ? _char_codePointAt : 63;
|
|
202
|
+
if (code >= 32 && code <= 126 || code >= 0xa0 && code <= 0xff) return code;
|
|
203
|
+
return (_WIN_ANSI_HIGH_code = WIN_ANSI_HIGH[code]) != null ? _WIN_ANSI_HIGH_code : 63;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* A PDF string literal's body for `text`: WinAnsi bytes, `\`, `(` and `)`
|
|
207
|
+
* escaped, and every byte outside printable ASCII as an octal escape, so the
|
|
208
|
+
* literal itself is plain ASCII. A line break prints as a space.
|
|
209
|
+
*/ export function pdfString(text) {
|
|
210
|
+
let out = '';
|
|
211
|
+
for (const char of toWinAnsiText(text)){
|
|
212
|
+
const byte = char === '\n' ? 32 : winAnsiByte(char);
|
|
213
|
+
if (byte === 0x5c || byte === 0x28 || byte === 0x29) out += `\\${String.fromCharCode(byte)}`;
|
|
214
|
+
else if (byte < 32 || byte > 126) out += `\\${byte.toString(8).padStart(3, '0')}`;
|
|
215
|
+
else out += String.fromCharCode(byte);
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
/*==========================================
|
|
220
|
+
* WIDTHS
|
|
221
|
+
*=========================================*/ /** Helvetica advance widths for WinAnsi bytes 32–255, 1/1000 em; 0 where WinAnsi has no character. */ const HELVETICA = [
|
|
222
|
+
278,
|
|
223
|
+
278,
|
|
224
|
+
355,
|
|
225
|
+
556,
|
|
226
|
+
556,
|
|
227
|
+
889,
|
|
228
|
+
667,
|
|
229
|
+
191,
|
|
230
|
+
333,
|
|
231
|
+
333,
|
|
232
|
+
389,
|
|
233
|
+
584,
|
|
234
|
+
278,
|
|
235
|
+
333,
|
|
236
|
+
278,
|
|
237
|
+
278,
|
|
238
|
+
556,
|
|
239
|
+
556,
|
|
240
|
+
556,
|
|
241
|
+
556,
|
|
242
|
+
556,
|
|
243
|
+
556,
|
|
244
|
+
556,
|
|
245
|
+
556,
|
|
246
|
+
556,
|
|
247
|
+
556,
|
|
248
|
+
278,
|
|
249
|
+
278,
|
|
250
|
+
584,
|
|
251
|
+
584,
|
|
252
|
+
584,
|
|
253
|
+
556,
|
|
254
|
+
1015,
|
|
255
|
+
667,
|
|
256
|
+
667,
|
|
257
|
+
722,
|
|
258
|
+
722,
|
|
259
|
+
667,
|
|
260
|
+
611,
|
|
261
|
+
778,
|
|
262
|
+
722,
|
|
263
|
+
278,
|
|
264
|
+
500,
|
|
265
|
+
667,
|
|
266
|
+
556,
|
|
267
|
+
833,
|
|
268
|
+
722,
|
|
269
|
+
778,
|
|
270
|
+
667,
|
|
271
|
+
778,
|
|
272
|
+
722,
|
|
273
|
+
667,
|
|
274
|
+
611,
|
|
275
|
+
722,
|
|
276
|
+
667,
|
|
277
|
+
944,
|
|
278
|
+
667,
|
|
279
|
+
667,
|
|
280
|
+
611,
|
|
281
|
+
278,
|
|
282
|
+
278,
|
|
283
|
+
278,
|
|
284
|
+
469,
|
|
285
|
+
556,
|
|
286
|
+
333,
|
|
287
|
+
556,
|
|
288
|
+
556,
|
|
289
|
+
500,
|
|
290
|
+
556,
|
|
291
|
+
556,
|
|
292
|
+
278,
|
|
293
|
+
556,
|
|
294
|
+
556,
|
|
295
|
+
222,
|
|
296
|
+
222,
|
|
297
|
+
500,
|
|
298
|
+
222,
|
|
299
|
+
833,
|
|
300
|
+
556,
|
|
301
|
+
556,
|
|
302
|
+
556,
|
|
303
|
+
556,
|
|
304
|
+
333,
|
|
305
|
+
500,
|
|
306
|
+
278,
|
|
307
|
+
556,
|
|
308
|
+
500,
|
|
309
|
+
722,
|
|
310
|
+
500,
|
|
311
|
+
500,
|
|
312
|
+
500,
|
|
313
|
+
334,
|
|
314
|
+
260,
|
|
315
|
+
334,
|
|
316
|
+
584,
|
|
317
|
+
0,
|
|
318
|
+
556,
|
|
319
|
+
0,
|
|
320
|
+
222,
|
|
321
|
+
556,
|
|
322
|
+
333,
|
|
323
|
+
1000,
|
|
324
|
+
556,
|
|
325
|
+
556,
|
|
326
|
+
333,
|
|
327
|
+
1000,
|
|
328
|
+
667,
|
|
329
|
+
333,
|
|
330
|
+
1000,
|
|
331
|
+
0,
|
|
332
|
+
611,
|
|
333
|
+
0,
|
|
334
|
+
0,
|
|
335
|
+
222,
|
|
336
|
+
222,
|
|
337
|
+
333,
|
|
338
|
+
333,
|
|
339
|
+
350,
|
|
340
|
+
556,
|
|
341
|
+
1000,
|
|
342
|
+
333,
|
|
343
|
+
1000,
|
|
344
|
+
500,
|
|
345
|
+
333,
|
|
346
|
+
944,
|
|
347
|
+
0,
|
|
348
|
+
500,
|
|
349
|
+
667,
|
|
350
|
+
278,
|
|
351
|
+
333,
|
|
352
|
+
556,
|
|
353
|
+
556,
|
|
354
|
+
556,
|
|
355
|
+
556,
|
|
356
|
+
260,
|
|
357
|
+
556,
|
|
358
|
+
333,
|
|
359
|
+
737,
|
|
360
|
+
370,
|
|
361
|
+
556,
|
|
362
|
+
584,
|
|
363
|
+
333,
|
|
364
|
+
737,
|
|
365
|
+
333,
|
|
366
|
+
400,
|
|
367
|
+
584,
|
|
368
|
+
333,
|
|
369
|
+
333,
|
|
370
|
+
333,
|
|
371
|
+
556,
|
|
372
|
+
537,
|
|
373
|
+
278,
|
|
374
|
+
333,
|
|
375
|
+
333,
|
|
376
|
+
365,
|
|
377
|
+
556,
|
|
378
|
+
834,
|
|
379
|
+
834,
|
|
380
|
+
834,
|
|
381
|
+
611,
|
|
382
|
+
667,
|
|
383
|
+
667,
|
|
384
|
+
667,
|
|
385
|
+
667,
|
|
386
|
+
667,
|
|
387
|
+
667,
|
|
388
|
+
1000,
|
|
389
|
+
722,
|
|
390
|
+
667,
|
|
391
|
+
667,
|
|
392
|
+
667,
|
|
393
|
+
667,
|
|
394
|
+
278,
|
|
395
|
+
278,
|
|
396
|
+
278,
|
|
397
|
+
278,
|
|
398
|
+
722,
|
|
399
|
+
722,
|
|
400
|
+
778,
|
|
401
|
+
778,
|
|
402
|
+
778,
|
|
403
|
+
778,
|
|
404
|
+
778,
|
|
405
|
+
584,
|
|
406
|
+
778,
|
|
407
|
+
722,
|
|
408
|
+
722,
|
|
409
|
+
722,
|
|
410
|
+
722,
|
|
411
|
+
667,
|
|
412
|
+
667,
|
|
413
|
+
611,
|
|
414
|
+
556,
|
|
415
|
+
556,
|
|
416
|
+
556,
|
|
417
|
+
556,
|
|
418
|
+
556,
|
|
419
|
+
556,
|
|
420
|
+
889,
|
|
421
|
+
500,
|
|
422
|
+
556,
|
|
423
|
+
556,
|
|
424
|
+
556,
|
|
425
|
+
556,
|
|
426
|
+
278,
|
|
427
|
+
278,
|
|
428
|
+
278,
|
|
429
|
+
278,
|
|
430
|
+
556,
|
|
431
|
+
556,
|
|
432
|
+
556,
|
|
433
|
+
556,
|
|
434
|
+
556,
|
|
435
|
+
556,
|
|
436
|
+
556,
|
|
437
|
+
584,
|
|
438
|
+
611,
|
|
439
|
+
556,
|
|
440
|
+
556,
|
|
441
|
+
556,
|
|
442
|
+
556,
|
|
443
|
+
500,
|
|
444
|
+
556,
|
|
445
|
+
500
|
|
446
|
+
];
|
|
447
|
+
/** Helvetica-Bold advance widths for WinAnsi bytes 32–255, 1/1000 em. */ const HELVETICA_BOLD = [
|
|
448
|
+
278,
|
|
449
|
+
333,
|
|
450
|
+
474,
|
|
451
|
+
556,
|
|
452
|
+
556,
|
|
453
|
+
889,
|
|
454
|
+
722,
|
|
455
|
+
238,
|
|
456
|
+
333,
|
|
457
|
+
333,
|
|
458
|
+
389,
|
|
459
|
+
584,
|
|
460
|
+
278,
|
|
461
|
+
333,
|
|
462
|
+
278,
|
|
463
|
+
278,
|
|
464
|
+
556,
|
|
465
|
+
556,
|
|
466
|
+
556,
|
|
467
|
+
556,
|
|
468
|
+
556,
|
|
469
|
+
556,
|
|
470
|
+
556,
|
|
471
|
+
556,
|
|
472
|
+
556,
|
|
473
|
+
556,
|
|
474
|
+
333,
|
|
475
|
+
333,
|
|
476
|
+
584,
|
|
477
|
+
584,
|
|
478
|
+
584,
|
|
479
|
+
611,
|
|
480
|
+
975,
|
|
481
|
+
722,
|
|
482
|
+
722,
|
|
483
|
+
722,
|
|
484
|
+
722,
|
|
485
|
+
667,
|
|
486
|
+
611,
|
|
487
|
+
778,
|
|
488
|
+
722,
|
|
489
|
+
278,
|
|
490
|
+
556,
|
|
491
|
+
722,
|
|
492
|
+
611,
|
|
493
|
+
833,
|
|
494
|
+
722,
|
|
495
|
+
778,
|
|
496
|
+
667,
|
|
497
|
+
778,
|
|
498
|
+
722,
|
|
499
|
+
667,
|
|
500
|
+
611,
|
|
501
|
+
722,
|
|
502
|
+
667,
|
|
503
|
+
944,
|
|
504
|
+
667,
|
|
505
|
+
667,
|
|
506
|
+
611,
|
|
507
|
+
333,
|
|
508
|
+
278,
|
|
509
|
+
333,
|
|
510
|
+
584,
|
|
511
|
+
556,
|
|
512
|
+
333,
|
|
513
|
+
556,
|
|
514
|
+
611,
|
|
515
|
+
556,
|
|
516
|
+
611,
|
|
517
|
+
556,
|
|
518
|
+
333,
|
|
519
|
+
611,
|
|
520
|
+
611,
|
|
521
|
+
278,
|
|
522
|
+
278,
|
|
523
|
+
556,
|
|
524
|
+
278,
|
|
525
|
+
889,
|
|
526
|
+
611,
|
|
527
|
+
611,
|
|
528
|
+
611,
|
|
529
|
+
611,
|
|
530
|
+
389,
|
|
531
|
+
556,
|
|
532
|
+
333,
|
|
533
|
+
611,
|
|
534
|
+
556,
|
|
535
|
+
778,
|
|
536
|
+
556,
|
|
537
|
+
556,
|
|
538
|
+
500,
|
|
539
|
+
389,
|
|
540
|
+
280,
|
|
541
|
+
389,
|
|
542
|
+
584,
|
|
543
|
+
0,
|
|
544
|
+
556,
|
|
545
|
+
0,
|
|
546
|
+
278,
|
|
547
|
+
556,
|
|
548
|
+
500,
|
|
549
|
+
1000,
|
|
550
|
+
556,
|
|
551
|
+
556,
|
|
552
|
+
333,
|
|
553
|
+
1000,
|
|
554
|
+
667,
|
|
555
|
+
333,
|
|
556
|
+
1000,
|
|
557
|
+
0,
|
|
558
|
+
611,
|
|
559
|
+
0,
|
|
560
|
+
0,
|
|
561
|
+
278,
|
|
562
|
+
278,
|
|
563
|
+
500,
|
|
564
|
+
500,
|
|
565
|
+
350,
|
|
566
|
+
556,
|
|
567
|
+
1000,
|
|
568
|
+
333,
|
|
569
|
+
1000,
|
|
570
|
+
556,
|
|
571
|
+
333,
|
|
572
|
+
944,
|
|
573
|
+
0,
|
|
574
|
+
500,
|
|
575
|
+
667,
|
|
576
|
+
278,
|
|
577
|
+
333,
|
|
578
|
+
556,
|
|
579
|
+
556,
|
|
580
|
+
556,
|
|
581
|
+
556,
|
|
582
|
+
280,
|
|
583
|
+
556,
|
|
584
|
+
333,
|
|
585
|
+
737,
|
|
586
|
+
370,
|
|
587
|
+
556,
|
|
588
|
+
584,
|
|
589
|
+
333,
|
|
590
|
+
737,
|
|
591
|
+
333,
|
|
592
|
+
400,
|
|
593
|
+
584,
|
|
594
|
+
333,
|
|
595
|
+
333,
|
|
596
|
+
333,
|
|
597
|
+
611,
|
|
598
|
+
556,
|
|
599
|
+
278,
|
|
600
|
+
333,
|
|
601
|
+
333,
|
|
602
|
+
365,
|
|
603
|
+
556,
|
|
604
|
+
834,
|
|
605
|
+
834,
|
|
606
|
+
834,
|
|
607
|
+
611,
|
|
608
|
+
722,
|
|
609
|
+
722,
|
|
610
|
+
722,
|
|
611
|
+
722,
|
|
612
|
+
722,
|
|
613
|
+
722,
|
|
614
|
+
1000,
|
|
615
|
+
722,
|
|
616
|
+
667,
|
|
617
|
+
667,
|
|
618
|
+
667,
|
|
619
|
+
667,
|
|
620
|
+
278,
|
|
621
|
+
278,
|
|
622
|
+
278,
|
|
623
|
+
278,
|
|
624
|
+
722,
|
|
625
|
+
722,
|
|
626
|
+
778,
|
|
627
|
+
778,
|
|
628
|
+
778,
|
|
629
|
+
778,
|
|
630
|
+
778,
|
|
631
|
+
584,
|
|
632
|
+
778,
|
|
633
|
+
722,
|
|
634
|
+
722,
|
|
635
|
+
722,
|
|
636
|
+
722,
|
|
637
|
+
667,
|
|
638
|
+
667,
|
|
639
|
+
611,
|
|
640
|
+
556,
|
|
641
|
+
556,
|
|
642
|
+
556,
|
|
643
|
+
556,
|
|
644
|
+
556,
|
|
645
|
+
556,
|
|
646
|
+
889,
|
|
647
|
+
556,
|
|
648
|
+
556,
|
|
649
|
+
556,
|
|
650
|
+
556,
|
|
651
|
+
556,
|
|
652
|
+
278,
|
|
653
|
+
278,
|
|
654
|
+
278,
|
|
655
|
+
278,
|
|
656
|
+
611,
|
|
657
|
+
611,
|
|
658
|
+
611,
|
|
659
|
+
611,
|
|
660
|
+
611,
|
|
661
|
+
611,
|
|
662
|
+
611,
|
|
663
|
+
584,
|
|
664
|
+
611,
|
|
665
|
+
611,
|
|
666
|
+
611,
|
|
667
|
+
611,
|
|
668
|
+
611,
|
|
669
|
+
556,
|
|
670
|
+
611,
|
|
671
|
+
556
|
|
672
|
+
];
|
|
673
|
+
/** `text`'s width in points at `size`, as a reader draws it in `font`. */ export function measureText(text, size, font = 'regular') {
|
|
674
|
+
const table = font === 'bold' ? HELVETICA_BOLD : HELVETICA;
|
|
675
|
+
let total = 0;
|
|
676
|
+
for (const char of toWinAnsiText(text)){
|
|
677
|
+
var _table_;
|
|
678
|
+
if (char === '\n') continue;
|
|
679
|
+
total += (_table_ = table[winAnsiByte(char) - 32]) != null ? _table_ : 556;
|
|
680
|
+
}
|
|
681
|
+
return total / 1000 * size;
|
|
682
|
+
}
|
|
683
|
+
/** Words wrapped into lines no wider than `width` by `measure`; an overlong word is broken. */ function wrapWith(text, width, measure) {
|
|
684
|
+
const lines = [];
|
|
685
|
+
for (const paragraph of String(text != null ? text : '').split('\n')){
|
|
686
|
+
const words = paragraph.split(/\s+/).filter(Boolean);
|
|
687
|
+
if (!words.length) {
|
|
688
|
+
lines.push('');
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
let line = '';
|
|
692
|
+
for (let word of words){
|
|
693
|
+
while(measure(word) > width){
|
|
694
|
+
let cut = word.length - 1;
|
|
695
|
+
while(cut > 1 && measure(word.slice(0, cut)) > width)cut -= 1;
|
|
696
|
+
if (line) {
|
|
697
|
+
lines.push(line);
|
|
698
|
+
line = '';
|
|
699
|
+
}
|
|
700
|
+
lines.push(word.slice(0, cut));
|
|
701
|
+
word = word.slice(cut);
|
|
702
|
+
}
|
|
703
|
+
const next = line ? `${line} ${word}` : word;
|
|
704
|
+
if (measure(next) > width && line) {
|
|
705
|
+
lines.push(line);
|
|
706
|
+
line = word;
|
|
707
|
+
} else {
|
|
708
|
+
line = next;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
if (line) lines.push(line);
|
|
712
|
+
}
|
|
713
|
+
return lines;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* `text` wrapped to `width` points at `size` in `font`, by the exact widths.
|
|
717
|
+
* Each `\n` starts a new line; a blank line comes back as `''`.
|
|
718
|
+
*/ export function wrapLines(text, size, width, font = 'regular') {
|
|
719
|
+
return wrapWith(toWinAnsiText(text), width, (line)=>measureText(line, size, font));
|
|
720
|
+
}
|
|
721
|
+
/*==========================================
|
|
722
|
+
* THE QUICK ESTIMATE
|
|
723
|
+
*
|
|
724
|
+
* The first hand-written document (the waivers plugin's signed copy) wraps by
|
|
725
|
+
* this cheaper estimate: Helvetica's widths for ASCII, an em-ish 556 for the
|
|
726
|
+
* rest, and bold as 7% wider. It stays as it was so that document's bytes
|
|
727
|
+
* never move; new layouts use `measureText` and `wrapLines`.
|
|
728
|
+
*=========================================*/ function estimatedWidth(byte) {
|
|
729
|
+
if (byte >= 32 && byte <= 126) return HELVETICA[byte - 32];
|
|
730
|
+
if (byte === 0x95) return 350;
|
|
731
|
+
if (byte === 0x97 || byte === 0x85) return 1000;
|
|
732
|
+
return 556;
|
|
733
|
+
}
|
|
734
|
+
/** A string's estimated width in points at `size`; bold runs 7% wider. */ export function textWidth(text, size, bold = false) {
|
|
735
|
+
let total = 0;
|
|
736
|
+
for (const char of toWinAnsiText(text))total += estimatedWidth(winAnsiByte(char));
|
|
737
|
+
return total / 1000 * size * (bold ? 1.07 : 1);
|
|
738
|
+
}
|
|
739
|
+
/** Words wrapped by {@link textWidth} into lines no wider than `width`; an overlong word is broken. */ export function wrapText(text, size, width, bold = false) {
|
|
740
|
+
return wrapWith(text, width, (line)=>textWidth(line, size, bold));
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
//# sourceMappingURL=pdf-text.js.map
|