/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{packFloatRGBA as t}from"../../core/floatRGBA.js";import{pt2px as o}from"../../core/screenUtils.js";import{isExtent as n}from"../../geometry/support/jsonTypeUtils.js";import r from"./Rect.js";import{defaultSdfTextureSize as e,spritePadding as i}from"../../views/2d/engine/webgl/definitions.js";const a=.45;function s(t){switch(t.type){case"CIMPointSymbol":{const o=t.symbolLayers;if(!o||1!==o.length)return null;const n=o[0];return"CIMVectorMarker"!==n.type?null:s(n)}case"CIMVectorMarker":{const o=t.markerGraphics;if(!o||1!==o.length)return null;const n=o[0];if(!n)return null;const r=n.geometry,e=c(r);if(!e)return null;const i=n.symbol;if(!i||"CIMPolygonSymbol"!==i.type&&"CIMLineSymbol"!==i.type||i.symbolLayers?.some(t=>!!t.effects))return null;let a=0;for(const t of i.symbolLayers??[])a=Math.max("CIMSolidStroke"===t.type?t.width:0,a);const{sdfTextureSize:s}=d(e,t.frame,t.size,t.anchorPoint,"Relative"!==t.anchorPointUnits,a,t.scaleSymbolsProportionally);return{type:"sdf",geometry:r,sdfTextureSize:s,sdfPaddingRatio:.5,asFill:"CIMPolygonSymbol"===i.type}}}}function l(t){return t?t.rings?t.rings:t.paths?t.paths:void 0!==t.xmin&&void 0!==t.ymin&&void 0!==t.xmax&&void 0!==t.ymax?[[[t.xmin,t.ymin],[t.xmin,t.ymax],[t.xmax,t.ymax],[t.xmax,t.ymin],[t.xmin,t.ymin]]]:null:null}function f(t){let o=1/0,n=-1/0,e=1/0,i=-1/0;for(const r of t)for(const t of r)t[0]<o&&(o=t[0]),t[0]>n&&(n=t[0]),t[1]<e&&(e=t[1]),t[1]>i&&(i=t[1]);return new r(o,e,n-o,i-e)}function m(t){let o=1/0,n=-1/0,r=1/0,e=-1/0;for(const i of t)for(const t of i)t[0]<o&&(o=t[0]),t[0]>n&&(n=t[0]),t[1]<r&&(r=t[1]),t[1]>e&&(e=t[1]);return[o,r,n,e]}function c(t){return t?t.rings?m(t.rings):t.paths?m(t.paths):n(t)?[t.xmin,t.ymin,t.xmax,t.ymax]:null:null}function h(t,o,n){const[r,e,a,s]=c(t),l=a-r,f=s-e,m=o,h=i,u=Math.floor(.5*(m*n-h)),y=(m-2*(u+h))/Math.max(l,f),d=Math.round(l*y),x=Math.round(f*y);return{pixelDimensions:[l,f],texelDimensions:[Math.round((d+2*u)/y),Math.round((x+2*u)/y)]}}const u=1.61803,y=512;function d(t,n,r,s,l,f,m){const[c,h,d,x]=t;if(d<c||x<h)return{frameSizeRatio:0,anchorX:0,anchorY:0,widthRatio:1,sdfPaddingRatio:.5,sdfTextureSize:e};const M=d-c,g=x-h,p=Math.max(M,g);let S,R=.5;{if(!m&&null!=n&&r>0){f*=(n.ymax-n.ymin)/r}const t=f/(f+p),o=.1;t>a&&t<1&&(R=Math.min(t+o,.99))}{const t=n?n.ymax-n.ymin:0,a=t>0?t:g,s=a>0?(p+f)/a:1,l=o(r)*s;let m=Math.floor(l*u+2*i);m=Math.min(m,y),m=Math.max(m,e),S=1<<32-Math.clz32(m-1)}const w=S,P=i,b=Math.floor(.5*(w*R-P)),z=(w-2*(b+P))/p,C=Math.round(M*z)+2*b,I=Math.round(g*z)+2*b;let j=1;if(n){j=I*(1-R)/((n.ymax-n.ymin)*z)}let v=0,T=0,k=1;s&&(l?n&&r&&n.ymax-n.ymin>0&&(k=(n.xmax-n.xmin)/(n.ymax-n.ymin),v=s.x/(r*k),T=s.y/r):(v=s.x,T=s.y)),n&&(v=.5*(n.xmax+n.xmin)+v*(n.xmax-n.xmin),T=.5*(n.ymax+n.ymin)+T*(n.ymax-n.ymin)),v-=c,T-=h,v*=z,T*=z,v+=b,T+=b;let F=v/C-.5,L=T/I-.5;return l&&r&&(F*=r*k,L*=r),{frameSizeRatio:j,anchorX:F,anchorY:L,widthRatio:k,sdfTextureSize:S,sdfPaddingRatio:R}}function x(t){const o=l(t.geometry),n=f(o),r=t.sdfTextureSize,e=i,a=Math.floor(.5*(r*t.sdfPaddingRatio-e)),s=r-2*(a+e),m=s/Math.max(n.width,n.height),c=Math.round(n.width*m)+2*a,h=Math.round(n.height*m)+2*a,u=[];for(const i of o)if(i&&i.length>1){const o=[];for(const r of i){let[e,i]=r;e-=n.x,i-=n.y,e*=m,i*=m,e+=a-.5,i+=a-.5,t.asFill?o.push([e,i]):o.push([Math.round(e),Math.round(i)])}if(t.asFill){const t=o.length-1;o[0][0]===o[t][0]&&o[0][1]===o[t][1]||o.push(o[0])}u.push(o)}const y=M(u,c,h,a);return t.asFill&&g(u,c,h,a,y),{data:p(y,a),width:c,height:h,sdfPaddingRatio:t.sdfPaddingRatio,sdfDecodeCoeff:2*a/s}}function M(t,o,n,r){const e=o*n,i=new Array(e),a=r*r+1;for(let s=0;s<e;++s)i[s]=a;for(const s of t){const t=s.length;for(let e=1;e<t;++e){const t=s[e-1],a=s[e];let l,f,m,c;t[0]<a[0]?(l=t[0],f=a[0]):(l=a[0],f=t[0]),t[1]<a[1]?(m=t[1],c=a[1]):(m=a[1],c=t[1]);let h=Math.floor(l)-r,u=Math.floor(f)+r,y=Math.floor(m)-r,d=Math.floor(c)+r;h<0&&(h=0),u>o&&(u=o),y<0&&(y=0),d>n&&(d=n);const x=a[0]-t[0],M=a[1]-t[1],g=x*x+M*M;for(let r=h;r<u;r++)for(let e=y;e<d;e++){const s=r+.5,l=e+.5;let f,m,c=(s-t[0])*x+(l-t[1])*M;c<0?(f=t[0],m=t[1]):c>g?(f=a[0],m=a[1]):(c/=g,f=t[0]+c*x,m=t[1]+c*M);const h=(s-f)*(s-f)+(l-m)*(l-m),u=(n-e-1)*o+r;h<i[u]&&(i[u]=h)}}}for(let s=0;s<e;++s)i[s]=Math.sqrt(i[s]);return i}function g(t,o,n,r,e){for(const i of t){const t=i.length;for(let a=1;a<t;++a){const t=i[a-1],s=i[a];let l,f,m,c;t[0]<s[0]?(l=t[0],f=s[0]):(l=s[0],f=t[0]),t[1]<s[1]?(m=t[1],c=s[1]):(m=s[1],c=t[1]);let h=Math.floor(l),u=Math.floor(f)+1,y=Math.floor(m),d=Math.floor(c)+1;h<r&&(h=r),u>o-r&&(u=o-r),y<r&&(y=r),d>n-r&&(d=n-r);for(let i=y;i<d;++i){if(t[1]>i==s[1]>i)continue;const a=i+.5,l=(n-i-1)*o;for(let o=h;o<u;++o){o+.5<(s[0]-t[0])*(a-t[1])/(s[1]-t[1])+t[0]&&(e[l+o]=-e[l+o])}for(let t=r;t<h;++t)e[l+t]=-e[l+t]}}}}function p(o,n){const r=2*n,e=o.length,i=new Uint8Array(4*e);for(let a=0;a<e;++a){const n=.5-o[a]/r;t(n,i,4*a)}return i}export{x as buildSDF,c as getExtent,h as getSDFDimensions,s as getSDFInfo,d as getSDFMetrics};
