cmap_spec.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 coreCMap = require('../../core/cmap.js');
  17. var corePrimitives = require('../../core/primitives.js');
  18. var coreStream = require('../../core/stream.js');
  19. var displayDOMUtils = require('../../display/dom_utils.js');
  20. var sharedUtil = require('../../shared/util.js');
  21. var testUnitTestUtils = require('./test_utils.js');
  22. var CMapFactory = coreCMap.CMapFactory;
  23. var CMap = coreCMap.CMap;
  24. var IdentityCMap = coreCMap.IdentityCMap;
  25. var Name = corePrimitives.Name;
  26. var StringStream = coreStream.StringStream;
  27. var DOMCMapReaderFactory = displayDOMUtils.DOMCMapReaderFactory;
  28. var isNodeJS = sharedUtil.isNodeJS;
  29. var NodeCMapReaderFactory = testUnitTestUtils.NodeCMapReaderFactory;
  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 (isNodeJS()) {
  40. CMapReaderFactory = new NodeCMapReaderFactory({
  41. baseUrl: cMapUrl.node,
  42. isCompressed: cMapPacked
  43. });
  44. } else {
  45. CMapReaderFactory = new DOMCMapReaderFactory({
  46. baseUrl: cMapUrl.dom,
  47. isCompressed: cMapPacked
  48. });
  49. }
  50. fetchBuiltInCMap = function (name) {
  51. return CMapReaderFactory.fetch({ name: name });
  52. };
  53. done();
  54. });
  55. afterAll(function () {
  56. fetchBuiltInCMap = null;
  57. });
  58. it('parses beginbfchar', function (done) {
  59. var str = '2 beginbfchar\n' + '<03> <00>\n' + '<04> <01>\n' + 'endbfchar\n';
  60. var stream = new StringStream(str);
  61. var cmapPromise = CMapFactory.create({ encoding: stream });
  62. cmapPromise.then(function (cmap) {
  63. expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
  64. expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
  65. expect(cmap.lookup(0x05)).toBeUndefined();
  66. done();
  67. }).catch(function (reason) {
  68. done.fail(reason);
  69. });
  70. });
  71. it('parses beginbfrange with range', function (done) {
  72. var str = '1 beginbfrange\n' + '<06> <0B> 0\n' + 'endbfrange\n';
  73. var stream = new StringStream(str);
  74. var cmapPromise = CMapFactory.create({ encoding: stream });
  75. cmapPromise.then(function (cmap) {
  76. expect(cmap.lookup(0x05)).toBeUndefined();
  77. expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
  78. expect(cmap.lookup(0x0B)).toEqual(String.fromCharCode(0x05));
  79. expect(cmap.lookup(0x0C)).toBeUndefined();
  80. done();
  81. }).catch(function (reason) {
  82. done.fail(reason);
  83. });
  84. });
  85. it('parses beginbfrange with array', function (done) {
  86. var str = '1 beginbfrange\n' + '<0D> <12> [ 0 1 2 3 4 5 ]\n' + 'endbfrange\n';
  87. var stream = new StringStream(str);
  88. var cmapPromise = CMapFactory.create({ encoding: stream });
  89. cmapPromise.then(function (cmap) {
  90. expect(cmap.lookup(0x0C)).toBeUndefined();
  91. expect(cmap.lookup(0x0D)).toEqual(0x00);
  92. expect(cmap.lookup(0x12)).toEqual(0x05);
  93. expect(cmap.lookup(0x13)).toBeUndefined();
  94. done();
  95. }).catch(function (reason) {
  96. done.fail(reason);
  97. });
  98. });
  99. it('parses begincidchar', function (done) {
  100. var str = '1 begincidchar\n' + '<14> 0\n' + 'endcidchar\n';
  101. var stream = new StringStream(str);
  102. var cmapPromise = CMapFactory.create({ encoding: stream });
  103. cmapPromise.then(function (cmap) {
  104. expect(cmap.lookup(0x14)).toEqual(0x00);
  105. expect(cmap.lookup(0x15)).toBeUndefined();
  106. done();
  107. }).catch(function (reason) {
  108. done.fail(reason);
  109. });
  110. });
  111. it('parses begincidrange', function (done) {
  112. var str = '1 begincidrange\n' + '<0016> <001B> 0\n' + 'endcidrange\n';
  113. var stream = new StringStream(str);
  114. var cmapPromise = CMapFactory.create({ encoding: stream });
  115. cmapPromise.then(function (cmap) {
  116. expect(cmap.lookup(0x15)).toBeUndefined();
  117. expect(cmap.lookup(0x16)).toEqual(0x00);
  118. expect(cmap.lookup(0x1B)).toEqual(0x05);
  119. expect(cmap.lookup(0x1C)).toBeUndefined();
  120. done();
  121. }).catch(function (reason) {
  122. done.fail(reason);
  123. });
  124. });
  125. it('decodes codespace ranges', function (done) {
  126. var str = '1 begincodespacerange\n' + '<01> <02>\n' + '<00000003> <00000004>\n' + 'endcodespacerange\n';
  127. var stream = new StringStream(str);
  128. var cmapPromise = CMapFactory.create({ encoding: stream });
  129. cmapPromise.then(function (cmap) {
  130. var c = {};
  131. cmap.readCharCode(String.fromCharCode(1), 0, c);
  132. expect(c.charcode).toEqual(1);
  133. expect(c.length).toEqual(1);
  134. cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
  135. expect(c.charcode).toEqual(3);
  136. expect(c.length).toEqual(4);
  137. done();
  138. }).catch(function (reason) {
  139. done.fail(reason);
  140. });
  141. });
  142. it('decodes 4 byte codespace ranges', function (done) {
  143. var str = '1 begincodespacerange\n' + '<8EA1A1A1> <8EA1FEFE>\n' + 'endcodespacerange\n';
  144. var stream = new StringStream(str);
  145. var cmapPromise = CMapFactory.create({ encoding: stream });
  146. cmapPromise.then(function (cmap) {
  147. var c = {};
  148. cmap.readCharCode(String.fromCharCode(0x8E, 0xA1, 0xA1, 0xA1), 0, c);
  149. expect(c.charcode).toEqual(0x8EA1A1A1);
  150. expect(c.length).toEqual(4);
  151. done();
  152. }).catch(function (reason) {
  153. done.fail(reason);
  154. });
  155. });
  156. it('read usecmap', function (done) {
  157. var str = '/Adobe-Japan1-1 usecmap\n';
  158. var stream = new StringStream(str);
  159. var cmapPromise = CMapFactory.create({
  160. encoding: stream,
  161. fetchBuiltInCMap: fetchBuiltInCMap,
  162. useCMap: null
  163. });
  164. cmapPromise.then(function (cmap) {
  165. expect(cmap instanceof CMap).toEqual(true);
  166. expect(cmap.useCMap).not.toBeNull();
  167. expect(cmap.builtInCMap).toBeFalsy();
  168. expect(cmap.length).toEqual(0x20A7);
  169. expect(cmap.isIdentityCMap).toEqual(false);
  170. done();
  171. }).catch(function (reason) {
  172. done.fail(reason);
  173. });
  174. });
  175. it('parses cmapname', function (done) {
  176. var str = '/CMapName /Identity-H def\n';
  177. var stream = new StringStream(str);
  178. var cmapPromise = CMapFactory.create({ encoding: stream });
  179. cmapPromise.then(function (cmap) {
  180. expect(cmap.name).toEqual('Identity-H');
  181. done();
  182. }).catch(function (reason) {
  183. done.fail(reason);
  184. });
  185. });
  186. it('parses wmode', function (done) {
  187. var str = '/WMode 1 def\n';
  188. var stream = new StringStream(str);
  189. var cmapPromise = CMapFactory.create({ encoding: stream });
  190. cmapPromise.then(function (cmap) {
  191. expect(cmap.vertical).toEqual(true);
  192. done();
  193. }).catch(function (reason) {
  194. done.fail(reason);
  195. });
  196. });
  197. it('loads built in cmap', function (done) {
  198. var cmapPromise = CMapFactory.create({
  199. encoding: Name.get('Adobe-Japan1-1'),
  200. fetchBuiltInCMap: fetchBuiltInCMap,
  201. useCMap: null
  202. });
  203. cmapPromise.then(function (cmap) {
  204. expect(cmap instanceof CMap).toEqual(true);
  205. expect(cmap.useCMap).toBeNull();
  206. expect(cmap.builtInCMap).toBeTruthy();
  207. expect(cmap.length).toEqual(0x20A7);
  208. expect(cmap.isIdentityCMap).toEqual(false);
  209. done();
  210. }).catch(function (reason) {
  211. done.fail(reason);
  212. });
  213. });
  214. it('loads built in identity cmap', function (done) {
  215. var cmapPromise = CMapFactory.create({
  216. encoding: Name.get('Identity-H'),
  217. fetchBuiltInCMap: fetchBuiltInCMap,
  218. useCMap: null
  219. });
  220. cmapPromise.then(function (cmap) {
  221. expect(cmap instanceof IdentityCMap).toEqual(true);
  222. expect(cmap.vertical).toEqual(false);
  223. expect(cmap.length).toEqual(0x10000);
  224. expect(function () {
  225. return cmap.isIdentityCMap;
  226. }).toThrow(new Error('should not access .isIdentityCMap'));
  227. done();
  228. }).catch(function (reason) {
  229. done.fail(reason);
  230. });
  231. });
  232. });