/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{isLineBreak as t,isWhiteSpace as e,allowsIdeographicBreak as i,hasVerticalOrientation as h}from"./ScriptUtils.js";import o from"../webgl/Rect.js";const n=24,s=17;class c{constructor(t,e,i,h,o,n,s){this._glyphItems=t,this._maxWidth=e,this._lineHeight=i,this._letterSpacing=h,this._hAnchor=o,this._vAnchor=n,this._justify=s}getShaping(o,n,s){const c=this._letterSpacing,l=this._lineHeight,r=this._justify,a=this._maxWidth,f=[];let p=0,g=0;for(const t of o){const e=t.codePointAt(0);if(null==e)continue;const i=s&&h(e);let o;for(const t of this._glyphItems)if(o=t[e],o)break;f.push({codePoint:e,x:p,y:g,vertical:i,glyphItem:o}),o&&(p+=o.shaping.xAdvance+c)}let m=p;if(a>0){m=p/Math.max(1,Math.ceil(p/a))}const x=o.includes("​"),y=[],d=f.length;for(let e=0;e<d-1;e++){const h=f[e].codePoint,o=i(h);if(t(h)||o){let t=0;if(10===h)t-=1e4;else if(o&&x)t+=150;else{40!==h&&65288!==h||(t+=50);const i=f[e+1].codePoint;41!==i&&65289!==i||(t+=50)}y.push(this._buildBreak(e+1,f[e].x,m,y,t,!1))}}const u=this._optimalBreaks(this._buildBreak(d,p,m,y,0,!0));let _=0;const I=n?-l:l;let v=0;for(let t=0;t<u.length;t++){const i=u[t];let h=v;for(;h<i&&e(f[h].codePoint);)f[h].glyphItem=null,++h;let o=i-1;for(;o>h&&e(f[o].codePoint);)f[o].glyphItem=null,--o;if(h<=o){const t=f[h].x;for(let i=h;i<=o;i++)f[i].x-=t,f[i].y=g;let e=f[o].x;f[o].glyphItem&&(e+=f[o].glyphItem.shaping.xAdvance),_=Math.max(e,_),r&&this._applyJustification(f,h,o)}v=i,g+=I}if(f.length>0){const t=u.length-1,e=(r-this._hAnchor)*_;let i=(-this._vAnchor*(t+1)+.5)*l;n&&t&&(i+=t*l);for(const h of f)h.x+=e,h.y+=i}return f.filter(t=>t.glyphItem)}static getTextBox(t,e){if(!t.length)return null;let i=1/0,h=1/0,o=0,n=0;for(const c of t){const t=c.glyphItem.shaping.xAdvance,l=c.x,r=c.y-s,a=l+t,f=r+e;i=Math.min(i,l),o=Math.max(o,a),h=Math.min(h,r),n=Math.max(n,f)}return{x:i,y:h,width:o-i,height:n-h}}static getBox(t){if(!t.length)return null;let e=1/0,i=1/0,h=0,o=0;for(const n of t){const{height:t,left:s,top:c,width:l}=n.glyphItem.rect,r=n.x,a=n.y-(t-Math.abs(c)),f=r+l+s,p=a+t;e=Math.min(e,r),h=Math.max(h,f),i=Math.min(i,a),o=Math.max(o,p)}return{x:e,y:i,width:h-e,height:o-i}}static addDecoration(t,e){const i=t.length;if(0===i)return;const h=3,n={rect:new o(4,0,4,8),sdf:!0,page:0};let s=t[0].x+t[0].glyphItem.rect.left,c=t[0].y;for(let o=1;o<i;o++){const i=t[o];if(i.y!==c){const l=t[o-1].x+t[o-1].glyphItem.rect.left+t[o-1].glyphItem.rect.width;t.push({codePoint:0,x:s,y:c+e-h,vertical:!1,glyphItem:{mosaicItem:n,rect:{width:l-s,height:2+2*h,left:0,top:0},shaping:{xOffset:0,yOffset:0,xAdvance:0,yAdvance:0},code:0}}),c=i.y,s=i.x+i.glyphItem.rect.left}}const l=t[i-1].x+t[i-1].glyphItem.rect.left+t[i-1].glyphItem.rect.width;t.push({codePoint:0,x:s,y:c+e-h,vertical:!1,glyphItem:{mosaicItem:n,rect:{width:l-s,height:2+2*h,left:0,top:0},shaping:{xOffset:0,yOffset:0,xAdvance:0,yAdvance:0},code:0}})}_breakScore(t,e,i,h){const o=(t-e)*(t-e);return h?t<e?o/2:2*o:o+Math.abs(i)*i}_buildBreak(t,e,i,h,o,n){let s=null,c=this._breakScore(e,i,o,n);for(const l of h){const t=e-l.x,h=this._breakScore(t,i,o,n)+l.score;h<=c&&(s=l,c=h)}return{index:t,x:e,score:c,previousBreak:s}}_optimalBreaks(t){return t?this._optimalBreaks(t.previousBreak).concat(t.index):[]}_applyJustification(t,e,i){const h=t[i],o=h.vertical?n:h.glyphItem?h.glyphItem.shaping.xAdvance:0,s=(h.x+o)*this._justify;for(let n=e;n<=i;n++)t[n].x-=s}}export{c as TextShaping,s as sdfGlyphBaseline,n as sdfGlyphSize};
