modern-text 0.1.3 → 0.1.5
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.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +229 -213
- package/package.json +1 -1
- package/types/render-text.d.ts +5 -0
- package/types/types.d.ts +13 -10
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class y{get left(){return this.x}get top(){return this.y}get right(){return this.x+this.width}get bottom(){return this.y+this.height}constructor({x:t=0,y:h=0,width:s=0,height:n=0}={}){this.x=t,this.y=h,this.width=s,this.height=n}static from(...t){const h=t[0],s=t.slice(1).reduce((n,a)=>(n.x=Math.min(n.x,a.x),n.y=Math.min(n.y,a.y),n.right=Math.max(n.right,a.right),n.bottom=Math.max(n.bottom,a.bottom),n),{x:h.x,y:h.y,right:h.right,bottom:h.bottom});return new y({x:s.x,y:s.y,width:s.right-s.x,height:s.bottom-s.y})}move(t,h){return this.x+=t,this.y+=h,this}clone(){return new y({x:this.x,y:this.y,width:this.width,height:this.height})}}class H{constructor({content:t="",style:h,parent:s,contentBox:n=new y,inlineBox:a=new y,glyphBox:d=new y,centerX:f=0,baseline:w=0}={}){switch(this.content=t,this.style=h,this.parent=s,this.contentBox=n,this.inlineBox=a,this.glyphBox=d,this.centerX=f,this.baseline=w,this.getComputedStyle().textTransform){case"uppercase":this.content=this.content.toUpperCase();break;case"lowercase":this.content=this.content.toLowerCase();break}}getComputedStyle(){var t;return{...(t=this.parent)==null?void 0:t.getComputedStyle(),...this.style}}clone(t){return new H({content:this.content,style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),inlineBox:this.inlineBox.clone(),glyphBox:this.glyphBox.clone(),centerX:this.centerX,baseline:this.baseline,...t})}}class M{constructor({style:t,parent:h,contentBox:s=new y,lineBox:n=new y,glyphBox:a=new y,baseline:d=0,xHeight:f=0,maxCharWidth:w=0,fragments:m=[]}={}){this.style=t,this.parent=h,this.contentBox=s,this.lineBox=n,this.glyphBox=a,this.baseline=d,this.xHeight=f,this.maxCharWidth=w,this.fragments=m}addFragment(t){return this.fragments.push(new H({...t,parent:this})),this}getComputedStyle(){return{...this.parent,...this.style}}clone(t){return new M({style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),lineBox:this.lineBox.clone(),glyphBox:this.glyphBox.clone(),baseline:this.baseline,xHeight:this.xHeight,maxCharWidth:this.maxCharWidth,fragments:this.fragments.map(h=>h.clone()),...t})}}function D(c,t){const h=[];if(typeof c=="string")h.push(new M({parent:t}).addFragment({content:c}));else{c=Array.isArray(c)?c:[c];for(const s of c)if(typeof s=="string")h.push(new M({parent:t}).addFragment({content:s}));else if(Array.isArray(s)){const n=new M({parent:t});s.forEach(a=>{if(typeof a=="string")n.addFragment({content:a});else{const{content:d,...f}=a;n.addFragment({content:d,style:f})}}),h.push(n)}else if("fragments"in s){const{fragments:n,...a}=s,d=new M({style:a,parent:t});n.forEach(f=>{const{content:w,...m}=f;d.addFragment({content:w,style:m})}),h.push(d)}else if("content"in s){const{content:n,...a}=s;h.push(new M({style:a,parent:t}).addFragment({content:n}))}}return h}const Y="OffscreenCanvas"in globalThis;let L;function N(){return L??(L=Y?new OffscreenCanvas(1,1):document.createElement("canvas"))}function T(c,t){switch(t.shadowColor&&(c.shadowColor=t.shadowColor),t.shadowOffsetX!==void 0&&(c.shadowOffsetX=t.shadowOffsetX),t.shadowOffsetY!==void 0&&(c.shadowOffsetY=t.shadowOffsetY),t.shadowBlur!==void 0&&(c.shadowBlur=t.shadowBlur),t.textStrokeColor&&(c.strokeStyle=t.textStrokeColor),t.textStrokeWidth!==void 0&&(c.lineWidth=t.textStrokeWidth),t.color&&(c.fillStyle=t.color),t.direction&&(c.direction=t.direction),t.textAlign&&(c.textAlign=t.textAlign),t.fontKerning&&(c.fontKerning=t.fontKerning),t.verticalAlign){case"baseline":c.textBaseline="alphabetic";break;case"top":case"middle":case"bottom":c.textBaseline=t.verticalAlign;break}(t.fontStyle||t.fontWeight!==void 0||t.fontSize!==void 0||t.fontFamily)&&(c.font=[t.fontStyle||"normal",t.fontWeight||"normal",`${t.fontSize||14}px`,t.fontFamily||"sans-serif"].join(" "))}function E(c,t){const h=N().getContext("2d");return T(h,t),h.measureText(c)}const K=/[\s\n\t\u200B\u200C\u200D\u200E\u200F.,?!:;"'(){}\[\]<>\/\\|~#\$%\*\+=&^,。?!:;“”‘’()【】《》……——]/;function j(c,t,h){const s=[],n=c.slice();let a,d;for(;a=n.shift();){const f=a.fragments.slice();let w=0;const m=[];for(;d=f.shift();){const p=d.getComputedStyle();let S="",l=!1,o=0,i="";for(const B of d.content){if(a.maxCharWidth=Math.max(a.maxCharWidth,E(B,p).width),i+=B,K.test(d.content[++o]))continue;let r,x;switch(p.writingMode){case"vertical-lr":case"vertical-rl":r=h,x=i.length*p.fontSize;break;case"horizontal-tb":default:r=t,x=E(i,p).width;break}x+=i.length*p.letterSpacing;const u=/^[\r\n]$/.test(i);if(u||p.textWrap==="wrap"&&r&&w+x>r){let e=u?S.length+1:S.length;!w&&!e&&(S+=i,e+=i.length),S.length&&m.push(d.clone({content:S})),m.length&&(s.push(a.clone({fragments:m.slice()})),m.length=0);const b=d.content.substring(e);(b.length||f.length)&&n.unshift(a.clone({maxCharWidth:0,fragments:(b.length?[d.clone({content:b})]:[]).concat(f.slice())})),f.length=0,l=!0;break}else w+=x;S+=i,i=""}l||m.push(d.clone())}m.length&&s.push(a.clone({fragments:m}))}return s}function G(c){return{width:0,height:0,color:null,backgroundColor:null,fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",verticalAlign:"baseline",textTransform:"none",textDecoration:null,textStrokeWidth:0,textStrokeColor:null,direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:null,shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0,writingMode:"horizontal-tb",...c}}function R(c){const{content:t}=c,{width:h,height:s,...n}=G(c.style);let a=D(t,n);a=j(a,h,s);const d=a.reduce((o,i)=>o+i.maxCharWidth,0);let f=0,w=0;a.forEach(o=>{const i=[];let B=null,r=f,x=w;o.fragments.forEach((e,b)=>{const g=e.getComputedStyle();switch(g.writingMode){case"vertical-rl":r=d-r;case"vertical-lr":{b||(x=0);const C=e.content.length,k=o.maxCharWidth,v=k*g.lineHeight,A=C*g.fontSize+(C-1)*g.letterSpacing;e.contentBox.x=r+(v-k)/2,e.contentBox.y=x,e.contentBox.width=k,e.contentBox.height=A,e.inlineBox.x=r,e.inlineBox.y=x,e.inlineBox.width=v,e.inlineBox.height=A,e.glyphBox.x=r+(v-k)/2,e.glyphBox.y=x,e.glyphBox.width=k,e.glyphBox.height=A,e.baseline=0,e.centerX=r+v/2,x+=A;break}case"horizontal-tb":{b||(r=0);const{fontBoundingBoxAscent:C,fontBoundingBoxDescent:k,actualBoundingBoxAscent:v,actualBoundingBoxDescent:A,actualBoundingBoxLeft:O,actualBoundingBoxRight:$,width:z}=E(e.content,{...g,textAlign:"center",verticalAlign:"baseline"}),F=g.fontSize,X=F*g.lineHeight,P=x+(X-(C+k))/2+C;e.contentBox.x=r,e.contentBox.y=x+(X-F)/2,e.contentBox.width=z,e.contentBox.height=F,e.inlineBox.x=r,e.inlineBox.y=x,e.inlineBox.width=z,e.inlineBox.height=X,e.glyphBox.x=r,e.glyphBox.y=P-v,e.glyphBox.width=O+$,e.glyphBox.height=v+A,e.baseline=P,e.centerX=r+O,i.push(e.contentBox),o.contentBox=y.from(...i),o.contentBox.height<e.contentBox.height&&(B=e),r+=z;break}}}),o.lineBox=y.from(...o.fragments.map(e=>e.inlineBox)),f+=o.lineBox.width,w+=o.lineBox.height;const u=E("x",{...(B??o).getComputedStyle(),textAlign:"left",verticalAlign:"baseline"});o.xHeight=u.actualBoundingBoxAscent,o.baseline=o.lineBox.y+(o.lineBox.height-(u.fontBoundingBoxAscent+u.fontBoundingBoxDescent))/2+u.fontBoundingBoxAscent});const m=y.from(...a.map(o=>o.lineBox),new y({width:h,height:s})),{width:p,height:S}=m;a.forEach(o=>{o.contentBox=y.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=y.from(...o.fragments.map(i=>i.glyphBox)),o.fragments.forEach(i=>{const B=i.getComputedStyle(),r=i.inlineBox.x,x=i.inlineBox.y;let u=r,e=x;switch(B.writingMode){case"vertical-rl":case"vertical-lr":switch(B.textAlign){case"end":case"right":e+=S-o.contentBox.height;break;case"center":e+=(S-o.contentBox.height)/2;break}break;case"horizontal-tb":{switch(B.textAlign){case"end":case"right":u+=p-o.contentBox.width;break;case"center":u+=(p-o.contentBox.width)/2;break}switch(B.verticalAlign){case"top":e+=o.lineBox.y-i.inlineBox.y;break;case"middle":e=o.baseline-o.xHeight/2-i.inlineBox.height/2;break;case"bottom":e+=o.lineBox.bottom-i.inlineBox.bottom;break;case"sub":e+=o.baseline-i.glyphBox.bottom;break;case"super":e+=o.baseline-i.glyphBox.y;break;case"text-top":e+=o.glyphBox.y-i.inlineBox.y;break;case"text-bottom":e+=o.glyphBox.bottom-i.inlineBox.bottom;break;case"baseline":default:i.inlineBox.height<o.lineBox.height&&(e+=o.baseline-i.baseline);break}break}}const b=u-r,g=e-x;i.inlineBox.move(b,g),i.contentBox.move(b,g),i.glyphBox.move(b,g),i.baseline+=g,i.centerX+=b}),o.contentBox=y.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=y.from(...o.fragments.map(i=>i.glyphBox))});const l=y.from(...a.map(o=>o.contentBox));return{box:m,contentBox:l,viewBox:y.from(m,l),glyphBox:y.from(...a.map(o=>o.glyphBox)),paragraphs:a}}function W(c,t,h){if(typeof t=="string"&&t.startsWith("linear-gradient")){const{x0:s,y0:n,x1:a,y1:d,stops:f}=U(t,h.left,h.top,h.width,h.height),w=c.createLinearGradient(s,n,a,d);return f.forEach(m=>w.addColorStop(m.offset,m.color)),w}return t}function U(c,t,h,s,n){var o;const a=((o=c.match(/linear-gradient\((.+)\)$/))==null?void 0:o[1])??"",d=a.split(",")[0],f=d.includes("deg")?d:"0deg",w=a.replace(f,"").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi),p=(Number(f.replace("deg",""))||0)*Math.PI/180,S=s*Math.sin(p),l=n*Math.cos(p);return{x0:t+s/2-S,y0:h+n/2+l,x1:t+s/2+S,y1:h+n/2-l,stops:Array.from(w).map(i=>{let B=i[2];return B.startsWith("(")?B=B.split(",").length>3?`rgba${B}`:`rgb${B}`:B=`#${B}`,{offset:Number(i[3].replace("%",""))/100,color:B}})}}function V(c){const{view:t=document.createElement("canvas"),style:h,pixelRatio:s=1}=c,n={...h},{box:a,viewBox:d,paragraphs:f}=R(c),{x:w,y:m,width:p,height:S}=d,l=t.getContext("2d"),o=-w+p,i=-m+S;t.style.width=`${o}px`,t.style.height=`${i}px`,t.dataset.width=String(a.width),t.dataset.height=String(a.height),t.dataset.pixelRatio=String(s),t.width=Math.max(1,Math.floor(o*s)),t.height=Math.max(1,Math.floor(i*s)),l.scale(s,s),l.clearRect(0,0,t.width,t.height),l.translate(-w,-m);const B=new y({width:p,height:S});return n!=null&&n.color&&(n.color=W(l,n.color,B)),n!=null&&n.backgroundColor&&(n.backgroundColor=W(l,n.backgroundColor,B)),n!=null&&n.textStrokeColor&&(n.textStrokeColor=W(l,n.textStrokeColor,B)),n!=null&&n.backgroundColor&&(l.fillStyle=n.backgroundColor,l.fillRect(0,0,t.width,t.height)),f.forEach(r=>{var x,u,e,b;(x=r.style)!=null&&x.color&&(r.style.color=W(l,r.style.color,r.contentBox)),(u=r.style)!=null&&u.backgroundColor&&(r.style.backgroundColor=W(l,r.style.backgroundColor,r.contentBox)),(e=r.style)!=null&&e.textStrokeColor&&(r.style.textStrokeColor=W(l,r.style.textStrokeColor,r.contentBox)),(b=r.style)!=null&&b.backgroundColor&&(l.fillStyle=r.style.backgroundColor,l.fillRect(r.lineBox.x,r.lineBox.y,r.lineBox.width,r.lineBox.height)),r.fragments.forEach(g=>{var C,k,v,A;(C=g.style)!=null&&C.color&&(g.style.color=W(l,g.style.color,g.contentBox)),(k=g.style)!=null&&k.backgroundColor&&(g.style.backgroundColor=W(l,g.style.backgroundColor,g.contentBox)),(v=g.style)!=null&&v.textStrokeColor&&(g.style.textStrokeColor=W(l,g.style.textStrokeColor,g.contentBox)),(A=g.style)!=null&&A.backgroundColor&&(l.fillStyle=g.style.backgroundColor,l.fillRect(g.inlineBox.x,g.inlineBox.y,g.inlineBox.width,g.inlineBox.height))})}),T(l,n),f.forEach(r=>{r.style&&T(l,r.style),r.fragments.forEach(x=>{T(l,{...x.style,textAlign:"left",verticalAlign:"top"});const{x:u,y:e,width:b,height:g}=x.contentBox,C=x.getComputedStyle();switch(C.writingMode){case"vertical-rl":case"vertical-lr":{let k=0;for(const v of x.content)C.textStrokeWidth&&l.strokeText(v,u,e+k),l.fillText(v,u,e+k),k+=C.fontSize+C.letterSpacing;break}case"horizontal-tb":C.textStrokeWidth&&l.strokeText(x.content,u,e),l.fillText(x.content,u,e);break}switch(C.textDecoration){case"underline":l.beginPath(),l.moveTo(u,e+g-2),l.lineTo(u+b,e+g-2),l.stroke();break;case"line-through":l.beginPath(),l.moveTo(u,e+g/2),l.lineTo(u+b,e+g/2),l.stroke();break}})}),t}exports.measureText=R;exports.renderText=V;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class m{get left(){return this.x}get top(){return this.y}get right(){return this.x+this.width}get bottom(){return this.y+this.height}constructor({x:t=0,y:n=0,width:l=0,height:s=0}={}){this.x=t,this.y=n,this.width=l,this.height=s}static from(...t){const n=t[0],l=t.slice(1).reduce((s,r)=>(s.x=Math.min(s.x,r.x),s.y=Math.min(s.y,r.y),s.right=Math.max(s.right,r.right),s.bottom=Math.max(s.bottom,r.bottom),s),{x:n.x,y:n.y,right:n.right,bottom:n.bottom});return new m({x:l.x,y:l.y,width:l.right-l.x,height:l.bottom-l.y})}move(t,n){return this.x+=t,this.y+=n,this}clone(){return new m({x:this.x,y:this.y,width:this.width,height:this.height})}}class R{constructor({content:t="",style:n,parent:l,contentBox:s=new m,inlineBox:r=new m,glyphBox:g=new m,centerX:f=0,baseline:b=0}={}){switch(this.content=t,this.style=n,this.parent=l,this.contentBox=s,this.inlineBox=r,this.glyphBox=g,this.centerX=f,this.baseline=b,this.getComputedStyle().textTransform){case"uppercase":this.content=this.content.toUpperCase();break;case"lowercase":this.content=this.content.toLowerCase();break}}getComputedStyle(){var t;return{...(t=this.parent)==null?void 0:t.getComputedStyle(),...this.style}}clone(t){return new R({content:this.content,style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),inlineBox:this.inlineBox.clone(),glyphBox:this.glyphBox.clone(),centerX:this.centerX,baseline:this.baseline,...t})}}class W{constructor({style:t,parent:n,contentBox:l=new m,lineBox:s=new m,glyphBox:r=new m,baseline:g=0,xHeight:f=0,maxCharWidth:b=0,fragments:u=[]}={}){this.style=t,this.parent=n,this.contentBox=l,this.lineBox=s,this.glyphBox=r,this.baseline=g,this.xHeight=f,this.maxCharWidth=b,this.fragments=u}addFragment(t){return this.fragments.push(new R({...t,parent:this})),this}getComputedStyle(){return{...this.parent,...this.style}}clone(t){return new W({style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),lineBox:this.lineBox.clone(),glyphBox:this.glyphBox.clone(),baseline:this.baseline,xHeight:this.xHeight,maxCharWidth:this.maxCharWidth,fragments:this.fragments.map(n=>n.clone()),...t})}}function D(a,t){const n=[];if(typeof a=="string")n.push(new W({parent:t}).addFragment({content:a}));else{a=Array.isArray(a)?a:[a];for(const l of a)if(typeof l=="string")n.push(new W({parent:t}).addFragment({content:l}));else if(Array.isArray(l)){const s=new W({parent:t});l.forEach(r=>{if(typeof r=="string")s.addFragment({content:r});else{const{content:g,...f}=r;s.addFragment({content:g,style:f})}}),n.push(s)}else if("fragments"in l){const{fragments:s,...r}=l,g=new W({style:r,parent:t});s.forEach(f=>{const{content:b,...u}=f;g.addFragment({content:b,style:u})}),n.push(g)}else if("content"in l){const{content:s,...r}=l;n.push(new W({style:r,parent:t}).addFragment({content:s}))}}return n}const L="OffscreenCanvas"in globalThis;let N;function K(){return N??(N=L?new OffscreenCanvas(1,1):document.createElement("canvas"))}function X(a,t){switch(t.shadowColor&&(a.shadowColor=t.shadowColor),t.shadowOffsetX!==void 0&&(a.shadowOffsetX=t.shadowOffsetX),t.shadowOffsetY!==void 0&&(a.shadowOffsetY=t.shadowOffsetY),t.shadowBlur!==void 0&&(a.shadowBlur=t.shadowBlur),t.textStrokeColor&&(a.strokeStyle=t.textStrokeColor),t.textStrokeWidth!==void 0&&(a.lineWidth=t.textStrokeWidth),t.color&&(a.fillStyle=t.color),t.textAlign&&(a.textAlign=t.textAlign),t.fontKerning&&(a.fontKerning=t.fontKerning),t.verticalAlign){case"baseline":a.textBaseline="alphabetic";break;case"top":case"middle":case"bottom":a.textBaseline=t.verticalAlign;break}t.letterSpacing!==void 0&&(a.letterSpacing=`${t.letterSpacing}px`),(t.fontStyle||t.fontWeight!==void 0||t.fontSize!==void 0||t.fontFamily)&&(a.font=[t.fontStyle||"normal",t.fontWeight||"normal",`${t.fontSize||14}px`,t.fontFamily||"sans-serif"].join(" "))}function z(a,t){const n=K().getContext("2d");return X(n,t),n.measureText(a)}const j=/[\s\n\t\u200B\u200C\u200D\u200E\u200F.,?!:;"'(){}\[\]<>\/\\|~#\$%\*\+=&^,。?!:;“”‘’()【】《》……——]/;function G(a,t,n){const l=[],s=a.slice();let r,g;for(;r=s.shift();){const f=r.fragments.slice();let b=0;const u=[];for(;g=f.shift();){const S=g.getComputedStyle();let C="",h=!1,o=0,i="";for(const B of g.content){if(r.maxCharWidth=Math.max(r.maxCharWidth,z(B,{...S,letterSpacing:0}).width),i+=B,j.test(g.content[++o]))continue;let d,x;switch(S.writingMode){case"vertical-lr":case"vertical-rl":d=n,x=i.length*S.fontSize;break;case"horizontal-tb":default:d=t,x=z(i,{...S,letterSpacing:0}).width;break}x+=i.length*S.letterSpacing;const y=/^[\r\n]$/.test(i);if(y||S.textWrap==="wrap"&&d&&b+x>d){let e=y?C.length+1:C.length;!b&&!e&&(C+=i,e+=i.length),C.length&&u.push(g.clone({content:C})),u.length&&(l.push(r.clone({fragments:u.slice()})),u.length=0);const w=g.content.substring(e);(w.length||f.length)&&s.unshift(r.clone({maxCharWidth:0,fragments:(w.length?[g.clone({content:w})]:[]).concat(f.slice())})),f.length=0,h=!0;break}else b+=x;C+=i,i=""}h||u.push(g.clone())}u.length&&l.push(r.clone({fragments:u}))}return l}function U(a){return{width:0,height:0,color:null,backgroundColor:null,fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",verticalAlign:"baseline",textTransform:"none",textDecoration:null,textStrokeWidth:0,textStrokeColor:null,lineHeight:1,letterSpacing:0,shadowColor:null,shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0,writingMode:"horizontal-tb",...a}}function $(a){const{content:t}=a,{width:n,height:l,...s}=U(a.style);let r=D(t,s);r=G(r,n,l);let g=r.reduce((o,i)=>o+i.maxCharWidth*i.getComputedStyle().lineHeight,0),f=0,b=0;r.forEach(o=>{const i=[];let B=null,d=f,x=b;o.fragments.forEach((e,w)=>{const c=e.getComputedStyle();switch(c.writingMode){case"vertical-rl":case"vertical-lr":{const p=o.maxCharWidth,v=p*c.lineHeight;w||(x=0,c.writingMode==="vertical-rl"&&(g-=v,d=g));const A=e.content.length,k=A*c.fontSize+(A-1)*c.letterSpacing;e.contentBox.x=d+(v-p)/2,e.contentBox.y=x,e.contentBox.width=p,e.contentBox.height=k,e.inlineBox.x=d,e.inlineBox.y=x,e.inlineBox.width=v,e.inlineBox.height=k,e.glyphBox.x=d+(v-p)/2,e.glyphBox.y=x,e.glyphBox.width=p,e.glyphBox.height=k,e.baseline=0,e.centerX=d+v/2,x+=k+c.letterSpacing;break}case"horizontal-tb":{w||(d=0);const{fontBoundingBoxAscent:p,fontBoundingBoxDescent:v,actualBoundingBoxAscent:A,actualBoundingBoxDescent:k,actualBoundingBoxLeft:M,actualBoundingBoxRight:E,width:F}=z(e.content,{...c,textAlign:"center",verticalAlign:"baseline"}),H=c.fontSize,O=H*c.lineHeight,Y=x+(O-(p+v))/2+p;e.contentBox.x=d,e.contentBox.y=x+(O-H)/2,e.contentBox.width=F,e.contentBox.height=H,e.inlineBox.x=d,e.inlineBox.y=x,e.inlineBox.width=F,e.inlineBox.height=O,e.glyphBox.x=d,e.glyphBox.y=Y-A,e.glyphBox.width=M+E,e.glyphBox.height=A+k,e.baseline=Y,e.centerX=d+M,i.push(e.contentBox),o.contentBox=m.from(...i),o.contentBox.height<e.contentBox.height&&(B=e),d+=F;break}}}),o.lineBox=m.from(...o.fragments.map(e=>e.inlineBox)),f+=o.lineBox.width,b+=o.lineBox.height;const y=z("x",{...(B??o).getComputedStyle(),textAlign:"left",verticalAlign:"baseline"});o.xHeight=y.actualBoundingBoxAscent,o.baseline=o.lineBox.y+(o.lineBox.height-(y.fontBoundingBoxAscent+y.fontBoundingBoxDescent))/2+y.fontBoundingBoxAscent});const u=m.from(...r.map(o=>o.lineBox),new m({width:n,height:l})),{width:S,height:C}=u;r.forEach(o=>{o.contentBox=m.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=m.from(...o.fragments.map(i=>i.glyphBox)),o.fragments.forEach(i=>{const B=i.getComputedStyle(),d=i.inlineBox.x,x=i.inlineBox.y;let y=d,e=x;switch(B.writingMode){case"vertical-rl":case"vertical-lr":switch(B.textAlign){case"end":case"right":e+=C-o.contentBox.height;break;case"center":e+=(C-o.contentBox.height)/2;break}break;case"horizontal-tb":{switch(B.textAlign){case"end":case"right":y+=S-o.contentBox.width;break;case"center":y+=(S-o.contentBox.width)/2;break}switch(B.verticalAlign){case"top":e+=o.lineBox.y-i.inlineBox.y;break;case"middle":e=o.baseline-o.xHeight/2-i.inlineBox.height/2;break;case"bottom":e+=o.lineBox.bottom-i.inlineBox.bottom;break;case"sub":e+=o.baseline-i.glyphBox.bottom;break;case"super":e+=o.baseline-i.glyphBox.y;break;case"text-top":e+=o.glyphBox.y-i.inlineBox.y;break;case"text-bottom":e+=o.glyphBox.bottom-i.inlineBox.bottom;break;case"baseline":default:i.inlineBox.height<o.lineBox.height&&(e+=o.baseline-i.baseline);break}break}}const w=y-d,c=e-x;i.inlineBox.move(w,c),i.contentBox.move(w,c),i.glyphBox.move(w,c),i.baseline+=c,i.centerX+=w}),o.contentBox=m.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=m.from(...o.fragments.map(i=>i.glyphBox))});const h=m.from(...r.map(o=>o.contentBox));return{box:u,contentBox:h,viewBox:m.from(u,h),glyphBox:m.from(...r.map(o=>o.glyphBox)),paragraphs:r}}function P(a,t,n){if(typeof t=="string"&&t.startsWith("linear-gradient")){const{x0:l,y0:s,x1:r,y1:g,stops:f}=I(t,n.left,n.top,n.width,n.height),b=a.createLinearGradient(l,s,r,g);return f.forEach(u=>b.addColorStop(u.offset,u.color)),b}return t}function I(a,t,n,l,s){var o;const r=((o=a.match(/linear-gradient\((.+)\)$/))==null?void 0:o[1])??"",g=r.split(",")[0],f=g.includes("deg")?g:"0deg",b=r.replace(f,"").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi),S=(Number(f.replace("deg",""))||0)*Math.PI/180,C=l*Math.sin(S),h=s*Math.cos(S);return{x0:t+l/2-C,y0:n+s/2+h,x1:t+l/2+C,y1:n+s/2-h,stops:Array.from(b).map(i=>{let B=i[2];return B.startsWith("(")?B=B.split(",").length>3?`rgba${B}`:`rgb${B}`:B=`#${B}`,{offset:Number(i[3].replace("%",""))/100,color:B}})}}function T(a,t,n){n!=null&&n.color&&(n.color=P(a,n.color,t)),n!=null&&n.backgroundColor&&(n.backgroundColor=P(a,n.backgroundColor,t)),n!=null&&n.textStrokeColor&&(n.textStrokeColor=P(a,n.textStrokeColor,t))}function V(a){const{view:t=document.createElement("canvas"),style:n,draws:l=[{}],pixelRatio:s=1}=a,{box:r,viewBox:g,paragraphs:f}=$(a),{x:b,y:u,width:S,height:C}=g,h=t.getContext("2d"),o=-b+S,i=-u+C;t.style.width=`${o}px`,t.style.height=`${i}px`,t.dataset.width=String(r.width),t.dataset.height=String(r.height),t.dataset.pixelRatio=String(s),t.width=Math.max(1,Math.floor(o*s)),t.height=Math.max(1,Math.floor(i*s)),h.scale(s,s),h.clearRect(0,0,t.width,t.height),h.translate(-b,-u);const B={...n};return T(h,new m({width:S,height:C}),B),f.forEach(d=>{T(h,d.contentBox,d.style),d.fragments.forEach(x=>{T(h,x.contentBox,x.style)})}),l.forEach(d=>{const x={...d};T(h,new m({width:S,height:C}),x);const y={...B,...x};y!=null&&y.backgroundColor&&(h.fillStyle=y.backgroundColor,h.fillRect(0,0,t.width,t.height)),f.forEach(e=>{var w;(w=e.style)!=null&&w.backgroundColor&&(h.fillStyle=e.style.backgroundColor,h.fillRect(e.lineBox.x,e.lineBox.y,e.lineBox.width,e.lineBox.height)),e.fragments.forEach(c=>{var p;(p=c.style)!=null&&p.backgroundColor&&(h.fillStyle=c.style.backgroundColor,h.fillRect(c.inlineBox.x,c.inlineBox.y,c.inlineBox.width,c.inlineBox.height))})}),X(h,y),f.forEach(e=>{e.style&&X(h,e.style),e.fragments.forEach(w=>{X(h,{...w.style,textAlign:"left",verticalAlign:"top"});let{x:c,y:p,width:v,height:A}=w.contentBox;x.offsetX&&(c+=x.offsetX),x.offsetY&&(p+=x.offsetY);const k=w.getComputedStyle();switch(k.writingMode){case"vertical-rl":case"vertical-lr":{let M=0;for(const E of w.content)h.fillText(E,c,p+M),k.textStrokeWidth&&h.strokeText(E,c,p+M),M+=k.fontSize+k.letterSpacing;break}case"horizontal-tb":h.fillText(w.content,c,p),k.textStrokeWidth&&h.strokeText(w.content,c,p);break}switch(k.textDecoration){case"underline":h.beginPath(),h.moveTo(c,p+A-2),h.lineTo(c+v,p+A-2),h.stroke();break;case"line-through":h.beginPath(),h.moveTo(c,p+A/2),h.lineTo(c+v,p+A/2),h.stroke();break}})})}),t}exports.measureText=$;exports.renderText=V;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(T,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(T=typeof globalThis<"u"?globalThis:T||self,f(T.modernText={}))})(this,function(T){"use strict";class f{get left(){return this.x}get top(){return this.y}get right(){return this.x+this.width}get bottom(){return this.y+this.height}constructor({x:t=0,y:h=0,width:l=0,height:n=0}={}){this.x=t,this.y=h,this.width=l,this.height=n}static from(...t){const h=t[0],l=t.slice(1).reduce((n,a)=>(n.x=Math.min(n.x,a.x),n.y=Math.min(n.y,a.y),n.right=Math.max(n.right,a.right),n.bottom=Math.max(n.bottom,a.bottom),n),{x:h.x,y:h.y,right:h.right,bottom:h.bottom});return new f({x:l.x,y:l.y,width:l.right-l.x,height:l.bottom-l.y})}move(t,h){return this.x+=t,this.y+=h,this}clone(){return new f({x:this.x,y:this.y,width:this.width,height:this.height})}}class F{constructor({content:t="",style:h,parent:l,contentBox:n=new f,inlineBox:a=new f,glyphBox:d=new f,centerX:u=0,baseline:w=0}={}){switch(this.content=t,this.style=h,this.parent=l,this.contentBox=n,this.inlineBox=a,this.glyphBox=d,this.centerX=u,this.baseline=w,this.getComputedStyle().textTransform){case"uppercase":this.content=this.content.toUpperCase();break;case"lowercase":this.content=this.content.toLowerCase();break}}getComputedStyle(){var t;return{...(t=this.parent)==null?void 0:t.getComputedStyle(),...this.style}}clone(t){return new F({content:this.content,style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),inlineBox:this.inlineBox.clone(),glyphBox:this.glyphBox.clone(),centerX:this.centerX,baseline:this.baseline,...t})}}class M{constructor({style:t,parent:h,contentBox:l=new f,lineBox:n=new f,glyphBox:a=new f,baseline:d=0,xHeight:u=0,maxCharWidth:w=0,fragments:y=[]}={}){this.style=t,this.parent=h,this.contentBox=l,this.lineBox=n,this.glyphBox=a,this.baseline=d,this.xHeight=u,this.maxCharWidth=w,this.fragments=y}addFragment(t){return this.fragments.push(new F({...t,parent:this})),this}getComputedStyle(){return{...this.parent,...this.style}}clone(t){return new M({style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),lineBox:this.lineBox.clone(),glyphBox:this.glyphBox.clone(),baseline:this.baseline,xHeight:this.xHeight,maxCharWidth:this.maxCharWidth,fragments:this.fragments.map(h=>h.clone()),...t})}}function D(c,t){const h=[];if(typeof c=="string")h.push(new M({parent:t}).addFragment({content:c}));else{c=Array.isArray(c)?c:[c];for(const l of c)if(typeof l=="string")h.push(new M({parent:t}).addFragment({content:l}));else if(Array.isArray(l)){const n=new M({parent:t});l.forEach(a=>{if(typeof a=="string")n.addFragment({content:a});else{const{content:d,...u}=a;n.addFragment({content:d,style:u})}}),h.push(n)}else if("fragments"in l){const{fragments:n,...a}=l,d=new M({style:a,parent:t});n.forEach(u=>{const{content:w,...y}=u;d.addFragment({content:w,style:y})}),h.push(d)}else if("content"in l){const{content:n,...a}=l;h.push(new M({style:a,parent:t}).addFragment({content:n}))}}return h}const Y="OffscreenCanvas"in globalThis;let L;function N(){return L??(L=Y?new OffscreenCanvas(1,1):document.createElement("canvas"))}function E(c,t){switch(t.shadowColor&&(c.shadowColor=t.shadowColor),t.shadowOffsetX!==void 0&&(c.shadowOffsetX=t.shadowOffsetX),t.shadowOffsetY!==void 0&&(c.shadowOffsetY=t.shadowOffsetY),t.shadowBlur!==void 0&&(c.shadowBlur=t.shadowBlur),t.textStrokeColor&&(c.strokeStyle=t.textStrokeColor),t.textStrokeWidth!==void 0&&(c.lineWidth=t.textStrokeWidth),t.color&&(c.fillStyle=t.color),t.direction&&(c.direction=t.direction),t.textAlign&&(c.textAlign=t.textAlign),t.fontKerning&&(c.fontKerning=t.fontKerning),t.verticalAlign){case"baseline":c.textBaseline="alphabetic";break;case"top":case"middle":case"bottom":c.textBaseline=t.verticalAlign;break}(t.fontStyle||t.fontWeight!==void 0||t.fontSize!==void 0||t.fontFamily)&&(c.font=[t.fontStyle||"normal",t.fontWeight||"normal",`${t.fontSize||14}px`,t.fontFamily||"sans-serif"].join(" "))}function z(c,t){const h=N().getContext("2d");return E(h,t),h.measureText(c)}const K=/[\s\n\t\u200B\u200C\u200D\u200E\u200F.,?!:;"'(){}\[\]<>\/\\|~#\$%\*\+=&^,。?!:;“”‘’()【】《》……——]/;function j(c,t,h){const l=[],n=c.slice();let a,d;for(;a=n.shift();){const u=a.fragments.slice();let w=0;const y=[];for(;d=u.shift();){const p=d.getComputedStyle();let S="",s=!1,o=0,i="";for(const B of d.content){if(a.maxCharWidth=Math.max(a.maxCharWidth,z(B,p).width),i+=B,K.test(d.content[++o]))continue;let r,x;switch(p.writingMode){case"vertical-lr":case"vertical-rl":r=h,x=i.length*p.fontSize;break;case"horizontal-tb":default:r=t,x=z(i,p).width;break}x+=i.length*p.letterSpacing;const m=/^[\r\n]$/.test(i);if(m||p.textWrap==="wrap"&&r&&w+x>r){let e=m?S.length+1:S.length;!w&&!e&&(S+=i,e+=i.length),S.length&&y.push(d.clone({content:S})),y.length&&(l.push(a.clone({fragments:y.slice()})),y.length=0);const b=d.content.substring(e);(b.length||u.length)&&n.unshift(a.clone({maxCharWidth:0,fragments:(b.length?[d.clone({content:b})]:[]).concat(u.slice())})),u.length=0,s=!0;break}else w+=x;S+=i,i=""}s||y.push(d.clone())}y.length&&l.push(a.clone({fragments:y}))}return l}function G(c){return{width:0,height:0,color:null,backgroundColor:null,fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",verticalAlign:"baseline",textTransform:"none",textDecoration:null,textStrokeWidth:0,textStrokeColor:null,direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:null,shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0,writingMode:"horizontal-tb",...c}}function P(c){const{content:t}=c,{width:h,height:l,...n}=G(c.style);let a=D(t,n);a=j(a,h,l);const d=a.reduce((o,i)=>o+i.maxCharWidth,0);let u=0,w=0;a.forEach(o=>{const i=[];let B=null,r=u,x=w;o.fragments.forEach((e,b)=>{const g=e.getComputedStyle();switch(g.writingMode){case"vertical-rl":r=d-r;case"vertical-lr":{b||(x=0);const C=e.content.length,k=o.maxCharWidth,v=k*g.lineHeight,W=C*g.fontSize+(C-1)*g.letterSpacing;e.contentBox.x=r+(v-k)/2,e.contentBox.y=x,e.contentBox.width=k,e.contentBox.height=W,e.inlineBox.x=r,e.inlineBox.y=x,e.inlineBox.width=v,e.inlineBox.height=W,e.glyphBox.x=r+(v-k)/2,e.glyphBox.y=x,e.glyphBox.width=k,e.glyphBox.height=W,e.baseline=0,e.centerX=r+v/2,x+=W;break}case"horizontal-tb":{b||(r=0);const{fontBoundingBoxAscent:C,fontBoundingBoxDescent:k,actualBoundingBoxAscent:v,actualBoundingBoxDescent:W,actualBoundingBoxLeft:R,actualBoundingBoxRight:I,width:X}=z(e.content,{...g,textAlign:"center",verticalAlign:"baseline"}),H=g.fontSize,O=H*g.lineHeight,$=x+(O-(C+k))/2+C;e.contentBox.x=r,e.contentBox.y=x+(O-H)/2,e.contentBox.width=X,e.contentBox.height=H,e.inlineBox.x=r,e.inlineBox.y=x,e.inlineBox.width=X,e.inlineBox.height=O,e.glyphBox.x=r,e.glyphBox.y=$-v,e.glyphBox.width=R+I,e.glyphBox.height=v+W,e.baseline=$,e.centerX=r+R,i.push(e.contentBox),o.contentBox=f.from(...i),o.contentBox.height<e.contentBox.height&&(B=e),r+=X;break}}}),o.lineBox=f.from(...o.fragments.map(e=>e.inlineBox)),u+=o.lineBox.width,w+=o.lineBox.height;const m=z("x",{...(B??o).getComputedStyle(),textAlign:"left",verticalAlign:"baseline"});o.xHeight=m.actualBoundingBoxAscent,o.baseline=o.lineBox.y+(o.lineBox.height-(m.fontBoundingBoxAscent+m.fontBoundingBoxDescent))/2+m.fontBoundingBoxAscent});const y=f.from(...a.map(o=>o.lineBox),new f({width:h,height:l})),{width:p,height:S}=y;a.forEach(o=>{o.contentBox=f.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=f.from(...o.fragments.map(i=>i.glyphBox)),o.fragments.forEach(i=>{const B=i.getComputedStyle(),r=i.inlineBox.x,x=i.inlineBox.y;let m=r,e=x;switch(B.writingMode){case"vertical-rl":case"vertical-lr":switch(B.textAlign){case"end":case"right":e+=S-o.contentBox.height;break;case"center":e+=(S-o.contentBox.height)/2;break}break;case"horizontal-tb":{switch(B.textAlign){case"end":case"right":m+=p-o.contentBox.width;break;case"center":m+=(p-o.contentBox.width)/2;break}switch(B.verticalAlign){case"top":e+=o.lineBox.y-i.inlineBox.y;break;case"middle":e=o.baseline-o.xHeight/2-i.inlineBox.height/2;break;case"bottom":e+=o.lineBox.bottom-i.inlineBox.bottom;break;case"sub":e+=o.baseline-i.glyphBox.bottom;break;case"super":e+=o.baseline-i.glyphBox.y;break;case"text-top":e+=o.glyphBox.y-i.inlineBox.y;break;case"text-bottom":e+=o.glyphBox.bottom-i.inlineBox.bottom;break;case"baseline":default:i.inlineBox.height<o.lineBox.height&&(e+=o.baseline-i.baseline);break}break}}const b=m-r,g=e-x;i.inlineBox.move(b,g),i.contentBox.move(b,g),i.glyphBox.move(b,g),i.baseline+=g,i.centerX+=b}),o.contentBox=f.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=f.from(...o.fragments.map(i=>i.glyphBox))});const s=f.from(...a.map(o=>o.contentBox));return{box:y,contentBox:s,viewBox:f.from(y,s),glyphBox:f.from(...a.map(o=>o.glyphBox)),paragraphs:a}}function A(c,t,h){if(typeof t=="string"&&t.startsWith("linear-gradient")){const{x0:l,y0:n,x1:a,y1:d,stops:u}=U(t,h.left,h.top,h.width,h.height),w=c.createLinearGradient(l,n,a,d);return u.forEach(y=>w.addColorStop(y.offset,y.color)),w}return t}function U(c,t,h,l,n){var o;const a=((o=c.match(/linear-gradient\((.+)\)$/))==null?void 0:o[1])??"",d=a.split(",")[0],u=d.includes("deg")?d:"0deg",w=a.replace(u,"").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi),p=(Number(u.replace("deg",""))||0)*Math.PI/180,S=l*Math.sin(p),s=n*Math.cos(p);return{x0:t+l/2-S,y0:h+n/2+s,x1:t+l/2+S,y1:h+n/2-s,stops:Array.from(w).map(i=>{let B=i[2];return B.startsWith("(")?B=B.split(",").length>3?`rgba${B}`:`rgb${B}`:B=`#${B}`,{offset:Number(i[3].replace("%",""))/100,color:B}})}}function V(c){const{view:t=document.createElement("canvas"),style:h,pixelRatio:l=1}=c,n={...h},{box:a,viewBox:d,paragraphs:u}=P(c),{x:w,y,width:p,height:S}=d,s=t.getContext("2d"),o=-w+p,i=-y+S;t.style.width=`${o}px`,t.style.height=`${i}px`,t.dataset.width=String(a.width),t.dataset.height=String(a.height),t.dataset.pixelRatio=String(l),t.width=Math.max(1,Math.floor(o*l)),t.height=Math.max(1,Math.floor(i*l)),s.scale(l,l),s.clearRect(0,0,t.width,t.height),s.translate(-w,-y);const B=new f({width:p,height:S});return n!=null&&n.color&&(n.color=A(s,n.color,B)),n!=null&&n.backgroundColor&&(n.backgroundColor=A(s,n.backgroundColor,B)),n!=null&&n.textStrokeColor&&(n.textStrokeColor=A(s,n.textStrokeColor,B)),n!=null&&n.backgroundColor&&(s.fillStyle=n.backgroundColor,s.fillRect(0,0,t.width,t.height)),u.forEach(r=>{var x,m,e,b;(x=r.style)!=null&&x.color&&(r.style.color=A(s,r.style.color,r.contentBox)),(m=r.style)!=null&&m.backgroundColor&&(r.style.backgroundColor=A(s,r.style.backgroundColor,r.contentBox)),(e=r.style)!=null&&e.textStrokeColor&&(r.style.textStrokeColor=A(s,r.style.textStrokeColor,r.contentBox)),(b=r.style)!=null&&b.backgroundColor&&(s.fillStyle=r.style.backgroundColor,s.fillRect(r.lineBox.x,r.lineBox.y,r.lineBox.width,r.lineBox.height)),r.fragments.forEach(g=>{var C,k,v,W;(C=g.style)!=null&&C.color&&(g.style.color=A(s,g.style.color,g.contentBox)),(k=g.style)!=null&&k.backgroundColor&&(g.style.backgroundColor=A(s,g.style.backgroundColor,g.contentBox)),(v=g.style)!=null&&v.textStrokeColor&&(g.style.textStrokeColor=A(s,g.style.textStrokeColor,g.contentBox)),(W=g.style)!=null&&W.backgroundColor&&(s.fillStyle=g.style.backgroundColor,s.fillRect(g.inlineBox.x,g.inlineBox.y,g.inlineBox.width,g.inlineBox.height))})}),E(s,n),u.forEach(r=>{r.style&&E(s,r.style),r.fragments.forEach(x=>{E(s,{...x.style,textAlign:"left",verticalAlign:"top"});const{x:m,y:e,width:b,height:g}=x.contentBox,C=x.getComputedStyle();switch(C.writingMode){case"vertical-rl":case"vertical-lr":{let k=0;for(const v of x.content)C.textStrokeWidth&&s.strokeText(v,m,e+k),s.fillText(v,m,e+k),k+=C.fontSize+C.letterSpacing;break}case"horizontal-tb":C.textStrokeWidth&&s.strokeText(x.content,m,e),s.fillText(x.content,m,e);break}switch(C.textDecoration){case"underline":s.beginPath(),s.moveTo(m,e+g-2),s.lineTo(m+b,e+g-2),s.stroke();break;case"line-through":s.beginPath(),s.moveTo(m,e+g/2),s.lineTo(m+b,e+g/2),s.stroke();break}})}),t}T.measureText=P,T.renderText=V,Object.defineProperty(T,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(W,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(W=typeof globalThis<"u"?globalThis:W||self,d(W.modernText={}))})(this,function(W){"use strict";class d{get left(){return this.x}get top(){return this.y}get right(){return this.x+this.width}get bottom(){return this.y+this.height}constructor({x:t=0,y:n=0,width:l=0,height:s=0}={}){this.x=t,this.y=n,this.width=l,this.height=s}static from(...t){const n=t[0],l=t.slice(1).reduce((s,r)=>(s.x=Math.min(s.x,r.x),s.y=Math.min(s.y,r.y),s.right=Math.max(s.right,r.right),s.bottom=Math.max(s.bottom,r.bottom),s),{x:n.x,y:n.y,right:n.right,bottom:n.bottom});return new d({x:l.x,y:l.y,width:l.right-l.x,height:l.bottom-l.y})}move(t,n){return this.x+=t,this.y+=n,this}clone(){return new d({x:this.x,y:this.y,width:this.width,height:this.height})}}class H{constructor({content:t="",style:n,parent:l,contentBox:s=new d,inlineBox:r=new d,glyphBox:g=new d,centerX:u=0,baseline:b=0}={}){switch(this.content=t,this.style=n,this.parent=l,this.contentBox=s,this.inlineBox=r,this.glyphBox=g,this.centerX=u,this.baseline=b,this.getComputedStyle().textTransform){case"uppercase":this.content=this.content.toUpperCase();break;case"lowercase":this.content=this.content.toLowerCase();break}}getComputedStyle(){var t;return{...(t=this.parent)==null?void 0:t.getComputedStyle(),...this.style}}clone(t){return new H({content:this.content,style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),inlineBox:this.inlineBox.clone(),glyphBox:this.glyphBox.clone(),centerX:this.centerX,baseline:this.baseline,...t})}}class T{constructor({style:t,parent:n,contentBox:l=new d,lineBox:s=new d,glyphBox:r=new d,baseline:g=0,xHeight:u=0,maxCharWidth:b=0,fragments:B=[]}={}){this.style=t,this.parent=n,this.contentBox=l,this.lineBox=s,this.glyphBox=r,this.baseline=g,this.xHeight=u,this.maxCharWidth=b,this.fragments=B}addFragment(t){return this.fragments.push(new H({...t,parent:this})),this}getComputedStyle(){return{...this.parent,...this.style}}clone(t){return new T({style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),lineBox:this.lineBox.clone(),glyphBox:this.glyphBox.clone(),baseline:this.baseline,xHeight:this.xHeight,maxCharWidth:this.maxCharWidth,fragments:this.fragments.map(n=>n.clone()),...t})}}function L(a,t){const n=[];if(typeof a=="string")n.push(new T({parent:t}).addFragment({content:a}));else{a=Array.isArray(a)?a:[a];for(const l of a)if(typeof l=="string")n.push(new T({parent:t}).addFragment({content:l}));else if(Array.isArray(l)){const s=new T({parent:t});l.forEach(r=>{if(typeof r=="string")s.addFragment({content:r});else{const{content:g,...u}=r;s.addFragment({content:g,style:u})}}),n.push(s)}else if("fragments"in l){const{fragments:s,...r}=l,g=new T({style:r,parent:t});s.forEach(u=>{const{content:b,...B}=u;g.addFragment({content:b,style:B})}),n.push(g)}else if("content"in l){const{content:s,...r}=l;n.push(new T({style:r,parent:t}).addFragment({content:s}))}}return n}const N="OffscreenCanvas"in globalThis;let K;function j(){return K??(K=N?new OffscreenCanvas(1,1):document.createElement("canvas"))}function E(a,t){switch(t.shadowColor&&(a.shadowColor=t.shadowColor),t.shadowOffsetX!==void 0&&(a.shadowOffsetX=t.shadowOffsetX),t.shadowOffsetY!==void 0&&(a.shadowOffsetY=t.shadowOffsetY),t.shadowBlur!==void 0&&(a.shadowBlur=t.shadowBlur),t.textStrokeColor&&(a.strokeStyle=t.textStrokeColor),t.textStrokeWidth!==void 0&&(a.lineWidth=t.textStrokeWidth),t.color&&(a.fillStyle=t.color),t.textAlign&&(a.textAlign=t.textAlign),t.fontKerning&&(a.fontKerning=t.fontKerning),t.verticalAlign){case"baseline":a.textBaseline="alphabetic";break;case"top":case"middle":case"bottom":a.textBaseline=t.verticalAlign;break}t.letterSpacing!==void 0&&(a.letterSpacing=`${t.letterSpacing}px`),(t.fontStyle||t.fontWeight!==void 0||t.fontSize!==void 0||t.fontFamily)&&(a.font=[t.fontStyle||"normal",t.fontWeight||"normal",`${t.fontSize||14}px`,t.fontFamily||"sans-serif"].join(" "))}function X(a,t){const n=j().getContext("2d");return E(n,t),n.measureText(a)}const G=/[\s\n\t\u200B\u200C\u200D\u200E\u200F.,?!:;"'(){}\[\]<>\/\\|~#\$%\*\+=&^,。?!:;“”‘’()【】《》……——]/;function U(a,t,n){const l=[],s=a.slice();let r,g;for(;r=s.shift();){const u=r.fragments.slice();let b=0;const B=[];for(;g=u.shift();){const S=g.getComputedStyle();let C="",h=!1,o=0,i="";for(const m of g.content){if(r.maxCharWidth=Math.max(r.maxCharWidth,X(m,{...S,letterSpacing:0}).width),i+=m,G.test(g.content[++o]))continue;let f,x;switch(S.writingMode){case"vertical-lr":case"vertical-rl":f=n,x=i.length*S.fontSize;break;case"horizontal-tb":default:f=t,x=X(i,{...S,letterSpacing:0}).width;break}x+=i.length*S.letterSpacing;const y=/^[\r\n]$/.test(i);if(y||S.textWrap==="wrap"&&f&&b+x>f){let e=y?C.length+1:C.length;!b&&!e&&(C+=i,e+=i.length),C.length&&B.push(g.clone({content:C})),B.length&&(l.push(r.clone({fragments:B.slice()})),B.length=0);const p=g.content.substring(e);(p.length||u.length)&&s.unshift(r.clone({maxCharWidth:0,fragments:(p.length?[g.clone({content:p})]:[]).concat(u.slice())})),u.length=0,h=!0;break}else b+=x;C+=i,i=""}h||B.push(g.clone())}B.length&&l.push(r.clone({fragments:B}))}return l}function I(a){return{width:0,height:0,color:null,backgroundColor:null,fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",verticalAlign:"baseline",textTransform:"none",textDecoration:null,textStrokeWidth:0,textStrokeColor:null,lineHeight:1,letterSpacing:0,shadowColor:null,shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0,writingMode:"horizontal-tb",...a}}function $(a){const{content:t}=a,{width:n,height:l,...s}=I(a.style);let r=L(t,s);r=U(r,n,l);let g=r.reduce((o,i)=>o+i.maxCharWidth*i.getComputedStyle().lineHeight,0),u=0,b=0;r.forEach(o=>{const i=[];let m=null,f=u,x=b;o.fragments.forEach((e,p)=>{const c=e.getComputedStyle();switch(c.writingMode){case"vertical-rl":case"vertical-lr":{const w=o.maxCharWidth,v=w*c.lineHeight;p||(x=0,c.writingMode==="vertical-rl"&&(g-=v,f=g));const A=e.content.length,k=A*c.fontSize+(A-1)*c.letterSpacing;e.contentBox.x=f+(v-w)/2,e.contentBox.y=x,e.contentBox.width=w,e.contentBox.height=k,e.inlineBox.x=f,e.inlineBox.y=x,e.inlineBox.width=v,e.inlineBox.height=k,e.glyphBox.x=f+(v-w)/2,e.glyphBox.y=x,e.glyphBox.width=w,e.glyphBox.height=k,e.baseline=0,e.centerX=f+v/2,x+=k+c.letterSpacing;break}case"horizontal-tb":{p||(f=0);const{fontBoundingBoxAscent:w,fontBoundingBoxDescent:v,actualBoundingBoxAscent:A,actualBoundingBoxDescent:k,actualBoundingBoxLeft:M,actualBoundingBoxRight:F,width:P}=X(e.content,{...c,textAlign:"center",verticalAlign:"baseline"}),R=c.fontSize,Y=R*c.lineHeight,D=x+(Y-(w+v))/2+w;e.contentBox.x=f,e.contentBox.y=x+(Y-R)/2,e.contentBox.width=P,e.contentBox.height=R,e.inlineBox.x=f,e.inlineBox.y=x,e.inlineBox.width=P,e.inlineBox.height=Y,e.glyphBox.x=f,e.glyphBox.y=D-A,e.glyphBox.width=M+F,e.glyphBox.height=A+k,e.baseline=D,e.centerX=f+M,i.push(e.contentBox),o.contentBox=d.from(...i),o.contentBox.height<e.contentBox.height&&(m=e),f+=P;break}}}),o.lineBox=d.from(...o.fragments.map(e=>e.inlineBox)),u+=o.lineBox.width,b+=o.lineBox.height;const y=X("x",{...(m??o).getComputedStyle(),textAlign:"left",verticalAlign:"baseline"});o.xHeight=y.actualBoundingBoxAscent,o.baseline=o.lineBox.y+(o.lineBox.height-(y.fontBoundingBoxAscent+y.fontBoundingBoxDescent))/2+y.fontBoundingBoxAscent});const B=d.from(...r.map(o=>o.lineBox),new d({width:n,height:l})),{width:S,height:C}=B;r.forEach(o=>{o.contentBox=d.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=d.from(...o.fragments.map(i=>i.glyphBox)),o.fragments.forEach(i=>{const m=i.getComputedStyle(),f=i.inlineBox.x,x=i.inlineBox.y;let y=f,e=x;switch(m.writingMode){case"vertical-rl":case"vertical-lr":switch(m.textAlign){case"end":case"right":e+=C-o.contentBox.height;break;case"center":e+=(C-o.contentBox.height)/2;break}break;case"horizontal-tb":{switch(m.textAlign){case"end":case"right":y+=S-o.contentBox.width;break;case"center":y+=(S-o.contentBox.width)/2;break}switch(m.verticalAlign){case"top":e+=o.lineBox.y-i.inlineBox.y;break;case"middle":e=o.baseline-o.xHeight/2-i.inlineBox.height/2;break;case"bottom":e+=o.lineBox.bottom-i.inlineBox.bottom;break;case"sub":e+=o.baseline-i.glyphBox.bottom;break;case"super":e+=o.baseline-i.glyphBox.y;break;case"text-top":e+=o.glyphBox.y-i.inlineBox.y;break;case"text-bottom":e+=o.glyphBox.bottom-i.inlineBox.bottom;break;case"baseline":default:i.inlineBox.height<o.lineBox.height&&(e+=o.baseline-i.baseline);break}break}}const p=y-f,c=e-x;i.inlineBox.move(p,c),i.contentBox.move(p,c),i.glyphBox.move(p,c),i.baseline+=c,i.centerX+=p}),o.contentBox=d.from(...o.fragments.map(i=>i.contentBox)),o.glyphBox=d.from(...o.fragments.map(i=>i.glyphBox))});const h=d.from(...r.map(o=>o.contentBox));return{box:B,contentBox:h,viewBox:d.from(B,h),glyphBox:d.from(...r.map(o=>o.glyphBox)),paragraphs:r}}function O(a,t,n){if(typeof t=="string"&&t.startsWith("linear-gradient")){const{x0:l,y0:s,x1:r,y1:g,stops:u}=V(t,n.left,n.top,n.width,n.height),b=a.createLinearGradient(l,s,r,g);return u.forEach(B=>b.addColorStop(B.offset,B.color)),b}return t}function V(a,t,n,l,s){var o;const r=((o=a.match(/linear-gradient\((.+)\)$/))==null?void 0:o[1])??"",g=r.split(",")[0],u=g.includes("deg")?g:"0deg",b=r.replace(u,"").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi),S=(Number(u.replace("deg",""))||0)*Math.PI/180,C=l*Math.sin(S),h=s*Math.cos(S);return{x0:t+l/2-C,y0:n+s/2+h,x1:t+l/2+C,y1:n+s/2-h,stops:Array.from(b).map(i=>{let m=i[2];return m.startsWith("(")?m=m.split(",").length>3?`rgba${m}`:`rgb${m}`:m=`#${m}`,{offset:Number(i[3].replace("%",""))/100,color:m}})}}function z(a,t,n){n!=null&&n.color&&(n.color=O(a,n.color,t)),n!=null&&n.backgroundColor&&(n.backgroundColor=O(a,n.backgroundColor,t)),n!=null&&n.textStrokeColor&&(n.textStrokeColor=O(a,n.textStrokeColor,t))}function q(a){const{view:t=document.createElement("canvas"),style:n,draws:l=[{}],pixelRatio:s=1}=a,{box:r,viewBox:g,paragraphs:u}=$(a),{x:b,y:B,width:S,height:C}=g,h=t.getContext("2d"),o=-b+S,i=-B+C;t.style.width=`${o}px`,t.style.height=`${i}px`,t.dataset.width=String(r.width),t.dataset.height=String(r.height),t.dataset.pixelRatio=String(s),t.width=Math.max(1,Math.floor(o*s)),t.height=Math.max(1,Math.floor(i*s)),h.scale(s,s),h.clearRect(0,0,t.width,t.height),h.translate(-b,-B);const m={...n};return z(h,new d({width:S,height:C}),m),u.forEach(f=>{z(h,f.contentBox,f.style),f.fragments.forEach(x=>{z(h,x.contentBox,x.style)})}),l.forEach(f=>{const x={...f};z(h,new d({width:S,height:C}),x);const y={...m,...x};y!=null&&y.backgroundColor&&(h.fillStyle=y.backgroundColor,h.fillRect(0,0,t.width,t.height)),u.forEach(e=>{var p;(p=e.style)!=null&&p.backgroundColor&&(h.fillStyle=e.style.backgroundColor,h.fillRect(e.lineBox.x,e.lineBox.y,e.lineBox.width,e.lineBox.height)),e.fragments.forEach(c=>{var w;(w=c.style)!=null&&w.backgroundColor&&(h.fillStyle=c.style.backgroundColor,h.fillRect(c.inlineBox.x,c.inlineBox.y,c.inlineBox.width,c.inlineBox.height))})}),E(h,y),u.forEach(e=>{e.style&&E(h,e.style),e.fragments.forEach(p=>{E(h,{...p.style,textAlign:"left",verticalAlign:"top"});let{x:c,y:w,width:v,height:A}=p.contentBox;x.offsetX&&(c+=x.offsetX),x.offsetY&&(w+=x.offsetY);const k=p.getComputedStyle();switch(k.writingMode){case"vertical-rl":case"vertical-lr":{let M=0;for(const F of p.content)h.fillText(F,c,w+M),k.textStrokeWidth&&h.strokeText(F,c,w+M),M+=k.fontSize+k.letterSpacing;break}case"horizontal-tb":h.fillText(p.content,c,w),k.textStrokeWidth&&h.strokeText(p.content,c,w);break}switch(k.textDecoration){case"underline":h.beginPath(),h.moveTo(c,w+A-2),h.lineTo(c+v,w+A-2),h.stroke();break;case"line-through":h.beginPath(),h.moveTo(c,w+A/2),h.lineTo(c+v,w+A/2),h.stroke();break}})})}),t}W.measureText=$,W.renderText=q,Object.defineProperty(W,Symbol.toStringTag,{value:"Module"})});
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class m {
|
|
2
2
|
get left() {
|
|
3
3
|
return this.x;
|
|
4
4
|
}
|
|
@@ -11,23 +11,23 @@ class y {
|
|
|
11
11
|
get bottom() {
|
|
12
12
|
return this.y + this.height;
|
|
13
13
|
}
|
|
14
|
-
constructor({ x: t = 0, y:
|
|
15
|
-
this.x = t, this.y =
|
|
14
|
+
constructor({ x: t = 0, y: n = 0, width: l = 0, height: h = 0 } = {}) {
|
|
15
|
+
this.x = t, this.y = n, this.width = l, this.height = h;
|
|
16
16
|
}
|
|
17
17
|
static from(...t) {
|
|
18
|
-
const
|
|
19
|
-
return new
|
|
20
|
-
x:
|
|
21
|
-
y:
|
|
22
|
-
width:
|
|
23
|
-
height:
|
|
18
|
+
const n = t[0], l = t.slice(1).reduce((h, r) => (h.x = Math.min(h.x, r.x), h.y = Math.min(h.y, r.y), h.right = Math.max(h.right, r.right), h.bottom = Math.max(h.bottom, r.bottom), h), { x: n.x, y: n.y, right: n.right, bottom: n.bottom });
|
|
19
|
+
return new m({
|
|
20
|
+
x: l.x,
|
|
21
|
+
y: l.y,
|
|
22
|
+
width: l.right - l.x,
|
|
23
|
+
height: l.bottom - l.y
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
|
-
move(t,
|
|
27
|
-
return this.x += t, this.y +=
|
|
26
|
+
move(t, n) {
|
|
27
|
+
return this.x += t, this.y += n, this;
|
|
28
28
|
}
|
|
29
29
|
clone() {
|
|
30
|
-
return new
|
|
30
|
+
return new m({
|
|
31
31
|
x: this.x,
|
|
32
32
|
y: this.y,
|
|
33
33
|
width: this.width,
|
|
@@ -35,18 +35,18 @@ class y {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
class
|
|
38
|
+
class Y {
|
|
39
39
|
constructor({
|
|
40
40
|
content: t = "",
|
|
41
|
-
style:
|
|
42
|
-
parent:
|
|
43
|
-
contentBox:
|
|
44
|
-
inlineBox:
|
|
45
|
-
glyphBox:
|
|
41
|
+
style: n,
|
|
42
|
+
parent: l,
|
|
43
|
+
contentBox: h = new m(),
|
|
44
|
+
inlineBox: r = new m(),
|
|
45
|
+
glyphBox: g = new m(),
|
|
46
46
|
centerX: f = 0,
|
|
47
|
-
baseline:
|
|
47
|
+
baseline: b = 0
|
|
48
48
|
} = {}) {
|
|
49
|
-
switch (this.content = t, this.style =
|
|
49
|
+
switch (this.content = t, this.style = n, this.parent = l, this.contentBox = h, this.inlineBox = r, this.glyphBox = g, this.centerX = f, this.baseline = b, this.getComputedStyle().textTransform) {
|
|
50
50
|
case "uppercase":
|
|
51
51
|
this.content = this.content.toUpperCase();
|
|
52
52
|
break;
|
|
@@ -63,7 +63,7 @@ class H {
|
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
65
|
clone(t) {
|
|
66
|
-
return new
|
|
66
|
+
return new Y({
|
|
67
67
|
content: this.content,
|
|
68
68
|
style: this.style,
|
|
69
69
|
parent: this.parent,
|
|
@@ -76,22 +76,22 @@ class H {
|
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
-
class
|
|
79
|
+
class W {
|
|
80
80
|
constructor({
|
|
81
81
|
style: t,
|
|
82
|
-
parent:
|
|
83
|
-
contentBox:
|
|
84
|
-
lineBox:
|
|
85
|
-
glyphBox:
|
|
86
|
-
baseline:
|
|
82
|
+
parent: n,
|
|
83
|
+
contentBox: l = new m(),
|
|
84
|
+
lineBox: h = new m(),
|
|
85
|
+
glyphBox: r = new m(),
|
|
86
|
+
baseline: g = 0,
|
|
87
87
|
xHeight: f = 0,
|
|
88
|
-
maxCharWidth:
|
|
89
|
-
fragments:
|
|
88
|
+
maxCharWidth: b = 0,
|
|
89
|
+
fragments: B = []
|
|
90
90
|
} = {}) {
|
|
91
|
-
this.style = t, this.parent =
|
|
91
|
+
this.style = t, this.parent = n, this.contentBox = l, this.lineBox = h, this.glyphBox = r, this.baseline = g, this.xHeight = f, this.maxCharWidth = b, this.fragments = B;
|
|
92
92
|
}
|
|
93
93
|
addFragment(t) {
|
|
94
|
-
return this.fragments.push(new
|
|
94
|
+
return this.fragments.push(new Y({ ...t, parent: this })), this;
|
|
95
95
|
}
|
|
96
96
|
getComputedStyle() {
|
|
97
97
|
return {
|
|
@@ -100,7 +100,7 @@ class M {
|
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
102
|
clone(t) {
|
|
103
|
-
return new
|
|
103
|
+
return new W({
|
|
104
104
|
style: this.style,
|
|
105
105
|
parent: this.parent,
|
|
106
106
|
contentBox: this.contentBox.clone(),
|
|
@@ -109,123 +109,126 @@ class M {
|
|
|
109
109
|
baseline: this.baseline,
|
|
110
110
|
xHeight: this.xHeight,
|
|
111
111
|
maxCharWidth: this.maxCharWidth,
|
|
112
|
-
fragments: this.fragments.map((
|
|
112
|
+
fragments: this.fragments.map((n) => n.clone()),
|
|
113
113
|
...t
|
|
114
114
|
});
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
-
function
|
|
118
|
-
const
|
|
119
|
-
if (typeof
|
|
120
|
-
|
|
117
|
+
function P(a, t) {
|
|
118
|
+
const n = [];
|
|
119
|
+
if (typeof a == "string")
|
|
120
|
+
n.push(new W({ parent: t }).addFragment({ content: a }));
|
|
121
121
|
else {
|
|
122
|
-
|
|
123
|
-
for (const
|
|
124
|
-
if (typeof
|
|
125
|
-
|
|
126
|
-
else if (Array.isArray(
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
if (typeof
|
|
130
|
-
|
|
122
|
+
a = Array.isArray(a) ? a : [a];
|
|
123
|
+
for (const l of a)
|
|
124
|
+
if (typeof l == "string")
|
|
125
|
+
n.push(new W({ parent: t }).addFragment({ content: l }));
|
|
126
|
+
else if (Array.isArray(l)) {
|
|
127
|
+
const h = new W({ parent: t });
|
|
128
|
+
l.forEach((r) => {
|
|
129
|
+
if (typeof r == "string")
|
|
130
|
+
h.addFragment({ content: r });
|
|
131
131
|
else {
|
|
132
|
-
const { content:
|
|
133
|
-
|
|
132
|
+
const { content: g, ...f } = r;
|
|
133
|
+
h.addFragment({ content: g, style: f });
|
|
134
134
|
}
|
|
135
|
-
}),
|
|
136
|
-
} else if ("fragments" in
|
|
137
|
-
const { fragments:
|
|
138
|
-
|
|
139
|
-
const { content:
|
|
140
|
-
|
|
141
|
-
}),
|
|
142
|
-
} else if ("content" in
|
|
143
|
-
const { content:
|
|
144
|
-
|
|
135
|
+
}), n.push(h);
|
|
136
|
+
} else if ("fragments" in l) {
|
|
137
|
+
const { fragments: h, ...r } = l, g = new W({ style: r, parent: t });
|
|
138
|
+
h.forEach((f) => {
|
|
139
|
+
const { content: b, ...B } = f;
|
|
140
|
+
g.addFragment({ content: b, style: B });
|
|
141
|
+
}), n.push(g);
|
|
142
|
+
} else if ("content" in l) {
|
|
143
|
+
const { content: h, ...r } = l;
|
|
144
|
+
n.push(new W({ style: r, parent: t }).addFragment({ content: h }));
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
|
-
return
|
|
147
|
+
return n;
|
|
148
148
|
}
|
|
149
149
|
const D = "OffscreenCanvas" in globalThis;
|
|
150
|
-
let
|
|
151
|
-
function
|
|
152
|
-
return
|
|
150
|
+
let L;
|
|
151
|
+
function N() {
|
|
152
|
+
return L ?? (L = D ? new OffscreenCanvas(1, 1) : document.createElement("canvas"));
|
|
153
153
|
}
|
|
154
|
-
function
|
|
155
|
-
switch (t.shadowColor && (
|
|
154
|
+
function X(a, t) {
|
|
155
|
+
switch (t.shadowColor && (a.shadowColor = t.shadowColor), t.shadowOffsetX !== void 0 && (a.shadowOffsetX = t.shadowOffsetX), t.shadowOffsetY !== void 0 && (a.shadowOffsetY = t.shadowOffsetY), t.shadowBlur !== void 0 && (a.shadowBlur = t.shadowBlur), t.textStrokeColor && (a.strokeStyle = t.textStrokeColor), t.textStrokeWidth !== void 0 && (a.lineWidth = t.textStrokeWidth), t.color && (a.fillStyle = t.color), t.textAlign && (a.textAlign = t.textAlign), t.fontKerning && (a.fontKerning = t.fontKerning), t.verticalAlign) {
|
|
156
156
|
case "baseline":
|
|
157
|
-
|
|
157
|
+
a.textBaseline = "alphabetic";
|
|
158
158
|
break;
|
|
159
159
|
case "top":
|
|
160
160
|
case "middle":
|
|
161
161
|
case "bottom":
|
|
162
|
-
|
|
162
|
+
a.textBaseline = t.verticalAlign;
|
|
163
163
|
break;
|
|
164
164
|
}
|
|
165
|
-
(t.fontStyle || t.fontWeight !== void 0 || t.fontSize !== void 0 || t.fontFamily) && (
|
|
165
|
+
t.letterSpacing !== void 0 && (a.letterSpacing = `${t.letterSpacing}px`), (t.fontStyle || t.fontWeight !== void 0 || t.fontSize !== void 0 || t.fontFamily) && (a.font = [
|
|
166
166
|
t.fontStyle || "normal",
|
|
167
167
|
t.fontWeight || "normal",
|
|
168
168
|
`${t.fontSize || 14}px`,
|
|
169
169
|
t.fontFamily || "sans-serif"
|
|
170
170
|
].join(" "));
|
|
171
171
|
}
|
|
172
|
-
function
|
|
173
|
-
const
|
|
174
|
-
return
|
|
172
|
+
function z(a, t) {
|
|
173
|
+
const n = N().getContext("2d");
|
|
174
|
+
return X(n, t), n.measureText(a);
|
|
175
175
|
}
|
|
176
|
-
const
|
|
177
|
-
function
|
|
178
|
-
const
|
|
179
|
-
let
|
|
180
|
-
for (;
|
|
181
|
-
const f =
|
|
182
|
-
let
|
|
183
|
-
const
|
|
184
|
-
for (;
|
|
185
|
-
const
|
|
186
|
-
let C = "",
|
|
187
|
-
for (const
|
|
188
|
-
if (
|
|
176
|
+
const K = /[\s\n\t\u200B\u200C\u200D\u200E\u200F.,?!:;"'(){}\[\]<>\/\\|~#\$%\*\+=&^,。?!:;“”‘’()【】《》……——]/;
|
|
177
|
+
function G(a, t, n) {
|
|
178
|
+
const l = [], h = a.slice();
|
|
179
|
+
let r, g;
|
|
180
|
+
for (; r = h.shift(); ) {
|
|
181
|
+
const f = r.fragments.slice();
|
|
182
|
+
let b = 0;
|
|
183
|
+
const B = [];
|
|
184
|
+
for (; g = f.shift(); ) {
|
|
185
|
+
const S = g.getComputedStyle();
|
|
186
|
+
let C = "", s = !1, o = 0, i = "";
|
|
187
|
+
for (const u of g.content) {
|
|
188
|
+
if (r.maxCharWidth = Math.max(
|
|
189
|
+
r.maxCharWidth,
|
|
190
|
+
z(u, { ...S, letterSpacing: 0 }).width
|
|
191
|
+
), i += u, K.test(g.content[++o]))
|
|
189
192
|
continue;
|
|
190
|
-
let
|
|
191
|
-
switch (
|
|
193
|
+
let d, x;
|
|
194
|
+
switch (S.writingMode) {
|
|
192
195
|
case "vertical-lr":
|
|
193
196
|
case "vertical-rl":
|
|
194
|
-
|
|
197
|
+
d = n, x = i.length * S.fontSize;
|
|
195
198
|
break;
|
|
196
199
|
case "horizontal-tb":
|
|
197
200
|
default:
|
|
198
|
-
|
|
201
|
+
d = t, x = z(i, { ...S, letterSpacing: 0 }).width;
|
|
199
202
|
break;
|
|
200
203
|
}
|
|
201
|
-
x += i.length *
|
|
202
|
-
const
|
|
203
|
-
if (
|
|
204
|
-
let e =
|
|
205
|
-
!
|
|
206
|
-
|
|
207
|
-
fragments:
|
|
204
|
+
x += i.length * S.letterSpacing;
|
|
205
|
+
const y = /^[\r\n]$/.test(i);
|
|
206
|
+
if (y || S.textWrap === "wrap" && d && b + x > d) {
|
|
207
|
+
let e = y ? C.length + 1 : C.length;
|
|
208
|
+
!b && !e && (C += i, e += i.length), C.length && B.push(g.clone({ content: C })), B.length && (l.push(
|
|
209
|
+
r.clone({
|
|
210
|
+
fragments: B.slice()
|
|
208
211
|
})
|
|
209
|
-
),
|
|
210
|
-
const
|
|
211
|
-
(
|
|
212
|
-
|
|
212
|
+
), B.length = 0);
|
|
213
|
+
const w = g.content.substring(e);
|
|
214
|
+
(w.length || f.length) && h.unshift(
|
|
215
|
+
r.clone({
|
|
213
216
|
maxCharWidth: 0,
|
|
214
|
-
fragments: (
|
|
217
|
+
fragments: (w.length ? [g.clone({ content: w })] : []).concat(f.slice())
|
|
215
218
|
})
|
|
216
|
-
), f.length = 0,
|
|
219
|
+
), f.length = 0, s = !0;
|
|
217
220
|
break;
|
|
218
221
|
} else
|
|
219
|
-
|
|
222
|
+
b += x;
|
|
220
223
|
C += i, i = "";
|
|
221
224
|
}
|
|
222
|
-
|
|
225
|
+
s || B.push(g.clone());
|
|
223
226
|
}
|
|
224
|
-
|
|
227
|
+
B.length && l.push(r.clone({ fragments: B }));
|
|
225
228
|
}
|
|
226
|
-
return
|
|
229
|
+
return l;
|
|
227
230
|
}
|
|
228
|
-
function
|
|
231
|
+
function U(a) {
|
|
229
232
|
return {
|
|
230
233
|
width: 0,
|
|
231
234
|
height: 0,
|
|
@@ -243,7 +246,6 @@ function G(c) {
|
|
|
243
246
|
textDecoration: null,
|
|
244
247
|
textStrokeWidth: 0,
|
|
245
248
|
textStrokeColor: null,
|
|
246
|
-
direction: "inherit",
|
|
247
249
|
lineHeight: 1,
|
|
248
250
|
letterSpacing: 0,
|
|
249
251
|
shadowColor: null,
|
|
@@ -251,68 +253,67 @@ function G(c) {
|
|
|
251
253
|
shadowOffsetY: 0,
|
|
252
254
|
shadowBlur: 0,
|
|
253
255
|
writingMode: "horizontal-tb",
|
|
254
|
-
...
|
|
256
|
+
...a
|
|
255
257
|
};
|
|
256
258
|
}
|
|
257
|
-
function
|
|
258
|
-
const { content: t } =
|
|
259
|
-
let
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
a.forEach((o) => {
|
|
259
|
+
function j(a) {
|
|
260
|
+
const { content: t } = a, { width: n, height: l, ...h } = U(a.style);
|
|
261
|
+
let r = P(t, h);
|
|
262
|
+
r = G(r, n, l);
|
|
263
|
+
let g = r.reduce((o, i) => o + i.maxCharWidth * i.getComputedStyle().lineHeight, 0), f = 0, b = 0;
|
|
264
|
+
r.forEach((o) => {
|
|
264
265
|
const i = [];
|
|
265
|
-
let
|
|
266
|
-
o.fragments.forEach((e,
|
|
267
|
-
const
|
|
268
|
-
switch (
|
|
266
|
+
let u = null, d = f, x = b;
|
|
267
|
+
o.fragments.forEach((e, w) => {
|
|
268
|
+
const c = e.getComputedStyle();
|
|
269
|
+
switch (c.writingMode) {
|
|
269
270
|
case "vertical-rl":
|
|
270
|
-
r = d - r;
|
|
271
271
|
case "vertical-lr": {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
272
|
+
const p = o.maxCharWidth, v = p * c.lineHeight;
|
|
273
|
+
w || (x = 0, c.writingMode === "vertical-rl" && (g -= v, d = g));
|
|
274
|
+
const A = e.content.length, k = A * c.fontSize + (A - 1) * c.letterSpacing;
|
|
275
|
+
e.contentBox.x = d + (v - p) / 2, e.contentBox.y = x, e.contentBox.width = p, e.contentBox.height = k, e.inlineBox.x = d, e.inlineBox.y = x, e.inlineBox.width = v, e.inlineBox.height = k, e.glyphBox.x = d + (v - p) / 2, e.glyphBox.y = x, e.glyphBox.width = p, e.glyphBox.height = k, e.baseline = 0, e.centerX = d + v / 2, x += k + c.letterSpacing;
|
|
275
276
|
break;
|
|
276
277
|
}
|
|
277
278
|
case "horizontal-tb": {
|
|
278
|
-
|
|
279
|
+
w || (d = 0);
|
|
279
280
|
const {
|
|
280
|
-
fontBoundingBoxAscent:
|
|
281
|
-
fontBoundingBoxDescent:
|
|
282
|
-
actualBoundingBoxAscent:
|
|
283
|
-
actualBoundingBoxDescent:
|
|
284
|
-
actualBoundingBoxLeft:
|
|
285
|
-
actualBoundingBoxRight:
|
|
286
|
-
width:
|
|
287
|
-
} =
|
|
288
|
-
...
|
|
281
|
+
fontBoundingBoxAscent: p,
|
|
282
|
+
fontBoundingBoxDescent: v,
|
|
283
|
+
actualBoundingBoxAscent: A,
|
|
284
|
+
actualBoundingBoxDescent: k,
|
|
285
|
+
actualBoundingBoxLeft: E,
|
|
286
|
+
actualBoundingBoxRight: M,
|
|
287
|
+
width: F
|
|
288
|
+
} = z(e.content, {
|
|
289
|
+
...c,
|
|
289
290
|
textAlign: "center",
|
|
290
291
|
verticalAlign: "baseline"
|
|
291
|
-
}),
|
|
292
|
-
e.contentBox.x =
|
|
292
|
+
}), H = c.fontSize, O = H * c.lineHeight, $ = x + (O - (p + v)) / 2 + p;
|
|
293
|
+
e.contentBox.x = d, e.contentBox.y = x + (O - H) / 2, e.contentBox.width = F, e.contentBox.height = H, e.inlineBox.x = d, e.inlineBox.y = x, e.inlineBox.width = F, e.inlineBox.height = O, e.glyphBox.x = d, e.glyphBox.y = $ - A, e.glyphBox.width = E + M, e.glyphBox.height = A + k, e.baseline = $, e.centerX = d + E, i.push(e.contentBox), o.contentBox = m.from(...i), o.contentBox.height < e.contentBox.height && (u = e), d += F;
|
|
293
294
|
break;
|
|
294
295
|
}
|
|
295
296
|
}
|
|
296
|
-
}), o.lineBox =
|
|
297
|
-
const
|
|
298
|
-
...(
|
|
297
|
+
}), o.lineBox = m.from(...o.fragments.map((e) => e.inlineBox)), f += o.lineBox.width, b += o.lineBox.height;
|
|
298
|
+
const y = z("x", {
|
|
299
|
+
...(u ?? o).getComputedStyle(),
|
|
299
300
|
textAlign: "left",
|
|
300
301
|
verticalAlign: "baseline"
|
|
301
302
|
});
|
|
302
|
-
o.xHeight =
|
|
303
|
+
o.xHeight = y.actualBoundingBoxAscent, o.baseline = o.lineBox.y + (o.lineBox.height - (y.fontBoundingBoxAscent + y.fontBoundingBoxDescent)) / 2 + y.fontBoundingBoxAscent;
|
|
303
304
|
});
|
|
304
|
-
const
|
|
305
|
-
...
|
|
306
|
-
new
|
|
307
|
-
), { width:
|
|
308
|
-
|
|
309
|
-
o.contentBox =
|
|
310
|
-
const
|
|
311
|
-
let
|
|
312
|
-
switch (
|
|
305
|
+
const B = m.from(
|
|
306
|
+
...r.map((o) => o.lineBox),
|
|
307
|
+
new m({ width: n, height: l })
|
|
308
|
+
), { width: S, height: C } = B;
|
|
309
|
+
r.forEach((o) => {
|
|
310
|
+
o.contentBox = m.from(...o.fragments.map((i) => i.contentBox)), o.glyphBox = m.from(...o.fragments.map((i) => i.glyphBox)), o.fragments.forEach((i) => {
|
|
311
|
+
const u = i.getComputedStyle(), d = i.inlineBox.x, x = i.inlineBox.y;
|
|
312
|
+
let y = d, e = x;
|
|
313
|
+
switch (u.writingMode) {
|
|
313
314
|
case "vertical-rl":
|
|
314
315
|
case "vertical-lr":
|
|
315
|
-
switch (
|
|
316
|
+
switch (u.textAlign) {
|
|
316
317
|
case "end":
|
|
317
318
|
case "right":
|
|
318
319
|
e += C - o.contentBox.height;
|
|
@@ -323,16 +324,16 @@ function U(c) {
|
|
|
323
324
|
}
|
|
324
325
|
break;
|
|
325
326
|
case "horizontal-tb": {
|
|
326
|
-
switch (
|
|
327
|
+
switch (u.textAlign) {
|
|
327
328
|
case "end":
|
|
328
329
|
case "right":
|
|
329
|
-
|
|
330
|
+
y += S - o.contentBox.width;
|
|
330
331
|
break;
|
|
331
332
|
case "center":
|
|
332
|
-
|
|
333
|
+
y += (S - o.contentBox.width) / 2;
|
|
333
334
|
break;
|
|
334
335
|
}
|
|
335
|
-
switch (
|
|
336
|
+
switch (u.verticalAlign) {
|
|
336
337
|
case "top":
|
|
337
338
|
e += o.lineBox.y - i.inlineBox.y;
|
|
338
339
|
break;
|
|
@@ -362,89 +363,104 @@ function U(c) {
|
|
|
362
363
|
break;
|
|
363
364
|
}
|
|
364
365
|
}
|
|
365
|
-
const
|
|
366
|
-
i.inlineBox.move(
|
|
367
|
-
}), o.contentBox =
|
|
366
|
+
const w = y - d, c = e - x;
|
|
367
|
+
i.inlineBox.move(w, c), i.contentBox.move(w, c), i.glyphBox.move(w, c), i.baseline += c, i.centerX += w;
|
|
368
|
+
}), o.contentBox = m.from(...o.fragments.map((i) => i.contentBox)), o.glyphBox = m.from(...o.fragments.map((i) => i.glyphBox));
|
|
368
369
|
});
|
|
369
|
-
const
|
|
370
|
+
const s = m.from(...r.map((o) => o.contentBox));
|
|
370
371
|
return {
|
|
371
|
-
box:
|
|
372
|
-
contentBox:
|
|
373
|
-
viewBox:
|
|
374
|
-
glyphBox:
|
|
375
|
-
paragraphs:
|
|
372
|
+
box: B,
|
|
373
|
+
contentBox: s,
|
|
374
|
+
viewBox: m.from(B, s),
|
|
375
|
+
glyphBox: m.from(...r.map((o) => o.glyphBox)),
|
|
376
|
+
paragraphs: r
|
|
376
377
|
};
|
|
377
378
|
}
|
|
378
|
-
function
|
|
379
|
+
function R(a, t, n) {
|
|
379
380
|
if (typeof t == "string" && t.startsWith("linear-gradient")) {
|
|
380
|
-
const { x0:
|
|
381
|
-
return f.forEach((
|
|
381
|
+
const { x0: l, y0: h, x1: r, y1: g, stops: f } = I(t, n.left, n.top, n.width, n.height), b = a.createLinearGradient(l, h, r, g);
|
|
382
|
+
return f.forEach((B) => b.addColorStop(B.offset, B.color)), b;
|
|
382
383
|
}
|
|
383
384
|
return t;
|
|
384
385
|
}
|
|
385
|
-
function
|
|
386
|
+
function I(a, t, n, l, h) {
|
|
386
387
|
var o;
|
|
387
|
-
const
|
|
388
|
+
const r = ((o = a.match(/linear-gradient\((.+)\)$/)) == null ? void 0 : o[1]) ?? "", g = r.split(",")[0], f = g.includes("deg") ? g : "0deg", b = r.replace(f, "").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi), S = (Number(f.replace("deg", "")) || 0) * Math.PI / 180, C = l * Math.sin(S), s = h * Math.cos(S);
|
|
388
389
|
return {
|
|
389
|
-
x0: t +
|
|
390
|
-
y0:
|
|
391
|
-
x1: t +
|
|
392
|
-
y1:
|
|
393
|
-
stops: Array.from(
|
|
394
|
-
let
|
|
395
|
-
return
|
|
390
|
+
x0: t + l / 2 - C,
|
|
391
|
+
y0: n + h / 2 + s,
|
|
392
|
+
x1: t + l / 2 + C,
|
|
393
|
+
y1: n + h / 2 - s,
|
|
394
|
+
stops: Array.from(b).map((i) => {
|
|
395
|
+
let u = i[2];
|
|
396
|
+
return u.startsWith("(") ? u = u.split(",").length > 3 ? `rgba${u}` : `rgb${u}` : u = `#${u}`, {
|
|
396
397
|
offset: Number(i[3].replace("%", "")) / 100,
|
|
397
|
-
color:
|
|
398
|
+
color: u
|
|
398
399
|
};
|
|
399
400
|
})
|
|
400
401
|
};
|
|
401
402
|
}
|
|
402
|
-
function
|
|
403
|
+
function T(a, t, n) {
|
|
404
|
+
n != null && n.color && (n.color = R(a, n.color, t)), n != null && n.backgroundColor && (n.backgroundColor = R(a, n.backgroundColor, t)), n != null && n.textStrokeColor && (n.textStrokeColor = R(a, n.textStrokeColor, t));
|
|
405
|
+
}
|
|
406
|
+
function V(a) {
|
|
403
407
|
const {
|
|
404
408
|
view: t = document.createElement("canvas"),
|
|
405
|
-
style:
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
(
|
|
413
|
-
|
|
414
|
-
(S = g.style) != null && S.color && (g.style.color = W(l, g.style.color, g.contentBox)), (k = g.style) != null && k.backgroundColor && (g.style.backgroundColor = W(l, g.style.backgroundColor, g.contentBox)), (v = g.style) != null && v.textStrokeColor && (g.style.textStrokeColor = W(l, g.style.textStrokeColor, g.contentBox)), (A = g.style) != null && A.backgroundColor && (l.fillStyle = g.style.backgroundColor, l.fillRect(g.inlineBox.x, g.inlineBox.y, g.inlineBox.width, g.inlineBox.height));
|
|
409
|
+
style: n,
|
|
410
|
+
draws: l = [{}],
|
|
411
|
+
pixelRatio: h = 1
|
|
412
|
+
} = a, { box: r, viewBox: g, paragraphs: f } = j(a), { x: b, y: B, width: S, height: C } = g, s = t.getContext("2d"), o = -b + S, i = -B + C;
|
|
413
|
+
t.style.width = `${o}px`, t.style.height = `${i}px`, t.dataset.width = String(r.width), t.dataset.height = String(r.height), t.dataset.pixelRatio = String(h), t.width = Math.max(1, Math.floor(o * h)), t.height = Math.max(1, Math.floor(i * h)), s.scale(h, h), s.clearRect(0, 0, t.width, t.height), s.translate(-b, -B);
|
|
414
|
+
const u = { ...n };
|
|
415
|
+
return T(s, new m({ width: S, height: C }), u), f.forEach((d) => {
|
|
416
|
+
T(s, d.contentBox, d.style), d.fragments.forEach((x) => {
|
|
417
|
+
T(s, x.contentBox, x.style);
|
|
415
418
|
});
|
|
416
|
-
}),
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
419
|
+
}), l.forEach((d) => {
|
|
420
|
+
const x = { ...d };
|
|
421
|
+
T(s, new m({ width: S, height: C }), x);
|
|
422
|
+
const y = { ...u, ...x };
|
|
423
|
+
y != null && y.backgroundColor && (s.fillStyle = y.backgroundColor, s.fillRect(0, 0, t.width, t.height)), f.forEach((e) => {
|
|
424
|
+
var w;
|
|
425
|
+
(w = e.style) != null && w.backgroundColor && (s.fillStyle = e.style.backgroundColor, s.fillRect(e.lineBox.x, e.lineBox.y, e.lineBox.width, e.lineBox.height)), e.fragments.forEach((c) => {
|
|
426
|
+
var p;
|
|
427
|
+
(p = c.style) != null && p.backgroundColor && (s.fillStyle = c.style.backgroundColor, s.fillRect(c.inlineBox.x, c.inlineBox.y, c.inlineBox.width, c.inlineBox.height));
|
|
422
428
|
});
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
429
|
+
}), X(s, y), f.forEach((e) => {
|
|
430
|
+
e.style && X(s, e.style), e.fragments.forEach((w) => {
|
|
431
|
+
X(s, {
|
|
432
|
+
...w.style,
|
|
433
|
+
textAlign: "left",
|
|
434
|
+
verticalAlign: "top"
|
|
435
|
+
});
|
|
436
|
+
let { x: c, y: p, width: v, height: A } = w.contentBox;
|
|
437
|
+
x.offsetX && (c += x.offsetX), x.offsetY && (p += x.offsetY);
|
|
438
|
+
const k = w.getComputedStyle();
|
|
439
|
+
switch (k.writingMode) {
|
|
440
|
+
case "vertical-rl":
|
|
441
|
+
case "vertical-lr": {
|
|
442
|
+
let E = 0;
|
|
443
|
+
for (const M of w.content)
|
|
444
|
+
s.fillText(M, c, p + E), k.textStrokeWidth && s.strokeText(M, c, p + E), E += k.fontSize + k.letterSpacing;
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
case "horizontal-tb":
|
|
448
|
+
s.fillText(w.content, c, p), k.textStrokeWidth && s.strokeText(w.content, c, p);
|
|
449
|
+
break;
|
|
431
450
|
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
l.beginPath(), l.moveTo(u, e + g / 2), l.lineTo(u + b, e + g / 2), l.stroke();
|
|
442
|
-
break;
|
|
443
|
-
}
|
|
451
|
+
switch (k.textDecoration) {
|
|
452
|
+
case "underline":
|
|
453
|
+
s.beginPath(), s.moveTo(c, p + A - 2), s.lineTo(c + v, p + A - 2), s.stroke();
|
|
454
|
+
break;
|
|
455
|
+
case "line-through":
|
|
456
|
+
s.beginPath(), s.moveTo(c, p + A / 2), s.lineTo(c + v, p + A / 2), s.stroke();
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
444
460
|
});
|
|
445
461
|
}), t;
|
|
446
462
|
}
|
|
447
463
|
export {
|
|
448
|
-
|
|
449
|
-
|
|
464
|
+
j as measureText,
|
|
465
|
+
V as renderText
|
|
450
466
|
};
|
package/package.json
CHANGED
package/types/render-text.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import type { TextDrawStyle } from './types';
|
|
1
2
|
import type { MeasureTextOptions } from './measure-text';
|
|
2
3
|
export interface RenderTextOptions extends MeasureTextOptions {
|
|
3
4
|
view?: HTMLCanvasElement;
|
|
5
|
+
draws?: Array<Partial<TextDrawStyle & {
|
|
6
|
+
offsetX: number;
|
|
7
|
+
offsetY: number;
|
|
8
|
+
}>>;
|
|
4
9
|
pixelRatio?: number;
|
|
5
10
|
}
|
|
6
11
|
export declare function renderText(options: RenderTextOptions): HTMLCanvasElement;
|
package/types/types.d.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
+
export type WritingMode = 'horizontal-tb' | 'vertical-lr' | 'vertical-rl';
|
|
1
2
|
export type FontWeight = 'normal' | 'bold' | 'lighter' | 'bolder' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
2
3
|
export type FontStyle = 'normal' | 'italic' | 'oblique' | `oblique ${string}`;
|
|
3
4
|
export type FontKerning = 'auto' | 'none' | 'normal';
|
|
4
5
|
export type TextWrap = 'wrap' | 'nowrap';
|
|
5
6
|
export type TextAlign = 'center' | 'end' | 'left' | 'right' | 'start';
|
|
6
7
|
export type VerticalAlign = 'baseline' | 'top' | 'middle' | 'bottom' | 'sub' | 'super' | 'text-top' | 'text-bottom';
|
|
7
|
-
export type TextDecoration = 'underline' | 'line-through';
|
|
8
8
|
export type TextTransform = 'uppercase' | 'lowercase' | 'none';
|
|
9
|
-
export type
|
|
10
|
-
export interface
|
|
11
|
-
|
|
12
|
-
backgroundColor: string | CanvasGradient | CanvasPattern | null;
|
|
9
|
+
export type TextDecoration = 'underline' | 'line-through';
|
|
10
|
+
export interface TextLayoutStyle {
|
|
11
|
+
writingMode: WritingMode;
|
|
13
12
|
fontSize: number;
|
|
14
13
|
fontWeight: FontWeight;
|
|
15
14
|
fontFamily: string;
|
|
@@ -19,17 +18,21 @@ export interface TextStyle {
|
|
|
19
18
|
textAlign: TextAlign;
|
|
20
19
|
verticalAlign: VerticalAlign;
|
|
21
20
|
textTransform: TextTransform;
|
|
22
|
-
textDecoration: TextDecoration | null;
|
|
23
|
-
textStrokeWidth: number;
|
|
24
|
-
textStrokeColor: string | CanvasGradient | CanvasPattern | null;
|
|
25
|
-
direction: 'inherit' | 'ltr' | 'rtl';
|
|
26
21
|
lineHeight: number;
|
|
27
22
|
letterSpacing: number;
|
|
23
|
+
}
|
|
24
|
+
export interface TextDrawStyle {
|
|
25
|
+
color: string | CanvasGradient | CanvasPattern | null;
|
|
26
|
+
backgroundColor: string | CanvasGradient | CanvasPattern | null;
|
|
27
|
+
textStrokeWidth: number;
|
|
28
|
+
textStrokeColor: string | CanvasGradient | CanvasPattern | null;
|
|
29
|
+
textDecoration: TextDecoration | null;
|
|
28
30
|
shadowColor: string | null;
|
|
29
31
|
shadowOffsetX: number;
|
|
30
32
|
shadowOffsetY: number;
|
|
31
33
|
shadowBlur: number;
|
|
32
|
-
|
|
34
|
+
}
|
|
35
|
+
export interface TextStyle extends TextLayoutStyle, TextDrawStyle {
|
|
33
36
|
}
|
|
34
37
|
export interface FragmentContent extends Partial<TextStyle> {
|
|
35
38
|
content: string;
|