@svg-fns/layout
Version:
Lightweight SVG layout utilities — viewBox parsing, scaling, cropping, and trimming for precise content-aware rendering.
2 lines (1 loc) • 7.42 kB
JavaScript
;var j=Object.create;var g=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,q=Object.prototype.hasOwnProperty;var L=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),U=(e,t)=>{for(var n in t)g(e,n,{get:t[n],enumerable:!0})},P=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of z(t))!q.call(e,i)&&i!==n&&g(e,i,{get:()=>t[i],enumerable:!(r=I(t,i))||r.enumerable});return e};var X=(e,t,n)=>(n=e!=null?j(H(e)):{},P(t||!e||!e.__esModule?g(n,"default",{value:e,enumerable:!0}):n,e)),Y=e=>P(g({},"__esModule",{value:!0}),e);var W=L((Ve,F)=>{"use strict";var V=Object.defineProperty,$=Object.getOwnPropertyDescriptor,k=Object.getOwnPropertyNames,J=Object.prototype.hasOwnProperty,K=(e,t)=>{for(var n in t)V(e,n,{get:t[n],enumerable:!0})},Q=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of k(t))!J.call(e,i)&&i!==n&&V(e,i,{get:()=>t[i],enumerable:!(r=$(t,i))||r.enumerable});return e},Z=e=>Q(V({},"__esModule",{value:!0}),e),T={};K(T,{DEG_TO_RAD:()=>C,RAD_TO_DEG:()=>_,assertClient:()=>ue,assertServer:()=>ae,blobToDataURLBrowser:()=>ee,canvasToBlob:()=>te,degToRad:()=>ce,isClient:()=>O,isServer:()=>A,isValidColor:()=>le,isValidRect:()=>ie,radToDeg:()=>de,rectBottom:()=>R,rectRight:()=>S,resolveDimensions:()=>he,roundIf:()=>re,toPadding:()=>ne,unionRects:()=>oe});F.exports=Z(T);var ee=e=>new Promise((t,n)=>{let r=new FileReader;r.onloadend=()=>t(r.result),r.onerror=n,r.readAsDataURL(e)}),te=(e,t,n,r)=>e?t.convertToBlob({type:n,quality:Math.round(r*100)}):new Promise((i,o)=>{var a;(a=t.toBlob)==null||a.call(t,u=>{u?i(u):o(new Error("Canvas toBlob failed"))},n,r)}),ne=(e=0)=>typeof e=="number"?{top:e,right:e,bottom:e,left:e}:e,re=(e,t=!0)=>t?Math.round(e*1e3)/1e3:e,ie=({x:e,y:t,width:n,height:r})=>[e,t,n,r].every(Number.isFinite),S=e=>e.x+e.width,R=e=>e.y+e.height,oe=(e,t)=>{let n=Math.min(e.x,t.x),r=Math.min(e.y,t.y),i=Math.max(S(e),S(t)),o=Math.max(R(e),R(t));return{x:n,y:r,width:i-n,height:o-r}},A=()=>{var e;return typeof process!="undefined"&&!!((e=process.versions)!=null&&e.node)},O=()=>typeof window!="undefined"&&typeof document!="undefined",ae=e=>{if(!A())throw new Error(e!=null?e:"requires server")},ue=e=>{if(!O())throw new Error(e!=null?e:"requires client")},le=e=>!!e&&e.trim()!==""&&e!=="none"&&e!=="transparent"&&!e.startsWith("url(")&&e!=="rgba(0, 0, 0, 0)",he=(e,t)=>{let{width:n,height:r}=e,{width:i,height:o}=t,a=i/o,u,l;return n&&r?(u=n,l=r):n?(u=n,l=n/a):r?(u=r*a,l=r):(u=i,l=o),{width:Math.round(u),height:Math.round(l)}},C=Math.PI/180,_=180/Math.PI,ce=e=>e*C,de=e=>e*_});var Se={};U(Se,{centerSvg:()=>ge,computeTrimBox:()=>M,fitSvg:()=>ye,getBBoxWithTransform:()=>v,getContentBBox:()=>y,getDimensions:()=>me,getViewBox:()=>f,parseDimension:()=>G,parseViewBox:()=>N,scaleViewBox:()=>xe,setViewBox:()=>x,setViewBoxTransform:()=>fe,tightlyCropSvg:()=>se,translateViewBox:()=>we,updateDimensions:()=>p});module.exports=Y(Se);var d=X(W()),N=e=>{if(!e)return null;let t=e.trim().split(/[\s,]+/).map(Number).filter(Number.isFinite);if(t.length<4)return null;let[n,r,i,o]=t;return{x:n,y:r,width:i,height:o}},f=e=>N(e.getAttribute("viewBox")),x=(e,{x:t,y:n,height:r,width:i},o)=>{e.setAttribute("viewBox",[t,n,i,r].map(a=>(0,d.roundIf)(a,o==null?void 0:o.round)).join(" ")),o!=null&&o.preserveAspectRatio&&e.setAttribute("preserveAspectRatio",o.preserveAspectRatio)},G=(e,t)=>{var r;if(!e)return null;let n=parseFloat(e);if(!Number.isFinite(n))return null;if(/^[0-9.+-]+(?:px)?$/.test(e))return n;if(e.endsWith("vw")){if(typeof window=="undefined")throw new Error("Cannot resolve vw without window");return n/100*window.innerWidth}if(e.endsWith("vh")){if(typeof window=="undefined")throw new Error("Cannot resolve vh without window");return n/100*window.innerHeight}if(e.endsWith("em")||e.endsWith("rem")){let i=(r=t==null?void 0:t.fontSize)!=null?r:parseFloat(getComputedStyle(document.documentElement).fontSize);if(!Number.isFinite(i))throw new Error(`Cannot resolve ${e} without font size context`);return n*i}throw new Error(`Unsupported or unknown unit in dimension: "${e}"`)},me=(e,t)=>({width:G(e.getAttribute("width"),t),height:G(e.getAttribute("height"),t)}),p=(e,t,n)=>{t!=null&&e.setAttribute("width",String(t)),n!=null&&e.setAttribute("height",String(n))},v=e=>{var t;try{let n=e.getBBox(),r=(t=e.getCTM)==null?void 0:t.call(e);if(!r)return n;let i=[new DOMPoint(n.x,n.y),new DOMPoint(n.x+n.width,n.y),new DOMPoint(n.x,n.y+n.height),new DOMPoint(n.x+n.width,n.y+n.height)].map(l=>l.matrixTransform(r)),o=i.map(l=>l.x),a=i.map(l=>l.y),u={x:Math.min(...o),y:Math.min(...a),width:Math.max(...o)-Math.min(...o),height:Math.max(...a)-Math.min(...a)};if(!(0,d.isValidRect)(u))throw new Error("Invalid rect");return u}catch(n){return console.debug("Error computing bbox with transform",e,n),{x:0,y:0,width:0,height:0}}},y=(e,t=!1)=>{let n=v(e);if(!t)for(let r of e.querySelectorAll("g, use, path, rect, circle, ellipse, line, polygon, polyline, text, image"))n=(0,d.unionRects)(n,v(r));return n},M=(e,t=0,n=!1)=>{let r=y(e,n),i=(0,d.toPadding)(t);return{x:r.x-i.left,y:r.y-i.top,width:r.width+i.left+i.right,height:r.height+i.top+i.bottom}},se=(e,t)=>{var o,a;let n=(o=t==null?void 0:t.padding)!=null?o:0,r=M(e,n,t==null?void 0:t.clipToViewBox),i=(u=e)=>(x(u,r,{round:t==null?void 0:t.round}),t!=null&&t.preserveDimensions||p(u,r.width,r.height),u);return((a=t==null?void 0:t.mutate)==null||a)&&i(e),{box:r,apply:i}},we=(e,t,n,r)=>{var a;let i=f(e);if(!i)return null;let o={x:i.x+t,y:i.y+n,width:i.width,height:i.height};return((a=r==null?void 0:r.mutate)==null||a)&&x(e,o,{round:r==null?void 0:r.round}),o},xe=(e,t,n,r,i)=>{var w;if(!Number.isFinite(t)||t<=0)throw new Error("factor must be > 0");let o=f(e);if(!o)return null;let a=Number.isFinite(n)?n:o.x+o.width/2,u=Number.isFinite(r)?r:o.y+o.height/2,l=o.width/t,h=o.height/t,c=a-(a-o.x)/t,b=u-(u-o.y)/t,s={x:c,y:b,width:l,height:h};return((w=i==null?void 0:i.mutate)==null||w)&&x(e,s,{round:i==null?void 0:i.round}),s},be=(e,t)=>({x:e[0]*t.x+e[2]*t.y+e[4],y:e[1]*t.x+e[3]*t.y+e[5]}),fe=(e,t,n)=>{var w;let r=f(e);if(!r)return null;let o=[{x:r.x,y:r.y},{x:r.x+r.width,y:r.y},{x:r.x,y:r.y+r.height},{x:r.x+r.width,y:r.y+r.height}].map(m=>be(t,m)),a=o.map(m=>m.x),u=o.map(m=>m.y),l=Math.min(...a),h=Math.min(...u),c=Math.max(...a)-l,b=Math.max(...u)-h,s={x:l,y:h,width:c,height:b};return((w=n==null?void 0:n.mutate)==null||w)&&x(e,s,{round:n==null?void 0:n.round}),s},ge=(e,t)=>{var h,c;let n=y(e),r=(h=f(e))!=null?h:{x:0,y:0,width:n.width,height:n.height},i=n.x+n.width/2,o=n.y+n.height/2,a=i-r.width/2,u=o-r.height/2,l={x:a,y:u,width:r.width,height:r.height};return((c=t==null?void 0:t.mutate)==null||c)&&x(e,l,{round:t==null?void 0:t.round}),l},ye=(e,t,n,r)=>{var E,B,D;let i=y(e),o=(0,d.toPadding)((E=r==null?void 0:r.padding)!=null?E:0),a=Math.max(1,i.width+o.left+o.right),u=Math.max(1,i.height+o.top+o.bottom),l=Math.min(t/a,n/u),h=M(e,(B=r==null?void 0:r.padding)!=null?B:0),c=h.width/l,b=h.height/l,s=h.x+h.width/2,w=h.y+h.height/2,m={x:s-c/2,y:w-b/2,width:c,height:b};return((D=r==null?void 0:r.mutate)==null||D)&&x(e,m,{round:r==null?void 0:r.round}),m};0&&(module.exports={centerSvg,computeTrimBox,fitSvg,getBBoxWithTransform,getContentBBox,getDimensions,getViewBox,parseDimension,parseViewBox,scaleViewBox,setViewBox,setViewBoxTransform,tightlyCropSvg,translateViewBox,updateDimensions});