-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api-minor] Use the
NodeCanvasFactory
/NodeCMapReaderFactory
class…
…es as defaults in Node.js environments (issue 11900) This moves, and slightly simplifies, code that's currently residing in the unit-test utils into the actual library, such that it's bundled with `GENERIC`-builds and used in e.g. the API-code. As an added bonus, this also brings out-of-the-box support for CMaps in e.g. the Node.js examples.
- Loading branch information
1 parent
fe3df49
commit cb6e745
Showing
10 changed files
with
167 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright 2020 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* globals __non_webpack_require__ */ | ||
/* eslint no-var: error */ | ||
|
||
import { BaseCanvasFactory, BaseCMapReaderFactory } from "./display_utils.js"; | ||
import { isNodeJS } from "../shared/is_node.js"; | ||
import { unreachable } from "../shared/util.js"; | ||
|
||
let NodeCanvasFactory = class { | ||
constructor() { | ||
unreachable("Not implemented: NodeCanvasFactory"); | ||
} | ||
}; | ||
|
||
let NodeCMapReaderFactory = class { | ||
constructor() { | ||
unreachable("Not implemented: NodeCMapReaderFactory"); | ||
} | ||
}; | ||
|
||
if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS) { | ||
NodeCanvasFactory = class extends BaseCanvasFactory { | ||
create(width, height) { | ||
if (width <= 0 || height <= 0) { | ||
throw new Error("Invalid canvas size"); | ||
} | ||
const Canvas = __non_webpack_require__("canvas"); | ||
const canvas = Canvas.createCanvas(width, height); | ||
return { | ||
canvas, | ||
context: canvas.getContext("2d"), | ||
}; | ||
} | ||
}; | ||
|
||
NodeCMapReaderFactory = class extends BaseCMapReaderFactory { | ||
_fetchData(url) { | ||
return new Promise((resolve, reject) => { | ||
const fs = __non_webpack_require__("fs"); | ||
fs.readFile(url, (error, data) => { | ||
if (error || !data) { | ||
reject(new Error(error)); | ||
return; | ||
} | ||
resolve(new Uint8Array(data)); | ||
}); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
export { NodeCanvasFactory, NodeCMapReaderFactory }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.