@pixldocs/canvas-renderer 0.3.24 → 0.3.26

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.js CHANGED
@@ -11091,7 +11091,12 @@ class PixldocsRenderer {
11091
11091
  { width: canvasWidth, height: canvasHeight },
11092
11092
  { cssOnly: false, backstoreOnly: false }
11093
11093
  );
11094
- const svgString = fabricInstance.toSVG();
11094
+ const rawSvgString = fabricInstance.toSVG();
11095
+ const svgString = this.normalizeSvgDimensions(
11096
+ rawSvgString,
11097
+ canvasWidth,
11098
+ canvasHeight
11099
+ );
11095
11100
  fabricInstance.enableRetinaScaling = prevRetina;
11096
11101
  fabricInstance.setDimensions(
11097
11102
  { width: prevWidth, height: prevHeight },
@@ -11137,22 +11142,44 @@ class PixldocsRenderer {
11137
11142
  * the SVG coordinate system matches the intended page size exactly.
11138
11143
  */
11139
11144
  normalizeSvgDimensions(svg, targetWidth, targetHeight) {
11140
- const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/);
11141
- const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/);
11145
+ const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/i);
11146
+ const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/i);
11142
11147
  const svgWidth = widthMatch ? parseFloat(widthMatch[1]) : targetWidth;
11143
11148
  const svgHeight = heightMatch ? parseFloat(heightMatch[1]) : targetHeight;
11144
- if (Math.abs(svgWidth - targetWidth) < 1 && Math.abs(svgHeight - targetHeight) < 1) {
11145
- return svg;
11146
- }
11147
11149
  console.log(
11148
- `[canvas-renderer][svg-normalize] SVG dimensions ${svgWidth}x${svgHeight} → ${targetWidth}x${targetHeight}`
11150
+ `[canvas-renderer][svg-normalize] root ${svgWidth}x${svgHeight} → page ${targetWidth}x${targetHeight}`
11149
11151
  );
11150
- let normalized = svg.replace(/(<svg[^>]*\b)width="[^"]*"/, `$1width="${targetWidth}"`).replace(/(<svg[^>]*\b)height="[^"]*"/, `$1height="${targetHeight}"`);
11151
- const viewBox = `0 0 ${svgWidth} ${svgHeight}`;
11152
- if (/viewBox="[^"]*"/.test(normalized)) {
11153
- normalized = normalized.replace(/viewBox="[^"]*"/, `viewBox="${viewBox}"`);
11152
+ let normalized = svg;
11153
+ if (/\bwidth="[^"]*"/i.test(normalized)) {
11154
+ normalized = normalized.replace(/(<svg[^>]*\b)width="[^"]*"/i, `$1width="${targetWidth}"`);
11155
+ } else {
11156
+ normalized = normalized.replace(/<svg\b/i, `<svg width="${targetWidth}"`);
11157
+ }
11158
+ if (/\bheight="[^"]*"/i.test(normalized)) {
11159
+ normalized = normalized.replace(/(<svg[^>]*\b)height="[^"]*"/i, `$1height="${targetHeight}"`);
11160
+ } else {
11161
+ normalized = normalized.replace(/<svg\b/i, `<svg height="${targetHeight}"`);
11162
+ }
11163
+ const viewBox = `0 0 ${targetWidth} ${targetHeight}`;
11164
+ if (/\bviewBox="[^"]*"/i.test(normalized)) {
11165
+ normalized = normalized.replace(/viewBox="[^"]*"/i, `viewBox="${viewBox}"`);
11154
11166
  } else {
11155
- normalized = normalized.replace(/<svg\b/, `<svg viewBox="${viewBox}"`);
11167
+ normalized = normalized.replace(/<svg\b/i, `<svg viewBox="${viewBox}"`);
11168
+ }
11169
+ normalized = normalized.replace(/\b(rx|ry)="undefined"/g, '$1="0"');
11170
+ if (/\bx="[^"]*"/i.test(normalized)) {
11171
+ normalized = normalized.replace(/(<svg[^>]*\b)x="[^"]*"/i, '$1x="0"');
11172
+ } else {
11173
+ normalized = normalized.replace(/<svg\b/i, '<svg x="0"');
11174
+ }
11175
+ if (/\by="[^"]*"/i.test(normalized)) {
11176
+ normalized = normalized.replace(/(<svg[^>]*\b)y="[^"]*"/i, '$1y="0"');
11177
+ } else {
11178
+ normalized = normalized.replace(/<svg\b/i, '<svg y="0"');
11179
+ }
11180
+ normalized = normalized.replace(/\bpreserveAspectRatio="[^"]*"/i, 'preserveAspectRatio="none"');
11181
+ if (!/\bpreserveAspectRatio="[^"]*"/i.test(normalized)) {
11182
+ normalized = normalized.replace(/<svg\b/i, '<svg preserveAspectRatio="none"');
11156
11183
  }
11157
11184
  return normalized;
11158
11185
  }