️(export) improve svg width when undefined

We improved the svg width calculation when the
width attribute is undefined by trying to use
the style attribute before falling back to a
default value.
This commit is contained in:
Anthony LC
2025-12-30 13:01:08 +01:00
parent 43a1a76a2f
commit 3a3ed0453b
2 changed files with 19 additions and 12 deletions

View File

@@ -21,9 +21,6 @@ export const blockMappingImagePDF: DocsExporterPDF['mappings']['blockMapping']['
if (blob.type.includes('svg')) { if (blob.type.includes('svg')) {
const svgText = await blob.text(); const svgText = await blob.text();
const FALLBACK_SIZE = 536;
previewWidth = previewWidth || FALLBACK_SIZE;
const result = await convertSvgToPng(svgText, previewWidth); const result = await convertSvgToPng(svgText, previewWidth);
pngConverted = result.png; pngConverted = result.png;
dimensions = { width: result.width, height: result.height }; dimensions = { width: result.width, height: result.height };

View File

@@ -36,7 +36,7 @@ export function downloadFile(blob: Blob, filename: string) {
*/ */
export async function convertSvgToPng( export async function convertSvgToPng(
svgText: string, svgText: string,
width: number, width?: number,
): Promise<{ png: string; width: number; height: number }> { ): Promise<{ png: string; width: number; height: number }> {
// Create a canvas and render the SVG onto it // Create a canvas and render the SVG onto it
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
@@ -54,26 +54,36 @@ export async function convertSvgToPng(
const svgElement = svgDoc.documentElement; const svgElement = svgDoc.documentElement;
// Get viewBox or fallback to width/height attributes // Get viewBox or fallback to width/height attributes
let height; let calculatedHeight: number | undefined;
const svgWidth = svgElement.getAttribute?.('width'); const svgWidth = svgElement.getAttribute?.('width');
const svgHeight = svgElement.getAttribute?.('height'); const svgHeight = svgElement.getAttribute?.('height');
const viewBox = svgElement.getAttribute('viewBox')?.split(' ').map(Number); const viewBox = svgElement.getAttribute('viewBox')?.split(' ').map(Number);
const originalWidth = svgWidth ? parseInt(svgWidth) : viewBox?.[2]; const originalWidth = svgWidth ? parseInt(svgWidth) : viewBox?.[2];
const originalHeight = svgHeight ? parseInt(svgHeight) : viewBox?.[3]; const originalHeight = svgHeight ? parseInt(svgHeight) : viewBox?.[3];
if (originalWidth && originalHeight) {
const aspectRatio = originalHeight / originalWidth;
height = Math.round(width * aspectRatio);
}
const svg = Canvg.fromString(ctx, svgText); const svg = Canvg.fromString(ctx, svgText);
svg.resize(width, height, true);
const FALLBACK_WIDTH = 536;
// Resize if width provided, preserving aspect ratio
if (originalWidth && originalHeight && width) {
const aspectRatio = originalHeight / originalWidth;
calculatedHeight = Math.round(width * aspectRatio);
svg.resize(width, calculatedHeight, true);
} else if (!width && !originalWidth) {
svg.resize(FALLBACK_WIDTH, undefined, true);
}
await svg.render(); await svg.render();
const returnWidth = width || originalWidth || FALLBACK_WIDTH;
const returnHeight = calculatedHeight || returnWidth;
return { return {
png: canvas.toDataURL('image/png'), png: canvas.toDataURL('image/png'),
width, width: returnWidth,
height: height || width, height: returnHeight,
}; };
} }