1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Convert the TTX driver code to promises

This commit removes the final callbacks in this code by switching to a
promises-based interface, overall simplifying the code. Moreover, we
document why we write to files on disk and modernize the code using e.g.
template strings.
This commit is contained in:
Tim van der Meij 2024-04-05 13:03:22 +02:00
parent 64065141b6
commit ac03d7054d
No known key found for this signature in database
GPG key ID: 8C3FD2925A5F2762
2 changed files with 43 additions and 32 deletions

View file

@ -21,42 +21,51 @@ import { spawn } from "child_process";
let ttxTaskId = Date.now();
function runTtx(fontPath, callback) {
const ttx = spawn("ttx", [fontPath], { stdio: "ignore" });
let ttxRunError;
ttx.on("error", function (errorTtx) {
ttxRunError = errorTtx;
callback(
"Unable to execute `ttx`; make sure the `fonttools` dependency is installed"
);
});
ttx.on("close", function (code) {
if (ttxRunError) {
return;
}
callback();
function runTtx(fontPath) {
return new Promise((resolve, reject) => {
const ttx = spawn("ttx", [fontPath], { stdio: "ignore" });
ttx.on("error", () => {
reject(
new Error(
"Unable to execute `ttx`; make sure the `fonttools` dependency is installed"
)
);
});
ttx.on("close", () => {
resolve();
});
});
}
function translateFont(content, callback) {
async function translateFont(content) {
const buffer = Buffer.from(content, "base64");
const taskId = (ttxTaskId++).toString();
const fontPath = path.join(os.tmpdir(), `pdfjs-font-test-${taskId}.otf`);
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);
runTtx(fontPath, 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);
}
});
// Run TTX on the temporary font file.
let ttxError;
try {
await runTtx(fontPath);
} catch (error) {
ttxError = error;
}
// 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 };

View file

@ -840,12 +840,14 @@ function unitTestPostHandler(req, res) {
req.on("data", function (data) {
body += data;
});
req.on("end", function () {
req.on("end", async function () {
if (pathname === "/ttx") {
translateFont(body, function (err, xml) {
res.writeHead(200, { "Content-Type": "text/xml" });
res.end(err ? "<error>" + err + "</error>" : xml);
});
res.writeHead(200, { "Content-Type": "text/xml" });
try {
res.end(await translateFont(body));
} catch (error) {
res.end(`<error>${error}</error>`);
}
return;
}