/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","./ScriptUtils","../webgl/Rect"],(function(t,e,i){"use strict";const o=24,c=17;let s=function(){function t(t,e,i,o,c,s,n){this._glyphItems=t,this._maxWidth=e,this._lineHeight=i,this._letterSpacing=o,this._hAnchor=c,this._vAnchor=s,this._justify=n}var s=t.prototype;return s.getShaping=function(t,i,o){const c=this._letterSpacing,s=this._lineHeight,n=this._justify,h=this._maxWidth,a=[];let l=0,r=0;const f=t.length;for(let M=0;M<f;M++){const i=t.charCodeAt(M),s=o&&e.hasVerticalOrientation(i);let n;for(const t of this._glyphItems)if(n=t[i],n)break;a.push({codePoint:i,x:l,y:r,vertical:s,glyphMosaicItem:n}),n&&(l+=n.metrics.advance+c)}let p=l;if(h>0){p=l/Math.max(1,Math.ceil(l/h))}const m=t.includes("​"),g=[];for(let M=0;M<f-1;M++){const t=a[M].codePoint,i=e.allowsIdeographicBreak(t);if(e.isLineBreak(t)||i){let e=0;if(10===t)e-=1e4;else if(i&&m)e+=150;else{40!==t&&65288!==t||(e+=50);const i=a[M+1].codePoint;41!==i&&65289!==i||(e+=50)}g.push(this._buildBreak(M+1,a[M].x,p,g,e,!1))}}const u=this._optimalBreaks(this._buildBreak(f,l,p,g,0,!0));let d=0;const y=i?-s:s;let x=0;for(let M=0;M<u.length;M++){const t=u[M];let i=x;for(;i<t&&e.isWhiteSpace(a[i].codePoint);)a[i].glyphMosaicItem=null,++i;let o=t-1;for(;o>i&&e.isWhiteSpace(a[o].codePoint);)a[o].glyphMosaicItem=null,--o;if(i<=o){const t=a[i].x;for(let c=i;c<=o;c++)a[c].x-=t,a[c].y=r;let e=a[o].x;a[o].glyphMosaicItem&&(e+=a[o].glyphMosaicItem.metrics.advance),d=Math.max(e,d),n&&this._applyJustification(a,i,o)}x=t,r+=y}if(a.length>0){const t=u.length-1,e=(n-this._hAnchor)*d;let o=(-this._vAnchor*(t+1)+.5)*s;i&&t&&(o+=t*s);for(const i of a)i.x+=e,i.y+=o}return a.filter((t=>t.glyphMosaicItem))},t.getTextBox=function(t,e){if(!t.length)return null;let i=1/0,o=1/0,s=0,n=0;for(const h of t){const t=h.glyphMosaicItem.metrics.advance,a=h.x,l=h.y-c,r=a+t,f=l+e;i=Math.min(i,a),s=Math.max(s,r),o=Math.min(o,l),n=Math.max(n,f)}return{x:i,y:o,width:s-i,height:n-o}},t.getBox=function(t){if(!t.length)return null;let e=1/0,i=1/0,o=0,c=0;for(const s of t){const{height:t,left:n,top:h,width:a}=s.glyphMosaicItem.metrics,l=s.x,r=s.y-(t-Math.abs(h)),f=l+a+n,p=r+t;e=Math.min(e,l),o=Math.max(o,f),i=Math.min(i,r),c=Math.max(c,p)}return{x:e,y:i,width:o-e,height:c-i}},t.addDecoration=function(t,e){const o=t.length;if(0===o)return;const c=3;let s=t[0].x+t[0].glyphMosaicItem.metrics.left,n=t[0].y;for(let a=1;a<o;a++){const o=t[a];if(o.y!==n){const h=t[a-1].x+t[a-1].glyphMosaicItem.metrics.left+t[a-1].glyphMosaicItem.metrics.width;t.push({codePoint:0,x:s,y:n+e-c,vertical:!1,glyphMosaicItem:{sdf:!0,rect:new i(4,0,4,8),metrics:{width:h-s,height:2+2*c,left:0,top:0,advance:0},page:0,code:0}}),n=o.y,s=o.x+o.glyphMosaicItem.metrics.left}}const h=t[o-1].x+t[o-1].glyphMosaicItem.metrics.left+t[o-1].glyphMosaicItem.metrics.width;t.push({codePoint:0,x:s,y:n+e-c,vertical:!1,glyphMosaicItem:{sdf:!0,rect:new i(4,0,4,8),metrics:{width:h-s,height:2+2*c,left:0,top:0,advance:0},page:0,code:0}})},s._breakScore=function(t,e,i,o){const c=(t-e)*(t-e);return o?t<e?c/2:2*c:c+Math.abs(i)*i},s._buildBreak=function(t,e,i,o,c,s){let n=null,h=this._breakScore(e,i,c,s);for(const a of o){const t=e-a.x,o=this._breakScore(t,i,c,s)+a.score;o<=h&&(n=a,h=o)}return{index:t,x:e,score:h,previousBreak:n}},s._optimalBreaks=function(t){return t?this._optimalBreaks(t.previousBreak).concat(t.index):[]},s._applyJustification=function(t,e,i){const c=t[i],s=c.vertical?o:c.glyphMosaicItem?c.glyphMosaicItem.metrics.advance:0,n=(c.x+s)*this._justify;for(let o=e;o<=i;o++)t[o].x-=n},t}();t.SDF_GLYPH_BASELINE=c,t.SDF_GLYPH_SIZE=o,t.TextShaping=s,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
