UNPKG

@pdfme/pdf-lib

Version:

Create and modify PDF files with JavaScript

39 lines 2.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fontkit_1 = __importDefault(require("@pdf-lib/fontkit")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const CMap_1 = require("../../../src/core/embedders/CMap"); const utils_1 = require("../../../src/utils"); const ASSETS_DIR = path_1.default.resolve(__dirname, '../../../assets/fonts'); const ubuntuFont = fs_1.default.readFileSync(path_1.default.join(ASSETS_DIR, 'ubuntu/Ubuntu-R.ttf')); const sourceHansJpFont = fs_1.default.readFileSync(path_1.default.join(ASSETS_DIR, 'source_hans_jp/SourceHanSerifJP-Regular.otf')); const DATA_DIR = path_1.default.join(__dirname, 'data'); const ubuntuFontCmap = fs_1.default.readFileSync(path_1.default.join(DATA_DIR, 'Ubuntu-R.ttf.cmap')); const sourceHansJpFontCmap = fs_1.default.readFileSync(path_1.default.join(DATA_DIR, 'SourceHanSerifJP-Regular.otf.cmap')); const allGlyphsInFontSortedById = (font) => { const glyphs = new Array(font.characterSet.length); for (let idx = 0, len = glyphs.length; idx < len; idx++) { const codePoint = font.characterSet[idx]; glyphs[idx] = font.glyphForCodePoint(codePoint); } return (0, utils_1.sortedUniq)(glyphs.sort(utils_1.byAscendingId), (g) => g.id); }; describe(`createCmap`, () => { it(`creates CMaps for embedded Ubuntu-R font files`, () => { const font = fontkit_1.default.create(ubuntuFont); const glyphs = allGlyphsInFontSortedById(font); const cmap = (0, CMap_1.createCmap)(glyphs, (g) => (g ? g.id : -1)); expect(cmap).toEqual(String(ubuntuFontCmap)); }); it(`creates CMaps for embedded SourceHanSerifJP-Regular font files`, () => { const font = fontkit_1.default.create(sourceHansJpFont); const glyphs = allGlyphsInFontSortedById(font); const cmap = (0, CMap_1.createCmap)(glyphs, (g) => (g ? g.id : -1)); expect(cmap).toEqual(String(sourceHansJpFontCmap)); }); }); //# sourceMappingURL=CMap.spec.js.map