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