@polotno/pdf-import 0.0.1 → 0.0.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.
@@ -4,12 +4,15 @@ export declare class FontRegistry {
4
4
  private fontDataMap;
5
5
  private fontMetricsMap;
6
6
  private otCache;
7
+ private renameMap;
7
8
  /**
8
9
  * Parse font data with opentype.js, returning cached result if available.
9
10
  * Key is the pdfjs loaded font name (e.g. "g_d0_f1").
10
11
  */
11
12
  parseOpentype(loadedName: string, data: Uint8Array): opentype.Font | null;
12
- recordFont(fontObj: any): void;
13
+ recordFont(fontObj: any, embedAllFonts?: boolean): void;
14
+ /** Get the fontFamily name for a PDF font, accounting for renames. */
15
+ getFontFamily(pdfFontName: string): string;
13
16
  finalize(fontStrategy: 'embed' | 'googleFontsMatch', pages: PolotnoPage[]): PolotnoFont[];
14
17
  }
15
18
  //# sourceMappingURL=font-registry.d.ts.map
@@ -8,6 +8,8 @@ export class FontRegistry {
8
8
  this.fontMetricsMap = new Map();
9
9
  // Cache opentype.js parsed fonts across pages to avoid re-parsing
10
10
  this.otCache = new Map();
11
+ // Maps PDF font name → renamed fontFamily (only for embed-all renamed fonts)
12
+ this.renameMap = new Map();
11
13
  }
12
14
  /**
13
15
  * Parse font data with opentype.js, returning cached result if available.
@@ -28,25 +30,33 @@ export class FontRegistry {
28
30
  return null;
29
31
  }
30
32
  }
31
- recordFont(fontObj) {
33
+ recordFont(fontObj, embedAllFonts = false) {
32
34
  if (!fontObj?.name)
33
35
  return;
34
36
  const mappedFamily = mapPdfFont(fontObj.name);
35
- const isUnknown = !isKnownWebFont(fontObj.name);
36
- // Collect font binary data for non-Google/non-standard fonts
37
- if (isUnknown && fontObj.data && fontObj.data.length > 0) {
37
+ const isGoogleFont = isKnownWebFont(fontObj.name);
38
+ const shouldEmbed = embedAllFonts || !isGoogleFont;
39
+ // When embedding a known Google Font, rename to avoid Polotno loading
40
+ // the Google version instead of the embedded subset.
41
+ const fontFamily = embedAllFonts && isGoogleFont ? `${mappedFamily} (PDF)` : mappedFamily;
42
+ // Track the rename so text elements can use the correct fontFamily
43
+ if (fontFamily !== mappedFamily) {
44
+ this.renameMap.set(fontObj.name, fontFamily);
45
+ }
46
+ // Collect font binary data
47
+ if (shouldEmbed && fontObj.data && fontObj.data.length > 0) {
38
48
  const mime = fontObj.mimetype || 'font/opentype';
39
- const arr = this.fontDataMap.get(mappedFamily) || [];
49
+ const arr = this.fontDataMap.get(fontFamily) || [];
40
50
  arr.push({ mime, data: new Uint8Array(fontObj.data) });
41
- this.fontDataMap.set(mappedFamily, arr);
51
+ this.fontDataMap.set(fontFamily, arr);
42
52
  }
43
53
  // Collect font metrics for unknown fonts (for Google Font matching)
44
- if (isUnknown && !this.fontMetricsMap.has(mappedFamily)) {
54
+ if (shouldEmbed && !this.fontMetricsMap.has(fontFamily)) {
45
55
  const widths = (fontObj.widths || []).filter((w) => w != null && w > 0);
46
56
  const avgWidth = widths.length > 0
47
57
  ? widths.reduce((a, b) => a + b, 0) / widths.length
48
58
  : 500;
49
- this.fontMetricsMap.set(mappedFamily, {
59
+ this.fontMetricsMap.set(fontFamily, {
50
60
  fontName: fontObj.name.replace(/^[A-Z]{6}\+/, ''),
51
61
  isSerifFont: fontObj.isSerifFont || false,
52
62
  isMonospace: fontObj.isMonospace || false,
@@ -56,6 +66,10 @@ export class FontRegistry {
56
66
  });
57
67
  }
58
68
  }
69
+ /** Get the fontFamily name for a PDF font, accounting for renames. */
70
+ getFontFamily(pdfFontName) {
71
+ return this.renameMap.get(pdfFontName) ?? mapPdfFont(pdfFontName);
72
+ }
59
73
  finalize(fontStrategy, pages) {
60
74
  const fonts = [];
61
75
  if (fontStrategy === 'googleFontsMatch') {