svg-toolbox 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 pipi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # svg-toolbox
2
+ This library provides some SVG-related tools
@@ -0,0 +1,38 @@
1
+ const fs = require('fs');
2
+ const sharp = require('sharp');
3
+ const PNG = require('pngjs').PNG;
4
+ const path = require('path');
5
+
6
+ function validSuffix(filename) {
7
+ return path.extname(filename) !== '';
8
+ }
9
+
10
+ module.exports = async function (pathA, pathB, diffFilePath) {
11
+ const pngA = await sharp(pathA).toBuffer();
12
+ const pngB = await sharp(pathB).toBuffer();
13
+ const pixelmatch = (await import('pixelmatch')).default;
14
+ const img1 = PNG.sync.read(pngA);
15
+ const img2 = PNG.sync.read(pngB);
16
+ const { width, height } = img1;
17
+ const diff = new PNG({ width, height });
18
+
19
+ const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 });
20
+ if (!fs.existsSync('diff')) {
21
+ fs.mkdirSync('diff', { recursive: true });
22
+ }
23
+ const diffPngBuffer = PNG.sync.write(diff);
24
+ if (diffFilePath) {
25
+ const diffFileName = diffFilePath.split('/').pop();
26
+ if (!validSuffix(diffFileName)) {
27
+ return console.error(`Error converting ${diffFileName} to PNG: No suffix found.`);
28
+ }
29
+ fs.writeFileSync(`${diffFilePath}`, diffPngBuffer);
30
+ if (numDiffPixels === 0) {
31
+ console.log(`\x1b[${32}mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
32
+ return { diffPngBuffer, numDiffPixels };
33
+ } else {
34
+ console.log(`\x1b[${33}mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
35
+ }
36
+ }
37
+ return { diffPngBuffer, numDiffPixels };
38
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @file applySvg2Png.js
3
+ * @description This module provides a function to convert SVG files to PNG format using the sharp library.
4
+ * @module applySvg2Png
5
+ * @requires sharp
6
+ * @author pipi
7
+ */
8
+
9
+ const sharp = require('sharp');
10
+ const fs = require('fs');
11
+
12
+ module.exports = function (svgPath, pngPath, x = 2) {
13
+ // Read the SVG file
14
+ const file = svgPath.split('/').pop();
15
+ const svgContent = fs.readFileSync(svgPath, 'utf8');
16
+
17
+ // Extract the viewBox from the SVG content
18
+ const viewBoxRegex = /viewBox="(\d+) (\d+) (\d+) (\d+)"/;
19
+ const viewBoxMatch = viewBoxRegex.exec(svgContent);
20
+
21
+ if (!viewBoxMatch) {
22
+ console.error(`Error converting ${file} to PNG: No viewBox found.`);
23
+ return;
24
+ }
25
+
26
+ // Extract the width and height from the viewBox
27
+ const [, , , baseWidth, baseHeight] = viewBoxMatch.map(Number);
28
+
29
+ // Resize the SVG to the desired dimensions and convert it to PNG
30
+ sharp(svgPath)
31
+ .resize(x * baseWidth, x * baseHeight)
32
+ .png()
33
+ .toFile(pngPath)
34
+ .then(() => {
35
+ console.log(`\x1b[32mConverted ${file} to PNG successfully.\x1b[0m`);
36
+ })
37
+ .catch(error => {
38
+ console.error(`Error converting ${file} to PNG:`, error);
39
+ });
40
+ }
package/lib/index.js ADDED
@@ -0,0 +1,7 @@
1
+ const svg2Png = require('./applySvg2Png');
2
+ const diffSvg = require('./applyDiffSvg');
3
+
4
+ module.exports = {
5
+ svg2Png,
6
+ diffSvg
7
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "svg-toolbox",
3
+ "version": "1.0.0",
4
+ "description": "This library provides some SVG-related tools",
5
+ "main": "lib/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "svg"
11
+ ],
12
+ "author": "pipi",
13
+ "license": "MIT",
14
+ "devDependencies": {
15
+ "pixelmatch": "^6.0.0",
16
+ "pngjs": "^7.0.0",
17
+ "sharp": "^0.33.5"
18
+ }
19
+ }