svg-toolbox 1.0.2 → 1.0.4
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/lib/applyDiffSvg.js +0 -5
- package/lib/applyRemoveNanCoordinates.js +45 -0
- package/lib/index.js +3 -1
- package/package.json +1 -1
package/lib/applyDiffSvg.js
CHANGED
|
@@ -49,11 +49,6 @@ module.exports = async function (pathA, pathB, diffFilePath) {
|
|
|
49
49
|
// Compare the images and get the number of different pixels
|
|
50
50
|
const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 });
|
|
51
51
|
|
|
52
|
-
// Ensure the diff directory exists
|
|
53
|
-
if (!fs.existsSync('diff')) {
|
|
54
|
-
fs.mkdirSync('diff', { recursive: true });
|
|
55
|
-
}
|
|
56
|
-
|
|
57
52
|
// Write the diff image to a buffer
|
|
58
53
|
const diffPngBuffer = PNG.sync.write(diff);
|
|
59
54
|
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file applyRemoveNanCoordinates.js
|
|
3
|
+
* @description This module provides a function to parse and normalize the 'd' attribute of all path elements in an SVG content.
|
|
4
|
+
* @requires jsdom - JavaScript DOM library
|
|
5
|
+
* @module applyRemoveNanCoordinates
|
|
6
|
+
* @author pipi
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const { JSDOM } = require('jsdom');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Parses and normalizes the 'd' attribute of all path elements in an SVG content.
|
|
13
|
+
* @param {*} svgContent
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
module.exports = function (svgContent) {
|
|
17
|
+
// Create a DOM from the SVG content
|
|
18
|
+
const dom = new JSDOM(svgContent, {
|
|
19
|
+
contentType: 'image/svg+xml' // Set content type to SVG
|
|
20
|
+
});
|
|
21
|
+
const document = dom.window.document;
|
|
22
|
+
const svgElement = document.querySelector('svg');
|
|
23
|
+
const paths = svgElement.querySelectorAll('path');
|
|
24
|
+
|
|
25
|
+
// Iterate over each path element
|
|
26
|
+
Array.from(paths).forEach(path => {
|
|
27
|
+
const d = path.getAttribute('d'); // Get the 'd' attribute
|
|
28
|
+
const commands = d.split(/(?=[MLHVCSQTAZ])/).map(command => {
|
|
29
|
+
const type = command[0]; // Command type (e.g., M, L, H, etc.)
|
|
30
|
+
const params = command.slice(1).trim().split(/[\s,]+/).filter(Number); // Command parameters
|
|
31
|
+
return { type, params };
|
|
32
|
+
}).filter(command => (command.type === 'Z' || command.params.length > 0)); // Filter out invalid commands
|
|
33
|
+
|
|
34
|
+
// Reconstruct the 'd' attribute
|
|
35
|
+
const modifiedD = commands.map(command => {
|
|
36
|
+
return command.type + command.params.join(' ');
|
|
37
|
+
}).join('');
|
|
38
|
+
path.setAttribute('d', modifiedD); // Set the modified 'd' attribute
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Get the new SVG content
|
|
42
|
+
const newSvgContent = svgElement.outerHTML.trim();
|
|
43
|
+
|
|
44
|
+
return newSvgContent;
|
|
45
|
+
}
|
package/lib/index.js
CHANGED