@nasser-sw/fabric 7.0.1-beta12 → 7.0.1-beta13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- var a="7.0.1-beta11";export{a as version};
1
+ var a="7.0.1-beta12";export{a as version};
2
2
  //# sourceMappingURL=package.json.min.mjs.map
@@ -1,4 +1,4 @@
1
- var version = "7.0.1-beta11";
1
+ var version = "7.0.1-beta12";
2
2
 
3
3
  export { version };
4
4
  //# sourceMappingURL=package.json.mjs.map
@@ -1,2 +1,2 @@
1
- import{defineProperty as t}from"../../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{cache as e}from"../../cache.min.mjs";import{RIGHT as i,CENTER as s,STROKE as n,BOTTOM as r,TOP as h,FILL as o,LEFT as l,DEFAULT_SVG_FONT_SIZE as a}from"../../constants.min.mjs";import{StyledText as d}from"./StyledText.min.mjs";import{SHARED_ATTRIBUTES as c}from"../../parser/attributes.min.mjs";import{parseAttributes as f}from"../../parser/parseAttributes.min.mjs";import{classRegistry as g}from"../../ClassRegistry.min.mjs";import{graphemeSplit as u}from"../../util/lang_string.min.mjs";import{createCanvasElementFor as p}from"../../util/misc/dom.min.mjs";import{layoutText as m}from"../../text/layout.min.mjs";import{segmentGraphemes as _}from"../../text/unicode.min.mjs";import{hasStyleChanged as x,stylesToArray as S,stylesFromArray as y}from"../../util/misc/textStyles.min.mjs";import{getPathSegmentsInfo as T,getPointOnPath as L}from"../../util/path/index.min.mjs";import"../Object/FabricObject.min.mjs";import{TextSVGExportMixin as w}from"./TextSVGExportMixin.min.mjs";import{applyMixins as v}from"../../util/applyMixins.min.mjs";import{textLayoutProperties as O,additionalProps as A,textDefaultValues as C,JUSTIFY as k,JUSTIFY_CENTER as F,JUSTIFY_RIGHT as W,JUSTIFY_LEFT as z,TEXT_DECORATION_THICKNESS as b}from"./constants.min.mjs";import{isFiller as D}from"../../util/typeAssertions.min.mjs";import{getBrowserLines as H,clearBrowserLines as j}from"../../text/browserLines.min.mjs";import{cacheProperties as B}from"../Object/defaultValues.min.mjs";let P;class I extends d{static getDefaults(){return{...super.getDefaults(),...I.ownDefaults}}constructor(e,i){super(),t(this,"__charBounds",[]),Object.assign(this,I.ownDefaults),this.setOptions(i),this.styles||(this.styles={}),this.text=e,this.initialized=!0,this.path&&this.setPathInfo(),this.initDimensions(),this.setCoords()}setPathInfo(){const t=this.path;t&&(t.segmentsInfo=T(t.path))}_splitText(){const t=H(this);if(t&&this.useOverlayEditing)return this._splitTextFromBrowserLines(t);const e=this._splitTextIntoLines(this.text);return this.textLines=e.lines,this._textLines=e.graphemeLines,this._unwrappedTextLines=e._unwrappedLines,this._text=e.graphemeText,e}_splitTextFromBrowserLines(t){const e=[],i=[],s=[];let n=[];for(const r of t){e.push(r.text);const h=this.graphemeSplit(r.text);i.push(h),s.push(h),n=n.concat(h),r!==t[t.length-1]&&n.push("\n")}const r={lines:e,graphemeLines:i,graphemeText:n,_unwrappedLines:s};return this.textLines=r.lines,this._textLines=r.graphemeLines,this._unwrappedTextLines=r._unwrappedLines,this._text=r.graphemeText,r}initDimensions(){if(this._isFontReady()||this.initialized||this._scheduleInitAfterFontLoad(),this.enableAdvancedLayout&&!this.path)return this.initDimensionsAdvanced();this._splitText(),this._clearCache(),this.dirty=!0,this.path?(this.width=this.path.width,this.height=this.path.height):(this.width=this.calcTextWidth()||this.cursorWidth||this.MIN_TEXT_WIDTH,this.height=this.calcTextHeight()),this.textAlign.includes(k)&&(this.__charBounds&&this.__charBounds.length>0?this.enlargeSpaces():(console.warn("⚠️ __charBounds not ready for justify alignment, deferring enlargeSpaces"),setTimeout(()=>{var t;this.__charBounds&&this.__charBounds.length>0&&this.enlargeSpaces&&(console.log("🔧 Applying deferred justify alignment"),this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.requestRenderAll())},0)))}enlargeSpaces(){let t,e,i,s,n,r,h;const o="rtl"===this.direction;for(let l=0,a=this._textLines.length;l<a;l++)if((this.textAlign.includes("justify")||l!==a-1&&!this.isEndOfWrapping(l))&&(s=0,n=this._textLines[l],e=this.getLineWidth(l),e<this.width&&(h=this.textLines[l].match(this._reSpacesAndTabs))))if(i=h.length,t=(this.width-e)/i,console.log(`🔧 EnlargeSpaces Line ${l}:`),console.log(` Current width: ${e}, Target: ${this.width}`),console.log(` Spaces: ${i}, diffSpace: ${t.toFixed(2)}`),o){for(let t=0;t<n.length;t++)this._reSpaceAndTab.test(n[t]);let e=0;for(let s=0;s<=n.length;s++)if(r=this.__charBounds[l][s],r){this._reSpaceAndTab.test(n[s])&&(r.width+=t,r.kernedWidth+=t,e++);const h=(i-e)*t;r.left+=h}}else for(let e=0;e<=n.length;e++)r=this.__charBounds[l][e],r&&(this._reSpaceAndTab.test(n[e])?(r.width+=t,r.kernedWidth+=t,r.left+=s,s+=t):r.left+=s)}_layoutTextAdvanced(){const t=this._getAdvancedLayoutOptions();return m(t)}_getAdvancedLayoutOptions(){return{text:this.text,width:this.width,height:this.height,wrap:this.wrap||"word",align:this._mapTextAlignToAlign(this.textAlign),ellipsis:this.ellipsis||!1,fontSize:this.fontSize,lineHeight:this.lineHeight,letterSpacing:this.letterSpacing||0,charSpacing:this.charSpacing,direction:"inherit"===this.direction?"ltr":this.direction,fontFamily:this.fontFamily,fontStyle:this.fontStyle,fontWeight:this.fontWeight,verticalAlign:this.verticalAlign||"top"}}_mapTextAlignToAlign(t){switch(t){case"center":case s:return"center";case"right":case i:return"right";case"justify":case k:case z:case W:case F:return"justify";default:return"left"}}initDimensionsAdvanced(){if(!this.enableAdvancedLayout)return this.initDimensions();const t=this._layoutTextAdvanced();this.width=t.totalWidth||this.MIN_TEXT_WIDTH,this.height=t.totalHeight,this._convertLayoutToLegacyFormat(t),this.textAlign.includes(k)&&setTimeout(()=>{var t;this.enlargeSpaces&&(this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.renderAll())},0),this.dirty=!0}_convertLayoutToLegacyFormat(t){this._textLines=t.lines.map(t=>t.graphemes),this.textLines=t.lines.map(t=>t.text),this.__charBounds=t.lines.map(t=>t.bounds.map(t=>({left:t.left,top:t.y,width:t.width,height:t.height,kernedWidth:t.kernedWidth,deltaY:t.deltaY||0}))),t.lines.length>0&&(this._unwrappedTextLines=t.lines.map(t=>t.graphemes))}isEndOfWrapping(t){return t===this._textLines.length-1}missingNewlineOffset(t){return 1}get2DCursorLocation(t,e){const i=e?this._unwrappedTextLines:this._textLines;let s;for(s=0;s<i.length;s++){if(t<=i[s].length)return{lineIndex:s,charIndex:t};t-=i[s].length+this.missingNewlineOffset(s,e)}return{lineIndex:s-1,charIndex:i[s-1].length<t?i[s-1].length:t}}toString(){return`#<Text (${this.complexity()}): { "text": "${this.text}", "fontFamily": "${this.fontFamily}" }>`}_getCacheCanvasDimensions(){const t=super._getCacheCanvasDimensions(),e=this.fontSize;return t.width+=e*t.zoomX,t.height+=e*t.zoomY,t}_render(t){const e=this.path;e&&!e.isNotVisible()&&e._render(t),this._setTextStyles(t),this._renderTextLinesBackground(t),this._renderTextDecoration(t,"underline"),this._renderText(t),this._renderTextDecoration(t,"overline"),this._renderTextDecoration(t,"linethrough")}_renderText(t){this.__overlayEditor||(this.paintFirst===n?(this._renderTextStroke(t),this._renderTextFill(t)):(this._renderTextFill(t),this._renderTextStroke(t)))}_setTextStyles(t,e,i){if(t.textBaseline="alphabetic",this.path)switch(this.pathAlign){case s:t.textBaseline="middle";break;case"ascender":t.textBaseline=h;break;case"descender":t.textBaseline=r}t.font=this._getFontDeclaration(e,i)}calcTextWidth(){let t=this.getLineWidth(0);for(let e=1,i=this._textLines.length;e<i;e++){const i=this.getLineWidth(e);i>t&&(t=i)}return t}_renderTextLine(t,e,i,s,n,r){this._renderChars(t,e,i,s,n,r)}_renderTextLinesBackground(t){if(!this.textBackgroundColor&&!this.styleHas("textBackgroundColor"))return;const e=t.fillStyle,i=this._getLeftOffset();let s=this._getTopOffset();for(let e=0,n=this._textLines.length;e<n;e++){const n=this.getHeightOfLine(e);if(!this.textBackgroundColor&&!this.styleHas("textBackgroundColor",e)){s+=n;continue}const r=this._textLines[e].length,h=this._getLineLeftOffset(e);let o,l,a=0,d=0,c=this.getValueOfPropertyAt(e,0,"textBackgroundColor");for(let f=0;f<r;f++){const r=this.__charBounds[e][f];l=this.getValueOfPropertyAt(e,f,"textBackgroundColor"),this.path?(t.save(),t.translate(r.renderLeft,r.renderTop),t.rotate(r.angle),t.fillStyle=l,l&&t.fillRect(-r.width/2,-n/this.lineHeight*(1-this._fontSizeFraction),r.width,n/this.lineHeight),t.restore()):l!==c?(o=i+h+d,"rtl"===this.direction&&(o=this.width-o-a),t.fillStyle=c,c&&t.fillRect(o,s,a,n/this.lineHeight),d=r.left,a=r.width,c=l):a+=r.kernedWidth}l&&!this.path&&(o=i+h+d,"rtl"===this.direction&&(o=this.width-o-a),t.fillStyle=l,t.fillRect(o,s,a,n/this.lineHeight)),s+=n}t.fillStyle=e,this._removeShadow(t)}_measureChar(t,i,s,n){const r=e.getFontCache(i),h=this._getFontDeclaration(i),o=s+t,l=s&&h===this._getFontDeclaration(n),a=i.fontSize/this.CACHE_FONT_SIZE;let d,c,f,g;if(s&&void 0!==r[s]&&(f=r[s]),void 0!==r[t]&&(g=d=r[t]),l&&void 0!==r[o]&&(c=r[o],g=c-f),void 0===d||void 0===f||void 0===c){const e=function(){if(!P){const t=p({width:0,height:0});P=t.getContext("2d")}return P}();this._setTextStyles(e,i,!0),void 0===d&&(g=d=e.measureText(t).width,r[t]=d),void 0===f&&l&&s&&(f=e.measureText(s).width,r[s]=f),l&&void 0===c&&(c=e.measureText(o).width,r[o]=c,g=c-f)}return{width:d*a,kernedWidth:g*a}}getHeightOfChar(t,e){return this.getValueOfPropertyAt(t,e,"fontSize")}measureLine(t){const e=this._measureLine(t);return 0!==this.charSpacing&&(e.width-=this._getWidthOfCharSpacing()),e.width<0&&(e.width=0),e}_measureLine(t){let e,n,r=0;const h=this.pathSide===i,o=this.path,a=this._textLines[t],d=a.length,c=new Array(d);this.__charBounds[t]=c;for(let i=0;i<d;i++){const s=a[i];n=this._getGraphemeBox(s,t,i,e),c[i]=n,r+=n.kernedWidth,e=s}if(c[d]={left:n?n.left+n.width:0,width:0,kernedWidth:0,height:this.fontSize,deltaY:0},o&&o.segmentsInfo){let t=0;const e=o.segmentsInfo[o.segmentsInfo.length-1].length;switch(this.textAlign){case l:t=h?e-r:0;break;case s:t=(e-r)/2;break;case i:t=h?0:e-r}t+=this.pathStartOffset*(h?-1:1);for(let i=h?d-1:0;h?i>=0:i<d;h?i--:i++)n=c[i],t>e?t%=e:t<0&&(t+=e),this._setGraphemeOnPath(t,n),t+=n.kernedWidth}return{width:r,numOfSpaces:0}}_setGraphemeOnPath(t,e){const s=t+e.kernedWidth/2,n=this.path,r=L(n.path,s,n.segmentsInfo);e.renderLeft=r.x-n.pathOffset.x,e.renderTop=r.y-n.pathOffset.y,e.angle=r.angle+(this.pathSide===i?Math.PI:0)}_getGraphemeBox(t,e,i,s,n){const r=this.getCompleteStyleDeclaration(e,i),h=s?this.getCompleteStyleDeclaration(e,i-1):{},o=this._measureChar(t,r,s,h);let l,a=o.kernedWidth,d=o.width;0!==this.charSpacing&&(l=this._getWidthOfCharSpacing(),d+=l,a+=l);const c={width:d,left:0,height:r.fontSize,kernedWidth:a,deltaY:r.deltaY};if(i>0&&!n){const t=this.__charBounds[e][i-1];c.left=t.left+t.width+o.kernedWidth-o.width}return c}getHeightOfLine(t){if(this.__lineHeights[t])return this.__lineHeights[t];let e=this.getHeightOfChar(t,0);for(let i=1,s=this._textLines[t].length;i<s;i++)e=Math.max(this.getHeightOfChar(t,i),e);return this.__lineHeights[t]=e*this.lineHeight*this._fontSizeMult}calcTextHeight(){let t,e=0;for(let i=0,s=this._textLines.length;i<s;i++)t=this.getHeightOfLine(i),e+=i===s-1?t/this.lineHeight:t;return e}_getLeftOffset(){return"ltr"===this.direction?-this.width/2:this.width/2}_getTopOffset(){return-this.height/2}_renderTextCommon(t,e){t.save();let i=0;const s=this._getLeftOffset(),n=this._getTopOffset();for(let r=0,h=this._textLines.length;r<h;r++){const h=this.getHeightOfLine(r),o=h/this.lineHeight,l=this._getLineLeftOffset(r);this._renderTextLine(e,t,this._textLines[r],s+l,n+i+o,r),i+=h}t.restore()}_renderTextFill(t){(this.fill||this.styleHas(o))&&this._renderTextCommon(t,"fillText")}_renderTextStroke(t){(this.stroke&&0!==this.strokeWidth||!this.isEmptyStyles())&&(this.shadow&&!this.shadow.affectStroke&&this._removeShadow(t),t.save(),this._setLineDash(t,this.strokeDashArray),t.beginPath(),this._renderTextCommon(t,"strokeText"),t.closePath(),t.restore())}_renderChars(t,e,s,n,r,h){const o=this.getHeightOfLine(h),a=this.textAlign.includes(k),d=this.path,c=!a&&0===this.charSpacing&&this.isEmptyStyles(h)&&!d,f="ltr"===this.direction,g="ltr"===this.direction?1:-1,u=e.direction;let p,m,_,S,y,T="",L=0;if(e.save(),u!==this.direction&&(e.canvas.setAttribute("dir",f?"ltr":"rtl"),e.direction=f?"ltr":"rtl",e.textAlign=a||f?l:i),r-=o*this._fontSizeFraction/this.lineHeight,c)return this._renderChar(t,e,h,0,s.join(""),n,r),void e.restore();for(let i=0,o=s.length-1;i<=o;i++)S=i===o||this.charSpacing||d,T+=s[i],_=this.__charBounds[h][i],0===L?(n+=g*(_.kernedWidth-_.width),L+=_.width):L+=_.kernedWidth,a&&!S&&this._reSpaceAndTab.test(s[i])&&(S=!0),S||(p=p||this.getCompleteStyleDeclaration(h,i),m=this.getCompleteStyleDeclaration(h,i+1),S=x(p,m,!1)),S&&(d?(e.save(),e.translate(_.renderLeft,_.renderTop),e.rotate(_.angle),this._renderChar(t,e,h,i,T,-L/2,0),e.restore()):(y=n,this._renderChar(t,e,h,i,T,y,r)),T="",p=m,n+=g*L,L=0);e.restore()}_applyPatternGradientTransformText(t){const e=this.width+this.strokeWidth,i=this.height+this.strokeWidth,s=p({width:e,height:i}),n=s.getContext("2d");return s.width=e,s.height=i,n.beginPath(),n.moveTo(0,0),n.lineTo(e,0),n.lineTo(e,i),n.lineTo(0,i),n.closePath(),n.translate(e/2,i/2),n.fillStyle=t.toLive(n),this._applyPatternGradientTransform(n,t),n.fill(),n.createPattern(s,"no-repeat")}handleFiller(t,e,i){let s,n;return D(i)?"percentage"===i.gradientUnits||i.gradientTransform||i.patternTransform?(s=-this.width/2,n=-this.height/2,t.translate(s,n),t[e]=this._applyPatternGradientTransformText(i),{offsetX:s,offsetY:n}):(t[e]=i.toLive(t),this._applyPatternGradientTransform(t,i)):(t[e]=i,{offsetX:0,offsetY:0})}_setStrokeStyles(t,e){let{stroke:i,strokeWidth:s}=e;return t.lineWidth=s,t.lineCap=this.strokeLineCap,t.lineDashOffset=this.strokeDashOffset,t.lineJoin=this.strokeLineJoin,t.miterLimit=this.strokeMiterLimit,this.handleFiller(t,"strokeStyle",i)}_setFillStyles(t,e){let{fill:i}=e;return this.handleFiller(t,"fillStyle",i)}_renderChar(t,e,i,s,n,r,h){const o=this._getStyleDeclaration(i,s),l=this.getCompleteStyleDeclaration(i,s),a="fillText"===t&&l.fill,d="strokeText"===t&&l.stroke&&l.strokeWidth;if(d||a){if(e.save(),e.font=this._getFontDeclaration(l),o.textBackgroundColor&&this._removeShadow(e),o.deltaY&&(h+=o.deltaY),a){const t=this._setFillStyles(e,l);e.fillText(n,r-t.offsetX,h-t.offsetY)}if(d){const t=this._setStrokeStyles(e,l);e.strokeText(n,r-t.offsetX,h-t.offsetY)}e.restore()}}setSuperscript(t,e){this._setScript(t,e,this.superscript)}setSubscript(t,e){this._setScript(t,e,this.subscript)}_setScript(t,e,i){const s=this.get2DCursorLocation(t,!0),n=this.getValueOfPropertyAt(s.lineIndex,s.charIndex,"fontSize"),r=this.getValueOfPropertyAt(s.lineIndex,s.charIndex,"deltaY"),h={fontSize:n*i.size,deltaY:r+n*i.baseline};this.setSelectionStyles(h,t,e)}_getLineLeftOffset(t){const e=this.getLineWidth(t),n=this.width-e,r=this.textAlign,h=this.direction,o=this.isEndOfWrapping(t);let a=0;return r===k||r===F&&!o||r===W&&!o||r===z&&!o?0:(r===s&&(a=n/2),r===i&&(a=n),r===F&&(a=n/2),r===W&&(a=n),"rtl"===h&&(r===i||r===k||r===W?a=0:r===l||r===z?a=-n:r!==s&&r!==F||(a=-n/2)),a)}_clearCache(){this._forceClearCache=!1,this.__lineWidths=[],this.__lineHeights=[],this.__charBounds=[]}getLineWidth(t){if(void 0!==this.__lineWidths[t])return this.__lineWidths[t];const{width:e}=this.measureLine(t);return this.__lineWidths[t]=e,e}_getWidthOfCharSpacing(){return 0!==this.charSpacing?this.fontSize*this.charSpacing/1e3:0}getValueOfPropertyAt(t,e,i){var s;return null!==(s=this._getStyleDeclaration(t,e)[i])&&void 0!==s?s:this[i]}_renderTextDecoration(t,e){if(!this[e]&&!this.styleHas(e))return;let i=this._getTopOffset();const s=this._getLeftOffset(),n=this.path,r=this._getWidthOfCharSpacing(),h="linethrough"===e?.5:"overline"===e?1:0,l=this.offsets[e];for(let a=0,d=this._textLines.length;a<d;a++){const d=this.getHeightOfLine(a);if(!this[e]&&!this.styleHas(e,a)){i+=d;continue}const c=this._textLines[a],f=d/this.lineHeight,g=this._getLineLeftOffset(a);let u=0,p=0,m=this.getValueOfPropertyAt(a,0,e),_=this.getValueOfPropertyAt(a,0,o),x=this.getValueOfPropertyAt(a,0,b),S=m,y=_,T=x;const L=i+f*(1-this._fontSizeFraction);let w=this.getHeightOfChar(a,0),v=this.getValueOfPropertyAt(a,0,"deltaY");for(let i=0,r=c.length;i<r;i++){const r=this.__charBounds[a][i];S=this.getValueOfPropertyAt(a,i,e),y=this.getValueOfPropertyAt(a,i,o),T=this.getValueOfPropertyAt(a,i,b);const d=this.getHeightOfChar(a,i),c=this.getValueOfPropertyAt(a,i,"deltaY");if(n&&S&&y){const e=this.fontSize*T/1e3;t.save(),t.fillStyle=_,t.translate(r.renderLeft,r.renderTop),t.rotate(r.angle),t.fillRect(-r.kernedWidth/2,l*d+c-h*e,r.kernedWidth,e),t.restore()}else if((S!==m||y!==_||d!==w||T!==x||c!==v)&&p>0){const e=this.fontSize*x/1e3;let i=s+g+u;"rtl"===this.direction&&(i=this.width-i-p),m&&_&&x&&(t.fillStyle=_,t.fillRect(i,L+l*w+v-h*e,p,e)),u=r.left,p=r.width,m=S,x=T,_=y,w=d,v=c}else p+=r.kernedWidth}let O=s+g+u;"rtl"===this.direction&&(O=this.width-O-p),t.fillStyle=y;const A=this.fontSize*T/1e3;S&&y&&T&&t.fillRect(O,L+l*w+v-h*A,p-r,A),i+=d}this._removeShadow(t)}_getFontDeclaration(){let{fontFamily:t=this.fontFamily,fontStyle:e=this.fontStyle,fontWeight:i=this.fontWeight,fontSize:s=this.fontSize}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=arguments.length>1?arguments[1]:void 0,r=t.includes("'")||t.includes('"')||t.includes(",")||I.genericFonts.includes(t.toLowerCase())?t:`"${t}"`;return n||t.includes(",")||!(t.toLowerCase().includes("stv")||t.toLowerCase().includes("arabic")||t.toLowerCase().includes("naskh")||t.toLowerCase().includes("kufi"))||(r=`${r}, "Arial Unicode MS", Arial, sans-serif`),[e,i,`${n?this.CACHE_FONT_SIZE:s}px`,r].join(" ")}render(t){this.visible&&(this.canvas&&this.canvas.skipOffscreen&&!this.group&&!this.isOnScreen()||(this._forceClearCache&&this.initDimensions(),super.render(t)))}graphemeSplit(t){return u(t)}_splitTextIntoLines(t){const e=t.split(this._reNewline),i=new Array(e.length),s=["\n"];let n=[];for(let t=0;t<e.length;t++)"rtl"===this.direction||this._containsArabicText(e[t])?(i[t]=_(e[t]),console.log(`🔤 BiDi-aware split line ${t}: "${e[t]}" -> [${i[t].join(", ")}]`)):i[t]=this.graphemeSplit(e[t]),n=n.concat(i[t],s);return n.pop(),{_unwrappedLines:i,lines:e,graphemeText:n,graphemeLines:i}}_containsArabicText(t){return/[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]/.test(t)}toObject(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];return{...super.toObject([...A,...t]),styles:S(this.styles,this.text),...this.path?{path:this.path.toObject()}:{}}}set(t,e){const{textLayoutProperties:i}=this.constructor;super.set(t,e);let s=!1,n=!1;if("object"==typeof t)for(const e in t)"path"===e&&this.setPathInfo(),s=s||i.includes(e),n=n||"path"===e;else s=i.includes(t),n="path"===t;return n&&this.setPathInfo(),s&&this.initialized&&(j(this),this.initDimensions(),this.setCoords()),this}complexity(){return 1}static async fromElement(t,e,n){const r=f(t,I.ATTRIBUTE_NAMES,n),{textAnchor:h=l,textDecoration:o="",dx:d=0,dy:c=0,top:g=0,left:u=0,fontSize:p=a,strokeWidth:m=1,..._}={...e,...r},x=new this((t.textContent||"").replace(/^\s+|\s+$|\n+/g,"").replace(/\s+/g," "),{left:u+d,top:g+c,underline:o.includes("underline"),overline:o.includes("overline"),linethrough:o.includes("line-through"),strokeWidth:0,fontSize:p,..._}),S=x.getScaledHeight()/x.height,y=((x.height+x.strokeWidth)*x.lineHeight-x.height)*S,T=x.getScaledHeight()+y;let L=0;return h===s&&(L=x.getScaledWidth()/2),h===i&&(L=x.getScaledWidth()),x.set({left:x.left-L,top:x.top-(T-x.fontSize*(.07+x._fontSizeFraction))/x.lineHeight,strokeWidth:m}),x}_isFontReady(){if("undefined"==typeof document||!("fonts"in document))return!0;try{return document.fonts.check(`${this.fontSize}px ${this.fontFamily}`)}catch(t){return!0}}_scheduleInitAfterFontLoad(){if("undefined"==typeof document||!("fonts"in document))return;if(this._fontLoadScheduled)return;this._fontLoadScheduled=!0;const t=`${this.fontSize}px ${this.fontFamily}`;document.fonts.load(t).then(()=>{var t;(this._fontLoadScheduled=!1,this.initDimensions(),this.textAlign&&this.textAlign.includes(k))?setTimeout(()=>{var t;this.enlargeSpaces&&this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.requestRenderAll()},10):null===(t=this.canvas)||void 0===t||t.requestRenderAll()}).catch(()=>{this._fontLoadScheduled=!1})}forceTextReinitialization(){console.log("🔄 Force reinitializing text object"),this._clearCache(),this.dirty=!0,this._splitText(),this.initDimensions(),this.textAlign&&this.textAlign.includes(k)&&setTimeout(()=>{var t;this.__charBounds&&this.__charBounds.length>0&&this.enlargeSpaces&&(this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.requestRenderAll())},10)}static fromObject(t){return this._fromObject({...t,styles:y(t.styles||{},t.text)},{extraParam:"text"}).then(t=>{t.initialized=!0,t._clearCache&&t._clearCache(),t.dirty=!0;const e=`${t.fontSize}px ${t.fontFamily}`;return"undefined"!=typeof document&&"fonts"in document&&"Arial"!==t.fontFamily&&"Times New Roman"!==t.fontFamily?document.fonts.load(e).then(()=>{var i;console.log(`🔤 Font loaded for JSON object: ${e}`),t.initialized=!0;if(null===(i=t.fontFamily)||void 0===i?void 0:i.toLowerCase().includes("stv")){console.log("🔤 STV font detected, using enhanced reinitialization"),t._browserWrapCache=null,t._lastDimensionState=null,t._browserWrapInitialized=!1,console.log("🔤 STV font: Cleared all cached states for fresh initialization"),t._usingBrowserWrapping=!0,console.log("🔤 STV font: Forcing browser wrapping flag during JSON load"),t.useOverlayEditing=!0,console.log("🔤 STV font: Enabling overlay editing during JSON load");const e=i=>{t.forceTextReinitialization?t.forceTextReinitialization():t.initDimensions(),t.width<50&&i<3&&(console.log(`🔤 STV font width still ${t.width}px, retrying in ${100*i}ms (attempt ${i+1}/3)`),setTimeout(()=>e(i+1),100*i))};e(0)}else t.forceTextReinitialization?(console.log("🔤 Using Textbox specialized reinitialization"),t.forceTextReinitialization()):t.initDimensions();return t}).catch(()=>(console.warn(`⚠️ Font loading failed for ${e}, proceeding with fallback`),t.initialized=!0,t.forceTextReinitialization?t.forceTextReinitialization():t.initDimensions(),t)):(t.initialized=!0,t.forceTextReinitialization?(console.log("🔤 Using Textbox specialized reinitialization for standard font"),t.forceTextReinitialization()):t.initDimensions(),t)})}}t(I,"textLayoutProperties",O),t(I,"cacheProperties",[...B,...A]),t(I,"ownDefaults",C),t(I,"type","Text"),t(I,"genericFonts",["serif","sans-serif","monospace","cursive","fantasy","system-ui","ui-serif","ui-sans-serif","ui-monospace","ui-rounded","math","emoji","fangsong"]),t(I,"ATTRIBUTE_NAMES",c.concat("x","y","dx","dy","font-family","font-style","font-weight","font-size","letter-spacing","text-decoration","text-anchor")),v(I,[w]),g.setClass(I),g.setSVGClass(I);export{I as FabricText};
1
+ import{defineProperty as t}from"../../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{cache as e}from"../../cache.min.mjs";import{RIGHT as i,CENTER as s,STROKE as n,BOTTOM as r,TOP as h,FILL as o,LEFT as l,DEFAULT_SVG_FONT_SIZE as a}from"../../constants.min.mjs";import{StyledText as d}from"./StyledText.min.mjs";import{SHARED_ATTRIBUTES as c}from"../../parser/attributes.min.mjs";import{parseAttributes as f}from"../../parser/parseAttributes.min.mjs";import{classRegistry as g}from"../../ClassRegistry.min.mjs";import{graphemeSplit as u}from"../../util/lang_string.min.mjs";import{createCanvasElementFor as p}from"../../util/misc/dom.min.mjs";import{layoutText as m}from"../../text/layout.min.mjs";import{segmentGraphemes as _}from"../../text/unicode.min.mjs";import{hasStyleChanged as x,stylesToArray as S,stylesFromArray as y}from"../../util/misc/textStyles.min.mjs";import{getPathSegmentsInfo as T,getPointOnPath as L}from"../../util/path/index.min.mjs";import"../Object/FabricObject.min.mjs";import{TextSVGExportMixin as w}from"./TextSVGExportMixin.min.mjs";import{applyMixins as v}from"../../util/applyMixins.min.mjs";import{textLayoutProperties as O,additionalProps as A,textDefaultValues as C,JUSTIFY as k,JUSTIFY_CENTER as F,JUSTIFY_RIGHT as W,JUSTIFY_LEFT as z,TEXT_DECORATION_THICKNESS as b}from"./constants.min.mjs";import{isFiller as D}from"../../util/typeAssertions.min.mjs";import{getBrowserLines as H,clearBrowserLines as j}from"../../text/browserLines.min.mjs";import{cacheProperties as B}from"../Object/defaultValues.min.mjs";let P;class I extends d{static getDefaults(){return{...super.getDefaults(),...I.ownDefaults}}constructor(e,i){super(),t(this,"__charBounds",[]),Object.assign(this,I.ownDefaults),this.setOptions(i),this.styles||(this.styles={}),this.text=e,this.initialized=!0,this.path&&this.setPathInfo(),this.initDimensions(),this.setCoords()}setPathInfo(){const t=this.path;t&&(t.segmentsInfo=T(t.path))}_splitText(){const t=H(this);if(t&&this.useOverlayEditing)return this._splitTextFromBrowserLines(t);const e=this._splitTextIntoLines(this.text);return this.textLines=e.lines,this._textLines=e.graphemeLines,this._unwrappedTextLines=e._unwrappedLines,this._text=e.graphemeText,e}_splitTextFromBrowserLines(t){const e=[],i=[],s=[];let n=[];for(const r of t){e.push(r.text);const h=this.graphemeSplit(r.text);i.push(h),s.push(h),n=n.concat(h),r!==t[t.length-1]&&n.push("\n")}const r={lines:e,graphemeLines:i,graphemeText:n,_unwrappedLines:s};return this.textLines=r.lines,this._textLines=r.graphemeLines,this._unwrappedTextLines=r._unwrappedLines,this._text=r.graphemeText,r}initDimensions(){if(this._isFontReady()||this.initialized||this._scheduleInitAfterFontLoad(),this.enableAdvancedLayout&&!this.path)return this.initDimensionsAdvanced();this._splitText(),this._clearCache(),this.dirty=!0,this.path?(this.width=this.path.width,this.height=this.path.height):(this.width=this.calcTextWidth()||this.cursorWidth||this.MIN_TEXT_WIDTH,this.height=this.calcTextHeight()),this.textAlign.includes(k)&&(this.__charBounds&&this.__charBounds.length>0?this.enlargeSpaces():(console.warn("⚠️ __charBounds not ready for justify alignment, deferring enlargeSpaces"),setTimeout(()=>{var t;this.__charBounds&&this.__charBounds.length>0&&this.enlargeSpaces&&(console.log("🔧 Applying deferred justify alignment"),this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.requestRenderAll())},0)))}enlargeSpaces(){let t,e,i,s,n,r,h;const o="rtl"===this.direction;for(let l=0,a=this._textLines.length;l<a;l++)if(this.textAlign.includes("justify")&&l!==a-1&&!this.isEndOfWrapping(l)&&(s=0,n=this._textLines[l],e=this.getLineWidth(l),e<this.width&&(h=this.textLines[l].match(this._reSpacesAndTabs))))if(i=h.length,t=(this.width-e)/i,console.log(`🔧 EnlargeSpaces Line ${l}:`),console.log(` Current width: ${e}, Target: ${this.width}`),console.log(` Spaces: ${i}, diffSpace: ${t.toFixed(2)}`),o){for(let t=0;t<n.length;t++)this._reSpaceAndTab.test(n[t]);let e=0;for(let s=0;s<=n.length;s++)if(r=this.__charBounds[l][s],r){this._reSpaceAndTab.test(n[s])&&(r.width+=t,r.kernedWidth+=t,e++);const h=(i-e)*t;r.left+=h}}else for(let e=0;e<=n.length;e++)r=this.__charBounds[l][e],r&&(this._reSpaceAndTab.test(n[e])?(r.width+=t,r.kernedWidth+=t,r.left+=s,s+=t):r.left+=s)}_layoutTextAdvanced(){const t=this._getAdvancedLayoutOptions();return m(t)}_getAdvancedLayoutOptions(){return{text:this.text,width:this.width,height:this.height,wrap:this.wrap||"word",align:this._mapTextAlignToAlign(this.textAlign),ellipsis:this.ellipsis||!1,fontSize:this.fontSize,lineHeight:this.lineHeight,letterSpacing:this.letterSpacing||0,charSpacing:this.charSpacing,direction:"inherit"===this.direction?"ltr":this.direction,fontFamily:this.fontFamily,fontStyle:this.fontStyle,fontWeight:this.fontWeight,verticalAlign:this.verticalAlign||"top"}}_mapTextAlignToAlign(t){switch(t){case"center":case s:return"center";case"right":case i:return"right";case"justify":case k:case z:case W:case F:return"justify";default:return"left"}}initDimensionsAdvanced(){if(!this.enableAdvancedLayout)return this.initDimensions();const t=this._layoutTextAdvanced();this.width=t.totalWidth||this.MIN_TEXT_WIDTH,this.height=t.totalHeight,this._convertLayoutToLegacyFormat(t),this.textAlign.includes(k)&&setTimeout(()=>{var t;this.enlargeSpaces&&(this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.renderAll())},0),this.dirty=!0}_convertLayoutToLegacyFormat(t){this._textLines=t.lines.map(t=>t.graphemes),this.textLines=t.lines.map(t=>t.text),this.__charBounds=t.lines.map(t=>t.bounds.map(t=>({left:t.left,top:t.y,width:t.width,height:t.height,kernedWidth:t.kernedWidth,deltaY:t.deltaY||0}))),t.lines.length>0&&(this._unwrappedTextLines=t.lines.map(t=>t.graphemes))}isEndOfWrapping(t){return t===this._textLines.length-1}missingNewlineOffset(t){return 1}get2DCursorLocation(t,e){const i=e?this._unwrappedTextLines:this._textLines;let s;for(s=0;s<i.length;s++){if(t<=i[s].length)return{lineIndex:s,charIndex:t};t-=i[s].length+this.missingNewlineOffset(s,e)}return{lineIndex:s-1,charIndex:i[s-1].length<t?i[s-1].length:t}}toString(){return`#<Text (${this.complexity()}): { "text": "${this.text}", "fontFamily": "${this.fontFamily}" }>`}_getCacheCanvasDimensions(){const t=super._getCacheCanvasDimensions(),e=this.fontSize;return t.width+=e*t.zoomX,t.height+=e*t.zoomY,t}_render(t){const e=this.path;e&&!e.isNotVisible()&&e._render(t),this._setTextStyles(t),this._renderTextLinesBackground(t),this._renderTextDecoration(t,"underline"),this._renderText(t),this._renderTextDecoration(t,"overline"),this._renderTextDecoration(t,"linethrough")}_renderText(t){this.__overlayEditor||(this.paintFirst===n?(this._renderTextStroke(t),this._renderTextFill(t)):(this._renderTextFill(t),this._renderTextStroke(t)))}_setTextStyles(t,e,i){if(t.textBaseline="alphabetic",this.path)switch(this.pathAlign){case s:t.textBaseline="middle";break;case"ascender":t.textBaseline=h;break;case"descender":t.textBaseline=r}t.font=this._getFontDeclaration(e,i)}calcTextWidth(){let t=this.getLineWidth(0);for(let e=1,i=this._textLines.length;e<i;e++){const i=this.getLineWidth(e);i>t&&(t=i)}return t}_renderTextLine(t,e,i,s,n,r){this._renderChars(t,e,i,s,n,r)}_renderTextLinesBackground(t){if(!this.textBackgroundColor&&!this.styleHas("textBackgroundColor"))return;const e=t.fillStyle,i=this._getLeftOffset();let s=this._getTopOffset();for(let e=0,n=this._textLines.length;e<n;e++){const n=this.getHeightOfLine(e);if(!this.textBackgroundColor&&!this.styleHas("textBackgroundColor",e)){s+=n;continue}const r=this._textLines[e].length,h=this._getLineLeftOffset(e);let o,l,a=0,d=0,c=this.getValueOfPropertyAt(e,0,"textBackgroundColor");for(let f=0;f<r;f++){const r=this.__charBounds[e][f];l=this.getValueOfPropertyAt(e,f,"textBackgroundColor"),this.path?(t.save(),t.translate(r.renderLeft,r.renderTop),t.rotate(r.angle),t.fillStyle=l,l&&t.fillRect(-r.width/2,-n/this.lineHeight*(1-this._fontSizeFraction),r.width,n/this.lineHeight),t.restore()):l!==c?(o=i+h+d,"rtl"===this.direction&&(o=this.width-o-a),t.fillStyle=c,c&&t.fillRect(o,s,a,n/this.lineHeight),d=r.left,a=r.width,c=l):a+=r.kernedWidth}l&&!this.path&&(o=i+h+d,"rtl"===this.direction&&(o=this.width-o-a),t.fillStyle=l,t.fillRect(o,s,a,n/this.lineHeight)),s+=n}t.fillStyle=e,this._removeShadow(t)}_measureChar(t,i,s,n){const r=e.getFontCache(i),h=this._getFontDeclaration(i),o=s+t,l=s&&h===this._getFontDeclaration(n),a=i.fontSize/this.CACHE_FONT_SIZE;let d,c,f,g;if(s&&void 0!==r[s]&&(f=r[s]),void 0!==r[t]&&(g=d=r[t]),l&&void 0!==r[o]&&(c=r[o],g=c-f),void 0===d||void 0===f||void 0===c){const e=function(){if(!P){const t=p({width:0,height:0});P=t.getContext("2d")}return P}();this._setTextStyles(e,i,!0),void 0===d&&(g=d=e.measureText(t).width,r[t]=d),void 0===f&&l&&s&&(f=e.measureText(s).width,r[s]=f),l&&void 0===c&&(c=e.measureText(o).width,r[o]=c,g=c-f)}return{width:d*a,kernedWidth:g*a}}getHeightOfChar(t,e){return this.getValueOfPropertyAt(t,e,"fontSize")}measureLine(t){const e=this._measureLine(t);return 0!==this.charSpacing&&(e.width-=this._getWidthOfCharSpacing()),e.width<0&&(e.width=0),e}_measureLine(t){let e,n,r=0;const h=this.pathSide===i,o=this.path,a=this._textLines[t],d=a.length,c=new Array(d);this.__charBounds[t]=c;for(let i=0;i<d;i++){const s=a[i];n=this._getGraphemeBox(s,t,i,e),c[i]=n,r+=n.kernedWidth,e=s}if(c[d]={left:n?n.left+n.width:0,width:0,kernedWidth:0,height:this.fontSize,deltaY:0},o&&o.segmentsInfo){let t=0;const e=o.segmentsInfo[o.segmentsInfo.length-1].length;switch(this.textAlign){case l:t=h?e-r:0;break;case s:t=(e-r)/2;break;case i:t=h?0:e-r}t+=this.pathStartOffset*(h?-1:1);for(let i=h?d-1:0;h?i>=0:i<d;h?i--:i++)n=c[i],t>e?t%=e:t<0&&(t+=e),this._setGraphemeOnPath(t,n),t+=n.kernedWidth}return{width:r,numOfSpaces:0}}_setGraphemeOnPath(t,e){const s=t+e.kernedWidth/2,n=this.path,r=L(n.path,s,n.segmentsInfo);e.renderLeft=r.x-n.pathOffset.x,e.renderTop=r.y-n.pathOffset.y,e.angle=r.angle+(this.pathSide===i?Math.PI:0)}_getGraphemeBox(t,e,i,s,n){const r=this.getCompleteStyleDeclaration(e,i),h=s?this.getCompleteStyleDeclaration(e,i-1):{},o=this._measureChar(t,r,s,h);let l,a=o.kernedWidth,d=o.width;0!==this.charSpacing&&(l=this._getWidthOfCharSpacing(),d+=l,a+=l);const c={width:d,left:0,height:r.fontSize,kernedWidth:a,deltaY:r.deltaY};if(i>0&&!n){const t=this.__charBounds[e][i-1];c.left=t.left+t.width+o.kernedWidth-o.width}return c}getHeightOfLine(t){if(this.__lineHeights[t])return this.__lineHeights[t];let e=this.getHeightOfChar(t,0);for(let i=1,s=this._textLines[t].length;i<s;i++)e=Math.max(this.getHeightOfChar(t,i),e);return this.__lineHeights[t]=e*this.lineHeight*this._fontSizeMult}calcTextHeight(){let t,e=0;for(let i=0,s=this._textLines.length;i<s;i++)t=this.getHeightOfLine(i),e+=i===s-1?t/this.lineHeight:t;return e}_getLeftOffset(){return"ltr"===this.direction?-this.width/2:this.width/2}_getTopOffset(){return-this.height/2}_renderTextCommon(t,e){t.save();let i=0;const s=this._getLeftOffset(),n=this._getTopOffset();for(let r=0,h=this._textLines.length;r<h;r++){const h=this.getHeightOfLine(r),o=h/this.lineHeight,l=this._getLineLeftOffset(r);this._renderTextLine(e,t,this._textLines[r],s+l,n+i+o,r),i+=h}t.restore()}_renderTextFill(t){(this.fill||this.styleHas(o))&&this._renderTextCommon(t,"fillText")}_renderTextStroke(t){(this.stroke&&0!==this.strokeWidth||!this.isEmptyStyles())&&(this.shadow&&!this.shadow.affectStroke&&this._removeShadow(t),t.save(),this._setLineDash(t,this.strokeDashArray),t.beginPath(),this._renderTextCommon(t,"strokeText"),t.closePath(),t.restore())}_renderChars(t,e,s,n,r,h){const o=this.getHeightOfLine(h),a=this.textAlign.includes(k),d=this.path,c=!a&&0===this.charSpacing&&this.isEmptyStyles(h)&&!d,f="ltr"===this.direction,g="ltr"===this.direction?1:-1,u=e.direction;let p,m,_,S,y,T="",L=0;if(e.save(),u!==this.direction&&(e.canvas.setAttribute("dir",f?"ltr":"rtl"),e.direction=f?"ltr":"rtl",e.textAlign=a||f?l:i),r-=o*this._fontSizeFraction/this.lineHeight,c)return this._renderChar(t,e,h,0,s.join(""),n,r),void e.restore();for(let i=0,o=s.length-1;i<=o;i++)S=i===o||this.charSpacing||d,T+=s[i],_=this.__charBounds[h][i],0===L?(n+=g*(_.kernedWidth-_.width),L+=_.width):L+=_.kernedWidth,a&&!S&&this._reSpaceAndTab.test(s[i])&&(S=!0),S||(p=p||this.getCompleteStyleDeclaration(h,i),m=this.getCompleteStyleDeclaration(h,i+1),S=x(p,m,!1)),S&&(d?(e.save(),e.translate(_.renderLeft,_.renderTop),e.rotate(_.angle),this._renderChar(t,e,h,i,T,-L/2,0),e.restore()):(y=n,this._renderChar(t,e,h,i,T,y,r)),T="",p=m,n+=g*L,L=0);e.restore()}_applyPatternGradientTransformText(t){const e=this.width+this.strokeWidth,i=this.height+this.strokeWidth,s=p({width:e,height:i}),n=s.getContext("2d");return s.width=e,s.height=i,n.beginPath(),n.moveTo(0,0),n.lineTo(e,0),n.lineTo(e,i),n.lineTo(0,i),n.closePath(),n.translate(e/2,i/2),n.fillStyle=t.toLive(n),this._applyPatternGradientTransform(n,t),n.fill(),n.createPattern(s,"no-repeat")}handleFiller(t,e,i){let s,n;return D(i)?"percentage"===i.gradientUnits||i.gradientTransform||i.patternTransform?(s=-this.width/2,n=-this.height/2,t.translate(s,n),t[e]=this._applyPatternGradientTransformText(i),{offsetX:s,offsetY:n}):(t[e]=i.toLive(t),this._applyPatternGradientTransform(t,i)):(t[e]=i,{offsetX:0,offsetY:0})}_setStrokeStyles(t,e){let{stroke:i,strokeWidth:s}=e;return t.lineWidth=s,t.lineCap=this.strokeLineCap,t.lineDashOffset=this.strokeDashOffset,t.lineJoin=this.strokeLineJoin,t.miterLimit=this.strokeMiterLimit,this.handleFiller(t,"strokeStyle",i)}_setFillStyles(t,e){let{fill:i}=e;return this.handleFiller(t,"fillStyle",i)}_renderChar(t,e,i,s,n,r,h){const o=this._getStyleDeclaration(i,s),l=this.getCompleteStyleDeclaration(i,s),a="fillText"===t&&l.fill,d="strokeText"===t&&l.stroke&&l.strokeWidth;if(d||a){if(e.save(),e.font=this._getFontDeclaration(l),o.textBackgroundColor&&this._removeShadow(e),o.deltaY&&(h+=o.deltaY),a){const t=this._setFillStyles(e,l);e.fillText(n,r-t.offsetX,h-t.offsetY)}if(d){const t=this._setStrokeStyles(e,l);e.strokeText(n,r-t.offsetX,h-t.offsetY)}e.restore()}}setSuperscript(t,e){this._setScript(t,e,this.superscript)}setSubscript(t,e){this._setScript(t,e,this.subscript)}_setScript(t,e,i){const s=this.get2DCursorLocation(t,!0),n=this.getValueOfPropertyAt(s.lineIndex,s.charIndex,"fontSize"),r=this.getValueOfPropertyAt(s.lineIndex,s.charIndex,"deltaY"),h={fontSize:n*i.size,deltaY:r+n*i.baseline};this.setSelectionStyles(h,t,e)}_getLineLeftOffset(t){const e=this.getLineWidth(t),n=this.width-e,r=this.textAlign,h=this.direction,o=this.isEndOfWrapping(t);let a=0;return r===k||r===F&&!o||r===W&&!o||r===z&&!o?0:(r===s&&(a=n/2),r===i&&(a=n),r===F&&(a=n/2),r===W&&(a=n),"rtl"===h&&(r===i||r===k||r===W?a=0:r===l||r===z?a=-n:r!==s&&r!==F||(a=-n/2)),a)}_clearCache(){this._forceClearCache=!1,this.__lineWidths=[],this.__lineHeights=[],this.__charBounds=[]}getLineWidth(t){if(void 0!==this.__lineWidths[t])return this.__lineWidths[t];const{width:e}=this.measureLine(t);return this.__lineWidths[t]=e,e}_getWidthOfCharSpacing(){return 0!==this.charSpacing?this.fontSize*this.charSpacing/1e3:0}getValueOfPropertyAt(t,e,i){var s;return null!==(s=this._getStyleDeclaration(t,e)[i])&&void 0!==s?s:this[i]}_renderTextDecoration(t,e){if(!this[e]&&!this.styleHas(e))return;let i=this._getTopOffset();const s=this._getLeftOffset(),n=this.path,r=this._getWidthOfCharSpacing(),h="linethrough"===e?.5:"overline"===e?1:0,l=this.offsets[e];for(let a=0,d=this._textLines.length;a<d;a++){const d=this.getHeightOfLine(a);if(!this[e]&&!this.styleHas(e,a)){i+=d;continue}const c=this._textLines[a],f=d/this.lineHeight,g=this._getLineLeftOffset(a);let u=0,p=0,m=this.getValueOfPropertyAt(a,0,e),_=this.getValueOfPropertyAt(a,0,o),x=this.getValueOfPropertyAt(a,0,b),S=m,y=_,T=x;const L=i+f*(1-this._fontSizeFraction);let w=this.getHeightOfChar(a,0),v=this.getValueOfPropertyAt(a,0,"deltaY");for(let i=0,r=c.length;i<r;i++){const r=this.__charBounds[a][i];S=this.getValueOfPropertyAt(a,i,e),y=this.getValueOfPropertyAt(a,i,o),T=this.getValueOfPropertyAt(a,i,b);const d=this.getHeightOfChar(a,i),c=this.getValueOfPropertyAt(a,i,"deltaY");if(n&&S&&y){const e=this.fontSize*T/1e3;t.save(),t.fillStyle=_,t.translate(r.renderLeft,r.renderTop),t.rotate(r.angle),t.fillRect(-r.kernedWidth/2,l*d+c-h*e,r.kernedWidth,e),t.restore()}else if((S!==m||y!==_||d!==w||T!==x||c!==v)&&p>0){const e=this.fontSize*x/1e3;let i=s+g+u;"rtl"===this.direction&&(i=this.width-i-p),m&&_&&x&&(t.fillStyle=_,t.fillRect(i,L+l*w+v-h*e,p,e)),u=r.left,p=r.width,m=S,x=T,_=y,w=d,v=c}else p+=r.kernedWidth}let O=s+g+u;"rtl"===this.direction&&(O=this.width-O-p),t.fillStyle=y;const A=this.fontSize*T/1e3;S&&y&&T&&t.fillRect(O,L+l*w+v-h*A,p-r,A),i+=d}this._removeShadow(t)}_getFontDeclaration(){let{fontFamily:t=this.fontFamily,fontStyle:e=this.fontStyle,fontWeight:i=this.fontWeight,fontSize:s=this.fontSize}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=arguments.length>1?arguments[1]:void 0,r=t.includes("'")||t.includes('"')||t.includes(",")||I.genericFonts.includes(t.toLowerCase())?t:`"${t}"`;return n||t.includes(",")||!(t.toLowerCase().includes("stv")||t.toLowerCase().includes("arabic")||t.toLowerCase().includes("naskh")||t.toLowerCase().includes("kufi"))||(r=`${r}, "Arial Unicode MS", Arial, sans-serif`),[e,i,`${n?this.CACHE_FONT_SIZE:s}px`,r].join(" ")}render(t){this.visible&&(this.canvas&&this.canvas.skipOffscreen&&!this.group&&!this.isOnScreen()||(this._forceClearCache&&this.initDimensions(),super.render(t)))}graphemeSplit(t){return u(t)}_splitTextIntoLines(t){const e=t.split(this._reNewline),i=new Array(e.length),s=["\n"];let n=[];for(let t=0;t<e.length;t++)"rtl"===this.direction||this._containsArabicText(e[t])?(i[t]=_(e[t]),console.log(`🔤 BiDi-aware split line ${t}: "${e[t]}" -> [${i[t].join(", ")}]`)):i[t]=this.graphemeSplit(e[t]),n=n.concat(i[t],s);return n.pop(),{_unwrappedLines:i,lines:e,graphemeText:n,graphemeLines:i}}_containsArabicText(t){return/[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]/.test(t)}toObject(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];return{...super.toObject([...A,...t]),styles:S(this.styles,this.text),...this.path?{path:this.path.toObject()}:{}}}set(t,e){const{textLayoutProperties:i}=this.constructor;super.set(t,e);let s=!1,n=!1;if("object"==typeof t)for(const e in t)"path"===e&&this.setPathInfo(),s=s||i.includes(e),n=n||"path"===e;else s=i.includes(t),n="path"===t;return n&&this.setPathInfo(),s&&this.initialized&&(j(this),this.initDimensions(),this.setCoords()),this}complexity(){return 1}static async fromElement(t,e,n){const r=f(t,I.ATTRIBUTE_NAMES,n),{textAnchor:h=l,textDecoration:o="",dx:d=0,dy:c=0,top:g=0,left:u=0,fontSize:p=a,strokeWidth:m=1,..._}={...e,...r},x=new this((t.textContent||"").replace(/^\s+|\s+$|\n+/g,"").replace(/\s+/g," "),{left:u+d,top:g+c,underline:o.includes("underline"),overline:o.includes("overline"),linethrough:o.includes("line-through"),strokeWidth:0,fontSize:p,..._}),S=x.getScaledHeight()/x.height,y=((x.height+x.strokeWidth)*x.lineHeight-x.height)*S,T=x.getScaledHeight()+y;let L=0;return h===s&&(L=x.getScaledWidth()/2),h===i&&(L=x.getScaledWidth()),x.set({left:x.left-L,top:x.top-(T-x.fontSize*(.07+x._fontSizeFraction))/x.lineHeight,strokeWidth:m}),x}_isFontReady(){if("undefined"==typeof document||!("fonts"in document))return!0;try{return document.fonts.check(`${this.fontSize}px ${this.fontFamily}`)}catch(t){return!0}}_scheduleInitAfterFontLoad(){if("undefined"==typeof document||!("fonts"in document))return;if(this._fontLoadScheduled)return;this._fontLoadScheduled=!0;const t=`${this.fontSize}px ${this.fontFamily}`;document.fonts.load(t).then(()=>{var t;(this._fontLoadScheduled=!1,this.initDimensions(),this.textAlign&&this.textAlign.includes(k))?setTimeout(()=>{var t;this.enlargeSpaces&&this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.requestRenderAll()},10):null===(t=this.canvas)||void 0===t||t.requestRenderAll()}).catch(()=>{this._fontLoadScheduled=!1})}forceTextReinitialization(){console.log("🔄 Force reinitializing text object"),this._clearCache(),this.dirty=!0,this._splitText(),this.initDimensions(),this.textAlign&&this.textAlign.includes(k)&&setTimeout(()=>{var t;this.__charBounds&&this.__charBounds.length>0&&this.enlargeSpaces&&(this.enlargeSpaces(),null===(t=this.canvas)||void 0===t||t.requestRenderAll())},10)}static fromObject(t){return this._fromObject({...t,styles:y(t.styles||{},t.text)},{extraParam:"text"}).then(t=>{t.initialized=!0,t._clearCache&&t._clearCache(),t.dirty=!0;const e=`${t.fontSize}px ${t.fontFamily}`;return"undefined"!=typeof document&&"fonts"in document&&"Arial"!==t.fontFamily&&"Times New Roman"!==t.fontFamily?document.fonts.load(e).then(()=>{var i;console.log(`🔤 Font loaded for JSON object: ${e}`),t.initialized=!0;if(null===(i=t.fontFamily)||void 0===i?void 0:i.toLowerCase().includes("stv")){console.log("🔤 STV font detected, using enhanced reinitialization"),t._browserWrapCache=null,t._lastDimensionState=null,t._browserWrapInitialized=!1,console.log("🔤 STV font: Cleared all cached states for fresh initialization"),t._usingBrowserWrapping=!0,console.log("🔤 STV font: Forcing browser wrapping flag during JSON load"),t.useOverlayEditing=!0,console.log("🔤 STV font: Enabling overlay editing during JSON load");const e=i=>{t.forceTextReinitialization?t.forceTextReinitialization():t.initDimensions(),t.width<50&&i<3&&(console.log(`🔤 STV font width still ${t.width}px, retrying in ${100*i}ms (attempt ${i+1}/3)`),setTimeout(()=>e(i+1),100*i))};e(0)}else t.forceTextReinitialization?(console.log("🔤 Using Textbox specialized reinitialization"),t.forceTextReinitialization()):t.initDimensions();return t}).catch(()=>(console.warn(`⚠️ Font loading failed for ${e}, proceeding with fallback`),t.initialized=!0,t.forceTextReinitialization?t.forceTextReinitialization():t.initDimensions(),t)):(t.initialized=!0,t.forceTextReinitialization?(console.log("🔤 Using Textbox specialized reinitialization for standard font"),t.forceTextReinitialization()):t.initDimensions(),t)})}}t(I,"textLayoutProperties",O),t(I,"cacheProperties",[...B,...A]),t(I,"ownDefaults",C),t(I,"type","Text"),t(I,"genericFonts",["serif","sans-serif","monospace","cursive","fantasy","system-ui","ui-serif","ui-sans-serif","ui-monospace","ui-rounded","math","emoji","fangsong"]),t(I,"ATTRIBUTE_NAMES",c.concat("x","y","dx","dy","font-family","font-style","font-weight","font-size","letter-spacing","text-decoration","text-anchor")),v(I,[w]),g.setClass(I),g.setSVGClass(I);export{I as FabricText};
2
2
  //# sourceMappingURL=Text.min.mjs.map