UNPKG

@nmmty/lazycanvas

Version:

A simple way to interact with @napi-rs/canvas in an advanced way!

173 lines (172 loc) 8.01 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JSONReader = void 0; const types_1 = require("../../../types"); const components_1 = require("../../components"); const __1 = require("../"); const LazyCanvas_1 = require("../../LazyCanvas"); const fs = __importStar(require("fs")); const LazyUtil_1 = require("../../../utils/LazyUtil"); const path = __importStar(require("path")); /** * Class responsible for reading and parsing JSON data into a LazyCanvas instance. */ class JSONReader { /** * Reads JSON data and converts it into a LazyCanvas instance. * @param data {IOLazyCanvas} - The JSON data to read. * @param opts {Object} - Optional settings. * @param opts.debug {boolean} - Whether to enable debug logging. * @returns {LazyCanvas} The created LazyCanvas instance. * @throws {LazyError} If the data contains invalid options or no layers are found. */ static read(data, opts) { if (data.options.width <= 0 || data.options.height <= 0) { throw new LazyUtil_1.LazyError("Invalid width or height"); } if (data.options.exportType === undefined) { throw new LazyUtil_1.LazyError("Invalid export type"); } if (data.options.flag === undefined) { throw new LazyUtil_1.LazyError("Invalid export flag"); } if (data.layers === undefined || data.layers.length === 0) { throw new LazyUtil_1.LazyError("No layers found"); } if (opts?.debug) LazyUtil_1.LazyLog.log("info", "Reading JSON...\nOptions:", data.options, "\nAnimation:", data.animation, "\nLayers Number:", data.layers.length, "\nLayers:", data.layers); const layers = JSONReader.layersParse(data.layers); const canvas = new LazyCanvas_1.LazyCanvas({ settings: data, debug: opts?.debug }) .create(data.options.width, data.options.height); canvas.manager.layers.add(...layers); return canvas; } /** * Reads a JSON file and converts it into a LazyCanvas instance. * @param file {string} - The path to the JSON file. * @param opts {Object} - Optional settings. * @param opts.debug {boolean} - Whether to enable debug logging. * @returns {LazyCanvas} The created LazyCanvas instance. * @throws {LazyError} If the file does not exist. */ static readFile(file, opts) { const filePath = path.resolve(file); if (!fs.existsSync(filePath)) throw new LazyUtil_1.LazyError("File not found"); const json = fs.readFileSync(filePath, "utf-8"); const data = JSON.parse(json); if (opts?.debug) LazyUtil_1.LazyLog.log("info", "Reading JSON file...\nFile:", filePath, "\nData:", data); return JSONReader.read(data, opts); } /** * Parses an array of JSON layers into an array of AnyLayer or Group instances. * @param data {Array<JSONLayer | Group>} - The array of JSON layers to parse. * @param opts {Object} - Optional settings. * @param opts.debug {boolean} - Whether to enable debug logging. * @returns {Array<AnyLayer | Group>} The parsed layers. */ static layersParse(data, opts) { return data.map((layer) => { if (opts?.debug) LazyUtil_1.LazyLog.log('info', `Parsing layer ${layer.id}...\nData:`, layer); const misc = { id: layer.id, zIndex: layer.zIndex, visible: layer.visible, }; if (layer.type === types_1.LayerType.Group) { return new components_1.Group(misc).add(...layer.layers.map((l) => this.layerParse(l))); } else { return this.layerParse(layer, misc); } }); } /** * Parses a single JSON layer into an AnyLayer or Group instance. * @param layer {JSONLayer | IGroup | Group} - The JSON layer to parse. * @param misc {IBaseLayerMisc} - Miscellaneous options for the layer. * @returns {AnyLayer | Group} The parsed layer. */ static layerParse(layer, misc) { if (layer instanceof components_1.Group) { return new components_1.Group(misc).add(...layer.layers.map((l) => this.layerParse(l))); } else { switch (layer.type) { case types_1.LayerType.BezierCurve: return new components_1.BezierLayer(layer.props, misc).setColor(this.fillParse(layer)); case types_1.LayerType.QuadraticCurve: return new components_1.QuadraticLayer(layer.props, misc).setColor(this.fillParse(layer)); case types_1.LayerType.Image: return new components_1.ImageLayer(layer.props, misc); case types_1.LayerType.Text: return new components_1.TextLayer(layer.props, misc).setColor(this.fillParse(layer)); case types_1.LayerType.Morph: return new components_1.MorphLayer(layer.props, misc).setColor(this.fillParse(layer)); case types_1.LayerType.Line: return new components_1.LineLayer(layer.props, misc).setColor(this.fillParse(layer)); case types_1.LayerType.Clear: return new components_1.ClearLayer(layer.props, misc); case types_1.LayerType.Path: return new components_1.Path2DLayer(layer.props, misc).setColor(this.fillParse(layer)); case types_1.LayerType.Group: return new components_1.Group(misc) .add(...layer.layers.map((l) => this.layerParse(l))); default: return layer; } } } /** * Parses the fill style of a layer. * @param layer {JSONLayer} - The layer whose fill style is to be parsed. * @returns {string | Gradient | Pattern} The parsed fill style. */ static fillParse(layer) { if ('fillStyle' in layer.props && layer.props.fillStyle && typeof layer.props.fillStyle !== 'string') { switch (layer.props.fillStyle?.fillType) { case 'gradient': return new __1.Gradient({ props: layer.props.fillStyle }); case 'pattern': console.log('Pattern:', layer.props.fillStyle); return new __1.Pattern() .setType(layer.props.fillStyle.type) .setSrc(typeof layer.props.fillStyle.src === 'string' ? layer.props.fillStyle.src : this.read(layer.props.fillStyle.src)); default: return layer.props.fillStyle; } } else if ('fillStyle' in layer.props) { return layer.props.fillStyle || '#000000'; } else { return '#000000'; } } } exports.JSONReader = JSONReader;