@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 10.4 kB
JavaScript
import t from"../../Color.js";import has from"../../core/has.js";import{assertIsSome as e,unwrapOrThrow as i}from"../../core/maybe.js";import{c as n}from"../../chunks/mat2df32.js";import{m as r,t as o,i as s,s as a,r as l}from"../../chunks/mat2d.js";import"../../widgets/support/widgetUtils.js";import{tsx as h}from"../../widgets/support/jsxFactory.js";const f="http://www.w3.org/2000/svg";let c=0,d=0;const u=has("android"),y=has("chrome")||u&&u>=4?"auto":"optimizeLegibility",p={m:2,l:2,h:1,v:1,c:6,s:4,q:4,t:2,a:7,z:0},g=/([A-DF-Za-df-z])|([-+]?\d*[.]?\d+(?:[eE][-+]?\d+)?)/g;let m={},x={};const w={solid:"none",shortdash:[4,1],shortdot:[1,1],shortdashdot:[4,1,1,1],shortdashdotdot:[4,1,1,1,1,1],dot:[1,3],dash:[4,3],longdash:[8,3],dashdot:[4,3,1,3],longdashdot:[8,3,1,3],longdashdotdot:[8,3,1,3,1,3]},k=Math.PI;let b=1;function j(t,e){const i=t*(k/180);return Math.abs(e*Math.sin(i))+Math.abs(e*Math.cos(i))}function v(t){return t.map((t=>`${t.command} ${t.values.join(" ")}`)).join(" ").trim()}function M(t,i,n,r){if(t){if("circle"===t.type)return h("circle",{fill:i,"fill-rule":"evenodd",stroke:n.color,"stroke-width":n.width,"stroke-linecap":n.cap,"stroke-linejoin":n.join,"stroke-dasharray":n.dashArray,"stroke-dashoffset":n.dashOffset,"stroke-miterlimit":"4",cx:t.cx,cy:t.cy,r:t.r});if("ellipse"===t.type)return h("ellipse",{fill:i,"fill-rule":"evenodd",stroke:n.color,"stroke-width":n.width,"stroke-linecap":n.cap,"stroke-linejoin":n.join,"stroke-dasharray":n.dashArray,"stroke-miterlimit":"4",cx:t.cx,cy:t.cy,rx:t.rx,ry:t.ry});if("rect"===t.type)return h("rect",{fill:i,"fill-rule":"evenodd",stroke:n.color,"stroke-width":n.width,"stroke-linecap":n.cap,"stroke-linejoin":n.join,"stroke-dasharray":n.dashArray,"stroke-miterlimit":"4",x:t.x,y:t.y,width:t.width,height:t.height});if("image"===t.type)return h("image",{href:t.src,x:t.x,y:t.y,width:t.width,height:t.height,preserveAspectRatio:"none"});if("path"===t.type){const e="string"!=typeof t.path?v(t.path):t.path;return h("path",{fill:i,"fill-rule":"evenodd",stroke:n.color,"stroke-width":n.width,"stroke-linecap":n.cap,"stroke-linejoin":n.join,"stroke-dasharray":n.dashArray,"stroke-miterlimit":"4",d:e})}if("text"===t.type)return e(r),h("text",{"dominant-baseline":r.dominantBaseline,fill:i,"fill-rule":"evenodd",stroke:n.color,"stroke-width":n.width,"stroke-linecap":n.cap,"stroke-linejoin":n.join,"stroke-dasharray":n.dashArray,"stroke-miterlimit":"4","text-anchor":r.align,"text-decoration":r.decoration,kerning:r.kerning,rotate:r.rotate,"text-rendering":y,"font-style":r.font.style,"font-variant":r.font.variant,"font-weight":r.font.weight,"font-size":r.font.size,"font-family":r.font.family,x:t.x,y:t.y},t.text)}return null}function A(e){const i={fill:"none",pattern:null,linearGradient:null};if(e)if("type"in e&&"pattern"===e.type){const t="patternId-"+ ++c;i.fill=`url(#${t})`,i.pattern={id:t,x:e.x,y:e.y,width:e.width,height:e.height,image:{x:0,y:0,width:e.width,height:e.height,href:e.src}}}else if("type"in e&&"linear"===e.type){const n="linearGradientId-"+ ++d;i.fill=`url(#${n})`,i.linearGradient={id:n,x1:e.x1,y1:e.y1,x2:e.x2,y2:e.y2,stops:e.colors.map((e=>({offset:e.offset,color:e.color&&new t(e.color).toString()})))}}else if(e){const n=new t(e);i.fill=n.toString()}return i}function S(e){const i={color:"none",width:1,cap:"butt",join:"4",dashArray:"none",dashOffset:"0"};if(e&&(null!=e.width&&(i.width=e.width),e.cap&&(i.cap=e.cap),e.join&&(i.join=e.join.toString()),e.color&&(i.color=new t(e.color).toString()),e.dashArray&&(i.dashArray=e.dashArray),e.dashArray&&(i.dashOffset=e.dashoffset),e.style)){let t=null;if(e.style in w&&(t=w[e.style]),Array.isArray(t)){t=t.slice(0);const i=e.width??0;for(let e=0;e<t.length;++e)t[e]*=i;if("butt"!==e.cap){for(let e=0;e<t.length;e+=2)t[e]-=i,t[e]<1&&(t[e]=1);for(let e=1;e<t.length;e+=2)t[e]+=i}t=t.join(",")}i.dashArray=t}return i}function $(t,i){const n={align:null,decoration:null,kerning:null,rotate:null,font:{style:null,variant:null,weight:null,size:null,family:null}};if(t){const r=t.alignBaseline,o="baseline"===r?"auto":"top"===r?"text-top":"bottom"===r?"hanging":r;n.align=t.align,n.dominantBaseline=o,n.decoration=t.decoration,n.kerning=t.kerning?"auto":"0",n.rotate=t.rotated?"90":"0",e(i),n.font.style=i.style||"normal",n.font.variant=i.variant||"normal",n.font.weight=i.weight||"normal",n.font.size=i.size&&i.size.toString()||"10pt",n.font.family=i.family||"serif"}return n}function N(t){const{pattern:e,linearGradient:i}=t;if(e)return h("pattern",{id:e.id,patternUnits:"userSpaceOnUse",x:e.x,y:e.y,width:e.width,height:e.height},h("image",{x:e.image.x,y:e.image.y,width:e.image.width,height:e.image.height,href:e.image.href}));if(i){const t=i.stops.map(((t,e)=>h("stop",{key:`${e}-stop`,offset:t.offset,"stop-color":t.color})));return h("linearGradient",{id:i.id,gradientUnits:"userSpaceOnUse",x1:i.x1,y1:i.y1,x2:i.x2,y2:i.y2},t)}return null}function G(t,e){if(!t)return null;const i=[];for(const n of t){const{shape:t,fill:e,stroke:r,font:o}=n,s=A(e),a=S(r),l="text"===t.type?$(t,o):null,h=M(t,s.fill,a,l);h&&i.push(h)}return h("mask",{id:e,maskUnits:"userSpaceOnUse"},h("g",null,i))}function I(t,e,i){return o(t,s(t),[e,i])}function z(t,e,i,n,r){return a(t,s(t),[e,i]),t[4]=t[4]*e-n*e+n,t[5]=t[5]*i-r*i+r,t}function B(t,e,i,n){const r=e%360*Math.PI/180;l(t,s(t),r);const o=Math.cos(r),a=Math.sin(r),h=t[4],f=t[5];return t[4]=h*o-f*a+n*a-i*o+i,t[5]=f*o+h*a-i*a-n*o+n,t}function U(t,e){m&&"left"in m?(null!=m.left&&m.left>t&&(m.left=t),(null==m.right||m.right<t)&&(m.right=t),(null==m.top||m.top>e)&&(m.top=e),(null==m.bottom||m.bottom<e)&&(m.bottom=e)):m={left:t,bottom:e,right:t,top:e}}function F(t){const e=t.args,i=e.length;let n;switch(t.action){case"M":case"L":case"C":case"S":case"Q":case"T":for(n=0;n<i;n+=2)U(e[n],e[n+1]);x.x=e[i-2],x.y=e[i-1];break;case"H":for(n=0;n<i;++n)U(e[n],x.y);x.x=e[i-1];break;case"V":for(n=0;n<i;++n)U(x.x,e[n]);x.y=e[i-1];break;case"m":{let t=0;"x"in x||(U(x.x=e[0],x.y=e[1]),t=2);for(n=t;n<i;n+=2)U(x.x+=e[n],x.y+=e[n+1]);break}case"l":case"t":for(n=0;n<i;n+=2)U(x.x+=e[n],x.y+=e[n+1]);break;case"h":for(n=0;n<i;++n)U(x.x+=e[n],x.y);break;case"v":for(n=0;n<i;++n)U(x.x,x.y+=e[n]);break;case"c":for(n=0;n<i;n+=6)U(x.x+e[n],x.y+e[n+1]),U(x.x+e[n+2],x.y+e[n+3]),U(x.x+=e[n+4],x.y+=e[n+5]);break;case"s":case"q":for(n=0;n<i;n+=4)U(x.x+e[n],x.y+e[n+1]),U(x.x+=e[n+2],x.y+=e[n+3]);break;case"A":for(n=0;n<i;n+=7)U(e[n+5],e[n+6]);x.x=e[i-2],x.y=e[i-1];break;case"a":for(n=0;n<i;n+=7)U(x.x+=e[n+5],x.y+=e[n+6])}}function O(t,e,i){const n=p[t.toLowerCase()];let r;"number"==typeof n&&(n?e.length>=n&&(r={action:t,args:e.slice(0,e.length-e.length%n)},i.push(r),F(r)):(r={action:t,args:[]},i.push(r),F(r)))}function T(t){const e=("string"!=typeof t.path?v(t.path):t.path).match(g),i=[];if(m={},x={},!e)return null;let n="",r=[];const o=e.length;for(let a=0;a<o;++a){const t=e[a],o=parseFloat(t);isNaN(o)?(n&&O(n,r,i),r=[],n=t):r.push(o)}O(n,r,i);const s={x:0,y:0,width:0,height:0};return m&&"left"in m&&(s.x=m.left,s.y=m.top,s.width=m.right-m.left,s.height=m.bottom-m.top),s}function E(t){const e={x:0,y:0,width:0,height:0};if("circle"===t.type)e.x=t.cx-t.r,e.y=t.cy-t.r,e.width=2*t.r,e.height=2*t.r;else if("ellipse"===t.type)e.x=t.cx-t.rx,e.y=t.cy-t.ry,e.width=2*t.rx,e.height=2*t.ry;else if("image"===t.type||"rect"===t.type)e.x=t.x,e.y=t.y,e.width=t.width,e.height=t.height;else if("path"===t.type){const n=i(T(t));e.x=n.x,e.y=n.y,e.width=n.width,e.height=n.height}return e}function V(t){const e={x:0,y:0,width:0,height:0};let i=null,n=Number.NEGATIVE_INFINITY,r=Number.NEGATIVE_INFINITY;for(const o of t)i?(i.x=Math.min(i.x,o.x),i.y=Math.min(i.y,o.y),n=Math.max(n,o.x+o.width),r=Math.max(r,o.y+o.height)):(i=e,i.x=o.x,i.y=o.y,n=o.x+o.width,r=o.y+o.height);return i&&(i.width=n-i.x,i.height=r-i.y),i}function C(t,e,i,o,s,a,l,h,f){let c=(l&&a?j(a,e):e)/2,d=(l&&a?j(a,i):i)/2;if(f){const t=f[0],e=f[1];c=(l&&a?j(a,t):t)/2,d=(l&&a?j(a,e):e)/2}const u=t.width+o,y=t.height+o,p=n(),g=n();let m=!1;if(s&&0!==u&&0!==y){const t=e!==i?e/i:u/y,n=e>i?e:i;let o=1,s=1;isNaN(n)||(t>1?(o=n/u,s=n/t/y):(s=n/y,o=n*t/u)),r(g,g,z(p,o,s,c,d)),m=!0}const x=t.x+(u-o)/2,w=t.y+(y-o)/2;if(r(g,g,I(p,c-x,d-w)),!m&&(u>e||y>i)){const t=u/e>y/i,n=(t?e:i)/(t?u:y);r(g,g,z(p,n,n,x,w))}return a&&r(g,g,B(p,a,x,w)),h&&r(g,g,I(p,h[0],h[1])),`matrix(${g[0]},${g[1]},${g[2]},${g[3]},${g[4]},${g[5]})`}function D(t,e,i){const n=t?.effects.find((t=>"bloom"===t.type));if(!n)return null;const{strength:r,radius:o}=n,s=r>0?o:0,a=(r+s)*e,l=4*r+1;return h("filter",{id:`bloom${i}`,x:"-100%",y:"-100%",width:"300%",height:"300%",filterUnits:"userSpaceOnUse"},h("feMorphology",{operator:"dilate",radius:(r+.5*s)*(5**(e/100)*(.4+e/100)),in:"SourceGraphic",result:"dilate"}),h("feGaussianBlur",{in:"dilate",stdDeviation:a/25,result:"blur"}),h("feGaussianBlur",{in:"blur",stdDeviation:a/50,result:"intensityBlur"}),h("feComponentTransfer",{in:"SourceGraphic",result:"intensityBrightness"},h("feFuncR",{type:"linear",slope:l}),h("feFuncG",{type:"linear",slope:l}),h("feFuncB",{type:"linear",slope:l})),h("feMerge",null,h("feMergeNode",{in:"intensityBlur"}),h("feMergeNode",{in:"intensityBrightness"}),h("feGaussianBlur",{stdDeviation:r/10})))}function R(t,n,r,o={}){const s=[],a=[],l=++b,c=D(o.effectView,n,l);let d=null;if(c){const t=o.effectView?.effects.find((t=>"bloom"===t.type)),e=(t.strength?t.strength+t.radius/2:0)/3,i=n+n*e,s=r+r*e;d=[Math.max(i,10),Math.max(s,10)]}for(let e=0;e<t.length;e++){const l=t[e],f=[],c=[];let u=0,y=0,p=0;for(const t of l){const{shape:e,fill:i,stroke:n,font:r,offset:a}=t;o.ignoreStrokeWidth||(u+=n&&n.width||0);const l=A(i),h=S(n),d="text"===e.type?$(e,r):null;s.push(N(l)),f.push(M(e,l.fill,h,d)),c.push(E(e)),a&&(y+=a[0],p+=a[1])}const g=C(i(V(c)),n,r,u,o.scale??!1,o.rotation,o.useRotationSize??!1,[y,p],d);let m=null;if(o.masking){const t=`mask-${e}`,i=o.masking[e];s.push(G(i,t)),m=`url(#${t})`}a.push(m?h("g",{mask:m},h("g",{transform:g},f)):h("g",{transform:g},f))}o.useRotationSize&&o.rotation&&(n=j(o.rotation,n),r=j(o.rotation,r)),c&&(e(d),n=d[0],r=d[1]);return h("svg",{xmlns:f,width:n,height:r,style:"display: block;"},c,h("defs",null,s),c?h("g",{filter:`url(#bloom${l})`},a):a)}export{V as computeBBox,A as generateFillAttributes,S as generateStrokeAttributes,$ as generateTextAttributes,E as getBoundingBox,C as getTransformMatrix,N as renderDef,R as renderSVG,M as renderShape};