fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
3 lines • 5.22 kB
JavaScript
import{config as e}from"../../config.min.mjs";import{FILL as t,STROKE as n}from"../../constants.min.mjs";import{Point as r}from"../../Point.min.mjs";import{radiansToDegrees as i}from"../../util/misc/radiansDegreesConversion.min.mjs";import{createRotateMatrix as a}from"../../util/misc/matrix.min.mjs";import{toFixed as o}from"../../util/misc/toFixed.min.mjs";import{matrixToSVG as s}from"../../util/misc/svgExport.min.mjs";import{escapeXml as c}from"../../util/lang_string.min.mjs";import{getSafeSvgStyleNumber as l,getSafeSvgStyleToken as u,isSafeSvgStyleValue as d}from"../../util/internals/svgExportCheck.min.mjs";import{colorPropToSVG as f,createSVGRect as p}from"../../util/misc/svgParsing.min.mjs";import{FabricObjectSVGExportMixin as m}from"../Object/FabricObjectSVGExportMixin.min.mjs";import{JUSTIFY as h,TEXT_DECORATION_COLOR as g}from"./constants.min.mjs";import{hasStyleChanged as _}from"../../util/misc/textStyles.min.mjs";const v=/ +/g,y=/"/g;function b(e,t,n,r,i){return`\t\t${p(e,{left:t,top:n,width:r,height:i})}\n`}var x=class extends m{_toSVG(){let e=this._getSVGLeftTopOffsets(),t=this._getSVGTextAndBg(e.textTop,e.textLeft);return this._wrapSVGTextAndBg(t)}toSVG(e){let t=this._createBaseSVGMarkup(this._toSVG(),{reviver:e,noStyle:!0,withShadow:!0}),n=this.path;return n?t+n._createBaseSVGMarkup(n._toSVG(),{reviver:e,withShadow:!0,additionalTransform:s(this.calcOwnMatrix())}):t}_getSVGLeftTopOffsets(){return{textLeft:-this.width/2,textTop:-this.height/2,lineTop:this.getHeightOfLine(0)}}_wrapSVGTextAndBg({textBgRects:e,textSpans:t}){let n=this.getSvgTextDecoration(this);return[e.join(``),` <text xml:space="preserve" `,`font-family="${c(this.fontFamily.replace(y,`'`))}" `,`font-size="${c(this.fontSize)}" `,this.fontStyle?`font-style="${c(this.fontStyle)}" `:``,this.fontWeight?`font-weight="${c(this.fontWeight)}" `:``,n?`text-decoration="${n}" `:``,this.direction===`rtl`?`direction="rtl" `:``,`style="`,this.getSvgStyles(!0),`"`,this.addPaintOrder(),` >`,t.join(``),`</text>
`]}_getSVGTextAndBg(e,t){let n=[],r=[],i,a=e;this.backgroundColor&&r.push(b(this.backgroundColor,-this.width/2,-this.height/2,this.width,this.height));for(let e=0,o=this._textLines.length;e<o;e++)i=this._getLineLeftOffset(e),this.direction===`rtl`&&(i+=this.width),(this.textBackgroundColor||this.styleHas(`textBackgroundColor`,e))&&this._setSVGTextLineBg(r,e,t+i,a),this._setSVGTextLineText(n,e,t+i,a),a+=this.getHeightOfLine(e);return{textSpans:n,textBgRects:r}}_createTextCharSpan(t,n,s,l,u){let d=e.NUM_FRACTION_DIGITS,f=this.getSvgSpanStyles(n,t!==t.trim()||!!t.match(v)),p=f?`style="${f}"`:``,m=n.deltaY,h=m?` dy="${o(m,d)}" `:``,{angle:g,renderLeft:_,renderTop:y,width:b}=u,x=``;if(_!==void 0){let e=b/2;g&&(x=` rotate="${o(i(g),d)}"`);let t=a({angle:i(g)});t[4]=_,t[5]=y;let n=new r(-e,0).transform(t);s=n.x,l=n.y}return`<tspan x="${o(s,d)}" y="${o(l,d)}" ${h}${x}${p}>${c(t)}</tspan>`}_setSVGTextLineText(e,t,n,r){let i=this.getHeightOfLine(t),a=this.textAlign.includes(h),o=this._textLines[t],s,c,l,u,d,f=``,p=0;r+=i*(1-this._fontSizeFraction)/this.lineHeight;for(let i=0,m=o.length-1;i<=m;i++)d=i===m||this.charSpacing||this.path,f+=o[i],l=this.__charBounds[t][i],p===0?(n+=l.kernedWidth-l.width,p+=l.width):p+=l.kernedWidth,a&&!d&&this._reSpaceAndTab.test(o[i])&&(d=!0),d||(s=s||this.getCompleteStyleDeclaration(t,i),c=this.getCompleteStyleDeclaration(t,i+1),d=_(s,c,!0)),d&&(u=this._getStyleDeclaration(t,i),e.push(this._createTextCharSpan(f,u,n,r,l)),f=``,s=c,this.direction===`rtl`?n-=p:n+=p,p=0)}_setSVGTextLineBg(e,t,n,r){let i=this._textLines[t],a=this.getHeightOfLine(t)/this.lineHeight,o,s=0,c=0,l=this.getValueOfPropertyAt(t,0,`textBackgroundColor`);for(let u=0;u<i.length;u++){let{left:i,width:d,kernedWidth:f}=this.__charBounds[t][u];o=this.getValueOfPropertyAt(t,u,`textBackgroundColor`),o===l?s+=f:(l&&e.push(b(l,n+c,r,s,a)),c=i,s=d,l=o)}o&&e.push(b(l,n+c,r,s,a))}getSvgStyles(t){let n=d(this.textDecorationColor)?` text-decoration-color: ${c(this[g])};`:``;return`${super.getSvgStyles(t)} text-decoration-thickness: ${o(this.textDecorationThickness*this.getObjectScaling().y/10,e.NUM_FRACTION_DIGITS)}%;${n} white-space: pre;`}getSvgSpanStyles(r,i){let{fontFamily:a,strokeWidth:s,stroke:d,fill:p,fontSize:m,fontStyle:h,fontWeight:g,textDecorationThickness:_,textDecorationColor:v,linethrough:y,overline:b,underline:x}=r,S=this.getSvgTextDecoration({underline:x==null?this.underline:x,overline:b==null?this.overline:b,linethrough:y==null?this.linethrough:y}),C=_||this.textDecorationThickness,w=v||this.textDecorationColor,T=l(s),E=u(a),D=l(m),O=u(h),k=l(g)||u(g),A=u(w);return[d?f(n,d):``,T?`stroke-width: ${c(T)}; `:``,E?`font-family: ${E.includes(`'`)||E.includes(`"`)?c(E):`'${c(E)}'`}; `:``,D?`font-size: ${c(D)}px; `:``,O?`font-style: ${c(O)}; `:``,k?`font-weight: ${c(k)}; `:``,S?`text-decoration: ${S}; text-decoration-thickness: ${o(C*this.getObjectScaling().y/10,e.NUM_FRACTION_DIGITS)}%;${A?` text-decoration-color: ${c(A)};`:``} `:``,p?f(t,p):``,i?`white-space: pre; `:``].join(``)}getSvgTextDecoration(e){return[`overline`,`underline`,`line-through`].filter(t=>e[t.replace(`-`,``)]).join(` `)}};export{x as TextSVGExportMixin};
//# sourceMappingURL=TextSVGExportMixin.min.mjs.map