1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 23:28:06 +02:00

Refactors CMapFactory.create to make it async

This commit is contained in:
Manas 2016-02-28 21:50:29 +05:30
parent c6d2b7f9d9
commit f6d28ca323
8 changed files with 559 additions and 473 deletions

View file

@ -6,17 +6,20 @@ describe('font_fpgm', function() {
it('table was truncated in the middle of functions', function() {
var output;
waitsFor(function() { return output; }, 10000);
var font = new Font("font", new Stream(font2324), {
loadedName: 'font',
type: 'CIDFontType2',
differences: [],
defaultEncoding: [],
cMap: CMapFactory.create(new Name('Identity-H'))
});
ttx(font.data, function(result) { output = result; });
runs(function() {
verifyTtxOutput(output);
expect(/(ENDF\[ \]|SVTCA\[0\])\s*<\/assembly>\s*<\/fpgm>/.test(output)).toEqual(true);
CMapFactory.create(new Name('Identity-H')).then(function (cMap) {
var font = new Font("font", new Stream(font2324), {
loadedName: 'font',
type: 'CIDFontType2',
differences: [],
defaultEncoding: [],
cMap: cMap,
toUnicode: new ToUnicodeMap([])
});
ttx(font.data, function(result) { output = result; });
runs(function() {
verifyTtxOutput(output);
expect(/(ENDF\[ \]|SVTCA\[0\])\s*<\/assembly>\s*<\/fpgm>/.test(output)).toEqual(true);
});
});
});
});

View file

@ -11,7 +11,8 @@ describe('font_post', function() {
loadedName: 'font',
type: 'TrueType',
differences: [],
defaultEncoding: []
defaultEncoding: [],
toUnicode: new ToUnicodeMap([])
});
ttx(font.data, function(result) { output = result; });
runs(function() {
@ -23,17 +24,20 @@ describe('font_post', function() {
it('has invalid selection attributes presence', function() {
var output;
waitsFor(function() { return output; }, 10000);
var font = new Font("font", new Stream(font1282), {
loadedName: 'font',
type: 'CIDFontType2',
differences: [],
defaultEncoding: [],
cMap: CMapFactory.create(new Name('Identity-H'))
});
ttx(font.data, function(result) { output = result; });
runs(function() {
verifyTtxOutput(output);
expect(/<OS_2>\s*<version value="3"\/>/.test(output)).toEqual(true);
CMapFactory.create(new Name('Identity-H')).then(function (cMap) {
var font = new Font("font", new Stream(font1282), {
loadedName: 'font',
type: 'CIDFontType2',
differences: [],
defaultEncoding: [],
cMap: cMap,
toUnicode: new ToUnicodeMap([])
});
ttx(font.data, function(result) { output = result; });
runs(function() {
verifyTtxOutput(output);
expect(/<OS_2>\s*<version value="3"\/>/.test(output)).toEqual(true);
});
});
});
});

View file

@ -8,17 +8,20 @@ describe('font_post', function() {
it('has invalid version number', function() {
var output;
waitsFor(function() { return output; }, 10000);
var font = new Font("font", new Stream(font2109), {
loadedName: 'font',
type: 'CIDFontType2',
differences: [],
defaultEncoding: [],
cMap: CMapFactory.create(new Name('Identity-H'))
});
ttx(font.data, function(result) { output = result; });
runs(function() {
verifyTtxOutput(output);
expect(/<post>\s*<formatType value="3\.0"\/>/.test(output)).toEqual(true);
CMapFactory.create(new Name('Identity-H')).then(function (cMap) {
var font = new Font("font", new Stream(font2109), {
loadedName: 'font',
type: 'CIDFontType2',
differences: [],
defaultEncoding: [],
cMap: cMap,
toUnicode: new ToUnicodeMap([])
});
ttx(font.data, function(result) { output = result; });
runs(function() {
verifyTtxOutput(output);
expect(/<post>\s*<formatType value="3\.0"\/>/.test(output)).toEqual(true);
});
});
});
@ -29,7 +32,8 @@ describe('font_post', function() {
loadedName: 'font',
type: 'TrueType',
differences: [],
defaultEncoding: []
defaultEncoding: [],
toUnicode: new ToUnicodeMap([])
});
ttx(font.data, function(result) { output = result; });
runs(function() {
@ -45,7 +49,8 @@ describe('font_post', function() {
loadedName: 'font',
type: 'TrueType',
differences: [],
defaultEncoding: []
defaultEncoding: [],
toUnicode: new ToUnicodeMap([])
});
ttx(font.data, function(result) { output = result; });
runs(function() {

View file

@ -27,6 +27,7 @@
'pdfjs/core/cmap'], function (fonts, stream, primitives, cmap) {
// Expose some of the PDFJS members to global scope for tests.
window.Font = fonts.Font;
window.ToUnicodeMap = fonts.ToUnicodeMap;
window.Stream = stream.Stream;
window.Name = primitives.Name;
window.CMapFactory = cmap.CMapFactory;

View file

@ -1,5 +1,5 @@
/* globals expect, it, describe, StringStream, CMapFactory, Name, CMap,
IdentityCMap */
IdentityCMap, waitsFor */
'use strict';
@ -7,58 +7,84 @@ var cMapUrl = '../../external/bcmaps/';
var cMapPacked = true;
describe('cmap', function() {
var TEST_TIMEOUT = 20000;
function waitsForPromiseResolved(promise, successCallback) {
var resolved = false;
promise.then(function(val) {
resolved = true;
successCallback(val);
},
function(error) {
// Shouldn't get here.
expect(error).toEqual('the promise should not have been rejected');
});
waitsFor(function() {
return resolved;
}, TEST_TIMEOUT);
}
it('parses beginbfchar', function() {
var str = '2 beginbfchar\n' +
'<03> <00>\n' +
'<04> <01>\n' +
'endbfchar\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
expect(cmap.lookup(0x05)).toBeUndefined();
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
expect(cmap.lookup(0x05)).toBeUndefined();
});
});
it('parses beginbfrange with range', function() {
var str = '1 beginbfrange\n' +
'<06> <0B> 0\n' +
'endbfrange\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.lookup(0x05)).toBeUndefined();
expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
expect(cmap.lookup(0x0B)).toEqual(String.fromCharCode(0x05));
expect(cmap.lookup(0x0C)).toBeUndefined();
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap.lookup(0x05)).toBeUndefined();
expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
expect(cmap.lookup(0x0B)).toEqual(String.fromCharCode(0x05));
expect(cmap.lookup(0x0C)).toBeUndefined();
});
});
it('parses beginbfrange with array', function() {
var str = '1 beginbfrange\n' +
'<0D> <12> [ 0 1 2 3 4 5 ]\n' +
'endbfrange\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.lookup(0x0C)).toBeUndefined();
expect(cmap.lookup(0x0D)).toEqual(0x00);
expect(cmap.lookup(0x12)).toEqual(0x05);
expect(cmap.lookup(0x13)).toBeUndefined();
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap.lookup(0x0C)).toBeUndefined();
expect(cmap.lookup(0x0D)).toEqual(0x00);
expect(cmap.lookup(0x12)).toEqual(0x05);
expect(cmap.lookup(0x13)).toBeUndefined();
});
});
it('parses begincidchar', function() {
var str = '1 begincidchar\n' +
'<14> 0\n' +
'endcidchar\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.lookup(0x14)).toEqual(0x00);
expect(cmap.lookup(0x15)).toBeUndefined();
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap.lookup(0x14)).toEqual(0x00);
expect(cmap.lookup(0x15)).toBeUndefined();
});
});
it('parses begincidrange', function() {
var str = '1 begincidrange\n' +
'<0016> <001B> 0\n' +
'endcidrange\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.lookup(0x15)).toBeUndefined();
expect(cmap.lookup(0x16)).toEqual(0x00);
expect(cmap.lookup(0x1B)).toEqual(0x05);
expect(cmap.lookup(0x1C)).toBeUndefined();
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap.lookup(0x15)).toBeUndefined();
expect(cmap.lookup(0x16)).toEqual(0x00);
expect(cmap.lookup(0x1B)).toEqual(0x05);
expect(cmap.lookup(0x1C)).toBeUndefined();
});
});
it('decodes codespace ranges', function() {
var str = '1 begincodespacerange\n' +
@ -66,65 +92,79 @@ describe('cmap', function() {
'<00000003> <00000004>\n' +
'endcodespacerange\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
var c = {};
cmap.readCharCode(String.fromCharCode(1), 0, c);
expect(c.charcode).toEqual(1);
expect(c.length).toEqual(1);
cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
expect(c.charcode).toEqual(3);
expect(c.length).toEqual(4);
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
var c = {};
cmap.readCharCode(String.fromCharCode(1), 0, c);
expect(c.charcode).toEqual(1);
expect(c.length).toEqual(1);
cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
expect(c.charcode).toEqual(3);
expect(c.length).toEqual(4);
});
});
it('decodes 4 byte codespace ranges', function() {
var str = '1 begincodespacerange\n' +
'<8EA1A1A1> <8EA1FEFE>\n' +
'endcodespacerange\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
var c = {};
cmap.readCharCode(String.fromCharCode(0x8E, 0xA1, 0xA1, 0xA1), 0, c);
expect(c.charcode).toEqual(0x8EA1A1A1);
expect(c.length).toEqual(4);
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
var c = {};
cmap.readCharCode(String.fromCharCode(0x8E, 0xA1, 0xA1, 0xA1), 0, c);
expect(c.charcode).toEqual(0x8EA1A1A1);
expect(c.length).toEqual(4);
});
});
it('read usecmap', function() {
var str = '/Adobe-Japan1-1 usecmap\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream,
var cmapPromise = CMapFactory.create(stream,
{ url: cMapUrl, packed: cMapPacked }, null);
expect(cmap instanceof CMap).toEqual(true);
expect(cmap.useCMap).not.toBeNull();
expect(cmap.builtInCMap).toBeFalsy();
expect(cmap.length).toEqual(0x20A7);
expect(cmap.isIdentityCMap).toEqual(false);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap instanceof CMap).toEqual(true);
expect(cmap.useCMap).not.toBeNull();
expect(cmap.builtInCMap).toBeFalsy();
expect(cmap.length).toEqual(0x20A7);
expect(cmap.isIdentityCMap).toEqual(false);
});
});
it('parses cmapname', function() {
var str = '/CMapName /Identity-H def\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.name).toEqual('Identity-H');
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap.name).toEqual('Identity-H');
});
});
it('parses wmode', function() {
var str = '/WMode 1 def\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.vertical).toEqual(true);
var cmapPromise = CMapFactory.create(stream);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap.vertical).toEqual(true);
});
});
it('loads built in cmap', function() {
var cmap = CMapFactory.create(new Name('Adobe-Japan1-1'),
var cmapPromise = CMapFactory.create(new Name('Adobe-Japan1-1'),
{ url: cMapUrl, packed: cMapPacked }, null);
expect(cmap instanceof CMap).toEqual(true);
expect(cmap.useCMap).toBeNull();
expect(cmap.builtInCMap).toBeTruthy();
expect(cmap.length).toEqual(0x20A7);
expect(cmap.isIdentityCMap).toEqual(false);
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap instanceof CMap).toEqual(true);
expect(cmap.useCMap).toBeNull();
expect(cmap.builtInCMap).toBeTruthy();
expect(cmap.length).toEqual(0x20A7);
expect(cmap.isIdentityCMap).toEqual(false);
});
});
it('loads built in identity cmap', function() {
var cmap = CMapFactory.create(new Name('Identity-H'),
var cmapPromise = CMapFactory.create(new Name('Identity-H'),
{ url: cMapUrl, packed: cMapPacked }, null);
expect(cmap instanceof IdentityCMap).toEqual(true);
expect(cmap.vertical).toEqual(false);
expect(cmap.length).toEqual(0x10000);
expect(function() { return cmap.isIdentityCMap; }).toThrow(
new Error('should not access .isIdentityCMap'));
waitsForPromiseResolved(cmapPromise, function (cmap) {
expect(cmap instanceof IdentityCMap).toEqual(true);
expect(cmap.vertical).toEqual(false);
expect(cmap.length).toEqual(0x10000);
expect(function() { return cmap.isIdentityCMap; }).toThrow(
new Error('should not access .isIdentityCMap'));
});
});
});