@pixldocs/canvas-renderer 0.3.21 → 0.3.22
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/index.cjs +29 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +29 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -11094,7 +11094,8 @@ class PixldocsRenderer {
|
|
|
11094
11094
|
const prevSvgVPT = fabricInstance.svgViewportTransformation;
|
|
11095
11095
|
fabricInstance.viewportTransform = [1, 0, 0, 1, 0, 0];
|
|
11096
11096
|
fabricInstance.svgViewportTransformation = false;
|
|
11097
|
-
|
|
11097
|
+
let svgString = fabricInstance.toSVG();
|
|
11098
|
+
svgString = this.normalizeSvgDimensions(svgString, canvasWidth, canvasHeight);
|
|
11098
11099
|
if (prevVPT) fabricInstance.viewportTransform = prevVPT;
|
|
11099
11100
|
fabricInstance.svgViewportTransformation = prevSvgVPT;
|
|
11100
11101
|
const page = config.pages[pageIndex];
|
|
@@ -11127,6 +11128,33 @@ class PixldocsRenderer {
|
|
|
11127
11128
|
);
|
|
11128
11129
|
});
|
|
11129
11130
|
}
|
|
11131
|
+
/**
|
|
11132
|
+
* Normalize the SVG's width/height/viewBox to match the logical page dimensions.
|
|
11133
|
+
* Fabric's toSVG() may output dimensions scaled by the canvas element's actual
|
|
11134
|
+
* pixel size (e.g., 2x due to devicePixelRatio), causing svg2pdf to render
|
|
11135
|
+
* content at the wrong scale. This rewrites the root <svg> attributes to ensure
|
|
11136
|
+
* the SVG coordinate system matches the intended page size exactly.
|
|
11137
|
+
*/
|
|
11138
|
+
normalizeSvgDimensions(svg, targetWidth, targetHeight) {
|
|
11139
|
+
const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/);
|
|
11140
|
+
const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/);
|
|
11141
|
+
const svgWidth = widthMatch ? parseFloat(widthMatch[1]) : targetWidth;
|
|
11142
|
+
const svgHeight = heightMatch ? parseFloat(heightMatch[1]) : targetHeight;
|
|
11143
|
+
if (Math.abs(svgWidth - targetWidth) < 1 && Math.abs(svgHeight - targetHeight) < 1) {
|
|
11144
|
+
return svg;
|
|
11145
|
+
}
|
|
11146
|
+
console.log(
|
|
11147
|
+
`[canvas-renderer][svg-normalize] SVG dimensions ${svgWidth}x${svgHeight} → ${targetWidth}x${targetHeight}`
|
|
11148
|
+
);
|
|
11149
|
+
let normalized = svg.replace(/(<svg[^>]*\b)width="[^"]*"/, `$1width="${targetWidth}"`).replace(/(<svg[^>]*\b)height="[^"]*"/, `$1height="${targetHeight}"`);
|
|
11150
|
+
const viewBox = `0 0 ${svgWidth} ${svgHeight}`;
|
|
11151
|
+
if (/viewBox="[^"]*"/.test(normalized)) {
|
|
11152
|
+
normalized = normalized.replace(/viewBox="[^"]*"/, `viewBox="${viewBox}"`);
|
|
11153
|
+
} else {
|
|
11154
|
+
normalized = normalized.replace(/<svg\b/, `<svg viewBox="${viewBox}"`);
|
|
11155
|
+
}
|
|
11156
|
+
return normalized;
|
|
11157
|
+
}
|
|
11130
11158
|
/**
|
|
11131
11159
|
* Find the Fabric.Canvas instance that belongs to a given container element,
|
|
11132
11160
|
* using the global __fabricCanvasRegistry (set by PageCanvas).
|