/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import{pt2px as t}from"../../core/screenUtils.js";function e(t){return`rgb(${t.slice(0,3).toString()})`}function i(t){return`rgba(${t.slice(0,3).toString()},${t[3]})`}class s{constructor(t){t&&(this._textRasterizationCanvas=t)}rasterizeText(t,s){this._textRasterizationCanvas||(this._textRasterizationCanvas=document.createElement("canvas"));const r=this._textRasterizationCanvas,o=r.getContext("2d");this._setFontProperties(o,s),this._parameters=s,this._textLines=t.split(/\r?\n/),this._lineHeight=this._computeLineHeight();const h=this._computeTextWidth(o,s),{decoration:a,weight:l}=s.font;this._lineThroughWidthOffset=a&&"line-through"===a?.1*this._lineHeight:0;const d=this._lineHeight*this._textLines.length;r.width=h+2*this._lineThroughWidthOffset,r.height=d,this._renderedLineHeight=Math.round(this._lineHeight*s.pixelRatio),this._renderedHaloSize=s.halo.size*s.pixelRatio,this._renderedWidth=h*s.pixelRatio,this._renderedHeight=d*s.pixelRatio,this._lineThroughWidthOffset*=s.pixelRatio;const c=s.color??[0,0,0,0],_=s.halo&&s.halo.color?s.halo.color:[0,0,0,0];this._fillStyle=i(c),this._haloStyle=e(_);const g=this._renderedLineHeight,f=this._renderedHaloSize;o.save(),o.clearRect(0,0,r.width,r.height),this._setFontProperties(o,s);const u=n(o.textAlign,this._renderedWidth)+f,p=f,x=f>0;let m=this._lineThroughWidthOffset,b=0;x&&this._renderHalo(o,u,p,m,b,s),b+=p,m+=u;for(const e of this._textLines)x?(o.globalCompositeOperation="destination-out",o.fillStyle="rgb(0, 0, 0)",o.fillText(e,m,b),o.globalCompositeOperation="source-over",o.fillStyle=this._fillStyle,o.fillText(e,m,b)):(o.fillStyle=this._fillStyle,o.fillText(e,m,b)),a&&"none"!==a&&this._renderDecoration(o,m,b,a,l),b+=g;o.restore();const z=this._renderedWidth+2*this._lineThroughWidthOffset,w=this._renderedHeight,v=o.getImageData(0,0,z,w),H=new Uint8Array(v.data);if(s.premultiplyColors){let t;for(let e=0;e600)&&(i+=.3*t.measureText("w").width),i+=2*this._parameters.halo.size,Math.round(i)}_computeLineHeight(){let t=1.275*this._parameters.size;const e=this._parameters.font.decoration;return e&&"underline"===e&&(t*=1.3),Math.round(t+2*this._parameters.halo.size)}_renderDecoration(t,e,i,s,n,r){const o=.9*this._lineHeight,h="bold"===n?.06:"bolder"===n?.09:.04;switch(t.textAlign){case"center":e-=this._renderedWidth/2;break;case"right":e-=this._renderedWidth}const a=t.textBaseline;if("underline"===s)switch(a){case"top":i+=o;break;case"middle":i+=o/2}else if("line-through"===s)switch(a){case"top":i+=o/1.5;break;case"middle":i+=o/3}const l=r?1.5*r:Math.ceil(o*h);t.save(),t.beginPath(),t.strokeStyle=t.fillStyle,t.lineWidth=l,t.moveTo(e-this._lineThroughWidthOffset,i),t.lineTo(e+this._renderedWidth+2*this._lineThroughWidthOffset,i),t.stroke(),t.restore()}}function n(t,e){return"center"===t?.5*e:"right"===t?e:0}export{s as default};