|
| 1 | +/* Copyright 2015 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +/* globals __non_webpack_require__ */ |
| 16 | +/* eslint no-var: error */ |
| 17 | + |
| 18 | +import { BaseCanvasFactory, BaseCMapReaderFactory } from "./display_utils.js"; |
| 19 | +import { CMapCompressionType } from "../shared/util.js"; |
| 20 | +import { isNodeJS } from "../shared/is_node.js"; |
| 21 | + |
| 22 | +let NodeCanvasFactory = class { |
| 23 | + constructor() { |
| 24 | + throw new Error("Not implemented: NodeCanvasFactory"); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +let NodeCMapReaderFactory = class { |
| 29 | + constructor() { |
| 30 | + throw new Error("Not implemented: NodeCMapReaderFactory"); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS) { |
| 35 | + NodeCanvasFactory = class extends BaseCanvasFactory { |
| 36 | + create(width, height) { |
| 37 | + if (width <= 0 || height <= 0) { |
| 38 | + throw new Error("Invalid canvas size"); |
| 39 | + } |
| 40 | + const Canvas = __non_webpack_require__("canvas"); |
| 41 | + const canvas = Canvas.createCanvas(width, height); |
| 42 | + return { |
| 43 | + canvas, |
| 44 | + context: canvas.getContext("2d"), |
| 45 | + }; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + NodeCMapReaderFactory = class extends BaseCMapReaderFactory { |
| 50 | + async fetch({ name }) { |
| 51 | + if (!this.baseUrl) { |
| 52 | + throw new Error( |
| 53 | + 'The CMap "baseUrl" parameter must be specified, ensure that ' + |
| 54 | + 'the "cMapUrl" and "cMapPacked" API parameters are provided.' |
| 55 | + ); |
| 56 | + } |
| 57 | + if (!name) { |
| 58 | + throw new Error("CMap name must be specified."); |
| 59 | + } |
| 60 | + const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : ""); |
| 61 | + const compressionType = this.isCompressed |
| 62 | + ? CMapCompressionType.BINARY |
| 63 | + : CMapCompressionType.NONE; |
| 64 | + |
| 65 | + return new Promise((resolve, reject) => { |
| 66 | + const fs = __non_webpack_require__("fs"); |
| 67 | + fs.readFile(url, (error, data) => { |
| 68 | + if (error || !data) { |
| 69 | + reject(new Error(error)); |
| 70 | + return; |
| 71 | + } |
| 72 | + resolve({ cMapData: new Uint8Array(data), compressionType }); |
| 73 | + }); |
| 74 | + }).catch(reason => { |
| 75 | + throw new Error( |
| 76 | + `Unable to load ${this.isCompressed ? "binary " : ""}` + |
| 77 | + `CMap at: ${url}` |
| 78 | + ); |
| 79 | + }); |
| 80 | + } |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +export { NodeCanvasFactory, NodeCMapReaderFactory }; |
0 commit comments