@polotno/pdf-export 0.1.36 → 0.1.37
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/lib/index.js +2 -2
- package/lib/text/fonts.d.ts +1 -0
- package/lib/text/fonts.js +18 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import path from 'path';
|
|
|
4
4
|
import { srcToBuffer, parseColor } from './utils.js';
|
|
5
5
|
import { renderImage } from './image.js';
|
|
6
6
|
import { loadFontIfNeeded, renderText } from './text/index.js';
|
|
7
|
+
import { registerFontUrl } from './text/fonts.js';
|
|
7
8
|
import { renderFigure } from './figure.js';
|
|
8
9
|
import { renderGroup } from './group.js';
|
|
9
10
|
import { lineToPDF } from './line.js';
|
|
@@ -75,8 +76,7 @@ export async function jsonToPDF(json, pdfFileName, attrs = {}) {
|
|
|
75
76
|
enableSpotColorSupport(doc, attrs.spotColors);
|
|
76
77
|
}
|
|
77
78
|
for (const font of json.fonts) {
|
|
78
|
-
|
|
79
|
-
fonts[font.fontFamily] = true;
|
|
79
|
+
registerFontUrl(font.fontFamily, font.url);
|
|
80
80
|
}
|
|
81
81
|
for (const page of json.pages) {
|
|
82
82
|
doc.addPage();
|
package/lib/text/fonts.d.ts
CHANGED
package/lib/text/fonts.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { srcToBuffer } from '../utils.js';
|
|
2
2
|
import getUrls from 'get-urls';
|
|
3
3
|
import fetch from 'node-fetch';
|
|
4
|
+
const fontUrlRegistry = {};
|
|
5
|
+
export function registerFontUrl(fontFamily, url) {
|
|
6
|
+
fontUrlRegistry[fontFamily] = url;
|
|
7
|
+
}
|
|
4
8
|
/**
|
|
5
9
|
* Get font weight string based on bold/italic state
|
|
6
10
|
*/
|
|
@@ -53,9 +57,20 @@ export async function loadFontForSegment(doc, segment, element, fonts) {
|
|
|
53
57
|
}
|
|
54
58
|
const fontKey = getFontKey(fontFamily, bold, italic, element.fontWeight);
|
|
55
59
|
if (!fonts[fontKey]) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
60
|
+
let src;
|
|
61
|
+
if (fontUrlRegistry[fontFamily]) {
|
|
62
|
+
src = fontUrlRegistry[fontFamily];
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const weight = getFontWeight(bold, italic, element.fontWeight);
|
|
66
|
+
src = await getGoogleFontPath(fontFamily, weight, italic);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
doc.registerFont(fontKey, await srcToBuffer(src));
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
throw new Error(`Failed to load font "${fontFamily}" from ${src}: ${error.message}`);
|
|
73
|
+
}
|
|
59
74
|
fonts[fontKey] = true;
|
|
60
75
|
}
|
|
61
76
|
doc.font(fontKey);
|