fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
2 lines • 1.86 kB
JavaScript
import"../constants.min.mjs";import{parsePreserveAspectRatioAttribute as e,parseUnit as t}from"../util/misc/svgParsing.min.mjs";import{reViewBoxAttrValue as n,svgNS as r,svgViewBoxElementsRegEx as i}from"./constants.min.mjs";function a(a){if(!i.test(a.nodeName))return{};let o=a.getAttribute(`viewBox`),s,c,l=1,u=1,d=0,f=0,p=a.getAttribute(`width`),m=a.getAttribute(`height`),h=a.getAttribute(`x`)||0,g=a.getAttribute(`y`)||0,_=!(o&&n.test(o)),v=!p||!m||p===`100%`||m===`100%`,y=``,b=0,x=0;if(_&&(h||g)&&a.parentNode&&a.parentNode.nodeName!==`#document`&&(y=` translate(`+t(h||`0`)+` `+t(g||`0`)+`) `,s=(a.getAttribute(`transform`)||``)+y,a.setAttribute(`transform`,s),a.removeAttribute(`x`),a.removeAttribute(`y`)),_&&v)return{width:0,height:0};let S={width:0,height:0};if(_)return S.width=t(p),S.height=t(m),S;let C=o.match(n);d=-parseFloat(C[1]),f=-parseFloat(C[2]);let w=parseFloat(C[3]),T=parseFloat(C[4]);S.minX=d,S.minY=f,S.viewBoxWidth=w,S.viewBoxHeight=T,v?(S.width=w,S.height=T):(S.width=t(p),S.height=t(m),l=S.width/w,u=S.height/T);let E=e(a.getAttribute(`preserveAspectRatio`)||``);if(E.alignX!==`none`&&(E.meetOrSlice===`meet`&&(u=l=l>u?u:l),E.meetOrSlice===`slice`&&(u=l=l>u?l:u),b=S.width-w*l,x=S.height-T*l,E.alignX===`Mid`&&(b/=2),E.alignY===`Mid`&&(x/=2),E.alignX===`Min`&&(b=0),E.alignY===`Min`&&(x=0)),l===1&&u===1&&d===0&&f===0&&h===0&&g===0)return S;if((h||g)&&a.parentNode.nodeName!==`#document`&&(y=` translate(`+t(h||`0`)+` `+t(g||`0`)+`) `),s=y+` matrix(`+l+` 0 0 `+u+` `+(d*l+b)+` `+(f*u+x)+`) `,a.nodeName===`svg`){for(c=a.ownerDocument.createElementNS(r,`g`);a.firstChild;)c.appendChild(a.firstChild);a.appendChild(c)}else c=a,c.removeAttribute(`x`),c.removeAttribute(`y`),s=c.getAttribute(`transform`)+s;return c.setAttribute(`transform`,s),S}export{a as applyViewboxTransform};
//# sourceMappingURL=applyViewboxTransform.min.mjs.map