pdf.js/test/font/ttxdriver.js
Tim van der Meij 3d4476ac53
Update ttx from version 2.5 to version 3.19.0
The current version 2.5 is from September 2014, which is almost 8 years
old now. The new version 3.19.0 is from November 2017, which is still
almost 5 years old, but is a step forward towards eventually using the
most recent version. Note that we currently can't update any further;
see #11802 for the details.

Fortunately using this newer version only required a few changes:

- The `ttx` output regexes needed updating to ignore comments that `ttx`
  now puts after some XML nodes (`<!-- ... -->` and `/* ... */`).
- The `ttx` invocation now explicitly uses `python2` (except on Windows
  where this alias doesn't exist) since otherwise the font tests can't
  be run on modern systems anymore given that `python` is nowadays an
  alias for `python3`, and it now points at the new location of the
  `ttx.py` file since the `Tools` folder got removed.
- The note about needing a 32-bit Python 2.6 version is dropped since
  it's obsolete: this version (and also the existing one already) work
  just fine on a 64-bit Python 2.7 as well.
2022-07-10 21:18:36 +02:00

91 lines
2.8 KiB
JavaScript

/*
* Copyright 2014 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.
*/
"use strict";
const fs = require("fs");
const path = require("path");
const spawn = require("child_process").spawn;
const ttxResourcesHome = path.join(__dirname, "..", "ttx");
let nextTTXTaskId = Date.now();
function runTtx(ttxResourcesHomePath, fontPath, registerOnCancel, callback) {
fs.realpath(ttxResourcesHomePath, function (error, realTtxResourcesHomePath) {
const fontToolsHome = path.join(realTtxResourcesHomePath, "fonttools-code");
fs.realpath(fontPath, function (errorFontPath, realFontPath) {
const ttxPath = path.join("Lib", "fontTools", "ttx.py");
if (!fs.existsSync(path.join(fontToolsHome, ttxPath))) {
callback("TTX was not found, please checkout PDF.js submodules");
return;
}
const ttxEnv = {
PYTHONPATH: path.join(fontToolsHome, "Lib"),
PYTHONDONTWRITEBYTECODE: true,
};
const ttxStdioMode = "ignore";
const python = process.platform !== "win32" ? "python2" : "python";
const ttx = spawn(python, [ttxPath, realFontPath], {
cwd: fontToolsHome,
stdio: ttxStdioMode,
env: ttxEnv,
});
let ttxRunError;
registerOnCancel(function (reason) {
ttxRunError = reason;
callback(reason);
ttx.kill();
});
ttx.on("error", function (errorTtx) {
ttxRunError = errorTtx;
callback("Unable to execute ttx");
});
ttx.on("close", function (code) {
if (ttxRunError) {
return;
}
callback();
});
});
});
}
exports.translateFont = function translateFont(
content,
registerOnCancel,
callback
) {
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) {
fs.unlinkSync(fontPath);
if (err) {
console.error(err);
callback(err);
} else if (!fs.existsSync(resultPath)) {
callback("Output was not generated");
} else {
callback(null, fs.readFileSync(resultPath));
fs.unlinkSync(resultPath);
}
});
};