cmap_spec.js 10 KB

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