node_utils.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.NodeStandardFontDataFactory = exports.NodeCMapReaderFactory = exports.NodeCanvasFactory = void 0;
  27. var _base_factory = require("./base_factory.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. let NodeStandardFontDataFactory = class {
  43. constructor() {
  44. (0, _util.unreachable)("Not implemented: NodeStandardFontDataFactory");
  45. }
  46. };
  47. exports.NodeStandardFontDataFactory = NodeStandardFontDataFactory;
  48. if (_is_node.isNodeJS) {
  49. const fetchData = function (url) {
  50. return new Promise((resolve, reject) => {
  51. const fs = require("fs");
  52. fs.readFile(url, (error, data) => {
  53. if (error || !data) {
  54. reject(new Error(error));
  55. return;
  56. }
  57. resolve(new Uint8Array(data));
  58. });
  59. });
  60. };
  61. exports.NodeCanvasFactory = NodeCanvasFactory = class extends _base_factory.BaseCanvasFactory {
  62. _createCanvas(width, height) {
  63. const Canvas = require("canvas");
  64. return Canvas.createCanvas(width, height);
  65. }
  66. };
  67. exports.NodeCMapReaderFactory = NodeCMapReaderFactory = class extends _base_factory.BaseCMapReaderFactory {
  68. _fetchData(url, compressionType) {
  69. return fetchData(url).then(data => {
  70. return {
  71. cMapData: data,
  72. compressionType
  73. };
  74. });
  75. }
  76. };
  77. exports.NodeStandardFontDataFactory = NodeStandardFontDataFactory = class extends _base_factory.BaseStandardFontDataFactory {
  78. _fetchData(url) {
  79. return fetchData(url);
  80. }
  81. };
  82. }