@pixldocs/canvas-renderer 0.5.57 → 0.5.58

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.d.ts CHANGED
@@ -25,6 +25,19 @@ export declare function applyThemeToConfig(config: TemplateConfig, themeOverride
25
25
  */
26
26
  export declare function assemblePdfFromSvgs(svgResults: SvgRenderResult[], options?: PdfAssemblyOptions): Promise<PdfRenderResult>;
27
27
 
28
+ /**
29
+ * Block until the webfonts referenced by `config` have actually loaded
30
+ * (or `maxWaitMs` elapses). Stronger than `ensureFontsForResolvedConfig`
31
+ * which only fire-and-forget queues `document.fonts.load()`.
32
+ *
33
+ * Use this BEFORE mounting `PreviewCanvas` so the synchronous
34
+ * `createText` auto-shrink loop measures against real font metrics
35
+ * instead of system fallback ones (which are typically narrower → loop
36
+ * decides "fits, no shrink needed" → text overflows once the real font
37
+ * swaps in).
38
+ */
39
+ export declare function awaitFontsForConfig(config: TemplateConfig, maxWaitMs: number): Promise<void>;
40
+
28
41
  export declare interface CanvasNode {
29
42
  id: string;
30
43
  name?: string;
@@ -64,6 +77,16 @@ export declare function collectFontsFromConfig(config: TemplateConfig): Set<stri
64
77
  */
65
78
  export declare function collectImageUrls(config: TemplateConfig): string[];
66
79
 
80
+ /**
81
+ * Returns true when any text node in `config` uses
82
+ * `overflowPolicy: 'auto-shrink'`. The synchronous shrink loop in
83
+ * `createText` measures with whatever font Fabric can resolve at mount
84
+ * time, so for these configs the consumer MUST block on real font load
85
+ * before mounting (otherwise it shrinks against fallback metrics and
86
+ * overflows once the real font swaps in).
87
+ */
88
+ export declare function configHasAutoShrinkText(config: TemplateConfig | null | undefined): boolean;
89
+
67
90
  export declare interface DynamicField {
68
91
  id: string;
69
92
  label: string;
package/dist/index.js CHANGED
@@ -12244,6 +12244,46 @@ async function ensureFontsForResolvedConfig(config) {
12244
12244
  });
12245
12245
  }
12246
12246
  }
12247
+ function configHasAutoShrinkText$1(config) {
12248
+ var _a;
12249
+ if (!((_a = config == null ? void 0 : config.pages) == null ? void 0 : _a.length)) return false;
12250
+ const walk = (nodes) => {
12251
+ for (const node of nodes || []) {
12252
+ if (!node) continue;
12253
+ if (node.type === "text" && node.overflowPolicy === "auto-shrink") return true;
12254
+ if (Array.isArray(node.children) && node.children.length && walk(node.children)) return true;
12255
+ }
12256
+ return false;
12257
+ };
12258
+ for (const page of config.pages) {
12259
+ if (walk(page.children || [])) return true;
12260
+ }
12261
+ return false;
12262
+ }
12263
+ async function awaitFontsForConfig(config, maxWaitMs) {
12264
+ if (typeof document === "undefined" || !document.fonts) return;
12265
+ void ensureFontsForResolvedConfig(config);
12266
+ const descriptors = collectFontDescriptorsFromConfig(config);
12267
+ if (descriptors.length === 0) return;
12268
+ const loads = Promise.all(
12269
+ descriptors.map((d) => {
12270
+ const stylePrefix = d.style === "italic" ? "italic " : "";
12271
+ const spec = `${stylePrefix}${d.weight} 16px "${d.family}"`;
12272
+ return document.fonts.load(spec).catch(() => []);
12273
+ })
12274
+ ).then(() => void 0);
12275
+ await Promise.race([
12276
+ loads,
12277
+ new Promise((resolve) => setTimeout(resolve, maxWaitMs))
12278
+ ]);
12279
+ await Promise.race([
12280
+ document.fonts.ready.catch(() => void 0).then(() => void 0),
12281
+ new Promise((r) => setTimeout(r, Math.min(500, maxWaitMs)))
12282
+ ]);
12283
+ await new Promise(
12284
+ (resolve) => requestAnimationFrame(() => requestAnimationFrame(() => resolve()))
12285
+ );
12286
+ }
12247
12287
  const PREVIEW_DEBUG_PREFIX = "[canvas-renderer][preview-debug]";
12248
12288
  function countUnderlinedNodes(config) {
12249
12289
  var _a;
@@ -12329,15 +12369,17 @@ function PixldocsPreview(props) {
12329
12369
  underlinedNodes: countUnderlinedNodes(resolved.config)
12330
12370
  });
12331
12371
  setResolvedConfig(resolved.config);
12332
- ensureFontsForResolvedConfig(resolved.config).then(() => {
12372
+ const hasAutoShrink = configHasAutoShrinkText$1(resolved.config);
12373
+ const waitMs = hasAutoShrink ? 4e3 : 1800;
12374
+ awaitFontsForConfig(resolved.config, waitMs).then(() => {
12333
12375
  if (!cancelled) {
12334
- console.log(PREVIEW_DEBUG_PREFIX, "resolve-mode fonts queued");
12376
+ console.log(PREVIEW_DEBUG_PREFIX, "resolve-mode fonts settled", { hasAutoShrink, waitMs });
12335
12377
  setFontsReady(true);
12336
12378
  setIsLoading(false);
12337
12379
  }
12338
12380
  }).catch((err) => {
12339
12381
  if (!cancelled) {
12340
- console.warn(PREVIEW_DEBUG_PREFIX, "resolve-mode fonts queue failed", err);
12382
+ console.warn(PREVIEW_DEBUG_PREFIX, "resolve-mode font wait failed", err);
12341
12383
  setFontsReady(true);
12342
12384
  setIsLoading(false);
12343
12385
  }
@@ -12406,16 +12448,26 @@ function PixldocsPreview(props) {
12406
12448
  setFontsReady(false);
12407
12449
  setCanvasSettled(false);
12408
12450
  setStabilizationPass(0);
12409
- ensureFontsForResolvedConfig(config).then(() => {
12410
- console.log(PREVIEW_DEBUG_PREFIX, "config-mode fonts queued", {
12451
+ let cancelled = false;
12452
+ const hasAutoShrink = configHasAutoShrinkText$1(config);
12453
+ const waitMs = hasAutoShrink ? 4e3 : 1800;
12454
+ awaitFontsForConfig(config, waitMs).then(() => {
12455
+ if (cancelled) return;
12456
+ console.log(PREVIEW_DEBUG_PREFIX, "config-mode fonts settled", {
12411
12457
  pageIndex,
12458
+ hasAutoShrink,
12459
+ waitMs,
12412
12460
  underlinedNodes: countUnderlinedNodes(config)
12413
12461
  });
12414
12462
  setFontsReady(true);
12415
12463
  }).catch((err) => {
12416
- console.warn(PREVIEW_DEBUG_PREFIX, "config-mode fonts queue failed", err);
12464
+ if (cancelled) return;
12465
+ console.warn(PREVIEW_DEBUG_PREFIX, "config-mode font wait failed", err);
12417
12466
  setFontsReady(true);
12418
12467
  });
12468
+ return () => {
12469
+ cancelled = true;
12470
+ };
12419
12471
  }, [isResolveMode, config]);
12420
12472
  const handleCanvasReady = useCallback(() => {
12421
12473
  if (stabilizationPass === 0) {
@@ -15624,9 +15676,11 @@ export {
15624
15676
  PixldocsRenderer,
15625
15677
  applyThemeToConfig,
15626
15678
  assemblePdfFromSvgs,
15679
+ awaitFontsForConfig,
15627
15680
  collectFontDescriptorsFromConfig,
15628
15681
  collectFontsFromConfig,
15629
15682
  collectImageUrls,
15683
+ configHasAutoShrinkText$1 as configHasAutoShrinkText,
15630
15684
  embedFont,
15631
15685
  embedFontsForConfig,
15632
15686
  embedFontsInPdf,