@pixldocs/canvas-renderer 0.3.11 → 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
@@ -10481,9 +10481,8 @@ async function loadGoogleFontCSS(rawFontFamily) {
10481
10481
  loadedFonts.add(fontFamily);
10482
10482
  return;
10483
10483
  }
10484
- const weights = "100;200;300;400;500;600;700;800;900";
10485
10484
  const encoded = encodeURIComponent(fontFamily);
10486
- const url = `https://fonts.googleapis.com/css2?family=${encoded}:ital,wght@0,${weights};1,${weights}&display=swap`;
10485
+ const url = `https://fonts.googleapis.com/css?family=${encoded}:300,400,500,600,700&display=swap`;
10487
10486
  const link = document.createElement("link");
10488
10487
  link.rel = "stylesheet";
10489
10488
  link.href = url;
@@ -10516,6 +10515,15 @@ function collectFontsFromConfig(config) {
10516
10515
  for (const node of nodes) {
10517
10516
  if (node.fontFamily) fonts.add(normalizeFontFamily(node.fontFamily));
10518
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
+ }
10519
10527
  if (node.children) walk(node.children);
10520
10528
  }
10521
10529
  }
@@ -10533,6 +10541,76 @@ function collectFontsFromConfig(config) {
10533
10541
  }
10534
10542
  return fonts;
10535
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
+ }
10536
10614
  class PixldocsRenderer {
10537
10615
  constructor(config) {
10538
10616
  __publicField(this, "config");
@@ -10553,8 +10631,7 @@ class PixldocsRenderer {
10553
10631
  if (!page) {
10554
10632
  throw new Error(`Page index ${pageIndex} not found (template has ${templateConfig.pages.length} pages)`);
10555
10633
  }
10556
- const fonts = collectFontsFromConfig(templateConfig);
10557
- await Promise.all([...fonts].map((f) => loadGoogleFontCSS(f)));
10634
+ await ensureFontsForResolvedConfig(templateConfig);
10558
10635
  const { setPackageApiUrl: setPackageApiUrl2 } = await Promise.resolve().then(() => appApi);
10559
10636
  setPackageApiUrl2(this.config.imageProxyUrl);
10560
10637
  const dataUrl = await this.renderPageViaPreviewCanvas(
@@ -10781,7 +10858,9 @@ class PixldocsRenderer {
10781
10858
  exports.PixldocsPreview = PixldocsPreview;
10782
10859
  exports.PixldocsRenderer = PixldocsRenderer;
10783
10860
  exports.applyThemeToConfig = applyThemeToConfig;
10861
+ exports.collectFontDescriptorsFromConfig = collectFontDescriptorsFromConfig;
10784
10862
  exports.collectFontsFromConfig = collectFontsFromConfig;
10863
+ exports.ensureFontsForResolvedConfig = ensureFontsForResolvedConfig;
10785
10864
  exports.loadGoogleFontCSS = loadGoogleFontCSS;
10786
10865
  exports.normalizeFontFamily = normalizeFontFamily;
10787
10866
  exports.resolveFromForm = resolveFromForm;