mirror of
https://github.com/iconify/iconify.git
synced 2024-11-08 14:20:57 +00:00
Simplify code to calculate dimensions in Utils
This commit is contained in:
parent
49f4f67e44
commit
4f38f14ad5
@ -133,7 +133,7 @@ export function iconToSVG(
|
|||||||
|
|
||||||
if (rotation % 2 === 1) {
|
if (rotation % 2 === 1) {
|
||||||
// Swap width/height and x/y for 90deg or 270deg rotation
|
// Swap width/height and x/y for 90deg or 270deg rotation
|
||||||
if (box.left !== 0 || box.top !== 0) {
|
if (box.left !== box.top) {
|
||||||
tempValue = box.left;
|
tempValue = box.left;
|
||||||
box.left = box.top;
|
box.left = box.top;
|
||||||
box.top = tempValue;
|
box.top = tempValue;
|
||||||
@ -156,40 +156,35 @@ export function iconToSVG(
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Calculate dimensions
|
// Calculate dimensions
|
||||||
let width, height;
|
const customisationsWidth = customisations.width;
|
||||||
|
const customisationsHeight = customisations.height;
|
||||||
|
const boxWidth = box.width;
|
||||||
|
const boxHeight = box.height;
|
||||||
|
|
||||||
if (customisations.width === null && customisations.height === null) {
|
let width, height;
|
||||||
// Set height to '1em', calculate width
|
if (customisationsWidth === null) {
|
||||||
height = '1em';
|
// Width is not set: calculate width from height, default to '1em'
|
||||||
width = calculateSize(height, box.width / box.height);
|
height =
|
||||||
} else if (
|
customisationsHeight === null
|
||||||
customisations.width !== null &&
|
? '1em'
|
||||||
customisations.height !== null
|
: customisationsHeight === 'auto'
|
||||||
) {
|
? boxHeight
|
||||||
// Values are set
|
: customisationsHeight;
|
||||||
width = customisations.width;
|
width = calculateSize(height, boxWidth / boxHeight);
|
||||||
height = customisations.height;
|
|
||||||
} else if (customisations.height !== null) {
|
|
||||||
// Height is set
|
|
||||||
height = customisations.height;
|
|
||||||
width = calculateSize(height, box.width / box.height);
|
|
||||||
} else {
|
} else {
|
||||||
// Width is set
|
// Width is set
|
||||||
width = customisations.width as number | string;
|
width = customisationsWidth === 'auto' ? boxWidth : customisationsWidth;
|
||||||
height = calculateSize(width, box.height / box.width);
|
height =
|
||||||
}
|
customisationsHeight === null
|
||||||
|
? calculateSize(width, boxHeight / boxWidth)
|
||||||
// Check for 'auto'
|
: customisationsHeight === 'auto'
|
||||||
if (width === 'auto') {
|
? boxHeight
|
||||||
width = box.width;
|
: customisationsHeight;
|
||||||
}
|
|
||||||
if (height === 'auto') {
|
|
||||||
height = box.height;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to string
|
// Convert to string
|
||||||
width = typeof width === 'string' ? width : width.toString() + '';
|
width = typeof width === 'string' ? width : width.toString();
|
||||||
height = typeof height === 'string' ? height : height.toString() + '';
|
height = typeof height === 'string' ? height : height.toString();
|
||||||
|
|
||||||
// Result
|
// Result
|
||||||
const result: IconifyIconBuildResult = {
|
const result: IconifyIconBuildResult = {
|
||||||
@ -201,9 +196,9 @@ export function iconToSVG(
|
|||||||
' ' +
|
' ' +
|
||||||
box.top.toString() +
|
box.top.toString() +
|
||||||
' ' +
|
' ' +
|
||||||
box.width.toString() +
|
boxWidth.toString() +
|
||||||
' ' +
|
' ' +
|
||||||
box.height.toString(),
|
boxHeight.toString(),
|
||||||
},
|
},
|
||||||
body,
|
body,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user