// 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/SymbolBucket","require exports ../../core/tsSupport/extendsHelper ../../core/tsSupport/decorateHelper ./Bucket ../2d/engine/webgl/Geometry ./style/StyleLayer ./Placement ./GeometryUtils ./TextShaping dojox/string/BidiEngine".split(" "),function(P,Q,K,R,L,A,H,I,r,M,N){function O(r,k){if(r.iconMosaicItem&&k.iconMosaicItem){if(r.iconMosaicItem.page!==k.iconMosaicItem.page)return r.iconMosaicItem.page<k.iconMosaicItem.page?-1:1}else{if(r.iconMosaicItem&&
!k.iconMosaicItem)return 1;if(!r.iconMosaicItem&&k.iconMosaicItem)return-1}return 0}(function(){return function(){}})();return function(J){function k(a,b,e,c,g,d,f,u){b=J.call(this,a,b)||this;b._markerMap=new Map;b._glyphMap=new Map;b._glyphBufferDataStorage=new Map;b._sdfMarkers=!1;if(a.hasDataDrivenIcon!==e.isDataDriven())throw Error("incompatible icon buffer");if(a.hasDataDrivenText!==g.isDataDriven())throw Error("incompatible text buffer");b._iconVertexBuffer=e;b._iconIndexBuffer=c;b._textVertexBuffer=
g;b._textIndexBuffer=d;b._placementEngine=f;b._workerTileHandler=u;return b}K(k,J);Object.defineProperty(k.prototype,"markerPageMap",{get:function(){return this._markerMap},enumerable:!0,configurable:!0});Object.defineProperty(k.prototype,"glyphsPageMap",{get:function(){return this._glyphMap},enumerable:!0,configurable:!0});Object.defineProperty(k.prototype,"sdfMarker",{get:function(){return this._sdfMarkers},enumerable:!0,configurable:!0});k.prototype.copy=function(a,b,e,c,g){a=new k(this.layer,
this.zoom,a,b,e,c,g,this._workerTileHandler);a.layerIndex=this.layerIndex;a.layerExtent=this.layerExtent;a._iconIndexStart=b.index;a._textIndexStart=c.index;a._iconIndexCount=0;a._textIndexCount=0;a._symbolInstances=this._symbolInstances;a._workerTileHandler=this._workerTileHandler;a._fontArray=this._fontArray;a._textLayout=this._textLayout;a._iconLayout=this._iconLayout;a._isLinePlacement=this._isLinePlacement;a._avoidEdges=this._avoidEdges;return a};k.prototype.getResources=function(a,b,e){var c=
this.layer,g=this.zoom,d=c.hasDataDrivenIcon,f=c.hasDataDrivenText;a&&a.setExtent(this.layerExtent);for(var u=c.getLayoutProperty("icon-image"),n=c.getLayoutProperty("text-field"),F=c.getLayoutValue("text-font",g),v=c.getLayoutValue("text-transform",g),y=[],h=0,q=this._features;h<q.length;h++){var l=q[h],p=l.getGeometry(a);if(p&&0!==p.length){var x=void 0;u&&(x=c.getLayoutValue("icon-image",g,l),u.isDataDriven||(x=this._replaceKeys(x,l.values)),x&&b.add(x));var m=void 0,t=!1;if(n&&(m=c.getLayoutValue("text-field",
g,l),n.isDataDriven||(m=this._replaceKeys(m,l.values)),m)){switch(v){case 2:m=m.toLowerCase();break;case 1:m=m.toUpperCase()}k._bidiEngine.hasBidiChar(m)&&(t=void 0,t="rtl"===k._bidiEngine.checkContextual(m)?"IDNNN":"ICNNN",m=k._bidiEngine.bidiTransform(m,t,"VLYSN"),t=!0);var w=m.length;if(0<w)for(var z=0,r=F;z<r.length;z++){var C=r[z],D=e[C];D||(D=e[C]=new Set);for(C=0;C<w;C++){var A=m.charCodeAt(C);D.add(A)}}}if(x||m)w=c.getLayoutValue("icon-size",g,l),z=c.getLayoutValue("text-size",g,l),l={sprite:x,
label:m,rtl:t,geometry:p,iconSize:w,iconRotate:c.getLayoutValue("icon-rotate",g,l),ddIconValues:d?{color:c.getPaintValue("icon-color",g,l),opacity:c.getPaintValue("icon-opacity",g,l),size:w}:null,textSize:z,textRotate:c.getLayoutValue("text-rotate",g,l),ddTextValues:f?{color:c.getPaintValue("text-color",g,l),opacity:c.getPaintValue("text-opacity",g,l),size:z}:null},y.push(l)}}this._symbolFeatures=y};k.prototype.processFeatures=function(a,b){a&&a.setExtent(this.layerExtent);var e=this.layer,c=this.zoom,
g=this._isLinePlacement=1===e.getLayoutValue("symbol-placement",c),d=this._avoidEdges=e.getLayoutValue("symbol-avoid-edges",c)&&!g,f=8*e.getLayoutValue("symbol-spacing",c),u=e.getLayoutProperty("icon-image"),n=e.getLayoutProperty("text-field"),F=this._workerTileHandler,v,y;u&&(this._iconLayout=new H.IconLayout(e,c,g),v=F.getSpriteItems(),y=this._getTranslate(!0));var h,q,l;if(n){h=this._textLayout=new H.TextLayout(e,c,g);this._fontArray=h.fontArray;l=.5;switch(h.anchor){case 5:case 1:case 7:l=0;break;
case 6:case 2:case 8:l=1}q=.5;switch(h.anchor){case 5:case 3:case 6:q=0;break;case 7:case 4:case 8:q=1}e=.5;switch(h.justify){case 0:e=0;break;case 2:e=1}var c=24*h.letterSpacing,u=g?0:24*h.maxWidth,n=24*h.lineHeight,p=[24*h.offset[0],24*h.offset[1]];h=this._fontArray.map(function(a){return F.getGlyphItems(a)});q=new M(h,u,n,c,p,l,q,e);l=this._getTranslate(!1)}this._iconIndexStart=this._iconIndexBuffer.index;this._textIndexStart=this._textIndexBuffer.index;this._textIndexCount=this._iconIndexCount=
0;this._markerMap.clear();this._glyphMap.clear();this._symbolInstances=e=[];c=this._textLayout;u=1;c&&c.size&&(u=c.size/24);n=c?c.maxAngle*r.C_DEG_TO_RAD:0;h=c?8*c.size:0;for(var p=0,x=this._symbolFeatures;p<x.length;p++){var m=x[p],t=void 0;m.sprite&&(t=v[m.sprite])&&t.sdf&&(this._sdfMarkers=!0);var w=void 0,z=m.label,B=0;if(z&&(w=q.getShaping(z,m.rtl))&&0<w.length){for(var B=1E30,z=-1E30,C=0,D=w;C<D.length;C++)var A=D[C],B=Math.min(B,A.x),z=Math.max(z,A.x);B=(z-B+48)*u*8}z=0;for(C=m.geometry;z<
C.length;z++){var D=C[z],G=void 0;g?(w&&0<w.length&&c&&c.size&&k._smoothVertices(D,8*c.size*(2+Math.min(2,4*Math.abs(c.offset[1])))),G=k._findAnchors(D,f,B)):G=[new I.Anchor(D[0].x,D[0].y)];for(A=0;A<G.length;A++){var E=G[A];0>E.x||4096<E.x||0>E.y||4096<E.y||g&&0<B&&0===c.rotationAlignment&&!k._honorsTextMaxAngle(D,E,B,n,h)||e.push({shaping:w,line:D,iconMosaicItem:t,anchor:E,iconSize:m.iconSize,iconRotate:m.iconRotate,ddIconValues:m.ddIconValues,textSize:m.textSize,textRotate:m.textRotate,ddTextValues:m.ddTextValues})}}}e.sort(O);
for(g=0;g<e.length;g++)this._processFeature(e[g],y,l,d);this._addPlacedGlyphs()};k.prototype.updateSymbols=function(){this._iconIndexStart=this._iconIndexBuffer.index;this._textIndexStart=this._textIndexBuffer.index;this._textIndexCount=this._iconIndexCount=0;this._markerMap.clear();this._glyphMap.clear();var a=this._avoidEdges,b=this.layer,e;b.getLayoutProperty("icon-image")&&(e=this._getTranslate(!0));var c;b.getLayoutProperty("text-field")&&(c=this._getTranslate(!1));for(var b=0,g=this._symbolInstances;b<
g.length;b++)this._processFeature(g[b],e,c,a);this._addPlacedGlyphs()};k.prototype._getTranslate=function(a){var b=this.layer.getPaintValue(a?"icon-translate":"text-translate",this.zoom);if(0!==b[0]||0!==b[1]){var e=this._placementEngine.mapAngle;return 0!==e&&0===this.layer.getPaintValue(a?"icon-translate-anchor":"text-translate-anchor",this.zoom)?(a=Math.sin(e),e=Math.cos(e),[8*(b[0]*e-b[1]*a),8*(b[0]*a+b[1]*e)]):[8*b[0],8*b[1]]}};k.prototype._replaceKeys=function(a,b){return a.replace(/{([^{}]+)}/g,
function(a,c){return c in b?b[c]:""})};k.prototype._processFeature=function(a,b,e,c){var g=a.line,d=a.iconMosaicItem,f=a.shaping,u=a.anchor,n=this._iconLayout,k=n&&!!d,v=!0,y=1;k&&(n.size=a.iconSize,n.rotate=a.iconRotate,y=8*n.size,v=n.optional||!d);var h=this._textLayout,q=h&&f&&0<f.length,l;l=1;var p=!0;q&&(h.size=a.textSize,h.rotate=a.textRotate,l=h.size/24,l*=8,p=h.optional||!f||0===f.length);var x=new A.Point(0,-17),m;if(k){m=this._placementEngine.getIconPlacement(u,b,d,y,g,n,c);if(m.footprint.minzoom===
r.C_INFINITY&&!v)return;u.minzoom>m.footprint.minzoom&&(m.footprint.minzoom=u.minzoom)}var t;if(q&&(t=this._placementEngine.getTextPlacement(u,e,x,f,l,g,h,c))){if(t.footprint.minzoom===r.C_INFINITY&&!p)return;u.minzoom>t.footprint.minzoom&&(t.footprint.minzoom=u.minzoom)}if(!p&&!v||!v&&t&&t.footprint.minzoom!==r.C_INFINITY||!p&&m&&m.footprint.minzoom!==r.C_INFINITY)b=Math.max(m.footprint.minzoom,t.footprint.minzoom),m.footprint.minzoom=b,t.footprint.minzoom=b;t&&t.footprint.minzoom!==r.C_INFINITY&&
(h.ignorePlacement||this._placementEngine.add(t),this._storePlacedGlyphs(t.shapes,t.footprint.minzoom,this.zoom,a.ddTextValues));m&&m.footprint.minzoom!==r.C_INFINITY&&(n.ignorePlacement||this._placementEngine.add(m),this._addPlacedIcons(m.shapes,m.footprint.minzoom,this.zoom,d.page,a.ddIconValues))};k.prototype._addPlacedIcons=function(a,b,e,c,g){b=Math.max(e+r.log2(b),0);for(var d=this._iconVertexBuffer,f=this._iconIndexBuffer,u=0;u<a.length;u++){var n=a[u],k=Math.max(e+r.log2(n.minzoom),b),v=Math.min(e+
r.log2(n.maxzoom),25);if(!(v<=k)){var y=n.tl,h=n.tr,q=n.bl,l=n.br,p=n.mosaicRect,x=n.labelAngle,n=n.anchor,m=d.index,t=p.x,w=p.y,z=t+p.width,p=w+p.height;d.add(n.x,n.y,y.x,y.y,t,w,x,k,v,b,g);d.add(n.x,n.y,h.x,h.y,z,w,x,k,v,b,g);d.add(n.x,n.y,q.x,q.y,t,p,x,k,v,b,g);d.add(n.x,n.y,l.x,l.y,z,p,x,k,v,b,g);f.add(m+0,m+1,m+2);f.add(m+1,m+2,m+3);this._markerMap.has(c)?this._markerMap.get(c)[1]+=6:this._markerMap.set(c,[this._iconIndexStart+this._iconIndexCount,6]);this._iconIndexCount+=2}}};k.prototype._addPlacedGlyphs=
function(){var a=this,b=this._textVertexBuffer,e=this._textIndexBuffer;this._glyphBufferDataStorage.forEach(function(c,g){for(var d=0;d<c.length;d++){var f=c[d],u=b.index;b.add(f.glyphAnchor[0],f.glyphAnchor[1],f.tl[0],f.tl[1],f.xmin,f.ymin,f.labelAngle,f.minLod,f.maxLod,f.placementLod,f.ddValues);b.add(f.glyphAnchor[0],f.glyphAnchor[1],f.tr[0],f.tr[1],f.xmax,f.ymin,f.labelAngle,f.minLod,f.maxLod,f.placementLod,f.ddValues);b.add(f.glyphAnchor[0],f.glyphAnchor[1],f.bl[0],f.bl[1],f.xmin,f.ymax,f.labelAngle,
f.minLod,f.maxLod,f.placementLod,f.ddValues);b.add(f.glyphAnchor[0],f.glyphAnchor[1],f.br[0],f.br[1],f.xmax,f.ymax,f.labelAngle,f.minLod,f.maxLod,f.placementLod,f.ddValues);e.add(u+0,u+1,u+2);e.add(u+1,u+2,u+3);a._glyphMap.has(g)?a._glyphMap.get(g)[1]+=6:a._glyphMap.set(g,[a._textIndexStart+a._textIndexCount,6]);a._textIndexCount+=2}});this._glyphBufferDataStorage.clear()};k.prototype._storePlacedGlyphs=function(a,b,e,c){b=Math.max(e+r.log2(b),0);for(var g=0;g<a.length;g++){var d=a[g],f=Math.max(e+
r.log2(d.minzoom),b),u=Math.min(e+r.log2(d.maxzoom),25);if(!(u<=f)){var n=d.tl,k=d.tr,v=d.bl,y=d.br,h=d.labelAngle,q=d.anchor,l=d.mosaicRect;this._glyphBufferDataStorage.has(d.page)||this._glyphBufferDataStorage.set(d.page,[]);this._glyphBufferDataStorage.get(d.page).push({glyphAnchor:[q.x,q.y],tl:[n.x,n.y],tr:[k.x,k.y],bl:[v.x,v.y],br:[y.x,y.y],xmin:l.x,ymin:l.y,xmax:l.x+l.width,ymax:l.y+l.height,labelAngle:h,minLod:f,maxLod:u,placementLod:b,ddValues:c})}}};k._findAnchors=function(a,b,e){b+=e;for(var c=
0,g=a.length-1,d=0;d<g;d++)c+=A.Point.distance(a[d],a[d+1]);d=.5*(e||b);if(c<=d)return[];e=d/c;b=c/Math.max(Math.round(c/b),1);for(var c=0,g=-b/2,f=[],u=a.length-1,d=0;d<u;d++){for(var n=a[d],k=a[d+1],v=k.x-n.x,y=k.y-n.y,h=Math.sqrt(v*v+y*y),q=void 0;g+b<c+h;){var g=g+b,l=(g-c)/h,p=r.interpolate(n.x,k.x,l),l=r.interpolate(n.y,k.y,l);void 0===q&&(q=Math.atan2(y,v));f.push(new I.Anchor(p,l,q,d,e))}c+=h}return f};k.deviation=function(a,b,e){return Math.atan2((b.x-a.x)*(e.y-b.y)-(b.y-a.y)*(e.x-b.x),(b.x-
a.x)*(e.x-b.x)+(b.y-a.y)*(e.y-b.y))};k._honorsTextMaxAngle=function(a,b,e,c,g){var d=0;e/=2;for(var f=new A.Point(b.x,b.y),k=b.segment+1;d>-e;){--k;if(0>k)return!1;d-=A.Point.distance(a[k],f);f=a[k]}d+=A.Point.distance(a[k],a[k+1]);b=[];for(var f=0,n=a.length;d<e;){var r=a[k],v=void 0;do{++k;if(k===n)return!1;v=a[k]}while(v.isEqual(r));var y=k,h=void 0;do{++y;if(y===n)return!1;h=a[y]}while(h.isEqual(v));r=this.deviation(r,v,h);b.push({deviation:r,distToAnchor:d});for(f+=r;d-b[0].distToAnchor>g;)f-=
b.shift().deviation;if(Math.abs(f)>c)return!1;d+=A.Point.distance(v,h)}return!0};k._smoothVertices=function(a,b){if(!(0>=b)){var e=a.length;if(!(3>e)){var c=[],g=0;c.push(0);for(var d=1;d<e;d++)g+=A.Point.distance(a[d],a[d-1]),c.push(g);b=Math.min(b,.2*g);g=[];g.push(a[0].x);g.push(a[0].y);var f=a[e-1].x,k=a[e-1].y,d=A.Point.sub(a[0],a[1]);d.normalize();a[0].x+=b*d.x;a[0].y+=b*d.y;d.assignSub(a[e-1],a[e-2]);d.normalize();a[e-1].x+=b*d.x;a[e-1].y+=b*d.y;for(d=1;d<e;d++)c[d]+=b;c[e-1]+=b;for(var n=
.5*b,d=1;d<e-1;d++){for(var r=0,v=0,y=0,h=d-1;0<=h&&!(c[h+1]<c[d]-n);h--){var q=n+c[h+1]-c[d],l=c[h+1]-c[h],p=c[d]-c[h]<n?1:q/l;if(1E-6>Math.abs(p))break;var x=p*p,m=p*q-.5*x*l,t=p*l/b,w=a[h+1],z=a[h].x-w.x,B=a[h].y-w.y,r=r+t/m*(w.x*p*q+.5*x*(q*z-l*w.x)-x*p*l*z/3),v=v+t/m*(w.y*p*q+.5*x*(q*B-l*w.y)-x*p*l*B/3),y=y+t}for(h=d+1;h<e&&!(c[h-1]>c[d]+n);h++){q=n-c[h-1]+c[d];l=c[h]-c[h-1];p=c[h]-c[d]<n?1:q/l;if(1E-6>Math.abs(p))break;x=p*p;m=p*q-.5*x*l;t=p*l/b;w=a[h-1];z=a[h].x-w.x;B=a[h].y-w.y;r+=t/m*(w.x*
p*q+.5*x*(q*z-l*w.x)-x*p*l*z/3);v+=t/m*(w.y*p*q+.5*x*(q*B-l*w.y)-x*p*l*B/3);y+=t}g.push(r/y);g.push(v/y)}g.push(f);g.push(k);for(h=d=0;d<e;d++)a[d].x=g[h++],a[d].y=g[h++]}}};k._bidiEngine=new N;return k}(L)});