Skip to content

Commit

Permalink
v0.0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvianen committed Jan 3, 2024
1 parent fe0b2ae commit fb924f7
Show file tree
Hide file tree
Showing 12 changed files with 1,234 additions and 127 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.18
0.0.19
4 changes: 4 additions & 0 deletions dist/js/class/SvgToPngConverter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare class SvgToPngConverter {
convert(svgContent: string, outputPath: string, width?: number, height?: number): Promise<void>;
}
export default SvgToPngConverter;
60 changes: 60 additions & 0 deletions dist/js/class/SvgToPngConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"use strict";
// class/SvgToPngConverter.ts
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Copyright 2023 Scape Agency BV
// 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.
// ============================================================================
// Import
// ============================================================================
var sharp_1 = __importDefault(require("sharp"));
var jsdom_1 = require("jsdom");
var fs_1 = __importDefault(require("fs"));
var path_1 = __importDefault(require("path"));
// ============================================================================
// Classes
// ============================================================================
class SvgToPngConverter {
async convert(svgContent, outputPath, width, height) {
try {
// Ensure the output directory exists
const outputDir = path_1.default.dirname(outputPath);
if (!fs_1.default.existsSync(outputDir)) {
fs_1.default.mkdirSync(outputDir, { recursive: true });
}
// Create a JSDOM instance to parse the SVG
const dom = new jsdom_1.JSDOM(svgContent);
const svgElement = dom.window.document.querySelector('svg');
if (!svgElement) {
throw new Error('Invalid SVG content');
}
if (width) {
svgElement.setAttribute('width', width.toString());
}
if (height) {
svgElement.setAttribute('height', height.toString());
}
const updatedSvgContent = svgElement.outerHTML;
const pngBuffer = await (0, sharp_1.default)(Buffer.from(updatedSvgContent)).png().toBuffer();
await (0, sharp_1.default)(pngBuffer).toFile(outputPath);
}
catch (error) {
console.error(`Error converting SVG to PNG: ${error}`);
throw error;
}
}
}
// ============================================================================
// Export
// ============================================================================
exports.default = SvgToPngConverter;
3 changes: 2 additions & 1 deletion dist/js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import JavaScriptMinifier from './class/JavaScriptMinifier.js';
import NpmCommandRunner from './class/NpmCommandRunner.js';
import StylizedLogger from './class/StylizedLogger.js';
import TemplateWriter from './class/TemplateWriter.js';
import SvgToPngConverter from './class/SvgToPngConverter.js';
import gl_installer from './function/gl_installer';
import cleanDirectory from './function/clean_directory';
export { DirectoryCleaner, DirectoryCopier, DirectoryCreator, FileCopier, FileRenamer, FontGenerator, PackageCreator, SvgPackager, StyleProcessor, SvgSpriteGenerator, VersionWriter, TypeScriptCompiler, JavaScriptMinifier, NpmCommandRunner, StylizedLogger, TemplateWriter, gl_installer, cleanDirectory, };
export { DirectoryCleaner, DirectoryCopier, DirectoryCreator, FileCopier, FileRenamer, FontGenerator, PackageCreator, SvgPackager, StyleProcessor, SvgSpriteGenerator, VersionWriter, TypeScriptCompiler, JavaScriptMinifier, NpmCommandRunner, StylizedLogger, TemplateWriter, SvgToPngConverter, gl_installer, cleanDirectory, };
4 changes: 3 additions & 1 deletion dist/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanDirectory = exports.gl_installer = exports.TemplateWriter = exports.StylizedLogger = exports.NpmCommandRunner = exports.JavaScriptMinifier = exports.TypeScriptCompiler = exports.VersionWriter = exports.SvgSpriteGenerator = exports.StyleProcessor = exports.SvgPackager = exports.PackageCreator = exports.FontGenerator = exports.FileRenamer = exports.FileCopier = exports.DirectoryCreator = exports.DirectoryCopier = exports.DirectoryCleaner = void 0;
exports.cleanDirectory = exports.gl_installer = exports.SvgToPngConverter = exports.TemplateWriter = exports.StylizedLogger = exports.NpmCommandRunner = exports.JavaScriptMinifier = exports.TypeScriptCompiler = exports.VersionWriter = exports.SvgSpriteGenerator = exports.StyleProcessor = exports.SvgPackager = exports.PackageCreator = exports.FontGenerator = exports.FileRenamer = exports.FileCopier = exports.DirectoryCreator = exports.DirectoryCopier = exports.DirectoryCleaner = void 0;
// Copyright 2023 Scape Agency BV
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,6 +52,8 @@ var StylizedLogger_js_1 = __importDefault(require("./class/StylizedLogger.js"));
exports.StylizedLogger = StylizedLogger_js_1.default;
var TemplateWriter_js_1 = __importDefault(require("./class/TemplateWriter.js"));
exports.TemplateWriter = TemplateWriter_js_1.default;
var SvgToPngConverter_js_1 = __importDefault(require("./class/SvgToPngConverter.js"));
exports.SvgToPngConverter = SvgToPngConverter_js_1.default;
// Import | Internal Functions
var gl_installer_1 = __importDefault(require("./function/gl_installer"));
exports.gl_installer = gl_installer_1.default;
Expand Down
2 changes: 1 addition & 1 deletion dist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pack.gl",
"version": "0.0.18",
"version": "0.0.19",
"description": "Package Builder.",
"keywords": [
"pack.gl",
Expand Down
74 changes: 74 additions & 0 deletions dist/ts/class/SvgToPngConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// class/SvgToPngConverter.ts

// Copyright 2023 Scape Agency BV

// 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.


// ============================================================================
// Import
// ============================================================================

import sharp from 'sharp';
import { JSDOM } from 'jsdom';
import fs from 'fs';
import path from 'path';


// ============================================================================
// Classes
// ============================================================================

class SvgToPngConverter {
async convert(svgContent: string, outputPath: string, width?: number, height?: number): Promise<void> {
try {

// Ensure the output directory exists
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}

// Create a JSDOM instance to parse the SVG
const dom = new JSDOM(svgContent);
const svgElement = dom.window.document.querySelector('svg');

if (!svgElement) {
throw new Error('Invalid SVG content');
}

if (width) {
svgElement.setAttribute('width', width.toString());
}

if (height) {
svgElement.setAttribute('height', height.toString());
}

const updatedSvgContent = svgElement.outerHTML;
const pngBuffer = await sharp(Buffer.from(updatedSvgContent)).png().toBuffer();

await sharp(pngBuffer).toFile(outputPath);
} catch (error) {
console.error(`Error converting SVG to PNG: ${error}`);
throw error;
}
}
}


// ============================================================================
// Export
// ============================================================================

export default SvgToPngConverter;
2 changes: 2 additions & 0 deletions dist/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import JavaScriptMinifier from './class/JavaScriptMinifier.js';
import NpmCommandRunner from './class/NpmCommandRunner.js';
import StylizedLogger from './class/StylizedLogger.js';
import TemplateWriter from './class/TemplateWriter.js';
import SvgToPngConverter from './class/SvgToPngConverter.js';

// Import | Internal Functions
import gl_installer from './function/gl_installer';
Expand Down Expand Up @@ -69,6 +70,7 @@ export {
NpmCommandRunner,
StylizedLogger,
TemplateWriter,
SvgToPngConverter,

// Export | Internal Functions
gl_installer,
Expand Down
Loading

0 comments on commit fb924f7

Please sign in to comment.