Skip to content

Commit 8098102

Browse files
Merge pull request #12532 from Snuffleupagus/refactor-font-tests
Modernize, and remove SystemJS usage from, the font-tests
2 parents 71a14be + 939af08 commit 8098102

10 files changed

+206
-159
lines changed

.eslintignore

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ test/tmp/
1313
test/features/
1414
test/pdfs/
1515
test/resources/
16-
test/font/*_spec.js
1716
*~/

test/font/.eslintrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": [
3+
"../.eslintrc"
4+
],
5+
6+
"rules": {
7+
// ECMAScript 6
8+
"no-var": "error",
9+
},
10+
}

test/font/font_core_spec.js

+10-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/font_fpgm_spec.js

+23-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/font_os2_spec.js

+30-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/font_post_spec.js

+49-29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/font/font_test.html

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55

66
<link rel="stylesheet" type="text/css" href="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
77

8-
<script src="../../node_modules/systemjs/dist/system.js"></script>
9-
<script src="../../systemjs.config.js"></script>
108
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
119
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
1210
<script src="../unit/testreporter.js"></script>
13-
<script src="jasmine-boot.js"></script>
1411

15-
<script src="fontutils.js"></script>
16-
17-
<!-- include spec files here... -->
18-
<script src="font_core_spec.js"></script>
19-
<script src="font_os2_spec.js"></script>
20-
<script src="font_post_spec.js"></script>
21-
<script src="font_fpgm_spec.js"></script>
12+
<script defer src="../../node_modules/es-module-shims/dist/es-module-shims.js"></script>
13+
<script type="importmap-shim">
14+
{
15+
"imports": {
16+
"pdfjs/": "../../src/",
17+
"pdfjs-lib": "../../src/pdf.js",
18+
"pdfjs-web/": "../../web/",
19+
"pdfjs-test/": "../"
20+
}
21+
}
22+
</script>
23+
<script src="jasmine-boot.js" type="module-shim"></script>
2224
</head>
2325
<body>
2426
</body>

test/font/fontutils.js

+20-23
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
"use strict";
18-
19-
var base64alphabet =
17+
const base64alphabet =
2018
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
2119

22-
// eslint-disable-next-line no-unused-vars
2320
function decodeFontData(base64) {
24-
var result = [];
21+
const result = [];
2522

26-
var bits = 0,
23+
let bits = 0,
2724
bitsLength = 0;
28-
for (var i = 0, ii = base64.length; i < ii; i++) {
29-
var ch = base64[i];
25+
for (let i = 0, ii = base64.length; i < ii; i++) {
26+
const ch = base64[i];
3027
if (ch <= " ") {
3128
continue;
3229
}
33-
var index = base64alphabet.indexOf(ch);
30+
const index = base64alphabet.indexOf(ch);
3431
if (index < 0) {
3532
throw new Error("Invalid character");
3633
}
@@ -41,24 +38,24 @@ function decodeFontData(base64) {
4138
bitsLength += 6;
4239
if (bitsLength >= 8) {
4340
bitsLength -= 8;
44-
var code = (bits >> bitsLength) & 0xff;
41+
const code = (bits >> bitsLength) & 0xff;
4542
result.push(code);
4643
}
4744
}
4845
return new Uint8Array(result);
4946
}
5047

5148
function encodeFontData(data) {
52-
var buffer = "";
53-
var i, n;
49+
let buffer = "";
50+
let i, n;
5451
for (i = 0, n = data.length; i < n; i += 3) {
55-
var b1 = data[i] & 0xff;
56-
var b2 = data[i + 1] & 0xff;
57-
var b3 = data[i + 2] & 0xff;
58-
var d1 = b1 >> 2,
52+
const b1 = data[i] & 0xff;
53+
const b2 = data[i + 1] & 0xff;
54+
const b3 = data[i + 2] & 0xff;
55+
const d1 = b1 >> 2,
5956
d2 = ((b1 & 3) << 4) | (b2 >> 4);
60-
var d3 = i + 1 < n ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64;
61-
var d4 = i + 2 < n ? b3 & 0x3f : 64;
57+
const d3 = i + 1 < n ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64;
58+
const d4 = i + 2 < n ? b3 & 0x3f : 64;
6259
buffer +=
6360
base64alphabet.charAt(d1) +
6461
base64alphabet.charAt(d2) +
@@ -68,12 +65,11 @@ function encodeFontData(data) {
6865
return buffer;
6966
}
7067

71-
// eslint-disable-next-line no-unused-vars
7268
function ttx(data, callback) {
73-
var xhr = new XMLHttpRequest();
69+
const xhr = new XMLHttpRequest();
7470
xhr.open("POST", "/ttx");
7571

76-
var encodedData = encodeFontData(data);
72+
const encodedData = encodeFontData(data);
7773
xhr.setRequestHeader("Content-type", "text/plain");
7874
xhr.setRequestHeader("Content-length", encodedData.length);
7975

@@ -89,10 +85,11 @@ function ttx(data, callback) {
8985
xhr.send(encodedData);
9086
}
9187

92-
// eslint-disable-next-line no-unused-vars
9388
function verifyTtxOutput(output) {
94-
var m = /^<error>(.*?)<\/error>/.exec(output);
89+
const m = /^<error>(.*?)<\/error>/.exec(output);
9590
if (m) {
9691
throw m[1];
9792
}
9893
}
94+
95+
export { decodeFontData, encodeFontData, ttx, verifyTtxOutput };

test/font/jasmine-boot.js

+37-43
Original file line numberDiff line numberDiff line change
@@ -40,63 +40,57 @@
4040

4141
"use strict";
4242

43-
function initializePDFJS(callback) {
44-
Promise.all([
45-
SystemJS.import("pdfjs/core/fonts.js"),
46-
SystemJS.import("pdfjs/core/stream.js"),
47-
SystemJS.import("pdfjs/core/primitives.js"),
48-
SystemJS.import("pdfjs/core/cmap.js"),
49-
]).then(function (modules) {
50-
var fonts = modules[0],
51-
stream = modules[1],
52-
primitives = modules[2],
53-
cmap = modules[3];
54-
// Expose some of the PDFJS members to global scope for tests.
55-
window.Font = fonts.Font;
56-
window.ToUnicodeMap = fonts.ToUnicodeMap;
57-
window.Stream = stream.Stream;
58-
window.Name = primitives.Name;
59-
window.CMapFactory = cmap.CMapFactory;
60-
61-
callback();
62-
});
43+
async function initializePDFJS(callback) {
44+
await Promise.all(
45+
[
46+
"pdfjs-test/font/font_core_spec.js",
47+
"pdfjs-test/font/font_os2_spec.js",
48+
"pdfjs-test/font/font_post_spec.js",
49+
"pdfjs-test/font/font_fpgm_spec.js",
50+
].map(function (moduleName) {
51+
// eslint-disable-next-line no-unsanitized/method
52+
return import(moduleName);
53+
})
54+
);
55+
56+
callback();
6357
}
6458

6559
(function () {
6660
window.jasmine = jasmineRequire.core(jasmineRequire);
6761

6862
jasmineRequire.html(jasmine);
6963

70-
var env = jasmine.getEnv();
64+
const env = jasmine.getEnv();
7165

72-
var jasmineInterface = jasmineRequire.interface(jasmine, env);
66+
const jasmineInterface = jasmineRequire.interface(jasmine, env);
7367
extend(window, jasmineInterface);
7468

7569
// Runner Parameters
76-
var queryString = new jasmine.QueryString({
70+
const queryString = new jasmine.QueryString({
7771
getWindowLocation() {
7872
return window.location;
7973
},
8074
});
8175

82-
var config = {
76+
const config = {
8377
failFast: queryString.getParam("failFast"),
8478
oneFailurePerSpec: queryString.getParam("oneFailurePerSpec"),
8579
hideDisabled: queryString.getParam("hideDisabled"),
8680
};
8781

88-
var random = queryString.getParam("random");
82+
const random = queryString.getParam("random");
8983
if (random !== undefined && random !== "") {
9084
config.random = random;
9185
}
9286

93-
var seed = queryString.getParam("seed");
87+
const seed = queryString.getParam("seed");
9488
if (seed) {
9589
config.seed = seed;
9690
}
9791

9892
// Reporters
99-
var htmlReporter = new jasmine.HtmlReporter({
93+
const htmlReporter = new jasmine.HtmlReporter({
10094
env,
10195
navigateWithNewParam(key, value) {
10296
return queryString.navigateWithNewParam(key, value);
@@ -119,13 +113,13 @@ function initializePDFJS(callback) {
119113
env.addReporter(htmlReporter);
120114

121115
if (queryString.getParam("browser")) {
122-
var testReporter = new TestReporter(queryString.getParam("browser"));
116+
const testReporter = new TestReporter(queryString.getParam("browser"));
123117
env.addReporter(testReporter);
124118
}
125119

126120
// Filter which specs will be run by matching the start of the full name
127121
// against the `spec` query param.
128-
var specFilter = new jasmine.HtmlSpecFilter({
122+
const specFilter = new jasmine.HtmlSpecFilter({
129123
filterString() {
130124
return queryString.getParam("spec");
131125
},
@@ -140,26 +134,26 @@ function initializePDFJS(callback) {
140134
// Sets longer timeout.
141135
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
142136

143-
// Replace the browser window's `onload`, ensure it's called, and then run
144-
// all of the loaded specs. This includes initializing the `HtmlReporter`
145-
// instance and then executing the loaded Jasmine environment.
146-
var currentWindowOnload = window.onload;
147-
148-
window.onload = function () {
149-
if (currentWindowOnload) {
150-
currentWindowOnload();
137+
function extend(destination, source) {
138+
for (const property in source) {
139+
destination[property] = source[property];
151140
}
141+
return destination;
142+
}
152143

144+
function fontTestInit() {
153145
initializePDFJS(function () {
154146
htmlReporter.initialize();
155147
env.execute();
156148
});
157-
};
149+
}
158150

159-
function extend(destination, source) {
160-
for (var property in source) {
161-
destination[property] = source[property];
162-
}
163-
return destination;
151+
if (
152+
document.readyState === "interactive" ||
153+
document.readyState === "complete"
154+
) {
155+
fontTestInit();
156+
} else {
157+
document.addEventListener("DOMContentLoaded", fontTestInit, true);
164158
}
165159
})();

test/font/ttxdriver.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,34 @@
1616

1717
"use strict";
1818

19-
var fs = require("fs");
20-
var path = require("path");
21-
var spawn = require("child_process").spawn;
19+
const fs = require("fs");
20+
const path = require("path");
21+
const spawn = require("child_process").spawn;
2222

23-
var ttxResourcesHome = path.join(__dirname, "..", "ttx");
23+
const ttxResourcesHome = path.join(__dirname, "..", "ttx");
2424

25-
var nextTTXTaskId = Date.now();
25+
let nextTTXTaskId = Date.now();
2626

2727
function runTtx(ttxResourcesHomePath, fontPath, registerOnCancel, callback) {
2828
fs.realpath(ttxResourcesHomePath, function (error, realTtxResourcesHomePath) {
29-
var fontToolsHome = path.join(realTtxResourcesHomePath, "fonttools-code");
29+
const fontToolsHome = path.join(realTtxResourcesHomePath, "fonttools-code");
3030
fs.realpath(fontPath, function (errorFontPath, realFontPath) {
31-
var ttxPath = path.join("Tools", "ttx");
31+
const ttxPath = path.join("Tools", "ttx");
3232
if (!fs.existsSync(path.join(fontToolsHome, ttxPath))) {
3333
callback("TTX was not found, please checkout PDF.js submodules");
3434
return;
3535
}
36-
var ttxEnv = {
36+
const ttxEnv = {
3737
PYTHONPATH: path.join(fontToolsHome, "Lib"),
3838
PYTHONDONTWRITEBYTECODE: true,
3939
};
40-
var ttxStdioMode = "ignore";
41-
var ttx = spawn("python", [ttxPath, realFontPath], {
40+
const ttxStdioMode = "ignore";
41+
const ttx = spawn("python", [ttxPath, realFontPath], {
4242
cwd: fontToolsHome,
4343
stdio: ttxStdioMode,
4444
env: ttxEnv,
4545
});
46-
var ttxRunError;
46+
let ttxRunError;
4747
registerOnCancel(function (reason) {
4848
ttxRunError = reason;
4949
callback(reason);
@@ -68,10 +68,10 @@ exports.translateFont = function translateFont(
6868
registerOnCancel,
6969
callback
7070
) {
71-
var buffer = Buffer.from(content, "base64");
72-
var taskId = (nextTTXTaskId++).toString();
73-
var fontPath = path.join(ttxResourcesHome, taskId + ".otf");
74-
var resultPath = path.join(ttxResourcesHome, taskId + ".ttx");
71+
const buffer = Buffer.from(content, "base64");
72+
const taskId = (nextTTXTaskId++).toString();
73+
const fontPath = path.join(ttxResourcesHome, taskId + ".otf");
74+
const resultPath = path.join(ttxResourcesHome, taskId + ".ttx");
7575

7676
fs.writeFileSync(fontPath, buffer);
7777
runTtx(ttxResourcesHome, fontPath, registerOnCancel, function (err) {

0 commit comments

Comments
 (0)