WXL
10 天以前 2895b4ea66e09cb355aeb4e030ca0de297bf8ce3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
 * @licstart The following is the entire license notice for the
 * JavaScript code in this page
 *
 * Copyright 2022 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @licend The above is the entire license notice for the
 * JavaScript code in this page
 */
"use strict";
 
var _cmap = require("../../core/cmap.js");
 
var _test_utils = require("./test_utils.js");
 
var _api = require("../../display/api.js");
 
var _primitives = require("../../core/primitives.js");
 
var _stream = require("../../core/stream.js");
 
describe("cmap", function () {
  let fetchBuiltInCMap;
  beforeAll(function () {
    const CMapReaderFactory = new _api.DefaultCMapReaderFactory({
      baseUrl: _test_utils.CMAP_PARAMS.cMapUrl,
      isCompressed: _test_utils.CMAP_PARAMS.cMapPacked
    });
 
    fetchBuiltInCMap = function (name) {
      return CMapReaderFactory.fetch({
        name
      });
    };
  });
  afterAll(function () {
    fetchBuiltInCMap = null;
  });
  it("parses beginbfchar", async function () {
    const str = "2 beginbfchar\n" + "<03> <00>\n" + "<04> <01>\n" + "endbfchar\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    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", async function () {
    const str = "1 beginbfrange\n" + "<06> <0B> 0\n" + "endbfrange\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: 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();
  });
  it("parses beginbfrange with array", async function () {
    const str = "1 beginbfrange\n" + "<0D> <12> [ 0 1 2 3 4 5 ]\n" + "endbfrange\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    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", async function () {
    const str = "1 begincidchar\n" + "<14> 0\n" + "endcidchar\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    expect(cmap.lookup(0x14)).toEqual(0x00);
    expect(cmap.lookup(0x15)).toBeUndefined();
  });
  it("parses begincidrange", async function () {
    const str = "1 begincidrange\n" + "<0016> <001B>   0\n" + "endcidrange\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    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", async function () {
    const str = "1 begincodespacerange\n" + "<01> <02>\n" + "<00000003> <00000004>\n" + "endcodespacerange\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    const 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", async function () {
    const str = "1 begincodespacerange\n" + "<8EA1A1A1> <8EA1FEFE>\n" + "endcodespacerange\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    const c = {};
    cmap.readCharCode(String.fromCharCode(0x8e, 0xa1, 0xa1, 0xa1), 0, c);
    expect(c.charcode).toEqual(0x8ea1a1a1);
    expect(c.length).toEqual(4);
  });
  it("read usecmap", async function () {
    const str = "/Adobe-Japan1-1 usecmap\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream,
      fetchBuiltInCMap,
      useCMap: null
    });
    expect(cmap instanceof _cmap.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", async function () {
    const str = "/CMapName /Identity-H def\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    expect(cmap.name).toEqual("Identity-H");
  });
  it("parses wmode", async function () {
    const str = "/WMode 1 def\n";
    const stream = new _stream.StringStream(str);
    const cmap = await _cmap.CMapFactory.create({
      encoding: stream
    });
    expect(cmap.vertical).toEqual(true);
  });
  it("loads built in cmap", async function () {
    const cmap = await _cmap.CMapFactory.create({
      encoding: _primitives.Name.get("Adobe-Japan1-1"),
      fetchBuiltInCMap,
      useCMap: null
    });
    expect(cmap instanceof _cmap.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", async function () {
    const cmap = await _cmap.CMapFactory.create({
      encoding: _primitives.Name.get("Identity-H"),
      fetchBuiltInCMap,
      useCMap: null
    });
    expect(cmap instanceof _cmap.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"));
  });
  it("attempts to load a non-existent built-in CMap", async function () {
    try {
      await _cmap.CMapFactory.create({
        encoding: _primitives.Name.get("null"),
        fetchBuiltInCMap,
        useCMap: null
      });
      expect(false).toEqual(true);
    } catch (reason) {
      expect(reason instanceof Error).toEqual(true);
      expect(reason.message).toEqual("Unknown CMap name: null");
    }
  });
  it("attempts to load a built-in CMap without the necessary API parameters", async function () {
    function tmpFetchBuiltInCMap(name) {
      const CMapReaderFactory = new _api.DefaultCMapReaderFactory({});
      return CMapReaderFactory.fetch({
        name
      });
    }
 
    try {
      await _cmap.CMapFactory.create({
        encoding: _primitives.Name.get("Adobe-Japan1-1"),
        fetchBuiltInCMap: tmpFetchBuiltInCMap,
        useCMap: null
      });
      expect(false).toEqual(true);
    } catch (reason) {
      expect(reason instanceof Error).toEqual(true);
      expect(reason.message).toEqual('The CMap "baseUrl" parameter must be specified, ensure that ' + 'the "cMapUrl" and "cMapPacked" API parameters are provided.');
    }
  });
  it("attempts to load a built-in CMap with inconsistent API parameters", async function () {
    function tmpFetchBuiltInCMap(name) {
      const CMapReaderFactory = new _api.DefaultCMapReaderFactory({
        baseUrl: _test_utils.CMAP_PARAMS.cMapUrl,
        isCompressed: false
      });
      return CMapReaderFactory.fetch({
        name
      });
    }
 
    try {
      await _cmap.CMapFactory.create({
        encoding: _primitives.Name.get("Adobe-Japan1-1"),
        fetchBuiltInCMap: tmpFetchBuiltInCMap,
        useCMap: null
      });
      expect(false).toEqual(true);
    } catch (reason) {
      expect(reason instanceof Error).toEqual(true);
      const message = reason.message;
      expect(message.startsWith("Unable to load CMap at: ")).toEqual(true);
      expect(message.endsWith("/external/bcmaps/Adobe-Japan1-1")).toEqual(true);
    }
  });
});