2
0

cmap_spec.js 11 KB

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