fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
2 lines • 1.85 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=a.getAttribute(`width`),f=a.getAttribute(`height`),p=a.getAttribute(`x`)||0,m=a.getAttribute(`y`)||0,h=!(o&&n.test(o)),g=!d||!f||d===`100%`||f===`100%`,_=``,v=0,y=0;if(h&&(p||m)&&a.parentNode&&a.parentNode.nodeName!==`#document`&&(_=` translate(`+t(p||`0`)+` `+t(m||`0`)+`) `,s=(a.getAttribute(`transform`)||``)+_,a.setAttribute(`transform`,s),a.removeAttribute(`x`),a.removeAttribute(`y`)),h&&g)return{width:0,height:0};let b={width:0,height:0};if(h)return b.width=t(d),b.height=t(f),b;let x=o.match(n),S=-parseFloat(x[1]),C=-parseFloat(x[2]),w=parseFloat(x[3]),T=parseFloat(x[4]);b.minX=S,b.minY=C,b.viewBoxWidth=w,b.viewBoxHeight=T,g?(b.width=w,b.height=T):(b.width=t(d),b.height=t(f),l=b.width/w,u=b.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),v=b.width-w*l,y=b.height-T*l,E.alignX===`Mid`&&(v/=2),E.alignY===`Mid`&&(y/=2),E.alignX===`Min`&&(v=0),E.alignY===`Min`&&(y=0)),l===1&&u===1&&S===0&&C===0&&p===0&&m===0)return b;if((p||m)&&a.parentNode.nodeName!==`#document`&&(_=` translate(`+t(p||`0`)+` `+t(m||`0`)+`) `),s=_+` matrix(`+l+` 0 0 `+u+` `+(S*l+v)+` `+(C*u+y)+`) `,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),b}export{a as applyViewboxTransform};
//# sourceMappingURL=applyViewboxTransform.min.mjs.map