// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/layers/vectorTiles/views/vectorTiles/SpriteMosaic","require exports ../webgl/Texture ../2d/engine/webgl/Geometry ./Rect ./GeometryUtils ./RectangleBinPack".split(" "),function(J,K,H,I,E,F,D){function w(c){return c-Math.floor(c)}function G(c,b,d){0>c?c=0:.9999991<c&&(c=.9999991);var a=w(c*z[0]),f=w(c*z[1]),p=w(c*z[2]);c=w(c*z[3]);b[d+0]=256*(a-a*C[0]);b[d+1]=256*(f-a*C[1]);b[d+2]=256*(p-f*C[2]);b[d+3]=256*(c-p*C[3])}var z=[16777216,65536,256,1],C=[0,1/256,1/256,1/256];return function(){function c(b,
d,a){void 0===a&&(a=0);this._size=[];this._mosaicsData=[];this._textures=[];this._dirties=[];this._pageHeight=this._pageWidth=this._currentPage=this._maxItemSize=0;this._mosaicRects={};this.pixelRatio=1;(0>=b||0>=d)&&console.error("Sprites mosaic defaultWidth and defaultHeight must be greater than zero!");this._pageWidth=b;this._pageHeight=d;0<a&&(this._maxItemSize=a);this._binPack=new D(b-4,d-4)}c.prototype.getWidth=function(b){return b>=this._size.length?-1:this._size[b][0]};c.prototype.getHeight=
function(b){return b>=this._size.length?-1:this._size[b][1]};c.prototype.setSpriteSource=function(b){this.dispose();this.pixelRatio=b.devicePixelRatio;if(0===this._mosaicsData.length){this._binPack=new D(this._pageWidth-4,this._pageHeight-4);var d=new Uint32Array(Math.floor(this._pageWidth)*Math.floor(this._pageHeight));this._mosaicsData[0]=d;this._dirties.push(!0);this._size.push([this._pageWidth,this._pageHeight]);this._textures.push(void 0)}this._sprites=b};c.prototype.getSpriteItem=function(b,
d){void 0===d&&(d=!1);var a=this._mosaicRects[b];if(a)return a;if(!this._sprites||"loaded"!==this._sprites.loadStatus)return null;a=this._sprites.getSpriteInfo(b);if(!a||!a.width||!a.height||0>a.width||0>a.height)return null;var f=a.width,p=a.height,k,h,g=1;if(a.cim){g=this._buildSDF(a.cim);k=g[0];h=g[1];var c=g[2],l=g[3],e=g[4],g=g[5];if(!k||0>=l||0>=e)return null;var m=this._allocateImage(h,c),e=m[0],l=m[1],m=m[2];if(0>=e.width)return null;(a.cim||a.sdf)&&this._clearRect(e,l,m,1);this._copy(e,{width:h,
height:c,x:0,y:0},l,m,d,k);e.x+=4;e.y+=4;e.width-=8;e.height-=8;k=e;h=l;g*=.75;g=c/p;a.sdf=!0}else{c=this._allocateImage(f,p);e=c[0];l=c[1];m=c[2];if(0>=e.width)return null;this._copy(e,a,l,m,d);k=e;h=l}a={rect:k,width:f,height:p,sdf:a.sdf,pixelRatio:a.pixelRatio,page:h,sdfRatio:g};return this._mosaicRects[b]=a};c.prototype.preloadSpriteItems=function(){for(var b=0,d=this._sprites.spriteNames;b<d.length;b++)this.getSpriteItem(d[b],!0)};c.prototype.getSpriteItems=function(b){for(var d={},a=0;a<b.length;a++){var f=
b[a];d[f]=this.getSpriteItem(f)}return d};c.prototype.getMosaicItemPosition=function(b,d){var a=this.getSpriteItem(b,d),f=a&&a.rect;if(!f)return null;f.width=a.width;f.height=a.height;return{size:[a.width,a.height],tl:[(f.x+2)/this._size[a.page][0],(f.y+2)/this._size[a.page][1]],br:[(f.x+2+a.width)/this._size[a.page][0],(f.y+2+a.height)/this._size[a.page][1]],page:a.page}};c.prototype.bind=function(b,d,a,f){void 0===a&&(a=0);void 0===f&&(f=0);this._textures[a]||(this._textures[a]=new H(b,{pixelFormat:6408,
dataType:5121,width:this._size[a][0],height:this._size[a][1]},new Uint8Array(this._mosaicsData[a].buffer)));var p=this._textures[a];p.setSamplingMode(d);this._dirties[a]&&p.setData(new Uint8Array(this._mosaicsData[a].buffer));b.bindTexture(p,f);this._dirties[a]=!1};c._copyBits=function(b,d,a,f,p,c,h,g,n,l,e){var k=f*d+a;h=g*c+h;if(e)for(h-=c,e=-1;e<=l;e++,k=((e+l)%l+f)*d+a,h+=c)for(g=-1;g<=n;g++)p[h+g]=b[k+(g+n)%n];else for(e=0;e<l;e++){for(g=0;g<n;g++)p[h+g]=b[k+g];k+=d;h+=c}};c.prototype._copy=
function(b,d,a,f,p,k){if(this._sprites&&"loaded"===this._sprites.loadStatus&&!(a>=this._mosaicsData.length)){var h=new Uint32Array(k?k.buffer:this._sprites.image.buffer),g=this._mosaicsData[a];g&&h||console.error("Source or target images are uninitialized!");c._copyBits(h,k?d.width:this._sprites.width,d.x,d.y,g,f[0],b.x+2,b.y+2,d.width,d.height,p);this._dirties[a]=!0}};c.prototype._allocateImage=function(b,d){b+=2;d+=2;var a=Math.max(b,d);if(this._maxItemSize&&this._maxItemSize<a){var a=Math.pow(2,
Math.ceil(F.log2(b))),f=Math.pow(2,Math.ceil(F.log2(d))),c=new E(0,0,b,d);this._mosaicsData.push(new Uint32Array(a*f));this._dirties.push(!0);this._size.push([a,f]);this._textures.push(void 0);return[c,this._mosaicsData.length-1,[a,f]]}a=b%4?4-b%4:4;f=d%4?4-d%4:4;1===a&&(a=5);1===f&&(f=5);a=this._binPack.allocate(b+a,d+f);return 0>=a.width?(this._dirties[this._currentPage]||(this._mosaicsData[this._currentPage]=null),this._currentPage=this._mosaicsData.length,this._mosaicsData.push(new Uint32Array(this._pageWidth*
this._pageHeight)),this._dirties.push(!0),this._size.push([this._pageWidth,this._pageHeight]),this._textures.push(void 0),this._binPack=new D(this._pageWidth-4,this._pageHeight-4),this._allocateImage(b,d)):[a,this._currentPage,[this._pageWidth,this._pageHeight]]};c.prototype._clearRect=function(b,d,a,f){(d=this._mosaicsData[d])||console.error("Source image is uninitialized!");var c=[0,0,0,0];G(f,c,0);f=c[0]&255|(c[1]&255)<<8|(c[2]&255)<<16|c[3]<<24;a=a[0];var c=b.y*a+b.x,k=b.width;b=b.height;for(var h=
0;h<b;h++){for(var g=0;g<k;g++)d[c+g]=f;c+=a}};c.prototype.dispose=function(){this._binPack=null;this._mosaicRects={};for(var b=0,d=this._textures;b<d.length;b++){var a=d[b];a&&a.dispose()}this._textures.length=0};c.prototype._extractGeometry=function(b){if(!b)return null;b=b.symbolLayers;if(!b||1!==b.length)return null;b=b[0];if(!b)return null;b=b.markerGraphics;if(!b||1!==b.length)return null;b=b[0];if(!b)return null;var d=b.geometry;if(!d||!d.rings)return null;b=[];for(var a=0,d=d.rings;a<d.length;a++){for(var f=
[],c=0,k=d[a];c<k.length;c++){var h=k[c];f.push(new I.Point(h[0],h[1]))}b.push(f)}return b};c.prototype._getEnvelope=function(b){for(var d=Infinity,a=-Infinity,f=Infinity,c=-Infinity,k=0;k<b.length;k++)for(var h=0,g=b[k];h<g.length;h++){var n=g[h];n.x<d&&(d=n.x);n.x>a&&(a=n.x);n.y<f&&(f=n.y);n.y>c&&(c=n.y)}return new E(d,f,a-d,c-f)};c.prototype._buildSDF=function(b){b=this._extractGeometry(b);if(!b)return null;var d=this._getEnvelope(b);if(0>=d.width||0>=d.height)return null;for(var a=86/Math.max(d.width,
d.height),f=Math.round(d.width*a),c=Math.round(d.height*a),k=f+32,h=c+32,g=0;g<b.length;g++)for(var n=0,l=b[g];n<l.length;n++){var e=l[n];e.x-=d.x;e.y-=d.y;e.x*=a;e.y*=a;e.x+=15.5;e.y+=15.5}d=this._dist_map(b,k,h,16);this._sign_dist_map(b,k,h,16,d);return[this._encodeDistMap(d),k,h,f,c,a]};c.prototype._dist_map=function(b,d,a,c){for(var f=d*a,k=new Float32Array(f),h=c*c+1,g=0;g<f;++g)k[g]=h;for(h=0;h<b.length;h++)for(var n=b[h],l=n.length,g=1;g<l;++g){var e=n[g-1],m=n[g],t=void 0,q=void 0;e.x<m.x?
(t=e.x,q=m.x):(t=m.x,q=e.x);var u=void 0,v=void 0;e.y<m.y?(u=e.y,v=m.y):(u=m.y,v=e.y);var r=Math.floor(t)-c,q=Math.floor(q)+c,u=Math.floor(u)-c,v=Math.floor(v)+c;0>r&&(r=0);q>d&&(q=d);0>u&&(u=0);v>a&&(v=a);for(var t=m.x-e.x,w=m.y-e.y,z=t*t+w*w;r<q;r++)for(var A=u;A<v;A++){var x=(r-e.x)*t+(A-e.y)*w,y=void 0,B=void 0;0>x?(y=e.x,B=e.y):x>z?(y=m.x,B=m.y):(x/=z,y=e.x+x*t,B=e.y+x*w);x=(r-y)*(r-y)+(A-B)*(A-B);y=(a-A-1)*d+r;x<k[y]&&(k[y]=x)}}for(g=0;g<f;++g)k[g]=Math.sqrt(k[g]);return k};c.prototype._sign_dist_map=
function(b,d,a,c,p){for(var f=0;f<b.length;f++)for(var h=b[f],g=h.length,n=1;n<g;++n){var l=h[n-1],e=h[n],m=void 0,t=void 0;l.x<e.x?(m=l.x,t=e.x):(m=e.x,t=l.x);var q=void 0,u=void 0;l.y<e.y?(q=l.y,u=e.y):(q=e.y,u=l.y);m=Math.floor(m);t=Math.floor(t)+1;q=Math.floor(q);u=Math.floor(u)+1;m<c&&(m=c);t>d-c&&(t=d-c);q<c&&(q=c);for(u>a-c&&(u=a-c);q<u;++q)if(l.y>q!==e.y>q){for(var v=(a-q-1)*d,r=m;r<t;++r)r<(e.x-l.x)*(q-l.y)/(e.y-l.y)+l.x&&(p[v+r]=-p[v+r]);for(r=c;r<m;++r)p[v+r]=-p[v+r]}}};c.prototype._encodeDistMap=
function(b){for(var c=b.length,a=new Uint8Array(4*c),f=0;f<c;++f)G(b[f]/16+.5,a,4*f);return a};return c}()});