@meonode/canvas 1.7.0 → 1.7.2

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 (43) hide show
  1. package/dist/cjs/canvas/image.canvas.util.d.ts +42 -2
  2. package/dist/cjs/canvas/image.canvas.util.d.ts.map +1 -1
  3. package/dist/cjs/canvas/image.canvas.util.js +145 -7
  4. package/dist/cjs/canvas/image.canvas.util.js.map +1 -1
  5. package/dist/cjs/canvas/root.canvas.util.d.ts +2 -0
  6. package/dist/cjs/canvas/root.canvas.util.d.ts.map +1 -1
  7. package/dist/cjs/canvas/root.canvas.util.js +59 -40
  8. package/dist/cjs/canvas/root.canvas.util.js.map +1 -1
  9. package/dist/cjs/index.d.ts +1 -1
  10. package/dist/cjs/index.d.ts.map +1 -1
  11. package/dist/cjs/index.js +1 -0
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/util/disk.cache.d.ts +4 -0
  14. package/dist/cjs/util/disk.cache.d.ts.map +1 -0
  15. package/dist/cjs/util/disk.cache.js +40 -0
  16. package/dist/cjs/util/disk.cache.js.map +1 -0
  17. package/dist/cjs/worker/render.worker.d.ts.map +1 -0
  18. package/dist/cjs/{render.worker.js → worker/render.worker.js} +1 -1
  19. package/dist/cjs/worker/render.worker.js.map +1 -0
  20. package/dist/{esm/canvas → cjs/worker}/worker.types.d.ts.map +1 -1
  21. package/dist/esm/canvas/image.canvas.util.d.ts +42 -2
  22. package/dist/esm/canvas/image.canvas.util.d.ts.map +1 -1
  23. package/dist/esm/canvas/image.canvas.util.js +143 -8
  24. package/dist/esm/canvas/root.canvas.util.d.ts +2 -0
  25. package/dist/esm/canvas/root.canvas.util.d.ts.map +1 -1
  26. package/dist/esm/canvas/root.canvas.util.js +60 -41
  27. package/dist/esm/index.d.ts +1 -1
  28. package/dist/esm/index.d.ts.map +1 -1
  29. package/dist/esm/index.js +1 -1
  30. package/dist/esm/util/disk.cache.d.ts +4 -0
  31. package/dist/esm/util/disk.cache.d.ts.map +1 -0
  32. package/dist/esm/util/disk.cache.js +35 -0
  33. package/dist/esm/worker/render.worker.d.ts.map +1 -0
  34. package/dist/esm/{render.worker.js → worker/render.worker.js} +1 -1
  35. package/dist/{cjs/canvas → esm/worker}/worker.types.d.ts.map +1 -1
  36. package/package.json +1 -1
  37. package/dist/cjs/render.worker.d.ts.map +0 -1
  38. package/dist/cjs/render.worker.js.map +0 -1
  39. package/dist/esm/render.worker.d.ts.map +0 -1
  40. /package/dist/cjs/{render.worker.d.ts → worker/render.worker.d.ts} +0 -0
  41. /package/dist/cjs/{canvas → worker}/worker.types.d.ts +0 -0
  42. /package/dist/esm/{render.worker.d.ts → worker/render.worker.d.ts} +0 -0
  43. /package/dist/esm/{canvas → worker}/worker.types.d.ts +0 -0
@@ -6,6 +6,36 @@ import { BoxNode } from '../canvas/layout.canvas.util.js';
6
6
  * Scoped to a single RootNode.render() call; discarded after rendering.
7
7
  */
8
8
  export type RenderImageCache = Map<string, Promise<CanvasImage>>;
9
+ /**
10
+ * A simple LRU cache for resolved `CanvasImage` objects.
11
+ *
12
+ * - Persists across render passes so repeated renders of the same tree don't
13
+ * re-fetch every image.
14
+ * - Bounded by `maxSize` entries; least-recently-used entries are evicted first.
15
+ * - Call `dispose()` to eagerly release all held images, or rely on the
16
+ * automatic `process.on('exit')` hook that clears the singleton.
17
+ */
18
+ export declare class ImageLRUCache {
19
+ private readonly map;
20
+ readonly maxSize: number;
21
+ constructor(maxSize: number);
22
+ get(key: string): CanvasImage | undefined;
23
+ set(key: string, image: CanvasImage): void;
24
+ has(key: string): boolean;
25
+ get size(): number;
26
+ dispose(): void;
27
+ }
28
+ /**
29
+ * Returns the singleton `ImageLRUCache`, creating it on first access.
30
+ * Registers a one-time process cleanup hook to clear the cache
31
+ * so native image buffers are freed when the process shuts down.
32
+ */
33
+ export declare function getImageCache(maxSize?: number): ImageLRUCache;
34
+ /**
35
+ * Explicitly disposes the global image cache.
36
+ * Useful in tests or when tearing down the rendering engine.
37
+ */
38
+ export declare function disposeImageCache(): void;
9
39
  /**
10
40
  * Renders images with configurable sizing, positioning, and effects.
11
41
  * Supports object-fit modes, positioning, border radius, and saturation filters.
@@ -21,11 +51,21 @@ export declare class ImageNode extends BoxNode {
21
51
  /**
22
52
  * Fetches and processes the image source into a CanvasImage.
23
53
  * Does not touch node state — pure fetch logic.
54
+ *
55
+ * If `diskCacheKey` is provided, the resolved image buffer is written to the
56
+ * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).
24
57
  */
25
58
  private _fetchCanvasImage;
26
59
  /**
27
- * Loads and processes an image, using the render-scoped cache to avoid
28
- * re-fetching the same source within a single render pass.
60
+ * Loads and processes an image.
61
+ *
62
+ * Resolution order:
63
+ * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
64
+ * 2. Disk cache at `.cache/files/<hash>` — survives process restarts.
65
+ * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
66
+ * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.
67
+ *
68
+ * Buffer sources use a SHA-256 hash as their cache key (same as string sources).
29
69
  */
30
70
  private _loadImage;
31
71
  getLoadingPromise(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"image.canvas.util.d.ts","sourceRoot":"","sources":["../../../src/canvas/image.canvas.util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AACnF,OAAO,EAAE,KAAK,wBAAwB,EAAE,KAAK,IAAI,WAAW,EAAa,MAAM,aAAa,CAAA;AAC5F,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAA;AAsBxD;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC,CAAA;AAEhE;;;GAGG;AACH,qBAAa,SAAU,SAAQ,OAAO;IAC5B,KAAK,EAA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AG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,IAAI;IAa1C,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,OAAO,IAAI,IAAI;CAGhB;AAUD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,MAA2B,GAAG,aAAa,CAejF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAGxC;AAED;;;GAGG;AACH,qBAAa,SAAU,SAAQ,OAAO;IAC5B,KAAK,EAAE,UAAU,GAAG,SAAS,CAAA;IACrC,OAAO,CAAC,WAAW,CAA2B;IAC9C,OAAO,CAAC,YAAY,CAAI;IACxB,OAAO,CAAC,aAAa,CAAI;IACzB,OAAO,CAAC,cAAc,CAA6B;gBAEvC,KAAK,EAAE,UAAU;IAYtB,IAAI,CAAC,KAAK,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOpD;;;;;;OAMG;YACW,iBAAiB;IAyE/B;;;;;;;;;;OAUG;IACH,OAAO,CAAC,UAAU;IA2FX,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAIzC;;;OAGG;cACgB,cAAc,CAAC,GAAG,EAAE,wBAAwB,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAoJrH;AAED;;GAEG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,UAAU,KAAG,aAGxC,CAAA"}
@@ -5,6 +5,7 @@ var layout_canvas_util = require('./layout.canvas.util.js');
5
5
  var canvas_helper = require('./canvas.helper.js');
6
6
  var fs = require('fs');
7
7
  var common_const = require('../constant/common.const.js');
8
+ var disk_cache = require('../util/disk.cache.js');
8
9
 
9
10
  /**
10
11
  * Calculates pixel offset for image positioning based on percentage or pixel values.
@@ -22,6 +23,86 @@ function calculateOffsetFromValue(positionValue, availableSpace) {
22
23
  console.warn(`[ImageNode] Invalid objectPosition value format: ${value}. Defaulting to 50%.`);
23
24
  return availableSpace * 0.5;
24
25
  }
26
+ /**
27
+ * A simple LRU cache for resolved `CanvasImage` objects.
28
+ *
29
+ * - Persists across render passes so repeated renders of the same tree don't
30
+ * re-fetch every image.
31
+ * - Bounded by `maxSize` entries; least-recently-used entries are evicted first.
32
+ * - Call `dispose()` to eagerly release all held images, or rely on the
33
+ * automatic `process.on('exit')` hook that clears the singleton.
34
+ */
35
+ class ImageLRUCache {
36
+ map = new Map();
37
+ maxSize;
38
+ constructor(maxSize) {
39
+ this.maxSize = maxSize;
40
+ }
41
+ get(key) {
42
+ const entry = this.map.get(key);
43
+ if (!entry)
44
+ return undefined;
45
+ // Move to end (most-recently used)
46
+ this.map.delete(key);
47
+ this.map.set(key, entry);
48
+ return entry.image;
49
+ }
50
+ set(key, image) {
51
+ // If key already exists, refresh it
52
+ if (this.map.has(key)) {
53
+ this.map.delete(key);
54
+ }
55
+ // Evict oldest if at capacity
56
+ while (this.map.size >= this.maxSize) {
57
+ const oldest = this.map.keys().next().value;
58
+ this.map.delete(oldest);
59
+ }
60
+ this.map.set(key, { image, key });
61
+ }
62
+ has(key) {
63
+ return this.map.has(key);
64
+ }
65
+ get size() {
66
+ return this.map.size;
67
+ }
68
+ dispose() {
69
+ this.map.clear();
70
+ }
71
+ }
72
+ /** Module-level singleton — lazily created on first render. */
73
+ let _globalImageCache = null;
74
+ const DEFAULT_CACHE_SIZE = 128;
75
+ // Symbol key on process to track hook registration across module reloads (e.g. Jest resetModules)
76
+ const HOOK_KEY = Symbol.for('__meonode_canvas_image_cache_hook__');
77
+ /**
78
+ * Returns the singleton `ImageLRUCache`, creating it on first access.
79
+ * Registers a one-time process cleanup hook to clear the cache
80
+ * so native image buffers are freed when the process shuts down.
81
+ */
82
+ function getImageCache(maxSize = DEFAULT_CACHE_SIZE) {
83
+ if (!_globalImageCache) {
84
+ _globalImageCache = new ImageLRUCache(maxSize);
85
+ }
86
+ if (!globalThis[HOOK_KEY]) {
87
+ globalThis[HOOK_KEY] = true;
88
+ const cleanup = () => {
89
+ _globalImageCache?.dispose();
90
+ _globalImageCache = null;
91
+ };
92
+ process.once('exit', cleanup);
93
+ process.once('SIGINT', cleanup);
94
+ process.once('SIGTERM', cleanup);
95
+ }
96
+ return _globalImageCache;
97
+ }
98
+ /**
99
+ * Explicitly disposes the global image cache.
100
+ * Useful in tests or when tearing down the rendering engine.
101
+ */
102
+ function disposeImageCache() {
103
+ _globalImageCache?.dispose();
104
+ _globalImageCache = null;
105
+ }
25
106
  /**
26
107
  * Renders images with configurable sizing, positioning, and effects.
27
108
  * Supports object-fit modes, positioning, border radius, and saturation filters.
@@ -50,8 +131,11 @@ class ImageNode extends layout_canvas_util.BoxNode {
50
131
  /**
51
132
  * Fetches and processes the image source into a CanvasImage.
52
133
  * Does not touch node state — pure fetch logic.
134
+ *
135
+ * If `diskCacheKey` is provided, the resolved image buffer is written to the
136
+ * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).
53
137
  */
54
- async _fetchCanvasImage() {
138
+ async _fetchCanvasImage(diskCacheKey) {
55
139
  const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type');
56
140
  let finalSource = this.props.src;
57
141
  let isSvg = false;
@@ -110,11 +194,24 @@ class ImageNode extends layout_canvas_util.BoxNode {
110
194
  const modifiedSvgString = svgString.replace(/fill="[^"]*"/g, `fill="${this.props.color}"`);
111
195
  finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer;
112
196
  }
197
+ // Write resolved buffer to disk cache (fire-and-forget, non-fatal)
198
+ if (diskCacheKey) {
199
+ const cacheBuffer = Buffer.isBuffer(finalSource) ? finalSource : contentBuffer;
200
+ if (cacheBuffer)
201
+ disk_cache.writeDiskCache(diskCacheKey, cacheBuffer);
202
+ }
113
203
  return skiaCanvas.loadImage(finalSource);
114
204
  }
115
205
  /**
116
- * Loads and processes an image, using the render-scoped cache to avoid
117
- * re-fetching the same source within a single render pass.
206
+ * Loads and processes an image.
207
+ *
208
+ * Resolution order:
209
+ * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.
210
+ * 2. Disk cache at `.cache/files/<hash>` — survives process restarts.
211
+ * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.
212
+ * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.
213
+ *
214
+ * Buffer sources use a SHA-256 hash as their cache key (same as string sources).
118
215
  */
119
216
  _loadImage(cache) {
120
217
  if (!this.props.src) {
@@ -127,18 +224,56 @@ class ImageNode extends layout_canvas_util.BoxNode {
127
224
  return new Promise(resolve => {
128
225
  const load = async () => {
129
226
  try {
227
+ const lru = getImageCache();
228
+ const cacheKey = typeof this.props.src === 'string'
229
+ ? this.props.color
230
+ ? `${this.props.src}|${this.props.color}`
231
+ : this.props.src
232
+ : this.props.color
233
+ ? `${disk_cache.hashBuffer(this.props.src)}|${this.props.color}`
234
+ : disk_cache.hashBuffer(this.props.src);
235
+ // 1. Check persistent LRU cache
236
+ const cached = lru.get(cacheKey);
237
+ if (cached) {
238
+ this.loadedImage = cached;
239
+ this.naturalWidth = cached.width;
240
+ this.naturalHeight = cached.height;
241
+ const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined;
242
+ const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
243
+ this.node.setAspectRatio(finalAspectRatio);
244
+ this.props.onLoad?.();
245
+ resolve();
246
+ return;
247
+ }
248
+ // 2. Check disk cache (persists across process restarts)
249
+ const diskBuffer = await disk_cache.readDiskCache(cacheKey);
250
+ if (diskBuffer) {
251
+ const img = await skiaCanvas.loadImage(diskBuffer);
252
+ lru.set(cacheKey, img);
253
+ this.loadedImage = img;
254
+ this.naturalWidth = img.width;
255
+ this.naturalHeight = img.height;
256
+ const calculatedAspectRatio = img.width > 0 && img.height > 0 ? img.width / img.height : undefined;
257
+ const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio;
258
+ this.node.setAspectRatio(finalAspectRatio);
259
+ this.props.onLoad?.();
260
+ resolve();
261
+ return;
262
+ }
263
+ // 3. Per-render dedup cache or fresh fetch (writes to disk internally)
130
264
  let imagePromise;
131
- if (cache && typeof this.props.src === 'string') {
132
- const cacheKey = this.props.color ? `${this.props.src}|${this.props.color}` : this.props.src;
265
+ if (cache) {
133
266
  if (!cache.has(cacheKey)) {
134
- cache.set(cacheKey, this._fetchCanvasImage());
267
+ cache.set(cacheKey, this._fetchCanvasImage(cacheKey));
135
268
  }
136
269
  imagePromise = cache.get(cacheKey);
137
270
  }
138
271
  else {
139
- imagePromise = this._fetchCanvasImage();
272
+ imagePromise = this._fetchCanvasImage(cacheKey);
140
273
  }
141
274
  const img = await imagePromise;
275
+ // 4. Store in persistent LRU cache
276
+ lru.set(cacheKey, img);
142
277
  this.loadedImage = img;
143
278
  this.naturalWidth = img.width;
144
279
  this.naturalHeight = img.height;
@@ -320,5 +455,8 @@ const Image = (props) => ({
320
455
  });
321
456
 
322
457
  exports.Image = Image;
458
+ exports.ImageLRUCache = ImageLRUCache;
323
459
  exports.ImageNode = ImageNode;
460
+ exports.disposeImageCache = disposeImageCache;
461
+ exports.getImageCache = getImageCache;
324
462
  //# sourceMappingURL=image.canvas.util.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"image.canvas.util.js","sources":["../../../../src/canvas/image.canvas.util.ts"],"sourcesContent":["import type { BaseProps, ImageProps, CanvasElement } from '@/canvas/canvas.type.js'\nimport { type CanvasRenderingContext2D, Image as CanvasImage, loadImage } from 'skia-canvas'\nimport { BoxNode } from '@/canvas/layout.canvas.util.js'\nimport { drawRoundedRectPath, parseBorderRadius } from '@/canvas/canvas.helper.js'\nimport { promises as fs } from 'fs'\nimport { Style } from '@/constant/common.const.js'\n\n/**\n * Calculates pixel offset for image positioning based on percentage or pixel values.\n * This handles centering, edge alignment, and percentage-based positioning.\n */\nfunction calculateOffsetFromValue(positionValue: number | `${number}%` | undefined, availableSpace: number): number {\n const value = positionValue ?? '50%'\n if (typeof value === 'number') {\n return value\n }\n if (typeof value === 'string' && value.endsWith('%')) {\n const percentage = parseFloat(value) / 100\n return availableSpace * percentage\n }\n console.warn(`[ImageNode] Invalid objectPosition value format: ${value}. Defaulting to 50%.`)\n return availableSpace * 0.5\n}\n\n/**\n * Per-render image cache — keyed by `src|color` for string sources.\n * Scoped to a single RootNode.render() call; discarded after rendering.\n */\nexport type RenderImageCache = Map<string, Promise<CanvasImage>>\n\n/**\n * Renders images with configurable sizing, positioning, and effects.\n * Supports object-fit modes, positioning, border radius, and saturation filters.\n */\nexport class ImageNode extends BoxNode {\n declare props: ImageProps & BaseProps\n private loadedImage: CanvasImage | null = null\n private naturalWidth = 0\n private naturalHeight = 0\n private loadingPromise: Promise<void> | null = null\n\n constructor(props: ImageProps) {\n super({ name: 'Image', ...props, children: undefined })\n\n this.props = {\n objectFit: 'fill',\n overflow: Style.Overflow.Hidden,\n saturate: 1,\n objectPosition: { Left: '50%', Top: '50%' },\n ...props,\n }\n }\n\n public load(cache?: RenderImageCache): Promise<void> {\n if (!this.loadingPromise) {\n this.loadingPromise = this._loadImage(cache)\n }\n return this.loadingPromise\n }\n\n /**\n * Fetches and processes the image source into a CanvasImage.\n * Does not touch node state — pure fetch logic.\n */\n private async _fetchCanvasImage(): Promise<CanvasImage> {\n const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type')\n let finalSource: string | Buffer = this.props.src\n let isSvg = false\n let contentBuffer: Buffer | null = null\n let detectedMime: string | undefined\n\n if (typeof this.props.src === 'string') {\n if (this.props.src.startsWith('http')) {\n const response = await fetch(this.props.src)\n if (!response.ok) {\n throw new Error(`HTTP error ${response.status} fetching image: ${this.props.src}`)\n }\n const imageArrayBuffer = await response.arrayBuffer()\n contentBuffer = Buffer.from(imageArrayBuffer)\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && contentBuffer.toString('utf-8').includes('<svg')) {\n isSvg = true\n }\n } else {\n finalSource = this.props.src\n const filePath = this.props.src\n\n try {\n const fileTypeResult = await fileTypeFromFile(filePath)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && filePath.toLowerCase().endsWith('.svg')) {\n isSvg = true\n }\n } catch {\n isSvg = filePath.toLowerCase().endsWith('.svg')\n }\n\n if (isSvg && this.props.color) {\n try {\n contentBuffer = await fs.readFile(filePath)\n } catch {\n isSvg = false\n contentBuffer = null\n }\n }\n }\n } else {\n contentBuffer = this.props.src\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n }\n\n if (isSvg && this.props.color && contentBuffer) {\n const svgString = contentBuffer.toString('utf-8')\n const modifiedSvgString = svgString.replace(/fill=\"[^\"]*\"/g, `fill=\"${this.props.color}\"`)\n finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer\n }\n\n return loadImage(finalSource as never)\n }\n\n /**\n * Loads and processes an image, using the render-scoped cache to avoid\n * re-fetching the same source within a single render pass.\n */\n private _loadImage(cache?: RenderImageCache): Promise<void> {\n if (!this.props.src) {\n const aspectRatioFromProps = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(aspectRatioFromProps)\n this.naturalWidth = 0\n this.naturalHeight = 0\n return Promise.resolve()\n }\n\n return new Promise(resolve => {\n const load = async () => {\n try {\n let imagePromise: Promise<CanvasImage>\n\n if (cache && typeof this.props.src === 'string') {\n const cacheKey = this.props.color ? `${this.props.src}|${this.props.color}` : this.props.src\n if (!cache.has(cacheKey)) {\n cache.set(cacheKey, this._fetchCanvasImage())\n }\n imagePromise = cache.get(cacheKey)!\n } else {\n imagePromise = this._fetchCanvasImage()\n }\n\n const img = await imagePromise\n this.loadedImage = img\n this.naturalWidth = img.width\n this.naturalHeight = img.height\n\n const calculatedAspectRatio = this.naturalWidth > 0 && this.naturalHeight > 0 ? this.naturalWidth / this.naturalHeight : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n } catch (error: any) {\n this.naturalWidth = 0\n this.naturalHeight = 0\n const finalAspectRatioOnError = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(finalAspectRatioOnError)\n this.props.onError?.(error)\n resolve()\n }\n }\n load()\n })\n }\n\n public getLoadingPromise(): Promise<void> {\n return this.loadingPromise ?? this.load()\n }\n\n /**\n * Renders the image with correct sizing, clipping, and positioning.\n * Handles object-fit, object-position, and visual effects like saturation.\n */\n protected override _renderContent(ctx: CanvasRenderingContext2D, x: number, y: number, width: number, height: number) {\n super._renderContent(ctx, x, y, width, height)\n\n if (!this.loadedImage || width <= 0 || height <= 0) return\n const img = this.loadedImage\n const imgW = this.naturalWidth\n const imgH = this.naturalHeight\n if (imgW <= 0 || imgH <= 0) return\n\n // Calculate content box accounting for padding and borders\n const paddingLeft = this.node.getComputedPadding(Style.Edge.Left)\n const paddingTop = this.node.getComputedPadding(Style.Edge.Top)\n const paddingRight = this.node.getComputedPadding(Style.Edge.Right)\n const paddingBottom = this.node.getComputedPadding(Style.Edge.Bottom)\n const borderLeft = this.node.getComputedBorder(Style.Edge.Left)\n const borderTop = this.node.getComputedBorder(Style.Edge.Top)\n const borderRight = this.node.getComputedBorder(Style.Edge.Right)\n const borderBottom = this.node.getComputedBorder(Style.Edge.Bottom)\n const contentX = x + borderLeft + paddingLeft\n const contentY = y + borderTop + paddingTop\n const contentWidth = Math.max(0, width - borderLeft - paddingLeft - borderRight - paddingRight)\n const contentHeight = Math.max(0, height - borderTop - paddingTop - borderBottom - paddingBottom)\n\n if (contentWidth <= 0 || contentHeight <= 0) return\n\n // Apply clipping for border radius\n ctx.save()\n const outerRadii = parseBorderRadius(this.props.borderRadius)\n const innerBorderRadii = {\n TopLeft: Math.max(0, outerRadii.TopLeft - borderTop),\n TopRight: Math.max(0, outerRadii.TopRight - borderTop),\n BottomRight: Math.max(0, outerRadii.BottomRight - borderBottom),\n BottomLeft: Math.max(0, outerRadii.BottomLeft - borderBottom),\n }\n const contentRadii = {\n TopLeft: Math.max(0, innerBorderRadii.TopLeft - Math.max(paddingLeft, paddingTop)),\n TopRight: Math.max(0, innerBorderRadii.TopRight - Math.max(paddingRight, paddingTop)),\n BottomRight: Math.max(0, innerBorderRadii.BottomRight - Math.max(paddingRight, paddingBottom)),\n BottomLeft: Math.max(0, innerBorderRadii.BottomLeft - Math.max(paddingLeft, paddingBottom)),\n }\n drawRoundedRectPath(ctx, contentX, contentY, contentWidth, contentHeight, contentRadii)\n ctx.clip()\n\n // Calculate image dimensions based on object-fit\n const nodeRatio = contentWidth / contentHeight\n const imgRatio = imgW / imgH\n const objectFit = this.props.objectFit\n let dw = contentWidth\n let dh = contentHeight\n\n if (objectFit === 'contain') {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n } else if (objectFit === 'cover') {\n if (imgRatio > nodeRatio) {\n dh = contentHeight\n dw = contentHeight * imgRatio\n } else {\n dw = contentWidth\n dh = contentWidth / imgRatio\n }\n } else if (objectFit === 'none') {\n dw = imgW\n dh = imgH\n } else if (objectFit === 'scale-down') {\n if (imgW <= contentWidth && imgH <= contentHeight) {\n dw = imgW\n dh = imgH\n } else {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n }\n }\n\n // Calculate image position based on object-position\n const sx = 0\n const sy = 0\n const sw = imgW\n const sh = imgH\n\n const availableWidth = contentWidth - dw\n const availableHeight = contentHeight - dh\n const posProps = this.props.objectPosition || {}\n const horizontalValue = posProps.Left !== undefined ? posProps.Left : posProps.Right !== undefined ? posProps.Right : '50%'\n const verticalValue = posProps.Top !== undefined ? posProps.Top : posProps.Bottom !== undefined ? posProps.Bottom : '50%'\n\n let offsetX = calculateOffsetFromValue(horizontalValue, availableWidth)\n let offsetY = calculateOffsetFromValue(verticalValue, availableHeight)\n\n if (posProps.Left === undefined && posProps.Right !== undefined) {\n offsetX = availableWidth - offsetX\n }\n if (posProps.Top === undefined && posProps.Bottom !== undefined) {\n offsetY = availableHeight - offsetY\n }\n\n const dx = contentX + offsetX\n const dy = contentY + offsetY\n\n // Draw image with filters\n ctx.save()\n try {\n if (this.props.dropShadow) {\n const shadow = this.props.dropShadow\n const shadowBlur = Math.max(shadow.offsetX ?? 0, shadow.offsetY ?? 0)\n ctx.shadowOffsetX = shadow.offsetX ?? 0\n ctx.shadowOffsetY = shadow.offsetY ?? 0\n ctx.shadowBlur = Math.max(0, shadow.blur ?? shadowBlur)\n ctx.shadowColor = shadow.color ?? 'black'\n }\n\n const saturateValue = this.props.saturate ?? 1\n let filterString = ''\n if (saturateValue !== 1) {\n filterString += `saturate(${saturateValue * 100}%) `\n }\n\n if (filterString) {\n const currentFilter = ctx.filter && ctx.filter !== 'none' ? ctx.filter + ' ' : ''\n ctx.filter = currentFilter + filterString.trim()\n }\n\n const finalDX = Math.floor(dx)\n const finalDY = Math.floor(dy)\n const finalDW = Math.ceil(dw + (dx - finalDX))\n const finalDH = Math.ceil(dh + (dy - finalDY))\n\n if (finalDW > 0 && finalDH > 0) {\n ctx.drawImage(img, sx, sy, sw, sh, finalDX, finalDY, finalDW, finalDH)\n }\n } catch (drawError) {\n console.error('[ImageNode] Error drawing image:', drawError)\n } finally {\n ctx.restore()\n }\n\n ctx.restore()\n }\n}\n\n/**\n * Factory function to create ImageNode instances\n */\nexport const Image = (props: ImageProps): CanvasElement => ({\n __type: 'Image',\n props: props as Omit<ImageProps, 'onLoad' | 'onError'>,\n})\n"],"names":["BoxNode","Style","fs","loadImage","parseBorderRadius","drawRoundedRectPath"],"mappings":";;;;;;;;AAOA;;;AAGG;AACH,SAAS,wBAAwB,CAAC,aAAgD,EAAE,cAAsB,EAAA;AACxG,IAAA,MAAM,KAAK,GAAG,aAAa,IAAI,KAAK;AACpC,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC7B,QAAA,OAAO,KAAK;IACd;AACA,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QACpD,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,GAAG;QAC1C,OAAO,cAAc,GAAG,UAAU;IACpC;AACA,IAAA,OAAO,CAAC,IAAI,CAAC,oDAAoD,KAAK,CAAA,oBAAA,CAAsB,CAAC;IAC7F,OAAO,cAAc,GAAG,GAAG;AAC7B;AAQA;;;AAGG;AACG,MAAO,SAAU,SAAQA,0BAAO,CAAA;IAE5B,WAAW,GAAuB,IAAI;IACtC,YAAY,GAAG,CAAC;IAChB,aAAa,GAAG,CAAC;IACjB,cAAc,GAAyB,IAAI;AAEnD,IAAA,WAAA,CAAY,KAAiB,EAAA;AAC3B,QAAA,KAAK,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;QAEvD,IAAI,CAAC,KAAK,GAAG;AACX,YAAA,SAAS,EAAE,MAAM;AACjB,YAAA,QAAQ,EAAEC,kBAAK,CAAC,QAAQ,CAAC,MAAM;AAC/B,YAAA,QAAQ,EAAE,CAAC;YACX,cAAc,EAAE,EAAE,IAAI,EAAE,KAA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O,EAAE,OAAO,CAAC;YACxE;QACF;QAAE,OAAO,SAAS,EAAE;AAClB,YAAA,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,SAAS,CAAC;QAC9D;gBAAU;YACR,GAAG,CAAC,OAAO,EAAE;QACf;QAEA,GAAG,CAAC,OAAO,EAAE;IACf;AACD;AAED;;AAEG;MACU,KAAK,GAAG,CAAC,KAAiB,MAAqB;AAC1D,IAAA,MAAM,EAAE,OAAO;AACf,IAAA,KAAK,EAAE,KAA+C;AACvD,CAAA;;;;;"}
1
+ {"version":3,"file":"image.canvas.util.js","sources":["../../../../src/canvas/image.canvas.util.ts"],"sourcesContent":["import type { BaseProps, ImageProps, CanvasElement } from '@/canvas/canvas.type.js'\nimport { type CanvasRenderingContext2D, Image as CanvasImage, loadImage } from 'skia-canvas'\nimport { BoxNode } from '@/canvas/layout.canvas.util.js'\nimport { drawRoundedRectPath, parseBorderRadius } from '@/canvas/canvas.helper.js'\nimport { promises as fs } from 'fs'\nimport { Style } from '@/constant/common.const.js'\nimport { hashBuffer, readDiskCache, writeDiskCache } from '@/util/disk.cache.js'\n\n/**\n * Calculates pixel offset for image positioning based on percentage or pixel values.\n * This handles centering, edge alignment, and percentage-based positioning.\n */\nfunction calculateOffsetFromValue(positionValue: number | `${number}%` | undefined, availableSpace: number): number {\n const value = positionValue ?? '50%'\n if (typeof value === 'number') {\n return value\n }\n if (typeof value === 'string' && value.endsWith('%')) {\n const percentage = parseFloat(value) / 100\n return availableSpace * percentage\n }\n console.warn(`[ImageNode] Invalid objectPosition value format: ${value}. Defaulting to 50%.`)\n return availableSpace * 0.5\n}\n\n/**\n * Per-render image cache — keyed by `src|color` for string sources.\n * Scoped to a single RootNode.render() call; discarded after rendering.\n */\nexport type RenderImageCache = Map<string, Promise<CanvasImage>>\n\n// ---------------------------------------------------------------------------\n// Persistent LRU image cache — survives across render passes\n// ---------------------------------------------------------------------------\n\ninterface LRUEntry {\n image: CanvasImage\n key: string\n}\n\n/**\n * A simple LRU cache for resolved `CanvasImage` objects.\n *\n * - Persists across render passes so repeated renders of the same tree don't\n * re-fetch every image.\n * - Bounded by `maxSize` entries; least-recently-used entries are evicted first.\n * - Call `dispose()` to eagerly release all held images, or rely on the\n * automatic `process.on('exit')` hook that clears the singleton.\n */\nexport class ImageLRUCache {\n private readonly map = new Map<string, LRUEntry>()\n readonly maxSize: number\n\n constructor(maxSize: number) {\n this.maxSize = maxSize\n }\n\n get(key: string): CanvasImage | undefined {\n const entry = this.map.get(key)\n if (!entry) return undefined\n // Move to end (most-recently used)\n this.map.delete(key)\n this.map.set(key, entry)\n return entry.image\n }\n\n set(key: string, image: CanvasImage): void {\n // If key already exists, refresh it\n if (this.map.has(key)) {\n this.map.delete(key)\n }\n // Evict oldest if at capacity\n while (this.map.size >= this.maxSize) {\n const oldest = this.map.keys().next().value!\n this.map.delete(oldest)\n }\n this.map.set(key, { image, key })\n }\n\n has(key: string): boolean {\n return this.map.has(key)\n }\n\n get size(): number {\n return this.map.size\n }\n\n dispose(): void {\n this.map.clear()\n }\n}\n\n/** Module-level singleton — lazily created on first render. */\nlet _globalImageCache: ImageLRUCache | null = null\n\nconst DEFAULT_CACHE_SIZE = 128\n\n// Symbol key on process to track hook registration across module reloads (e.g. Jest resetModules)\nconst HOOK_KEY = Symbol.for('__meonode_canvas_image_cache_hook__')\n\n/**\n * Returns the singleton `ImageLRUCache`, creating it on first access.\n * Registers a one-time process cleanup hook to clear the cache\n * so native image buffers are freed when the process shuts down.\n */\nexport function getImageCache(maxSize: number = DEFAULT_CACHE_SIZE): ImageLRUCache {\n if (!_globalImageCache) {\n _globalImageCache = new ImageLRUCache(maxSize)\n }\n if (!(globalThis as any)[HOOK_KEY]) {\n ;(globalThis as any)[HOOK_KEY] = true\n const cleanup = () => {\n _globalImageCache?.dispose()\n _globalImageCache = null\n }\n process.once('exit', cleanup)\n process.once('SIGINT', cleanup)\n process.once('SIGTERM', cleanup)\n }\n return _globalImageCache\n}\n\n/**\n * Explicitly disposes the global image cache.\n * Useful in tests or when tearing down the rendering engine.\n */\nexport function disposeImageCache(): void {\n _globalImageCache?.dispose()\n _globalImageCache = null\n}\n\n/**\n * Renders images with configurable sizing, positioning, and effects.\n * Supports object-fit modes, positioning, border radius, and saturation filters.\n */\nexport class ImageNode extends BoxNode {\n declare props: ImageProps & BaseProps\n private loadedImage: CanvasImage | null = null\n private naturalWidth = 0\n private naturalHeight = 0\n private loadingPromise: Promise<void> | null = null\n\n constructor(props: ImageProps) {\n super({ name: 'Image', ...props, children: undefined })\n\n this.props = {\n objectFit: 'fill',\n overflow: Style.Overflow.Hidden,\n saturate: 1,\n objectPosition: { Left: '50%', Top: '50%' },\n ...props,\n }\n }\n\n public load(cache?: RenderImageCache): Promise<void> {\n if (!this.loadingPromise) {\n this.loadingPromise = this._loadImage(cache)\n }\n return this.loadingPromise\n }\n\n /**\n * Fetches and processes the image source into a CanvasImage.\n * Does not touch node state — pure fetch logic.\n *\n * If `diskCacheKey` is provided, the resolved image buffer is written to the\n * disk cache at `.cache/files/<diskCacheKey>` (best-effort, fire-and-forget).\n */\n private async _fetchCanvasImage(diskCacheKey?: string): Promise<CanvasImage> {\n const { fileTypeFromBuffer, fileTypeFromFile } = await import('file-type')\n let finalSource: string | Buffer = this.props.src\n let isSvg = false\n let contentBuffer: Buffer | null = null\n let detectedMime: string | undefined\n\n if (typeof this.props.src === 'string') {\n if (this.props.src.startsWith('http')) {\n const response = await fetch(this.props.src)\n if (!response.ok) {\n throw new Error(`HTTP error ${response.status} fetching image: ${this.props.src}`)\n }\n const imageArrayBuffer = await response.arrayBuffer()\n contentBuffer = Buffer.from(imageArrayBuffer)\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && contentBuffer.toString('utf-8').includes('<svg')) {\n isSvg = true\n }\n } else {\n finalSource = this.props.src\n const filePath = this.props.src\n\n try {\n const fileTypeResult = await fileTypeFromFile(filePath)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n\n if ((!detectedMime || detectedMime === 'application/xml') && filePath.toLowerCase().endsWith('.svg')) {\n isSvg = true\n }\n } catch {\n isSvg = filePath.toLowerCase().endsWith('.svg')\n }\n\n if (isSvg && this.props.color) {\n try {\n contentBuffer = await fs.readFile(filePath)\n } catch {\n isSvg = false\n contentBuffer = null\n }\n }\n }\n } else {\n contentBuffer = this.props.src\n finalSource = contentBuffer\n\n const fileTypeResult = await fileTypeFromBuffer(contentBuffer)\n detectedMime = fileTypeResult?.mime\n isSvg = detectedMime === 'image/svg+xml'\n }\n\n if (isSvg && this.props.color && contentBuffer) {\n const svgString = contentBuffer.toString('utf-8')\n const modifiedSvgString = svgString.replace(/fill=\"[^\"]*\"/g, `fill=\"${this.props.color}\"`)\n finalSource = modifiedSvgString !== svgString ? Buffer.from(modifiedSvgString) : contentBuffer\n }\n\n // Write resolved buffer to disk cache (fire-and-forget, non-fatal)\n if (diskCacheKey) {\n const cacheBuffer = Buffer.isBuffer(finalSource) ? finalSource : contentBuffer\n if (cacheBuffer) writeDiskCache(diskCacheKey, cacheBuffer)\n }\n\n return loadImage(finalSource as never)\n }\n\n /**\n * Loads and processes an image.\n *\n * Resolution order:\n * 1. Persistent LRU cache (cross-render) — instant hit, no I/O.\n * 2. Disk cache at `.cache/files/<hash>` — survives process restarts.\n * 3. Per-render dedup cache — avoids duplicate in-flight fetches within a single render.\n * 4. Fresh fetch via `_fetchCanvasImage()` — writes buffer to disk cache after fetch.\n *\n * Buffer sources use a SHA-256 hash as their cache key (same as string sources).\n */\n private _loadImage(cache?: RenderImageCache): Promise<void> {\n if (!this.props.src) {\n const aspectRatioFromProps = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(aspectRatioFromProps)\n this.naturalWidth = 0\n this.naturalHeight = 0\n return Promise.resolve()\n }\n\n return new Promise(resolve => {\n const load = async () => {\n try {\n const lru = getImageCache()\n const cacheKey =\n typeof this.props.src === 'string'\n ? this.props.color\n ? `${this.props.src}|${this.props.color}`\n : this.props.src\n : this.props.color\n ? `${hashBuffer(this.props.src)}|${this.props.color}`\n : hashBuffer(this.props.src)\n\n // 1. Check persistent LRU cache\n const cached = lru.get(cacheKey)\n if (cached) {\n this.loadedImage = cached\n this.naturalWidth = cached.width\n this.naturalHeight = cached.height\n const calculatedAspectRatio = cached.width > 0 && cached.height > 0 ? cached.width / cached.height : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n return\n }\n\n // 2. Check disk cache (persists across process restarts)\n const diskBuffer = await readDiskCache(cacheKey)\n if (diskBuffer) {\n const img = await loadImage(diskBuffer as never)\n lru.set(cacheKey, img)\n this.loadedImage = img\n this.naturalWidth = img.width\n this.naturalHeight = img.height\n const calculatedAspectRatio = img.width > 0 && img.height > 0 ? img.width / img.height : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n return\n }\n\n // 3. Per-render dedup cache or fresh fetch (writes to disk internally)\n let imagePromise: Promise<CanvasImage>\n if (cache) {\n if (!cache.has(cacheKey)) {\n cache.set(cacheKey, this._fetchCanvasImage(cacheKey))\n }\n imagePromise = cache.get(cacheKey)!\n } else {\n imagePromise = this._fetchCanvasImage(cacheKey)\n }\n\n const img = await imagePromise\n\n // 4. Store in persistent LRU cache\n lru.set(cacheKey, img)\n\n this.loadedImage = img\n this.naturalWidth = img.width\n this.naturalHeight = img.height\n\n const calculatedAspectRatio = this.naturalWidth > 0 && this.naturalHeight > 0 ? this.naturalWidth / this.naturalHeight : undefined\n const finalAspectRatio = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : calculatedAspectRatio\n\n this.node.setAspectRatio(finalAspectRatio)\n this.props.onLoad?.()\n resolve()\n } catch (error: any) {\n this.naturalWidth = 0\n this.naturalHeight = 0\n const finalAspectRatioOnError = typeof this.props.aspectRatio === 'number' && this.props.aspectRatio > 0 ? this.props.aspectRatio : undefined\n this.node.setAspectRatio(finalAspectRatioOnError)\n this.props.onError?.(error)\n resolve()\n }\n }\n load()\n })\n }\n\n public getLoadingPromise(): Promise<void> {\n return this.loadingPromise ?? this.load()\n }\n\n /**\n * Renders the image with correct sizing, clipping, and positioning.\n * Handles object-fit, object-position, and visual effects like saturation.\n */\n protected override _renderContent(ctx: CanvasRenderingContext2D, x: number, y: number, width: number, height: number) {\n super._renderContent(ctx, x, y, width, height)\n\n if (!this.loadedImage || width <= 0 || height <= 0) return\n const img = this.loadedImage\n const imgW = this.naturalWidth\n const imgH = this.naturalHeight\n if (imgW <= 0 || imgH <= 0) return\n\n // Calculate content box accounting for padding and borders\n const paddingLeft = this.node.getComputedPadding(Style.Edge.Left)\n const paddingTop = this.node.getComputedPadding(Style.Edge.Top)\n const paddingRight = this.node.getComputedPadding(Style.Edge.Right)\n const paddingBottom = this.node.getComputedPadding(Style.Edge.Bottom)\n const borderLeft = this.node.getComputedBorder(Style.Edge.Left)\n const borderTop = this.node.getComputedBorder(Style.Edge.Top)\n const borderRight = this.node.getComputedBorder(Style.Edge.Right)\n const borderBottom = this.node.getComputedBorder(Style.Edge.Bottom)\n const contentX = x + borderLeft + paddingLeft\n const contentY = y + borderTop + paddingTop\n const contentWidth = Math.max(0, width - borderLeft - paddingLeft - borderRight - paddingRight)\n const contentHeight = Math.max(0, height - borderTop - paddingTop - borderBottom - paddingBottom)\n\n if (contentWidth <= 0 || contentHeight <= 0) return\n\n // Apply clipping for border radius\n ctx.save()\n const outerRadii = parseBorderRadius(this.props.borderRadius)\n const innerBorderRadii = {\n TopLeft: Math.max(0, outerRadii.TopLeft - borderTop),\n TopRight: Math.max(0, outerRadii.TopRight - borderTop),\n BottomRight: Math.max(0, outerRadii.BottomRight - borderBottom),\n BottomLeft: Math.max(0, outerRadii.BottomLeft - borderBottom),\n }\n const contentRadii = {\n TopLeft: Math.max(0, innerBorderRadii.TopLeft - Math.max(paddingLeft, paddingTop)),\n TopRight: Math.max(0, innerBorderRadii.TopRight - Math.max(paddingRight, paddingTop)),\n BottomRight: Math.max(0, innerBorderRadii.BottomRight - Math.max(paddingRight, paddingBottom)),\n BottomLeft: Math.max(0, innerBorderRadii.BottomLeft - Math.max(paddingLeft, paddingBottom)),\n }\n drawRoundedRectPath(ctx, contentX, contentY, contentWidth, contentHeight, contentRadii)\n ctx.clip()\n\n // Calculate image dimensions based on object-fit\n const nodeRatio = contentWidth / contentHeight\n const imgRatio = imgW / imgH\n const objectFit = this.props.objectFit\n let dw = contentWidth\n let dh = contentHeight\n\n if (objectFit === 'contain') {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n } else if (objectFit === 'cover') {\n if (imgRatio > nodeRatio) {\n dh = contentHeight\n dw = contentHeight * imgRatio\n } else {\n dw = contentWidth\n dh = contentWidth / imgRatio\n }\n } else if (objectFit === 'none') {\n dw = imgW\n dh = imgH\n } else if (objectFit === 'scale-down') {\n if (imgW <= contentWidth && imgH <= contentHeight) {\n dw = imgW\n dh = imgH\n } else {\n if (imgRatio > nodeRatio) {\n dw = contentWidth\n dh = contentWidth / imgRatio\n } else {\n dh = contentHeight\n dw = contentHeight * imgRatio\n }\n }\n }\n\n // Calculate image position based on object-position\n const sx = 0\n const sy = 0\n const sw = imgW\n const sh = imgH\n\n const availableWidth = contentWidth - dw\n const availableHeight = contentHeight - dh\n const posProps = this.props.objectPosition || {}\n const horizontalValue = posProps.Left !== undefined ? posProps.Left : posProps.Right !== undefined ? posProps.Right : '50%'\n const verticalValue = posProps.Top !== undefined ? posProps.Top : posProps.Bottom !== undefined ? posProps.Bottom : '50%'\n\n let offsetX = calculateOffsetFromValue(horizontalValue, availableWidth)\n let offsetY = calculateOffsetFromValue(verticalValue, availableHeight)\n\n if (posProps.Left === undefined && posProps.Right !== undefined) {\n offsetX = availableWidth - offsetX\n }\n if (posProps.Top === undefined && posProps.Bottom !== undefined) {\n offsetY = availableHeight - offsetY\n }\n\n const dx = contentX + offsetX\n const dy = contentY + offsetY\n\n // Draw image with filters\n ctx.save()\n try {\n if (this.props.dropShadow) {\n const shadow = this.props.dropShadow\n const shadowBlur = Math.max(shadow.offsetX ?? 0, shadow.offsetY ?? 0)\n ctx.shadowOffsetX = shadow.offsetX ?? 0\n ctx.shadowOffsetY = shadow.offsetY ?? 0\n ctx.shadowBlur = Math.max(0, shadow.blur ?? shadowBlur)\n ctx.shadowColor = shadow.color ?? 'black'\n }\n\n const saturateValue = this.props.saturate ?? 1\n let filterString = ''\n if (saturateValue !== 1) {\n filterString += `saturate(${saturateValue * 100}%) `\n }\n\n if (filterString) {\n const currentFilter = ctx.filter && ctx.filter !== 'none' ? ctx.filter + ' ' : ''\n ctx.filter = currentFilter + filterString.trim()\n }\n\n const finalDX = Math.floor(dx)\n const finalDY = Math.floor(dy)\n const finalDW = Math.ceil(dw + (dx - finalDX))\n const finalDH = Math.ceil(dh + (dy - finalDY))\n\n if (finalDW > 0 && finalDH > 0) {\n ctx.drawImage(img, sx, sy, sw, sh, finalDX, finalDY, finalDW, finalDH)\n }\n } catch (drawError) {\n console.error('[ImageNode] Error drawing image:', drawError)\n } finally {\n ctx.restore()\n }\n\n ctx.restore()\n }\n}\n\n/**\n * Factory function to create ImageNode instances\n */\nexport const Image = (props: ImageProps): CanvasElement => ({\n __type: 'Image',\n props: props as Omit<ImageProps, 'onLoad' | 'onError'>,\n})\n"],"names":["BoxNode","Style","fs","writeDiskCache","loadImage","hashBuffer","readDiskCache","parseBorderRadius","drawRoundedRectPath"],"mappings":";;;;;;;;;A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AAI,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,CAAC;AAC9C,YAAA,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,CAAC;YAE9C,IAAI,OAAO,GAAG,CAAC,IAAI,OAAO,GAAG,CAAC,EAAE;gBAC9B,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC;YACxE;QACF;QAAE,OAAO,SAAS,EAAE;AAClB,YAAA,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,SAAS,CAAC;QAC9D;gBAAU;YACR,GAAG,CAAC,OAAO,EAAE;QACf;QAEA,GAAG,CAAC,OAAO,EAAE;IACf;AACD;AAED;;AAEG;MACU,KAAK,GAAG,CAAC,KAAiB,MAAqB;AAC1D,IAAA,MAAM,EAAE,OAAO;AACf,IAAA,KAAK,EAAE,KAA+C;AACvD,CAAA;;;;;;;;"}
@@ -7,6 +7,8 @@ export interface CanvasEngineConfig {
7
7
  workerMode?: boolean;
8
8
  /** Number of worker threads in the pool (default: os.cpus().length - 1) */
9
9
  workers?: number;
10
+ /** Maximum number of resolved images to keep in the persistent LRU cache (default: 128) */
11
+ imageCacheSize?: number;
10
12
  }
11
13
  /**
12
14
  * Configure the canvas rendering engine.
@@ -1 +1 @@
1
- {"version":3,"file":"root.canvas.util.d.ts","sourceRoot":"","sources":["../../../src/canvas/root.canvas.util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAA8C,MAAM,aAAa,CAAA;AAEhF,OAAO,EAAE,UAAU,EAAE,OAAO,EAAW,MAAM,gCAAgC,CAAA;AAC7E,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAkBlF,eAAO,MAAM,qBAAqB,YAEjC,CAAA;AAOD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AC,EAAE,OAAO,CAAA;IACpB,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,2FAA2F;IAC3F,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,kBAAkB,QAWpD;AA4LD;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,UAAU,EAAE,aAAa,GAAG,OAAO,CAmB5D;AAED;;;GAGG;AACH,qBAAa,QAAS,SAAQ,UAAU;IACtC,6CAA6C;IAC7C,OAAO,CAAC,MAAM,CAAoB;IAClC,8CAA8C;IAC9C,OAAO,CAAC,GAAG,CAAwC;IACnD,4CAA4C;IAC5C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;IACpC,6CAA6C;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAQ;IACrC,4DAA4D;IAC5D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAE9B;;;;OAIG;gBACS,KAAK,EAAE,SAAS,GAAG,SAAS;IAoDxC;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAazB;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC;CAmDhC;AAED;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,GAAU,OAAO,SAAS,KAAG,OAAO,CAAC,MAAM,CAe3D,CAAA"}
@@ -50,6 +50,11 @@ function configure(options) {
50
50
  _workerMode = options.workerMode;
51
51
  if (options.workers !== undefined)
52
52
  _workerPoolSize = options.workers;
53
+ if (options.imageCacheSize !== undefined) {
54
+ // Dispose existing cache and create a new one with the specified size
55
+ image_canvas_util.disposeImageCache();
56
+ image_canvas_util.getImageCache(options.imageCacheSize);
57
+ }
53
58
  if (_workerMode) {
54
59
  _workerPool = new WorkerPool(_workerPoolSize);
55
60
  }
@@ -136,7 +141,7 @@ class WorkerPool {
136
141
  this.init(size);
137
142
  }
138
143
  init(size) {
139
- const workerFile = path__namespace.join(path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('canvas/root.canvas.util.js', document.baseURI).href)))), '../render.worker.js');
144
+ const workerFile = path__namespace.join(path__namespace.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('canvas/root.canvas.util.js', document.baseURI).href)))), '../worker/render.worker.js');
140
145
  for (let i = 0; i < size; i++) {
141
146
  const workerIdx = i;
142
147
  const worker = new node_worker_threads.Worker(workerFile);
@@ -313,42 +318,49 @@ class RootNode extends layout_canvas_util.ColumnNode {
313
318
  * @returns Promise resolving to the rendered Canvas instance
314
319
  */
315
320
  async render() {
316
- // Step 1: Load all images with a concurrency limit to avoid overwhelming remote sources.
317
- // A per-render cache deduplicates identical src+color combinations within this render pass.
318
- const imageNodes = this.findAllImageNodes();
319
- if (imageNodes.length > 0) {
320
- const imageCache = new Map();
321
- const CONCURRENCY = 5;
322
- const queue = [...imageNodes];
323
- const workers = Array.from({ length: Math.min(CONCURRENCY, queue.length) }, async () => {
324
- while (queue.length > 0) {
325
- const node = queue.shift();
326
- await node.load(imageCache);
327
- }
328
- });
329
- await Promise.allSettled(workers);
330
- }
331
- // Step 2: Calculate initial layout
332
- this.node.calculateLayout(this.targetWidth, undefined, common_const.Style.Direction.LTR);
333
- // Step 3: Allow nodes to finalize their layout
334
- const needRecalculate = this.finalizeLayout();
335
- if (needRecalculate) {
321
+ try {
322
+ // Step 1: Load all images with a concurrency limit to avoid overwhelming remote sources.
323
+ // A per-render cache deduplicates identical src+color combinations within this render pass.
324
+ const imageNodes = this.findAllImageNodes();
325
+ if (imageNodes.length > 0) {
326
+ const imageCache = new Map();
327
+ const CONCURRENCY = 5;
328
+ const queue = [...imageNodes];
329
+ const workers = Array.from({ length: Math.min(CONCURRENCY, queue.length) }, async () => {
330
+ while (queue.length > 0) {
331
+ const node = queue.shift();
332
+ await node.load(imageCache);
333
+ }
334
+ });
335
+ await Promise.allSettled(workers);
336
+ }
337
+ // Step 2: Calculate initial layout
336
338
  this.node.calculateLayout(this.targetWidth, undefined, common_const.Style.Direction.LTR);
339
+ // Step 3: Allow nodes to finalize their layout
340
+ const needRecalculate = this.finalizeLayout();
341
+ if (needRecalculate) {
342
+ this.node.calculateLayout(this.targetWidth, undefined, common_const.Style.Direction.LTR);
343
+ }
344
+ // Step 4: Create a canvas with calculated dimensions
345
+ const calculatedContentHeight = this.node.getComputedHeight();
346
+ const finalCanvasWidth = Math.ceil(this.targetWidth * this.scale);
347
+ const finalCanvasHeight = this.targetHeight ? Math.ceil(this.targetHeight * this.scale) : Math.max(1, Math.ceil(calculatedContentHeight * this.scale));
348
+ // Step 5: Set up canvas context
349
+ this.canvas = new skiaCanvas.Canvas(finalCanvasWidth, finalCanvasHeight);
350
+ this.ctx = this.canvas.getContext('2d');
351
+ this.ctx.scale(this.scale, this.scale);
352
+ // Step 6: Render content
353
+ super.render(this.ctx, 0, 0);
354
+ if (!this.canvas) {
355
+ throw new Error('Canvas not initialized');
356
+ }
357
+ return this.canvas;
337
358
  }
338
- // Step 4: Create a canvas with calculated dimensions
339
- const calculatedContentHeight = this.node.getComputedHeight();
340
- const finalCanvasWidth = Math.ceil(this.targetWidth * this.scale);
341
- const finalCanvasHeight = this.targetHeight ? Math.ceil(this.targetHeight * this.scale) : Math.max(1, Math.ceil(calculatedContentHeight * this.scale));
342
- // Step 5: Set up canvas context
343
- this.canvas = new skiaCanvas.Canvas(finalCanvasWidth, finalCanvasHeight);
344
- this.ctx = this.canvas.getContext('2d');
345
- this.ctx.scale(this.scale, this.scale);
346
- // Step 6: Render content
347
- super.render(this.ctx, 0, 0);
348
- if (!this.canvas) {
349
- throw new Error('Canvas not initialized');
359
+ finally {
360
+ // Always clear the persistent image cache after render (success or error)
361
+ // so resolved CanvasImage references don't outlive the render pass.
362
+ image_canvas_util.disposeImageCache();
350
363
  }
351
- return this.canvas;
352
364
  }
353
365
  }
354
366
  /**
@@ -359,14 +371,21 @@ class RootNode extends layout_canvas_util.ColumnNode {
359
371
  * @returns Promise resolving to the rendered Canvas (or WorkerCanvas in worker mode)
360
372
  */
361
373
  const Root = async (props) => {
362
- if (_workerMode) {
363
- if (!_workerPool) {
364
- _workerPool = new WorkerPool(_workerPoolSize);
374
+ try {
375
+ if (_workerMode) {
376
+ if (!_workerPool) {
377
+ _workerPool = new WorkerPool(_workerPoolSize);
378
+ }
379
+ const result = await _workerPool.render(props);
380
+ return new WorkerCanvas({ ...result, pool: _workerPool });
365
381
  }
366
- const result = await _workerPool.render(props);
367
- return new WorkerCanvas({ ...result, pool: _workerPool });
382
+ return await new RootNode(props).render();
383
+ }
384
+ catch (err) {
385
+ // Ensure cache is cleared even if Root-level orchestration fails
386
+ image_canvas_util.disposeImageCache();
387
+ throw err;
368
388
  }
369
- return new RootNode(props).render();
370
389
  };
371
390
 
372
391
  exports.Root = Root;