@polotno/pdf-export 0.1.29 → 0.1.30
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/svg-render.js +48 -5
- package/lib/svg.d.ts +1 -0
- package/lib/svg.js +1 -1
- package/package.json +1 -1
package/lib/svg-render.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as svg from './svg.js';
|
|
2
|
+
import { Util } from 'konva/lib/Util.js';
|
|
2
3
|
export async function renderSVG(doc, element, cache = null) {
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const str = await svg.urlToString(src, cache);
|
|
4
|
+
const str = await svg.urlToString(element.src, cache);
|
|
5
|
+
const replaceEntries = Object.entries(element.colorsReplace || {});
|
|
6
6
|
doc.addSVG(str, 0, 0, {
|
|
7
7
|
// Use 'none' to allow stretching to exact dimensions, 'xMinYMin meet' to preserve aspect ratio
|
|
8
8
|
preserveAspectRatio: 'none',
|
|
@@ -13,8 +13,51 @@ export async function renderSVG(doc, element, cache = null) {
|
|
|
13
13
|
if (!colors) {
|
|
14
14
|
return colors;
|
|
15
15
|
}
|
|
16
|
-
const [
|
|
17
|
-
|
|
16
|
+
const [rgb, opacity] = colors;
|
|
17
|
+
let colorString = null;
|
|
18
|
+
if (Array.isArray(rgb) && rgb.length === 3) {
|
|
19
|
+
colorString = `rgb(${rgb[0]},${rgb[1]},${rgb[2]})`;
|
|
20
|
+
}
|
|
21
|
+
else if (typeof rgb === 'string') {
|
|
22
|
+
colorString = rgb;
|
|
23
|
+
}
|
|
24
|
+
let nextColorString = colorString;
|
|
25
|
+
if (replaceEntries.length && colorString) {
|
|
26
|
+
for (const [from, to] of replaceEntries) {
|
|
27
|
+
if (svg.sameColors(from, colorString)) {
|
|
28
|
+
nextColorString = to;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
let nextColorArray = rgb;
|
|
34
|
+
let finalOpacity = opacity * element.opacity;
|
|
35
|
+
if (nextColorString != null) {
|
|
36
|
+
const rgbaObject = Util.colorToRGBA(nextColorString);
|
|
37
|
+
if (rgbaObject) {
|
|
38
|
+
nextColorArray = [
|
|
39
|
+
Math.round(rgbaObject.r),
|
|
40
|
+
Math.round(rgbaObject.g),
|
|
41
|
+
Math.round(rgbaObject.b),
|
|
42
|
+
];
|
|
43
|
+
// Handle alpha channel from the color string
|
|
44
|
+
if (rgbaObject.a !== undefined) {
|
|
45
|
+
finalOpacity = rgbaObject.a * opacity * element.opacity;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!Array.isArray(nextColorArray) &&
|
|
50
|
+
typeof nextColorArray === 'string') {
|
|
51
|
+
const rgbObject = Util.getRGB(nextColorArray);
|
|
52
|
+
if (rgbObject) {
|
|
53
|
+
nextColorArray = [
|
|
54
|
+
Math.round(rgbObject.r),
|
|
55
|
+
Math.round(rgbObject.g),
|
|
56
|
+
Math.round(rgbObject.b),
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return [nextColorArray, finalOpacity];
|
|
18
61
|
},
|
|
19
62
|
});
|
|
20
63
|
}
|
package/lib/svg.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ export declare function getSvgSize(url: string): Promise<{
|
|
|
8
8
|
height: number;
|
|
9
9
|
}>;
|
|
10
10
|
export declare function fixSize(svgString: string): string;
|
|
11
|
+
export declare const sameColors: (color1: any, color2: any) => boolean;
|
|
11
12
|
export declare function replaceColors(svgString: string, replaceMap: Map<string, string>): string;
|
package/lib/svg.js
CHANGED
|
@@ -175,7 +175,7 @@ export function fixSize(svgString) {
|
|
|
175
175
|
const str = xmlSerializer.serializeToString(doc);
|
|
176
176
|
return str;
|
|
177
177
|
}
|
|
178
|
-
const sameColors = (color1, color2) => {
|
|
178
|
+
export const sameColors = (color1, color2) => {
|
|
179
179
|
if (!color1 || !color2) {
|
|
180
180
|
return false;
|
|
181
181
|
}
|