@pixldocs/canvas-renderer 0.3.12 → 0.3.13

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
@@ -10515,6 +10515,15 @@ function collectFontsFromConfig(config) {
10515
10515
  for (const node of nodes) {
10516
10516
  if (node.fontFamily) fonts.add(normalizeFontFamily(node.fontFamily));
10517
10517
  if ((_a2 = node.smartProps) == null ? void 0 : _a2.fontFamily) fonts.add(normalizeFontFamily(node.smartProps.fontFamily));
10518
+ if (node.styles && Array.isArray(node.styles)) {
10519
+ for (const lineStyle of node.styles) {
10520
+ if (lineStyle && typeof lineStyle === "object") {
10521
+ for (const charStyle of Object.values(lineStyle)) {
10522
+ if (charStyle == null ? void 0 : charStyle.fontFamily) fonts.add(normalizeFontFamily(charStyle.fontFamily));
10523
+ }
10524
+ }
10525
+ }
10526
+ }
10518
10527
  if (node.children) walk(node.children);
10519
10528
  }
10520
10529
  }
@@ -10532,6 +10541,76 @@ function collectFontsFromConfig(config) {
10532
10541
  }
10533
10542
  return fonts;
10534
10543
  }
10544
+ function collectFontDescriptorsFromConfig(config) {
10545
+ var _a;
10546
+ const seen = /* @__PURE__ */ new Set();
10547
+ const descriptors = [];
10548
+ function add(family, weight, style) {
10549
+ const f = normalizeFontFamily(family);
10550
+ if (!f) return;
10551
+ const w = weight ?? 400;
10552
+ const s = style ?? "normal";
10553
+ const key = `${f}|${w}|${s}`;
10554
+ if (seen.has(key)) return;
10555
+ seen.add(key);
10556
+ descriptors.push({ family: f, weight: w, style: s });
10557
+ }
10558
+ function walk(nodes) {
10559
+ var _a2;
10560
+ if (!nodes) return;
10561
+ for (const node of nodes) {
10562
+ if (node.fontFamily) {
10563
+ add(node.fontFamily, node.fontWeight, node.fontStyle);
10564
+ }
10565
+ if ((_a2 = node.smartProps) == null ? void 0 : _a2.fontFamily) {
10566
+ add(node.smartProps.fontFamily, node.smartProps.fontWeight, node.smartProps.fontStyle);
10567
+ }
10568
+ if (node.styles && Array.isArray(node.styles)) {
10569
+ for (const lineStyle of node.styles) {
10570
+ if (lineStyle && typeof lineStyle === "object") {
10571
+ for (const charStyle of Object.values(lineStyle)) {
10572
+ if (charStyle == null ? void 0 : charStyle.fontFamily) {
10573
+ add(charStyle.fontFamily, charStyle.fontWeight, charStyle.fontStyle);
10574
+ }
10575
+ }
10576
+ }
10577
+ }
10578
+ }
10579
+ if (node.children) walk(node.children);
10580
+ }
10581
+ }
10582
+ add("Open Sans", 400, "normal");
10583
+ for (const page of config.pages || []) {
10584
+ walk(page.children || []);
10585
+ }
10586
+ if ((_a = config.themeConfig) == null ? void 0 : _a.variables) {
10587
+ for (const def of Object.values(config.themeConfig.variables)) {
10588
+ if (def.value && typeof def.value === "string" && !def.value.startsWith("#") && !def.value.startsWith("rgb")) {
10589
+ if (def.label && /font/i.test(def.label)) {
10590
+ add(def.value);
10591
+ }
10592
+ }
10593
+ }
10594
+ }
10595
+ return descriptors;
10596
+ }
10597
+ async function ensureFontsForResolvedConfig(config) {
10598
+ if (typeof document === "undefined") return;
10599
+ const descriptors = collectFontDescriptorsFromConfig(config);
10600
+ const families = new Set(descriptors.map((d) => d.family));
10601
+ await Promise.all([...families].map((f) => loadGoogleFontCSS(f)));
10602
+ if (document.fonts) {
10603
+ const loadPromises = descriptors.map((d) => {
10604
+ const stylePrefix = d.style === "italic" ? "italic " : "";
10605
+ const weightStr = String(d.weight);
10606
+ const spec = `${stylePrefix}${weightStr} 16px "${d.family}"`;
10607
+ return document.fonts.load(spec).catch(() => {
10608
+ });
10609
+ });
10610
+ await Promise.all(loadPromises);
10611
+ await document.fonts.ready;
10612
+ }
10613
+ }
10535
10614
  class PixldocsRenderer {
10536
10615
  constructor(config) {
10537
10616
  __publicField(this, "config");
@@ -10552,8 +10631,7 @@ class PixldocsRenderer {
10552
10631
  if (!page) {
10553
10632
  throw new Error(`Page index ${pageIndex} not found (template has ${templateConfig.pages.length} pages)`);
10554
10633
  }
10555
- const fonts = collectFontsFromConfig(templateConfig);
10556
- await Promise.all([...fonts].map((f) => loadGoogleFontCSS(f)));
10634
+ await ensureFontsForResolvedConfig(templateConfig);
10557
10635
  const { setPackageApiUrl: setPackageApiUrl2 } = await Promise.resolve().then(() => appApi);
10558
10636
  setPackageApiUrl2(this.config.imageProxyUrl);
10559
10637
  const dataUrl = await this.renderPageViaPreviewCanvas(
@@ -10780,7 +10858,9 @@ class PixldocsRenderer {
10780
10858
  exports.PixldocsPreview = PixldocsPreview;
10781
10859
  exports.PixldocsRenderer = PixldocsRenderer;
10782
10860
  exports.applyThemeToConfig = applyThemeToConfig;
10861
+ exports.collectFontDescriptorsFromConfig = collectFontDescriptorsFromConfig;
10783
10862
  exports.collectFontsFromConfig = collectFontsFromConfig;
10863
+ exports.ensureFontsForResolvedConfig = ensureFontsForResolvedConfig;
10784
10864
  exports.loadGoogleFontCSS = loadGoogleFontCSS;
10785
10865
  exports.normalizeFontFamily = normalizeFontFamily;
10786
10866
  exports.resolveFromForm = resolveFromForm;