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