UNPKG

11.1 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22'use strict';
23
24var _cmap = require('../../core/cmap');
25
26var _dom_utils = require('../../display/dom_utils');
27
28var _is_node = require('../../shared/is_node');
29
30var _is_node2 = _interopRequireDefault(_is_node);
31
32var _primitives = require('../../core/primitives');
33
34var _test_utils = require('./test_utils');
35
36var _stream = require('../../core/stream');
37
38function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
40var cMapUrl = {
41 dom: '../../external/bcmaps/',
42 node: './external/bcmaps/'
43};
44var cMapPacked = true;
45describe('cmap', function () {
46 var fetchBuiltInCMap;
47 beforeAll(function (done) {
48 var CMapReaderFactory;
49 if ((0, _is_node2.default)()) {
50 CMapReaderFactory = new _test_utils.NodeCMapReaderFactory({
51 baseUrl: cMapUrl.node,
52 isCompressed: cMapPacked
53 });
54 } else {
55 CMapReaderFactory = new _dom_utils.DOMCMapReaderFactory({
56 baseUrl: cMapUrl.dom,
57 isCompressed: cMapPacked
58 });
59 }
60 fetchBuiltInCMap = function fetchBuiltInCMap(name) {
61 return CMapReaderFactory.fetch({ name: name });
62 };
63 done();
64 });
65 afterAll(function () {
66 fetchBuiltInCMap = null;
67 });
68 it('parses beginbfchar', function (done) {
69 var str = '2 beginbfchar\n' + '<03> <00>\n' + '<04> <01>\n' + 'endbfchar\n';
70 var stream = new _stream.StringStream(str);
71 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
72 cmapPromise.then(function (cmap) {
73 expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
74 expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
75 expect(cmap.lookup(0x05)).toBeUndefined();
76 done();
77 }).catch(function (reason) {
78 done.fail(reason);
79 });
80 });
81 it('parses beginbfrange with range', function (done) {
82 var str = '1 beginbfrange\n' + '<06> <0B> 0\n' + 'endbfrange\n';
83 var stream = new _stream.StringStream(str);
84 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
85 cmapPromise.then(function (cmap) {
86 expect(cmap.lookup(0x05)).toBeUndefined();
87 expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
88 expect(cmap.lookup(0x0B)).toEqual(String.fromCharCode(0x05));
89 expect(cmap.lookup(0x0C)).toBeUndefined();
90 done();
91 }).catch(function (reason) {
92 done.fail(reason);
93 });
94 });
95 it('parses beginbfrange with array', function (done) {
96 var str = '1 beginbfrange\n' + '<0D> <12> [ 0 1 2 3 4 5 ]\n' + 'endbfrange\n';
97 var stream = new _stream.StringStream(str);
98 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
99 cmapPromise.then(function (cmap) {
100 expect(cmap.lookup(0x0C)).toBeUndefined();
101 expect(cmap.lookup(0x0D)).toEqual(0x00);
102 expect(cmap.lookup(0x12)).toEqual(0x05);
103 expect(cmap.lookup(0x13)).toBeUndefined();
104 done();
105 }).catch(function (reason) {
106 done.fail(reason);
107 });
108 });
109 it('parses begincidchar', function (done) {
110 var str = '1 begincidchar\n' + '<14> 0\n' + 'endcidchar\n';
111 var stream = new _stream.StringStream(str);
112 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
113 cmapPromise.then(function (cmap) {
114 expect(cmap.lookup(0x14)).toEqual(0x00);
115 expect(cmap.lookup(0x15)).toBeUndefined();
116 done();
117 }).catch(function (reason) {
118 done.fail(reason);
119 });
120 });
121 it('parses begincidrange', function (done) {
122 var str = '1 begincidrange\n' + '<0016> <001B> 0\n' + 'endcidrange\n';
123 var stream = new _stream.StringStream(str);
124 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
125 cmapPromise.then(function (cmap) {
126 expect(cmap.lookup(0x15)).toBeUndefined();
127 expect(cmap.lookup(0x16)).toEqual(0x00);
128 expect(cmap.lookup(0x1B)).toEqual(0x05);
129 expect(cmap.lookup(0x1C)).toBeUndefined();
130 done();
131 }).catch(function (reason) {
132 done.fail(reason);
133 });
134 });
135 it('decodes codespace ranges', function (done) {
136 var str = '1 begincodespacerange\n' + '<01> <02>\n' + '<00000003> <00000004>\n' + 'endcodespacerange\n';
137 var stream = new _stream.StringStream(str);
138 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
139 cmapPromise.then(function (cmap) {
140 var c = {};
141 cmap.readCharCode(String.fromCharCode(1), 0, c);
142 expect(c.charcode).toEqual(1);
143 expect(c.length).toEqual(1);
144 cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
145 expect(c.charcode).toEqual(3);
146 expect(c.length).toEqual(4);
147 done();
148 }).catch(function (reason) {
149 done.fail(reason);
150 });
151 });
152 it('decodes 4 byte codespace ranges', function (done) {
153 var str = '1 begincodespacerange\n' + '<8EA1A1A1> <8EA1FEFE>\n' + 'endcodespacerange\n';
154 var stream = new _stream.StringStream(str);
155 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
156 cmapPromise.then(function (cmap) {
157 var c = {};
158 cmap.readCharCode(String.fromCharCode(0x8E, 0xA1, 0xA1, 0xA1), 0, c);
159 expect(c.charcode).toEqual(0x8EA1A1A1);
160 expect(c.length).toEqual(4);
161 done();
162 }).catch(function (reason) {
163 done.fail(reason);
164 });
165 });
166 it('read usecmap', function (done) {
167 var str = '/Adobe-Japan1-1 usecmap\n';
168 var stream = new _stream.StringStream(str);
169 var cmapPromise = _cmap.CMapFactory.create({
170 encoding: stream,
171 fetchBuiltInCMap: fetchBuiltInCMap,
172 useCMap: null
173 });
174 cmapPromise.then(function (cmap) {
175 expect(cmap instanceof _cmap.CMap).toEqual(true);
176 expect(cmap.useCMap).not.toBeNull();
177 expect(cmap.builtInCMap).toBeFalsy();
178 expect(cmap.length).toEqual(0x20A7);
179 expect(cmap.isIdentityCMap).toEqual(false);
180 done();
181 }).catch(function (reason) {
182 done.fail(reason);
183 });
184 });
185 it('parses cmapname', function (done) {
186 var str = '/CMapName /Identity-H def\n';
187 var stream = new _stream.StringStream(str);
188 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
189 cmapPromise.then(function (cmap) {
190 expect(cmap.name).toEqual('Identity-H');
191 done();
192 }).catch(function (reason) {
193 done.fail(reason);
194 });
195 });
196 it('parses wmode', function (done) {
197 var str = '/WMode 1 def\n';
198 var stream = new _stream.StringStream(str);
199 var cmapPromise = _cmap.CMapFactory.create({ encoding: stream });
200 cmapPromise.then(function (cmap) {
201 expect(cmap.vertical).toEqual(true);
202 done();
203 }).catch(function (reason) {
204 done.fail(reason);
205 });
206 });
207 it('loads built in cmap', function (done) {
208 var cmapPromise = _cmap.CMapFactory.create({
209 encoding: _primitives.Name.get('Adobe-Japan1-1'),
210 fetchBuiltInCMap: fetchBuiltInCMap,
211 useCMap: null
212 });
213 cmapPromise.then(function (cmap) {
214 expect(cmap instanceof _cmap.CMap).toEqual(true);
215 expect(cmap.useCMap).toBeNull();
216 expect(cmap.builtInCMap).toBeTruthy();
217 expect(cmap.length).toEqual(0x20A7);
218 expect(cmap.isIdentityCMap).toEqual(false);
219 done();
220 }).catch(function (reason) {
221 done.fail(reason);
222 });
223 });
224 it('loads built in identity cmap', function (done) {
225 var cmapPromise = _cmap.CMapFactory.create({
226 encoding: _primitives.Name.get('Identity-H'),
227 fetchBuiltInCMap: fetchBuiltInCMap,
228 useCMap: null
229 });
230 cmapPromise.then(function (cmap) {
231 expect(cmap instanceof _cmap.IdentityCMap).toEqual(true);
232 expect(cmap.vertical).toEqual(false);
233 expect(cmap.length).toEqual(0x10000);
234 expect(function () {
235 return cmap.isIdentityCMap;
236 }).toThrow(new Error('should not access .isIdentityCMap'));
237 done();
238 }).catch(function (reason) {
239 done.fail(reason);
240 });
241 });
242 it('attempts to load a non-existent built-in CMap', function (done) {
243 var cmapPromise = _cmap.CMapFactory.create({
244 encoding: _primitives.Name.get('null'),
245 fetchBuiltInCMap: fetchBuiltInCMap,
246 useCMap: null
247 });
248 cmapPromise.then(function () {
249 done.fail('No CMap should be loaded');
250 }, function (reason) {
251 expect(reason instanceof Error).toEqual(true);
252 expect(reason.message).toEqual('Unknown CMap name: null');
253 done();
254 });
255 });
256 it('attempts to load a built-in CMap without the necessary API parameters', function (done) {
257 function tmpFetchBuiltInCMap(name) {
258 var CMapReaderFactory = (0, _is_node2.default)() ? new _test_utils.NodeCMapReaderFactory({}) : new _dom_utils.DOMCMapReaderFactory({});
259 return CMapReaderFactory.fetch({ name: name });
260 }
261 var cmapPromise = _cmap.CMapFactory.create({
262 encoding: _primitives.Name.get('Adobe-Japan1-1'),
263 fetchBuiltInCMap: tmpFetchBuiltInCMap,
264 useCMap: null
265 });
266 cmapPromise.then(function () {
267 done.fail('No CMap should be loaded');
268 }, function (reason) {
269 expect(reason instanceof Error).toEqual(true);
270 expect(reason.message).toEqual('The CMap "baseUrl" parameter must be specified, ensure that ' + 'the "cMapUrl" and "cMapPacked" API parameters are provided.');
271 done();
272 });
273 });
274 it('attempts to load a built-in CMap with inconsistent API parameters', function (done) {
275 function tmpFetchBuiltInCMap(name) {
276 var CMapReaderFactory = void 0;
277 if ((0, _is_node2.default)()) {
278 CMapReaderFactory = new _test_utils.NodeCMapReaderFactory({
279 baseUrl: cMapUrl.node,
280 isCompressed: false
281 });
282 } else {
283 CMapReaderFactory = new _dom_utils.DOMCMapReaderFactory({
284 baseUrl: cMapUrl.dom,
285 isCompressed: false
286 });
287 }
288 return CMapReaderFactory.fetch({ name: name });
289 }
290 var cmapPromise = _cmap.CMapFactory.create({
291 encoding: _primitives.Name.get('Adobe-Japan1-1'),
292 fetchBuiltInCMap: tmpFetchBuiltInCMap,
293 useCMap: null
294 });
295 cmapPromise.then(function () {
296 done.fail('No CMap should be loaded');
297 }, function (reason) {
298 expect(reason instanceof Error).toEqual(true);
299 var message = reason.message;
300 expect(message.startsWith('Unable to load CMap at: ')).toEqual(true);
301 expect(message.endsWith('/external/bcmaps/Adobe-Japan1-1')).toEqual(true);
302 done();
303 });
304 });
305});
\No newline at end of file