From db48b42c64a4798a713c4654ae3803dba807e6e8 Mon Sep 17 00:00:00 2001 From: SegaraRai Date: Sat, 15 Jun 2024 23:38:14 +0900 Subject: [PATCH] chore: check if scale is uniform --- .../iconSymbolsPlugin/expandClippedViewBox.ts | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/vite-plugins/iconSymbolsPlugin/expandClippedViewBox.ts b/vite-plugins/iconSymbolsPlugin/expandClippedViewBox.ts index 426e9f3..f4d1925 100644 --- a/vite-plugins/iconSymbolsPlugin/expandClippedViewBox.ts +++ b/vite-plugins/iconSymbolsPlugin/expandClippedViewBox.ts @@ -97,9 +97,13 @@ export function expandClippedViewBox(source: string): string { expandX = Math.ceil(expandX + 10); expandY = Math.ceil(expandY + 10); - const newViewBox = `${vbX - expandX} ${vbY - expandY} ${vbWidth + expandX * 2} ${vbHeight + expandY * 2}`; - + const newVBX = vbX - expandX; + const newVBY = vbY - expandY; + const newVBWidth = vbWidth + expandX * 2; + const newVBHeight = vbHeight + expandY * 2; + const newViewBox = `${newVBX} ${newVBY} ${newVBWidth} ${newVBHeight}`; symbol.setAttribute("viewBox", newViewBox); + for (const use of uses) { const x = parseFloat(use.getAttribute("x") || "0"); const y = parseFloat(use.getAttribute("y") || "0"); @@ -128,15 +132,33 @@ export function expandClippedViewBox(source: string): string { continue; } - let scale = Math.min(width / vbWidth, height / vbHeight); - if (scale !== 1) { - console.log("Rare scale", scale, "in of ", symbolId); + const scaleX = width / vbWidth; + const scaleY = height / vbHeight; + if (Math.abs(scaleX - scaleY) > 0.01) { + console.warn( + "Non-uniform scale", + scaleX, + scaleY, + "in of ", + symbolId + ); + continue; + } + + const effectiveScale = Math.min(scaleX, scaleY); + if (effectiveScale !== 1) { + console.info( + "Rare scale", + effectiveScale, + "in of ", + symbolId + ); } - const newX = x - expandX * scale; - const newY = y - expandY * scale; - const newWidth = (vbWidth + expandX * 2) * scale; - const newHeight = (vbHeight + expandY * 2) * scale; + const newX = x - expandX * effectiveScale; + const newY = y - expandY * effectiveScale; + const newWidth = newVBWidth * effectiveScale; + const newHeight = newVBHeight * effectiveScale; if (newX !== 0) { use.setAttribute("x", String(newX));