diff --git a/tests/unit/font.spec.js b/tests/unit/font.spec.js index 67241554..33e19d6a 100644 --- a/tests/unit/font.spec.js +++ b/tests/unit/font.spec.js @@ -1,5 +1,6 @@ -import PDFFontFactory from '../../lib/font_factory'; import PDFDocument from '../../lib/document'; +import PDFFontFactory from '../../lib/font_factory'; +import { logData } from './helpers'; describe('EmbeddedFont', () => { test('no fontLayoutCache option', () => { @@ -52,4 +53,46 @@ describe('EmbeddedFont', () => { expect(dictionary.data.BaseFont).toBe('BAJJZZ+Roboto-Regular'); }); }); + + describe.only('toUnicodeMap', () => { + test('bfrange lines should not cross highcode boundary', () => { + const doc = new PDFDocument({ compress: false }); + const font = PDFFontFactory.open( + doc, + 'tests/fonts/Roboto-Regular.ttf', + undefined, + 'F1099' + ); + + // 398 different glyphs + font.encode('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); + font.encode('abcdefghijklmnopqrstuvwxyz'); + font.encode('ÁÀÂÄÅÃÆÇÐÉÈÊËÍÌÎÏÑÓÒÔÖÕØŒÞÚÙÛÜÝŸ'); + font.encode('áàâäãåæçðéèêëíìîïıñóòôöõøœßþúùûüýÿ'); + font.encode('ĀĂĄĆČĎĐĒĖĘĚĞĢĪĮİĶŁĹĻĽŃŅŇŌŐŔŖŘŠŚŞȘŢȚŤŪŮŰŲŽŹŻ'); + font.encode('āăąćčďđēėęěğģīįķłĺļľńņňōőŕŗřšśşșţțťūůűųžźż'); + font.encode('ΑΒΓ∆ΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩΆΈΉΊΌΎΏΪΫ'); + font.encode('αβγδεζηθικλµνξοπρςστυφχψωάέήίόύώϊϋΐΰ'); + font.encode('АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'); + font.encode('абвгдежзийклмнопрстуфхцчшщъыьэюя'); + font.encode('ЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏҐӁҒҖҚҢҮҰҲҶҺӘӢӨӮ'); + font.encode('ѐёђѓєѕіїјљњћќѝўџґӂғҗқңүұҳҷһәӣөӯ'); + + const docData = logData(doc); + font.toUnicodeCmap(); + const text = docData.map((d) => d.toString("utf8")).join(""); + + let glyphs = 0 + for (const block of text.matchAll(/beginbfrange\n((?:.|\n)*?)\nendbfrange/g)) { + for (const line of block[1].matchAll(/^<([0-9a-f]+)>\s+<([0-9a-f]+)>\s+\[/igm)) { + const low = parseInt(line[1], 16); + const high = parseInt(line[2], 16); + glyphs += high - low + 1; + expect(high & 0xFFFFFF00).toBe(low & 0xFFFFFF00); + } + } + + expect(glyphs).toBe(398 + 1); + }); + }); });