@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.
Files changed (125) hide show
  1. package/dist/CanvasEditorProvider.d.ts +6 -1
  2. package/dist/components/CanvasEditor/CanvasEditorShell.js +139 -36
  3. package/dist/components/CanvasEditor/editorShell.d.ts +6 -0
  4. package/dist/components/CanvasEditor/types.d.ts +28 -0
  5. package/dist/components/GenericCanvas.d.ts +19 -2
  6. package/dist/components/GenericCanvas.js +28 -3
  7. package/dist/components/PagePreview.d.ts +8 -1
  8. package/dist/components/PagePreview.js +4 -1
  9. package/dist/components/TopBar/DownloadSection.d.ts +24 -3
  10. package/dist/components/TopBar/DownloadSection.js +45 -7
  11. package/dist/components/TopBar/ShareDropdown.d.ts +4 -2
  12. package/dist/components/TopBar/ShareDropdown.js +2 -2
  13. package/dist/entries/export.d.ts +14 -1
  14. package/dist/entries/export.js +14 -1
  15. package/dist/entries/services.d.ts +1 -1
  16. package/dist/export/fonts/fontFiles.d.ts +100 -0
  17. package/dist/export/fonts/fontFiles.js +333 -0
  18. package/dist/export/fonts/glyphs.d.ts +113 -0
  19. package/dist/export/fonts/glyphs.js +120 -0
  20. package/dist/export/fonts/variation.d.ts +23 -0
  21. package/dist/export/fonts/variation.js +43 -0
  22. package/dist/export/pdf/pdfContent.d.ts +108 -0
  23. package/dist/export/pdf/pdfContent.js +566 -0
  24. package/dist/export/pdf/pdfFontkit.d.ts +54 -0
  25. package/dist/export/pdf/pdfFontkit.js +159 -0
  26. package/dist/export/pdf/pdfGeometry.d.ts +54 -0
  27. package/dist/export/pdf/pdfGeometry.js +228 -0
  28. package/dist/export/pdf/pdfResources.d.ts +109 -0
  29. package/dist/export/pdf/pdfResources.js +314 -0
  30. package/dist/export/pdf/writePdf.d.ts +34 -0
  31. package/dist/export/pdf/writePdf.js +165 -0
  32. package/dist/export/pptx/exportPptxDeck.d.ts +10 -2
  33. package/dist/export/pptx/exportPptxDeck.js +5 -2
  34. package/dist/export/pptx/svgPicture.d.ts +19 -0
  35. package/dist/export/pptx/svgPicture.js +64 -0
  36. package/dist/export/pptx/vectorizeElement.d.ts +9 -0
  37. package/dist/export/pptx/vectorizeElement.js +118 -0
  38. package/dist/export/pptx/writePptx.d.ts +13 -0
  39. package/dist/export/pptx/writePptx.js +17 -0
  40. package/dist/export/printPdf.d.ts +1 -0
  41. package/dist/export/printPdf.js +1 -1
  42. package/dist/export/scene/browserDeps.d.ts +32 -0
  43. package/dist/export/scene/browserDeps.js +174 -0
  44. package/dist/export/scene/imageBytes.d.ts +79 -0
  45. package/dist/export/scene/imageBytes.js +330 -0
  46. package/dist/export/scene/itemBounds.d.ts +9 -0
  47. package/dist/export/scene/itemBounds.js +40 -0
  48. package/dist/export/scene/matrix.d.ts +13 -0
  49. package/dist/export/scene/matrix.js +32 -0
  50. package/dist/export/scene/rasterLimit.d.ts +13 -0
  51. package/dist/export/scene/rasterLimit.js +24 -0
  52. package/dist/export/scene/rebaseOps.d.ts +8 -0
  53. package/dist/export/scene/rebaseOps.js +51 -0
  54. package/dist/export/scene/recordPageScene.d.ts +28 -0
  55. package/dist/export/scene/recordPageScene.js +224 -0
  56. package/dist/export/scene/recordingCanvas.d.ts +55 -0
  57. package/dist/export/scene/recordingCanvas.js +71 -0
  58. package/dist/export/scene/recordingContext.d.ts +162 -0
  59. package/dist/export/scene/recordingContext.js +1031 -0
  60. package/dist/export/scene/replayScene.d.ts +31 -0
  61. package/dist/export/scene/replayScene.js +213 -0
  62. package/dist/export/scene/resolveScene.d.ts +48 -0
  63. package/dist/export/scene/resolveScene.js +236 -0
  64. package/dist/export/scene/trustedSvgImages.d.ts +3 -0
  65. package/dist/export/scene/trustedSvgImages.js +21 -0
  66. package/dist/export/scene/types.d.ts +241 -0
  67. package/dist/export/scene/types.js +38 -0
  68. package/dist/export/slidesPdf.d.ts +10 -2
  69. package/dist/export/slidesPdf.js +8 -4
  70. package/dist/export/svg/__fixtures__/barChart.svg +1 -0
  71. package/dist/export/svg/__golden__/fixture-editable.svg +87 -0
  72. package/dist/export/svg/__golden__/fixture-outlines.svg +88 -0
  73. package/dist/export/svg/inlineSvg.d.ts +10 -0
  74. package/dist/export/svg/inlineSvg.js +119 -0
  75. package/dist/export/svg/scopeCss.d.ts +45 -0
  76. package/dist/export/svg/scopeCss.js +214 -0
  77. package/dist/export/svg/svgColor.d.ts +8 -0
  78. package/dist/export/svg/svgColor.js +46 -0
  79. package/dist/export/svg/svgImageScene.d.ts +41 -0
  80. package/dist/export/svg/svgImageScene.js +128 -0
  81. package/dist/export/svg/svgPathData.d.ts +31 -0
  82. package/dist/export/svg/svgPathData.js +466 -0
  83. package/dist/export/svg/svgToScene.d.ts +19 -0
  84. package/dist/export/svg/svgToScene.js +1183 -0
  85. package/dist/export/svg/writeSvg.d.ts +33 -0
  86. package/dist/export/svg/writeSvg.js +594 -0
  87. package/dist/export/svg/xml.d.ts +23 -0
  88. package/dist/export/svg/xml.js +49 -0
  89. package/dist/export/vectorDeps.d.ts +27 -0
  90. package/dist/export/vectorDeps.js +37 -0
  91. package/dist/export/vectorExport.d.ts +85 -0
  92. package/dist/export/vectorExport.js +287 -0
  93. package/dist/hostServices.d.ts +15 -0
  94. package/dist/i18n/de.d.ts +26 -9
  95. package/dist/i18n/de.js +26 -10
  96. package/dist/i18n/en.js +24 -0
  97. package/dist/primitives/AssetPrimitive.js +7 -2
  98. package/dist/primitives/CanvasIcons.js +6 -0
  99. package/dist/primitives/CanvasStage.js +2 -2
  100. package/dist/primitives/ChartPrimitive.d.ts +11 -1
  101. package/dist/primitives/ChartPrimitive.js +73 -19
  102. package/dist/primitives/IconPrimitive.js +3 -0
  103. package/dist/primitives/IllustrationPrimitive.js +14 -1
  104. package/dist/primitives/chartFont.d.ts +30 -0
  105. package/dist/primitives/chartFont.js +127 -0
  106. package/dist/sidebar/sections/ImageAdjustSection.js +5 -4
  107. package/dist/sidebar/sections/assets/ElementsPanel.js +4 -2
  108. package/dist/sidebar/sections/assets/assetAudience.d.ts +12 -0
  109. package/dist/sidebar/sections/assets/assetAudience.js +18 -0
  110. package/dist/sidebar/sections/assets/useElementCatalog.js +3 -2
  111. package/dist/styles/compiled.css +1 -1
  112. package/dist/utils/canvasAssets.d.ts +11 -1
  113. package/dist/utils/canvasAssets.js +8 -0
  114. package/dist/utils/canvasIcons.js +5 -3
  115. package/dist/utils/captureStage.d.ts +19 -0
  116. package/dist/utils/captureStage.js +14 -1
  117. package/dist/utils/externalIcons.d.ts +7 -0
  118. package/dist/utils/externalIcons.js +70 -3
  119. package/dist/utils/illustrations/svgCache.js +6 -1
  120. package/dist/utils/imageFilters.d.ts +13 -0
  121. package/dist/utils/imageFilters.js +11 -0
  122. package/dist/utils/zipPageImages.d.ts +8 -0
  123. package/dist/utils/zipPageImages.js +16 -4
  124. package/package.json +6 -2
  125. package/skills/ggraphic-embed/SKILL.md +33 -1
@@ -0,0 +1,224 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ /**
3
+ * Records a mounted page stage as a scene (`types.ts`): one item per canvas element, in render
4
+ * order, each a flat list of ops in page px. Konva draws the elements; the recording canvas
5
+ * (`recordingCanvas.ts`) turns what it draws into ops.
6
+ *
7
+ * Page px are the format's px: the stage is shown at a display scale, and a page whose layout is
8
+ * drawn in a smaller logical size sits under a scaled group (`PAGE_ROOT_NAME`). Elements are drawn
9
+ * relative to their layer, which leaves out the display scale of the stage and keeps the group's.
10
+ */
11
+ import Konva from 'konva';
12
+ import { CANVAS_ITEM_ID_ATTR, CANVAS_ITEM_NAME } from '../../utils/deepSelect.js';
13
+ import { PAGE_ROOT_NAME, withSelectionChromeHidden } from '../../utils/captureStage.js';
14
+ import { IDENTITY } from './matrix.js';
15
+ import { RecordingSceneCanvas } from './recordingCanvas.js';
16
+ import { emptyReport, } from './types.js';
17
+ export function recordPageScene(stage, options) {
18
+ const measure = Konva.Util.createCanvasElement().getContext('2d');
19
+ if (!measure)
20
+ throw new Error('no 2D canvas context to measure text with');
21
+ const report = emptyReport();
22
+ const items = [];
23
+ const width = options.size?.width ?? roundTo3(stage.width() / stage.scaleX());
24
+ const height = options.size?.height ?? roundTo3(stage.height() / stage.scaleY());
25
+ const pixelRatio = 1 / (Math.abs(stage.scaleX()) || 1);
26
+ // `CanvasStage` hides the same nodes for a transparent PNG.
27
+ const backgrounds = options.transparent
28
+ ? stage.find('.canvas-background').filter((node) => node.visible())
29
+ : [];
30
+ backgrounds.forEach((node) => node.hide());
31
+ try {
32
+ withSelectionChromeHidden(stage, () => {
33
+ for (const layer of stage.getLayers()) {
34
+ const page = {
35
+ layer,
36
+ page: options.page,
37
+ width,
38
+ height,
39
+ pixelRatio,
40
+ measure,
41
+ report,
42
+ labels: options.labels,
43
+ };
44
+ for (const node of pageNodes(layer)) {
45
+ const item = recordItem(node, page);
46
+ if (item)
47
+ items.push(item);
48
+ }
49
+ }
50
+ });
51
+ }
52
+ finally {
53
+ backgrounds.forEach((node) => node.show());
54
+ }
55
+ return { width, height, items, report };
56
+ }
57
+ const roundTo3 = (value) => Math.round(value * 1000) / 1000;
58
+ /** The layer's children in draw order, with the page-root group replaced by its own children. */
59
+ function* pageNodes(layer) {
60
+ for (const child of layer.getChildren()) {
61
+ if (child instanceof Konva.Group && child.hasName(PAGE_ROOT_NAME))
62
+ yield* child.getChildren();
63
+ else
64
+ yield child;
65
+ }
66
+ }
67
+ function recordItem(node, page) {
68
+ if (!node.isVisible())
69
+ return null;
70
+ const id = node.hasName(CANVAS_ITEM_NAME) ? node.getAttr(CANVAS_ITEM_ID_ATTR) : null;
71
+ const elementId = typeof id === 'string' ? id : null;
72
+ const ops = recordOps(node, page, elementId);
73
+ if (ops.length === 0)
74
+ return null;
75
+ return { elementId, label: (elementId && page.labels?.get(elementId)) || null, ops };
76
+ }
77
+ function recordOps(node, page, elementId) {
78
+ const island = (reason) => {
79
+ page.report.rasterIslands.push({ page: page.page, elementId, reason });
80
+ return [rasterIsland(node.getClientRect({ relativeTo: page.layer }), reason, node)];
81
+ };
82
+ const cached = cachedNodes(node);
83
+ // A filter works on pixels; there is nothing to record from.
84
+ if (cached.some((cachedNode) => (cachedNode.filters() ?? []).length > 0))
85
+ return island('filter');
86
+ const unknown = new Set();
87
+ const canvas = new RecordingSceneCanvas({
88
+ width: page.width,
89
+ height: page.height,
90
+ pixelRatio: page.pixelRatio,
91
+ page: page.page,
92
+ measure: page.measure,
93
+ onUnknown: (call) => unknown.add(call),
94
+ });
95
+ // Konva takes an element's opacity from all of its ancestors; the layer above the page is not
96
+ // part of the page.
97
+ canvas.getContext()._opacityRoot = page.layer;
98
+ drawFromSource(cached, canvas.recorder, () => node.drawScene(canvas, page.layer));
99
+ if (unknown.size === 0)
100
+ return canvas.collect();
101
+ for (const call of unknown) {
102
+ page.report.unknownCalls.push({ page: page.page, elementId, call });
103
+ }
104
+ return island('unsupported-call');
105
+ }
106
+ function rasterIsland(box, reason, node) {
107
+ return {
108
+ kind: 'raster',
109
+ matrix: IDENTITY,
110
+ alpha: 1,
111
+ clips: [],
112
+ shadow: null,
113
+ // `resolveScene` renders the PNG; here only where it goes and why it is needed. The node is
114
+ // kept for an island whose item has no element id, which cannot be found on the stage by id.
115
+ png: null,
116
+ source: null,
117
+ nodeRef: node,
118
+ reason,
119
+ ...box,
120
+ };
121
+ }
122
+ // ---------------------------------------------------------------------------------------------
123
+ // Drawing from the source
124
+ /** The node and every descendant that holds a `cache()` canvas. */
125
+ function cachedNodes(node) {
126
+ const below = node instanceof Konva.Container ? node.find((n) => n.isCached()) : [];
127
+ return [node, ...below].filter((n) => n.isCached());
128
+ }
129
+ /**
130
+ * Runs `draw` with every cached node drawing its children, not its cache canvas (a cached node
131
+ * would be recorded as one bitmap `drawImage`), and with gradients and patterns built on the
132
+ * recorder; then puts both back.
133
+ *
134
+ * A cache is detached from its node and the same object is handed back, not cleared and rebuilt:
135
+ * `cache()` would rasterise the node again, and the hit canvas, the region Konva answers pointer
136
+ * events from, would be lost. `applyPixelHitArea` cuts that canvas to the visible pixels
137
+ * (`drawHitFromCache`) once, when the image loads, and nothing repeats it, so a rebuilt cache
138
+ * would make an image's transparent corners clickable.
139
+ *
140
+ * Putting back is plain assignment, so it cannot fail half-way and hide the error `draw` threw.
141
+ */
142
+ function drawFromSource(cached, recorder, draw) {
143
+ const detached = cached.map((node) => ({ node, cache: node._getCanvasCache() }));
144
+ detached.forEach(({ node }) => {
145
+ node._canvasCache = null;
146
+ });
147
+ const restorePaints = recordPaintsOn(recorder);
148
+ try {
149
+ draw();
150
+ }
151
+ finally {
152
+ restorePaints();
153
+ detached.forEach(({ node, cache }) => {
154
+ node._canvasCache = cache;
155
+ });
156
+ }
157
+ }
158
+ /**
159
+ * Konva builds the gradients and patterns of a shape's own attributes (`fillLinearGradient…`,
160
+ * `fillPatternImage`) on a private native context, not on the context it draws to, so the
161
+ * recorder would only ever see opaque native objects. For the duration of a recording the three
162
+ * builders make them on `recorder` instead. These are the memoising entry points
163
+ * (`_getLinearGradient`, not `__getLinearGradient`): the memo may already hold the native object
164
+ * from an earlier draw, and must not be filled with a recorded one the editor would then draw
165
+ * with. Stroke gradients are built on the drawing context, so they already come out recorded.
166
+ *
167
+ * Recording is synchronous; nothing else draws while the prototype is patched. Returns the
168
+ * function that puts it back.
169
+ */
170
+ function recordPaintsOn(recorder) {
171
+ const proto = Konva.Shape.prototype;
172
+ const saved = {
173
+ pattern: proto._getFillPattern,
174
+ linear: proto._getLinearGradient,
175
+ radial: proto._getRadialGradient,
176
+ };
177
+ const addStops = (gradient, stops) => {
178
+ for (let n = 0; n < stops.length; n += 2) {
179
+ gradient.addColorStop(stops[n], stops[n + 1]);
180
+ }
181
+ };
182
+ proto._getFillPattern = function () {
183
+ const image = this.fillPatternImage();
184
+ if (!image)
185
+ return undefined;
186
+ const pattern = recorder.createPattern(image, this.fillPatternRepeat() || 'repeat');
187
+ if (pattern) {
188
+ // The same transform Konva sets on its own pattern (Shape.js, `__getFillPattern`).
189
+ const transform = new Konva.Transform();
190
+ transform.translate(this.fillPatternX(), this.fillPatternY());
191
+ transform.rotate(Konva.getAngle(this.fillPatternRotation()));
192
+ transform.scale(this.fillPatternScaleX(), this.fillPatternScaleY());
193
+ transform.translate(-1 * this.fillPatternOffsetX(), -1 * this.fillPatternOffsetY());
194
+ const [a, b, c, d, e, f] = transform.getMatrix();
195
+ pattern.setTransform({ a, b, c, d, e, f });
196
+ }
197
+ return pattern;
198
+ };
199
+ proto._getLinearGradient = function () {
200
+ const stops = this.fillLinearGradientColorStops();
201
+ if (!stops)
202
+ return undefined;
203
+ const start = this.fillLinearGradientStartPoint();
204
+ const end = this.fillLinearGradientEndPoint();
205
+ const gradient = recorder.createLinearGradient(start.x, start.y, end.x, end.y);
206
+ addStops(gradient, stops);
207
+ return gradient;
208
+ };
209
+ proto._getRadialGradient = function () {
210
+ const stops = this.fillRadialGradientColorStops();
211
+ if (!stops)
212
+ return undefined;
213
+ const start = this.fillRadialGradientStartPoint();
214
+ const end = this.fillRadialGradientEndPoint();
215
+ const gradient = recorder.createRadialGradient(start.x, start.y, this.fillRadialGradientStartRadius(), end.x, end.y, this.fillRadialGradientEndRadius());
216
+ addStops(gradient, stops);
217
+ return gradient;
218
+ };
219
+ return () => {
220
+ proto._getFillPattern = saved.pattern;
221
+ proto._getLinearGradient = saved.linear;
222
+ proto._getRadialGradient = saved.radial;
223
+ };
224
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * The canvas Konva draws a page element into while it is recorded for export. Its 2D context is a
3
+ * `RecordingContext`, so what Konva draws becomes scene ops instead of pixels.
4
+ *
5
+ * Konva's one non-trivial use of a second canvas is the buffer: a shape or isolated group that
6
+ * needs compositing (opacity over fill and stroke, a shadow under both, `isolated: true`) is drawn
7
+ * into a nested canvas first, and that canvas is then copied onto the parent with the opacity,
8
+ * shadow and clip of the parent. `_prepareIsolationCanvas` hands out that nested canvas; here it
9
+ * is another recording canvas, and the parent's `drawImage` of it becomes a `group` op.
10
+ */
11
+ import { SceneCanvas } from 'konva/lib/Canvas.js';
12
+ import { RecordingContext } from './recordingContext.js';
13
+ import type { SceneOp } from './types.js';
14
+ export interface RecordingSceneCanvasOptions {
15
+ /** The page size in px. Konva only buffers on a canvas that is not empty, so it must be > 0. */
16
+ width: number;
17
+ height: number;
18
+ page: number;
19
+ /**
20
+ * The canvas pixel ratio Konva sizes device-px quantities with: a shadow's blur and offsets, and
21
+ * the width of a stroke that does not scale (`strokeScaleEnabled: false`), are the node's own
22
+ * numbers times its absolute scale times this. An export draws the stage at 1 / display scale,
23
+ * so that the display scale cancels; so must this, or these quantities come out in display px.
24
+ * Default 1.
25
+ */
26
+ pixelRatio?: number;
27
+ /** Real 2D context used only for `measureText`. */
28
+ measure: CanvasRenderingContext2D;
29
+ /** Called for a context call the recorder does not model; the caller makes the element a raster island. */
30
+ onUnknown: (call: string) => void;
31
+ }
32
+ export declare class RecordingSceneCanvas extends SceneCanvas {
33
+ readonly recorder: RecordingContext;
34
+ private readonly options;
35
+ /** The nested canvases handed out for buffered draws, by their canvas element. */
36
+ private readonly nested;
37
+ constructor(options: RecordingSceneCanvasOptions);
38
+ /**
39
+ * A fresh recording canvas for one buffered draw. Konva makes its own buffer cover the painted
40
+ * bounds and shift the origin there; this one keeps the parent's origin and size, so the ops it
41
+ * records are already in the parent's space (page px) and the group needs no extra translate.
42
+ */
43
+ _prepareIsolationCanvas(): SceneCanvas;
44
+ /**
45
+ * Nothing to release: every buffered draw has its own canvas, and `collect` still needs them.
46
+ * Konva calls this from `setSize` (the constructor) and from the error paths.
47
+ */
48
+ _releaseIsolationCanvas(): void;
49
+ /**
50
+ * The recorded ops. A `drawImage` of one of this canvas's nested canvases becomes a `group` op
51
+ * with what that canvas recorded; its alpha, clip and shadow are the ones in force when Konva
52
+ * copied it. A buffer that drew nothing leaves no op.
53
+ */
54
+ collect(): SceneOp[];
55
+ }
@@ -0,0 +1,71 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ /**
3
+ * The canvas Konva draws a page element into while it is recorded for export. Its 2D context is a
4
+ * `RecordingContext`, so what Konva draws becomes scene ops instead of pixels.
5
+ *
6
+ * Konva's one non-trivial use of a second canvas is the buffer: a shape or isolated group that
7
+ * needs compositing (opacity over fill and stroke, a shadow under both, `isolated: true`) is drawn
8
+ * into a nested canvas first, and that canvas is then copied onto the parent with the opacity,
9
+ * shadow and clip of the parent. `_prepareIsolationCanvas` hands out that nested canvas; here it
10
+ * is another recording canvas, and the parent's `drawImage` of it becomes a `group` op.
11
+ */
12
+ import { SceneCanvas } from 'konva/lib/Canvas.js';
13
+ import { RecordingContext } from './recordingContext.js';
14
+ export class RecordingSceneCanvas extends SceneCanvas {
15
+ recorder;
16
+ options;
17
+ /** The nested canvases handed out for buffered draws, by their canvas element. */
18
+ nested = new Map();
19
+ constructor(options) {
20
+ // `pixelRatio: 1` keeps the context's transform and every `drawImage` argument in page px. The
21
+ // bitmap stays 1 px: nothing is ever drawn into it, and one page-sized bitmap per element is
22
+ // tens of MB on a print format. Konva reads the size from `width` and `height`, set below.
23
+ super({ width: 1, height: 1, pixelRatio: 1 });
24
+ this.width = options.width;
25
+ this.height = options.height;
26
+ // A plain field in Konva; `setPixelRatio` would resize the bitmap.
27
+ this.pixelRatio = options.pixelRatio ?? 1;
28
+ this.options = options;
29
+ this.recorder = new RecordingContext(options);
30
+ // The one place the recorder meets Konva: `SceneContext` forwards every call to `_context`,
31
+ // which is typed as the DOM context. The recorder implements the members Konva calls on it.
32
+ this.getContext()._context = this.recorder;
33
+ }
34
+ /**
35
+ * A fresh recording canvas for one buffered draw. Konva makes its own buffer cover the painted
36
+ * bounds and shift the origin there; this one keeps the parent's origin and size, so the ops it
37
+ * records are already in the parent's space (page px) and the group needs no extra translate.
38
+ */
39
+ _prepareIsolationCanvas() {
40
+ const surface = new RecordingSceneCanvas(this.options);
41
+ surface._isolationRect = { x: 0, y: 0, width: this.width, height: this.height };
42
+ const { a, b, c, d, e, f } = this.recorder.getTransform();
43
+ surface.recorder.setTransform(a, b, c, d, e, f);
44
+ this.nested.set(surface._canvas, surface);
45
+ return surface;
46
+ }
47
+ /**
48
+ * Nothing to release: every buffered draw has its own canvas, and `collect` still needs them.
49
+ * Konva calls this from `setSize` (the constructor) and from the error paths.
50
+ */
51
+ _releaseIsolationCanvas() { }
52
+ /**
53
+ * The recorded ops. A `drawImage` of one of this canvas's nested canvases becomes a `group` op
54
+ * with what that canvas recorded; its alpha, clip and shadow are the ones in force when Konva
55
+ * copied it. A buffer that drew nothing leaves no op.
56
+ */
57
+ collect() {
58
+ return this.recorder.ops.flatMap((op) => {
59
+ if (op.kind !== 'image' || op.image.kind !== 'element')
60
+ return [op];
61
+ const surface = this.nested.get(op.image.element);
62
+ if (!surface)
63
+ return [op];
64
+ const ops = surface.collect();
65
+ if (ops.length === 0)
66
+ return [];
67
+ const { matrix, alpha, clips, shadow } = op;
68
+ return [{ kind: 'group', matrix, alpha, clips, shadow, ops }];
69
+ });
70
+ }
71
+ }
@@ -0,0 +1,162 @@
1
+ import type { PathCommand, SceneOp } from './types.js';
2
+ export interface RecordingContextOptions {
3
+ /** Real 2D context used only for measureText (same fonts as the canvas). */
4
+ measure: CanvasRenderingContext2D;
5
+ page: number;
6
+ /** Size of the canvas this context stands for; returned by the `canvas` getter. */
7
+ width: number;
8
+ height: number;
9
+ /** Called for a context call the recorder does not model; the caller turns the element into a raster island. */
10
+ onUnknown: (call: string) => void;
11
+ }
12
+ type CubicCommand = Extract<PathCommand, {
13
+ op: 'C';
14
+ }>;
15
+ /**
16
+ * Approximates an elliptical arc by cubic beziers.
17
+ *
18
+ * The ellipse is centred on `(cx, cy)` with radii `rx`, `ry`, rotated by `rotation` (radians).
19
+ * `startAngle` is the parametric angle of the start and `sweep` the signed angle to travel, in
20
+ * the canvas convention: y points down, so a positive sweep runs clockwise on screen.
21
+ *
22
+ * Returns the start point and the cubics from it. The caller emits the `M`/`L` to `start`
23
+ * first, because whether it is a move or a line depends on the path it extends.
24
+ */
25
+ export declare function arcToBeziers(cx: number, cy: number, rx: number, ry: number, rotation: number, startAngle: number, sweep: number): {
26
+ start: {
27
+ x: number;
28
+ y: number;
29
+ };
30
+ curves: CubicCommand[];
31
+ };
32
+ export interface Rgba {
33
+ hex: string;
34
+ alpha: number;
35
+ }
36
+ /** `#rgb[a]`, `#rrggbb[aa]`, `rgb[a](...)` and `transparent`; anything else is `null`. */
37
+ export declare function parseCssColor(value: string): Rgba | null;
38
+ /** The families of a CSS `font-family` list, in order, unquoted. */
39
+ export declare function fontFamilies(list: string): string[];
40
+ export declare class RecordingContext {
41
+ readonly ops: SceneOp[];
42
+ readonly page: number;
43
+ private readonly measure;
44
+ private readonly size;
45
+ private readonly onUnknown;
46
+ private readonly colors;
47
+ private readonly stack;
48
+ private s;
49
+ private segs;
50
+ /** Current point in device space; the user-space point is `inverse(CTM)` of it. */
51
+ private cur;
52
+ private subpathStart;
53
+ constructor(options: RecordingContextOptions);
54
+ save(): void;
55
+ restore(): void;
56
+ get canvas(): HTMLCanvasElement;
57
+ get fillStyle(): string | CanvasGradient | CanvasPattern;
58
+ set fillStyle(value: string | CanvasGradient | CanvasPattern);
59
+ get strokeStyle(): string | CanvasGradient | CanvasPattern;
60
+ set strokeStyle(value: string | CanvasGradient | CanvasPattern);
61
+ get lineWidth(): number;
62
+ set lineWidth(value: number);
63
+ get lineCap(): CanvasLineCap;
64
+ set lineCap(value: CanvasLineCap);
65
+ get lineJoin(): CanvasLineJoin;
66
+ set lineJoin(value: CanvasLineJoin);
67
+ get miterLimit(): number;
68
+ set miterLimit(value: number);
69
+ get lineDashOffset(): number;
70
+ set lineDashOffset(value: number);
71
+ setLineDash(segments: Iterable<number>): void;
72
+ getLineDash(): number[];
73
+ get globalAlpha(): number;
74
+ set globalAlpha(value: number);
75
+ get globalCompositeOperation(): GlobalCompositeOperation;
76
+ set globalCompositeOperation(value: GlobalCompositeOperation);
77
+ get filter(): string;
78
+ set filter(value: string);
79
+ get imageSmoothingEnabled(): boolean;
80
+ set imageSmoothingEnabled(value: boolean);
81
+ get imageSmoothingQuality(): ImageSmoothingQuality;
82
+ set imageSmoothingQuality(value: ImageSmoothingQuality);
83
+ get shadowColor(): string;
84
+ set shadowColor(value: string);
85
+ get shadowBlur(): number;
86
+ set shadowBlur(value: number);
87
+ get shadowOffsetX(): number;
88
+ set shadowOffsetX(value: number);
89
+ get shadowOffsetY(): number;
90
+ set shadowOffsetY(value: number);
91
+ get font(): string;
92
+ set font(value: string);
93
+ get textAlign(): CanvasTextAlign;
94
+ set textAlign(value: CanvasTextAlign);
95
+ get textBaseline(): CanvasTextBaseline;
96
+ set textBaseline(value: CanvasTextBaseline);
97
+ get direction(): CanvasDirection;
98
+ set direction(value: CanvasDirection);
99
+ get letterSpacing(): string;
100
+ set letterSpacing(value: string);
101
+ getTransform(): DOMMatrix;
102
+ setTransform(a: number, b: number, c: number, d: number, e: number, f: number): void;
103
+ resetTransform(): void;
104
+ transform(a: number, b: number, c: number, d: number, e: number, f: number): void;
105
+ translate(x: number, y: number): void;
106
+ scale(x: number, y: number): void;
107
+ rotate(angle: number): void;
108
+ beginPath(): void;
109
+ closePath(): void;
110
+ moveTo(x: number, y: number): void;
111
+ lineTo(x: number, y: number): void;
112
+ bezierCurveTo(x1: number, y1: number, x2: number, y2: number, x: number, y: number): void;
113
+ quadraticCurveTo(x1: number, y1: number, x: number, y: number): void;
114
+ rect(x: number, y: number, width: number, height: number): void;
115
+ arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void;
116
+ ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void;
117
+ arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void;
118
+ roundRect(x: number, y: number, width: number, height: number, radii?: number | DOMPointInit | (number | DOMPointInit)[]): void;
119
+ fill(fillRule?: CanvasFillRule): void;
120
+ fill(path: Path2D, fillRule?: CanvasFillRule): void;
121
+ stroke(path?: Path2D): void;
122
+ clip(fillRule?: CanvasFillRule): void;
123
+ clip(path: Path2D, fillRule?: CanvasFillRule): void;
124
+ fillRect(x: number, y: number, width: number, height: number): void;
125
+ strokeRect(x: number, y: number, width: number, height: number): void;
126
+ clearRect(x: number, y: number, width: number, height: number): void;
127
+ fillText(text: string, x: number, y: number, maxWidth?: number): void;
128
+ strokeText(text: string, x: number, y: number, maxWidth?: number): void;
129
+ measureText(text: string): TextMetrics;
130
+ drawImage(image: CanvasImageSource, dx: number, dy: number): void;
131
+ drawImage(image: CanvasImageSource, dx: number, dy: number, dw: number, dh: number): void;
132
+ drawImage(image: CanvasImageSource, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
133
+ createLinearGradient(x0: number, y0: number, x1: number, y1: number): CanvasGradient;
134
+ createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): CanvasGradient;
135
+ createPattern(image: CanvasImageSource, repetition: string | null): CanvasPattern | null;
136
+ getImageData(_sx: number, _sy: number, sw: number, sh: number): ImageData;
137
+ putImageData(_imageData: ImageData, _dx: number, _dy: number, ..._dirty: number[]): void;
138
+ createImageData(sw: number | ImageData, sh?: number): ImageData;
139
+ isPointInPath(x: number, y: number, fillRule?: CanvasFillRule): boolean;
140
+ isPointInPath(path: Path2D, x: number, y: number, fillRule?: CanvasFillRule): boolean;
141
+ private add;
142
+ private rectSegments;
143
+ /**
144
+ * The path as commands in the user space of the current CTM, or `null` when it draws nothing:
145
+ * no segment beyond moves, or a singular CTM that cannot be inverted.
146
+ */
147
+ private inCallSpace;
148
+ private drawPath;
149
+ private drawText;
150
+ /** What the canvas would apply that the IR cannot express. */
151
+ private effects;
152
+ private base;
153
+ private shadowState;
154
+ private strokeState;
155
+ private paintOf;
156
+ private styleSlot;
157
+ private gradient;
158
+ /** Our own parser first; names, `hsl()` and the like are canonicalised by the measuring context. */
159
+ private parseColor;
160
+ private syncMeasure;
161
+ }
162
+ export {};