mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 22:58:07 +02:00
Merge pull request #17890 from timvandermeij/font-tests-callbacks
Modernize the TTX driver code
This commit is contained in:
commit
fb21c4261d
2 changed files with 43 additions and 49 deletions
|
@ -21,47 +21,51 @@ import { spawn } from "child_process";
|
|||
|
||||
let ttxTaskId = Date.now();
|
||||
|
||||
function runTtx(fontPath, registerOnCancel, callback) {
|
||||
const ttx = spawn("ttx", [fontPath], { stdio: "ignore" });
|
||||
let ttxRunError;
|
||||
registerOnCancel(function (reason) {
|
||||
ttxRunError = reason;
|
||||
callback(reason);
|
||||
ttx.kill();
|
||||
});
|
||||
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, registerOnCancel, 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, 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);
|
||||
}
|
||||
});
|
||||
|
||||
// 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 };
|
||||
|
|
|
@ -840,24 +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") {
|
||||
var onCancel = null,
|
||||
ttxTimeout = 10000;
|
||||
var timeoutId = setTimeout(function () {
|
||||
onCancel?.("TTX timeout");
|
||||
}, ttxTimeout);
|
||||
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);
|
||||
}
|
||||
);
|
||||
res.writeHead(200, { "Content-Type": "text/xml" });
|
||||
try {
|
||||
res.end(await translateFont(body));
|
||||
} catch (error) {
|
||||
res.end(`<error>${error}</error>`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue