svg-toolbox 1.0.1 → 1.0.3
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/README.md +21 -0
- package/lib/applyDiffSvg.js +44 -9
- package/lib/applySvg2Png.js +9 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,2 +1,23 @@
|
|
|
1
1
|
# svg-toolbox
|
|
2
2
|
This library provides some SVG-related tools
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
```bash
|
|
6
|
+
npm install svg-toolbox
|
|
7
|
+
```
|
|
8
|
+
## Usage
|
|
9
|
+
```js
|
|
10
|
+
const { svg2png } = require('svg-toolbox')
|
|
11
|
+
|
|
12
|
+
...
|
|
13
|
+
svg2png(svgPath, pngSavePath, x)
|
|
14
|
+
...
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
const { diffSvg } = require('svg-toolbox')
|
|
19
|
+
|
|
20
|
+
...
|
|
21
|
+
diffSvg(svgPath1, svgPath2, diffResultSavePath)
|
|
22
|
+
...
|
|
23
|
+
```
|
package/lib/applyDiffSvg.js
CHANGED
|
@@ -1,38 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file applyDiffSvg.js
|
|
3
|
+
* @description This module provides a function to compare two PNG images and generate a diff image using the sharp and pixelmatch libraries.
|
|
4
|
+
* @module applyDiffSvg
|
|
5
|
+
* @requires sharp - Image processing library
|
|
6
|
+
* @requires pixelmatch - Image comparison library
|
|
7
|
+
* @requires fs - File system module
|
|
8
|
+
* @requires path - Path module
|
|
9
|
+
* @author pipi
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
const fs = require('fs');
|
|
2
13
|
const sharp = require('sharp');
|
|
3
14
|
const PNG = require('pngjs').PNG;
|
|
4
15
|
const path = require('path');
|
|
5
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the filename has a valid suffix.
|
|
19
|
+
* @param {string} filename - The name of the file.
|
|
20
|
+
* @returns {boolean} - True if the filename has a suffix, false otherwise.
|
|
21
|
+
*/
|
|
6
22
|
function validSuffix(filename) {
|
|
7
23
|
return path.extname(filename) !== '';
|
|
8
24
|
}
|
|
9
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Compares two SVG files and generates a diff image.
|
|
28
|
+
* @param {string} pathA - The path of the first SVG file.
|
|
29
|
+
* @param {string} pathB - The path of the second SVG file.
|
|
30
|
+
* @param {string} diffFilePath - The path to save the diff image.
|
|
31
|
+
* @returns - The diff image buffer and the number of different pixels.
|
|
32
|
+
*/
|
|
10
33
|
module.exports = async function (pathA, pathB, diffFilePath) {
|
|
34
|
+
// Read the PNG files as buffers
|
|
11
35
|
const pngA = await sharp(pathA).toBuffer();
|
|
12
36
|
const pngB = await sharp(pathB).toBuffer();
|
|
37
|
+
|
|
38
|
+
// Import pixelmatch library
|
|
13
39
|
const pixelmatch = (await import('pixelmatch')).default;
|
|
40
|
+
|
|
41
|
+
// Decode the PNG buffers
|
|
14
42
|
const img1 = PNG.sync.read(pngA);
|
|
15
43
|
const img2 = PNG.sync.read(pngB);
|
|
16
44
|
const { width, height } = img1;
|
|
45
|
+
|
|
46
|
+
// Create a new PNG object for the diff image
|
|
17
47
|
const diff = new PNG({ width, height });
|
|
18
48
|
|
|
49
|
+
// Compare the images and get the number of different pixels
|
|
19
50
|
const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 });
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
51
|
+
|
|
52
|
+
// Write the diff image to a buffer
|
|
23
53
|
const diffPngBuffer = PNG.sync.write(diff);
|
|
54
|
+
|
|
55
|
+
// If a diff file path is provided, save the diff image
|
|
24
56
|
if (diffFilePath) {
|
|
25
|
-
const diffFileName =
|
|
57
|
+
const diffFileName = path.basename(diffFilePath);
|
|
26
58
|
if (!validSuffix(diffFileName)) {
|
|
27
|
-
|
|
59
|
+
console.error(`Error converting ${diffFileName} to PNG: No suffix found.`);
|
|
60
|
+
return;
|
|
28
61
|
}
|
|
29
|
-
fs.writeFileSync(
|
|
62
|
+
fs.writeFileSync(diffFilePath, diffPngBuffer);
|
|
63
|
+
|
|
64
|
+
// Log the result
|
|
30
65
|
if (numDiffPixels === 0) {
|
|
31
|
-
console.log(`\x1b[
|
|
32
|
-
return { diffPngBuffer, numDiffPixels };
|
|
66
|
+
console.log(`\x1b[32mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
|
|
33
67
|
} else {
|
|
34
|
-
console.log(`\x1b[
|
|
68
|
+
console.log(`\x1b[33mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
|
|
35
69
|
}
|
|
36
70
|
}
|
|
71
|
+
|
|
37
72
|
return { diffPngBuffer, numDiffPixels };
|
|
38
73
|
}
|
package/lib/applySvg2Png.js
CHANGED
|
@@ -2,13 +2,21 @@
|
|
|
2
2
|
* @file applySvg2Png.js
|
|
3
3
|
* @description This module provides a function to convert SVG files to PNG format using the sharp library.
|
|
4
4
|
* @module applySvg2Png
|
|
5
|
-
* @requires sharp
|
|
5
|
+
* @requires sharp - Image processing library
|
|
6
|
+
* @requires fs - File system module
|
|
6
7
|
* @author pipi
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
const sharp = require('sharp');
|
|
10
11
|
const fs = require('fs');
|
|
11
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Converts an SVG file to PNG format.
|
|
15
|
+
* @param {string} svgPath - The path to read the SVG file.
|
|
16
|
+
* @param {string} pngPath - The path to save the PNG file.
|
|
17
|
+
* @param {number} x - The scaling factor for the PNG image.
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
12
20
|
module.exports = function (svgPath, pngPath, x = 2) {
|
|
13
21
|
// Read the SVG file
|
|
14
22
|
const file = svgPath.split('/').pop();
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svg-toolbox",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "This library provides some SVG-related tools",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"repository": "https://github.com/SteamedBread2333/svg-toolbox",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
9
|
},
|
|
@@ -16,4 +17,4 @@
|
|
|
16
17
|
"pngjs": "^7.0.0",
|
|
17
18
|
"sharp": "^0.33.5"
|
|
18
19
|
}
|
|
19
|
-
}
|
|
20
|
+
}
|