Merge pull request #17890 from timvandermeij/font-tests-callbacks

Modernize the TTX driver code
This commit is contained in:
Tim van der Meij 2024-04-08 20:12:08 +02:00 committed by GitHub
commit fb21c4261d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 49 deletions

View file

@ -21,47 +21,51 @@ import { spawn } from "child_process";
let ttxTaskId = Date.now(); let ttxTaskId = Date.now();
function runTtx(fontPath, registerOnCancel, callback) { function runTtx(fontPath) {
const ttx = spawn("ttx", [fontPath], { stdio: "ignore" }); return new Promise((resolve, reject) => {
let ttxRunError; const ttx = spawn("ttx", [fontPath], { stdio: "ignore" });
registerOnCancel(function (reason) { ttx.on("error", () => {
ttxRunError = reason; reject(
callback(reason); new Error(
ttx.kill(); "Unable to execute `ttx`; make sure the `fonttools` dependency is installed"
}); )
ttx.on("error", function (errorTtx) { );
ttxRunError = errorTtx; });
callback( ttx.on("close", () => {
"Unable to execute `ttx`; make sure the `fonttools` dependency is installed" resolve();
); });
});
ttx.on("close", function (code) {
if (ttxRunError) {
return;
}
callback();
}); });
} }
function translateFont(content, registerOnCancel, callback) { async function translateFont(content) {
const buffer = Buffer.from(content, "base64"); const buffer = Buffer.from(content, "base64");
const taskId = (ttxTaskId++).toString(); const taskId = (ttxTaskId++).toString();
const fontPath = path.join(os.tmpdir(), `pdfjs-font-test-${taskId}.otf`); const fontPath = path.join(os.tmpdir(), `pdfjs-font-test-${taskId}.otf`);
const resultPath = path.join(os.tmpdir(), `pdfjs-font-test-${taskId}.ttx`); const resultPath = path.join(os.tmpdir(), `pdfjs-font-test-${taskId}.ttx`);
// Write the font data to a temporary file on disk (because TTX only accepts
// files as input).
fs.writeFileSync(fontPath, buffer); fs.writeFileSync(fontPath, buffer);
runTtx(fontPath, registerOnCancel, function (err) {
fs.unlinkSync(fontPath); // Run TTX on the temporary font file.
if (err) { let ttxError;
console.error(err); try {
callback(err); await runTtx(fontPath);
} else if (!fs.existsSync(resultPath)) { } catch (error) {
callback("Output was not generated"); ttxError = error;
} else { }
callback(null, fs.readFileSync(resultPath));
fs.unlinkSync(resultPath); // Remove the temporary font/result files and report on the outcome.
} fs.unlinkSync(fontPath);
}); if (ttxError) {
throw ttxError;
}
if (!fs.existsSync(resultPath)) {
throw new Error("TTX did not generate output");
}
const xml = fs.readFileSync(resultPath);
fs.unlinkSync(resultPath);
return xml;
} }
export { translateFont }; export { translateFont };

View file

@ -840,24 +840,14 @@ function unitTestPostHandler(req, res) {
req.on("data", function (data) { req.on("data", function (data) {
body += data; body += data;
}); });
req.on("end", function () { req.on("end", async function () {
if (pathname === "/ttx") { if (pathname === "/ttx") {
var onCancel = null, res.writeHead(200, { "Content-Type": "text/xml" });
ttxTimeout = 10000; try {
var timeoutId = setTimeout(function () { res.end(await translateFont(body));
onCancel?.("TTX timeout"); } catch (error) {
}, ttxTimeout); res.end(`<error>${error}</error>`);
translateFont( }
body,
function (fn) {
onCancel = fn;
},
function (err, xml) {
clearTimeout(timeoutId);
res.writeHead(200, { "Content-Type": "text/xml" });
res.end(err ? "<error>" + err + "</error>" : xml);
}
);
return; return;
} }