node_utils.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.NodeCMapReaderFactory = exports.NodeCanvasFactory = void 0;
  27. var _display_utils = require("./display_utils.js");
  28. var _is_node = require("../shared/is_node.js");
  29. var _util = require("../shared/util.js");
  30. let NodeCanvasFactory = class {
  31. constructor() {
  32. (0, _util.unreachable)("Not implemented: NodeCanvasFactory");
  33. }
  34. };
  35. exports.NodeCanvasFactory = NodeCanvasFactory;
  36. let NodeCMapReaderFactory = class {
  37. constructor() {
  38. (0, _util.unreachable)("Not implemented: NodeCMapReaderFactory");
  39. }
  40. };
  41. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  42. if (_is_node.isNodeJS) {
  43. exports.NodeCanvasFactory = NodeCanvasFactory = class extends _display_utils.BaseCanvasFactory {
  44. create(width, height) {
  45. if (width <= 0 || height <= 0) {
  46. throw new Error("Invalid canvas size");
  47. }
  48. const Canvas = require("canvas");
  49. const canvas = Canvas.createCanvas(width, height);
  50. return {
  51. canvas,
  52. context: canvas.getContext("2d")
  53. };
  54. }
  55. };
  56. exports.NodeCMapReaderFactory = NodeCMapReaderFactory = class extends _display_utils.BaseCMapReaderFactory {
  57. _fetchData(url, compressionType) {
  58. return new Promise((resolve, reject) => {
  59. const fs = require("fs");
  60. fs.readFile(url, (error, data) => {
  61. if (error || !data) {
  62. reject(new Error(error));
  63. return;
  64. }
  65. resolve({
  66. cMapData: new Uint8Array(data),
  67. compressionType
  68. });
  69. });
  70. });
  71. }
  72. };
  73. }