svg-toolbox 1.1.8 → 1.1.10

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.
Files changed (52) hide show
  1. package/README.md +43 -40
  2. package/es/common.d.ts +3 -36
  3. package/es/common.js +8 -42
  4. package/es/common.js.map +1 -1
  5. package/es/{applyDiffSvg.d.ts → extra-apply/applyDiffSvg.d.ts} +2 -2
  6. package/es/{applyDiffSvg.js → extra-apply/applyDiffSvg.js} +21 -27
  7. package/es/extra-apply/applyDiffSvg.js.map +1 -0
  8. package/es/extra-apply/applyRemoveNanCoordinates.js.map +1 -0
  9. package/es/{applySvg2Png.d.ts → extra-apply/applySvg2Png.d.ts} +3 -3
  10. package/es/extra-apply/applySvg2Png.js +64 -0
  11. package/es/extra-apply/applySvg2Png.js.map +1 -0
  12. package/es/index.d.ts +5 -4
  13. package/es/index.js +5 -4
  14. package/es/index.js.map +1 -1
  15. package/es/utils/pixelLevelDiffPng.d.ts +4 -0
  16. package/es/utils/pixelLevelDiffPng.js +29 -0
  17. package/es/utils/pixelLevelDiffPng.js.map +1 -0
  18. package/es/validate.d.ts +0 -6
  19. package/es/validate.js +14 -8
  20. package/es/validate.js.map +1 -1
  21. package/lib/common.d.ts +3 -36
  22. package/lib/common.js +8 -42
  23. package/lib/common.js.map +1 -1
  24. package/lib/{applyDiffSvg.d.ts → extra-apply/applyDiffSvg.d.ts} +2 -2
  25. package/lib/{applyDiffSvg.js → extra-apply/applyDiffSvg.js} +27 -28
  26. package/lib/extra-apply/applyDiffSvg.js.map +1 -0
  27. package/lib/extra-apply/applyRemoveNanCoordinates.js.map +1 -0
  28. package/lib/{applySvg2Png.d.ts → extra-apply/applySvg2Png.d.ts} +3 -3
  29. package/lib/extra-apply/applySvg2Png.js +111 -0
  30. package/lib/extra-apply/applySvg2Png.js.map +1 -0
  31. package/lib/index.d.ts +5 -4
  32. package/lib/index.js +6 -4
  33. package/lib/index.js.map +1 -1
  34. package/lib/utils/pixelLevelDiffPng.d.ts +4 -0
  35. package/lib/utils/pixelLevelDiffPng.js +36 -0
  36. package/lib/utils/pixelLevelDiffPng.js.map +1 -0
  37. package/lib/validate.d.ts +0 -6
  38. package/lib/validate.js +17 -11
  39. package/lib/validate.js.map +1 -1
  40. package/package.json +5 -2
  41. package/es/applyDiffSvg.js.map +0 -1
  42. package/es/applyRemoveNanCoordinates.js.map +0 -1
  43. package/es/applySvg2Png.js +0 -43
  44. package/es/applySvg2Png.js.map +0 -1
  45. package/lib/applyDiffSvg.js.map +0 -1
  46. package/lib/applyRemoveNanCoordinates.js.map +0 -1
  47. package/lib/applySvg2Png.js +0 -50
  48. package/lib/applySvg2Png.js.map +0 -1
  49. /package/es/{applyRemoveNanCoordinates.d.ts → extra-apply/applyRemoveNanCoordinates.d.ts} +0 -0
  50. /package/es/{applyRemoveNanCoordinates.js → extra-apply/applyRemoveNanCoordinates.js} +0 -0
  51. /package/lib/{applyRemoveNanCoordinates.d.ts → extra-apply/applyRemoveNanCoordinates.d.ts} +0 -0
  52. /package/lib/{applyRemoveNanCoordinates.js → extra-apply/applyRemoveNanCoordinates.js} +0 -0
package/README.md CHANGED
@@ -1,9 +1,15 @@
1
+ <h1 align="center">
2
+ <br/>
3
+ <img width="200" alt="image" src="https://github.com/user-attachments/assets/5fb4fbd0-10b4-4abf-93fd-d98b17845f34" />
4
+ <br/>
5
+ </h1>
6
+
7
+
1
8
  # SVG Utility Functions
2
9
  This module provides utility functions for working with SVG elements and files, including creating, cloning, merging, converting SVG to Base64, comparing SVG images, normalizing path data, and converting SVG to PNG format.
3
10
 
4
11
  [![npm version](https://img.shields.io/npm/v/svg-toolbox.svg?style=flat-square)](https://www.npmjs.com/package/svg-toolbox)
5
12
  [![npm downloads](https://img.shields.io/npm/dt/svg-toolbox.svg?style=flat-square)](https://www.npmjs.com/package/svg-toolbox)
6
- [![deps](https://img.shields.io/github/license/SteamedBread2333/svg-toolbox.svg)](https://www.npmjs.com/package/svg-toolbox)
7
13
 
8
14
  ## Installation
9
15
  ```bash
@@ -22,8 +28,8 @@ npm install svg-toolbox
22
28
  - [convertSVGToBase64](#convertsvgtobase64)
23
29
  - [convertBase64ToSVG](#convertbase64tosvg)
24
30
  - [diffSvg](#diffsvg)
25
- - [removeNanCoordinates](#removenancoordinates)
26
31
  - [svg2png](#svg2png)
32
+ - [removeNanCoordinates](#removenancoordinates)
27
33
  - [License](#license)
28
34
 
29
35
  ## Usage
@@ -33,10 +39,7 @@ npm install svg-toolbox
33
39
  Creates an SVG element from a given SVG content string.
34
40
 
35
41
  ```typescript
36
- import { createSVGElement } from 'svg-toolbox';
37
-
38
- const svgContent = `<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />`;
39
- const svgElement = createSVGElement(svgContent);
42
+ const svgElement = createSVGElement(`<svg><path d="M10 20L30 40Z" /></svg>`);
40
43
  console.log(svgElement);
41
44
  ```
42
45
  ### cloneSVGElement
@@ -92,25 +95,21 @@ console.log(mergedElement);
92
95
  ```
93
96
 
94
97
  ### convertSVGToBase64
95
- Converts an SVG element to a Base64-encoded string.
98
+ Converts an SVG element or SVG string to a Base64-encoded string.
96
99
 
97
100
  ```typescript
98
- import { convertSVGToBase64 } from 'svg-toolbox';
99
- import { JSDOM } from 'jsdom';
101
+ import { createSVGElement, convertSVGToBase64, convertBase64ToSVG } from 'svg-toolbox';
100
102
 
101
- const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
102
- const { document } = dom.window;
103
-
104
- const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
105
- svgElement.setAttribute('cx', '50');
106
- svgElement.setAttribute('cy', '50');
107
- svgElement.setAttribute('r', '40');
108
- svgElement.setAttribute('stroke', 'black');
109
- svgElement.setAttribute('stroke-width', '3');
110
- svgElement.setAttribute('fill', 'red');
103
+ const svgElement = createSVGElement(`<svg><path d="M10 20L30 40Z" /></svg>`);
111
104
 
112
105
  const base64String = convertSVGToBase64(svgElement);
113
- console.log(base64String);
106
+ console.log('convertSVGToBase64 param element', base64String);
107
+
108
+ const svgString = convertBase64ToSVG(base64String);
109
+ console.log('convertBase64ToSVG', svgString);
110
+
111
+ const svgBase64 = convertSVGToBase64(svgString);
112
+ console.log('convertSVGToBase64 param string', svgBase64);
114
113
  ```
115
114
 
116
115
  ### convertBase64ToSVG
@@ -134,31 +133,18 @@ const pathA = 'path/to/first/image.png';
134
133
  const pathB = 'path/to/second/image.png';
135
134
  const diffFilePath = 'path/to/save/diff/image.png';
136
135
 
137
- diffSvg(pathA, pathB, diffFilePath)
138
- .then(({ diffPngBuffer, numDiffPixels }) => {
139
- console.log(`Number of different pixels: ${numDiffPixels}`);
140
- })
141
- .catch(error => {
142
- console.error('Error generating diff image:', error);
143
- });
144
- ```
145
-
146
- ### removeNanCoordinates
147
- Parses and normalizes the d attribute of all path elements in an SVG content.
148
-
149
- ```typescript
150
- import { removeNanCoordinates } from 'svg-toolbox';
151
-
152
- const svgContent = `<svg><path d="M 10,20 nan L 30,40 -nan Z" /></svg>`;
153
- const normalizedSvgContent = removeNanCoordinates(svgContent);
154
- console.log(normalizedSvgContent);
155
-
136
+ // diffPngBuffer is a Buffer object containing the diff image in PNG format.
137
+ // numDiffPixels is the number of different pixels between the two images.
138
+ const { diffPngBuffer, numDiffPixels } = await diffSvg(pathA, pathB, diffFilePath)
139
+ const diffPngBase64 = `data:image/png;base64,${diffPngBuffer.toString('base64')}`;
140
+ console.log(`Number of different pixels: ${numDiffPixels}`);
156
141
  ```
157
142
 
158
143
  ### svg2png
159
144
  Converts an SVG file to PNG format.
160
145
 
161
146
  ```typescript
147
+ // Callback/Promise
162
148
  import { svg2Png } from 'svg-toolbox';
163
149
 
164
150
  const svgPath = 'path/to/input/image.svg';
@@ -166,7 +152,24 @@ const pngPath = 'path/to/output/image.png';
166
152
  const scale = 2; // Scaling factor
167
153
 
168
154
  svg2Png(svgPath, pngPath, scale);
155
+
156
+ // Async/await
157
+ const pngBuffer = await svg2Png(svgPath, scale);
158
+ const pngBase64 = `data:image/png;base64,${pngBuffer.toString('base64')}`;
159
+ console.log(pngBase64);
160
+ ```
161
+
162
+ ### removeNanCoordinates
163
+ Parses and normalizes the d attribute of all path elements in an SVG content.
164
+
165
+ ```typescript
166
+ import { removeNanCoordinates } from 'svg-toolbox';
167
+
168
+ const svgContent = `<svg><path d="M 10,20 nan L 30,40 -nan Z" /></svg>`;
169
+ const normalizedSvgContent = removeNanCoordinates(svgContent);
170
+ console.log(normalizedSvgContent);
171
+
169
172
  ```
170
173
 
171
174
  ## License
172
- MIT License
175
+ MIT License
package/es/common.d.ts CHANGED
@@ -13,17 +13,9 @@
13
13
  *
14
14
  * @param {Element} element - The element to create an SVG element from.
15
15
  * @returns {Element} - The created SVG element.
16
- *
17
- * @example
18
- *
19
- * @param {Element} element - The element to create an SVG element from.
20
- * @returns {Element} - The created SVG element.
21
- *
22
- * @example
23
- * const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
24
- * const svgElement2 = createSVGElement(svgElement);
25
- * @param svgContent
26
- * @returns
16
+ * @see URL_ADDRESS * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core spe
17
+ * @see URL_ADDRESS.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
18
+ * @see URL_ADDRESS * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
27
19
  */
28
20
  export declare function createSVGElement(svgContent: string): Element;
29
21
  /**
@@ -34,12 +26,6 @@ export declare function createSVGElement(svgContent: string): Element;
34
26
  *
35
27
  * @param {Element} element - The SVG element to clone.
36
28
  * @returns {Element} - The cloned SVG element.
37
- *
38
- * @example
39
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
40
- * const clonedElement = cloneSVGElement(svgElement);
41
- * console.log(clonedElement);
42
- *
43
29
  * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
44
30
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
45
31
  */
@@ -52,13 +38,6 @@ export declare function cloneSVGElement(element: Element): Element;
52
38
  *
53
39
  * @param {Element[]} elements - An array of SVG elements to merge.
54
40
  * @returns {Element} - The merged SVG element.
55
- *
56
- * @example
57
- * const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
58
- * const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
59
- * const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
60
- * console.log(mergedElement);
61
- *
62
41
  * @see https://www.w3.org/TR/SVG/ - SVG specification
63
42
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
64
43
  */
@@ -71,12 +50,6 @@ export declare function mergeSVGElements(elements: Element[]): Element;
71
50
  *
72
51
  * @param {Element | string} svgContent - The SVG element or SVG string to convert.
73
52
  * @returns {string} - The Base64-encoded string representation of the SVG element.
74
- *
75
- * @example
76
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
77
- * const base64String = convertSVGToBase64(svgElement);
78
- * console.log(base64String);
79
- *
80
53
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
81
54
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
82
55
  */
@@ -89,12 +62,6 @@ export declare function convertSVGToBase64(svgContent: Element | string): string
89
62
  *
90
63
  * @param {string} base64String - The Base64-encoded string to convert.
91
64
  * @returns {string} - The SVG string representation of the Base64-encoded string.
92
- *
93
- * @example
94
- * const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
95
- * const svgString = convertBase64ToSVG(base64String);
96
- * console.log(svgString);
97
- *
98
65
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
99
66
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
100
67
  */
package/es/common.js CHANGED
@@ -9,8 +9,7 @@ import { JSDOM } from 'jsdom';
9
9
  import { isValidSvgElement, isValidSvgString } from './validate';
10
10
  // Create a virtual DOM environment
11
11
  const dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`);
12
- const fakeWindow = dom.window;
13
- const { document } = fakeWindow;
12
+ const { document } = dom.window;
14
13
  /**
15
14
  * Creates an SVG element from a given element.
16
15
  *
@@ -19,20 +18,12 @@ const { document } = fakeWindow;
19
18
  *
20
19
  * @param {Element} element - The element to create an SVG element from.
21
20
  * @returns {Element} - The created SVG element.
22
- *
23
- * @example
24
- *
25
- * @param {Element} element - The element to create an SVG element from.
26
- * @returns {Element} - The created SVG element.
27
- *
28
- * @example
29
- * const svgElement = document.createElementNS('URL_ADDRESS * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
30
- * const svgElement2 = createSVGElement(svgElement);
31
- * @param svgContent
32
- * @returns
21
+ * @see URL_ADDRESS * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core spe
22
+ * @see URL_ADDRESS.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
23
+ * @see URL_ADDRESS * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
33
24
  */
34
25
  export function createSVGElement(svgContent) {
35
- const svgElement = new fakeWindow.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
26
+ const svgElement = new dom.window.DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
36
27
  return svgElement;
37
28
  }
38
29
  /**
@@ -43,19 +34,13 @@ export function createSVGElement(svgContent) {
43
34
  *
44
35
  * @param {Element} element - The SVG element to clone.
45
36
  * @returns {Element} - The cloned SVG element.
46
- *
47
- * @example
48
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
49
- * const clonedElement = cloneSVGElement(svgElement);
50
- * console.log(clonedElement);
51
- *
52
37
  * @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-6ED8C4D5 - DOM Level 2 Core specification
53
38
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
54
39
  */
55
40
  export function cloneSVGElement(element) {
56
- const serializer = new fakeWindow.XMLSerializer();
41
+ const serializer = new dom.window.XMLSerializer();
57
42
  const sourceCode = serializer.serializeToString(element);
58
- const parser = new fakeWindow.DOMParser();
43
+ const parser = new dom.window.DOMParser();
59
44
  const doc = parser.parseFromString(sourceCode, 'image/svg+xml');
60
45
  return doc.documentElement;
61
46
  }
@@ -67,13 +52,6 @@ export function cloneSVGElement(element) {
67
52
  *
68
53
  * @param {Element[]} elements - An array of SVG elements to merge.
69
54
  * @returns {Element} - The merged SVG element.
70
- *
71
- * @example
72
- * const svgElement1 = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
73
- * const svgElement2 = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
74
- * const mergedElement = mergeSVGElements([svgElement1, svgElement2]);
75
- * console.log(mergedElement);
76
- *
77
55
  * @see https://www.w3.org/TR/SVG/ - SVG specification
78
56
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS - createElementNS documentation
79
57
  */
@@ -92,12 +70,6 @@ export function mergeSVGElements(elements) {
92
70
  *
93
71
  * @param {Element | string} svgContent - The SVG element or SVG string to convert.
94
72
  * @returns {string} - The Base64-encoded string representation of the SVG element.
95
- *
96
- * @example
97
- * const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
98
- * const base64String = convertSVGToBase64(svgElement);
99
- * console.log(base64String);
100
- *
101
73
  * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer - XMLSerializer documentation
102
74
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
103
75
  */
@@ -107,7 +79,7 @@ export function convertSVGToBase64(svgContent) {
107
79
  svgString = svgContent;
108
80
  }
109
81
  else if (isValidSvgElement(svgContent)) {
110
- const serializer = new fakeWindow.XMLSerializer();
82
+ const serializer = new dom.window.XMLSerializer();
111
83
  svgString = serializer.serializeToString(svgContent);
112
84
  }
113
85
  else {
@@ -123,12 +95,6 @@ export function convertSVGToBase64(svgContent) {
123
95
  *
124
96
  * @param {string} base64String - The Base64-encoded string to convert.
125
97
  * @returns {string} - The SVG string representation of the Base64-encoded string.
126
- *
127
- * @example
128
- * const base64String = 'data:image/svg+xml;base64, * const base64String = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3
129
- * const svgString = convertBase64ToSVG(base64String);
130
- * console.log(svgString);
131
- *
132
98
  * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser - DOMParser documentation
133
99
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Buffer - Buffer documentation
134
100
  */
package/es/common.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEjE,mCAAmC;AACnC,MAAM,GAAG,GAAG,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;AACnE,MAAM,UAAU,GAAG,GAAG,CAAC,MAAM,CAAC;AAC9B,MAAM,EAAE,QAAQ,EAAE,GAAG,UAAU,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAAkB;IACjD,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,SAAS,EAAE,CAAC,eAAe,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,eAAe,CAAC;IAC3G,OAAO,UAAU,CAAA;AACnB,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,eAAe,C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C;QACtF,OAAO,SAAS,CAAC;IACnB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;AACH,CAAC"}
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Compares two SVG files and generates a diff image.
3
- * @param {string} pathA - The path of the first SVG file.
4
- * @param {string} pathB - The path of the second SVG file.
3
+ * @param {string} pathA - The path to the first SVG file.
4
+ * @param {string} pathB - The path to the second SVG file.
5
5
  * @param {string} diffFilePath - The path to save the diff image.
6
6
  * @returns - The diff image buffer and the number of different pixels.
7
7
  */
@@ -10,10 +10,8 @@
10
10
  */
11
11
  import fs from 'fs';
12
12
  import sharp from 'sharp';
13
- import pngjs from 'pngjs';
14
13
  import path from 'path';
15
- import pixelmatch from 'pixelmatch';
16
- const PNG = pngjs.PNG;
14
+ import pixelLevelDiffSvg from '../utils/pixelLevelDiffPng';
17
15
  /**
18
16
  * Checks if the filename has a valid suffix.
19
17
  * @param {string} filename - The name of the file.
@@ -24,40 +22,36 @@ function validSuffix(filename) {
24
22
  }
25
23
  /**
26
24
  * Compares two SVG files and generates a diff image.
27
- * @param {string} pathA - The path of the first SVG file.
28
- * @param {string} pathB - The path of the second SVG file.
25
+ * @param {string} pathA - The path to the first SVG file.
26
+ * @param {string} pathB - The path to the second SVG file.
29
27
  * @param {string} diffFilePath - The path to save the diff image.
30
28
  * @returns - The diff image buffer and the number of different pixels.
31
29
  */
32
30
  export default async function (pathA, pathB, diffFilePath) {
33
- // Read the PNG files as buffers
34
- const pngA = await sharp(pathA).toBuffer();
35
- const pngB = await sharp(pathB).toBuffer();
36
- // Decode the PNG buffers
37
- const img1 = PNG.sync.read(pngA);
38
- const img2 = PNG.sync.read(pngB);
39
- const { width, height } = img1;
40
- // Create a new PNG object for the diff image
41
- const diff = new PNG({ width, height });
42
- // Compare the images and get the number of different pixels
43
- const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 });
44
- // Write the diff image to a buffer
45
- const diffPngBuffer = PNG.sync.write(diff);
31
+ let diffFileName = '';
46
32
  // If a diff file path is provided, save the diff image
47
33
  if (diffFilePath) {
48
- const diffFileName = path.basename(diffFilePath);
34
+ diffFileName = path.basename(diffFilePath);
49
35
  if (!validSuffix(diffFileName)) {
50
36
  console.error(`Error converting ${diffFileName} to PNG: No suffix found.`);
51
37
  return;
52
38
  }
53
- fs.writeFileSync(diffFilePath, diffPngBuffer);
54
- // Log the result
55
- if (numDiffPixels === 0) {
56
- console.log(`\x1b[32mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
57
- }
58
- else {
59
- console.log(`\x1b[33mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
60
- }
39
+ }
40
+ else {
41
+ console.error('Error converting to PNG: No diff file path provided.');
42
+ return;
43
+ }
44
+ // Read the PNG files as buffers
45
+ const pngA = await sharp(pathA).toBuffer();
46
+ const pngB = await sharp(pathB).toBuffer();
47
+ const { diffPngBuffer, numDiffPixels } = pixelLevelDiffSvg(pngA, pngB, 0.1);
48
+ fs.writeFileSync(diffFilePath, diffPngBuffer);
49
+ // Log the result
50
+ if (numDiffPixels === 0) {
51
+ console.log(`\x1b[32mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
52
+ }
53
+ else {
54
+ console.log(`\x1b[33mFile name: ${diffFileName} Number of different pixels: ${numDiffPixels}\x1b[0m`);
61
55
  }
62
56
  return {
63
57
  // The diff image buffer
@@ -0,0 +1 @@
1
+ {"version":3,"file":"applyDiffSvg.js","sourceRoot":"","sources":["../../src/extra-apply/applyDiffSvg.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,iBAAiB,MAAM,4BAA4B,CAAC;AAE3D;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AAC,CAAC,CAAC;IAEH,0BAA0B;IAC1B,MAAM,aAAa,GAAG,UAAU,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;IAClD,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -9,8 +9,8 @@
9
9
  /**
10
10
  * Converts an SVG file to PNG format.
11
11
  * @param {string} svgPath - The path to read the SVG file.
12
- * @param {string} pngPath - The path to save the PNG file.
12
+ * @param {string} pngPath - The path to save the PNG file. if not provided, return the buffer.
13
13
  * @param {number} x - The scaling factor for the PNG image.
14
- * @returns
14
+ * @returns {Promise<void | Buffer<ArrayBufferLike>>} - The PNG image buffer.
15
15
  */
16
- export default function (svgPath: string, pngPath: string, x?: number): void;
16
+ export default function (svgPath: string, pngPath?: string, x?: number): Promise<void | Buffer<ArrayBufferLike>>;
@@ -0,0 +1,64 @@
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 - Image processing library
6
+ * @requires fs - File system module
7
+ * @author pipi
8
+ */
9
+ import fs from 'fs';
10
+ import sharp from 'sharp';
11
+ /**
12
+ * Converts an SVG file to PNG format.
13
+ * @param {string} svgPath - The path to read the SVG file.
14
+ * @param {string} pngPath - The path to save the PNG file. if not provided, return the buffer.
15
+ * @param {number} x - The scaling factor for the PNG image.
16
+ * @returns {Promise<void | Buffer<ArrayBufferLike>>} - The PNG image buffer.
17
+ */
18
+ export default async function (svgPath, pngPath, x) {
19
+ if (!svgPath) {
20
+ console.error('Error converting to PNG: No svg file path provided.');
21
+ return;
22
+ }
23
+ if (!pngPath) {
24
+ console.log(`\x1b[33mNo png file path provided, return the buffer.\x1b[0m`);
25
+ }
26
+ if (!x) {
27
+ x = 2;
28
+ console.log(`\x1b[33mNo scaling factor provided, use the default value 2.\x1b[0m`);
29
+ }
30
+ // Read the SVG file
31
+ const file = svgPath.split('/').pop();
32
+ const svgContent = fs.readFileSync(svgPath, 'utf8');
33
+ // Extract the viewBox from the SVG content
34
+ const viewBoxRegex = /viewBox="(\d+) (\d+) (\d+) (\d+)"/;
35
+ const viewBoxMatch = viewBoxRegex.exec(svgContent);
36
+ if (!viewBoxMatch) {
37
+ console.error(`Error converting ${file} to PNG: No viewBox found.`);
38
+ return;
39
+ }
40
+ // Extract the width and height from the viewBox
41
+ const [, , , baseWidth, baseHeight] = viewBoxMatch.map(Number);
42
+ if (pngPath) {
43
+ // Resize the SVG to the desired dimensions and convert it to PNG
44
+ sharp(svgPath)
45
+ .resize(x * baseWidth, x * baseHeight)
46
+ .png()
47
+ .toFile(pngPath)
48
+ .then(() => {
49
+ console.log(`\x1b[32mConverted ${file} to PNG successfully.\x1b[0m`);
50
+ })
51
+ .catch(error => {
52
+ console.error(`Error converting ${file} to PNG:`, error);
53
+ });
54
+ }
55
+ else {
56
+ // Resize the SVG to the desired dimensions and convert it to PNG
57
+ const buffer = await sharp(svgPath)
58
+ .resize(x * baseWidth, x * baseHeight)
59
+ .png()
60
+ .toBuffer();
61
+ return buffer;
62
+ }
63
+ }
64
+ //# sourceMappingURL=applySvg2Png.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"applySvg2Png.js","sourceRoot":"","sources":["../../src/extra-apply/applySvg2Png.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,CAAC,KAAK,WAAW,OAAe,EAAE,OAAgB,EAAE,CAAU;IAC1E,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,qDAAqD,CAAC,CAAC;QACrE,OAAO;IACT,CAAC;IACD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,CAAC,GAAG,CAAC,8DAA8D,CAAC,CAAC;IAC9E,CAAC;IACD,IAAI,CAAC,CAAC,EAAE,CAAC;QACP,CAAC,GAAG,C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E;aACL,QAAQ,EAAE,CAAA;QACb,OAAO,MAAM,CAAC;IAChB,CAAC;AACH,CAAC"}
package/es/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import svg2Png from './applySvg2Png';
2
- import diffSvg from './applyDiffSvg';
3
- import removeNanCoordinates from './applyRemoveNanCoordinates';
1
+ import svg2Png from './extra-apply/applySvg2Png';
2
+ import diffSvg from './extra-apply/applyDiffSvg';
3
+ import removeNanCoordinates from './extra-apply/applyRemoveNanCoordinates';
4
4
  import { createSVGElement, cloneSVGElement, mergeSVGElements, convertSVGToBase64, convertBase64ToSVG } from './common';
5
+ import pixelLevelDiffPng from './utils/pixelLevelDiffPng';
5
6
  declare const removeEmptyCoordinates: typeof removeNanCoordinates;
6
7
  export { svg2Png, diffSvg,
7
8
  /**
8
9
  * @deprecated
9
10
  * @see removeNanCoordinates
10
11
  */
11
- removeEmptyCoordinates, removeNanCoordinates, createSVGElement, cloneSVGElement, mergeSVGElements, convertSVGToBase64, convertBase64ToSVG };
12
+ removeEmptyCoordinates, removeNanCoordinates, createSVGElement, cloneSVGElement, mergeSVGElements, convertSVGToBase64, convertBase64ToSVG, pixelLevelDiffPng };
package/es/index.js CHANGED
@@ -1,12 +1,13 @@
1
- import svg2Png from './applySvg2Png';
2
- import diffSvg from './applyDiffSvg';
3
- import removeNanCoordinates from './applyRemoveNanCoordinates';
1
+ import svg2Png from './extra-apply/applySvg2Png';
2
+ import diffSvg from './extra-apply/applyDiffSvg';
3
+ import removeNanCoordinates from './extra-apply/applyRemoveNanCoordinates';
4
4
  import { createSVGElement, cloneSVGElement, mergeSVGElements, convertSVGToBase64, convertBase64ToSVG } from './common';
5
+ import pixelLevelDiffPng from './utils/pixelLevelDiffPng';
5
6
  const removeEmptyCoordinates = removeNanCoordinates;
6
7
  export { svg2Png, diffSvg,
7
8
  /**
8
9
  * @deprecated
9
10
  * @see removeNanCoordinates
10
11
  */
11
- removeEmptyCoordinates, removeNanCoordinates, createSVGElement, cloneSVGElement, mergeSVGElements, convertSVGToBase64, convertBase64ToSVG };
12
+ removeEmptyCoordinates, removeNanCoordinates, createSVGElement, cloneSVGElement, mergeSVGElements, convertSVGToBase64, convertBase64ToSVG, pixelLevelDiffPng };
12
13
  //# sourceMappingURL=index.js.map
package/es/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,gBAAgB,CAAC;AACrC,OAAO,OAAO,MAAM,gBAAgB,CAAC;AACrC,OAAO,oBAAoB,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAEtH,MAAM,sBAAsB,GAAG,oBAAoB,CAAA;AAEnD,OAAO,EACL,OAAO,EACP,OAAO;AACP;;;GAGG;AACH,sBAAsB,EACtB,oBAAoB,EACpB,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,kBAAkB,EAC5F,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,4BAA4B,CAAC;AACjD,OAAO,OAAO,MAAM,4BAA4B,CAAC;AACjD,OAAO,oBAAoB,MAAM,yCAAyC,CAAC;AAC3E,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AACtH,OAAO,iBAAiB,MAAM,2BAA2B,CAAC;AAE1D,MAAM,sBAAsB,GAAG,oBAAoB,CAAA;AAEnD,OAAO,EACL,OAAO,EACP,OAAO;AACP;;;GAGG;AACH,sBAAsB,EACtB,oBAAoB,EACpB,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,kBAAkB,EAC3F,iBAAiB,EAClB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export default function (pngA: Buffer<ArrayBufferLike>, pngB: Buffer<ArrayBufferLike>, threshold?: number): {
2
+ diffPngBuffer: Buffer<ArrayBufferLike>;
3
+ numDiffPixels: number;
4
+ };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @file pixelLevelDiffSvg.ts
3
+ * @description This module provides a function to compare two SVG files and generate a diff image.
4
+ * @module applyDiffSvg
5
+ * @requires pixelmatch - Image comparison library
6
+ * @requires pngjs - PNG image processing library
7
+ * @author pipi
8
+ */
9
+ import { PNG } from "pngjs";
10
+ import pixelmatch from 'pixelmatch';
11
+ export default function (pngA, pngB, threshold = 0.1) {
12
+ // Decode the PNG buffers
13
+ const img1 = PNG.sync.read(pngA);
14
+ const img2 = PNG.sync.read(pngB);
15
+ const { width, height } = img1;
16
+ // Create a new PNG object for the diff image
17
+ const diff = new PNG({ width, height });
18
+ // Compare the images and get the number of different pixels
19
+ const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold });
20
+ // Write the diff image to a buffer
21
+ const diffPngBuffer = PNG.sync.write(diff);
22
+ return {
23
+ // The diff image buffer
24
+ diffPngBuffer,
25
+ // The number of different pixels
26
+ numDiffPixels
27
+ };
28
+ }
29
+ //# sourceMappingURL=pixelLevelDiffPng.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pixelLevelDiffPng.js","sourceRoot":"","sources":["../../src/utils/pixelLevelDiffPng.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAC5B,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,MAAM,CAAC,OAAO,WAAW,IAA6B,EAAE,IAA6B,EAAE,YAAoB,GAAG;IAC5G,yBAAyB;IACzB,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;IAE/B,6CAA6C;IAC7C,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;IAExC,4DAA4D;IAC5D,MAAM,aAAa,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;IAEhG,mCAAmC;IACnC,MAAM,aAAa,GAAG,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAE3C,OAAO;QACL,wBAAwB;QACxB,aAAa;QACb,iCAAiC;QACjC,aAAa;KACd,CAAC;AACJ,CAAC"}
package/es/validate.d.ts CHANGED
@@ -1,9 +1,3 @@
1
- /**
2
- * @file validate.ts
3
- * @description This module provides functions to validate SVG content.
4
- * @module validate
5
- * @author pipi
6
- */
7
1
  /**
8
2
  * Validates whether the provided content is a valid SVG string.
9
3
  *
package/es/validate.js CHANGED
@@ -4,6 +4,7 @@
4
4
  * @module validate
5
5
  * @author pipi
6
6
  */
7
+ import { JSDOM } from 'jsdom';
7
8
  /**
8
9
  * Validates whether the provided content is a valid SVG string.
9
10
  *
@@ -15,11 +16,18 @@ export function isValidSvgString(content) {
15
16
  if (typeof content !== 'string') {
16
17
  return false;
17
18
  }
18
- // Regular expression to find the <svg> opening tag in the string.
19
- const svgRegExp = /<svg(\s+[^>]*)?>/i;
20
- const startTag = content.match(svgRegExp);
21
- // Check if a matching <svg> tag was found and it's in lowercase (case-insensitive check).
22
- return !!(startTag && startTag[0].toLowerCase().includes('<svg'));
19
+ try {
20
+ // Create a virtual DOM environment
21
+ const dom = new JSDOM(content);
22
+ // Parse the content as SVG and check if the root node is an SVG element.
23
+ const rootNode = new dom.window.DOMParser().parseFromString(content, 'image/svg+xml').documentElement;
24
+ // Return true if the root node is an SVG element, false otherwise.
25
+ return rootNode.nodeName.toLowerCase() === 'svg';
26
+ }
27
+ catch (error) {
28
+ // If there's an error during parsing, it's not a valid SVG string.
29
+ return false;
30
+ }
23
31
  }
24
32
  /**
25
33
  * Validates whether the provided content is a valid SVG element.
@@ -31,8 +39,6 @@ export function isValidSvgElement(content) {
31
39
  // Check if the content is an instance of Element and its tag name is 'svg'
32
40
  return (content &&
33
41
  typeof content === 'object' &&
34
- content.tagName.toLowerCase() === 'svg' &&
35
- content.namespaceURI === 'http://www.w3.org/2000/svg' // SVG namespace URI
36
- );
42
+ content.tagName.toLowerCase() === 'svg');
37
43
  }
38
44
  //# sourceMappingURL=validate.js.map