@pdfme/pdf-lib
Version:
Create and modify PDF files with JavaScript
88 lines • 3.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const CustomFontEmbedder_1 = __importDefault(require("./CustomFontEmbedder"));
const PDFHexString_1 = __importDefault(require("../objects/PDFHexString"));
const utils_1 = require("../../utils");
/**
* A note of thanks to the developers of https://github.com/foliojs/pdfkit, as
* this class borrows from:
* https://github.com/devongovett/pdfkit/blob/e71edab0dd4657b5a767804ba86c94c58d01fbca/lib/image/jpeg.coffee
*/
class CustomFontSubsetEmbedder extends CustomFontEmbedder_1.default {
static async for(fontkit, fontData, customFontName, fontFeatures) {
const font = await fontkit.create(fontData);
return new CustomFontSubsetEmbedder(font, fontData, customFontName, fontFeatures);
}
constructor(font, fontData, customFontName, fontFeatures) {
super(font, fontData, customFontName, fontFeatures);
Object.defineProperty(this, "subset", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "glyphs", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "glyphIdMap", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.subset = this.font.createSubset();
this.glyphs = [];
this.glyphCache = utils_1.Cache.populatedBy(() => this.glyphs);
this.glyphIdMap = new Map();
}
encodeText(text) {
const { glyphs } = this.font.layout(text, this.fontFeatures);
const hexCodes = new Array(glyphs.length);
for (let idx = 0, len = glyphs.length; idx < len; idx++) {
const glyph = glyphs[idx];
const subsetGlyphId = this.subset.includeGlyph(glyph);
this.glyphs[subsetGlyphId - 1] = glyph;
this.glyphIdMap.set(glyph.id, subsetGlyphId);
hexCodes[idx] = (0, utils_1.toHexStringOfMinLength)(subsetGlyphId, 4);
}
this.glyphCache.invalidate();
return PDFHexString_1.default.of(hexCodes.join(''));
}
isCFF() {
return this.subset.cff;
}
glyphId(glyph) {
return glyph ? this.glyphIdMap.get(glyph.id) : -1;
}
serializeFont() {
return new Promise((resolve, reject) => {
if ('encodeStream' in this.subset) {
const parts = [];
this.subset
.encodeStream()
.on('data', (bytes) => parts.push(bytes))
.on('end', () => resolve((0, utils_1.mergeUint8Arrays)(parts)))
.on('error', (err) => reject(err));
}
else if ('encode' in this.subset) {
try {
resolve(this.subset.encode());
}
catch (err) {
reject(err);
}
}
else {
reject(new Error('Subset does not have an encode method'));
}
});
}
}
exports.default = CustomFontSubsetEmbedder;
//# sourceMappingURL=CustomFontSubsetEmbedder.js.map