import { Item } from '../types/svgo' ; export const convertRoundedRectToPath = { active : true , type : 'perItem' , fn : convertRoundedRectToPathFn , params : undefined as any , } ; const none = { value : 0 } ; function convertRoundedRectToPathFn ( item ) { if ( ! item . isElem ( 'rect' ) || ! item . hasAttr ( 'width' ) || ! item . hasAttr ( 'height' ) || ! ( item . hasAttr ( 'rx' ) || item . hasAttr ( 'ry' ) ) ) { return undefined ; } const x = + ( item . attr ( 'x' ) || none ) . value ; const y = + ( item . attr ( 'y' ) || none ) . value ; const width = + item . attr ( 'width' ) . value ; const height = + item . attr ( 'height' ) . value ; const hasRx = item . hasAttr ( 'rx' ) && isValidCornerRadius ( + item . attr ( 'rx' ) . value ) ; const hasRy = item . hasAttr ( 'ry' ) && isValidCornerRadius ( + item . attr ( 'ry' ) . value ) ; let rx = + ( item . attr ( 'rx' ) || none ) . value ; let ry = + ( item . attr ( 'ry' ) || none ) . value ; if ( ! hasRx && ! hasRy ) { rx = ry = 0 ; } else if ( hasRx && ! hasRy ) { ry = rx ; } else if ( ! hasRx && hasRy ) { rx = ry ; } else { if ( rx > width / 2 ) { rx = width / 2 ; } if ( ry > height / 2 ) { ry = height / 2 ; } } if ( isNaN ( x - y + width - height + rx - ry ) ) { return undefined ; } let pathData ; if ( ! rx && ! ry ) { pathData = `M ${ x } ${ y } H ${ x + width } V ${ y + height } H ${ x } Z` ; } else { pathData = `M ${ x + rx } ${ y } ` + `H ${ x + width - rx } ` + `A ${ rx } ${ ry } 0 0 1 ${ x + width } ${ y + ry } ` + `V ${ y + height - ry } ` + `A ${ rx } ${ ry } 0 0 1 ${ x + width - rx } ${ y + height } ` + `H ${ x + rx } ` + `A ${ rx } ${ ry } 0 0 1 ${ x } ${ y + height - ry } ` + `V ${ y + ry } ` + `A ${ rx } ${ ry } 0 0 1 ${ x + rx } ${ y }` ; } item . addAttr ( { name : 'd' , value : pathData , prefix : '' , local : 'd' } ) ; item . renameElem ( 'path' ) . removeAttr ( [ 'x' , 'y' , 'width' , 'height' , 'rx' , 'ry' ] ) ; } function isValidCornerRadius ( val ) { return ! ( typeof val !== 'number' || val === Infinity || val < 0 ) ; }