/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import e from"../../Color.js";import t from"../../request.js";import{unwrapOrThrow as a}from"../../core/maybe.js";import{throwIfAborted as i}from"../../core/promiseUtils.js";import{pt2px as r}from"../../core/screenUtils.js";import{analyzeCIMSymbol as s,analyzeCIMResource as o}from"./cimAnalyzer.js";import n from"./CIMResourceManager.js";import{Transformation as c,CanvasDrawHelper as h}from"./CIMSymbolDrawHelper.js";import{OverrideHelper as l,CIMSymbolHelper as m}from"./CIMSymbolHelper.js";import g from"./Rasterizer.js";import u from"./TextRasterizer.js";import{mapCIMSymbolToGeometryType as f,createLabelOverrideFunction as d,evaluateValueOrFunction as p,colorToArray as y}from"./utils.js";import{scaleCIMMarker as w}from"../support/cimSymbolUtils.js";import{Symbol3DAnchorPosition2D as C}from"../support/Symbol3DAnchorPosition2D.js";var M;!function(e){e.Legend="legend",e.Preview="preview"}(M||(M={}));const _=e=>e&&e.scaleFactor?e.scaleFactor:1,v=96/72;class z{constructor(e,t){this._spatialReference=e,this._avoidSDF=t,this._resourceCache=new Map,this._imageDataCanvas=null,this._pictureMarkerCache=new Map,this._textRasterizer=new u,this._cimResourceManager=new n,this._rasterizer=new g(this._cimResourceManager)}get resourceManager(){return this._cimResourceManager}async rasterizeCIMSymbolAsync(e,t,a,i,r,s,o,n){if(!e)return null;const{data:g}=e;if(!g||"CIMSymbolReference"!==g.type||!g.symbol)return null;const{symbol:u}=g;s||(s=f(u));const d=await l.resolveSymbolOverrides(g,t,this._spatialReference,r,s,o,n);this._imageDataCanvas||(this._imageDataCanvas=document.createElement("canvas"));const p=this._imageDataCanvas,y=this._cimResourceManager,w=[];m.fetchResources(d,y,w),w.length>0&&await Promise.all(w);const{width:C,height:M}=a,_=I(s,C,M,i),z=m.getEnvelope(d,_,y);if(!z)return null;const x=(window.devicePixelRatio||1)*v;let R=1,b=0,P=0;switch(u.type){case"CIMPointSymbol":case"CIMTextSymbol":{let e=1;z.width>C&&(e=C/z.width);let t=1;z.height>M&&(t=M/z.height),"preview"===i&&(z.widthM&&(e=M/z.height),R=e,P=z.y+z.height/2;const t=z.x*R+C/2,a=(z.x+z.width)*R+C/2;if(t<0){const{paths:e}=_;e[0][0][0]-=t}if(a>C){const{paths:e}=_;e[0][2][0]-=a-C}}break;case"CIMPolygonSymbol":{b=z.x+z.width/2,P=z.y+z.height/2;const e=z.x*R+C/2,t=(z.x+z.width)*R+C/2,a=z.y*R+M/2,i=(z.y+z.height)*R+M/2,{rings:r}=_;e<0&&(r[0][0][0]-=e,r[0][3][0]-=e,r[0][4][0]-=e),a<0&&(r[0][0][1]+=a,r[0][1][1]+=a,r[0][4][1]+=a),t>C&&(r[0][1][0]-=t-C,r[0][2][0]-=t-C),i>M&&(r[0][2][1]+=i-M,r[0][3][1]+=i-M)}}p.width=C*x,p.height=M*x;const D=1;p.width+=2*D,p.height+=2*D;const S=p.getContext("2d"),k=c.createIdentity();k.translate(-b,-P),k.scale(R*x,-R*x),k.translate(C*x/2+D,M*x/2+D),S.clearRect(0,0,p.width,p.height);return new h(S,y,k,!0).drawSymbol(d,_),S.getImageData(0,0,p.width,p.height)}async analyzeCIMSymbol(e,t,a,r,o){const n=[],c=t?{geometryType:r,spatialReference:this._spatialReference,fields:t}:null;let h;await s(e.data,c,this._cimResourceManager,n,this._avoidSDF),i(o);for(const i of n)"CIMPictureMarker"!==i.cim.type&&"CIMPictureFill"!==i.cim.type&&"CIMPictureStroke"!==i.cim.type||(h||(h=[]),h.push(this._fetchPictureMarkerResource(i,o))),a&&"text"===i.type&&"string"==typeof i.text&&i.text.includes("[")&&(i.text=d(a,i.text,i.cim.textCase));return h&&await Promise.all(h),n}rasterizeCIMSymbol3D(e,t,a,i,r,s){const o=[];for(const n of e){i&&"function"==typeof i.scaleFactor&&(i.scaleFactor=i.scaleFactor(t,r,s));const e=this._getRasterizedResource(n,t,a,i,r,s);if(!e)continue;let c=0,h=e.anchorX||0,l=e.anchorY||0,m=!1,g=0,u=0;if("esriGeometryPoint"===a){const e=_(i);if(g=p(n.offsetX,t,r,s)*e||0,u=p(n.offsetY,t,r,s)*e||0,"marker"===n.type)c=p(n.rotation,t,r,s)||0,m=!!n.rotateClockwise&&n.rotateClockwise;else if("text"===n.type){if(c=p(n.angle,t,r,s)||0,void 0!==n.horizontalAlignment)switch(n.horizontalAlignment){case"left":h=-.5;break;case"right":h=.5;break;default:h=0}if(void 0!==n.verticalAlignment)switch(n.verticalAlignment){case"top":l=.5;break;case"bottom":l=-.5;break;case"baseline":l=-.25;break;default:l=0}}}null!=e&&o.push({angle:c,rotateClockWise:m,anchorX:h,anchorY:l,offsetX:g,offsetY:u,rasterizedResource:e})}return this.getSymbolImage(o)}getSymbolImage(e){const t=document.createElement("canvas"),i=a(t.getContext("2d"));let s=0,o=0,n=0,c=0;const h=[];for(let a=0;an?M:n,c=_>c?_:c;const v=i.createImageData(l.size[0],l.size[1]);v.data.set(new Uint8ClampedArray(l.image.buffer));const z={offsetX:g,offsetY:u,rotateClockwise:p,angle:y,rasterizedImage:v,anchorX:f,anchorY:d};h.push(z)}t.width=n-s,t.height=c-o;const l=-s,m=c;for(let a=0;a1?r(t):r(t)/s:i.width,n=t?s>1?r(t)*s:r(t):i.height;return{image:this._imageTo32Array(i,o,n,a),width:o,height:n}}}function I(e,t,a,i){const r=1,s=-t/2+r,o=t/2-r,n=a/2-r,c=-a/2+r;switch(e){case"esriGeometryPoint":return{x:0,y:0};case"esriGeometryPolyline":return{paths:[[[s,0],[0,0],[o,0]]]};default:return"legend"===i?{rings:[[[s,n],[o,0],[o,c],[s,c],[s,n]]]}:{rings:[[[s,n],[o,n],[o,c],[s,c],[s,n]]]}}}function x(e,t,a,i){let r,s;if("function"==typeof e.materialHash){r=(0,e.materialHash)(t,a,i),s=o(e.cim,e.materialOverrides)}else r=e.materialHash,s=e.cim;return{analyzedCIM:s,hash:r}}export{z as CIMSymbolRasterizer,M as GeometryStyle};