Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fontUsage.props['font-display'] = fontDisplay;
}
}
}
// Generate codepoint sets for original font, the used subset and the unused subset
for (const htmlAssetTextWithProps of htmlAssetTextsWithProps) {
for (const fontUsage of htmlAssetTextWithProps.fontUsages) {
const originalFont = assetGraph.findAssets({
url: fontUsage.fontUrl
})[0];
if (originalFont.isLoaded) {
let originalCodepoints;
try {
// Guard against 'Unknown font format' errors
originalCodepoints = fontkit.create(originalFont.rawSrc)
.characterSet;
} catch (err) {}
if (originalCodepoints) {
const usedCodepoints = fontUsage.text
.split('')
.map(c => c.codePointAt(0));
const unusedCodepoints = originalCodepoints.filter(
n => !usedCodepoints.includes(n)
);
fontUsage.codepoints = {
original: originalCodepoints,
used: usedCodepoints,
unused: unusedCodepoints
};
}
htmlAsset: htmlAsset.urlOrDescription,
fontUsages: fontUsages
}))
};
}
// Generate subsets:
await getSubsetsForFontUsage(assetGraph, htmlAssetTextsWithProps, formats);
// Warn about missing glyphs
const missingGlyphsErrors = [];
for (const { htmlAsset, fontUsages } of htmlAssetTextsWithProps) {
for (const fontUsage of fontUsages) {
if (fontUsage.subsets) {
const characterSet = fontkit.create(Object.values(fontUsage.subsets)[0])
.characterSet;
for (const char of [...fontUsage.pageText]) {
// Turns out that browsers don't mind that these are missing:
if (char === '\t' || char === '\n') {
continue;
}
const codePoint = char.codePointAt(0);
const isMissing = !characterSet.includes(codePoint);
if (isMissing) {
let location;
const charIdx = htmlAsset.text.indexOf(char);
});
fontRelation.node.each(decl => {
if (decl.prop !== 'src') {
result[decl.prop] = decl.value;
}
});
return [fontRelation.to, fontRelation.format];
})
);
if (!('unicode-range' in result)) {
const font = result.src[0][0];
result['unicode-range'] = unicodeRange(
fontkit.create(font.rawSrc).characterSet
);
}
result['font-display'] = fontDisplay;
// Output font face declaration object as CSS
const declarationStrings = [];
for (const [property, value] of Object.entries(result)) {
if (property !== 'src') {
declarationStrings.push(` ${property}: ${value};`);
}
}
const sources = result.src.map(([font, format]) => {
return `url('${font.dataUrl}') format('${format}')`;
private async ensure(): Promise {
if (this.font) return;
const data = await this.file.load();
const buffer = new Buffer(data); // WTF! I don't like Buffer.
this.font = fontkit.create(buffer);
}
};
}
}
}
}
// Generate subsets:
await getSubsetsForFontUsage(assetGraph, htmlAssetTextsWithProps, formats);
// Warn about missing glyphs
const missingGlyphsErrors = [];
for (const { htmlAsset, fontUsages } of htmlAssetTextsWithProps) {
for (const fontUsage of fontUsages) {
if (fontUsage.subsets) {
const characterSet = fontkit.create(fontUsage.subsets.woff)
.characterSet;
for (const char of [...fontUsage.pageText]) {
// Turns out that browsers don't mind that these are missing:
if (char === '\t' || char === '\n') {
continue;
}
const codePoint = char.codePointAt(0);
const isMissing = !characterSet.includes(codePoint);
if (isMissing) {
let location;
const charIdx = htmlAsset.text.indexOf(char);
export const addFontToSvg = (fontPath: string, style: FontStyle = {}) => {
const resolvedPath = require.resolve(fontPath)
const fontFile = fs.readFileSync(resolvedPath)
const font = fontkit.create(fontFile, style.postscriptName)
const key = getFontKey(font, style)
svgFonts[key] = { path: resolvedPath, style: getFontStyle(font, style) }
if (font.fonts) {
font.fonts.forEach(f => addFont(f, { fontFamily: style.fontFamily }))
} else {
addFont(font, style)
}
}
static open(document, src, family, id) {
let font;
if (typeof src === 'string') {
if (StandardFont.isStandardFont(src)) {
return new StandardFont(document, src, id);
}
src = fs.readFileSync(src);
}
if (Buffer.isBuffer(src)) {
font = fontkit.create(src, family);
} else if (src instanceof Uint8Array) {
font = fontkit.create(new Buffer(src), family);
} else if (src instanceof ArrayBuffer) {
font = fontkit.create(new Buffer(new Uint8Array(src)), family);
}
if (font == null) {
throw new Error('Not a supported font format or standard PDF font.');
}
return new EmbeddedFont(document, font, id);
}
}
export const loadFont = (fontFile, style: FontStyle = {}) => {
const font = fontkit.create(fontFile, style.postscriptName)
if (font.fonts) {
font.fonts.forEach(f => addFont(f, { fontFamily: style.fontFamily }))
} else {
addFont(font, style)
}
}
static open(document, src, family, id) {
let font;
if (typeof src === 'string') {
if (StandardFont.isStandardFont(src)) {
return new StandardFont(document, src, id);
}
src = fs.readFileSync(src);
}
if (Buffer.isBuffer(src)) {
font = fontkit.create(src, family);
} else if (src instanceof Uint8Array) {
font = fontkit.create(new Buffer(src), family);
} else if (src instanceof ArrayBuffer) {
font = fontkit.create(new Buffer(new Uint8Array(src)), family);
}
if (font == null) {
throw new Error('Not a supported font format or standard PDF font.');
}
return new EmbeddedFont(document, font, id);
}
}