frontbacked-svg 5.0.0 → 5.0.2
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/dist/main.cjs +211 -86
- package/dist/main.d.cts +7 -4
- package/dist/main.d.ts +7 -4
- package/dist/main.mjs +211 -86
- package/package.json +2 -1
- package/src/base64Image.ts +100 -100
- package/src/cssParser.ts +58 -58
- package/src/filters.ts +225 -225
- package/src/font-utils.ts +192 -204
- package/src/get-default-fields-value.ts +63 -63
- package/src/getSvg.ts +476 -284
- package/src/imageHelper.ts +295 -293
- package/src/imagePassportUtils.ts +699 -717
- package/src/images-processor.ts +179 -186
- package/src/polyfills/DOMParser.ts +27 -27
- package/src/polyfills/File.ts +19 -19
- package/src/polyfills/Image.ts +89 -89
- package/src/svgScaler.ts +178 -178
- package/src/textGenCodeParser.ts +318 -318
- package/src/time.ts +146 -146
- package/src/toolsFunc.ts +78 -78
- package/src/types.ts +176 -176
- package/src/utils.ts +757 -757
- package/src/watermaker.ts +189 -189
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "frontbacked-svg",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.2",
|
|
4
4
|
"description": "frontbacked svg renderer",
|
|
5
5
|
"types": "dist/main.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -95,6 +95,7 @@
|
|
|
95
95
|
},
|
|
96
96
|
"dependencies": {
|
|
97
97
|
"@types/firebase": "^3.2.1",
|
|
98
|
+
"axios": "^1.9.0",
|
|
98
99
|
"canvas": "^3.1.0",
|
|
99
100
|
"jsdom": "^26.0.0",
|
|
100
101
|
"jspdf": "^2.5.1",
|
package/src/base64Image.ts
CHANGED
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import { ImageWrapper } from "./polyfills/Image.ts";
|
|
2
|
-
|
|
3
|
-
const isNode = typeof window === 'undefined';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Gets the dimensions (width and height) of an image from a base64 string.
|
|
7
|
-
*
|
|
8
|
-
* @param base64Image - The base64 string of the image.
|
|
9
|
-
* @returns A Promise that resolves to an object containing the base64 string, width, and height of the image.
|
|
10
|
-
*/
|
|
11
|
-
export function getImageDimensions(base64Image: string): Promise<{ width: number, height: number }> {
|
|
12
|
-
return new Promise<{ width: number, height: number }>((resolve, reject) => {
|
|
13
|
-
const img = new Image();
|
|
14
|
-
|
|
15
|
-
img.onload = () => {
|
|
16
|
-
resolve({
|
|
17
|
-
width: img.width,
|
|
18
|
-
height: img.height,
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
img.onerror = (error) => {
|
|
23
|
-
reject(new Error(`Failed to load image: ${error}`));
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
img.src = base64Image;
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Resizes a base64 image string to the given width while maintaining the aspect ratio.
|
|
32
|
-
*
|
|
33
|
-
* @param base64Image - The base64 string of the image.
|
|
34
|
-
* @param targetWidth - The desired width for the resized image.
|
|
35
|
-
* @returns A Promise that resolves to the resized image as a base64 string.
|
|
36
|
-
*/
|
|
37
|
-
export async function resizeBase64Image(base64Image: string, targetWidth: number): Promise<string> {
|
|
38
|
-
return new Promise<string>((resolve, reject) => {
|
|
39
|
-
const img = new Image();
|
|
40
|
-
|
|
41
|
-
// Set up the onload handler to resize the image once it's loaded
|
|
42
|
-
img.onload = () => {
|
|
43
|
-
const aspectRatio = img.width / img.height;
|
|
44
|
-
const targetHeight = targetWidth / aspectRatio;
|
|
45
|
-
|
|
46
|
-
// Create a canvas and draw the resized image on it
|
|
47
|
-
const canvas = document.createElement('canvas');
|
|
48
|
-
canvas.width = targetWidth;
|
|
49
|
-
canvas.height = targetHeight;
|
|
50
|
-
const ctx = canvas.getContext('2d') as any;
|
|
51
|
-
|
|
52
|
-
if (ctx) {
|
|
53
|
-
const realImage = ((img as any) as ImageWrapper).realImage? ((img as any) as ImageWrapper).realImage : img
|
|
54
|
-
ctx.drawImage(realImage, 0, 0, targetWidth, targetHeight);
|
|
55
|
-
|
|
56
|
-
// Convert the canvas to a base64 string and resolve the promise
|
|
57
|
-
const resizedBase64Image = canvas.toDataURL('image/png');
|
|
58
|
-
resolve(resizedBase64Image);
|
|
59
|
-
} else {
|
|
60
|
-
reject(new Error('Failed to get canvas context'));
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// Set up the onerror handler to reject the promise if the image fails to load
|
|
65
|
-
img.onerror = (error) => {
|
|
66
|
-
reject(new Error(`Failed to load image: ${error}`));
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
// Set the src attribute to the base64 image string to start loading the image
|
|
70
|
-
img.src = base64Image;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function base64UrlToFile(base64Url: string, fileName?: string, fileType?: string): File {
|
|
75
|
-
// Extract base64 data and the MIME type from the header
|
|
76
|
-
const [header, base64Data] = base64Url.split(',');
|
|
77
|
-
|
|
78
|
-
// Infer the file type from the header if not provided
|
|
79
|
-
fileType = fileType || header.match(/:(.*?);/)?.[1];
|
|
80
|
-
|
|
81
|
-
// If the fileName is not provided, generate a random one with an appropriate extension
|
|
82
|
-
if (!fileName) {
|
|
83
|
-
const extension = fileType?.split('/')[1] || 'bin'; // Default to 'bin' if no type is inferred
|
|
84
|
-
fileName = `file_${Date.now()}.${extension}`;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Decode the base64 string to a binary string
|
|
88
|
-
const binaryString = window.atob(base64Data);
|
|
89
|
-
const len = binaryString.length;
|
|
90
|
-
const bytes = new Uint8Array(len);
|
|
91
|
-
|
|
92
|
-
for (let i = 0; i < len; i++) {
|
|
93
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Create a Blob from the decoded data
|
|
97
|
-
const blob = new Blob([bytes], { type: fileType });
|
|
98
|
-
|
|
99
|
-
// Create and return a File from the Blob
|
|
100
|
-
return new File([blob], fileName, { type: fileType });
|
|
1
|
+
import { ImageWrapper } from "./polyfills/Image.ts";
|
|
2
|
+
|
|
3
|
+
const isNode = typeof window === 'undefined';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Gets the dimensions (width and height) of an image from a base64 string.
|
|
7
|
+
*
|
|
8
|
+
* @param base64Image - The base64 string of the image.
|
|
9
|
+
* @returns A Promise that resolves to an object containing the base64 string, width, and height of the image.
|
|
10
|
+
*/
|
|
11
|
+
export function getImageDimensions(base64Image: string): Promise<{ width: number, height: number }> {
|
|
12
|
+
return new Promise<{ width: number, height: number }>((resolve, reject) => {
|
|
13
|
+
const img = new Image();
|
|
14
|
+
|
|
15
|
+
img.onload = () => {
|
|
16
|
+
resolve({
|
|
17
|
+
width: img.width,
|
|
18
|
+
height: img.height,
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
img.onerror = (error) => {
|
|
23
|
+
reject(new Error(`Failed to load image: ${error}`));
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
img.src = base64Image;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Resizes a base64 image string to the given width while maintaining the aspect ratio.
|
|
32
|
+
*
|
|
33
|
+
* @param base64Image - The base64 string of the image.
|
|
34
|
+
* @param targetWidth - The desired width for the resized image.
|
|
35
|
+
* @returns A Promise that resolves to the resized image as a base64 string.
|
|
36
|
+
*/
|
|
37
|
+
export async function resizeBase64Image(base64Image: string, targetWidth: number): Promise<string> {
|
|
38
|
+
return new Promise<string>((resolve, reject) => {
|
|
39
|
+
const img = new Image();
|
|
40
|
+
|
|
41
|
+
// Set up the onload handler to resize the image once it's loaded
|
|
42
|
+
img.onload = () => {
|
|
43
|
+
const aspectRatio = img.width / img.height;
|
|
44
|
+
const targetHeight = targetWidth / aspectRatio;
|
|
45
|
+
|
|
46
|
+
// Create a canvas and draw the resized image on it
|
|
47
|
+
const canvas = document.createElement('canvas');
|
|
48
|
+
canvas.width = targetWidth;
|
|
49
|
+
canvas.height = targetHeight;
|
|
50
|
+
const ctx = canvas.getContext('2d') as any;
|
|
51
|
+
|
|
52
|
+
if (ctx) {
|
|
53
|
+
const realImage = ((img as any) as ImageWrapper).realImage? ((img as any) as ImageWrapper).realImage : img
|
|
54
|
+
ctx.drawImage(realImage, 0, 0, targetWidth, targetHeight);
|
|
55
|
+
|
|
56
|
+
// Convert the canvas to a base64 string and resolve the promise
|
|
57
|
+
const resizedBase64Image = canvas.toDataURL('image/png');
|
|
58
|
+
resolve(resizedBase64Image);
|
|
59
|
+
} else {
|
|
60
|
+
reject(new Error('Failed to get canvas context'));
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Set up the onerror handler to reject the promise if the image fails to load
|
|
65
|
+
img.onerror = (error) => {
|
|
66
|
+
reject(new Error(`Failed to load image: ${error}`));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Set the src attribute to the base64 image string to start loading the image
|
|
70
|
+
img.src = base64Image;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function base64UrlToFile(base64Url: string, fileName?: string, fileType?: string): File {
|
|
75
|
+
// Extract base64 data and the MIME type from the header
|
|
76
|
+
const [header, base64Data] = base64Url.split(',');
|
|
77
|
+
|
|
78
|
+
// Infer the file type from the header if not provided
|
|
79
|
+
fileType = fileType || header.match(/:(.*?);/)?.[1];
|
|
80
|
+
|
|
81
|
+
// If the fileName is not provided, generate a random one with an appropriate extension
|
|
82
|
+
if (!fileName) {
|
|
83
|
+
const extension = fileType?.split('/')[1] || 'bin'; // Default to 'bin' if no type is inferred
|
|
84
|
+
fileName = `file_${Date.now()}.${extension}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Decode the base64 string to a binary string
|
|
88
|
+
const binaryString = window.atob(base64Data);
|
|
89
|
+
const len = binaryString.length;
|
|
90
|
+
const bytes = new Uint8Array(len);
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < len; i++) {
|
|
93
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Create a Blob from the decoded data
|
|
97
|
+
const blob = new Blob([bytes], { type: fileType });
|
|
98
|
+
|
|
99
|
+
// Create and return a File from the Blob
|
|
100
|
+
return new File([blob], fileName, { type: fileType });
|
|
101
101
|
}
|
package/src/cssParser.ts
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import { Declaration } from "./types.ts";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export const getIdentifier = (selectorOrProperty: string, propertyValue?: string) => {
|
|
5
|
-
return `${selectorOrProperty.replace(/\s/g, "")}${propertyValue? `:${propertyValue.replace(/\s/g, "")}` : ""}`
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function parseValueUnit(input: string) {
|
|
9
|
-
const match = input.match(/^([-+]?\d*\.?\d+)([a-zA-Z]+)$/);
|
|
10
|
-
if (match) {
|
|
11
|
-
return {
|
|
12
|
-
value: match[1],
|
|
13
|
-
unit: match[2]
|
|
14
|
-
};
|
|
15
|
-
} else {
|
|
16
|
-
throw new Error(`Invalid input format: ${input}`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function parseAndModifyCSS(cssString: string, callback: (selector: string, declarations: Declaration[]) => Declaration[]) {
|
|
21
|
-
const cssObject: {[x: string]: Declaration[]} = {};
|
|
22
|
-
|
|
23
|
-
// Regular expression to match selectors and their declarations
|
|
24
|
-
const regex = /([^{]+)\{([^}]+)\}/g;
|
|
25
|
-
let match;
|
|
26
|
-
|
|
27
|
-
// Parse the CSS content into an object
|
|
28
|
-
while ((match = regex.exec(cssString)) !== null) {
|
|
29
|
-
const selector = match[1].trim();
|
|
30
|
-
const declarations = match[2].trim().split(';').filter(Boolean).map(decl => {
|
|
31
|
-
const [property, value] = decl.split(':').map(item => item.trim());
|
|
32
|
-
return { property, value };
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
cssObject[selector] = declarations;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Function to process each selector and its declarations using the callback
|
|
39
|
-
Object.keys(cssObject).forEach(selector => {
|
|
40
|
-
const declarations = cssObject[selector];
|
|
41
|
-
|
|
42
|
-
// Call the callback, passing the selector and its declarations
|
|
43
|
-
const updatedDeclarations = callback(selector, declarations);
|
|
44
|
-
|
|
45
|
-
// Replace the original declarations with the returned value
|
|
46
|
-
cssObject[selector] = updatedDeclarations;
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Convert the object back to a CSS string
|
|
50
|
-
let newCssString = '';
|
|
51
|
-
Object.keys(cssObject).forEach(selector => {
|
|
52
|
-
const declarations = cssObject[selector]
|
|
53
|
-
.map(({ property, value }) => `${property}: ${value};`)
|
|
54
|
-
.join(' ');
|
|
55
|
-
newCssString += `${selector} { ${declarations} }\n`;
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return newCssString;
|
|
1
|
+
import { Declaration } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const getIdentifier = (selectorOrProperty: string, propertyValue?: string) => {
|
|
5
|
+
return `${selectorOrProperty.replace(/\s/g, "")}${propertyValue? `:${propertyValue.replace(/\s/g, "")}` : ""}`
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function parseValueUnit(input: string) {
|
|
9
|
+
const match = input.match(/^([-+]?\d*\.?\d+)([a-zA-Z]+)$/);
|
|
10
|
+
if (match) {
|
|
11
|
+
return {
|
|
12
|
+
value: match[1],
|
|
13
|
+
unit: match[2]
|
|
14
|
+
};
|
|
15
|
+
} else {
|
|
16
|
+
throw new Error(`Invalid input format: ${input}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function parseAndModifyCSS(cssString: string, callback: (selector: string, declarations: Declaration[]) => Declaration[]) {
|
|
21
|
+
const cssObject: {[x: string]: Declaration[]} = {};
|
|
22
|
+
|
|
23
|
+
// Regular expression to match selectors and their declarations
|
|
24
|
+
const regex = /([^{]+)\{([^}]+)\}/g;
|
|
25
|
+
let match;
|
|
26
|
+
|
|
27
|
+
// Parse the CSS content into an object
|
|
28
|
+
while ((match = regex.exec(cssString)) !== null) {
|
|
29
|
+
const selector = match[1].trim();
|
|
30
|
+
const declarations = match[2].trim().split(';').filter(Boolean).map(decl => {
|
|
31
|
+
const [property, value] = decl.split(':').map(item => item.trim());
|
|
32
|
+
return { property, value };
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
cssObject[selector] = declarations;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Function to process each selector and its declarations using the callback
|
|
39
|
+
Object.keys(cssObject).forEach(selector => {
|
|
40
|
+
const declarations = cssObject[selector];
|
|
41
|
+
|
|
42
|
+
// Call the callback, passing the selector and its declarations
|
|
43
|
+
const updatedDeclarations = callback(selector, declarations);
|
|
44
|
+
|
|
45
|
+
// Replace the original declarations with the returned value
|
|
46
|
+
cssObject[selector] = updatedDeclarations;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Convert the object back to a CSS string
|
|
50
|
+
let newCssString = '';
|
|
51
|
+
Object.keys(cssObject).forEach(selector => {
|
|
52
|
+
const declarations = cssObject[selector]
|
|
53
|
+
.map(({ property, value }) => `${property}: ${value};`)
|
|
54
|
+
.join(' ');
|
|
55
|
+
newCssString += `${selector} { ${declarations} }\n`;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return newCssString;
|
|
59
59
|
}
|