/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.24/esri/copyright.txt for details. */ import e from"../../Color.js";import t from"../../request.js";import{throwIfAborted as r}from"../../core/promiseUtils.js";import{pt2px as a}from"../../core/screenUtils.js";import{numericHash as i}from"../../core/string.js";import{getJsonType as s,isPolygon as o,isPolyline as n}from"../../geometry/support/jsonUtils.js";import{analyzeCIMSymbol as c,analyzeCIMResource as l}from"./cimAnalyzer.js";import m from"./CIMResourceManager.js";import h from"./Rasterizer.js";import f from"./TextRasterizer.js";import{createLabelOverrideFunction as g,evaluateValueOrFunction as u,colorToArray as y}from"./utils.js";import{scaleCIMMarker as p}from"../support/cimSymbolUtils.js";import{Symbol3DAnchorPosition2D as d}from"../support/Symbol3DAnchorPosition2D.js";var M;!function(e){e.Legend="legend",e.Preview="preview"}(M||(M={}));const C=(e,t,r)=>{if(e&&e.targetSize){let i;if(r){const t=Math.max(r.frame.xmax-r.frame.xmin,r.frame.ymax-r.frame.ymin);i=e.targetSize/a(t)}else i=e.targetSize/t.referenceSize;return i}return e&&e.scaleFactor?e.scaleFactor:1},I={fill:{legend:{frame:{xmax:15,xmin:0,ymax:15,ymin:0},geometry:{rings:[[[0,15],[15,7.5],[15,0],[0,0],[0,15]]]},canvasPaths:{rings:[[[0,15],[0,0],[15,7.5],[15,15],[0,15]]]}},preview:{frame:{xmax:100,xmin:0,ymax:100,ymin:0},geometry:{rings:[[[0,100],[100,100],[100,0],[0,0],[0,100]]]},canvasPaths:{rings:[[[0,100],[0,0],[100,0],[100,100],[0,100]]]}}},stroke:{legend:{frame:{xmax:24,xmin:0,ymax:2,ymin:-2},geometry:{paths:[[[0,0],[12,0],[24,0]]]},canvasPaths:{paths:[[[0,2],[12,2],[24,2]]]}},preview:{frame:{xmax:100,xmin:0,ymax:2,ymin:-2},geometry:{paths:[[[0,0],[50,0],[100,0]]]},canvasPaths:{paths:[[[0,2],[50,2],[100,2]]]}}}};class z{constructor(e,t){this._spatialReference=e,this._avoidSDF=t,this._resourceCache=new Map,this._pictureMarkerCache=new Map,this._textRasterizer=new f,this._cimResourceManager=new m,this._rasterizer=new h(this._cimResourceManager)}async rasterizeCIMSymbolAsync(e,t,r,a,i,o,n,c){a=a||(t?null!=t.centroid?"esriGeometryPolygon":s(t.geometry):null)||x(e);const l=await this.analyzeCIMSymbol(e,t?P(t.attributes):null,r,a,c);return this.rasterizeCIMSymbol(l,t,a,i,o,n)}async analyzeCIMSymbol(e,t,a,i,s){const o=[],n=t?{geometryType:i,spatialReference:this._spatialReference,fields:t}:null;let l;await c(e.data,n,this._cimResourceManager,o,this._avoidSDF),r(s);for(const r of o)"CIMPictureMarker"!==r.cim.type&&"CIMPictureFill"!==r.cim.type&&"CIMPictureStroke"!==r.cim.type||(l||(l=[]),l.push(this._fetchPictureMarkerResource(r,s))),a&&"text"===r.type&&"string"==typeof r.text&&r.text.includes("[")&&(r.text=g(a,r.text,r.cim.textCase));return l&&await Promise.all(l),o}async _fetchPictureMarkerResource(e,r){const a=e.materialHash;if(!this._pictureMarkerCache.get(a)){const i=(await t(e.cim.url,{responseType:"image",signal:r&&r.signal})).data;this._pictureMarkerCache.set(a,i)}}rasterizeCIMSymbol(e,t,r,a,i,s){const o=[];for(const n of e){a&&"function"==typeof a.scaleFactor&&(a.scaleFactor=a.scaleFactor(t,i,s));const e=this._getRasterizedResource(n,t,r,a,i,s);if(!e)continue;let c=0,l=e.anchorX||0,m=e.anchorY||0,h=!1,f=0,g=0;if("esriGeometryPoint"===r){const e=C(a,n,null);if(f=u(n.offsetX,t,i,s)*e||0,g=u(n.offsetY,t,i,s)*e||0,"marker"===n.type)c=u(n.rotation,t,i,s)||0,h=!!n.rotateClockwise&&n.rotateClockwise;else if("text"===n.type){if(c=u(n.angle,t,i,s)||0,void 0!==n.horizontalAlignment)switch(n.horizontalAlignment){case"left":l=-.5;break;case"right":l=.5;break;default:l=0}if(void 0!==n.verticalAlignment)switch(n.verticalAlignment){case"top":m=.5;break;case"bottom":m=-.5;break;case"baseline":m=-.25;break;default:m=0}}}null!=e&&o.push({angle:c,rotateClockWise:h,anchorX:l,anchorY:m,offsetX:f,offsetY:g,rasterizedResource:e})}return this.getSymbolImage(o)}getSymbolImage(e){const t=document.createElement("canvas"),r=t.getContext("2d");let i=0,s=0,o=0,n=0;const c=[];for(let f=0;fo?I:o,n=z>n?z:n;const P=r.createImageData(l.size[0],l.size[1]);P.data.set(new Uint8ClampedArray(l.image.buffer));const x={offsetX:h,offsetY:g,rotateClockwise:p,angle:d,rasterizedImage:P,anchorX:u,anchorY:y};c.push(x)}t.width=o-i,t.height=n-s;const l=-i,m=n;for(let f=0;f1){let t=o[0];m.moveTo(a(t[0]),a(t[1]));for(let r=1;r1?a(t):a(t)/s:i.width,n=t?s>1?a(t)*s:a(t):i.height;return{image:this._imageTo32Array(i,o,n,r),width:o,height:n}}_embedCIMLayerInVectorMarker(e,t){const r=o(t.geometry)?"CIMPolygonSymbol":"CIMLineSymbol",a=t.frame;return{type:"CIMVectorMarker",frame:a,size:a.ymax-a.ymin,markerGraphics:[{type:"CIMMarkerGraphic",geometry:t.geometry,symbol:{type:r,symbolLayers:[e]}}]}}}function P(e){return(e?Object.keys(e):[]).map((t=>({name:t,alias:t,type:"string"==typeof e[t]?"esriFieldTypeString":"esriFieldTypeDouble"})))}function x(e){if(!(e&&e.data&&e.data.symbol))return null;switch(e.data.symbol.type){case"CIMPointSymbol":case"CIMTextSymbol":return"esriGeometryPoint";case"CIMLineSymbol":return"esriGeometryPolyline";case"CIMPolygonSymbol":return"esriGeometryPolygon";default:return null}}function w(e,t,r,a){let i,s;if("function"==typeof e.materialHash){i=(0,e.materialHash)(t,r,a),s=l(e.cim,e.materialOverrides)}else i=e.materialHash,s=e.cim;return{analyzedCIM:s,hash:i}}export{z as CIMSymbolRasterizer,M as GeometryStyle};