@pixldocs/canvas-renderer 0.5.8 → 0.5.10

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/index.cjs CHANGED
@@ -2690,10 +2690,12 @@ const waitForFontsReady = async () => {
2690
2690
  if (!document.fonts) return;
2691
2691
  await document.fonts.ready;
2692
2692
  };
2693
+ const DEFAULT_FONT_CHECK_TIMEOUT_MS = 3500;
2694
+ const FONT_CHECK_POLL_MS = 60;
2693
2695
  const waitUntilFontsAvailable = async (fontFamilies, options) => {
2694
2696
  if (!document.fonts || fontFamilies.length === 0) return;
2695
- const timeoutMs = options == null ? void 0 : options.timeoutMs;
2696
- const pollMs = options == null ? void 0 : options.pollIntervalMs;
2697
+ const timeoutMs = (options == null ? void 0 : options.timeoutMs) ?? DEFAULT_FONT_CHECK_TIMEOUT_MS;
2698
+ const pollMs = (options == null ? void 0 : options.pollIntervalMs) ?? FONT_CHECK_POLL_MS;
2697
2699
  const deadline = Date.now() + timeoutMs;
2698
2700
  const check = () => fontFamilies.every(
2699
2701
  (f) => document.fonts.check(`16px "${f}"`) && document.fonts.check(`bold 16px "${f}"`)
@@ -12337,16 +12339,17 @@ class PixldocsRenderer {
12337
12339
  };
12338
12340
  const onReady = () => {
12339
12341
  const expectedImageCount = this.getExpectedImageCount(config, pageIndex);
12340
- this.waitForCanvasImages(container, expectedImageCount).then(() => {
12342
+ this.waitForCanvasImages(container, expectedImageCount).then(async () => {
12341
12343
  try {
12344
+ const fabricInstance = this.getFabricCanvasFromContainer(container);
12345
+ await this.waitForStableTextMetrics(container, config);
12342
12346
  const fabricCanvas = container.querySelector("canvas.upper-canvas, canvas");
12343
- if (!fabricCanvas) {
12347
+ const sourceCanvas = (fabricInstance == null ? void 0 : fabricInstance.lowerCanvasEl) || container.querySelector("canvas.lower-canvas") || fabricCanvas;
12348
+ if (!sourceCanvas) {
12344
12349
  cleanup();
12345
12350
  reject(new Error("No canvas element found after render"));
12346
12351
  return;
12347
12352
  }
12348
- const lowerCanvas = container.querySelector("canvas.lower-canvas");
12349
- const sourceCanvas = lowerCanvas || fabricCanvas;
12350
12353
  const exportCanvas = document.createElement("canvas");
12351
12354
  exportCanvas.width = sourceCanvas.width;
12352
12355
  exportCanvas.height = sourceCanvas.height;
@@ -12416,7 +12419,7 @@ class PixldocsRenderer {
12416
12419
  };
12417
12420
  const onReady = () => {
12418
12421
  const expectedImageCount = this.getExpectedImageCount(config, pageIndex);
12419
- this.waitForCanvasImages(container, expectedImageCount).then(() => {
12422
+ this.waitForCanvasImages(container, expectedImageCount).then(async () => {
12420
12423
  var _a, _b;
12421
12424
  try {
12422
12425
  const fabricInstance = this.getFabricCanvasFromContainer(container);
@@ -12425,6 +12428,7 @@ class PixldocsRenderer {
12425
12428
  reject(new Error("No Fabric canvas instance found for SVG capture"));
12426
12429
  return;
12427
12430
  }
12431
+ await this.waitForStableTextMetrics(container, config);
12428
12432
  const prevVPT = fabricInstance.viewportTransform ? [...fabricInstance.viewportTransform] : void 0;
12429
12433
  const prevSvgVPT = fabricInstance.svgViewportTransformation;
12430
12434
  const prevRetina = fabricInstance.enableRetinaScaling;
@@ -12546,6 +12550,52 @@ class PixldocsRenderer {
12546
12550
  }
12547
12551
  return null;
12548
12552
  }
12553
+ async waitForStableTextMetrics(container, config) {
12554
+ var _a;
12555
+ if (typeof document !== "undefined") {
12556
+ await ensureFontsForResolvedConfig(config);
12557
+ await ((_a = document.fonts) == null ? void 0 : _a.ready);
12558
+ const fontFamilies = [...collectFontsFromConfig(config)].filter(Boolean);
12559
+ if (fontFamilies.length > 0) {
12560
+ await waitUntilFontsAvailable(fontFamilies, { timeoutMs: 4e3, pollIntervalMs: 50 });
12561
+ }
12562
+ }
12563
+ const fabricInstance = this.getFabricCanvasFromContainer(container);
12564
+ if (!(fabricInstance == null ? void 0 : fabricInstance.getObjects)) return;
12565
+ const waitForPaint = () => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => r())));
12566
+ const clearTextMetricCaches = () => {
12567
+ clearFabricCharCache();
12568
+ clearMeasurementCache();
12569
+ };
12570
+ const reflowTextboxes = () => {
12571
+ var _a2, _b;
12572
+ const walk = (obj) => {
12573
+ var _a3, _b2;
12574
+ if (!obj) return;
12575
+ const children = Array.isArray(obj._objects) ? obj._objects : Array.isArray(obj.objects) ? obj.objects : [];
12576
+ if (children.length) children.forEach(walk);
12577
+ if (typeof obj.initDimensions === "function" && (obj.type === "textbox" || obj.isEditing !== void 0)) {
12578
+ const savedWidth = obj.width;
12579
+ obj.dirty = true;
12580
+ obj.initDimensions();
12581
+ if (savedWidth != null) (_a3 = obj.set) == null ? void 0 : _a3.call(obj, { width: savedWidth });
12582
+ (_b2 = obj.setCoords) == null ? void 0 : _b2.call(obj);
12583
+ }
12584
+ };
12585
+ fabricInstance.getObjects().forEach(walk);
12586
+ (_a2 = fabricInstance.calcOffset) == null ? void 0 : _a2.call(fabricInstance);
12587
+ (_b = fabricInstance.requestRenderAll) == null ? void 0 : _b.call(fabricInstance);
12588
+ };
12589
+ clearTextMetricCaches();
12590
+ await waitForPaint();
12591
+ reflowTextboxes();
12592
+ await waitForPaint();
12593
+ clearTextMetricCaches();
12594
+ reflowTextboxes();
12595
+ await waitForPaint();
12596
+ reflowTextboxes();
12597
+ await waitForPaint();
12598
+ }
12549
12599
  }
12550
12600
  const FONT_WEIGHT_LABELS = {
12551
12601
  300: "Light",