@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.cjs CHANGED
@@ -11110,7 +11110,12 @@ class PixldocsRenderer {
11110
11110
  { width: canvasWidth, height: canvasHeight },
11111
11111
  { cssOnly: false, backstoreOnly: false }
11112
11112
  );
11113
- const svgString = fabricInstance.toSVG();
11113
+ const rawSvgString = fabricInstance.toSVG();
11114
+ const svgString = this.normalizeSvgDimensions(
11115
+ rawSvgString,
11116
+ canvasWidth,
11117
+ canvasHeight
11118
+ );
11114
11119
  fabricInstance.enableRetinaScaling = prevRetina;
11115
11120
  fabricInstance.setDimensions(
11116
11121
  { width: prevWidth, height: prevHeight },
@@ -11156,22 +11161,44 @@ class PixldocsRenderer {
11156
11161
  * the SVG coordinate system matches the intended page size exactly.
11157
11162
  */
11158
11163
  normalizeSvgDimensions(svg, targetWidth, targetHeight) {
11159
- const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/);
11160
- const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/);
11164
+ const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/i);
11165
+ const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/i);
11161
11166
  const svgWidth = widthMatch ? parseFloat(widthMatch[1]) : targetWidth;
11162
11167
  const svgHeight = heightMatch ? parseFloat(heightMatch[1]) : targetHeight;
11163
- if (Math.abs(svgWidth - targetWidth) < 1 && Math.abs(svgHeight - targetHeight) < 1) {
11164
- return svg;
11165
- }
11166
11168
  console.log(
11167
- `[canvas-renderer][svg-normalize] SVG dimensions ${svgWidth}x${svgHeight} → ${targetWidth}x${targetHeight}`
11169
+ `[canvas-renderer][svg-normalize] root ${svgWidth}x${svgHeight} → page ${targetWidth}x${targetHeight}`
11168
11170
  );
11169
- let normalized = svg.replace(/(<svg[^>]*\b)width="[^"]*"/, `$1width="${targetWidth}"`).replace(/(<svg[^>]*\b)height="[^"]*"/, `$1height="${targetHeight}"`);
11170
- const viewBox = `0 0 ${svgWidth} ${svgHeight}`;
11171
- if (/viewBox="[^"]*"/.test(normalized)) {
11172
- normalized = normalized.replace(/viewBox="[^"]*"/, `viewBox="${viewBox}"`);
11171
+ let normalized = svg;
11172
+ if (/\bwidth="[^"]*"/i.test(normalized)) {
11173
+ normalized = normalized.replace(/(<svg[^>]*\b)width="[^"]*"/i, `$1width="${targetWidth}"`);
11174
+ } else {
11175
+ normalized = normalized.replace(/<svg\b/i, `<svg width="${targetWidth}"`);
11176
+ }
11177
+ if (/\bheight="[^"]*"/i.test(normalized)) {
11178
+ normalized = normalized.replace(/(<svg[^>]*\b)height="[^"]*"/i, `$1height="${targetHeight}"`);
11179
+ } else {
11180
+ normalized = normalized.replace(/<svg\b/i, `<svg height="${targetHeight}"`);
11181
+ }
11182
+ const viewBox = `0 0 ${targetWidth} ${targetHeight}`;
11183
+ if (/\bviewBox="[^"]*"/i.test(normalized)) {
11184
+ normalized = normalized.replace(/viewBox="[^"]*"/i, `viewBox="${viewBox}"`);
11173
11185
  } else {
11174
- normalized = normalized.replace(/<svg\b/, `<svg viewBox="${viewBox}"`);
11186
+ normalized = normalized.replace(/<svg\b/i, `<svg viewBox="${viewBox}"`);
11187
+ }
11188
+ normalized = normalized.replace(/\b(rx|ry)="undefined"/g, '$1="0"');
11189
+ if (/\bx="[^"]*"/i.test(normalized)) {
11190
+ normalized = normalized.replace(/(<svg[^>]*\b)x="[^"]*"/i, '$1x="0"');
11191
+ } else {
11192
+ normalized = normalized.replace(/<svg\b/i, '<svg x="0"');
11193
+ }
11194
+ if (/\by="[^"]*"/i.test(normalized)) {
11195
+ normalized = normalized.replace(/(<svg[^>]*\b)y="[^"]*"/i, '$1y="0"');
11196
+ } else {
11197
+ normalized = normalized.replace(/<svg\b/i, '<svg y="0"');
11198
+ }
11199
+ normalized = normalized.replace(/\bpreserveAspectRatio="[^"]*"/i, 'preserveAspectRatio="none"');
11200
+ if (!/\bpreserveAspectRatio="[^"]*"/i.test(normalized)) {
11201
+ normalized = normalized.replace(/<svg\b/i, '<svg preserveAspectRatio="none"');
11175
11202
  }
11176
11203
  return normalized;
11177
11204
  }