@tectra-ng/ui 0.4.4 → 0.4.5
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/amount/README.md +19 -1
- package/analog-clock/README.md +1 -1
- package/autocomplete/README.md +1 -1
- package/avatar/README.md +24 -4
- package/cascade-select/README.md +1 -1
- package/chips-input/README.md +10 -5
- package/color-picker/README.md +5 -0
- package/countdown/README.md +7 -1
- package/country/README.md +14 -1
- package/country-select/README.md +2 -1
- package/export/README.md +27 -2
- package/export-font/README.md +1 -1
- package/fesm2022/tectra-ng-ui-amount.mjs +96 -14
- package/fesm2022/tectra-ng-ui-amount.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-analog-clock.mjs +6 -19
- package/fesm2022/tectra-ng-ui-analog-clock.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-autocomplete.mjs +5 -4
- package/fesm2022/tectra-ng-ui-autocomplete.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-avatar.mjs +59 -17
- package/fesm2022/tectra-ng-ui-avatar.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-button.mjs +2 -2
- package/fesm2022/tectra-ng-ui-button.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-card.mjs +2 -2
- package/fesm2022/tectra-ng-ui-card.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-cascade-select.mjs +7 -4
- package/fesm2022/tectra-ng-ui-cascade-select.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-chips-input.mjs +114 -15
- package/fesm2022/tectra-ng-ui-chips-input.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-color-picker.mjs +93 -6
- package/fesm2022/tectra-ng-ui-color-picker.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-countdown.mjs +39 -10
- package/fesm2022/tectra-ng-ui-countdown.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-country-select.mjs +28 -5
- package/fesm2022/tectra-ng-ui-country-select.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-country.mjs +15 -6
- package/fesm2022/tectra-ng-ui-country.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-datepicker.mjs +10 -8
- package/fesm2022/tectra-ng-ui-datepicker.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-dialog.mjs +6 -6
- package/fesm2022/tectra-ng-ui-dialog.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-export.mjs +1237 -84
- package/fesm2022/tectra-ng-ui-export.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-field.mjs +1 -0
- package/fesm2022/tectra-ng-ui-field.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-fieldset.mjs +2 -2
- package/fesm2022/tectra-ng-ui-fieldset.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-icon-field.mjs +51 -23
- package/fesm2022/tectra-ng-ui-icon-field.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-input-number.mjs +2 -2
- package/fesm2022/tectra-ng-ui-input-number.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-input.mjs +5 -4
- package/fesm2022/tectra-ng-ui-input.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-key-filter.mjs +84 -19
- package/fesm2022/tectra-ng-ui-key-filter.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-knob.mjs +24 -2
- package/fesm2022/tectra-ng-ui-knob.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-label.mjs +68 -9
- package/fesm2022/tectra-ng-ui-label.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-link.mjs +38 -14
- package/fesm2022/tectra-ng-ui-link.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-mention-textarea.mjs +16 -5
- package/fesm2022/tectra-ng-ui-mention-textarea.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-message.mjs +5 -1
- package/fesm2022/tectra-ng-ui-message.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-month-calendar.mjs +26 -9
- package/fesm2022/tectra-ng-ui-month-calendar.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-multiselect.mjs +3 -2
- package/fesm2022/tectra-ng-ui-multiselect.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-panel-menu.mjs +45 -14
- package/fesm2022/tectra-ng-ui-panel-menu.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-panel.mjs +4 -2
- package/fesm2022/tectra-ng-ui-panel.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-password-generator.mjs +18 -1
- package/fesm2022/tectra-ng-ui-password-generator.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-password.mjs +2 -2
- package/fesm2022/tectra-ng-ui-password.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-pill.mjs +36 -16
- package/fesm2022/tectra-ng-ui-pill.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-rating.mjs +2 -2
- package/fesm2022/tectra-ng-ui-rating.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-select.mjs +30 -7
- package/fesm2022/tectra-ng-ui-select.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-stat-tiles.mjs +93 -8
- package/fesm2022/tectra-ng-ui-stat-tiles.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-status-icon.mjs +40 -3
- package/fesm2022/tectra-ng-ui-status-icon.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-table.mjs +20 -6
- package/fesm2022/tectra-ng-ui-table.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-tag.mjs +2 -2
- package/fesm2022/tectra-ng-ui-tag.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-toast.mjs +2 -2
- package/fesm2022/tectra-ng-ui-toast.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-tree-select.mjs +10 -8
- package/fesm2022/tectra-ng-ui-tree-select.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-tree-table.mjs +4 -4
- package/fesm2022/tectra-ng-ui-tree-table.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui-truncate-text.mjs +33 -9
- package/fesm2022/tectra-ng-ui-truncate-text.mjs.map +1 -1
- package/fesm2022/tectra-ng-ui.mjs +1 -1
- package/icon-field/README.md +2 -1
- package/key-filter/README.md +3 -2
- package/knob/README.md +3 -1
- package/label/README.md +4 -1
- package/link/README.md +4 -1
- package/message/README.md +2 -2
- package/month-calendar/README.md +2 -1
- package/multiselect/README.md +1 -1
- package/package.json +4 -4
- package/panel/README.md +3 -1
- package/panel-menu/README.md +3 -2
- package/password-generator/README.md +2 -0
- package/pill/README.md +4 -4
- package/select/README.md +1 -1
- package/stat-tiles/README.md +17 -1
- package/status-icon/README.md +3 -1
- package/table/README.md +1 -1
- package/tag/README.md +2 -0
- package/tree-select/README.md +1 -1
- package/truncate-text/README.md +2 -1
- package/types/tectra-ng-ui-amount.d.ts +41 -1
- package/types/tectra-ng-ui-amount.d.ts.map +1 -1
- package/types/tectra-ng-ui-analog-clock.d.ts +1 -4
- package/types/tectra-ng-ui-analog-clock.d.ts.map +1 -1
- package/types/tectra-ng-ui-autocomplete.d.ts +1 -1
- package/types/tectra-ng-ui-avatar.d.ts +45 -12
- package/types/tectra-ng-ui-avatar.d.ts.map +1 -1
- package/types/tectra-ng-ui-cascade-select.d.ts +3 -2
- package/types/tectra-ng-ui-cascade-select.d.ts.map +1 -1
- package/types/tectra-ng-ui-chips-input.d.ts +54 -6
- package/types/tectra-ng-ui-chips-input.d.ts.map +1 -1
- package/types/tectra-ng-ui-color-picker.d.ts +38 -1
- package/types/tectra-ng-ui-color-picker.d.ts.map +1 -1
- package/types/tectra-ng-ui-countdown.d.ts +12 -1
- package/types/tectra-ng-ui-countdown.d.ts.map +1 -1
- package/types/tectra-ng-ui-country-select.d.ts +17 -4
- package/types/tectra-ng-ui-country-select.d.ts.map +1 -1
- package/types/tectra-ng-ui-country.d.ts +7 -1
- package/types/tectra-ng-ui-country.d.ts.map +1 -1
- package/types/tectra-ng-ui-datepicker.d.ts +2 -2
- package/types/tectra-ng-ui-datepicker.d.ts.map +1 -1
- package/types/tectra-ng-ui-export.d.ts +68 -8
- package/types/tectra-ng-ui-export.d.ts.map +1 -1
- package/types/tectra-ng-ui-icon-field.d.ts +9 -2
- package/types/tectra-ng-ui-icon-field.d.ts.map +1 -1
- package/types/tectra-ng-ui-key-filter.d.ts +19 -4
- package/types/tectra-ng-ui-key-filter.d.ts.map +1 -1
- package/types/tectra-ng-ui-knob.d.ts +10 -2
- package/types/tectra-ng-ui-knob.d.ts.map +1 -1
- package/types/tectra-ng-ui-label.d.ts +8 -1
- package/types/tectra-ng-ui-label.d.ts.map +1 -1
- package/types/tectra-ng-ui-link.d.ts +8 -2
- package/types/tectra-ng-ui-link.d.ts.map +1 -1
- package/types/tectra-ng-ui-mention-textarea.d.ts +5 -0
- package/types/tectra-ng-ui-mention-textarea.d.ts.map +1 -1
- package/types/tectra-ng-ui-message.d.ts +1 -1
- package/types/tectra-ng-ui-message.d.ts.map +1 -1
- package/types/tectra-ng-ui-month-calendar.d.ts +14 -4
- package/types/tectra-ng-ui-month-calendar.d.ts.map +1 -1
- package/types/tectra-ng-ui-multiselect.d.ts +1 -1
- package/types/tectra-ng-ui-panel-menu.d.ts +34 -6
- package/types/tectra-ng-ui-panel-menu.d.ts.map +1 -1
- package/types/tectra-ng-ui-panel.d.ts +1 -1
- package/types/tectra-ng-ui-panel.d.ts.map +1 -1
- package/types/tectra-ng-ui-password-generator.d.ts +10 -3
- package/types/tectra-ng-ui-password-generator.d.ts.map +1 -1
- package/types/tectra-ng-ui-pill.d.ts +9 -5
- package/types/tectra-ng-ui-pill.d.ts.map +1 -1
- package/types/tectra-ng-ui-select.d.ts +5 -2
- package/types/tectra-ng-ui-select.d.ts.map +1 -1
- package/types/tectra-ng-ui-stat-tiles.d.ts +49 -5
- package/types/tectra-ng-ui-stat-tiles.d.ts.map +1 -1
- package/types/tectra-ng-ui-status-icon.d.ts +20 -2
- package/types/tectra-ng-ui-status-icon.d.ts.map +1 -1
- package/types/tectra-ng-ui-table.d.ts +2 -1
- package/types/tectra-ng-ui-table.d.ts.map +1 -1
- package/types/tectra-ng-ui-tree-select.d.ts +3 -3
- package/types/tectra-ng-ui-tree-select.d.ts.map +1 -1
- package/types/tectra-ng-ui-truncate-text.d.ts +7 -2
- package/types/tectra-ng-ui-truncate-text.d.ts.map +1 -1
- package/types/tectra-ng-ui.d.ts +1 -1
|
@@ -129,6 +129,9 @@ function hexColor(color, fallback) {
|
|
|
129
129
|
const full = value.length === 3 ? value.replace(/./g, (char) => char + char) : value;
|
|
130
130
|
return /^[0-9a-f]{6}$/i.test(full) ? full.toUpperCase() : fallback.replace("#", "").toUpperCase();
|
|
131
131
|
}
|
|
132
|
+
function loadDefaultExportFont() {
|
|
133
|
+
return import("@tectra-ng/ui/export-font").then((module) => module.TX_EXPORT_FONT);
|
|
134
|
+
}
|
|
132
135
|
const SIZES = {
|
|
133
136
|
a3: [841.89, 1190.55],
|
|
134
137
|
a4: [595.28, 841.89],
|
|
@@ -370,6 +373,260 @@ async function writeDocx(table, options = {}, context = {
|
|
|
370
373
|
], date, options.compress !== false);
|
|
371
374
|
return new Blob([bytes], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
|
|
372
375
|
}
|
|
376
|
+
const VOID_TAG = /^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
|
|
377
|
+
const RAW_TAGS = /* @__PURE__ */ new Set([
|
|
378
|
+
"script",
|
|
379
|
+
"style",
|
|
380
|
+
"textarea",
|
|
381
|
+
"title"
|
|
382
|
+
]);
|
|
383
|
+
const SKIPPED_TAGS = /* @__PURE__ */ new Set([
|
|
384
|
+
"script",
|
|
385
|
+
"style",
|
|
386
|
+
"template",
|
|
387
|
+
"title",
|
|
388
|
+
"head",
|
|
389
|
+
"noscript",
|
|
390
|
+
"svg",
|
|
391
|
+
"canvas",
|
|
392
|
+
"iframe",
|
|
393
|
+
"object",
|
|
394
|
+
"select",
|
|
395
|
+
"button",
|
|
396
|
+
"input",
|
|
397
|
+
"textarea"
|
|
398
|
+
]);
|
|
399
|
+
const BLOCK_TAG = /^(?:address|article|aside|blockquote|center|details|dialog|div|dl|fieldset|figcaption|figure|footer|form|h[1-6]|header|hr|main|menu|nav|ol|p|pre|section|summary|table|ul)$/;
|
|
400
|
+
const SECTIONS = /^(?:thead|tbody|tfoot|tr|td|th|caption)$/;
|
|
401
|
+
const TABLE_SCOPE = /^table$/;
|
|
402
|
+
const IMPLIED_ENDS = {
|
|
403
|
+
li: {
|
|
404
|
+
closes: /^li$/,
|
|
405
|
+
scope: /^(?:ul|ol|menu)$/
|
|
406
|
+
},
|
|
407
|
+
dt: {
|
|
408
|
+
closes: /^(?:dt|dd)$/,
|
|
409
|
+
scope: /^dl$/
|
|
410
|
+
},
|
|
411
|
+
dd: {
|
|
412
|
+
closes: /^(?:dt|dd)$/,
|
|
413
|
+
scope: /^dl$/
|
|
414
|
+
},
|
|
415
|
+
tr: {
|
|
416
|
+
closes: /^(?:tr|td|th|caption)$/,
|
|
417
|
+
scope: /^(?:table|thead|tbody|tfoot)$/
|
|
418
|
+
},
|
|
419
|
+
td: {
|
|
420
|
+
closes: /^(?:td|th)$/,
|
|
421
|
+
scope: /^(?:tr|table)$/
|
|
422
|
+
},
|
|
423
|
+
th: {
|
|
424
|
+
closes: /^(?:td|th)$/,
|
|
425
|
+
scope: /^(?:tr|table)$/
|
|
426
|
+
},
|
|
427
|
+
thead: {
|
|
428
|
+
closes: SECTIONS,
|
|
429
|
+
scope: TABLE_SCOPE
|
|
430
|
+
},
|
|
431
|
+
tbody: {
|
|
432
|
+
closes: SECTIONS,
|
|
433
|
+
scope: TABLE_SCOPE
|
|
434
|
+
},
|
|
435
|
+
tfoot: {
|
|
436
|
+
closes: SECTIONS,
|
|
437
|
+
scope: TABLE_SCOPE
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
const ENTITIES = {
|
|
441
|
+
amp: "&",
|
|
442
|
+
lt: "<",
|
|
443
|
+
gt: ">",
|
|
444
|
+
quot: "\"",
|
|
445
|
+
apos: "'",
|
|
446
|
+
nbsp: "\xA0",
|
|
447
|
+
ensp: " ",
|
|
448
|
+
emsp: " ",
|
|
449
|
+
thinsp: " ",
|
|
450
|
+
shy: "",
|
|
451
|
+
zwnj: "",
|
|
452
|
+
zwj: "",
|
|
453
|
+
copy: "©",
|
|
454
|
+
reg: "®",
|
|
455
|
+
trade: "™",
|
|
456
|
+
hellip: "…",
|
|
457
|
+
mdash: "—",
|
|
458
|
+
ndash: "–",
|
|
459
|
+
minus: "−",
|
|
460
|
+
lsquo: "‘",
|
|
461
|
+
rsquo: "’",
|
|
462
|
+
sbquo: "‚",
|
|
463
|
+
ldquo: "“",
|
|
464
|
+
rdquo: "”",
|
|
465
|
+
bdquo: "„",
|
|
466
|
+
laquo: "«",
|
|
467
|
+
raquo: "»",
|
|
468
|
+
bull: "•",
|
|
469
|
+
middot: "·",
|
|
470
|
+
deg: "°",
|
|
471
|
+
plusmn: "±",
|
|
472
|
+
times: "×",
|
|
473
|
+
divide: "÷",
|
|
474
|
+
euro: "€",
|
|
475
|
+
pound: "£",
|
|
476
|
+
yen: "¥",
|
|
477
|
+
cent: "¢",
|
|
478
|
+
sect: "§",
|
|
479
|
+
para: "¶",
|
|
480
|
+
numero: "№",
|
|
481
|
+
larr: "←",
|
|
482
|
+
rarr: "→",
|
|
483
|
+
uarr: "↑",
|
|
484
|
+
darr: "↓",
|
|
485
|
+
harr: "↔",
|
|
486
|
+
check: "✓"
|
|
487
|
+
};
|
|
488
|
+
function decodeEntities(text) {
|
|
489
|
+
return text.replace(/&(#x[\da-f]+|#\d+|[a-z][a-z\d]*);/gi, (entity, body) => {
|
|
490
|
+
if (body[0] === "#") {
|
|
491
|
+
const code = body[1] === "x" || body[1] === "X" ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
492
|
+
return code > 0 && code <= 1114111 ? String.fromCodePoint(code) : entity;
|
|
493
|
+
}
|
|
494
|
+
return ENTITIES[body.toLowerCase()] ?? entity;
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
function parseAttributes(source) {
|
|
498
|
+
const attrs = {};
|
|
499
|
+
for (const match of source.matchAll(/([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g)) attrs[match[1].toLowerCase()] = decodeEntities(match[2] ?? match[3] ?? match[4] ?? "");
|
|
500
|
+
return attrs;
|
|
501
|
+
}
|
|
502
|
+
function tagEnd(html, from) {
|
|
503
|
+
let quote = "";
|
|
504
|
+
for (let index = from; index < html.length; index++) {
|
|
505
|
+
const char = html[index];
|
|
506
|
+
if (quote) {
|
|
507
|
+
if (char === quote) quote = "";
|
|
508
|
+
} else if (char === "\"" || char === "'") quote = char;
|
|
509
|
+
else if (char === ">") return index;
|
|
510
|
+
}
|
|
511
|
+
return html.length;
|
|
512
|
+
}
|
|
513
|
+
function parseHtml(html) {
|
|
514
|
+
const root = {
|
|
515
|
+
tag: "#root",
|
|
516
|
+
attrs: {},
|
|
517
|
+
children: []
|
|
518
|
+
};
|
|
519
|
+
const stack = [root];
|
|
520
|
+
const top = () => stack[stack.length - 1];
|
|
521
|
+
const text = (value) => {
|
|
522
|
+
if (value) top().children.push(decodeEntities(value));
|
|
523
|
+
};
|
|
524
|
+
const closeImplied = (tag) => {
|
|
525
|
+
const rule = IMPLIED_ENDS[tag];
|
|
526
|
+
if (rule) {
|
|
527
|
+
let close = 0;
|
|
528
|
+
for (let index = stack.length - 1; index > 0 && !rule.scope.test(stack[index].tag); index--) if (rule.closes.test(stack[index].tag)) close = index;
|
|
529
|
+
if (close) stack.length = close;
|
|
530
|
+
}
|
|
531
|
+
if (BLOCK_TAG.test(tag)) for (let index = stack.length - 1; index > 0; index--) {
|
|
532
|
+
const open = stack[index].tag;
|
|
533
|
+
if (open === "p") {
|
|
534
|
+
stack.length = index;
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
if (BLOCK_TAG.test(open) || open === "li" || open === "td" || open === "th") break;
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
let index = 0;
|
|
541
|
+
while (index < html.length) {
|
|
542
|
+
const open = html.indexOf("<", index);
|
|
543
|
+
if (open < 0) {
|
|
544
|
+
text(html.slice(index));
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
text(html.slice(index, open));
|
|
548
|
+
if (html.startsWith("<!--", open)) {
|
|
549
|
+
const end = html.indexOf("-->", open + 4);
|
|
550
|
+
index = end < 0 ? html.length : end + 3;
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
const name = /^<(\/?)([a-zA-Z][\w:-]*)/.exec(html.slice(open, open + 64));
|
|
554
|
+
if (!name) {
|
|
555
|
+
if (html[open + 1] === "!" || html[open + 1] === "?") index = tagEnd(html, open) + 1;
|
|
556
|
+
else {
|
|
557
|
+
text("<");
|
|
558
|
+
index = open + 1;
|
|
559
|
+
}
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
const end = tagEnd(html, open + name[0].length);
|
|
563
|
+
const tag = name[2].toLowerCase();
|
|
564
|
+
index = end + 1;
|
|
565
|
+
if (name[1]) {
|
|
566
|
+
const at = stack.map((element) => element.tag).lastIndexOf(tag);
|
|
567
|
+
if (at > 0) stack.length = at;
|
|
568
|
+
else if (tag === "br") top().children.push({
|
|
569
|
+
tag,
|
|
570
|
+
attrs: {},
|
|
571
|
+
children: []
|
|
572
|
+
});
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
const body = html.slice(open + name[0].length, end);
|
|
576
|
+
closeImplied(tag);
|
|
577
|
+
const element = {
|
|
578
|
+
tag,
|
|
579
|
+
attrs: parseAttributes(body),
|
|
580
|
+
children: []
|
|
581
|
+
};
|
|
582
|
+
top().children.push(element);
|
|
583
|
+
if (RAW_TAGS.has(tag)) {
|
|
584
|
+
const close = html.toLowerCase().indexOf(`</${tag}`, index);
|
|
585
|
+
const stop = close < 0 ? html.length : close;
|
|
586
|
+
element.children.push(html.slice(index, stop));
|
|
587
|
+
index = close < 0 ? html.length : tagEnd(html, close) + 1;
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
if (!VOID_TAG.test(tag) && !body.trimEnd().endsWith("/")) stack.push(element);
|
|
591
|
+
}
|
|
592
|
+
return root;
|
|
593
|
+
}
|
|
594
|
+
function domChildren(node) {
|
|
595
|
+
return Array.from(node.childNodes).flatMap((child) => {
|
|
596
|
+
const read = fromDomNode(child);
|
|
597
|
+
return read === null ? [] : [read];
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
function fromDomNode(node) {
|
|
601
|
+
if (node.nodeType === 3) return node.nodeValue;
|
|
602
|
+
if (node.nodeType !== 1) return null;
|
|
603
|
+
const attrs = {};
|
|
604
|
+
for (const attr of Array.from(node.attributes)) attrs[attr.name.toLowerCase()] = attr.value;
|
|
605
|
+
return {
|
|
606
|
+
tag: node.tagName.toLowerCase(),
|
|
607
|
+
attrs,
|
|
608
|
+
children: domChildren(node)
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function readDom(node) {
|
|
612
|
+
const source = node;
|
|
613
|
+
if (source.nodeType === 9) return {
|
|
614
|
+
tag: "#root",
|
|
615
|
+
attrs: {},
|
|
616
|
+
children: source.body ? domChildren(source.body) : []
|
|
617
|
+
};
|
|
618
|
+
if (source.nodeType === 11) return {
|
|
619
|
+
tag: "#root",
|
|
620
|
+
attrs: {},
|
|
621
|
+
children: domChildren(source)
|
|
622
|
+
};
|
|
623
|
+
const read = fromDomNode(source);
|
|
624
|
+
return {
|
|
625
|
+
tag: "#root",
|
|
626
|
+
attrs: {},
|
|
627
|
+
children: read === null ? [] : [read]
|
|
628
|
+
};
|
|
629
|
+
}
|
|
373
630
|
const KEEP_TABLES = [
|
|
374
631
|
"OS/2",
|
|
375
632
|
"cmap",
|
|
@@ -629,13 +886,13 @@ var PdfFont = class {
|
|
|
629
886
|
}
|
|
630
887
|
width(text, size) {
|
|
631
888
|
let units = 0;
|
|
632
|
-
for (const char of text) units += this.ttf.advance(this.ttf.glyphId(char.codePointAt(0)
|
|
889
|
+
for (const char of text) units += this.ttf.advance(this.ttf.glyphId(char.codePointAt(0)));
|
|
633
890
|
return units * size / this.ttf.unitsPerEm;
|
|
634
891
|
}
|
|
635
892
|
encode(text) {
|
|
636
893
|
let hex = "";
|
|
637
894
|
for (const char of text) {
|
|
638
|
-
const code = char.codePointAt(0)
|
|
895
|
+
const code = char.codePointAt(0);
|
|
639
896
|
const glyph = this.ttf.glyphId(code);
|
|
640
897
|
if (!this.used.has(glyph)) this.used.set(glyph, code);
|
|
641
898
|
hex += glyph.toString(16).padStart(4, "0");
|
|
@@ -666,10 +923,955 @@ function utf16Hex(text) {
|
|
|
666
923
|
function pdfString(text) {
|
|
667
924
|
return `<FEFF${utf16Hex(text)}>`;
|
|
668
925
|
}
|
|
926
|
+
function asciiString(text) {
|
|
927
|
+
return `(${encodeURI(decodeUriSafe(text)).replace(/[\\()]/g, (char) => `\\${char}`)})`;
|
|
928
|
+
}
|
|
929
|
+
function decodeUriSafe(text) {
|
|
930
|
+
try {
|
|
931
|
+
return decodeURI(text);
|
|
932
|
+
} catch {
|
|
933
|
+
return text;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
669
936
|
function pdfDate(date) {
|
|
670
937
|
const pad = (value) => String(value).padStart(2, "0");
|
|
671
938
|
return `D:${date.getFullYear()}${pad(date.getMonth() + 1)}${pad(date.getDate())}${pad(date.getHours())}${pad(date.getMinutes())}${pad(date.getSeconds())}`;
|
|
672
939
|
}
|
|
940
|
+
async function loadPdfFonts(font) {
|
|
941
|
+
return {
|
|
942
|
+
regular: parseTrueType(toBytes(font.regular)),
|
|
943
|
+
bold: font.bold ? parseTrueType(toBytes(font.bold)) : null
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
function loadOptionalFont(source) {
|
|
947
|
+
return source ? parseTrueType(toBytes(source)) : null;
|
|
948
|
+
}
|
|
949
|
+
async function assemblePdf(document) {
|
|
950
|
+
const objects = [];
|
|
951
|
+
const reserve = () => {
|
|
952
|
+
objects.push(null);
|
|
953
|
+
return objects.length;
|
|
954
|
+
};
|
|
955
|
+
const set = (id, body) => {
|
|
956
|
+
objects[id - 1] = typeof body === "string" ? utf8(`${id} 0 obj\n${body}\nendobj\n`) : body;
|
|
957
|
+
};
|
|
958
|
+
const stream = async (id, dict, data) => {
|
|
959
|
+
const packed = document.compress === false ? null : await compress(data, "deflate");
|
|
960
|
+
const body = packed ?? data;
|
|
961
|
+
set(id, concatBytes([
|
|
962
|
+
utf8(`${id} 0 obj\n<< ${dict} /Length ${body.length}${packed ? " /Filter /FlateDecode" : ""} >>\nstream\n`),
|
|
963
|
+
body,
|
|
964
|
+
utf8("\nendstream\nendobj\n")
|
|
965
|
+
]));
|
|
966
|
+
};
|
|
967
|
+
const catalog = reserve();
|
|
968
|
+
const pagesId = reserve();
|
|
969
|
+
const info = reserve();
|
|
970
|
+
const fontIds = /* @__PURE__ */ new Map();
|
|
971
|
+
for (const pdfFont of document.fonts) fontIds.set(pdfFont, reserve());
|
|
972
|
+
const pageIds = document.pages.map((page) => ({
|
|
973
|
+
page: reserve(),
|
|
974
|
+
content: reserve(),
|
|
975
|
+
links: page.links.map(() => reserve())
|
|
976
|
+
}));
|
|
977
|
+
const fontsDict = [...fontIds].map(([pdfFont, id]) => `/${pdfFont.resource} ${id} 0 R`).join(" ");
|
|
978
|
+
for (const [index, ids] of pageIds.entries()) {
|
|
979
|
+
const page = document.pages[index];
|
|
980
|
+
const annots = ids.links.length ? ` /Annots [${ids.links.map((id) => `${id} 0 R`).join(" ")}]` : "";
|
|
981
|
+
set(ids.page, `<< /Type /Page /Parent ${pagesId} 0 R /MediaBox [0 0 ${num(document.width)} ${num(document.height)}] /Resources << /Font << ${fontsDict} >> >> /Contents ${ids.content} 0 R${annots} >>`);
|
|
982
|
+
await stream(ids.content, "", utf8(page.ops.join("\n")));
|
|
983
|
+
page.links.forEach((link, linkIndex) => {
|
|
984
|
+
const rect = [
|
|
985
|
+
link.x,
|
|
986
|
+
link.y,
|
|
987
|
+
link.x + link.width,
|
|
988
|
+
link.y + link.height
|
|
989
|
+
].map(num).join(" ");
|
|
990
|
+
set(ids.links[linkIndex], `<< /Type /Annot /Subtype /Link /Rect [${rect}] /Border [0 0 0] /A << /Type /Action /S /URI /URI ${asciiString(link.uri)} >> >>`);
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
for (const [pdfFont, id] of fontIds) {
|
|
994
|
+
const ttf = pdfFont.ttf;
|
|
995
|
+
const scale = (value) => Math.round(value * 1e3 / ttf.unitsPerEm);
|
|
996
|
+
const glyphs = [...pdfFont.used.keys()].sort((a, b) => a - b);
|
|
997
|
+
const baseFont = `${pdfFont.tag}+${ttf.name}`;
|
|
998
|
+
const cid = reserve();
|
|
999
|
+
const descriptor = reserve();
|
|
1000
|
+
const file = reserve();
|
|
1001
|
+
const toUnicode = reserve();
|
|
1002
|
+
set(id, `<< /Type /Font /Subtype /Type0 /BaseFont /${baseFont} /Encoding /Identity-H /DescendantFonts [${cid} 0 R] /ToUnicode ${toUnicode} 0 R >>`);
|
|
1003
|
+
const w = glyphs.map((glyph) => `${glyph} [${scale(ttf.advance(glyph))}]`).join(" ");
|
|
1004
|
+
set(cid, `<< /Type /Font /Subtype /CIDFontType2 /BaseFont /${baseFont} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor ${descriptor} 0 R /DW ${scale(ttf.advance(0))} /W [${w}] /CIDToGIDMap /Identity >>`);
|
|
1005
|
+
set(descriptor, `<< /Type /FontDescriptor /FontName /${baseFont} /Flags 32 /FontBBox [${ttf.bbox.map(scale).join(" ")}] /ItalicAngle 0 /Ascent ${scale(ttf.ascent)} /Descent ${scale(ttf.descent)} /CapHeight ${scale(ttf.capHeight)} /StemV 80 /FontFile2 ${file} 0 R >>`);
|
|
1006
|
+
const subset = ttf.subset(new Set(glyphs));
|
|
1007
|
+
await stream(file, `/Length1 ${subset.length}`, subset);
|
|
1008
|
+
const entries = glyphs.map((glyph) => `<${glyph.toString(16).padStart(4, "0")}> <${utf16Hex(String.fromCodePoint(pdfFont.used.get(glyph)))}>`);
|
|
1009
|
+
const chunks = [];
|
|
1010
|
+
for (let index = 0; index < entries.length; index += 100) {
|
|
1011
|
+
const chunk = entries.slice(index, index + 100);
|
|
1012
|
+
chunks.push(`${chunk.length} beginbfchar\n${chunk.join("\n")}\nendbfchar`);
|
|
1013
|
+
}
|
|
1014
|
+
await stream(toUnicode, "", utf8(`/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n/CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n${chunks.join("\n")}\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend`));
|
|
1015
|
+
}
|
|
1016
|
+
set(catalog, `<< /Type /Catalog /Pages ${pagesId} 0 R >>`);
|
|
1017
|
+
set(pagesId, `<< /Type /Pages /Kids [${pageIds.map((ids) => `${ids.page} 0 R`).join(" ")}] /Count ${pageIds.length} >>`);
|
|
1018
|
+
const infoParts = [`/Producer (tectra)`, `/CreationDate (${pdfDate(document.date)})`];
|
|
1019
|
+
if (document.title) infoParts.push(`/Title ${pdfString(document.title)}`);
|
|
1020
|
+
if (document.author) infoParts.push(`/Author ${pdfString(document.author)}`);
|
|
1021
|
+
set(info, `<< ${infoParts.join(" ")} >>`);
|
|
1022
|
+
const header = utf8("%PDF-1.7\n%âãÏÓ\n");
|
|
1023
|
+
const offsets = [];
|
|
1024
|
+
let offset = header.length;
|
|
1025
|
+
for (const object of objects) {
|
|
1026
|
+
offsets.push(offset);
|
|
1027
|
+
offset += object.length;
|
|
1028
|
+
}
|
|
1029
|
+
const xref = [`xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`, ...offsets.map((value) => `${String(value).padStart(10, "0")} 00000 n \n`)].join("");
|
|
1030
|
+
const trailer = `trailer\n<< /Size ${objects.length + 1} /Root ${catalog} 0 R /Info ${info} 0 R >>\nstartxref\n${offset}\n%%EOF\n`;
|
|
1031
|
+
const bytes = concatBytes([
|
|
1032
|
+
header,
|
|
1033
|
+
...objects,
|
|
1034
|
+
utf8(xref + trailer)
|
|
1035
|
+
]);
|
|
1036
|
+
return new Blob([bytes], { type: "application/pdf" });
|
|
1037
|
+
}
|
|
1038
|
+
const PLAIN = {
|
|
1039
|
+
bold: false,
|
|
1040
|
+
italic: false,
|
|
1041
|
+
code: false,
|
|
1042
|
+
block: false,
|
|
1043
|
+
underline: false,
|
|
1044
|
+
strike: false,
|
|
1045
|
+
link: null,
|
|
1046
|
+
color: null,
|
|
1047
|
+
scale: 1,
|
|
1048
|
+
rise: 0
|
|
1049
|
+
};
|
|
1050
|
+
const HEADING_SCALE = [
|
|
1051
|
+
2,
|
|
1052
|
+
1.6,
|
|
1053
|
+
1.35,
|
|
1054
|
+
1.15,
|
|
1055
|
+
1,
|
|
1056
|
+
.9
|
|
1057
|
+
];
|
|
1058
|
+
const FLOW_PARTS = /* @__PURE__ */ new Set([
|
|
1059
|
+
"li",
|
|
1060
|
+
"dt",
|
|
1061
|
+
"dd",
|
|
1062
|
+
"html",
|
|
1063
|
+
"body",
|
|
1064
|
+
"caption",
|
|
1065
|
+
"thead",
|
|
1066
|
+
"tbody",
|
|
1067
|
+
"tfoot",
|
|
1068
|
+
"tr",
|
|
1069
|
+
"td",
|
|
1070
|
+
"th"
|
|
1071
|
+
]);
|
|
1072
|
+
const TABLE_TAG = /^table$/;
|
|
1073
|
+
const LINK_SCHEMES = /^(https?:|mailto:|tel:)/i;
|
|
1074
|
+
function isFlow(tag) {
|
|
1075
|
+
return BLOCK_TAG.test(tag) || FLOW_PARTS.has(tag);
|
|
1076
|
+
}
|
|
1077
|
+
function styleOf(element) {
|
|
1078
|
+
const result = {};
|
|
1079
|
+
for (const part of (element.attrs["style"] ?? "").split(";")) {
|
|
1080
|
+
const colon = part.indexOf(":");
|
|
1081
|
+
if (colon > 0) result[part.slice(0, colon).trim().toLowerCase()] = part.slice(colon + 1).trim().toLowerCase();
|
|
1082
|
+
}
|
|
1083
|
+
return result;
|
|
1084
|
+
}
|
|
1085
|
+
function alignOf(element, fallback) {
|
|
1086
|
+
const value = styleOf(element)["text-align"] ?? element.attrs["align"]?.toLowerCase();
|
|
1087
|
+
if (value === "center") return "center";
|
|
1088
|
+
if (value === "right" || value === "end") return "end";
|
|
1089
|
+
if (value === "left" || value === "start" || value === "justify") return "start";
|
|
1090
|
+
return fallback;
|
|
1091
|
+
}
|
|
1092
|
+
function breaks(element, side) {
|
|
1093
|
+
const css = styleOf(element);
|
|
1094
|
+
return ["page", "always"].includes(css[`break-${side}`] ?? css[`page-break-${side}`] ?? "");
|
|
1095
|
+
}
|
|
1096
|
+
function inlineStyle(element, style, linkColor) {
|
|
1097
|
+
const css = styleOf(element);
|
|
1098
|
+
const next = { ...style };
|
|
1099
|
+
switch (element.tag) {
|
|
1100
|
+
case "b":
|
|
1101
|
+
case "strong":
|
|
1102
|
+
next.bold = true;
|
|
1103
|
+
break;
|
|
1104
|
+
case "i":
|
|
1105
|
+
case "em":
|
|
1106
|
+
case "cite":
|
|
1107
|
+
case "dfn":
|
|
1108
|
+
case "var":
|
|
1109
|
+
next.italic = true;
|
|
1110
|
+
break;
|
|
1111
|
+
case "code":
|
|
1112
|
+
case "kbd":
|
|
1113
|
+
case "samp":
|
|
1114
|
+
case "tt":
|
|
1115
|
+
next.code = true;
|
|
1116
|
+
break;
|
|
1117
|
+
case "u":
|
|
1118
|
+
case "ins":
|
|
1119
|
+
next.underline = true;
|
|
1120
|
+
break;
|
|
1121
|
+
case "s":
|
|
1122
|
+
case "strike":
|
|
1123
|
+
case "del":
|
|
1124
|
+
next.strike = true;
|
|
1125
|
+
break;
|
|
1126
|
+
case "small":
|
|
1127
|
+
next.scale = style.scale * .85;
|
|
1128
|
+
break;
|
|
1129
|
+
case "sub":
|
|
1130
|
+
case "sup":
|
|
1131
|
+
next.scale = style.scale * .75;
|
|
1132
|
+
next.rise = element.tag === "sup" ? .35 : -.15;
|
|
1133
|
+
break;
|
|
1134
|
+
case "a": {
|
|
1135
|
+
const href = element.attrs["href"] ?? "";
|
|
1136
|
+
if (LINK_SCHEMES.test(href)) {
|
|
1137
|
+
next.link = href;
|
|
1138
|
+
next.underline = true;
|
|
1139
|
+
next.color = linkColor;
|
|
1140
|
+
}
|
|
1141
|
+
break;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
const weight = css["font-weight"];
|
|
1145
|
+
if (/^bold/.test(weight ?? "") || Number(weight) >= 600) next.bold = true;
|
|
1146
|
+
if (/^(?:italic|oblique)/.test(css["font-style"] ?? "")) next.italic = true;
|
|
1147
|
+
if (/underline/.test(css["text-decoration"] ?? "")) next.underline = true;
|
|
1148
|
+
if (/line-through/.test(css["text-decoration"] ?? "")) next.strike = true;
|
|
1149
|
+
return next;
|
|
1150
|
+
}
|
|
1151
|
+
function sameStyle(a, b) {
|
|
1152
|
+
return Object.keys(a).every((key) => a[key] === b[key]);
|
|
1153
|
+
}
|
|
1154
|
+
var HtmlPdfLayout = class {
|
|
1155
|
+
fonts;
|
|
1156
|
+
items = [];
|
|
1157
|
+
pending = 0;
|
|
1158
|
+
fresh = true;
|
|
1159
|
+
marker = null;
|
|
1160
|
+
depth = 0;
|
|
1161
|
+
size;
|
|
1162
|
+
lineFactor;
|
|
1163
|
+
text;
|
|
1164
|
+
muted;
|
|
1165
|
+
link;
|
|
1166
|
+
border;
|
|
1167
|
+
headerFill;
|
|
1168
|
+
codeFill;
|
|
1169
|
+
constructor(fonts, options) {
|
|
1170
|
+
this.fonts = fonts;
|
|
1171
|
+
this.size = options.fontSize ?? 10;
|
|
1172
|
+
this.lineFactor = options.lineHeight ?? 1.45;
|
|
1173
|
+
this.text = hexColor(options.textColor, "1D2939");
|
|
1174
|
+
this.muted = hexColor(options.mutedColor, "667085");
|
|
1175
|
+
this.link = hexColor(options.linkColor, "2560E4");
|
|
1176
|
+
this.border = hexColor(options.borderColor, "D0D5DD");
|
|
1177
|
+
this.headerFill = options.headerBackground === "none" ? null : hexColor(options.headerBackground, "F2F4F7");
|
|
1178
|
+
this.codeFill = hexColor(options.codeBackground, "F2F4F7");
|
|
1179
|
+
}
|
|
1180
|
+
flow(nodes, frame, style, pre = false) {
|
|
1181
|
+
let runs = [];
|
|
1182
|
+
const flush = () => {
|
|
1183
|
+
this.paragraph(runs, frame, pre);
|
|
1184
|
+
runs = [];
|
|
1185
|
+
};
|
|
1186
|
+
for (const node of nodes) if (typeof node === "string") runs.push({
|
|
1187
|
+
text: node,
|
|
1188
|
+
style
|
|
1189
|
+
});
|
|
1190
|
+
else if (SKIPPED_TAGS.has(node.tag)) continue;
|
|
1191
|
+
else if (isFlow(node.tag)) {
|
|
1192
|
+
flush();
|
|
1193
|
+
this.block(node, frame, style);
|
|
1194
|
+
} else this.inline(node, style, runs);
|
|
1195
|
+
flush();
|
|
1196
|
+
}
|
|
1197
|
+
inline(element, style, runs) {
|
|
1198
|
+
if (SKIPPED_TAGS.has(element.tag)) return;
|
|
1199
|
+
if (element.tag === "br") {
|
|
1200
|
+
runs.push({
|
|
1201
|
+
text: "",
|
|
1202
|
+
style,
|
|
1203
|
+
brk: "hard"
|
|
1204
|
+
});
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
if (element.tag === "img") {
|
|
1208
|
+
const alt = element.attrs["alt"]?.trim();
|
|
1209
|
+
if (alt) runs.push({
|
|
1210
|
+
text: alt,
|
|
1211
|
+
style: {
|
|
1212
|
+
...style,
|
|
1213
|
+
italic: true
|
|
1214
|
+
}
|
|
1215
|
+
});
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
const cell = element.tag === "td" || element.tag === "th";
|
|
1219
|
+
const block = isFlow(element.tag) && !cell;
|
|
1220
|
+
const next = isFlow(element.tag) ? style : inlineStyle(element, style, this.link);
|
|
1221
|
+
if (block) runs.push({
|
|
1222
|
+
text: "",
|
|
1223
|
+
style,
|
|
1224
|
+
brk: "soft"
|
|
1225
|
+
});
|
|
1226
|
+
if (element.tag === "li") runs.push({
|
|
1227
|
+
text: "• ",
|
|
1228
|
+
style
|
|
1229
|
+
});
|
|
1230
|
+
if (element.tag === "q") runs.push({
|
|
1231
|
+
text: "“",
|
|
1232
|
+
style
|
|
1233
|
+
});
|
|
1234
|
+
for (const child of element.children) if (typeof child === "string") runs.push({
|
|
1235
|
+
text: child,
|
|
1236
|
+
style: next
|
|
1237
|
+
});
|
|
1238
|
+
else this.inline(child, next, runs);
|
|
1239
|
+
if (element.tag === "q") runs.push({
|
|
1240
|
+
text: "”",
|
|
1241
|
+
style
|
|
1242
|
+
});
|
|
1243
|
+
if (block) runs.push({
|
|
1244
|
+
text: "",
|
|
1245
|
+
style,
|
|
1246
|
+
brk: "soft"
|
|
1247
|
+
});
|
|
1248
|
+
if (cell) runs.push({
|
|
1249
|
+
text: " ",
|
|
1250
|
+
style
|
|
1251
|
+
});
|
|
1252
|
+
}
|
|
1253
|
+
block(element, frame, style) {
|
|
1254
|
+
if (breaks(element, "before")) this.pageBreak();
|
|
1255
|
+
const aligned = {
|
|
1256
|
+
...frame,
|
|
1257
|
+
align: alignOf(element, frame.align)
|
|
1258
|
+
};
|
|
1259
|
+
const size = this.size;
|
|
1260
|
+
const tag = element.tag;
|
|
1261
|
+
if (/^h[1-6]$/.test(tag)) {
|
|
1262
|
+
const level = Number(tag[1]);
|
|
1263
|
+
this.gap(size * (1.5 - level * .08));
|
|
1264
|
+
this.paragraph(this.runs(element.children, {
|
|
1265
|
+
...style,
|
|
1266
|
+
bold: true,
|
|
1267
|
+
scale: HEADING_SCALE[level - 1]
|
|
1268
|
+
}), aligned, false, true);
|
|
1269
|
+
this.gap(size * .5);
|
|
1270
|
+
} else if (tag === "p" || tag === "figure" || tag === "dl" || tag === "details" || tag === "fieldset") {
|
|
1271
|
+
this.gap(size * .75);
|
|
1272
|
+
this.flow(element.children, aligned, style);
|
|
1273
|
+
this.gap(size * .75);
|
|
1274
|
+
} else if (tag === "ul" || tag === "ol" || tag === "menu") this.list(element, aligned, style);
|
|
1275
|
+
else if (tag === "li") this.listItem(element, aligned, style, 16, null);
|
|
1276
|
+
else if (tag === "dt") {
|
|
1277
|
+
this.gap(size * .4);
|
|
1278
|
+
this.flow(element.children, aligned, {
|
|
1279
|
+
...style,
|
|
1280
|
+
bold: true
|
|
1281
|
+
});
|
|
1282
|
+
} else if (tag === "dd") this.flow(element.children, {
|
|
1283
|
+
...aligned,
|
|
1284
|
+
left: frame.left + 18,
|
|
1285
|
+
width: frame.width - 18
|
|
1286
|
+
}, style);
|
|
1287
|
+
else if (tag === "figcaption" || tag === "caption") this.flow(element.children, aligned, {
|
|
1288
|
+
...style,
|
|
1289
|
+
color: this.muted,
|
|
1290
|
+
scale: style.scale * .9
|
|
1291
|
+
});
|
|
1292
|
+
else if (tag === "blockquote") {
|
|
1293
|
+
this.gap(size * .75);
|
|
1294
|
+
this.open(frame);
|
|
1295
|
+
this.flow(element.children, {
|
|
1296
|
+
...aligned,
|
|
1297
|
+
left: frame.left + 14,
|
|
1298
|
+
width: frame.width - 14,
|
|
1299
|
+
bars: [...frame.bars, frame.left + 2]
|
|
1300
|
+
}, {
|
|
1301
|
+
...style,
|
|
1302
|
+
color: this.muted
|
|
1303
|
+
});
|
|
1304
|
+
this.gap(size * .75);
|
|
1305
|
+
} else if (tag === "pre") this.code(element, frame, style);
|
|
1306
|
+
else if (tag === "hr") this.rule(frame);
|
|
1307
|
+
else if (TABLE_TAG.test(tag)) this.table(element, aligned, style);
|
|
1308
|
+
else this.flow(element.children, aligned, style);
|
|
1309
|
+
if (breaks(element, "after")) this.pageBreak();
|
|
1310
|
+
}
|
|
1311
|
+
runs(nodes, style) {
|
|
1312
|
+
const runs = [];
|
|
1313
|
+
for (const node of nodes) if (typeof node === "string") runs.push({
|
|
1314
|
+
text: node,
|
|
1315
|
+
style
|
|
1316
|
+
});
|
|
1317
|
+
else this.inline(node, style, runs);
|
|
1318
|
+
return runs;
|
|
1319
|
+
}
|
|
1320
|
+
gap(value) {
|
|
1321
|
+
if (!this.fresh) this.pending = Math.max(this.pending, value);
|
|
1322
|
+
}
|
|
1323
|
+
open(frame) {
|
|
1324
|
+
this.flushGap(frame);
|
|
1325
|
+
this.fresh = true;
|
|
1326
|
+
}
|
|
1327
|
+
flushGap(frame) {
|
|
1328
|
+
if (this.pending > 0) this.items.push(this.spaceItem(this.pending, frame.bars));
|
|
1329
|
+
this.pending = 0;
|
|
1330
|
+
}
|
|
1331
|
+
emit(item, frame) {
|
|
1332
|
+
this.flushGap(frame);
|
|
1333
|
+
this.fresh = false;
|
|
1334
|
+
this.items.push(item);
|
|
1335
|
+
}
|
|
1336
|
+
pageBreak() {
|
|
1337
|
+
this.pending = 0;
|
|
1338
|
+
this.fresh = true;
|
|
1339
|
+
this.items.push({
|
|
1340
|
+
...this.spaceItem(0, []),
|
|
1341
|
+
space: false,
|
|
1342
|
+
pageBreak: true
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
spaceItem(height, bars) {
|
|
1346
|
+
return {
|
|
1347
|
+
height,
|
|
1348
|
+
space: true,
|
|
1349
|
+
draw: (page, top) => this.drawBars(page, bars, top, height)
|
|
1350
|
+
};
|
|
1351
|
+
}
|
|
1352
|
+
drawBars(page, bars, top, height) {
|
|
1353
|
+
for (const x of bars) page.ops.push(`${rgb(this.border)} rg ${num(x)} ${num(top - height)} 3 ${num(height)} re f`);
|
|
1354
|
+
}
|
|
1355
|
+
fontOf(style) {
|
|
1356
|
+
const { regular, bold, italic, mono, fakeBold } = this.fonts;
|
|
1357
|
+
if (style.code && mono) return {
|
|
1358
|
+
font: mono,
|
|
1359
|
+
fakeBold: false,
|
|
1360
|
+
slant: style.italic
|
|
1361
|
+
};
|
|
1362
|
+
if (style.bold) return {
|
|
1363
|
+
font: bold,
|
|
1364
|
+
fakeBold,
|
|
1365
|
+
slant: style.italic
|
|
1366
|
+
};
|
|
1367
|
+
if (style.italic) return italic ? {
|
|
1368
|
+
font: italic,
|
|
1369
|
+
fakeBold: false,
|
|
1370
|
+
slant: false
|
|
1371
|
+
} : {
|
|
1372
|
+
font: regular,
|
|
1373
|
+
fakeBold: false,
|
|
1374
|
+
slant: true
|
|
1375
|
+
};
|
|
1376
|
+
return {
|
|
1377
|
+
font: regular,
|
|
1378
|
+
fakeBold: false,
|
|
1379
|
+
slant: false
|
|
1380
|
+
};
|
|
1381
|
+
}
|
|
1382
|
+
pieceSize(style) {
|
|
1383
|
+
return this.size * style.scale * (style.code ? .9 : 1);
|
|
1384
|
+
}
|
|
1385
|
+
piece(text, style, space) {
|
|
1386
|
+
const size = this.pieceSize(style);
|
|
1387
|
+
return {
|
|
1388
|
+
text,
|
|
1389
|
+
style,
|
|
1390
|
+
size,
|
|
1391
|
+
space,
|
|
1392
|
+
width: this.fontOf(style).font.width(text, size)
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1395
|
+
tokens(runs, pre) {
|
|
1396
|
+
const tokens = [];
|
|
1397
|
+
for (const run of runs) {
|
|
1398
|
+
if (run.brk) {
|
|
1399
|
+
tokens.push(run.brk);
|
|
1400
|
+
continue;
|
|
1401
|
+
}
|
|
1402
|
+
(pre ? run.text.replace(/\r\n?/g, "\n").replace(/\t/g, " ").split("\n") : [run.text.replace(/[ \t\n\r\f]+/g, " ")]).forEach((part, index) => {
|
|
1403
|
+
if (index > 0) tokens.push("hard");
|
|
1404
|
+
for (const word of part.split(pre ? /( +)/ : /( )/)) if (word) tokens.push(this.piece(word, run.style, word.trim() === ""));
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
return tokens;
|
|
1408
|
+
}
|
|
1409
|
+
layout(runs, width, pre) {
|
|
1410
|
+
const lines = [];
|
|
1411
|
+
let pieces = [];
|
|
1412
|
+
let x = 0;
|
|
1413
|
+
const push = (force) => {
|
|
1414
|
+
if (!pre) while (pieces.length && pieces[pieces.length - 1].space) x -= pieces.pop().width;
|
|
1415
|
+
if (!pieces.length && !force) return;
|
|
1416
|
+
const size = Math.max(this.size * (pre ? .9 : 1), ...pieces.map((piece) => piece.size));
|
|
1417
|
+
lines.push({
|
|
1418
|
+
pieces,
|
|
1419
|
+
width: x,
|
|
1420
|
+
size,
|
|
1421
|
+
height: size * this.lineFactor
|
|
1422
|
+
});
|
|
1423
|
+
pieces = [];
|
|
1424
|
+
x = 0;
|
|
1425
|
+
};
|
|
1426
|
+
for (const token of this.tokens(runs, pre)) {
|
|
1427
|
+
if (token === "hard" || token === "soft") {
|
|
1428
|
+
push(token === "hard");
|
|
1429
|
+
continue;
|
|
1430
|
+
}
|
|
1431
|
+
if (token.space && !pre && (!pieces.length || pieces[pieces.length - 1].space)) continue;
|
|
1432
|
+
if (x + token.width > width && pieces.some((piece) => !piece.space)) {
|
|
1433
|
+
push(true);
|
|
1434
|
+
if (token.space && !pre) continue;
|
|
1435
|
+
}
|
|
1436
|
+
if (token.width > width && !token.space) {
|
|
1437
|
+
let part = "";
|
|
1438
|
+
for (const char of token.text) {
|
|
1439
|
+
const next = part + char;
|
|
1440
|
+
if (part && x + this.piece(next, token.style, false).width > width) {
|
|
1441
|
+
const piece = this.piece(part, token.style, false);
|
|
1442
|
+
pieces.push(piece);
|
|
1443
|
+
x += piece.width;
|
|
1444
|
+
push(true);
|
|
1445
|
+
part = char;
|
|
1446
|
+
} else part = next;
|
|
1447
|
+
}
|
|
1448
|
+
const rest = this.piece(part, token.style, false);
|
|
1449
|
+
pieces.push(rest);
|
|
1450
|
+
x += rest.width;
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
pieces.push(token);
|
|
1454
|
+
x += token.width;
|
|
1455
|
+
}
|
|
1456
|
+
push(false);
|
|
1457
|
+
return lines;
|
|
1458
|
+
}
|
|
1459
|
+
paragraph(runs, frame, pre, keep = false, fill = null, inset = 0) {
|
|
1460
|
+
const lines = this.layout(runs, frame.width - inset * 2, pre);
|
|
1461
|
+
lines.forEach((line, index) => {
|
|
1462
|
+
const marker = this.marker;
|
|
1463
|
+
this.marker = null;
|
|
1464
|
+
this.emit({
|
|
1465
|
+
height: line.height,
|
|
1466
|
+
keep: keep && index === lines.length - 1,
|
|
1467
|
+
draw: (page, top) => {
|
|
1468
|
+
if (fill) page.ops.push(`${rgb(fill)} rg ${num(frame.left)} ${num(top - line.height)} ${num(frame.width)} ${num(line.height)} re f`);
|
|
1469
|
+
this.drawBars(page, frame.bars, top, line.height);
|
|
1470
|
+
const baseline = this.drawLine(page, line, frame, top, inset);
|
|
1471
|
+
if (marker) this.drawMarker(page, marker, baseline);
|
|
1472
|
+
}
|
|
1473
|
+
}, frame);
|
|
1474
|
+
});
|
|
1475
|
+
}
|
|
1476
|
+
baseline(line, top) {
|
|
1477
|
+
const { ascent, descent } = this.fonts;
|
|
1478
|
+
return top - (line.height - (ascent + descent) * line.size) / 2 - ascent * line.size;
|
|
1479
|
+
}
|
|
1480
|
+
drawLine(page, line, frame, top, inset) {
|
|
1481
|
+
const baseline = this.baseline(line, top);
|
|
1482
|
+
const room = frame.width - inset * 2;
|
|
1483
|
+
let x = frame.left + inset + (frame.align === "center" ? (room - line.width) / 2 : frame.align === "end" ? room - line.width : 0);
|
|
1484
|
+
const segments = [];
|
|
1485
|
+
for (const piece of line.pieces) {
|
|
1486
|
+
const last = segments[segments.length - 1];
|
|
1487
|
+
if (last && sameStyle(last.style, piece.style)) segments[segments.length - 1] = {
|
|
1488
|
+
...last,
|
|
1489
|
+
text: last.text + piece.text,
|
|
1490
|
+
width: last.width + piece.width
|
|
1491
|
+
};
|
|
1492
|
+
else segments.push(piece);
|
|
1493
|
+
}
|
|
1494
|
+
for (const segment of segments) {
|
|
1495
|
+
this.drawSegment(page, segment, x, baseline);
|
|
1496
|
+
x += segment.width;
|
|
1497
|
+
}
|
|
1498
|
+
return baseline;
|
|
1499
|
+
}
|
|
1500
|
+
drawSegment(page, piece, x, baseline) {
|
|
1501
|
+
const { style, size, width } = piece;
|
|
1502
|
+
const { ascent, descent } = this.fonts;
|
|
1503
|
+
const y = baseline + style.rise * this.size;
|
|
1504
|
+
const color = style.color ?? this.text;
|
|
1505
|
+
if (style.code && !style.block) page.ops.push(`${rgb(this.codeFill)} rg ${num(x - 1.5)} ${num(y - descent * size)} ${num(width + 3)} ${num((ascent + descent) * size)} re f`);
|
|
1506
|
+
const { font, fakeBold, slant } = this.fontOf(style);
|
|
1507
|
+
const effect = fakeBold ? `2 Tr ${num(size * .03)} w ${rgb(color)} RG ` : "";
|
|
1508
|
+
const position = slant ? `1 0 0.2 1 ${num(x)} ${num(y)} Tm` : `${num(x)} ${num(y)} Td`;
|
|
1509
|
+
page.ops.push(`q BT ${effect}/${font.resource} ${num(size)} Tf ${rgb(color)} rg ${position} <${font.encode(piece.text)}> Tj ET Q`);
|
|
1510
|
+
const stroke = (offset) => page.ops.push(`${rgb(color)} RG ${num(size * .06)} w ${num(x)} ${num(y + offset)} m ${num(x + width)} ${num(y + offset)} l S`);
|
|
1511
|
+
if (style.underline) stroke(-size * .12);
|
|
1512
|
+
if (style.strike) stroke(size * .28);
|
|
1513
|
+
if (style.link) page.links.push({
|
|
1514
|
+
x,
|
|
1515
|
+
y: y - descent * size,
|
|
1516
|
+
width,
|
|
1517
|
+
height: (ascent + descent) * size,
|
|
1518
|
+
uri: style.link
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
drawMarker(page, marker, baseline) {
|
|
1522
|
+
const size = this.size;
|
|
1523
|
+
if (marker.label !== null) {
|
|
1524
|
+
const font = this.fonts.regular;
|
|
1525
|
+
page.ops.push(`BT /${font.resource} ${num(size)} Tf ${rgb(this.text)} rg ${num(marker.right - font.width(marker.label, size))} ${num(baseline)} Td <${font.encode(marker.label)}> Tj ET`);
|
|
1526
|
+
return;
|
|
1527
|
+
}
|
|
1528
|
+
const r = size * .17;
|
|
1529
|
+
const cx = marker.right - r - 1;
|
|
1530
|
+
const cy = baseline + size * .33;
|
|
1531
|
+
const shape = marker.depth % 3;
|
|
1532
|
+
if (shape === 2) {
|
|
1533
|
+
page.ops.push(`${rgb(this.text)} rg ${num(cx - r)} ${num(cy - r)} ${num(r * 2)} ${num(r * 2)} re f`);
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
const k = r * .5523;
|
|
1537
|
+
const circle = `${num(cx + r)} ${num(cy)} m ${num(cx + r)} ${num(cy + k)} ${num(cx + k)} ${num(cy + r)} ${num(cx)} ${num(cy + r)} c ${num(cx - k)} ${num(cy + r)} ${num(cx - r)} ${num(cy + k)} ${num(cx - r)} ${num(cy)} c ${num(cx - r)} ${num(cy - k)} ${num(cx - k)} ${num(cy - r)} ${num(cx)} ${num(cy - r)} c ${num(cx + k)} ${num(cy - r)} ${num(cx + r)} ${num(cy - k)} ${num(cx + r)} ${num(cy)} c`;
|
|
1538
|
+
page.ops.push(shape === 0 ? `${rgb(this.text)} rg ${circle} f` : `${rgb(this.text)} RG ${num(size * .07)} w ${circle} S`);
|
|
1539
|
+
}
|
|
1540
|
+
list(element, frame, style) {
|
|
1541
|
+
const ordered = element.tag === "ol";
|
|
1542
|
+
const items = element.children.filter((child) => typeof child !== "string" && child.tag === "li");
|
|
1543
|
+
const start = Number.parseInt(element.attrs["start"] ?? "", 10);
|
|
1544
|
+
const first = Number.isFinite(start) ? start : 1;
|
|
1545
|
+
const widest = ordered ? Math.max(...items.map((_, index) => this.fonts.regular.width(`${first + index}.`, this.size))) : 0;
|
|
1546
|
+
const indent = Math.max(16, widest + 8);
|
|
1547
|
+
this.gap(this.depth ? this.size * .25 : this.size * .75);
|
|
1548
|
+
this.depth++;
|
|
1549
|
+
let number = first;
|
|
1550
|
+
for (const child of element.children) {
|
|
1551
|
+
if (typeof child === "string") continue;
|
|
1552
|
+
if (child.tag === "li") this.listItem(child, frame, style, indent, ordered ? `${number++}.` : null);
|
|
1553
|
+
else this.flow([child], {
|
|
1554
|
+
...frame,
|
|
1555
|
+
left: frame.left + indent,
|
|
1556
|
+
width: frame.width - indent
|
|
1557
|
+
}, style);
|
|
1558
|
+
}
|
|
1559
|
+
this.depth--;
|
|
1560
|
+
this.gap(this.depth ? this.size * .25 : this.size * .75);
|
|
1561
|
+
}
|
|
1562
|
+
listItem(element, frame, style, indent, label) {
|
|
1563
|
+
const inner = {
|
|
1564
|
+
...frame,
|
|
1565
|
+
left: frame.left + indent,
|
|
1566
|
+
width: frame.width - indent
|
|
1567
|
+
};
|
|
1568
|
+
this.gap(this.size * .3);
|
|
1569
|
+
this.open(frame);
|
|
1570
|
+
this.marker = {
|
|
1571
|
+
label,
|
|
1572
|
+
depth: Math.max(0, this.depth - 1),
|
|
1573
|
+
right: inner.left - 5
|
|
1574
|
+
};
|
|
1575
|
+
this.flow(element.children, inner, style);
|
|
1576
|
+
if (this.marker) this.paragraph([{
|
|
1577
|
+
text: "",
|
|
1578
|
+
style,
|
|
1579
|
+
brk: "hard"
|
|
1580
|
+
}], inner, false);
|
|
1581
|
+
this.gap(this.size * .3);
|
|
1582
|
+
}
|
|
1583
|
+
code(element, frame, style) {
|
|
1584
|
+
const pad = 8;
|
|
1585
|
+
const codeStyle = {
|
|
1586
|
+
...style,
|
|
1587
|
+
code: true,
|
|
1588
|
+
block: true,
|
|
1589
|
+
color: null
|
|
1590
|
+
};
|
|
1591
|
+
const runs = this.runs(element.children, codeStyle);
|
|
1592
|
+
const first = runs.find((run) => !run.brk);
|
|
1593
|
+
if (first) first.text = first.text.replace(/^\r?\n/, "");
|
|
1594
|
+
const last = [...runs].reverse().find((run) => !run.brk);
|
|
1595
|
+
if (last) last.text = last.text.replace(/\r?\n$/, "");
|
|
1596
|
+
this.gap(this.size * .75);
|
|
1597
|
+
const fillItem = () => ({
|
|
1598
|
+
height: pad * .75,
|
|
1599
|
+
draw: (page, top) => {
|
|
1600
|
+
page.ops.push(`${rgb(this.codeFill)} rg ${num(frame.left)} ${num(top - pad * .75)} ${num(frame.width)} ${num(pad * .75)} re f`);
|
|
1601
|
+
this.drawBars(page, frame.bars, top, pad * .75);
|
|
1602
|
+
}
|
|
1603
|
+
});
|
|
1604
|
+
this.emit(fillItem(), frame);
|
|
1605
|
+
this.paragraph(runs, {
|
|
1606
|
+
...frame,
|
|
1607
|
+
align: "start"
|
|
1608
|
+
}, true, false, this.codeFill, pad);
|
|
1609
|
+
this.emit(fillItem(), frame);
|
|
1610
|
+
this.gap(this.size * .75);
|
|
1611
|
+
}
|
|
1612
|
+
rule(frame) {
|
|
1613
|
+
this.gap(this.size * .9);
|
|
1614
|
+
this.emit({
|
|
1615
|
+
height: 1,
|
|
1616
|
+
draw: (page, top) => page.ops.push(`${rgb(this.border)} RG 0.75 w ${num(frame.left)} ${num(top - .5)} m ${num(frame.left + frame.width)} ${num(top - .5)} l S`)
|
|
1617
|
+
}, frame);
|
|
1618
|
+
this.gap(this.size * .9);
|
|
1619
|
+
}
|
|
1620
|
+
table(element, frame, style) {
|
|
1621
|
+
const rows = [];
|
|
1622
|
+
const captions = [];
|
|
1623
|
+
const readRow = (tr, head) => {
|
|
1624
|
+
const cells = tr.children.filter((cell) => typeof cell !== "string" && (cell.tag === "td" || cell.tag === "th")).map((cell) => {
|
|
1625
|
+
const header = cell.tag === "th" || head;
|
|
1626
|
+
return {
|
|
1627
|
+
header,
|
|
1628
|
+
span: Math.max(1, Number.parseInt(cell.attrs["colspan"] ?? "1", 10) || 1),
|
|
1629
|
+
align: alignOf(cell, "start"),
|
|
1630
|
+
runs: this.runs(cell.children, header ? {
|
|
1631
|
+
...style,
|
|
1632
|
+
bold: true
|
|
1633
|
+
} : style)
|
|
1634
|
+
};
|
|
1635
|
+
});
|
|
1636
|
+
if (cells.length) rows.push({
|
|
1637
|
+
cells,
|
|
1638
|
+
head
|
|
1639
|
+
});
|
|
1640
|
+
};
|
|
1641
|
+
const walk = (nodes, head) => {
|
|
1642
|
+
for (const node of nodes) {
|
|
1643
|
+
if (typeof node === "string") continue;
|
|
1644
|
+
if (node.tag === "tr") readRow(node, head);
|
|
1645
|
+
else if (node.tag === "caption") captions.push(node);
|
|
1646
|
+
else if (node.tag === "thead" || node.tag === "tbody" || node.tag === "tfoot") walk(node.children, node.tag === "thead");
|
|
1647
|
+
}
|
|
1648
|
+
};
|
|
1649
|
+
walk(element.children, false);
|
|
1650
|
+
if (!rows.some((row) => row.head) && rows.length > 1 && rows[0].cells.every((cell) => cell.header)) rows[0].head = true;
|
|
1651
|
+
const count = Math.max(0, ...rows.map((row) => row.cells.reduce((sum, cell) => sum + cell.span, 0)));
|
|
1652
|
+
this.gap(this.size * .75);
|
|
1653
|
+
for (const caption of captions) this.block(caption, {
|
|
1654
|
+
...frame,
|
|
1655
|
+
align: "center"
|
|
1656
|
+
}, style);
|
|
1657
|
+
if (!count) {
|
|
1658
|
+
this.gap(this.size * .75);
|
|
1659
|
+
return;
|
|
1660
|
+
}
|
|
1661
|
+
const padX = 5;
|
|
1662
|
+
const padY = 3.5;
|
|
1663
|
+
const natural = new Array(count).fill(0);
|
|
1664
|
+
const minimum = new Array(count).fill(0);
|
|
1665
|
+
for (const row of rows) {
|
|
1666
|
+
let column = 0;
|
|
1667
|
+
for (const cell of row.cells) {
|
|
1668
|
+
if (cell.span === 1 && column < count) {
|
|
1669
|
+
const lines = this.layout(cell.runs, Number.POSITIVE_INFINITY, false);
|
|
1670
|
+
natural[column] = Math.max(natural[column], ...lines.map((line) => line.width + 10));
|
|
1671
|
+
const words = lines.flatMap((line) => line.pieces.filter((piece) => !piece.space).map((piece) => piece.width));
|
|
1672
|
+
minimum[column] = Math.max(minimum[column], ...words.map((width) => width + 10));
|
|
1673
|
+
}
|
|
1674
|
+
column += cell.span;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
const xs = this.columnWidths(natural, minimum, frame.width).reduce((acc, width, index) => [...acc, acc[index] + width], [frame.left]);
|
|
1678
|
+
const header = [];
|
|
1679
|
+
const items = rows.map((row) => {
|
|
1680
|
+
let column = 0;
|
|
1681
|
+
const cells = row.cells.map((cell) => {
|
|
1682
|
+
const from = Math.min(column, count - 1);
|
|
1683
|
+
column += cell.span;
|
|
1684
|
+
const to = Math.min(column, count);
|
|
1685
|
+
const x = xs[from];
|
|
1686
|
+
const width = xs[to] - x;
|
|
1687
|
+
return {
|
|
1688
|
+
x,
|
|
1689
|
+
width,
|
|
1690
|
+
align: cell.align,
|
|
1691
|
+
header: cell.header,
|
|
1692
|
+
lines: this.layout(cell.runs, Math.max(1, width - 10), false)
|
|
1693
|
+
};
|
|
1694
|
+
});
|
|
1695
|
+
const item = this.rowItem(cells, padX, padY, row.head ? void 0 : header);
|
|
1696
|
+
if (row.head) header.push(item);
|
|
1697
|
+
return item;
|
|
1698
|
+
});
|
|
1699
|
+
if (header.length) header[header.length - 1].keep = true;
|
|
1700
|
+
for (const item of items) this.emit(item, frame);
|
|
1701
|
+
this.gap(this.size * .75);
|
|
1702
|
+
}
|
|
1703
|
+
columnWidths(natural, minimum, available) {
|
|
1704
|
+
const sum = (values) => values.reduce((total, value) => total + value, 0);
|
|
1705
|
+
const wanted = natural.map((value, index) => Math.max(value, minimum[index], 12));
|
|
1706
|
+
if (sum(wanted) <= available) {
|
|
1707
|
+
const total = sum(wanted);
|
|
1708
|
+
return wanted.map((value) => value * available / total);
|
|
1709
|
+
}
|
|
1710
|
+
const floor = minimum.map((value) => Math.max(12, Math.min(value, available / natural.length)));
|
|
1711
|
+
const room = available - sum(floor);
|
|
1712
|
+
const extra = wanted.map((value, index) => value - floor[index]);
|
|
1713
|
+
const extraSum = sum(extra);
|
|
1714
|
+
if (room <= 0 || extraSum <= 0) {
|
|
1715
|
+
const total = sum(floor);
|
|
1716
|
+
return floor.map((value) => value * available / total);
|
|
1717
|
+
}
|
|
1718
|
+
return floor.map((value, index) => value + extra[index] * room / extraSum);
|
|
1719
|
+
}
|
|
1720
|
+
rowItem(cells, padX, padY, header) {
|
|
1721
|
+
const contentHeight = (lines) => lines.reduce((total, line) => total + line.height, 0);
|
|
1722
|
+
const height = Math.max(this.size * this.lineFactor, ...cells.map((cell) => contentHeight(cell.lines))) + padY * 2;
|
|
1723
|
+
return {
|
|
1724
|
+
height,
|
|
1725
|
+
header,
|
|
1726
|
+
draw: (page, top) => {
|
|
1727
|
+
for (const cell of cells) {
|
|
1728
|
+
if (cell.header && this.headerFill) page.ops.push(`${rgb(this.headerFill)} rg ${num(cell.x)} ${num(top - height)} ${num(cell.width)} ${num(height)} re f`);
|
|
1729
|
+
let y = top - padY;
|
|
1730
|
+
for (const line of cell.lines) {
|
|
1731
|
+
this.drawLine(page, line, {
|
|
1732
|
+
left: cell.x,
|
|
1733
|
+
width: cell.width,
|
|
1734
|
+
bars: [],
|
|
1735
|
+
align: cell.align
|
|
1736
|
+
}, y, padX);
|
|
1737
|
+
y -= line.height;
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
for (const cell of cells) page.ops.push(`${rgb(this.border)} RG 0.5 w ${num(cell.x)} ${num(top - height)} ${num(cell.width)} ${num(height)} re S`);
|
|
1741
|
+
},
|
|
1742
|
+
split: (room) => {
|
|
1743
|
+
const fit = room - padY * 2;
|
|
1744
|
+
const counts = cells.map((cell) => {
|
|
1745
|
+
let used = 0;
|
|
1746
|
+
let taken = 0;
|
|
1747
|
+
while (taken < cell.lines.length && used + cell.lines[taken].height <= fit) used += cell.lines[taken++].height;
|
|
1748
|
+
return taken;
|
|
1749
|
+
});
|
|
1750
|
+
if (counts.every((taken) => taken === 0) || counts.every((taken, index) => taken === cells[index].lines.length)) return null;
|
|
1751
|
+
const part = (from) => cells.map((cell, index) => ({
|
|
1752
|
+
...cell,
|
|
1753
|
+
lines: from(cell, index)
|
|
1754
|
+
}));
|
|
1755
|
+
return [this.rowItem(part((cell, index) => cell.lines.slice(0, counts[index])), padX, padY, header), this.rowItem(part((cell, index) => cell.lines.slice(counts[index])), padX, padY, header)];
|
|
1756
|
+
}
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
};
|
|
1760
|
+
function paginate(items, height, top, bottom) {
|
|
1761
|
+
const pages = [];
|
|
1762
|
+
let page = {
|
|
1763
|
+
ops: [],
|
|
1764
|
+
links: []
|
|
1765
|
+
};
|
|
1766
|
+
let y = 0;
|
|
1767
|
+
let atTop = true;
|
|
1768
|
+
const newPage = () => {
|
|
1769
|
+
page = {
|
|
1770
|
+
ops: [],
|
|
1771
|
+
links: []
|
|
1772
|
+
};
|
|
1773
|
+
pages.push(page);
|
|
1774
|
+
y = height - top;
|
|
1775
|
+
atTop = true;
|
|
1776
|
+
};
|
|
1777
|
+
const place = (item) => {
|
|
1778
|
+
item.draw(page, y);
|
|
1779
|
+
y -= item.height;
|
|
1780
|
+
atTop = false;
|
|
1781
|
+
};
|
|
1782
|
+
const breakPage = (item) => {
|
|
1783
|
+
newPage();
|
|
1784
|
+
for (const head of item.header ?? []) place(head);
|
|
1785
|
+
};
|
|
1786
|
+
newPage();
|
|
1787
|
+
for (let index = 0; index < items.length; index++) {
|
|
1788
|
+
let item = items[index];
|
|
1789
|
+
if (item.pageBreak) {
|
|
1790
|
+
if (!atTop) newPage();
|
|
1791
|
+
continue;
|
|
1792
|
+
}
|
|
1793
|
+
if (item.space) {
|
|
1794
|
+
if (item.height > y - bottom) newPage();
|
|
1795
|
+
else place(item);
|
|
1796
|
+
continue;
|
|
1797
|
+
}
|
|
1798
|
+
let keepRoom = 0;
|
|
1799
|
+
if (item.keep) {
|
|
1800
|
+
let next = index + 1;
|
|
1801
|
+
while (items[next]?.space) keepRoom += items[next++].height;
|
|
1802
|
+
keepRoom = items[next] && !items[next].pageBreak ? keepRoom + Math.min(items[next].height, 30) : 0;
|
|
1803
|
+
}
|
|
1804
|
+
if (!atTop && item.height + keepRoom > y - bottom) {
|
|
1805
|
+
const parts = item.split && !keepRoom ? item.split(y - bottom) : null;
|
|
1806
|
+
if (parts) {
|
|
1807
|
+
place(parts[0]);
|
|
1808
|
+
item = parts[1];
|
|
1809
|
+
}
|
|
1810
|
+
breakPage(item);
|
|
1811
|
+
}
|
|
1812
|
+
while (item.split && item.height > y - bottom) {
|
|
1813
|
+
const parts = item.split(y - bottom);
|
|
1814
|
+
if (!parts) break;
|
|
1815
|
+
place(parts[0]);
|
|
1816
|
+
breakPage(item);
|
|
1817
|
+
item = parts[1];
|
|
1818
|
+
}
|
|
1819
|
+
place(item);
|
|
1820
|
+
}
|
|
1821
|
+
return pages;
|
|
1822
|
+
}
|
|
1823
|
+
async function writeHtmlPdf(source, options = {}, date = /* @__PURE__ */ new Date()) {
|
|
1824
|
+
const font = await (options.font ?? loadDefaultExportFont)();
|
|
1825
|
+
const loaded = await loadPdfFonts(font);
|
|
1826
|
+
const regular = new PdfFont(loaded.regular, "F1", "TXREGU");
|
|
1827
|
+
const bold = loaded.bold ? new PdfFont(loaded.bold, "F2", "TXBOLD") : regular;
|
|
1828
|
+
const italicFont = loadOptionalFont(font.italic);
|
|
1829
|
+
const monoFont = loadOptionalFont(font.mono);
|
|
1830
|
+
const fonts = {
|
|
1831
|
+
regular,
|
|
1832
|
+
bold,
|
|
1833
|
+
italic: italicFont ? new PdfFont(italicFont, "F3", "TXITAL") : null,
|
|
1834
|
+
mono: monoFont ? new PdfFont(monoFont, "F4", "TXMONO") : null,
|
|
1835
|
+
fakeBold: bold === regular,
|
|
1836
|
+
ascent: loaded.regular.ascent / loaded.regular.unitsPerEm,
|
|
1837
|
+
descent: -loaded.regular.descent / loaded.regular.unitsPerEm
|
|
1838
|
+
};
|
|
1839
|
+
const page = pageGeometry(options.pageSize, options.orientation === "landscape" ? "landscape" : "portrait", options.margin ?? 48, 0);
|
|
1840
|
+
const [top, right, bottom, left] = page.margin;
|
|
1841
|
+
const layout = new HtmlPdfLayout(fonts, options);
|
|
1842
|
+
const tree = typeof source === "string" ? parseHtml(source) : readDom(source);
|
|
1843
|
+
layout.flow(tree.children, {
|
|
1844
|
+
left,
|
|
1845
|
+
width: page.width - left - right,
|
|
1846
|
+
bars: [],
|
|
1847
|
+
align: "start"
|
|
1848
|
+
}, PLAIN);
|
|
1849
|
+
const pages = paginate(layout.items, page.height, top, bottom);
|
|
1850
|
+
if (options.pageNumbers !== false) {
|
|
1851
|
+
const muted = hexColor(options.mutedColor, "667085");
|
|
1852
|
+
const size = layout.size * .85;
|
|
1853
|
+
pages.forEach((pdfPage, index) => {
|
|
1854
|
+
const label = `${index + 1} / ${pages.length}`;
|
|
1855
|
+
pdfPage.ops.push(`BT /${regular.resource} ${num(size)} Tf ${rgb(muted)} rg ${num((page.width - regular.width(label, size)) / 2)} ${num(Math.max(12, bottom / 2 - size / 2))} Td <${regular.encode(label)}> Tj ET`);
|
|
1856
|
+
});
|
|
1857
|
+
}
|
|
1858
|
+
const used = [
|
|
1859
|
+
regular,
|
|
1860
|
+
bold,
|
|
1861
|
+
fonts.italic,
|
|
1862
|
+
fonts.mono
|
|
1863
|
+
].filter((pdfFont, index) => !!pdfFont && (index === 0 || pdfFont.used.size > 0));
|
|
1864
|
+
return assemblePdf({
|
|
1865
|
+
width: page.width,
|
|
1866
|
+
height: page.height,
|
|
1867
|
+
pages,
|
|
1868
|
+
fonts: [...new Set(used)],
|
|
1869
|
+
title: options.title,
|
|
1870
|
+
author: options.author,
|
|
1871
|
+
compress: options.compress,
|
|
1872
|
+
date
|
|
1873
|
+
});
|
|
1874
|
+
}
|
|
673
1875
|
function wrapText(text, width, measure) {
|
|
674
1876
|
const lines = [];
|
|
675
1877
|
for (const paragraph of text.replace(/\t/g, " ").split(/\r?\n/)) {
|
|
@@ -724,12 +1926,6 @@ function layoutColumns(table, texts, regular, bold, size, padding, available) {
|
|
|
724
1926
|
if (total > available) widths = widths.map((width) => width * available / total);
|
|
725
1927
|
return { widths };
|
|
726
1928
|
}
|
|
727
|
-
async function loadPdfFonts(font) {
|
|
728
|
-
return {
|
|
729
|
-
regular: parseTrueType(toBytes(font.regular)),
|
|
730
|
-
bold: font.bold ? parseTrueType(toBytes(font.bold)) : null
|
|
731
|
-
};
|
|
732
|
-
}
|
|
733
1929
|
async function writePdf(table, options, font, context = {
|
|
734
1930
|
yes: "Yes",
|
|
735
1931
|
no: "No"
|
|
@@ -842,78 +2038,19 @@ async function writePdf(table, options, font, context = {
|
|
|
842
2038
|
const footerSize = size * .85;
|
|
843
2039
|
drawText(label, (page.width - regular.width(label, footerSize)) / 2, Math.max(12, bottom / 2 - footerSize / 2), regular, footerSize, muted);
|
|
844
2040
|
});
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
body,
|
|
859
|
-
utf8("\nendstream\nendobj\n")
|
|
860
|
-
]));
|
|
861
|
-
};
|
|
862
|
-
const catalog = reserve();
|
|
863
|
-
const pagesId = reserve();
|
|
864
|
-
const info = reserve();
|
|
865
|
-
const fontIds = /* @__PURE__ */ new Map();
|
|
866
|
-
for (const pdfFont of /* @__PURE__ */ new Set([regular, bold])) fontIds.set(pdfFont, reserve());
|
|
867
|
-
const pageIds = pages.map(() => [reserve(), reserve()]);
|
|
868
|
-
for (const [index, [pageId, contentId]] of pageIds.entries()) {
|
|
869
|
-
const fontsDict = [...fontIds].map(([pdfFont, id]) => `/${pdfFont.resource} ${id} 0 R`).join(" ");
|
|
870
|
-
set(pageId, `<< /Type /Page /Parent ${pagesId} 0 R /MediaBox [0 0 ${num(page.width)} ${num(page.height)}] /Resources << /Font << ${fontsDict} >> >> /Contents ${contentId} 0 R >>`);
|
|
871
|
-
await stream(contentId, "", utf8(pages[index].join("\n")));
|
|
872
|
-
}
|
|
873
|
-
for (const [pdfFont, id] of fontIds) {
|
|
874
|
-
const ttf = pdfFont.ttf;
|
|
875
|
-
const scale = (value) => Math.round(value * 1e3 / ttf.unitsPerEm);
|
|
876
|
-
const glyphs = [...pdfFont.used.keys()].sort((a, b) => a - b);
|
|
877
|
-
const baseFont = `${pdfFont.tag}+${ttf.name}`;
|
|
878
|
-
const cid = reserve();
|
|
879
|
-
const descriptor = reserve();
|
|
880
|
-
const file = reserve();
|
|
881
|
-
const toUnicode = reserve();
|
|
882
|
-
set(id, `<< /Type /Font /Subtype /Type0 /BaseFont /${baseFont} /Encoding /Identity-H /DescendantFonts [${cid} 0 R] /ToUnicode ${toUnicode} 0 R >>`);
|
|
883
|
-
const w = glyphs.map((glyph) => `${glyph} [${scale(ttf.advance(glyph))}]`).join(" ");
|
|
884
|
-
set(cid, `<< /Type /Font /Subtype /CIDFontType2 /BaseFont /${baseFont} /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor ${descriptor} 0 R /DW ${scale(ttf.advance(0))} /W [${w}] /CIDToGIDMap /Identity >>`);
|
|
885
|
-
set(descriptor, `<< /Type /FontDescriptor /FontName /${baseFont} /Flags 32 /FontBBox [${ttf.bbox.map(scale).join(" ")}] /ItalicAngle 0 /Ascent ${scale(ttf.ascent)} /Descent ${scale(ttf.descent)} /CapHeight ${scale(ttf.capHeight)} /StemV 80 /FontFile2 ${file} 0 R >>`);
|
|
886
|
-
const subset = ttf.subset(new Set(glyphs));
|
|
887
|
-
await stream(file, `/Length1 ${subset.length}`, subset);
|
|
888
|
-
const entries = glyphs.map((glyph) => `<${glyph.toString(16).padStart(4, "0")}> <${utf16Hex(String.fromCodePoint(pdfFont.used.get(glyph) ?? 65533))}>`);
|
|
889
|
-
const chunks = [];
|
|
890
|
-
for (let index = 0; index < entries.length; index += 100) {
|
|
891
|
-
const chunk = entries.slice(index, index + 100);
|
|
892
|
-
chunks.push(`${chunk.length} beginbfchar\n${chunk.join("\n")}\nendbfchar`);
|
|
893
|
-
}
|
|
894
|
-
await stream(toUnicode, "", utf8(`/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n/CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n${chunks.join("\n")}\nendcmap\nCMapName currentdict /CMap defineresource pop\nend\nend`));
|
|
895
|
-
}
|
|
896
|
-
set(catalog, `<< /Type /Catalog /Pages ${pagesId} 0 R >>`);
|
|
897
|
-
set(pagesId, `<< /Type /Pages /Kids [${pageIds.map(([pageId]) => `${pageId} 0 R`).join(" ")}] /Count ${pageIds.length} >>`);
|
|
898
|
-
const infoParts = [`/Producer (tectra)`, `/CreationDate (${pdfDate(date)})`];
|
|
899
|
-
if (options.title) infoParts.push(`/Title ${pdfString(options.title)}`);
|
|
900
|
-
if (options.author) infoParts.push(`/Author ${pdfString(options.author)}`);
|
|
901
|
-
set(info, `<< ${infoParts.join(" ")} >>`);
|
|
902
|
-
const header = utf8("%PDF-1.7\n%âãÏÓ\n");
|
|
903
|
-
const offsets = [];
|
|
904
|
-
let offset = header.length;
|
|
905
|
-
for (const object of objects) {
|
|
906
|
-
offsets.push(offset);
|
|
907
|
-
offset += object.length;
|
|
908
|
-
}
|
|
909
|
-
const xref = [`xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`, ...offsets.map((value) => `${String(value).padStart(10, "0")} 00000 n \n`)].join("");
|
|
910
|
-
const trailer = `trailer\n<< /Size ${objects.length + 1} /Root ${catalog} 0 R /Info ${info} 0 R >>\nstartxref\n${offset}\n%%EOF\n`;
|
|
911
|
-
const bytes = concatBytes([
|
|
912
|
-
header,
|
|
913
|
-
...objects,
|
|
914
|
-
utf8(xref + trailer)
|
|
915
|
-
]);
|
|
916
|
-
return new Blob([bytes], { type: "application/pdf" });
|
|
2041
|
+
return assemblePdf({
|
|
2042
|
+
width: page.width,
|
|
2043
|
+
height: page.height,
|
|
2044
|
+
pages: pages.map((pageOps) => ({
|
|
2045
|
+
ops: pageOps,
|
|
2046
|
+
links: []
|
|
2047
|
+
})),
|
|
2048
|
+
fonts: [.../* @__PURE__ */ new Set([regular, bold])],
|
|
2049
|
+
title: options.title,
|
|
2050
|
+
author: options.author,
|
|
2051
|
+
compress: options.compress,
|
|
2052
|
+
date
|
|
2053
|
+
});
|
|
917
2054
|
}
|
|
918
2055
|
function printHtml(document, html) {
|
|
919
2056
|
return new Promise((resolve) => {
|
|
@@ -1231,9 +2368,6 @@ function mergeExportOptions(...layers) {
|
|
|
1231
2368
|
}
|
|
1232
2369
|
return merged;
|
|
1233
2370
|
}
|
|
1234
|
-
function loadDefaultExportFont() {
|
|
1235
|
-
return import("@tectra-ng/ui/export-font").then((module) => module.TX_EXPORT_FONT);
|
|
1236
|
-
}
|
|
1237
2371
|
var TxExportService = class TxExportService {
|
|
1238
2372
|
document = inject(DOCUMENT);
|
|
1239
2373
|
defaults = inject(TX_EXPORT_OPTIONS);
|
|
@@ -1327,6 +2461,25 @@ var TxExportService = class TxExportService {
|
|
|
1327
2461
|
};
|
|
1328
2462
|
}
|
|
1329
2463
|
}
|
|
2464
|
+
async exportHtmlPdf(source, options = {}) {
|
|
2465
|
+
const { blob, filename } = await this.htmlPdfFile(source, options);
|
|
2466
|
+
downloadBlob(this.document, blob, filename);
|
|
2467
|
+
return filename;
|
|
2468
|
+
}
|
|
2469
|
+
async htmlPdfFile(source, options = {}) {
|
|
2470
|
+
const { filename, ...pdf } = options;
|
|
2471
|
+
const defaults = {
|
|
2472
|
+
title: this.defaults.title,
|
|
2473
|
+
...this.defaults.pdf
|
|
2474
|
+
};
|
|
2475
|
+
return {
|
|
2476
|
+
filename: withExtension(filename || this.defaults.filename || "ExportedData", "pdf"),
|
|
2477
|
+
blob: await writeHtmlPdf(source, {
|
|
2478
|
+
...defaults,
|
|
2479
|
+
...pdf
|
|
2480
|
+
})
|
|
2481
|
+
};
|
|
2482
|
+
}
|
|
1330
2483
|
textContext(options = {}) {
|
|
1331
2484
|
const translations = this.locale.translations();
|
|
1332
2485
|
return {
|
|
@@ -2594,6 +3747,6 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
2594
3747
|
}]
|
|
2595
3748
|
}
|
|
2596
3749
|
});
|
|
2597
|
-
export { TX_EXPORT_EXTENSIONS, TX_EXPORT_FORMATS, TX_EXPORT_ICONS, TX_EXPORT_OPTIONS, TxExport, TxExportAction, TxExportButton, TxExportCsv, TxExportExcel, TxExportPdf, TxExportService, TxExportWord, buildExportTable, cellText, columnName, crc32, csvCell, docxDocument, downloadBlob, downloadCsv, escapeXml, excelSerial, excelWidths, exportMatrix, fieldsFor, hexColor, htmlTable, inferType, loadDefaultExportFont, localIso, matrixToCsv, mergeExportOptions, parseTrueType, provideTxExport, readExportField, safeExportText, sheetName, toDate, withExtension, wrapText, writeCsv, writeDocx, writeHtml, writeJson, writeMarkdown, writePdf, writeTsv, writeXlsx, xlsxParts, zip };
|
|
3750
|
+
export { TX_EXPORT_EXTENSIONS, TX_EXPORT_FORMATS, TX_EXPORT_ICONS, TX_EXPORT_OPTIONS, TxExport, TxExportAction, TxExportButton, TxExportCsv, TxExportExcel, TxExportPdf, TxExportService, TxExportWord, buildExportTable, cellText, columnName, crc32, csvCell, docxDocument, downloadBlob, downloadCsv, escapeXml, excelSerial, excelWidths, exportMatrix, fieldsFor, hexColor, htmlTable, inferType, loadDefaultExportFont, localIso, matrixToCsv, mergeExportOptions, parseTrueType, provideTxExport, readExportField, safeExportText, sheetName, toDate, withExtension, wrapText, writeCsv, writeDocx, writeHtml, writeHtmlPdf, writeJson, writeMarkdown, writePdf, writeTsv, writeXlsx, xlsxParts, zip };
|
|
2598
3751
|
|
|
2599
3752
|
//# sourceMappingURL=tectra-ng-ui-export.mjs.map
|