@ggraphic/editor 0.1.0-alpha.5 → 0.1.0-alpha.6
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/CanvasEditorProvider.d.ts +6 -1
- package/dist/components/CanvasEditor/CanvasEditorShell.js +139 -36
- package/dist/components/CanvasEditor/editorShell.d.ts +6 -0
- package/dist/components/CanvasEditor/types.d.ts +28 -0
- package/dist/components/GenericCanvas.d.ts +19 -2
- package/dist/components/GenericCanvas.js +28 -3
- package/dist/components/PagePreview.d.ts +8 -1
- package/dist/components/PagePreview.js +4 -1
- package/dist/components/TopBar/DownloadSection.d.ts +24 -3
- package/dist/components/TopBar/DownloadSection.js +45 -7
- package/dist/components/TopBar/ShareDropdown.d.ts +4 -2
- package/dist/components/TopBar/ShareDropdown.js +2 -2
- package/dist/entries/export.d.ts +14 -1
- package/dist/entries/export.js +14 -1
- package/dist/entries/services.d.ts +1 -1
- package/dist/export/fonts/fontFiles.d.ts +100 -0
- package/dist/export/fonts/fontFiles.js +333 -0
- package/dist/export/fonts/glyphs.d.ts +113 -0
- package/dist/export/fonts/glyphs.js +120 -0
- package/dist/export/fonts/variation.d.ts +23 -0
- package/dist/export/fonts/variation.js +43 -0
- package/dist/export/pdf/pdfContent.d.ts +108 -0
- package/dist/export/pdf/pdfContent.js +566 -0
- package/dist/export/pdf/pdfFontkit.d.ts +54 -0
- package/dist/export/pdf/pdfFontkit.js +159 -0
- package/dist/export/pdf/pdfGeometry.d.ts +54 -0
- package/dist/export/pdf/pdfGeometry.js +228 -0
- package/dist/export/pdf/pdfResources.d.ts +109 -0
- package/dist/export/pdf/pdfResources.js +314 -0
- package/dist/export/pdf/writePdf.d.ts +34 -0
- package/dist/export/pdf/writePdf.js +165 -0
- package/dist/export/pptx/exportPptxDeck.d.ts +10 -2
- package/dist/export/pptx/exportPptxDeck.js +5 -2
- package/dist/export/pptx/svgPicture.d.ts +19 -0
- package/dist/export/pptx/svgPicture.js +64 -0
- package/dist/export/pptx/vectorizeElement.d.ts +9 -0
- package/dist/export/pptx/vectorizeElement.js +118 -0
- package/dist/export/pptx/writePptx.d.ts +13 -0
- package/dist/export/pptx/writePptx.js +17 -0
- package/dist/export/printPdf.d.ts +1 -0
- package/dist/export/printPdf.js +1 -1
- package/dist/export/scene/browserDeps.d.ts +32 -0
- package/dist/export/scene/browserDeps.js +174 -0
- package/dist/export/scene/imageBytes.d.ts +79 -0
- package/dist/export/scene/imageBytes.js +330 -0
- package/dist/export/scene/itemBounds.d.ts +9 -0
- package/dist/export/scene/itemBounds.js +40 -0
- package/dist/export/scene/matrix.d.ts +13 -0
- package/dist/export/scene/matrix.js +32 -0
- package/dist/export/scene/rasterLimit.d.ts +13 -0
- package/dist/export/scene/rasterLimit.js +24 -0
- package/dist/export/scene/rebaseOps.d.ts +8 -0
- package/dist/export/scene/rebaseOps.js +51 -0
- package/dist/export/scene/recordPageScene.d.ts +28 -0
- package/dist/export/scene/recordPageScene.js +224 -0
- package/dist/export/scene/recordingCanvas.d.ts +55 -0
- package/dist/export/scene/recordingCanvas.js +71 -0
- package/dist/export/scene/recordingContext.d.ts +162 -0
- package/dist/export/scene/recordingContext.js +1031 -0
- package/dist/export/scene/replayScene.d.ts +31 -0
- package/dist/export/scene/replayScene.js +213 -0
- package/dist/export/scene/resolveScene.d.ts +48 -0
- package/dist/export/scene/resolveScene.js +236 -0
- package/dist/export/scene/trustedSvgImages.d.ts +3 -0
- package/dist/export/scene/trustedSvgImages.js +21 -0
- package/dist/export/scene/types.d.ts +241 -0
- package/dist/export/scene/types.js +38 -0
- package/dist/export/slidesPdf.d.ts +10 -2
- package/dist/export/slidesPdf.js +8 -4
- package/dist/export/svg/__fixtures__/barChart.svg +1 -0
- package/dist/export/svg/__golden__/fixture-editable.svg +87 -0
- package/dist/export/svg/__golden__/fixture-outlines.svg +88 -0
- package/dist/export/svg/inlineSvg.d.ts +10 -0
- package/dist/export/svg/inlineSvg.js +119 -0
- package/dist/export/svg/scopeCss.d.ts +45 -0
- package/dist/export/svg/scopeCss.js +214 -0
- package/dist/export/svg/svgColor.d.ts +8 -0
- package/dist/export/svg/svgColor.js +46 -0
- package/dist/export/svg/svgImageScene.d.ts +41 -0
- package/dist/export/svg/svgImageScene.js +128 -0
- package/dist/export/svg/svgPathData.d.ts +31 -0
- package/dist/export/svg/svgPathData.js +466 -0
- package/dist/export/svg/svgToScene.d.ts +19 -0
- package/dist/export/svg/svgToScene.js +1183 -0
- package/dist/export/svg/writeSvg.d.ts +33 -0
- package/dist/export/svg/writeSvg.js +594 -0
- package/dist/export/svg/xml.d.ts +23 -0
- package/dist/export/svg/xml.js +49 -0
- package/dist/export/vectorDeps.d.ts +27 -0
- package/dist/export/vectorDeps.js +37 -0
- package/dist/export/vectorExport.d.ts +85 -0
- package/dist/export/vectorExport.js +287 -0
- package/dist/hostServices.d.ts +15 -0
- package/dist/i18n/de.d.ts +26 -9
- package/dist/i18n/de.js +26 -10
- package/dist/i18n/en.js +24 -0
- package/dist/primitives/AssetPrimitive.js +7 -2
- package/dist/primitives/CanvasIcons.js +6 -0
- package/dist/primitives/CanvasStage.js +2 -2
- package/dist/primitives/ChartPrimitive.d.ts +11 -1
- package/dist/primitives/ChartPrimitive.js +73 -19
- package/dist/primitives/IconPrimitive.js +3 -0
- package/dist/primitives/IllustrationPrimitive.js +14 -1
- package/dist/primitives/chartFont.d.ts +30 -0
- package/dist/primitives/chartFont.js +127 -0
- package/dist/sidebar/sections/ImageAdjustSection.js +5 -4
- package/dist/sidebar/sections/assets/ElementsPanel.js +4 -2
- package/dist/sidebar/sections/assets/assetAudience.d.ts +12 -0
- package/dist/sidebar/sections/assets/assetAudience.js +18 -0
- package/dist/sidebar/sections/assets/useElementCatalog.js +3 -2
- package/dist/styles/compiled.css +1 -1
- package/dist/utils/canvasAssets.d.ts +11 -1
- package/dist/utils/canvasAssets.js +8 -0
- package/dist/utils/canvasIcons.js +5 -3
- package/dist/utils/captureStage.d.ts +19 -0
- package/dist/utils/captureStage.js +14 -1
- package/dist/utils/externalIcons.d.ts +7 -0
- package/dist/utils/externalIcons.js +70 -3
- package/dist/utils/illustrations/svgCache.js +6 -1
- package/dist/utils/imageFilters.d.ts +13 -0
- package/dist/utils/imageFilters.js +11 -0
- package/dist/utils/zipPageImages.d.ts +8 -0
- package/dist/utils/zipPageImages.js +16 -4
- package/package.json +6 -2
- package/skills/ggraphic-embed/SKILL.md +33 -1
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/** True for a font whose `glyf` is stored transformed, which is what WOFF2 does. */
|
|
2
|
+
function hasTransformedGlyf(font) {
|
|
3
|
+
return font.directory?.tables?.glyf?.transformed === true;
|
|
4
|
+
}
|
|
5
|
+
/** TrueType contours of a path: quadratic segments only, which is all a `glyf` table has. */
|
|
6
|
+
function contoursOf(commands) {
|
|
7
|
+
const contours = [];
|
|
8
|
+
let current = [];
|
|
9
|
+
const close = () => {
|
|
10
|
+
const [first] = current;
|
|
11
|
+
const last = current.at(-1);
|
|
12
|
+
// A contour closes itself: a last point on the first one says nothing.
|
|
13
|
+
if (first &&
|
|
14
|
+
last &&
|
|
15
|
+
current.length > 1 &&
|
|
16
|
+
last.onCurve &&
|
|
17
|
+
last.x === first.x &&
|
|
18
|
+
last.y === first.y) {
|
|
19
|
+
current.pop();
|
|
20
|
+
}
|
|
21
|
+
if (current.length > 0)
|
|
22
|
+
contours.push(current);
|
|
23
|
+
current = [];
|
|
24
|
+
};
|
|
25
|
+
for (const { command, args } of commands) {
|
|
26
|
+
switch (command) {
|
|
27
|
+
case 'moveTo':
|
|
28
|
+
close();
|
|
29
|
+
current.push({ x: Math.round(args[0]), y: Math.round(args[1]), onCurve: true });
|
|
30
|
+
break;
|
|
31
|
+
case 'lineTo':
|
|
32
|
+
current.push({ x: Math.round(args[0]), y: Math.round(args[1]), onCurve: true });
|
|
33
|
+
break;
|
|
34
|
+
case 'quadraticCurveTo':
|
|
35
|
+
current.push({ x: Math.round(args[0]), y: Math.round(args[1]), onCurve: false }, { x: Math.round(args[2]), y: Math.round(args[3]), onCurve: true });
|
|
36
|
+
break;
|
|
37
|
+
case 'bezierCurveTo':
|
|
38
|
+
throw new Error('a cubic outline cannot be written as a TrueType glyph');
|
|
39
|
+
case 'closePath':
|
|
40
|
+
close();
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
close();
|
|
45
|
+
return contours;
|
|
46
|
+
}
|
|
47
|
+
/** A coordinate as the flag bits and bytes of a `glyf` record: same as before, one byte, or two. */
|
|
48
|
+
function coordinate(delta, shortBit, sameBit, bytes) {
|
|
49
|
+
if (delta === 0)
|
|
50
|
+
return sameBit;
|
|
51
|
+
if (Math.abs(delta) <= 255) {
|
|
52
|
+
bytes.push(Math.abs(delta));
|
|
53
|
+
return shortBit | (delta > 0 ? sameBit : 0);
|
|
54
|
+
}
|
|
55
|
+
bytes.push((delta >> 8) & 255, delta & 255);
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
/** The simple glyph record of `glyf`, aligned to four bytes, and the glyph's left edge. */
|
|
59
|
+
export function encodeSimpleGlyph(commands) {
|
|
60
|
+
const contours = contoursOf(commands);
|
|
61
|
+
const points = contours.flat();
|
|
62
|
+
if (points.length === 0)
|
|
63
|
+
return { bytes: new Uint8Array(0), xMin: 0 };
|
|
64
|
+
const xs = points.map((p) => p.x);
|
|
65
|
+
const ys = points.map((p) => p.y);
|
|
66
|
+
const out = [];
|
|
67
|
+
const int16 = (value) => out.push((value >> 8) & 255, value & 255);
|
|
68
|
+
const xMin = Math.min(...xs);
|
|
69
|
+
int16(contours.length);
|
|
70
|
+
int16(xMin);
|
|
71
|
+
int16(Math.min(...ys));
|
|
72
|
+
int16(Math.max(...xs));
|
|
73
|
+
int16(Math.max(...ys));
|
|
74
|
+
let end = -1;
|
|
75
|
+
for (const contour of contours)
|
|
76
|
+
int16((end += contour.length));
|
|
77
|
+
int16(0); // no instructions
|
|
78
|
+
const flags = [];
|
|
79
|
+
const xBytes = [];
|
|
80
|
+
const yBytes = [];
|
|
81
|
+
let [x, y] = [0, 0];
|
|
82
|
+
points.forEach((point, index) => {
|
|
83
|
+
const flag = (point.onCurve ? 1 : 0) |
|
|
84
|
+
coordinate(xs[index] - x, 2, 16, xBytes) |
|
|
85
|
+
coordinate(ys[index] - y, 4, 32, yBytes);
|
|
86
|
+
flags.push(flag);
|
|
87
|
+
[x, y] = [xs[index], ys[index]];
|
|
88
|
+
});
|
|
89
|
+
// Equal flags in a row: the flag with the repeat bit, then how many more.
|
|
90
|
+
for (let i = 0; i < flags.length;) {
|
|
91
|
+
let run = 1;
|
|
92
|
+
while (i + run < flags.length && flags[i + run] === flags[i] && run < 256)
|
|
93
|
+
run++;
|
|
94
|
+
if (run > 1)
|
|
95
|
+
out.push(flags[i] | 8, run - 1);
|
|
96
|
+
else
|
|
97
|
+
out.push(flags[i]);
|
|
98
|
+
i += run;
|
|
99
|
+
}
|
|
100
|
+
out.push(...xBytes, ...yBytes);
|
|
101
|
+
while (out.length % 4 !== 0)
|
|
102
|
+
out.push(0);
|
|
103
|
+
return { bytes: Uint8Array.from(out), xMin };
|
|
104
|
+
}
|
|
105
|
+
/** Makes the glyph record from the outline instead of copying bytes. */
|
|
106
|
+
function addGlyphFromPath(id) {
|
|
107
|
+
const glyph = this.font.getGlyph(id);
|
|
108
|
+
const { bytes, xMin } = encodeSimpleGlyph(glyph.path.commands);
|
|
109
|
+
// fontkit's stream takes only the kind of buffer it has bundled, which a `Uint8Array` is not.
|
|
110
|
+
// That class is reached through a buffer `encodeSimple` made (an empty glyph, which it can do).
|
|
111
|
+
const sample = this.glyphEncoder.encodeSimple({ commands: [], bbox: { minX: 0, minY: 0, maxX: 0, maxY: 0 } }, []);
|
|
112
|
+
const record = sample.constructor.from(bytes);
|
|
113
|
+
this.glyf.push(record);
|
|
114
|
+
this.loca.offsets.push(this.offset);
|
|
115
|
+
// The left side bearing has to be the glyph's own left edge, or a rasteriser shifts the glyph.
|
|
116
|
+
this.hmtx.metrics.push({ advance: glyph.advanceWidth, bearing: xMin });
|
|
117
|
+
this.offset += bytes.length;
|
|
118
|
+
return this.glyf.length - 1;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The instances of variable fonts, by the bytes object `embeddableBytes` handed out for them.
|
|
122
|
+
* `embedFont` takes bytes and has fontkit parse them, which gives the default instance: this is
|
|
123
|
+
* how `create` is told to hand out the instance instead.
|
|
124
|
+
*/
|
|
125
|
+
const instances = new WeakMap();
|
|
126
|
+
const patched = new WeakSet();
|
|
127
|
+
/**
|
|
128
|
+
* The bytes to give `embedFont` for `file`: its own bytes, or for a variation instance a view of
|
|
129
|
+
* them that `fontkitForPdf(...).create` recognises (identity, not content).
|
|
130
|
+
*/
|
|
131
|
+
export function embeddableBytes(file) {
|
|
132
|
+
if (file.variation !== 'instance')
|
|
133
|
+
return file.bytes;
|
|
134
|
+
const view = new Uint8Array(file.bytes.buffer, file.bytes.byteOffset, file.bytes.byteLength);
|
|
135
|
+
instances.set(view, file.font);
|
|
136
|
+
return view;
|
|
137
|
+
}
|
|
138
|
+
/** What `registerFontkit` takes: `create` as fontkit has it, with the WOFF2 subset repaired. */
|
|
139
|
+
export function fontkitForPdf(fontkit) {
|
|
140
|
+
return {
|
|
141
|
+
create(bytes) {
|
|
142
|
+
const instance = instances.get(bytes);
|
|
143
|
+
// Boundary cast: fontkit's shipped declarations do not describe the font it returns.
|
|
144
|
+
const font = (instance ?? fontkit.create(bytes));
|
|
145
|
+
// An instance of a variable font is cut from its paths too: fontkit's own encoder for
|
|
146
|
+
// variable glyphs has the flag-run bug described at the top.
|
|
147
|
+
if ((instance || hasTransformedGlyf(font)) && !patched.has(font)) {
|
|
148
|
+
patched.add(font);
|
|
149
|
+
const createSubset = font.createSubset.bind(font);
|
|
150
|
+
font.createSubset = () => {
|
|
151
|
+
const subset = createSubset();
|
|
152
|
+
subset._addGlyph = addGlyphFromPath;
|
|
153
|
+
return subset;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
return font;
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Matrix, PathCommand, SceneOp } from '../scene/types.js';
|
|
2
|
+
/** Min and max corners. */
|
|
3
|
+
export interface Rect {
|
|
4
|
+
x0: number;
|
|
5
|
+
y0: number;
|
|
6
|
+
x1: number;
|
|
7
|
+
y1: number;
|
|
8
|
+
}
|
|
9
|
+
export declare const num: (value: number) => string;
|
|
10
|
+
/** Colour components and the like: 4 decimals round-trip an 8 bit channel. */
|
|
11
|
+
export declare const unit: (value: number) => string;
|
|
12
|
+
/**
|
|
13
|
+
* A CSS hex colour as the lower-case `#rrggbb` the scene and the CMYK map are keyed by: `#rgb` is
|
|
14
|
+
* expanded, an alpha digit (`#rgba`, `#rrggbbaa`) is dropped. Null for anything else.
|
|
15
|
+
*/
|
|
16
|
+
export declare function normalHex(colour: string): string | null;
|
|
17
|
+
/** The linear part has six decimals (a rotation 1000 px from the origin), the move three. */
|
|
18
|
+
export declare function matrixOperands(m: Matrix): string;
|
|
19
|
+
export declare const isIdentity: (m: Matrix) => boolean;
|
|
20
|
+
/** A matrix that squeezes the plane to a line or a point (or is not a number) draws nothing. */
|
|
21
|
+
export declare function isDegenerate(m: Matrix): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* The path as PDF operators, every point mapped by `m` when given. PDF has no quadratic curve:
|
|
24
|
+
* `Q` becomes the cubic with the same shape. A segment that starts a path without a `M` starts
|
|
25
|
+
* one at its first point, as the canvas does.
|
|
26
|
+
*/
|
|
27
|
+
export declare function pathOperators(commands: readonly PathCommand[], m?: Matrix): string;
|
|
28
|
+
export declare function commandsBounds(commands: readonly PathCommand[]): Rect | null;
|
|
29
|
+
export declare function transformRect(rect: Rect, m: Matrix): Rect;
|
|
30
|
+
export declare const unionRect: (a: Rect | null, b: Rect | null) => Rect | null;
|
|
31
|
+
/** The overlap, or null when the rects do not share any area. */
|
|
32
|
+
export declare function intersectRect(a: Rect, b: Rect): Rect | null;
|
|
33
|
+
export declare const growRect: (r: Rect, by: number) => Rect;
|
|
34
|
+
/** How much a matrix can stretch a length. */
|
|
35
|
+
export declare const maxScale: (m: Matrix) => number;
|
|
36
|
+
type TextOp = Extract<SceneOp, {
|
|
37
|
+
kind: 'text';
|
|
38
|
+
}>;
|
|
39
|
+
type StrokeOf = Extract<SceneOp, {
|
|
40
|
+
kind: 'path';
|
|
41
|
+
}>['stroke'];
|
|
42
|
+
/** Half the stroke width, plus what a miter can add, in the op's user space. */
|
|
43
|
+
export declare function strokePad(stroke: StrokeOf): number;
|
|
44
|
+
/**
|
|
45
|
+
* Where a text op can put ink, in its user space. Without glyph metrics this is generous: from
|
|
46
|
+
* the first x to the last x plus two em, and the line from above the capitals to below the
|
|
47
|
+
* descenders. A raster island made from it can be larger than the text, never smaller.
|
|
48
|
+
*/
|
|
49
|
+
export declare function textBounds(op: Pick<TextOp, 'xs' | 'y' | 'font' | 'mode' | 'stroke'>): Rect | null;
|
|
50
|
+
/** The ink of an op in page px, clipped by its clips. Null: nothing, or nothing that can be known. */
|
|
51
|
+
export declare function opBounds(op: SceneOp): Rect | null;
|
|
52
|
+
/** `bounds` cut to the clips of `op`: null when they leave nothing, or one has no path to know. */
|
|
53
|
+
export declare function clipped(bounds: Rect | null, op: Pick<SceneOp, 'clips'>): Rect | null;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
/**
|
|
3
|
+
* The arithmetic of the PDF writer: number formatting, path operators, and bounding boxes of ops
|
|
4
|
+
* (for raster islands and soft masks). No pdf-lib here; the output is text a content stream holds.
|
|
5
|
+
*/
|
|
6
|
+
import { apply } from '../scene/matrix.js';
|
|
7
|
+
import { formatNumber } from '../svg/xml.js';
|
|
8
|
+
export const num = (value) => formatNumber(value, 3);
|
|
9
|
+
/** Colour components and the like: 4 decimals round-trip an 8 bit channel. */
|
|
10
|
+
export const unit = (value) => formatNumber(value, 4);
|
|
11
|
+
/**
|
|
12
|
+
* A CSS hex colour as the lower-case `#rrggbb` the scene and the CMYK map are keyed by: `#rgb` is
|
|
13
|
+
* expanded, an alpha digit (`#rgba`, `#rrggbbaa`) is dropped. Null for anything else.
|
|
14
|
+
*/
|
|
15
|
+
export function normalHex(colour) {
|
|
16
|
+
const match = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.exec(colour.trim());
|
|
17
|
+
if (!match)
|
|
18
|
+
return null;
|
|
19
|
+
const digits = match[1].toLowerCase();
|
|
20
|
+
if (digits.length <= 4) {
|
|
21
|
+
return `#${Array.from(digits.slice(0, 3), (digit) => digit + digit).join('')}`;
|
|
22
|
+
}
|
|
23
|
+
return `#${digits.slice(0, 6)}`;
|
|
24
|
+
}
|
|
25
|
+
/** The linear part has six decimals (a rotation 1000 px from the origin), the move three. */
|
|
26
|
+
export function matrixOperands(m) {
|
|
27
|
+
const [a, b, c, d] = [m[0], m[1], m[2], m[3]].map((v) => formatNumber(v, 6));
|
|
28
|
+
return `${a} ${b} ${c} ${d} ${num(m[4])} ${num(m[5])}`;
|
|
29
|
+
}
|
|
30
|
+
export const isIdentity = (m) => m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
|
|
31
|
+
/** A matrix that squeezes the plane to a line or a point (or is not a number) draws nothing. */
|
|
32
|
+
export function isDegenerate(m) {
|
|
33
|
+
const det = m[0] * m[3] - m[1] * m[2];
|
|
34
|
+
return !m.every(Number.isFinite) || det === 0;
|
|
35
|
+
}
|
|
36
|
+
// ---------------------------------------------------------------------------------------------
|
|
37
|
+
// Paths
|
|
38
|
+
/**
|
|
39
|
+
* The path as PDF operators, every point mapped by `m` when given. PDF has no quadratic curve:
|
|
40
|
+
* `Q` becomes the cubic with the same shape. A segment that starts a path without a `M` starts
|
|
41
|
+
* one at its first point, as the canvas does.
|
|
42
|
+
*/
|
|
43
|
+
export function pathOperators(commands, m) {
|
|
44
|
+
const out = [];
|
|
45
|
+
const point = (x, y) => {
|
|
46
|
+
const p = m ? apply(m, x, y) : { x, y };
|
|
47
|
+
return `${num(p.x)} ${num(p.y)}`;
|
|
48
|
+
};
|
|
49
|
+
let open = false;
|
|
50
|
+
let cx = 0;
|
|
51
|
+
let cy = 0;
|
|
52
|
+
let sx = 0;
|
|
53
|
+
let sy = 0;
|
|
54
|
+
for (const c of commands) {
|
|
55
|
+
switch (c.op) {
|
|
56
|
+
case 'M':
|
|
57
|
+
out.push(`${point(c.x, c.y)} m`);
|
|
58
|
+
[cx, cy, sx, sy, open] = [c.x, c.y, c.x, c.y, true];
|
|
59
|
+
break;
|
|
60
|
+
case 'L':
|
|
61
|
+
out.push(`${point(c.x, c.y)} ${open ? 'l' : 'm'}`);
|
|
62
|
+
if (!open)
|
|
63
|
+
[sx, sy, open] = [c.x, c.y, true];
|
|
64
|
+
[cx, cy] = [c.x, c.y];
|
|
65
|
+
break;
|
|
66
|
+
case 'C':
|
|
67
|
+
if (!open) {
|
|
68
|
+
out.push(`${point(c.x1, c.y1)} m`);
|
|
69
|
+
[cx, cy, sx, sy, open] = [c.x1, c.y1, c.x1, c.y1, true];
|
|
70
|
+
}
|
|
71
|
+
out.push(`${point(c.x1, c.y1)} ${point(c.x2, c.y2)} ${point(c.x, c.y)} c`);
|
|
72
|
+
[cx, cy] = [c.x, c.y];
|
|
73
|
+
break;
|
|
74
|
+
case 'Q':
|
|
75
|
+
if (!open) {
|
|
76
|
+
out.push(`${point(c.x1, c.y1)} m`);
|
|
77
|
+
[cx, cy, sx, sy, open] = [c.x1, c.y1, c.x1, c.y1, true];
|
|
78
|
+
}
|
|
79
|
+
out.push(`${point(cx + (2 / 3) * (c.x1 - cx), cy + (2 / 3) * (c.y1 - cy))} ` +
|
|
80
|
+
`${point(c.x + (2 / 3) * (c.x1 - c.x), c.y + (2 / 3) * (c.y1 - c.y))} ` +
|
|
81
|
+
`${point(c.x, c.y)} c`);
|
|
82
|
+
[cx, cy] = [c.x, c.y];
|
|
83
|
+
break;
|
|
84
|
+
case 'Z':
|
|
85
|
+
out.push('h');
|
|
86
|
+
[cx, cy] = [sx, sy];
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return out.join(' ');
|
|
91
|
+
}
|
|
92
|
+
// ---------------------------------------------------------------------------------------------
|
|
93
|
+
// Bounds
|
|
94
|
+
export function commandsBounds(commands) {
|
|
95
|
+
let bounds = null;
|
|
96
|
+
const add = (x, y) => {
|
|
97
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
98
|
+
return;
|
|
99
|
+
bounds = bounds
|
|
100
|
+
? {
|
|
101
|
+
x0: Math.min(bounds.x0, x),
|
|
102
|
+
y0: Math.min(bounds.y0, y),
|
|
103
|
+
x1: Math.max(bounds.x1, x),
|
|
104
|
+
y1: Math.max(bounds.y1, y),
|
|
105
|
+
}
|
|
106
|
+
: { x0: x, y0: y, x1: x, y1: y };
|
|
107
|
+
};
|
|
108
|
+
for (const c of commands) {
|
|
109
|
+
if (c.op === 'Z')
|
|
110
|
+
continue;
|
|
111
|
+
add(c.x, c.y);
|
|
112
|
+
if (c.op === 'C') {
|
|
113
|
+
add(c.x1, c.y1);
|
|
114
|
+
add(c.x2, c.y2);
|
|
115
|
+
}
|
|
116
|
+
else if (c.op === 'Q')
|
|
117
|
+
add(c.x1, c.y1);
|
|
118
|
+
}
|
|
119
|
+
return bounds;
|
|
120
|
+
}
|
|
121
|
+
export function transformRect(rect, m) {
|
|
122
|
+
const corners = [
|
|
123
|
+
apply(m, rect.x0, rect.y0),
|
|
124
|
+
apply(m, rect.x1, rect.y0),
|
|
125
|
+
apply(m, rect.x1, rect.y1),
|
|
126
|
+
apply(m, rect.x0, rect.y1),
|
|
127
|
+
];
|
|
128
|
+
const xs = corners.map((p) => p.x);
|
|
129
|
+
const ys = corners.map((p) => p.y);
|
|
130
|
+
return { x0: Math.min(...xs), y0: Math.min(...ys), x1: Math.max(...xs), y1: Math.max(...ys) };
|
|
131
|
+
}
|
|
132
|
+
export const unionRect = (a, b) => !a || !b
|
|
133
|
+
? (a ?? b)
|
|
134
|
+
: {
|
|
135
|
+
x0: Math.min(a.x0, b.x0),
|
|
136
|
+
y0: Math.min(a.y0, b.y0),
|
|
137
|
+
x1: Math.max(a.x1, b.x1),
|
|
138
|
+
y1: Math.max(a.y1, b.y1),
|
|
139
|
+
};
|
|
140
|
+
/** The overlap, or null when the rects do not share any area. */
|
|
141
|
+
export function intersectRect(a, b) {
|
|
142
|
+
const r = {
|
|
143
|
+
x0: Math.max(a.x0, b.x0),
|
|
144
|
+
y0: Math.max(a.y0, b.y0),
|
|
145
|
+
x1: Math.min(a.x1, b.x1),
|
|
146
|
+
y1: Math.min(a.y1, b.y1),
|
|
147
|
+
};
|
|
148
|
+
return r.x1 > r.x0 && r.y1 > r.y0 ? r : null;
|
|
149
|
+
}
|
|
150
|
+
export const growRect = (r, by) => ({
|
|
151
|
+
x0: r.x0 - by,
|
|
152
|
+
y0: r.y0 - by,
|
|
153
|
+
x1: r.x1 + by,
|
|
154
|
+
y1: r.y1 + by,
|
|
155
|
+
});
|
|
156
|
+
/** How much a matrix can stretch a length. */
|
|
157
|
+
export const maxScale = (m) => Math.max(Math.hypot(m[0], m[1]), Math.hypot(m[2], m[3]));
|
|
158
|
+
/** Half the stroke width, plus what a miter can add, in the op's user space. */
|
|
159
|
+
export function strokePad(stroke) {
|
|
160
|
+
if (!stroke)
|
|
161
|
+
return 0.5;
|
|
162
|
+
return (stroke.width / 2) * (stroke.join === 'miter' ? Math.max(1, stroke.miterLimit) : 1);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Where a text op can put ink, in its user space. Without glyph metrics this is generous: from
|
|
166
|
+
* the first x to the last x plus two em, and the line from above the capitals to below the
|
|
167
|
+
* descenders. A raster island made from it can be larger than the text, never smaller.
|
|
168
|
+
*/
|
|
169
|
+
export function textBounds(op) {
|
|
170
|
+
const xs = op.xs.filter(Number.isFinite);
|
|
171
|
+
if (xs.length === 0 || !Number.isFinite(op.y))
|
|
172
|
+
return null;
|
|
173
|
+
const size = op.font.size;
|
|
174
|
+
const pad = op.mode === 'stroke' ? strokePad(op.stroke) : 0;
|
|
175
|
+
return {
|
|
176
|
+
x0: Math.min(...xs) - 0.3 * size - pad,
|
|
177
|
+
y0: op.y - 1.3 * size - pad,
|
|
178
|
+
x1: Math.max(...xs) + 2 * size + pad,
|
|
179
|
+
y1: op.y + 0.5 * size + pad,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/** The ink of an op in page px, clipped by its clips. Null: nothing, or nothing that can be known. */
|
|
183
|
+
export function opBounds(op) {
|
|
184
|
+
let user = null;
|
|
185
|
+
switch (op.kind) {
|
|
186
|
+
case 'path': {
|
|
187
|
+
const bounds = commandsBounds(op.commands);
|
|
188
|
+
user = bounds && (op.mode === 'stroke' ? growRect(bounds, strokePad(op.stroke)) : bounds);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case 'text':
|
|
192
|
+
user = textBounds(op);
|
|
193
|
+
break;
|
|
194
|
+
case 'image':
|
|
195
|
+
user = {
|
|
196
|
+
x0: Math.min(op.dx, op.dx + op.dw),
|
|
197
|
+
y0: Math.min(op.dy, op.dy + op.dh),
|
|
198
|
+
x1: Math.max(op.dx, op.dx + op.dw),
|
|
199
|
+
y1: Math.max(op.dy, op.dy + op.dh),
|
|
200
|
+
};
|
|
201
|
+
break;
|
|
202
|
+
case 'raster':
|
|
203
|
+
user = { x0: op.x, y0: op.y, x1: op.x + op.width, y1: op.y + op.height };
|
|
204
|
+
break;
|
|
205
|
+
case 'group': {
|
|
206
|
+
// The children are in page px already; the group's own matrix then moves the buffer.
|
|
207
|
+
let union = null;
|
|
208
|
+
for (const child of op.ops)
|
|
209
|
+
union = unionRect(union, opBounds(child));
|
|
210
|
+
user = union;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return clipped(user && transformRect(user, op.matrix), op);
|
|
215
|
+
}
|
|
216
|
+
/** `bounds` cut to the clips of `op`: null when they leave nothing, or one has no path to know. */
|
|
217
|
+
export function clipped(bounds, op) {
|
|
218
|
+
let result = bounds;
|
|
219
|
+
for (const clip of op.clips) {
|
|
220
|
+
if (!result)
|
|
221
|
+
return null;
|
|
222
|
+
const box = commandsBounds(clip.commands);
|
|
223
|
+
if (!box)
|
|
224
|
+
return null;
|
|
225
|
+
result = intersectRect(result, transformRect(box, clip.matrix));
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The objects a PDF page draws with, made through pdf-lib: embedded fonts and images (shared by
|
|
3
|
+
* the whole document), and the page's resource dictionary with the names content streams use
|
|
4
|
+
* (`/F1`, `/Im1`, `/GS1`, ...), plus the dictionaries the writer builds by hand: shadings,
|
|
5
|
+
* tiling patterns, transparency groups and soft masks.
|
|
6
|
+
*
|
|
7
|
+
* pdf-lib loads by dynamic `import()` in `writePdf`; this file only holds its types and gets the
|
|
8
|
+
* module passed in.
|
|
9
|
+
*/
|
|
10
|
+
import { type FontFile } from '../fonts/fontFiles.js';
|
|
11
|
+
import { type Rect } from './pdfGeometry.js';
|
|
12
|
+
import type { FontSpec, Matrix, Paint, SceneImageSource } from '../scene/types.js';
|
|
13
|
+
import type * as PdfLib from 'pdf-lib';
|
|
14
|
+
type PdfLibModule = typeof PdfLib;
|
|
15
|
+
type Gradient = Extract<Paint, {
|
|
16
|
+
type: 'linear' | 'radial';
|
|
17
|
+
}>;
|
|
18
|
+
/** The four inks of a colour, each 0..1. */
|
|
19
|
+
export type Ink = readonly [number, number, number, number];
|
|
20
|
+
export declare class PdfAssets {
|
|
21
|
+
readonly lib: PdfLibModule;
|
|
22
|
+
readonly doc: PdfLib.PDFDocument;
|
|
23
|
+
readonly cmyk: ReadonlyMap<string, Ink>;
|
|
24
|
+
/** The hex (lower case) of every colour written as RGB, for `report.rgbBrandColors`. */
|
|
25
|
+
readonly rgbHexes: Set<string>;
|
|
26
|
+
private readonly fonts;
|
|
27
|
+
private readonly images;
|
|
28
|
+
private readonly shadings;
|
|
29
|
+
private readonly patterns;
|
|
30
|
+
private readonly made;
|
|
31
|
+
/**
|
|
32
|
+
* @param cmyk the colours (hex, lower case) the brand has print values for; whatever paints
|
|
33
|
+
* with one of them is DeviceCMYK, whatever does not is RGB
|
|
34
|
+
*/
|
|
35
|
+
constructor(lib: PdfLibModule, doc: PdfLib.PDFDocument, cmyk?: ReadonlyMap<string, Ink>);
|
|
36
|
+
get context(): PdfLib.PDFContext;
|
|
37
|
+
/**
|
|
38
|
+
* A subset of the font file, named `ABCDEF+PostScriptName` (pdf-lib's own default has no such
|
|
39
|
+
* tag).
|
|
40
|
+
*
|
|
41
|
+
* A font whose licence says "no subsetting" goes in whole, under its plain name (the prefix
|
|
42
|
+
* tells a reader the font is a subset). An sfnt file (TrueType, OpenType) is embedded as it is.
|
|
43
|
+
* A WOFF or WOFF2 file cannot be: pdf-lib would write its bytes unchanged and no viewer reads a
|
|
44
|
+
* WOFF as a font program. Such a font goes through the subsetter with every glyph it has.
|
|
45
|
+
*/
|
|
46
|
+
font(file: FontFile, face: FontSpec, fileIndex: number): Promise<PdfLib.PDFFont>;
|
|
47
|
+
/** The image once per `key` (the source object or the PNG bytes); null if pdf-lib cannot read it. */
|
|
48
|
+
image(key: object, bytes: Uint8Array, mime: 'image/png' | 'image/jpeg'): Promise<PdfLib.PDFImage | null>;
|
|
49
|
+
/** The picture `make` draws for `key`, made the first time it is asked for and not again. */
|
|
50
|
+
once(key: object, make: () => Promise<PdfLib.PDFImage | null>): Promise<PdfLib.PDFImage | null>;
|
|
51
|
+
bitmap(source: Extract<SceneImageSource, {
|
|
52
|
+
kind: 'bitmap';
|
|
53
|
+
}>): Promise<PdfLib.PDFImage | null>;
|
|
54
|
+
/**
|
|
55
|
+
* The shading of a gradient: axial (type 2) or radial (type 3), extended past both ends as a
|
|
56
|
+
* canvas gradient is. `channel` picks what the colour function returns: the RGB of the stops,
|
|
57
|
+
* their CMYK (every stop must be in the brand's map), or their alpha as a gray value (for a soft
|
|
58
|
+
* mask). Null when the geometry paints nothing.
|
|
59
|
+
*/
|
|
60
|
+
shading(paint: Gradient, channel: 'rgb' | 'cmyk' | 'alpha'): PdfLib.PDFRef | null;
|
|
61
|
+
/**
|
|
62
|
+
* A stitching function (type 3) of exponential segments (type 2, N = 1) from stop to stop. The
|
|
63
|
+
* ends are extended to 0 and 1 with the first and last colour; a hard stop (two stops at one
|
|
64
|
+
* offset) is a segment of no width and is left out, so the bounds rise strictly.
|
|
65
|
+
*/
|
|
66
|
+
private colourFunction;
|
|
67
|
+
/** A shading pattern (type 2). `matrix` maps the pattern space to the default space of its content stream. */
|
|
68
|
+
shadingPattern(shading: PdfLib.PDFRef, matrix: Matrix): PdfLib.PDFRef;
|
|
69
|
+
/** One pattern object per key: a chart's bars share a gradient and a matrix. */
|
|
70
|
+
private pattern;
|
|
71
|
+
/**
|
|
72
|
+
* A tiling pattern (type 1) that draws `image` at `width` x `height` once per cell, upright in a
|
|
73
|
+
* y-down pattern space as a canvas pattern is. `matrix` as for `shadingPattern`.
|
|
74
|
+
*/
|
|
75
|
+
tilingPattern(image: PdfLib.PDFImage, width: number, height: number, matrix: Matrix): PdfLib.PDFRef;
|
|
76
|
+
/** A form XObject; `group` makes it a transparency group, drawn as one picture with the alpha set outside. */
|
|
77
|
+
form(content: string, bbox: Rect, resources: PdfLib.PDFRef | PdfLib.PDFDict, group: {
|
|
78
|
+
colourSpace?: 'DeviceGray';
|
|
79
|
+
} | null): PdfLib.PDFRef;
|
|
80
|
+
/**
|
|
81
|
+
* The soft mask that lets a gradient's alpha through: a luminosity mask whose group paints the
|
|
82
|
+
* gradient's alpha as gray. It is evaluated in the space the graphics state is set in, so the
|
|
83
|
+
* shading's coordinates are the op's own. `bbox` is the area the op can touch, in that space.
|
|
84
|
+
*/
|
|
85
|
+
alphaMask(paint: Gradient, bbox: Rect): PdfLib.PDFRef | null;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The resource dictionary of a page. The content of a form XObject on the page is written with the
|
|
89
|
+
* same names, so a form gets a copy of the dictionary as it is when the form is made (`snapshot`):
|
|
90
|
+
* it holds what the form draws with, and not itself.
|
|
91
|
+
*/
|
|
92
|
+
export declare class PageResources {
|
|
93
|
+
private readonly assets;
|
|
94
|
+
readonly ref: PdfLib.PDFRef;
|
|
95
|
+
private readonly dict;
|
|
96
|
+
private readonly names;
|
|
97
|
+
private readonly counts;
|
|
98
|
+
private readonly states;
|
|
99
|
+
constructor(assets: PdfAssets);
|
|
100
|
+
private add;
|
|
101
|
+
font: (font: PdfLib.PDFFont) => string;
|
|
102
|
+
xobject: (ref: PdfLib.PDFRef) => string;
|
|
103
|
+
pattern: (ref: PdfLib.PDFRef) => string;
|
|
104
|
+
/** `ca` and `CA` together, optionally with a soft mask; one dictionary per distinct state. */
|
|
105
|
+
extGState(alpha: number, softMask?: PdfLib.PDFRef | null): string;
|
|
106
|
+
/** A copy of the dictionary now: the sections are copied too, so what is added later is not in it. */
|
|
107
|
+
snapshot(): PdfLib.PDFDict;
|
|
108
|
+
}
|
|
109
|
+
export {};
|