UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 3.22 kB
import{nextPowerOfTwo as e}from"../../core/mathUtils.js";import{slsDashToTemplateArray as t,CIMSymbolHelper as a}from"./CIMSymbolHelper.js";import{rasterizeSimpleFill as r,rasterizeDash as i}from"./rasterizingUtils.js";import n from"./Rect.js";import{getSDFInfo as s,buildSDF as o}from"./SDFHelper.js";import{resampleHermite as m}from"./utils.js";const l=512;class c{constructor(e){this._resourceManager=e,this._rasterizationCanvas=null}dispose(){this._rasterizationCanvas=null}rasterizeJSONResource(m,l,c){if(this._rasterizationCanvas||(this._rasterizationCanvas=document.createElement("canvas")),"simple-fill"===m.type||"esriSFS"===m.type){const[t,a,i]=r(this._rasterizationCanvas,m.style,l);return{size:[a,i],image:new Uint32Array(t.buffer),sdf:!1,simplePattern:!0,anchorX:0,anchorY:0,rasterizationScale:e(Math.ceil(l))}}if("simple-line"===m.type||"esriSLS"===m.type||"line"===m.type&&m.dashTemplate){let e,a;if("simple-line"===m.type||"esriSLS"===m.type)switch(e=t(m.style,m.cap),m.cap){case"butt":a="Butt";break;case"square":a="Square";break;default:a="Round"}else e=m.dashTemplate,a=m.cim.capStyle;const[r,n,s]=i(e,a);return{size:[n,s],image:new Uint32Array(r.buffer),sdf:!0,simplePattern:!0,anchorX:0,anchorY:0}}let f,h=null,p=null,u=1;if("simple-marker"===m.type||"esriSMS"===m.type||"line-marker"===m.type?(f=a.fromSimpleMarker(m),p=s(f)):m.cim&&"CIMHatchFill"===m.cim.type?(f=a.fromCIMHatchFill(m.cim,l),h=new n(f.frame.xmin,-f.frame.ymax,f.frame.xmax-f.frame.xmin,f.frame.ymax-f.frame.ymin),u=l):m.cim.markerPlacement&&"CIMMarkerPlacementInsidePolygon"===m.cim.markerPlacement.type?(f=a.fromCIMInsidePolygon(m.cim),h=new n(f.frame.xmin,-f.frame.ymax,f.frame.xmax-f.frame.xmin,f.frame.ymax-f.frame.ymin)):(f=m.cim,m.avoidSDFRasterization||(p=s(f))),p&&!c){const[e,t,a]=o(p);return e?{size:[t,a],image:new Uint32Array(e.buffer),sdf:!0,simplePattern:!0,anchorX:0,anchorY:0,rasterizationScale:u}:null}const[y,d,C,g,z]=a.rasterize(this._rasterizationCanvas,f,h,this._resourceManager,!c);return y?{size:[d,C],image:new Uint32Array(y.buffer),sdf:!1,simplePattern:!1,anchorX:g,anchorY:z}:null}rasterizeImageResource(e,t,a,r){this._rasterizationCanvas||(this._rasterizationCanvas=document.createElement("canvas")),this._rasterizationCanvas.width=e,this._rasterizationCanvas.height=t;const i=this._rasterizationCanvas.getContext("2d");a instanceof ImageData?i.putImageData(a,0,0):(a.setAttribute("width",`${e}px`),a.setAttribute("height",`${t}px`),i.drawImage(a,0,0,e,t));const n=i.getImageData(0,0,e,t),s=new Uint8Array(n.data);if(r)for(const m of r)if(m&&m.oldColor&&4===m.oldColor.length&&m.newColor&&4===m.newColor.length){const[e,t,a,r]=m.oldColor,[i,n,o,l]=m.newColor;if(e===i&&t===n&&a===o&&r===l)continue;for(let m=0;m<s.length;m+=4)e===s[m]&&t===s[m+1]&&a===s[m+2]&&r===s[m+3]&&(s[m]=i,s[m+1]=n,s[m+2]=o,s[m+3]=l)}let o;for(let m=0;m<s.length;m+=4)o=s[m+3]/255,s[m]=s[m]*o,s[m+1]=s[m+1]*o,s[m+2]=s[m+2]*o;let c=s,f=e,h=t;const p=l;if(f>=p||h>=p){const a=f/h;a>1?(f=p,h=Math.round(p/a)):(h=p,f=Math.round(p*a)),c=new Uint8Array(4*f*h);const r=new Uint8ClampedArray(c.buffer);m(s,e,t,r,f,h,!1)}return{size:[f,h],image:new Uint32Array(c.buffer),sdf:!1,simplePattern:!1,anchorX:0,anchorY:0}}}export{c as default};