UNPKG

pdf-to-png-converter-allow-yarn

Version:

Node.js utility to convert PDF file/buffer pages to PNG files/buffers with no native dependencies.

45 lines (44 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeCanvasFactory = void 0; const canvas_1 = require("@napi-rs/canvas"); const node_assert_1 = require("node:assert"); class NodeCanvasFactory { /** * Creates a new canvas context with the specified width and height. * @param width The width of the canvas. * @param height The height of the canvas. * @returns A new canvas context with the specified width and height. * @throws An error if the width or height is less than or equal to zero. */ create(width, height) { const canvas = new canvas_1.Canvas(width, height); return { canvas, context: canvas.getContext('2d'), }; } /** * Resets the canvas to the specified width and height. * @param canvasAndContext - The canvas and its context. * @param width - The new width of the canvas. * @param height - The new height of the canvas. */ reset(canvasAndContext, width, height) { (0, node_assert_1.strict)(canvasAndContext.canvas, 'Canvas is not specified'); (0, node_assert_1.strict)(width > 0 && height > 0, 'Invalid canvas size'); canvasAndContext.canvas.width = width; canvasAndContext.canvas.height = height; } /** * Destroys the canvas and its context by setting the canvas width and height to 0 and * setting the canvas and context properties to undefined. * @param canvasAndContext - The canvas and its context to be destroyed. */ destroy(canvasAndContext) { (0, node_assert_1.strict)(canvasAndContext.canvas, 'Canvas is not specified'); canvasAndContext.canvas = undefined; canvasAndContext.context = undefined; } } exports.NodeCanvasFactory = NodeCanvasFactory;