Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const ttfOptions = {
type: 'ttf',
hinting: true,
compound2simple: true,
inflate: null,
combinePath: false,
};
const opts = normalizeOptions(options);
if ( opts.subset ) {
ttfOptions.subset = opts.subset;
}
const buffer = fs.readFileSync( input );
const font = Font.create( buffer, ttfOptions );
// alias empty space glyphs
// If a font file doesn't include these empty glyphs, MS IE/Edge would display unknown .
let space = null;
const ttf = new TTF(font.get());
if ( opts.subset ) {
const spaceIndex = ttf.getGlyfIndexByCode(32);
if ( spaceIndex !== -1 ) {
space = ttf.getGlyfByIndex(spaceIndex);
if ( space && space.unicode && space.unicode.indexOf(32) !== -1 ) {
space.contours = [];
[ 10, 13 ].forEach( code => {
if ( space.unicode.indexOf(code) === -1 ) {
space.unicode.push(code);
function parseFont(fontObject) {
const pdfStreamInput = cpyCxtParser.parseNewObject(fontObject.getObjectID());
const fontDictionary = pdfStreamInput.toJSObject();
// See "Introduction to Font Data Structures" from PDF specification
if (fontDictionary.Subtype.value === 'Type0') {
// TODO: properly support composite fonts with multiple descendants
const descendant = cpyCxtParser
.parseNewObject(fontDictionary.DescendantFonts.toJSArray()[0].getObjectID())
.toJSObject();
if (descendant.Subtype.value == 'CIDFontType2') {
const descriptor = cpyCxtParser
.parseNewObject(descendant.FontDescriptor.getObjectID())
.toJSObject();
const id = descriptor.FontFile2.getObjectID();
const buffer = readStream(cpyCxtParser.parseNewObject(id));
const font = Font.create(buffer, { type: 'ttf', hinting: true });
// PDF font name does not contain sub family on Windows 10 so a more robust key
// is computed from the font metadata
const name = descriptor.FontName.value + ' - ' + fontMetadataKey(font.data.name);
if (context.pdfFonts[name]) {
const f = context.pdfFonts[name].font;
font.data.glyf.forEach((g, i) => {
if (g.contours && g.contours.length > 0) {
if (!f.data.glyf[i].contours || f.data.glyf[i].contours.length === 0) {
f.data.glyf[i] = g;
}
} else if (g.compound) {
if (typeof f.data.glyf[i].compound === 'undefined'
&& f.data.glyf[i].contours.length === 0) {
f.data.glyf[i] = g;
}
}
sliceGlyf({ type, subset }) {
this._hash = generateHash(subset.toString());
const { Font } = require("fonteditor-core");
this._font = Font.create(this._buffer, {
type,
subset
});
const defaultFontFamily = this._font.data.name.fontFamily;
this._ff = `${defaultFontFamily} ${this.getHash(6)}`;
this._font.data.name.fullName = this._ff;
this._font.data.name.fontFamily = this._ff;
return this;
}