Skip to content

Commit

Permalink
Add unit test for bfrange lines in toUnicodeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
orzFly committed Feb 26, 2024
1 parent ba09658 commit 6784de0
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion tests/unit/font.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 6784de0

Please sign in to comment.