@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 3.85 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import e from"./CIMResourceManager.js";import{Transformation as t,CanvasDrawHelper as i}from"./CIMSymbolDrawHelper.js";import{CIMSymbolHelper as h}from"./CIMSymbolHelper.js";import{OverrideHelper as r}from"./OverrideHelper.js";import{scale as n,translate as s}from"./rasterizingUtils.js";import{mapCIMSymbolToGeometryType as a}from"./utils.js";const o=96/72;class l{constructor(t){this._spatialReference=t,this._imageDataCanvas=null,this._cimResourceManager=new e}get _canvas(){return this._imageDataCanvas||(this._imageDataCanvas=document.createElement("canvas")),this._imageDataCanvas}get resourceManager(){return this._cimResourceManager}async rasterizeCIMSymbolAsync(e,t,i,s,o,l,m,g,y,d){if(!e)return null;const{data:w}=e;if(!w||"CIMSymbolReference"!==w.type||!w.symbol)return null;const{symbol:u}=w;l||(l=a(u));const x=await r.resolveSymbolOverrides(w,t,this._spatialReference,o,l,m,g),f=this._cimResourceManager,p=[];h.fetchResources(x,f,p),h.fetchFonts(x,f,p),p.length>0&&await Promise.all(p);const{width:b,height:M}=i;let C=c(l,b,M,s,d);const R=h.getEnvelope(x,C,f);if(!R)return null;R.x===1/0&&(R.x=b+2),R.y===1/0&&(R.y=-M/2),R.width===-1/0&&(R.width=b),R.height===-1/0&&(R.height=M);let v=1,I=0,S=0;switch(u.type){case"CIMPointSymbol":case"CIMTextSymbol":{let e=1;R.width>b&&(e=b/R.width);let t=1;R.height>M&&(t=M/R.height),"preview"===s&&(R.width<b&&(e=b/R.width),R.height<M&&(t=M/R.height)),v=Math.min(e,t),I=R.x+R.width/2,S=R.y+R.height/2}break;case"CIMLineSymbol":if(d){S=R.y+R.height/2,I=R.x+R.width/2;const e=R.width-b,t=R.height-M;C={paths:n(C.paths,{xmin:-1*R.width/2+e,xmax:R.width/2-e,ymin:-1*R.height/2+t,ymax:R.height/2-t,width:R.width-2*e,height:R.height-2*t})}}else{(y||R.height>M)&&(v=M/R.height),S=R.y+R.height/2;const e=R.x*v+b/2,t=(R.x+R.width)*v+b/2,{paths:i}=C;i[0][0][0]-=e/v,i[0][2][0]-=(t-b)/v}break;case"CIMPolygonSymbol":if(d){S=R.y+R.height/2,I=R.x+R.width/2;const e=R.width-b,t=R.height-M;C={paths:n(C.rings,{xmin:-1*R.width/2+e,xmax:R.width/2-e,ymin:-1*R.height/2+t,ymax:R.height/2-t,width:R.width-2*e,height:R.height-2*t})}}else{I=R.x+R.width/2,S=R.y+R.height/2;const e=R.x*v+b/2,t=(R.x+R.width)*v+b/2,i=R.y*v+M/2,h=(R.y+R.height)*v+M/2,{rings:r}=C;e<0&&(r[0][0][0]-=e,r[0][3][0]-=e,r[0][4][0]-=e),i<0&&(r[0][0][1]+=i,r[0][1][1]+=i,r[0][4][1]+=i),t>b&&(r[0][1][0]-=t-b,r[0][2][0]-=t-b),h>M&&(r[0][2][1]+=h-M,r[0][3][1]+=h-M)}}const _={type:"cim",data:{type:"CIMSymbolReference",symbol:x}};return this.rasterize(_,b,M,I,S,v,l,1,C)}rasterize(e,h,r,n,s,l,m,g=0,y=null,d=window.devicePixelRatio||1){const{data:w}=e;if(!w||"CIMSymbolReference"!==w.type||!w.symbol)return null;const{symbol:u}=w,x=this._canvas,f=d*o;x.width=h*f,x.height=r*f,m||(m=a(u)),y||(y=c(m,h,r,"legend")),x.width+=2*g,x.height+=2*g;const p=x.getContext("2d",{willReadFrequently:!0}),b=t.createIdentity();b.translate(-n,-s),b.scale(l*f,-l*f),b.translate(h*f/2+g,r*f/2+g),p.clearRect(0,0,x.width,x.height);return new i(p,this._cimResourceManager,b,!0).drawSymbol(u,y),p.getImageData(0,0,x.width,x.height)}}function m(e,t,i,h){if("esriGeometryPolygon"===t){return{rings:s(n(e.rings,{xmin:0,ymin:0,xmax:i,ymax:h,width:i,height:h}),-1*i/2,-1*h/2)}}if("esriGeometryPolyline"===t){return{paths:s(n(e.paths,{xmin:0,ymin:0,xmax:i,ymax:h,width:i,height:h}),-1*i/2,-1*h/2)}}return null}function c(e,t,i,h,r){const n=1,s=-t/2+n,a=t/2-n,o=i/2-n,l=-i/2+n;if(r&&("esriGeometryPolygon"===e||"esriGeometryPolyline"===e)){const h=m(r,e,t,i);if(h)return h}switch(e){case"esriGeometryPoint":return{x:0,y:0};case"esriGeometryPolyline":return{paths:[[[s,0],[0,0],[a,0]]]};default:return"legend"===h?{rings:[[[s,o],[a,0],[a,l],[s,l],[s,o]]]}:{rings:[[[s,o],[a,o],[a,l],[s,l],[s,o]]]}}}export{l as CIMSymbolRasterizer};