@jasy/pdf 1.0.0-alpha.6 → 1.0.0-alpha.7
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/dist/api/descriptor.js +3 -1
- package/dist/api/layout.d.ts +47 -14
- package/dist/api/layout.js +47 -11
- package/dist/api/structure.d.ts +5 -0
- package/dist/api/structure.js +1 -0
- package/dist/api/text.d.ts +18 -2
- package/dist/api/text.js +15 -0
- package/dist/elements/container-element.d.ts +10 -1
- package/dist/elements/container-element.js +26 -8
- package/dist/elements/index.d.ts +2 -0
- package/dist/elements/index.js +2 -0
- package/dist/elements/layout/keep-together-element.d.ts +42 -0
- package/dist/elements/layout/keep-together-element.js +85 -0
- package/dist/elements/layout/padding-element.d.ts +3 -0
- package/dist/elements/layout/padding-element.js +12 -0
- package/dist/elements/layout/page-break-element.d.ts +17 -0
- package/dist/elements/layout/page-break-element.js +24 -0
- package/dist/elements/layout/positioned-element.d.ts +10 -7
- package/dist/elements/layout/positioned-element.js +21 -9
- package/dist/elements/page-element.d.ts +11 -1
- package/dist/elements/page-element.js +41 -16
- package/dist/elements/pdf-element.d.ts +28 -0
- package/dist/elements/pdf-element.js +32 -0
- package/dist/elements/rectangle-element.d.ts +10 -1
- package/dist/elements/rectangle-element.js +27 -6
- package/dist/elements/row-element.d.ts +9 -1
- package/dist/elements/row-element.js +9 -1
- package/dist/elements/text-element.d.ts +30 -4
- package/dist/elements/text-element.js +40 -17
- package/dist/ir/display-list.d.ts +1 -0
- package/dist/layout/fragmentation.d.ts +12 -0
- package/dist/layout/fragmentation.js +45 -8
- package/dist/renderer/keep-together-renderer.d.ts +10 -0
- package/dist/renderer/keep-together-renderer.js +12 -0
- package/dist/renderer/page-break-renderer.d.ts +14 -0
- package/dist/renderer/page-break-renderer.js +15 -0
- package/dist/renderer/pdf-backend.d.ts +7 -0
- package/dist/renderer/pdf-backend.js +40 -6
- package/dist/renderer/pdf-document-class.js +3 -0
- package/dist/renderer/pdf-document-renderer.js +26 -7
- package/dist/renderer/pdf-renderer.js +6 -0
- package/dist/renderer/text-renderer.d.ts +1 -1
- package/dist/renderer/text-renderer.js +107 -59
- package/dist/text/advance.d.ts +34 -0
- package/dist/text/advance.js +42 -0
- package/dist/text/line-breaker.d.ts +9 -8
- package/dist/text/line-breaker.js +28 -21
- package/dist/text/line-metrics.d.ts +49 -0
- package/dist/text/line-metrics.js +57 -0
- package/dist/text/text-decoration.d.ts +50 -0
- package/dist/text/text-decoration.js +62 -0
- package/dist/text/text-style.d.ts +13 -3
- package/dist/text/text-style.js +10 -3
- package/dist/utils/afm-parser.d.ts +34 -0
- package/dist/utils/afm-parser.js +67 -0
- package/dist/utils/font-metrics.d.ts +14 -0
- package/dist/utils/pdf-object-manager.d.ts +27 -1
- package/dist/utils/pdf-object-manager.js +134 -20
- package/dist/utils/ttf-parser.d.ts +53 -0
- package/dist/utils/ttf-parser.js +368 -0
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { PDFElement } from "../pdf-element.js";
|
|
2
|
+
/**
|
|
3
|
+
* A forced page break. Placed in a flow (a `Column`/`Row`/`Box`), it makes everything AFTER it start
|
|
4
|
+
* on a fresh page, no matter how much space is left. It takes zero space and draws nothing - the
|
|
5
|
+
* pagination packer consumes it at the cut, so it never reaches the render pass.
|
|
6
|
+
*
|
|
7
|
+
* Nesting works: a break deep inside a container is honoured because the packer fragments any child
|
|
8
|
+
* whose subtree `hasForcedBreak()`, and the resulting `forceBreak` bubbles up so the outer flow stops
|
|
9
|
+
* too. A break wins over a `keepTogether` (a forced break cannot be avoided) - CSS behaves the same.
|
|
10
|
+
*/
|
|
11
|
+
export class PageBreakElement extends PDFElement {
|
|
12
|
+
calculateLayout(_constraints, _offset, _ctx) {
|
|
13
|
+
return { width: 0, height: 0 };
|
|
14
|
+
}
|
|
15
|
+
isPageBreak() {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
hasForcedBreak() {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
getProps() {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -23,14 +23,17 @@ export interface PositionedInsets {
|
|
|
23
23
|
interface PositionedElementParams extends WithChild, PositionedInsets {
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
* An out-of-flow child, placed relative to the nearest enclosing positioning frame
|
|
27
|
-
* Box
|
|
28
|
-
* registers a placement closure on the frame. The
|
|
29
|
-
* size, so `right`/`bottom` resolve against the final
|
|
30
|
-
* `top`/`left` makes it poke into / out of the
|
|
26
|
+
* An out-of-flow child, placed relative to the nearest enclosing positioning frame: a `relative`
|
|
27
|
+
* Box, or failing that the page's content box. It takes ZERO space in the normal flow -
|
|
28
|
+
* `calculateLayout` returns `Size(0,0)` and instead registers a placement closure on the frame. The
|
|
29
|
+
* frame runs that closure once it knows its own size, so `right`/`bottom` resolve against the final
|
|
30
|
+
* box and the child can overflow it (a negative `top`/`left` makes it poke into / out of the
|
|
31
|
+
* corner, e.g. a badge or tab).
|
|
31
32
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
33
|
+
* Every page supplies a frame - header, footer and body alike - so within a document there is
|
|
34
|
+
* always one. Outside of one we REFUSE rather than draw: the element used to fall through to its
|
|
35
|
+
* child's default (0, 0), which is how a header watermark landed silently in the page corner
|
|
36
|
+
* (ISSUE-4). Content that lands somewhere unasked is worse than content that does not render.
|
|
34
37
|
*/
|
|
35
38
|
export declare class PositionedElement extends PDFElement {
|
|
36
39
|
private child;
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { BoxConstraints } from "../../layout/box-constraints.js";
|
|
2
2
|
import { PDFElement } from "../pdf-element.js";
|
|
3
3
|
/**
|
|
4
|
-
* An out-of-flow child, placed relative to the nearest enclosing positioning frame
|
|
5
|
-
* Box
|
|
6
|
-
* registers a placement closure on the frame. The
|
|
7
|
-
* size, so `right`/`bottom` resolve against the final
|
|
8
|
-
* `top`/`left` makes it poke into / out of the
|
|
4
|
+
* An out-of-flow child, placed relative to the nearest enclosing positioning frame: a `relative`
|
|
5
|
+
* Box, or failing that the page's content box. It takes ZERO space in the normal flow -
|
|
6
|
+
* `calculateLayout` returns `Size(0,0)` and instead registers a placement closure on the frame. The
|
|
7
|
+
* frame runs that closure once it knows its own size, so `right`/`bottom` resolve against the final
|
|
8
|
+
* box and the child can overflow it (a negative `top`/`left` makes it poke into / out of the
|
|
9
|
+
* corner, e.g. a badge or tab).
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
11
|
+
* Every page supplies a frame - header, footer and body alike - so within a document there is
|
|
12
|
+
* always one. Outside of one we REFUSE rather than draw: the element used to fall through to its
|
|
13
|
+
* child's default (0, 0), which is how a header watermark landed silently in the page corner
|
|
14
|
+
* (ISSUE-4). Content that lands somewhere unasked is worse than content that does not render.
|
|
12
15
|
*/
|
|
13
16
|
export class PositionedElement extends PDFElement {
|
|
14
17
|
constructor({ child, top, right, bottom, left, h, v, x, y }) {
|
|
@@ -17,8 +20,12 @@ export class PositionedElement extends PDFElement {
|
|
|
17
20
|
this.insets = { top, right, bottom, left, h, v, x, y };
|
|
18
21
|
}
|
|
19
22
|
calculateLayout(_constraints, _offset, ctx) {
|
|
23
|
+
if (!ctx.frame) {
|
|
24
|
+
throw new Error("Positioned found no positioning frame. It must sit inside a Page (header, footer or body) " +
|
|
25
|
+
"or inside a Box({ relative: true }).");
|
|
26
|
+
}
|
|
20
27
|
// Defer to the frame: it calls back once it has sized itself. Out of flow either way.
|
|
21
|
-
ctx.frame
|
|
28
|
+
ctx.frame.place.push((frame, frameCtx) => this.placeInFrame(frame, frameCtx));
|
|
22
29
|
return { width: 0, height: 0 };
|
|
23
30
|
}
|
|
24
31
|
/** Lays the child out at the resolved position inside (or overflowing) the frame box. */
|
|
@@ -35,7 +42,12 @@ export class PositionedElement extends PDFElement {
|
|
|
35
42
|
: Infinity;
|
|
36
43
|
const constraints = BoxConstraints.loose(width, height);
|
|
37
44
|
// Measure first (so end/center anchors and right/bottom edges resolve against the child's size).
|
|
38
|
-
|
|
45
|
+
// The measuring pass gets a throwaway `place` list: this subtree is laid out twice, and a nested
|
|
46
|
+
// out-of-flow child would otherwise register its placement on both passes.
|
|
47
|
+
const measureCtx = ctx.frame
|
|
48
|
+
? { ...ctx, frame: { ...ctx.frame, place: [] } }
|
|
49
|
+
: ctx;
|
|
50
|
+
const measured = this.child.calculateLayout(constraints, { x: 0, y: 0 }, measureCtx);
|
|
39
51
|
// Resolve one axis: a near-edge pins to origin, a far-edge pins to the far side; otherwise the
|
|
40
52
|
// anchor (default `start`) positions the child and the nudge shifts it from there.
|
|
41
53
|
const place = (origin, frameExtent, childExtent, nearEdge, farEdge, anchor, nudge) => {
|
|
@@ -2,7 +2,7 @@ import { PageSize } from "../constants/page-sizes.js";
|
|
|
2
2
|
import { Orientation } from "../renderer/pdf-config.js";
|
|
3
3
|
import type { ColorMode, DefaultFont, Margin } from "../renderer/index.js";
|
|
4
4
|
import { BoxConstraints, Offset, Size } from "../layout/box-constraints.js";
|
|
5
|
-
import { LayoutContext, PDFElement, WithChildren } from "./pdf-element.js";
|
|
5
|
+
import { LayoutContext, PDFElement, PositioningFrame, WithChildren } from "./pdf-element.js";
|
|
6
6
|
import { TextElement } from "./text-element.js";
|
|
7
7
|
export interface PDFPageConfig {
|
|
8
8
|
pageSize?: PageSize;
|
|
@@ -54,6 +54,16 @@ export interface PageBands {
|
|
|
54
54
|
* no header/footer the bands are zero and the body equals the full content box - identical
|
|
55
55
|
* to a page without them.
|
|
56
56
|
*/
|
|
57
|
+
/**
|
|
58
|
+
* The page's positioning frame: its CONTENT box (paper minus margins). Header, footer and body all
|
|
59
|
+
* resolve a `Positioned` against this same box, so `top`/`bottom` mean the same thing in each.
|
|
60
|
+
*
|
|
61
|
+
* `layoutPageBands` lays the bands out, so its caller must hand it a context carrying this frame -
|
|
62
|
+
* otherwise a `Positioned` in a band has nothing to resolve against (ISSUE-4). A caller that only
|
|
63
|
+
* wants the band HEIGHTS may throw the frame away without draining it: an out-of-flow child
|
|
64
|
+
* contributes no height.
|
|
65
|
+
*/
|
|
66
|
+
export declare function pageFrame(config: PDFPageConfig): PositioningFrame;
|
|
57
67
|
export declare function layoutPageBands(config: PDFPageConfig, header: PDFElement | undefined, footer: PDFElement | undefined, ctx: LayoutContext): PageBands;
|
|
58
68
|
export declare class PageElement extends PDFElement {
|
|
59
69
|
private config;
|
|
@@ -36,6 +36,19 @@ export function resolvePageContentBox(config) {
|
|
|
36
36
|
* no header/footer the bands are zero and the body equals the full content box - identical
|
|
37
37
|
* to a page without them.
|
|
38
38
|
*/
|
|
39
|
+
/**
|
|
40
|
+
* The page's positioning frame: its CONTENT box (paper minus margins). Header, footer and body all
|
|
41
|
+
* resolve a `Positioned` against this same box, so `top`/`bottom` mean the same thing in each.
|
|
42
|
+
*
|
|
43
|
+
* `layoutPageBands` lays the bands out, so its caller must hand it a context carrying this frame -
|
|
44
|
+
* otherwise a `Positioned` in a band has nothing to resolve against (ISSUE-4). A caller that only
|
|
45
|
+
* wants the band HEIGHTS may throw the frame away without draining it: an out-of-flow child
|
|
46
|
+
* contributes no height.
|
|
47
|
+
*/
|
|
48
|
+
export function pageFrame(config) {
|
|
49
|
+
const { origin, width, height } = resolvePageContentBox(config);
|
|
50
|
+
return { origin, size: { width, height }, place: [] };
|
|
51
|
+
}
|
|
39
52
|
export function layoutPageBands(config, header, footer, ctx) {
|
|
40
53
|
const { origin, width, height } = resolvePageContentBox(config);
|
|
41
54
|
let headerHeight = 0;
|
|
@@ -44,8 +57,13 @@ export function layoutPageBands(config, header, footer, ctx) {
|
|
|
44
57
|
}
|
|
45
58
|
let footerHeight = 0;
|
|
46
59
|
if (footer) {
|
|
47
|
-
// Measure first to learn its height, then place it flush against the bottom edge.
|
|
48
|
-
|
|
60
|
+
// Measure first to learn its height, then place it flush against the bottom edge. The MEASURING
|
|
61
|
+
// pass gets a throwaway `place` list: an out-of-flow child registers a placement every time it is
|
|
62
|
+
// laid out, and this subtree is laid out twice. Only the second, real pass may register.
|
|
63
|
+
const measureCtx = ctx.frame
|
|
64
|
+
? { ...ctx, frame: { ...ctx.frame, place: [] } }
|
|
65
|
+
: ctx;
|
|
66
|
+
footerHeight = footer.calculateLayout(BoxConstraints.loose(width, Infinity), origin, measureCtx).height;
|
|
49
67
|
footer.calculateLayout(BoxConstraints.loose(width, Infinity), { x: origin.x, y: origin.y + height - footerHeight }, ctx);
|
|
50
68
|
}
|
|
51
69
|
return {
|
|
@@ -76,24 +94,31 @@ export class PageElement extends PDFElement {
|
|
|
76
94
|
onOverflow: ctx.onOverflow,
|
|
77
95
|
pageInfo: ctx.pageInfo, // the render pass supplies it; absent during pagination
|
|
78
96
|
};
|
|
97
|
+
// The PAGE is a positioning frame, and the frame is the content box - not the body region left
|
|
98
|
+
// over between the bands. So `Positioned` means the same thing in the header, in the footer and
|
|
99
|
+
// in the body, `bottom: 0` is the foot of the page rather than the top of the footer, and adding
|
|
100
|
+
// a header no longer shifts a page-level `Positioned` that nobody touched. A `relative` Box
|
|
101
|
+
// still overrides it for its own subtree.
|
|
102
|
+
//
|
|
103
|
+
// Built BEFORE the bands are laid out: they need to see it, and their heights are not known yet
|
|
104
|
+
// anyway. (That is the whole of ISSUE-4 - a `Positioned` in a band had no frame to resolve
|
|
105
|
+
// against, so it silently stayed at the page's top-left corner.)
|
|
106
|
+
const frame = pageFrame(this.config);
|
|
107
|
+
const frameCtx = { ...pageCtx, frame };
|
|
79
108
|
// Place the header/footer bands; the body gets the region left in between (the whole
|
|
80
109
|
// content box when there is neither - byte-identical to a plain page).
|
|
81
|
-
const bands = layoutPageBands(this.config, this.header, this.footer,
|
|
110
|
+
const bands = layoutPageBands(this.config, this.header, this.footer, frameCtx);
|
|
82
111
|
const childConstraints = BoxConstraints.loose(bands.bodyWidth, bands.bodyHeight);
|
|
83
|
-
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
};
|
|
91
|
-
const bodyCtx = { ...pageCtx, frame };
|
|
92
|
-
this.children.forEach((child) => child.calculateLayout(childConstraints, bands.bodyOrigin, bodyCtx));
|
|
112
|
+
this.children.forEach((child) => child.calculateLayout(childConstraints, bands.bodyOrigin, frameCtx));
|
|
113
|
+
// Drained last, once every band and the body have registered: an out-of-flow child is placed
|
|
114
|
+
// against the final frame box and paints on top of the flow content.
|
|
115
|
+
//
|
|
116
|
+
// The callback gets `frameCtx`, not `pageCtx`: a `Positioned` NESTED inside an out-of-flow child
|
|
117
|
+
// must still see a frame. It appends to `frame.place` while we walk it, and a `for..of` over an
|
|
118
|
+
// array visits what was appended, so nested placements drain in the same loop.
|
|
93
119
|
for (const place of frame.place)
|
|
94
|
-
place(frame,
|
|
95
|
-
|
|
96
|
-
return { width, height };
|
|
120
|
+
place(frame, frameCtx);
|
|
121
|
+
return { width: frame.size.width, height: frame.size.height };
|
|
97
122
|
}
|
|
98
123
|
getProps() {
|
|
99
124
|
return {
|
|
@@ -51,6 +51,10 @@ export interface LayoutContext {
|
|
|
51
51
|
/** What to do when an element overflows a page region and cannot break (set from the render option;
|
|
52
52
|
* absent = clip silently). Evaluated in `packChildren` where the forced placement happens. */
|
|
53
53
|
onOverflow?: OverflowPolicy;
|
|
54
|
+
/** The FULL body height of the current page (between header and footer), set by the page driver. A
|
|
55
|
+
* `keepTogether` group reads it to decide the degrade rule: veto splitting only when the group would
|
|
56
|
+
* fit on a fresh page; a group taller than a whole page drops the veto and splits. */
|
|
57
|
+
pageBodyHeight?: number;
|
|
54
58
|
}
|
|
55
59
|
export declare abstract class PDFElement {
|
|
56
60
|
private _structId;
|
|
@@ -80,6 +84,30 @@ export declare abstract class PDFElement {
|
|
|
80
84
|
* the parent hands it the line's bounded main extent instead of `Infinity`. Default: no.
|
|
81
85
|
*/
|
|
82
86
|
needsBoundedMain(_horizontal: boolean): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Whether this element IS a forced page break (a `PageBreak`). The pagination packer cuts the flow
|
|
89
|
+
* at such a marker: everything before it stays, everything after starts a new page. Default: no.
|
|
90
|
+
*/
|
|
91
|
+
isPageBreak(): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Whether this element is, or CONTAINS anywhere in its subtree, a forced page break. A container
|
|
94
|
+
* that merely fits by height must still be fragmented when this is true, so a break nested inside
|
|
95
|
+
* it is honoured rather than swallowed. Containers override this to recurse into their children.
|
|
96
|
+
* Default: no.
|
|
97
|
+
*/
|
|
98
|
+
hasForcedBreak(): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Whether this element asks to START on a fresh page (CSS `break-before: page`, react-pdf's `break`
|
|
101
|
+
* prop). Read by the parent packer at the child boundary, not by the element itself; ignored when the
|
|
102
|
+
* element is already at the top of a region (nothing before it), so it never makes an empty page.
|
|
103
|
+
* Default: no.
|
|
104
|
+
*/
|
|
105
|
+
breaksBefore(): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Whether everything AFTER this element must start a fresh page (CSS `break-after: page`). Read by the
|
|
108
|
+
* parent packer once this element has been placed whole. Default: no.
|
|
109
|
+
*/
|
|
110
|
+
breaksAfter(): boolean;
|
|
83
111
|
}
|
|
84
112
|
export declare abstract class SizedPDFElement extends PDFElement {
|
|
85
113
|
protected x: number;
|
|
@@ -33,6 +33,38 @@ export class PDFElement {
|
|
|
33
33
|
needsBoundedMain(_horizontal) {
|
|
34
34
|
return false;
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether this element IS a forced page break (a `PageBreak`). The pagination packer cuts the flow
|
|
38
|
+
* at such a marker: everything before it stays, everything after starts a new page. Default: no.
|
|
39
|
+
*/
|
|
40
|
+
isPageBreak() {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether this element is, or CONTAINS anywhere in its subtree, a forced page break. A container
|
|
45
|
+
* that merely fits by height must still be fragmented when this is true, so a break nested inside
|
|
46
|
+
* it is honoured rather than swallowed. Containers override this to recurse into their children.
|
|
47
|
+
* Default: no.
|
|
48
|
+
*/
|
|
49
|
+
hasForcedBreak() {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Whether this element asks to START on a fresh page (CSS `break-before: page`, react-pdf's `break`
|
|
54
|
+
* prop). Read by the parent packer at the child boundary, not by the element itself; ignored when the
|
|
55
|
+
* element is already at the top of a region (nothing before it), so it never makes an empty page.
|
|
56
|
+
* Default: no.
|
|
57
|
+
*/
|
|
58
|
+
breaksBefore() {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Whether everything AFTER this element must start a fresh page (CSS `break-after: page`). Read by the
|
|
63
|
+
* parent packer once this element has been placed whole. Default: no.
|
|
64
|
+
*/
|
|
65
|
+
breaksAfter() {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
36
68
|
}
|
|
37
69
|
export class SizedPDFElement extends PDFElement {
|
|
38
70
|
constructor(data) {
|
|
@@ -28,6 +28,10 @@ interface RectangleElementParams extends SizedElement, WithChildren {
|
|
|
28
28
|
widthFactor?: number;
|
|
29
29
|
/** Height as a fraction (0..1) of the offered height; see `widthFactor`. */
|
|
30
30
|
heightFactor?: number;
|
|
31
|
+
/** Start this box on a fresh page (CSS `break-before: page`). */
|
|
32
|
+
breakBefore?: boolean;
|
|
33
|
+
/** Start everything after this box on a fresh page (CSS `break-after: page`). */
|
|
34
|
+
breakAfter?: boolean;
|
|
31
35
|
}
|
|
32
36
|
export declare class RectangleElement extends SizedPDFElement implements Fragmentable {
|
|
33
37
|
private children;
|
|
@@ -38,8 +42,12 @@ export declare class RectangleElement extends SizedPDFElement implements Fragmen
|
|
|
38
42
|
private sideBorders?;
|
|
39
43
|
private relative;
|
|
40
44
|
private overflow;
|
|
45
|
+
private breakBefore;
|
|
46
|
+
private breakAfter;
|
|
41
47
|
private sizeMemory;
|
|
42
|
-
constructor({ children, color, backgroundColor, borderWidth, width, height, radius, sideBorders, relative, overflow, widthFactor, heightFactor, }: RectangleElementParams);
|
|
48
|
+
constructor({ children, color, backgroundColor, borderWidth, width, height, radius, sideBorders, relative, overflow, widthFactor, heightFactor, breakBefore, breakAfter, }: RectangleElementParams);
|
|
49
|
+
breaksBefore(): boolean;
|
|
50
|
+
breaksAfter(): boolean;
|
|
43
51
|
/**
|
|
44
52
|
* Splits the bordered box across pages (box-decoration-break: clone - every fragment
|
|
45
53
|
* gets its own full border). The children stack is packed into the space left after
|
|
@@ -49,6 +57,7 @@ export declare class RectangleElement extends SizedPDFElement implements Fragmen
|
|
|
49
57
|
* box is sized by what it actually holds on each page.
|
|
50
58
|
*/
|
|
51
59
|
fragment(maxHeight: number, width: number, ctx: LayoutContext): FragmentResult;
|
|
60
|
+
hasForcedBreak(): boolean;
|
|
52
61
|
private cloneWithChildren;
|
|
53
62
|
relativeSizeFactor(horizontal: boolean): number | undefined;
|
|
54
63
|
/** A box has no flex children of its own, but it must not swallow the need of the stack inside it: a
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Color } from "../common/color.js";
|
|
2
2
|
import { BoxConstraints, resolveExtent } from "../layout/box-constraints.js";
|
|
3
|
-
import { packChildren } from "../layout/fragmentation.js";
|
|
3
|
+
import { childrenForceBreak, packChildren, } from "../layout/fragmentation.js";
|
|
4
4
|
import { SizedPDFElement, } from "./pdf-element.js";
|
|
5
5
|
export class RectangleElement extends SizedPDFElement {
|
|
6
|
-
constructor({ children = [], color = new Color(0, 0, 0), backgroundColor, borderWidth, width, height, radius, sideBorders, relative, overflow, widthFactor, heightFactor, }) {
|
|
6
|
+
constructor({ children = [], color = new Color(0, 0, 0), backgroundColor, borderWidth, width, height, radius, sideBorders, relative, overflow, widthFactor, heightFactor, breakBefore, breakAfter, }) {
|
|
7
7
|
super({ x: 0, y: 0, width, height });
|
|
8
8
|
this.children = [];
|
|
9
9
|
this.children = children;
|
|
@@ -15,8 +15,16 @@ export class RectangleElement extends SizedPDFElement {
|
|
|
15
15
|
this.sideBorders = sideBorders;
|
|
16
16
|
this.relative = relative ?? false;
|
|
17
17
|
this.overflow = overflow ?? "visible";
|
|
18
|
+
this.breakBefore = breakBefore ?? false;
|
|
19
|
+
this.breakAfter = breakAfter ?? false;
|
|
18
20
|
this.sizeMemory = { x: 0, y: 0, width, height, widthFactor, heightFactor };
|
|
19
21
|
}
|
|
22
|
+
breaksBefore() {
|
|
23
|
+
return this.breakBefore;
|
|
24
|
+
}
|
|
25
|
+
breaksAfter() {
|
|
26
|
+
return this.breakAfter;
|
|
27
|
+
}
|
|
20
28
|
/**
|
|
21
29
|
* Splits the bordered box across pages (box-decoration-break: clone - every fragment
|
|
22
30
|
* gets its own full border). The children stack is packed into the space left after
|
|
@@ -34,18 +42,30 @@ export class RectangleElement extends SizedPDFElement {
|
|
|
34
42
|
// Border-box: the content is inset by the border on top and bottom, so a fragment
|
|
35
43
|
// holding `c` of content is `c + 2*border` tall. (Derived, not a fudge factor.)
|
|
36
44
|
const innerMaxHeight = maxHeight - 2 * this.borderWidth;
|
|
37
|
-
const { fitted, remainder } = packChildren(this.children, innerMaxHeight, innerWidth, ctx);
|
|
38
|
-
|
|
45
|
+
const { fitted, remainder, forceBreak } = packChildren(this.children, innerMaxHeight, innerWidth, ctx);
|
|
46
|
+
// Fits with no forced cut: return the whole box unchanged. A trailing forced break is the
|
|
47
|
+
// exception - fall through so the box is rebuilt from the packed children (without the consumed
|
|
48
|
+
// break marker), rather than handing back the original that still holds it.
|
|
49
|
+
if (remainder.length === 0 && !forceBreak)
|
|
39
50
|
return { fitted: this, remainder: null };
|
|
40
51
|
const contentHeight = (kids) => kids.reduce((sum, child) => sum +
|
|
41
52
|
child.calculateLayout(BoxConstraints.loose(innerWidth, Infinity), { x: 0, y: 0 }, ctx)
|
|
42
53
|
.height, 0);
|
|
43
54
|
return {
|
|
44
55
|
fitted: this.cloneWithChildren(fitted, contentHeight(fitted) + 2 * this.borderWidth),
|
|
45
|
-
|
|
56
|
+
// A trailing forced break leaves nothing to carry over: no remainder box, no extra page.
|
|
57
|
+
// Only the CONTINUATION carries `breakAfter` (a box that splits still breaks after its LAST
|
|
58
|
+
// fragment, not its first); `breakBefore` is dropped, already honoured before the split.
|
|
59
|
+
remainder: remainder.length === 0
|
|
60
|
+
? null
|
|
61
|
+
: this.cloneWithChildren(remainder, contentHeight(remainder) + 2 * this.borderWidth, this.breakAfter),
|
|
62
|
+
forceBreak,
|
|
46
63
|
};
|
|
47
64
|
}
|
|
48
|
-
|
|
65
|
+
hasForcedBreak() {
|
|
66
|
+
return childrenForceBreak(this.children);
|
|
67
|
+
}
|
|
68
|
+
cloneWithChildren(children, height, breakAfter = false) {
|
|
49
69
|
return new RectangleElement({
|
|
50
70
|
x: 0,
|
|
51
71
|
y: 0,
|
|
@@ -62,6 +82,7 @@ export class RectangleElement extends SizedPDFElement {
|
|
|
62
82
|
sideBorders: this.sideBorders,
|
|
63
83
|
relative: this.relative,
|
|
64
84
|
overflow: this.overflow,
|
|
85
|
+
breakAfter,
|
|
65
86
|
});
|
|
66
87
|
}
|
|
67
88
|
relativeSizeFactor(horizontal) {
|
|
@@ -13,6 +13,10 @@ interface RowElementParams extends WithChildren {
|
|
|
13
13
|
height?: number;
|
|
14
14
|
widthFactor?: number;
|
|
15
15
|
heightFactor?: number;
|
|
16
|
+
/** Start this row on a fresh page (CSS `break-before: page`). */
|
|
17
|
+
breakBefore?: boolean;
|
|
18
|
+
/** Start everything after this row on a fresh page (CSS `break-after: page`). */
|
|
19
|
+
breakAfter?: boolean;
|
|
16
20
|
}
|
|
17
21
|
/**
|
|
18
22
|
* Horizontal stack: the mirror of `ContainerElement` (Column). Children are laid out
|
|
@@ -31,8 +35,12 @@ export declare class RowElement extends SizedPDFElement {
|
|
|
31
35
|
private gap;
|
|
32
36
|
private main;
|
|
33
37
|
private cross;
|
|
38
|
+
private breakBefore;
|
|
39
|
+
private breakAfter;
|
|
34
40
|
private requested;
|
|
35
|
-
constructor({ children, gap, main, cross, width, height, widthFactor, heightFactor, }: RowElementParams);
|
|
41
|
+
constructor({ children, gap, main, cross, width, height, widthFactor, heightFactor, breakBefore, breakAfter, }: RowElementParams);
|
|
42
|
+
breaksBefore(): boolean;
|
|
43
|
+
breaksAfter(): boolean;
|
|
36
44
|
relativeSizeFactor(horizontal: boolean): number | undefined;
|
|
37
45
|
/** The mirror of `ContainerElement`: a Row's OWN flex child needs a bounded width, and the need of any
|
|
38
46
|
* descendant propagates on both axes (see the Column for the reasoning). */
|
|
@@ -14,13 +14,21 @@ import { FlexiblePDFElement, SizedPDFElement, } from "./pdf-element.js";
|
|
|
14
14
|
* Grid/Table concern.
|
|
15
15
|
*/
|
|
16
16
|
export class RowElement extends SizedPDFElement {
|
|
17
|
-
constructor({ children, gap, main, cross, width, height, widthFactor, heightFactor, }) {
|
|
17
|
+
constructor({ children, gap, main, cross, width, height, widthFactor, heightFactor, breakBefore, breakAfter, }) {
|
|
18
18
|
super({ x: 0, y: 0 });
|
|
19
19
|
this.children = children;
|
|
20
20
|
this.gap = gap ?? 0;
|
|
21
21
|
this.main = main ?? "start";
|
|
22
22
|
this.cross = cross ?? "stretch";
|
|
23
23
|
this.requested = { width, height, widthFactor, heightFactor };
|
|
24
|
+
this.breakBefore = breakBefore ?? false;
|
|
25
|
+
this.breakAfter = breakAfter ?? false;
|
|
26
|
+
}
|
|
27
|
+
breaksBefore() {
|
|
28
|
+
return this.breakBefore;
|
|
29
|
+
}
|
|
30
|
+
breaksAfter() {
|
|
31
|
+
return this.breakAfter;
|
|
24
32
|
}
|
|
25
33
|
relativeSizeFactor(horizontal) {
|
|
26
34
|
return horizontal ? this.requested.widthFactor : this.requested.heightFactor;
|
|
@@ -14,6 +14,11 @@ export interface TextSegment {
|
|
|
14
14
|
href?: string;
|
|
15
15
|
/** Internal named destination (an `Anchor`): this segment links to it (a /GoTo /Link annotation). */
|
|
16
16
|
dest?: string;
|
|
17
|
+
/** Unset inherits the Text's own setting; `true`/`false` overrides it for this run only. */
|
|
18
|
+
underline?: boolean;
|
|
19
|
+
strikethrough?: boolean;
|
|
20
|
+
/** Extra space after every glyph, in points; unset inherits the Text's own value. */
|
|
21
|
+
letterSpacing?: number;
|
|
17
22
|
}
|
|
18
23
|
/** Accessibility role for the tagged structure tree: a heading level or a paragraph (the default). */
|
|
19
24
|
export type TextRole = "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "p";
|
|
@@ -30,8 +35,17 @@ interface TextElementParams {
|
|
|
30
35
|
maxLines?: number;
|
|
31
36
|
/** What to do past `maxLines`: `"clip"` (default) drops them, `"ellipsis"` ends with "…". */
|
|
32
37
|
overflow?: TextOverflow;
|
|
33
|
-
/** Line-height multiplier: each line is `fontSize * lineHeight` tall
|
|
38
|
+
/** Line-height multiplier: each line is `fontSize * lineHeight` tall. Unset means the font's
|
|
39
|
+
* natural line height (`ascent + descent + lineGap`), like CSS `line-height: normal`. */
|
|
34
40
|
lineHeight?: number;
|
|
41
|
+
/** Draw a line under the text, at the position and thickness the font declares. */
|
|
42
|
+
underline?: boolean;
|
|
43
|
+
/** Draw a line through the text, at half its x-height. */
|
|
44
|
+
strikethrough?: boolean;
|
|
45
|
+
/** Let the underline step around descenders. Needs an embedded font. */
|
|
46
|
+
skipInk?: boolean;
|
|
47
|
+
/** Extra space after every glyph, in points (CSS `letter-spacing`). Default 0. */
|
|
48
|
+
letterSpacing?: number;
|
|
35
49
|
/** Accessibility role for the tagged structure tree (heading level or paragraph; default `"p"`). */
|
|
36
50
|
role?: TextRole;
|
|
37
51
|
}
|
|
@@ -42,17 +56,25 @@ export declare class TextElement extends SizedPDFElement implements Fragmentable
|
|
|
42
56
|
private readonly rawColor?;
|
|
43
57
|
private readonly rawTextAlignment?;
|
|
44
58
|
private readonly rawLineHeight?;
|
|
59
|
+
private readonly rawUnderline?;
|
|
60
|
+
private readonly rawStrikethrough?;
|
|
61
|
+
private readonly rawSkipInk?;
|
|
62
|
+
private readonly rawLetterSpacing?;
|
|
45
63
|
private fontSize;
|
|
46
64
|
private fontFamily;
|
|
47
65
|
private fontStyle;
|
|
48
66
|
private color;
|
|
49
67
|
private textAlignment;
|
|
50
|
-
private lineHeight
|
|
68
|
+
private lineHeight?;
|
|
69
|
+
private underline;
|
|
70
|
+
private strikethrough;
|
|
71
|
+
private skipInk;
|
|
72
|
+
private letterSpacing;
|
|
51
73
|
private content;
|
|
52
74
|
private maxLines?;
|
|
53
75
|
private overflow;
|
|
54
76
|
private readonly role?;
|
|
55
|
-
constructor({ fontSize, content, fontFamily, fontStyle, color, textAlignment, maxLines, overflow, lineHeight, role, }: TextElementParams);
|
|
77
|
+
constructor({ fontSize, content, fontFamily, fontStyle, color, textAlignment, maxLines, overflow, lineHeight, underline, strikethrough, skipInk, letterSpacing, role, }: TextElementParams);
|
|
56
78
|
private resolveStyle;
|
|
57
79
|
private applyStyle;
|
|
58
80
|
/**
|
|
@@ -88,7 +110,11 @@ export declare class TextElement extends SizedPDFElement implements Fragmentable
|
|
|
88
110
|
textAlignment: HorizontalAlignment;
|
|
89
111
|
maxLines: number | undefined;
|
|
90
112
|
overflow: TextOverflow;
|
|
91
|
-
lineHeight: number;
|
|
113
|
+
lineHeight: number | undefined;
|
|
114
|
+
underline: boolean;
|
|
115
|
+
strikethrough: boolean;
|
|
116
|
+
skipInk: boolean;
|
|
117
|
+
letterSpacing: number;
|
|
92
118
|
role: TextRole | undefined;
|
|
93
119
|
};
|
|
94
120
|
}
|