modern-text 0.1.2 → 0.1.3

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