@jasy/pdf 1.0.0-alpha.2 → 1.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +24 -14
  3. package/dist/api/args.d.ts +1 -1
  4. package/dist/api/args.js +2 -5
  5. package/dist/api/color.d.ts +4 -4
  6. package/dist/api/color.js +11 -17
  7. package/dist/api/content.d.ts +8 -8
  8. package/dist/api/content.js +23 -24
  9. package/dist/api/descriptor.d.ts +2 -2
  10. package/dist/api/descriptor.js +75 -31
  11. package/dist/api/index.d.ts +8 -8
  12. package/dist/api/index.js +8 -24
  13. package/dist/api/insets.js +4 -8
  14. package/dist/api/layout.d.ts +18 -16
  15. package/dist/api/layout.js +41 -52
  16. package/dist/api/structure.d.ts +65 -13
  17. package/dist/api/structure.js +140 -88
  18. package/dist/api/table.d.ts +5 -5
  19. package/dist/api/table.js +28 -24
  20. package/dist/api/text.d.ts +27 -2
  21. package/dist/api/text.js +45 -27
  22. package/dist/assets/font-data.d.ts +2 -0
  23. package/dist/assets/font-data.js +21 -0
  24. package/dist/assets/font-data.ts +37 -0
  25. package/dist/common/color.js +1 -5
  26. package/dist/constants/page-sizes.js +3 -6
  27. package/dist/constants/pdf-parts.js +1 -4
  28. package/dist/crypto/security-handler.d.ts +46 -0
  29. package/dist/crypto/security-handler.js +129 -0
  30. package/dist/crypto/webcrypto.d.ts +11 -0
  31. package/dist/crypto/webcrypto.js +62 -0
  32. package/dist/elements/container-element.d.ts +4 -4
  33. package/dist/elements/container-element.js +9 -13
  34. package/dist/elements/image-element.d.ts +18 -2
  35. package/dist/elements/image-element.js +81 -105
  36. package/dist/elements/index.d.ts +12 -11
  37. package/dist/elements/index.js +12 -29
  38. package/dist/elements/layout/default-text-style-element.d.ts +30 -0
  39. package/dist/elements/layout/default-text-style-element.js +47 -0
  40. package/dist/elements/layout/deferred-element.d.ts +3 -3
  41. package/dist/elements/layout/deferred-element.js +4 -8
  42. package/dist/elements/layout/expanded-element.d.ts +3 -3
  43. package/dist/elements/layout/expanded-element.js +10 -14
  44. package/dist/elements/layout/padding-element.d.ts +3 -3
  45. package/dist/elements/layout/padding-element.js +9 -14
  46. package/dist/elements/layout/positioned-element.d.ts +17 -4
  47. package/dist/elements/layout/positioned-element.js +29 -25
  48. package/dist/elements/layout/repeating-header-element.d.ts +3 -3
  49. package/dist/elements/layout/repeating-header-element.js +8 -12
  50. package/dist/elements/layout/sized-container-element.d.ts +2 -2
  51. package/dist/elements/layout/sized-container-element.js +6 -11
  52. package/dist/elements/line-element.d.ts +3 -3
  53. package/dist/elements/line-element.js +5 -10
  54. package/dist/elements/page-element.d.ts +8 -6
  55. package/dist/elements/page-element.js +20 -23
  56. package/dist/elements/pdf-document-element.d.ts +10 -4
  57. package/dist/elements/pdf-document-element.js +11 -10
  58. package/dist/elements/pdf-element.d.ts +12 -3
  59. package/dist/elements/pdf-element.js +10 -19
  60. package/dist/elements/rectangle-element.d.ts +5 -5
  61. package/dist/elements/rectangle-element.js +19 -25
  62. package/dist/elements/row-element.d.ts +3 -3
  63. package/dist/elements/row-element.js +7 -11
  64. package/dist/elements/text-element.d.ts +37 -11
  65. package/dist/elements/text-element.js +64 -39
  66. package/dist/index.d.ts +3 -3
  67. package/dist/index.js +3 -19
  68. package/dist/ir/display-list.d.ts +4 -2
  69. package/dist/ir/display-list.js +1 -2
  70. package/dist/layout/box-constraints.js +2 -6
  71. package/dist/layout/fragmentation.d.ts +8 -1
  72. package/dist/layout/fragmentation.js +22 -10
  73. package/dist/platform/browser-fs.d.ts +2 -0
  74. package/dist/platform/browser-fs.js +9 -0
  75. package/dist/platform/browser-image.d.ts +5 -0
  76. package/dist/platform/browser-image.js +13 -0
  77. package/dist/platform/node-fs.d.ts +2 -0
  78. package/dist/platform/node-fs.js +10 -0
  79. package/dist/platform/node-image.d.ts +5 -0
  80. package/dist/platform/node-image.js +9 -0
  81. package/dist/renderer/container-renderer.d.ts +3 -3
  82. package/dist/renderer/container-renderer.js +12 -27
  83. package/dist/renderer/default-text-style-renderer.d.ts +6 -0
  84. package/dist/renderer/default-text-style-renderer.js +10 -0
  85. package/dist/renderer/deferred-renderer.d.ts +3 -3
  86. package/dist/renderer/deferred-renderer.js +8 -23
  87. package/dist/renderer/expanded-renderer.d.ts +3 -3
  88. package/dist/renderer/expanded-renderer.js +6 -21
  89. package/dist/renderer/image-renderer.d.ts +3 -3
  90. package/dist/renderer/image-renderer.js +77 -75
  91. package/dist/renderer/index.d.ts +10 -10
  92. package/dist/renderer/index.js +10 -26
  93. package/dist/renderer/line-renderer.d.ts +3 -3
  94. package/dist/renderer/line-renderer.js +13 -28
  95. package/dist/renderer/padding-renderer.d.ts +3 -3
  96. package/dist/renderer/padding-renderer.js +6 -21
  97. package/dist/renderer/page-renderer.d.ts +2 -2
  98. package/dist/renderer/page-renderer.js +61 -77
  99. package/dist/renderer/pdf-backend.d.ts +2 -2
  100. package/dist/renderer/pdf-backend.js +21 -19
  101. package/dist/renderer/pdf-config.js +4 -7
  102. package/dist/renderer/pdf-document-class.d.ts +5 -5
  103. package/dist/renderer/pdf-document-class.js +24 -41
  104. package/dist/renderer/pdf-document-renderer.d.ts +3 -3
  105. package/dist/renderer/pdf-document-renderer.js +71 -85
  106. package/dist/renderer/pdf-renderer.d.ts +2 -2
  107. package/dist/renderer/pdf-renderer.js +85 -93
  108. package/dist/renderer/positioned-renderer.d.ts +3 -3
  109. package/dist/renderer/positioned-renderer.js +8 -23
  110. package/dist/renderer/rectangle-renderer.d.ts +3 -3
  111. package/dist/renderer/rectangle-renderer.js +45 -52
  112. package/dist/renderer/repeating-header-renderer.d.ts +3 -3
  113. package/dist/renderer/repeating-header-renderer.js +11 -26
  114. package/dist/renderer/row-renderer.d.ts +3 -3
  115. package/dist/renderer/row-renderer.js +12 -27
  116. package/dist/renderer/text-renderer.d.ts +6 -5
  117. package/dist/renderer/text-renderer.js +33 -42
  118. package/dist/text/line-breaker.d.ts +8 -5
  119. package/dist/text/line-breaker.js +67 -16
  120. package/dist/text/text-style.d.ts +25 -0
  121. package/dist/text/text-style.js +29 -0
  122. package/dist/utils/afm-parser.js +3 -13
  123. package/dist/utils/bytes.d.ts +24 -0
  124. package/dist/utils/bytes.js +76 -0
  125. package/dist/utils/flex-layout.d.ts +2 -2
  126. package/dist/utils/flex-layout.js +15 -20
  127. package/dist/utils/font-metrics.d.ts +1 -1
  128. package/dist/utils/font-metrics.js +1 -2
  129. package/dist/utils/font-path.js +3 -6
  130. package/dist/utils/image-helper.d.ts +6 -5
  131. package/dist/utils/image-helper.js +101 -111
  132. package/dist/utils/md5.d.ts +4 -0
  133. package/dist/utils/md5.js +79 -0
  134. package/dist/utils/pdf-object-manager.d.ts +18 -6
  135. package/dist/utils/pdf-object-manager.js +0 -0
  136. package/dist/utils/renderer-registry.js +1 -5
  137. package/dist/utils/ttf-parser.d.ts +2 -2
  138. package/dist/utils/ttf-parser.js +32 -36
  139. package/dist/utils/ttf-subsetter.d.ts +1 -1
  140. package/dist/utils/ttf-subsetter.js +40 -42
  141. package/dist/utils/utf8-to-windows1252-encoder.js +1 -4
  142. package/dist/validators/element-validator.d.ts +2 -2
  143. package/dist/validators/element-validator.js +9 -13
  144. package/package.json +14 -2
package/dist/api/table.js CHANGED
@@ -1,13 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Table = Table;
4
- const box_constraints_1 = require("../layout/box-constraints");
5
- const repeating_header_element_1 = require("../elements/layout/repeating-header-element");
6
- const deferred_element_1 = require("../elements/layout/deferred-element");
7
- const layout_1 = require("./layout");
8
- const text_1 = require("./text");
1
+ import { BoxConstraints } from "../layout/box-constraints.js";
2
+ import { RepeatingHeaderElement } from "../elements/layout/repeating-header-element.js";
3
+ import { DeferredElement } from "../elements/layout/deferred-element.js";
4
+ import { Column, Row, Box, Expanded, Padding } from "./layout.js";
5
+ import { Text } from "./text.js";
9
6
  function asElement(c) {
10
- return typeof c === "string" ? (0, text_1.Text)(c) : c;
7
+ return typeof c === "string" ? Text(c) : c;
11
8
  }
12
9
  /** `"2fr"` → 2, `"fr"` → 1; not a fraction → null. */
13
10
  function fractionOf(col) {
@@ -18,8 +15,8 @@ function fractionOf(col) {
18
15
  }
19
16
  /** The cell's unbounded (max-content) width - what an `"auto"` column sizes to. */
20
17
  function naturalWidth(cell, cellPadding, ctx) {
21
- const el = cellPadding !== undefined ? (0, layout_1.Padding)(cellPadding, asElement(cell)) : asElement(cell);
22
- return el.calculateLayout(box_constraints_1.BoxConstraints.loose(Infinity, Infinity), { x: 0, y: 0 }, ctx).width;
18
+ const el = cellPadding !== undefined ? Padding(cellPadding, asElement(cell)) : asElement(cell);
19
+ return el.calculateLayout(BoxConstraints.loose(Infinity, Infinity), { x: 0, y: 0 }, ctx).width;
23
20
  }
24
21
  /** Replaces every `"auto"` column with the widest cell width measured down that column. */
25
22
  function resolveAutoColumns(opts, rows, ctx) {
@@ -40,37 +37,44 @@ function resolveAutoColumns(opts, rows, ctx) {
40
37
  }
41
38
  /** Builds the table tree for already-resolved column widths (no `"auto"`). */
42
39
  function composeTable(opts, rows, columns) {
43
- var _a, _b, _c, _d;
44
40
  const { cellPadding } = opts;
45
- const colGap = (_b = (_a = opts.colGap) !== null && _a !== void 0 ? _a : opts.gap) !== null && _b !== void 0 ? _b : 0;
46
- const rowGap = (_d = (_c = opts.rowGap) !== null && _c !== void 0 ? _c : opts.gap) !== null && _d !== void 0 ? _d : 0;
41
+ const colGap = opts.colGap ?? opts.gap ?? 0;
42
+ const rowGap = opts.rowGap ?? opts.gap ?? 0;
47
43
  const cb = opts.cellBorder;
48
44
  const rule = opts.rule;
49
45
  // The complete grid, once: every cell bottom+right, first row also top, first col also left.
50
46
  const borderFor = (firstRow, firstCol) => cb === undefined
51
47
  ? {}
52
- : Object.assign(Object.assign({ borderBottom: cb, borderRight: cb }, (firstRow ? { borderTop: cb } : {})), (firstCol ? { borderLeft: cb } : {}));
48
+ : {
49
+ borderBottom: cb,
50
+ borderRight: cb,
51
+ ...(firstRow ? { borderTop: cb } : {}),
52
+ ...(firstCol ? { borderLeft: cb } : {}),
53
+ };
53
54
  // `ruled` rows get just a bottom line (the header underline / the table foot), without the full grid.
54
55
  const wrap = (cell, col, firstRow, firstCol, ruled) => {
55
- const inner = cellPadding !== undefined ? (0, layout_1.Padding)(cellPadding, asElement(cell)) : asElement(cell);
56
+ const inner = cellPadding !== undefined ? Padding(cellPadding, asElement(cell)) : asElement(cell);
56
57
  // The border lives on the wrapper, which stretches to the row height (crisp lines).
57
- const border = Object.assign(Object.assign({}, borderFor(firstRow, firstCol)), (ruled && rule !== undefined ? { borderBottom: rule } : {}));
58
+ const border = {
59
+ ...borderFor(firstRow, firstCol),
60
+ ...(ruled && rule !== undefined ? { borderBottom: rule } : {}),
61
+ };
58
62
  const boxed = cb !== undefined || (ruled && rule !== undefined);
59
63
  if (typeof col === "number")
60
- return (0, layout_1.Box)(Object.assign({ width: col }, border), [inner]);
64
+ return Box({ width: col, ...border }, [inner]);
61
65
  const fr = fractionOf(col);
62
66
  if (fr !== null)
63
- return (0, layout_1.Expanded)({ flex: fr }, boxed ? (0, layout_1.Box)(Object.assign({}, border), [inner]) : inner);
67
+ return Expanded({ flex: fr }, boxed ? Box({ ...border }, [inner]) : inner);
64
68
  throw new Error(`Unsupported column width "${col}" - use a number of points or "<n>fr"`);
65
69
  };
66
70
  // align:stretch → equal-height cells, so a wrapping cell keeps the row's bottom rule straight.
67
- const buildRow = (cells, firstRow, ruled = false) => (0, layout_1.Row)({ gap: colGap, align: "stretch" }, cells.map((cell, i) => { var _a; return wrap(cell, (_a = columns[i]) !== null && _a !== void 0 ? _a : "1fr", firstRow, i === 0, ruled); }));
71
+ const buildRow = (cells, firstRow, ruled = false) => Row({ gap: colGap, align: "stretch" }, cells.map((cell, i) => wrap(cell, columns[i] ?? "1fr", firstRow, i === 0, ruled)));
68
72
  // The first row (which gets the top border) is the header if present, else body row 0.
69
73
  // `rule` underlines the header and the last body row (the table foot).
70
74
  const last = rows.length - 1;
71
- const body = (0, layout_1.Column)({ gap: rowGap }, rows.map((cells, idx) => buildRow(cells, !opts.header && idx === 0, rule !== undefined && idx === last)));
75
+ const body = Column({ gap: rowGap }, rows.map((cells, idx) => buildRow(cells, !opts.header && idx === 0, rule !== undefined && idx === last)));
72
76
  return opts.header
73
- ? new repeating_header_element_1.RepeatingHeaderElement(buildRow(opts.header, true, rule !== undefined), body, rowGap)
77
+ ? new RepeatingHeaderElement(buildRow(opts.header, true, rule !== undefined), body, rowGap)
74
78
  : body;
75
79
  }
76
80
  /**
@@ -79,9 +83,9 @@ function composeTable(opts, rows, columns) {
79
83
  * paginates at row boundaries for free (a row that doesn't fit moves whole). `"auto"`
80
84
  * columns are resolved at layout time (a `DeferredElement`), once metrics are available.
81
85
  */
82
- function Table(opts, rows) {
86
+ export function Table(opts, rows) {
83
87
  if (opts.columns.some((c) => c === "auto")) {
84
- return new deferred_element_1.DeferredElement((ctx) => composeTable(opts, rows, resolveAutoColumns(opts, rows, ctx)));
88
+ return new DeferredElement((ctx) => composeTable(opts, rows, resolveAutoColumns(opts, rows, ctx)));
85
89
  }
86
90
  return composeTable(opts, rows, opts.columns);
87
91
  }
@@ -1,5 +1,8 @@
1
- import { TextElement, TextSegment } from "../elements/text-element";
2
- import { ColorInput } from "./color";
1
+ import { TextElement, TextSegment } from "../elements/text-element.js";
2
+ import { TextOverflow } from "../text/line-breaker.js";
3
+ import { ResolvedTextStyle } from "../text/text-style.js";
4
+ import { ColorInput } from "./color.js";
5
+ export type { TextOverflow };
3
6
  /** Text styling shared by `Text`, `Paragraph` and `span`. `bold`/`italic` are booleans
4
7
  * (locked §7.3) and combine into one engine `FontStyle`. */
5
8
  export interface TextStyle {
@@ -12,6 +15,14 @@ export interface TextStyle {
12
15
  export interface TextOptions extends TextStyle {
13
16
  /** Text-internal alignment (left/center/right) - independent of a parent's `cross` (§5). */
14
17
  align?: "left" | "center" | "right";
18
+ /** Cap the number of wrapped lines (default: unlimited - the text grows down as far as it needs,
19
+ * paginating onto the next page). Needs a bounded width (a Column/Expanded/`Box({ width })`). */
20
+ maxLines?: number;
21
+ /** What happens past `maxLines`: `"clip"` (default) cuts hard, `"ellipsis"` ends with "…". */
22
+ overflow?: TextOverflow;
23
+ /** Line-height multiplier (default `1`). `1.4` gives roomier body copy; each line is
24
+ * `size * lineHeight` tall. */
25
+ lineHeight?: number;
15
26
  }
16
27
  /**
17
28
  * An inline run for mixed-style `Text` (`Text([span("a", {bold:true}), span("b")])`). Each
@@ -26,3 +37,17 @@ export declare function span(text: string, style?: TextStyle): TextSegment;
26
37
  export declare function Text(content: string | TextSegment[], opts?: TextOptions): TextElement;
27
38
  /** `Text` with body-paragraph defaults (same options; a separate name reads as intent). */
28
39
  export declare function Paragraph(content: string | TextSegment[], opts?: TextOptions): TextElement;
40
+ /** The inheritable text defaults a `Document` can set, using the same option names as `Text`.
41
+ * Unset fields stay out so they keep inheriting. */
42
+ export interface TextDefaults {
43
+ size?: number;
44
+ font?: string;
45
+ bold?: boolean;
46
+ italic?: boolean;
47
+ color?: ColorInput;
48
+ align?: "left" | "center" | "right";
49
+ lineHeight?: number;
50
+ }
51
+ /** Maps the `Text`-style option names onto a partial engine `ResolvedTextStyle` (only the set
52
+ * fields), for seeding the inheritance cascade. */
53
+ export declare function toTextStyleOverride(opts: TextDefaults): Partial<ResolvedTextStyle>;
package/dist/api/text.js CHANGED
@@ -1,34 +1,27 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.span = span;
4
- exports.Text = Text;
5
- exports.Paragraph = Paragraph;
6
- const text_element_1 = require("../elements/text-element");
7
- const pdf_element_1 = require("../elements/pdf-element");
8
- const pdf_object_manager_1 = require("../utils/pdf-object-manager");
9
- const color_1 = require("./color");
10
- /** Body-text default size when none is given (matches the engine default font). */
11
- const DEFAULT_SIZE = 12;
1
+ import { TextElement } from "../elements/text-element.js";
2
+ import { HorizontalAlignment } from "../elements/pdf-element.js";
3
+ import { FontStyle } from "../utils/pdf-object-manager.js";
4
+ import { toColor } from "./color.js";
12
5
  /** bold + italic → the single engine `FontStyle`. */
13
6
  function toFontStyle(bold, italic) {
14
7
  if (bold && italic)
15
- return pdf_object_manager_1.FontStyle.BoldItalic;
8
+ return FontStyle.BoldItalic;
16
9
  if (bold)
17
- return pdf_object_manager_1.FontStyle.Bold;
10
+ return FontStyle.Bold;
18
11
  if (italic)
19
- return pdf_object_manager_1.FontStyle.Italic;
20
- return pdf_object_manager_1.FontStyle.Normal;
12
+ return FontStyle.Italic;
13
+ return FontStyle.Normal;
21
14
  }
22
15
  const ALIGN = {
23
- left: pdf_element_1.HorizontalAlignment.left,
24
- center: pdf_element_1.HorizontalAlignment.center,
25
- right: pdf_element_1.HorizontalAlignment.right,
16
+ left: HorizontalAlignment.left,
17
+ center: HorizontalAlignment.center,
18
+ right: HorizontalAlignment.right,
26
19
  };
27
20
  /**
28
21
  * An inline run for mixed-style `Text` (`Text([span("a", {bold:true}), span("b")])`). Each
29
22
  * field overrides the enclosing `Text`'s defaults for just this run; omitted fields inherit.
30
23
  */
31
- function span(text, style = {}) {
24
+ export function span(text, style = {}) {
32
25
  return {
33
26
  content: text,
34
27
  fontSize: style.size,
@@ -36,7 +29,7 @@ function span(text, style = {}) {
36
29
  fontStyle: style.bold !== undefined || style.italic !== undefined
37
30
  ? toFontStyle(style.bold, style.italic)
38
31
  : undefined,
39
- fontColor: style.color !== undefined ? (0, color_1.toColor)(style.color) : undefined,
32
+ fontColor: style.color !== undefined ? toColor(style.color) : undefined,
40
33
  };
41
34
  }
42
35
  /**
@@ -44,18 +37,43 @@ function span(text, style = {}) {
44
37
  * options set the defaults (size/font/style/color) that any spans inherit, plus the
45
38
  * text-internal `align`.
46
39
  */
47
- function Text(content, opts = {}) {
48
- var _a;
49
- return new text_element_1.TextElement({
40
+ export function Text(content, opts = {}) {
41
+ // Unset properties are left undefined so they inherit the cascaded TextStyle (Document default /
42
+ // built-in). Only bold/italic that the caller actually set become an explicit FontStyle.
43
+ return new TextElement({
50
44
  content,
51
- fontSize: (_a = opts.size) !== null && _a !== void 0 ? _a : DEFAULT_SIZE,
45
+ fontSize: opts.size,
52
46
  fontFamily: opts.font,
53
- fontStyle: toFontStyle(opts.bold, opts.italic),
54
- color: opts.color !== undefined ? (0, color_1.toColor)(opts.color) : undefined,
47
+ fontStyle: opts.bold !== undefined || opts.italic !== undefined
48
+ ? toFontStyle(opts.bold, opts.italic)
49
+ : undefined,
50
+ color: opts.color !== undefined ? toColor(opts.color) : undefined,
55
51
  textAlignment: opts.align ? ALIGN[opts.align] : undefined,
52
+ maxLines: opts.maxLines,
53
+ overflow: opts.overflow,
54
+ lineHeight: opts.lineHeight,
56
55
  });
57
56
  }
58
57
  /** `Text` with body-paragraph defaults (same options; a separate name reads as intent). */
59
- function Paragraph(content, opts = {}) {
58
+ export function Paragraph(content, opts = {}) {
60
59
  return Text(content, opts);
61
60
  }
61
+ /** Maps the `Text`-style option names onto a partial engine `ResolvedTextStyle` (only the set
62
+ * fields), for seeding the inheritance cascade. */
63
+ export function toTextStyleOverride(opts) {
64
+ const style = {};
65
+ if (opts.size !== undefined)
66
+ style.fontSize = opts.size;
67
+ if (opts.font !== undefined)
68
+ style.fontFamily = opts.font;
69
+ if (opts.bold !== undefined || opts.italic !== undefined) {
70
+ style.fontStyle = toFontStyle(opts.bold, opts.italic);
71
+ }
72
+ if (opts.color !== undefined)
73
+ style.color = toColor(opts.color);
74
+ if (opts.align !== undefined)
75
+ style.textAlignment = ALIGN[opts.align];
76
+ if (opts.lineHeight !== undefined)
77
+ style.lineHeight = opts.lineHeight;
78
+ return style;
79
+ }
@@ -0,0 +1,2 @@
1
+ export declare const STANDARD_AFM: Record<string, string>;
2
+ export declare const AGL: string;