ttxdriver.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2014 Mozilla Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. "use strict";
  17. const fs = require("fs");
  18. const path = require("path");
  19. const spawn = require("child_process").spawn;
  20. const ttxResourcesHome = path.join(__dirname, "..", "ttx");
  21. let nextTTXTaskId = Date.now();
  22. function runTtx(ttxResourcesHomePath, fontPath, registerOnCancel, callback) {
  23. fs.realpath(ttxResourcesHomePath, function (error, realTtxResourcesHomePath) {
  24. const fontToolsHome = path.join(realTtxResourcesHomePath, "fonttools-code");
  25. fs.realpath(fontPath, function (errorFontPath, realFontPath) {
  26. const ttxPath = path.join("Lib", "fontTools", "ttx.py");
  27. if (!fs.existsSync(path.join(fontToolsHome, ttxPath))) {
  28. callback("TTX was not found, please checkout PDF.js submodules");
  29. return;
  30. }
  31. const ttxEnv = {
  32. PYTHONPATH: path.join(fontToolsHome, "Lib"),
  33. PYTHONDONTWRITEBYTECODE: true,
  34. };
  35. const ttxStdioMode = "ignore";
  36. const python = process.platform !== "win32" ? "python2" : "python";
  37. const ttx = spawn(python, [ttxPath, realFontPath], {
  38. cwd: fontToolsHome,
  39. stdio: ttxStdioMode,
  40. env: ttxEnv,
  41. });
  42. let ttxRunError;
  43. registerOnCancel(function (reason) {
  44. ttxRunError = reason;
  45. callback(reason);
  46. ttx.kill();
  47. });
  48. ttx.on("error", function (errorTtx) {
  49. ttxRunError = errorTtx;
  50. callback("Unable to execute ttx");
  51. });
  52. ttx.on("close", function (code) {
  53. if (ttxRunError) {
  54. return;
  55. }
  56. callback();
  57. });
  58. });
  59. });
  60. }
  61. exports.translateFont = function translateFont(
  62. content,
  63. registerOnCancel,
  64. callback
  65. ) {
  66. const buffer = Buffer.from(content, "base64");
  67. const taskId = (nextTTXTaskId++).toString();
  68. const fontPath = path.join(ttxResourcesHome, taskId + ".otf");
  69. const resultPath = path.join(ttxResourcesHome, taskId + ".ttx");
  70. fs.writeFileSync(fontPath, buffer);
  71. runTtx(ttxResourcesHome, fontPath, registerOnCancel, function (err) {
  72. fs.unlinkSync(fontPath);
  73. if (err) {
  74. console.error(err);
  75. callback(err);
  76. } else if (!fs.existsSync(resultPath)) {
  77. callback("Output was not generated");
  78. } else {
  79. callback(null, fs.readFileSync(resultPath));
  80. fs.unlinkSync(resultPath);
  81. }
  82. });
  83. };