UNPKG

kist

Version:

Package Pipeline Processor

112 lines (111 loc) 5.21 kB
"use strict"; // ============================================================================ // Imports // ============================================================================ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SvgToPngAction = void 0; const canvas_1 = require("canvas"); const canvg_1 = require("canvg"); const fs_1 = __importDefault(require("fs")); const jsdom_1 = require("jsdom"); const path_1 = __importDefault(require("path")); const Action_1 = require("../../core/pipeline/Action"); // ============================================================================ // Utilities // ============================================================================ function getSafeRenderingContext(canvas) { return canvas.getContext("2d"); } // ============================================================================ // Classes // ============================================================================ /** * SvgToPngAction converts SVG content to PNG format. * Uses `canvg` for conversion and `jsdom` for SVG element manipulation. */ class SvgToPngAction extends Action_1.Action { /** * Executes the SVG-to-PNG conversion process. * @param options - Options including SVG content, output path, width, * and height. */ execute(options) { return __awaiter(this, void 0, void 0, function* () { const { svgContent, outputPath, width, height } = options; if (!svgContent || !outputPath) { throw new Error("Both 'svgContent' and 'outputPath' must be provided."); } this.logInfo(`Converting SVG to PNG: ${outputPath}`); try { yield this.convert(svgContent, outputPath, width, height); this.logInfo(`SVG successfully converted to PNG: ${outputPath}`); } catch (error) { this.logError("Error converting SVG to PNG:", error); throw error; } }); } /** * Converts SVG content to a PNG file, optionally resizing the output. * @param svgContent - The SVG content to be converted. * @param outputPath - The filesystem path where the PNG should be saved. * @param width - Optional width for resizing. * @param height - Optional height for resizing. */ convert(svgContent, outputPath, width, height) { return __awaiter(this, void 0, void 0, function* () { try { const outputDir = path_1.default.dirname(outputPath); if (!fs_1.default.existsSync(outputDir)) { fs_1.default.mkdirSync(outputDir, { recursive: true }); } const dom = new jsdom_1.JSDOM(svgContent); const svgElement = dom.window.document.querySelector("svg"); if (!svgElement) { throw new Error("Invalid SVG content"); } const w = width || parseInt(svgElement.getAttribute("width") || "800", 10); const h = height || parseInt(svgElement.getAttribute("height") || "600", 10); svgElement.setAttribute("width", w.toString()); svgElement.setAttribute("height", h.toString()); const updatedSvgContent = svgElement.outerHTML; const canvas = (0, canvas_1.createCanvas)(w, h); const ctx = getSafeRenderingContext(canvas); const canvg = yield canvg_1.Canvg.from(ctx, updatedSvgContent); yield canvg.render(); const pngBuffer = canvas.toBuffer("image/png"); fs_1.default.writeFileSync(outputPath, pngBuffer); } catch (error) { throw new Error(`Error converting SVG to PNG: ${error.message}`); } }); } /** * Provides a description of the action. * @returns A string description of the action. */ describe() { return "Converts SVG content to PNG format with optional resizing using canvg and canvas."; } } exports.SvgToPngAction = SvgToPngAction; // ============================================================================ // Export // ============================================================================ exports.default = SvgToPngAction;