Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize, and remove SystemJS usage from, the font-tests #12532

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ test/tmp/
test/features/
test/pdfs/
test/resources/
test/font/*_spec.js
*~/
10 changes: 10 additions & 0 deletions test/font/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": [
"../.eslintrc"
],

"rules": {
// ECMAScript 6
"no-var": "error",
},
}
16 changes: 10 additions & 6 deletions test/font/font_core_spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 23 additions & 13 deletions test/font/font_fpgm_spec.js

Large diffs are not rendered by default.

49 changes: 30 additions & 19 deletions test/font/font_os2_spec.js

Large diffs are not rendered by default.

78 changes: 49 additions & 29 deletions test/font/font_post_spec.js

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions test/font/font_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

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

<script src="../../node_modules/systemjs/dist/system.js"></script>
<script src="../../systemjs.config.js"></script>
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="../unit/testreporter.js"></script>
<script src="jasmine-boot.js"></script>

<script src="fontutils.js"></script>

<!-- include spec files here... -->
<script src="font_core_spec.js"></script>
<script src="font_os2_spec.js"></script>
<script src="font_post_spec.js"></script>
<script src="font_fpgm_spec.js"></script>
<script defer src="../../node_modules/es-module-shims/dist/es-module-shims.js"></script>
<script type="importmap-shim">
{
"imports": {
"pdfjs/": "../../src/",
"pdfjs-lib": "../../src/pdf.js",
"pdfjs-web/": "../../web/",
"pdfjs-test/": "../"
}
}
</script>
<script src="jasmine-boot.js" type="module-shim"></script>
</head>
<body>
</body>
Expand Down
43 changes: 20 additions & 23 deletions test/font/fontutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@
* limitations under the License.
*/

"use strict";

var base64alphabet =
const base64alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

// eslint-disable-next-line no-unused-vars
function decodeFontData(base64) {
var result = [];
const result = [];

var bits = 0,
let bits = 0,
bitsLength = 0;
for (var i = 0, ii = base64.length; i < ii; i++) {
var ch = base64[i];
for (let i = 0, ii = base64.length; i < ii; i++) {
const ch = base64[i];
if (ch <= " ") {
continue;
}
var index = base64alphabet.indexOf(ch);
const index = base64alphabet.indexOf(ch);
if (index < 0) {
throw new Error("Invalid character");
}
Expand All @@ -41,24 +38,24 @@ function decodeFontData(base64) {
bitsLength += 6;
if (bitsLength >= 8) {
bitsLength -= 8;
var code = (bits >> bitsLength) & 0xff;
const code = (bits >> bitsLength) & 0xff;
result.push(code);
}
}
return new Uint8Array(result);
}

function encodeFontData(data) {
var buffer = "";
var i, n;
let buffer = "";
let i, n;
for (i = 0, n = data.length; i < n; i += 3) {
var b1 = data[i] & 0xff;
var b2 = data[i + 1] & 0xff;
var b3 = data[i + 2] & 0xff;
var d1 = b1 >> 2,
const b1 = data[i] & 0xff;
const b2 = data[i + 1] & 0xff;
const b3 = data[i + 2] & 0xff;
const d1 = b1 >> 2,
d2 = ((b1 & 3) << 4) | (b2 >> 4);
var d3 = i + 1 < n ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64;
var d4 = i + 2 < n ? b3 & 0x3f : 64;
const d3 = i + 1 < n ? ((b2 & 0xf) << 2) | (b3 >> 6) : 64;
const d4 = i + 2 < n ? b3 & 0x3f : 64;
buffer +=
base64alphabet.charAt(d1) +
base64alphabet.charAt(d2) +
Expand All @@ -68,12 +65,11 @@ function encodeFontData(data) {
return buffer;
}

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

var encodedData = encodeFontData(data);
const encodedData = encodeFontData(data);
xhr.setRequestHeader("Content-type", "text/plain");
xhr.setRequestHeader("Content-length", encodedData.length);

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

// eslint-disable-next-line no-unused-vars
function verifyTtxOutput(output) {
var m = /^<error>(.*?)<\/error>/.exec(output);
const m = /^<error>(.*?)<\/error>/.exec(output);
if (m) {
throw m[1];
}
}

export { decodeFontData, encodeFontData, ttx, verifyTtxOutput };
80 changes: 37 additions & 43 deletions test/font/jasmine-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,57 @@

"use strict";

function initializePDFJS(callback) {
Promise.all([
SystemJS.import("pdfjs/core/fonts.js"),
SystemJS.import("pdfjs/core/stream.js"),
SystemJS.import("pdfjs/core/primitives.js"),
SystemJS.import("pdfjs/core/cmap.js"),
]).then(function (modules) {
var fonts = modules[0],
stream = modules[1],
primitives = modules[2],
cmap = modules[3];
// Expose some of the PDFJS members to global scope for tests.
window.Font = fonts.Font;
window.ToUnicodeMap = fonts.ToUnicodeMap;
window.Stream = stream.Stream;
window.Name = primitives.Name;
window.CMapFactory = cmap.CMapFactory;

callback();
});
async function initializePDFJS(callback) {
await Promise.all(
[
"pdfjs-test/font/font_core_spec.js",
"pdfjs-test/font/font_os2_spec.js",
"pdfjs-test/font/font_post_spec.js",
"pdfjs-test/font/font_fpgm_spec.js",
].map(function (moduleName) {
// eslint-disable-next-line no-unsanitized/method
return import(moduleName);
})
);

callback();
}

(function () {
window.jasmine = jasmineRequire.core(jasmineRequire);

jasmineRequire.html(jasmine);

var env = jasmine.getEnv();
const env = jasmine.getEnv();

var jasmineInterface = jasmineRequire.interface(jasmine, env);
const jasmineInterface = jasmineRequire.interface(jasmine, env);
extend(window, jasmineInterface);

// Runner Parameters
var queryString = new jasmine.QueryString({
const queryString = new jasmine.QueryString({
getWindowLocation() {
return window.location;
},
});

var config = {
const config = {
failFast: queryString.getParam("failFast"),
oneFailurePerSpec: queryString.getParam("oneFailurePerSpec"),
hideDisabled: queryString.getParam("hideDisabled"),
};

var random = queryString.getParam("random");
const random = queryString.getParam("random");
if (random !== undefined && random !== "") {
config.random = random;
}

var seed = queryString.getParam("seed");
const seed = queryString.getParam("seed");
if (seed) {
config.seed = seed;
}

// Reporters
var htmlReporter = new jasmine.HtmlReporter({
const htmlReporter = new jasmine.HtmlReporter({
env,
navigateWithNewParam(key, value) {
return queryString.navigateWithNewParam(key, value);
Expand All @@ -119,13 +113,13 @@ function initializePDFJS(callback) {
env.addReporter(htmlReporter);

if (queryString.getParam("browser")) {
var testReporter = new TestReporter(queryString.getParam("browser"));
const testReporter = new TestReporter(queryString.getParam("browser"));
env.addReporter(testReporter);
}

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

// Replace the browser window's `onload`, ensure it's called, and then run
// all of the loaded specs. This includes initializing the `HtmlReporter`
// instance and then executing the loaded Jasmine environment.
var currentWindowOnload = window.onload;

window.onload = function () {
if (currentWindowOnload) {
currentWindowOnload();
function extend(destination, source) {
for (const property in source) {
destination[property] = source[property];
}
return destination;
}

function fontTestInit() {
initializePDFJS(function () {
htmlReporter.initialize();
env.execute();
});
};
}

function extend(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
if (
document.readyState === "interactive" ||
document.readyState === "complete"
) {
fontTestInit();
} else {
document.addEventListener("DOMContentLoaded", fontTestInit, true);
}
})();
30 changes: 15 additions & 15 deletions test/font/ttxdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@

"use strict";

var fs = require("fs");
var path = require("path");
var spawn = require("child_process").spawn;
const fs = require("fs");
const path = require("path");
const spawn = require("child_process").spawn;

var ttxResourcesHome = path.join(__dirname, "..", "ttx");
const ttxResourcesHome = path.join(__dirname, "..", "ttx");

var nextTTXTaskId = Date.now();
let nextTTXTaskId = Date.now();

function runTtx(ttxResourcesHomePath, fontPath, registerOnCancel, callback) {
fs.realpath(ttxResourcesHomePath, function (error, realTtxResourcesHomePath) {
var fontToolsHome = path.join(realTtxResourcesHomePath, "fonttools-code");
const fontToolsHome = path.join(realTtxResourcesHomePath, "fonttools-code");
fs.realpath(fontPath, function (errorFontPath, realFontPath) {
var ttxPath = path.join("Tools", "ttx");
const ttxPath = path.join("Tools", "ttx");
if (!fs.existsSync(path.join(fontToolsHome, ttxPath))) {
callback("TTX was not found, please checkout PDF.js submodules");
return;
}
var ttxEnv = {
const ttxEnv = {
PYTHONPATH: path.join(fontToolsHome, "Lib"),
PYTHONDONTWRITEBYTECODE: true,
};
var ttxStdioMode = "ignore";
var ttx = spawn("python", [ttxPath, realFontPath], {
const ttxStdioMode = "ignore";
const ttx = spawn("python", [ttxPath, realFontPath], {
cwd: fontToolsHome,
stdio: ttxStdioMode,
env: ttxEnv,
});
var ttxRunError;
let ttxRunError;
registerOnCancel(function (reason) {
ttxRunError = reason;
callback(reason);
Expand All @@ -68,10 +68,10 @@ exports.translateFont = function translateFont(
registerOnCancel,
callback
) {
var buffer = Buffer.from(content, "base64");
var taskId = (nextTTXTaskId++).toString();
var fontPath = path.join(ttxResourcesHome, taskId + ".otf");
var resultPath = path.join(ttxResourcesHome, taskId + ".ttx");
const buffer = Buffer.from(content, "base64");
const taskId = (nextTTXTaskId++).toString();
const fontPath = path.join(ttxResourcesHome, taskId + ".otf");
const resultPath = path.join(ttxResourcesHome, taskId + ".ttx");

fs.writeFileSync(fontPath, buffer);
runTtx(ttxResourcesHome, fontPath, registerOnCancel, function (err) {
Expand Down