modern-text 0.1.7 → 0.1.8
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 +200 -190
- 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:l=0,height:s=0}={}){this.x=t,this.y=o,this.width=l,this.height=s}static from(...t){const o=t[0],l=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 B({x:l.x,y:l.y,width:l.right-l.x,height:l.bottom-l.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:l,contentBox:s=new B,inlineBox:a=new B,glyphBox:g=new B,centerX:u=0,baseline:p=0}={}){switch(this.content=t,this.style=o,this.parent=l,this.contentBox=s,this.inlineBox=a,this.glyphBox=g,this.centerX=u,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 $({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:o,contentBox:l=new B,lineBox:s=new B,glyphBox:a=new B,baseline:g=0,xHeight:u=0,maxCharWidth:p=0,fragments:m=[]}={}){this.style=t,this.parent=o,this.contentBox=l,this.lineBox=s,this.glyphBox=a,this.baseline=g,this.xHeight=u,this.maxCharWidth=p,this.fragments=m}addFragment(t){return this.fragments.push(new $({...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(o=>o.clone()),...t})}}function K(r,t){const o=[];if(typeof r=="string")o.push(new W({parent:t}).addFragment({content:r}));else{r=Array.isArray(r)?r:[r];for(const l of r)if(typeof l=="string")o.push(new W({parent:t}).addFragment({content:l}));else if(Array.isArray(l)){const s=new W({parent:t});l.forEach(a=>{if(typeof a=="string")s.addFragment({content:a});else{const{content:g,...u}=a;s.addFragment({content:g,style:u})}}),o.push(s)}else if("fragments"in l){const{fragments:s,...a}=l,g=new W({style:a,parent:t});s.forEach(u=>{const{content:p,...m}=u;g.addFragment({content:p,style:m})}),o.push(g)}else if("content"in l){const{content:s,...a}=l;o.push(new W({style:a,parent:t}).addFragment({content:s}))}}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 T(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 l=[],s=r.slice();let a,g;for(;a=s.shift();){const u=a.fragments.slice();let p=0;const m=[];for(;g=u.shift();){const b=g.getComputedStyle();let y="",h=!1,v=0,e="";for(const i of g.content){if(a.maxCharWidth=Math.max(a.maxCharWidth,T(i,{...b,letterSpacing:0}).width),e+=i,V.test(g.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=T(e,{...b,letterSpacing:0}).width;break}c+=e.length*b.letterSpacing;const d=/^[\r\n]$/.test(e);if(d||b.textWrap==="wrap"&&S&&p+c>S){let x=d?y.length+1:y.length;!p&&!x&&(y+=e,x+=e.length),y.length&&m.push(g.clone({content:y})),m.length&&(l.push(a.clone({fragments:m.slice()})),m.length=0);const n=g.content.substring(x);(n.length||u.length)&&s.unshift(a.clone({maxCharWidth:0,fragments:(n.length?[g.clone({content:n})]:[]).concat(u.slice())})),u.length=0,h=!0;break}else p+=c;y+=e,e=""}h||m.push(g.clone())}m.length&&l.push(a.clone({fragments:m}))}return l}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:l,...s}=J(r.style);let a=K(t,s);a=q(a,o,l);let g=a.reduce((e,i)=>e+i.maxCharWidth*i.getComputedStyle().lineHeight,0),u=0,p=0;a.forEach(e=>{const i=[];let S=null,c=u,d=p;e.fragments.forEach((n,C)=>{const f=n.getComputedStyle();switch(f.writingMode){case"vertical-rl":case"vertical-lr":{const w=e.maxCharWidth,k=w*f.lineHeight;C||(d=0,f.writingMode==="vertical-rl"&&(g-=k,c=g));const A=n.content.length,M=A*f.fontSize+(A-1)*f.letterSpacing;n.contentBox.x=c+(k-w)/2,n.contentBox.y=d,n.contentBox.width=w,n.contentBox.height=M,n.inlineBox.x=c,n.inlineBox.y=d,n.inlineBox.width=k,n.inlineBox.height=M,n.glyphBox.x=c+(k-w)/2,n.glyphBox.y=d,n.glyphBox.width=w,n.glyphBox.height=M,n.baseline=0,n.centerX=c+k/2,d+=M+f.letterSpacing;break}case"horizontal-tb":{C||(c=0);const{fontBoundingBoxAscent:w,fontBoundingBoxDescent:k,actualBoundingBoxAscent:A,actualBoundingBoxDescent:M,actualBoundingBoxLeft:O,actualBoundingBoxRight:D,width:z}=T(n.content,{...f,textAlign:"center",verticalAlign:"baseline"}),H=f.fontSize,F=H*f.lineHeight,L=w+k,N=A+M,G=O+D,P=d+(F-L)/2+w;n.contentBox.x=c,n.contentBox.y=d+(F-H)/2,n.contentBox.width=z,n.contentBox.height=H,n.inlineBox.x=c,n.inlineBox.y=d,n.inlineBox.width=z,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+=z;break}}}),e.lineBox=B.from(...e.fragments.map(n=>n.inlineBox)),u+=e.lineBox.width,p+=e.lineBox.height;const x=T("x",{...(S??e).getComputedStyle(),textAlign:"left",verticalAlign:"baseline"});e.xHeight=x.actualBoundingBoxAscent,e.baseline=e.lineBox.y+(e.lineBox.height-(x.fontBoundingBoxAscent+x.fontBoundingBoxDescent))/2+x.fontBoundingBoxAscent});const m=B.from(...a.map(e=>e.lineBox),new B({width:o,height:l})),{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 x=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":x+=b-e.contentBox.width;break;case"center":x+=(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 C=x-c,f=n-d;i.inlineBox.move(C,f),i.contentBox.move(C,f),i.glyphBox.move(C,f),i.baseline+=f,i.centerX+=C}),e.contentBox=B.from(...e.fragments.map(i=>i.contentBox)),e.glyphBox=B.from(...e.fragments.map(i=>i.glyphBox))});const h=B.from(...a.map(e=>e.contentBox)),v=B.from(...a.map(e=>e.glyphBox));return{box:m,contentBox:h,glyphBox:v,paragraphs:a,viewBox:B.from(m,v)}}function X(r,t,o){if(typeof t=="string"&&t.startsWith("linear-gradient")){const{x0:l,y0:s,x1:a,y1:g,stops:u}=Q(t,o.left,o.top,o.width,o.height),p=r.createLinearGradient(l,s,a,g);return u.forEach(m=>p.addColorStop(m.offset,m.color)),p}return t}function Q(r,t,o,l,s){var v;const a=((v=r.match(/linear-gradient\((.+)\)$/))==null?void 0:v[1])??"",g=a.split(",")[0],u=g.includes("deg")?g:"0deg",p=a.replace(u,"").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi),b=(Number(u.replace("deg",""))||0)*Math.PI/180,y=l*Math.sin(b),h=s*Math.cos(b);return{x0:t+l/2-y,y0:o+s/2+h,x1:t+l/2+y,y1:o+s/2-h,stops:Array.from(p).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:l=[],pixelRatio:s=1}=r,a=l.length>0?l:[{}],{viewBox:g,paragraphs:u}=Y(r),{x:p,y:m,width:b,height:y}=g,h=t.getContext("2d");t.style.width=`${b}px`,t.style.height=`${y}px`,t.dataset.viewbox=`${p} ${m} ${b} ${y}`,t.dataset.pixelRatio=String(s),t.width=Math.max(1,Math.floor(b*s)),t.height=Math.max(1,Math.floor(y*s)),h.scale(s,s),h.clearRect(0,0,t.width,t.height);const v={...o};return E(h,new B({width:b,height:y}),v),u.forEach(e=>{E(h,e.contentBox,e.style),e.fragments.forEach(i=>{E(h,i.contentBox,i.style)})}),a.forEach(e=>{const i={...e};E(h,new B({width:b,height:y}),i);const S={...v,...i};S!=null&&S.backgroundColor&&(h.fillStyle=S.backgroundColor,h.fillRect(0,0,t.width,t.height)),u.forEach(c=>{var d;(d=c.style)!=null&&d.backgroundColor&&(h.fillStyle=c.style.backgroundColor,h.fillRect(c.lineBox.x,c.lineBox.y,c.lineBox.width,c.lineBox.height)),c.fragments.forEach(x=>{var n;(n=x.style)!=null&&n.backgroundColor&&(h.fillStyle=x.style.backgroundColor,h.fillRect(x.inlineBox.x,x.inlineBox.y,x.inlineBox.width,x.inlineBox.height))})}),u.forEach(c=>{c.fragments.forEach(d=>{const x={...d.getComputedStyle(),...i};R(h,{...x,textAlign:"left",verticalAlign:"baseline"});const{width:n,height:C}=d.contentBox;let f=-g.x,w=-g.y;switch(x.writingMode){case"vertical-rl":case"vertical-lr":f+=d.contentBox.x,w+=(x.fontSize-c.xHeight)/2+c.xHeight+1;break;case"horizontal-tb":f+=d.contentBox.x,w+=d.baseline;break}switch(i.offsetX&&(f+=i.offsetX),i.offsetY&&(w+=i.offsetY),x.writingMode){case"vertical-rl":case"vertical-lr":{let k=0;for(const A of d.content)h.fillText(A,f,w+k),x.textStrokeWidth&&h.strokeText(A,f,w+k),k+=x.fontSize+x.letterSpacing;break}case"horizontal-tb":h.fillText(d.content,f,w),x.textStrokeWidth&&h.strokeText(d.content,f,w);break}switch(x.textDecoration){case"underline":h.beginPath(),h.moveTo(f,w+C-2),h.lineTo(f+n,w+C-2),h.stroke();break;case"line-through":h.beginPath(),h.moveTo(f,w+C/2),h.lineTo(f+n,w+C/2),h.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(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: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 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:s=new d,inlineBox:a=new d,glyphBox:g=new d,centerX:B=0,baseline:p=0}={}){switch(this.content=t,this.style=o,this.parent=h,this.contentBox=s,this.inlineBox=a,this.glyphBox=g,this.centerX=B,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 H({content:this.content,style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),inlineBox:this.inlineBox.clone(),glyphBox:this.glyphBox.clone(),centerX:this.centerX,baseline:this.baseline,...t})}}class M{constructor({style:t,parent:o,contentBox:h=new d,lineBox:s=new d,glyphBox:a=new d,baseline:g=0,xHeight:B=0,maxCharWidth:p=0,fragments:m=[]}={}){this.style=t,this.parent=o,this.contentBox=h,this.lineBox=s,this.glyphBox=a,this.baseline=g,this.xHeight=B,this.maxCharWidth=p,this.fragments=m}addFragment(t){return this.fragments.push(new H({...t,parent:this})),this}getComputedStyle(){return{...this.parent,...this.style}}clone(t){return new M({style:this.style,parent:this.parent,contentBox:this.contentBox.clone(),lineBox:this.lineBox.clone(),glyphBox:this.glyphBox.clone(),baseline:this.baseline,xHeight:this.xHeight,maxCharWidth:this.maxCharWidth,fragments:this.fragments.map(o=>o.clone()),...t})}}function L(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:g,...B}=a;s.addFragment({content:g,style:B})}}),o.push(s)}else if("fragments"in h){const{fragments:s,...a}=h,g=new M({style:a,parent:t});s.forEach(B=>{const{content:p,...m}=B;g.addFragment({content:p,style:m})}),o.push(g)}else if("content"in h){const{content:s,...a}=h;o.push(new M({style:a,parent:t}).addFragment({content:s}))}}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=[],s=r.slice();let a,g;for(;a=s.shift();){const B=a.fragments.slice();let p=0;const m=[];for(;g=B.shift();){const b=g.getComputedStyle();let y="",l=!1,v=0,e="";for(const i of g.content){if(a.maxCharWidth=Math.max(a.maxCharWidth,E(i,{...b,letterSpacing:0}).width),e+=i,j.test(g.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&&p+c>S){let x=f?y.length+1:y.length;!p&&!x&&(y+=e,x+=e.length),y.length&&m.push(g.clone({content:y})),m.length&&(h.push(a.clone({fragments:m.slice()})),m.length=0);const n=g.content.substring(x);(n.length||B.length)&&s.unshift(a.clone({maxCharWidth:0,fragments:(n.length?[g.clone({content:n})]:[]).concat(B.slice())})),B.length=0,l=!0;break}else p+=c;y+=e,e=""}l||m.push(g.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,...s}=I(r.style);let a=L(t,s);a=U(a,o,h);let g=a.reduce((e,i)=>e+i.maxCharWidth*i.getComputedStyle().lineHeight,0),B=0,p=0;a.forEach(e=>{const i=[];let S=null,c=B,f=p;e.fragments.forEach((n,C)=>{const u=n.getComputedStyle();switch(u.writingMode){case"vertical-rl":case"vertical-lr":{const w=e.maxCharWidth,k=w*u.lineHeight;C||(f=0,u.writingMode==="vertical-rl"&&(g-=k,c=g));const A=n.content.length,T=A*u.fontSize+(A-1)*u.letterSpacing;n.contentBox.x=c+(k-w)/2,n.contentBox.y=f,n.contentBox.width=w,n.contentBox.height=T,n.inlineBox.x=c,n.inlineBox.y=f,n.inlineBox.width=k,n.inlineBox.height=T,n.glyphBox.x=c+(k-w)/2,n.glyphBox.y=f,n.glyphBox.width=w,n.glyphBox.height=T,n.baseline=0,n.centerX=c+k/2,f+=T+u.letterSpacing;break}case"horizontal-tb":{C||(c=0);const{fontBoundingBoxAscent:w,fontBoundingBoxDescent:k,actualBoundingBoxAscent:A,actualBoundingBoxDescent:T,actualBoundingBoxLeft:Y,actualBoundingBoxRight:J,width:X}=E(n.content,{...u,textAlign:"center",verticalAlign:"baseline"}),$=u.fontSize,O=$*u.lineHeight,Q=w+k,Z=A+T,_=Y+J,D=f+(O-Q)/2+w;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,p+=e.lineBox.height;const x=E("x",{...(S??e).getComputedStyle(),textAlign:"left",verticalAlign:"baseline"});e.xHeight=x.actualBoundingBoxAscent,e.baseline=e.lineBox.y+(e.lineBox.height-(x.fontBoundingBoxAscent+x.fontBoundingBoxDescent))/2+x.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 x=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":x+=b-e.contentBox.width;break;case"center":x+=(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 C=x-c,u=n-f;i.inlineBox.move(C,u),i.contentBox.move(C,u),i.glyphBox.move(C,u),i.baseline+=u,i.centerX+=C}),e.contentBox=d.from(...e.fragments.map(i=>i.contentBox)),e.glyphBox=d.from(...e.fragments.map(i=>i.glyphBox))});const l=d.from(...a.map(e=>e.contentBox)),v=d.from(...a.map(e=>e.glyphBox));return{box:m,contentBox:l,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:s,x1:a,y1:g,stops:B}=V(t,o.left,o.top,o.width,o.height),p=r.createLinearGradient(h,s,a,g);return B.forEach(m=>p.addColorStop(m.offset,m.color)),p}return t}function V(r,t,o,h,s){var v;const a=((v=r.match(/linear-gradient\((.+)\)$/))==null?void 0:v[1])??"",g=a.split(",")[0],B=g.includes("deg")?g:"0deg",p=a.replace(B,"").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi),b=(Number(B.replace("deg",""))||0)*Math.PI/180,y=h*Math.sin(b),l=s*Math.cos(b);return{x0:t+h/2-y,y0:o+s/2+l,x1:t+h/2+y,y1:o+s/2-l,stops:Array.from(p).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:s=1}=r,a=h.length>0?h:[{}],{viewBox:g,paragraphs:B}=R(r),{x:p,y:m,width:b,height:y}=g,l=t.getContext("2d");t.style.width=`${b}px`,t.style.height=`${y}px`,t.dataset.viewbox=`${p} ${m} ${b} ${y}`,t.dataset.pixelRatio=String(s),t.width=Math.max(1,Math.floor(b*s)),t.height=Math.max(1,Math.floor(y*s)),l.scale(s,s),l.clearRect(0,0,t.width,t.height);const v={...o};return z(l,new d({width:b,height:y}),v),B.forEach(e=>{z(l,e.contentBox,e.style),e.fragments.forEach(i=>{z(l,i.contentBox,i.style)})}),a.forEach(e=>{const i={...e};z(l,new d({width:b,height:y}),i);const S={...v,...i};S!=null&&S.backgroundColor&&(l.fillStyle=S.backgroundColor,l.fillRect(0,0,t.width,t.height)),B.forEach(c=>{var f;(f=c.style)!=null&&f.backgroundColor&&(l.fillStyle=c.style.backgroundColor,l.fillRect(c.lineBox.x,c.lineBox.y,c.lineBox.width,c.lineBox.height)),c.fragments.forEach(x=>{var n;(n=x.style)!=null&&n.backgroundColor&&(l.fillStyle=x.style.backgroundColor,l.fillRect(x.inlineBox.x,x.inlineBox.y,x.inlineBox.width,x.inlineBox.height))})}),B.forEach(c=>{c.fragments.forEach(f=>{const x={...f.getComputedStyle(),...i};P(l,{...x,textAlign:"left",verticalAlign:"baseline"});const{width:n,height:C}=f.contentBox;let u=-g.x,w=-g.y;switch(x.writingMode){case"vertical-rl":case"vertical-lr":u+=f.contentBox.x,w+=(x.fontSize-c.xHeight)/2+c.xHeight+1;break;case"horizontal-tb":u+=f.contentBox.x,w+=f.baseline;break}switch(i.offsetX&&(u+=i.offsetX),i.offsetY&&(w+=i.offsetY),x.writingMode){case"vertical-rl":case"vertical-lr":{let k=0;for(const A of f.content)l.fillText(A,u,w+k),x.textStrokeWidth&&l.strokeText(A,u,w+k),k+=x.fontSize+x.letterSpacing;break}case"horizontal-tb":l.fillText(f.content,u,w),x.textStrokeWidth&&l.strokeText(f.content,u,w);break}switch(x.textDecoration){case"underline":l.beginPath(),l.moveTo(u,w+C-2),l.lineTo(u+n,w+C-2),l.stroke();break;case"line-through":l.beginPath(),l.moveTo(u,w+C/2),l.lineTo(u+n,w+C/2),l.stroke();break}})})}),t}W.measureText=R,W.renderText=q,Object.defineProperty(W,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: s = 0 } = {}) {
|
|
15
|
+
this.x = t, this.y = o, this.width = h, this.height = s;
|
|
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((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 });
|
|
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:
|
|
42
|
+
parent: h,
|
|
43
|
+
contentBox: s = new u(),
|
|
44
|
+
inlineBox: a = new u(),
|
|
45
|
+
glyphBox: g = new u(),
|
|
46
|
+
centerX: B = 0,
|
|
47
47
|
baseline: p = 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 = s, this.inlineBox = a, this.glyphBox = g, this.centerX = B, 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 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 W {
|
|
80
80
|
constructor({
|
|
81
81
|
style: t,
|
|
82
82
|
parent: o,
|
|
83
|
-
contentBox:
|
|
84
|
-
lineBox:
|
|
85
|
-
glyphBox: a = new
|
|
86
|
-
baseline:
|
|
87
|
-
xHeight:
|
|
83
|
+
contentBox: h = new u(),
|
|
84
|
+
lineBox: s = new u(),
|
|
85
|
+
glyphBox: a = new u(),
|
|
86
|
+
baseline: g = 0,
|
|
87
|
+
xHeight: B = 0,
|
|
88
88
|
maxCharWidth: p = 0,
|
|
89
|
-
fragments:
|
|
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 = s, this.glyphBox = a, this.baseline = g, this.xHeight = B, this.maxCharWidth = p, 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 W({
|
|
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 W({ 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 W({ parent: t }).addFragment({ content: h }));
|
|
126
|
+
else if (Array.isArray(h)) {
|
|
127
|
+
const s = new W({ parent: t });
|
|
128
|
+
h.forEach((a) => {
|
|
129
129
|
if (typeof a == "string")
|
|
130
|
-
|
|
130
|
+
s.addFragment({ content: a });
|
|
131
131
|
else {
|
|
132
|
-
const { content:
|
|
133
|
-
|
|
132
|
+
const { content: g, ...B } = a;
|
|
133
|
+
s.addFragment({ content: g, style: B });
|
|
134
134
|
}
|
|
135
|
-
}), o.push(
|
|
136
|
-
} else if ("fragments" in
|
|
137
|
-
const { fragments:
|
|
138
|
-
|
|
139
|
-
const { content: p, ...
|
|
140
|
-
|
|
141
|
-
}), o.push(
|
|
142
|
-
} else if ("content" in
|
|
143
|
-
const { content:
|
|
144
|
-
o.push(new
|
|
135
|
+
}), o.push(s);
|
|
136
|
+
} else if ("fragments" in h) {
|
|
137
|
+
const { fragments: s, ...a } = h, g = new W({ style: a, parent: t });
|
|
138
|
+
s.forEach((B) => {
|
|
139
|
+
const { content: p, ...m } = B;
|
|
140
|
+
g.addFragment({ content: p, style: m });
|
|
141
|
+
}), o.push(g);
|
|
142
|
+
} else if ("content" in h) {
|
|
143
|
+
const { content: s, ...a } = h;
|
|
144
|
+
o.push(new W({ style: a, parent: t }).addFragment({ content: s }));
|
|
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
|
|
176
|
+
const I = /[\s\n\t\u200B\u200C\u200D\u200E\u200F.,?!:;"'(){}\[\]<>\/\\|~#\$%\*\+=&^,。?!:;“”‘’()【】《》……——]/;
|
|
177
|
+
function V(r, t, o) {
|
|
178
|
+
const h = [], s = r.slice();
|
|
179
|
+
let a, g;
|
|
180
|
+
for (; a = s.shift(); ) {
|
|
181
|
+
const B = a.fragments.slice();
|
|
182
182
|
let p = 0;
|
|
183
|
-
const
|
|
184
|
-
for (;
|
|
185
|
-
const
|
|
186
|
-
let
|
|
187
|
-
for (const
|
|
183
|
+
const m = [];
|
|
184
|
+
for (; g = B.shift(); ) {
|
|
185
|
+
const b = g.getComputedStyle();
|
|
186
|
+
let y = "", l = !1, v = 0, e = "";
|
|
187
|
+
for (const i of g.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(g.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
|
-
!p && !
|
|
204
|
+
c += e.length * b.letterSpacing;
|
|
205
|
+
const f = /^[\r\n]$/.test(e);
|
|
206
|
+
if (f || b.textWrap === "wrap" && S && p + c > S) {
|
|
207
|
+
let x = f ? y.length + 1 : y.length;
|
|
208
|
+
!p && !x && (y += e, x += e.length), y.length && m.push(g.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 = g.content.substring(x);
|
|
214
|
+
(n.length || B.length) && s.unshift(
|
|
215
215
|
a.clone({
|
|
216
216
|
maxCharWidth: 0,
|
|
217
|
-
fragments: (
|
|
217
|
+
fragments: (n.length ? [g.clone({ content: n })] : []).concat(B.slice())
|
|
218
218
|
})
|
|
219
|
-
),
|
|
219
|
+
), B.length = 0, l = !0;
|
|
220
220
|
break;
|
|
221
221
|
} else
|
|
222
222
|
p += c;
|
|
223
|
-
|
|
223
|
+
y += e, e = "";
|
|
224
224
|
}
|
|
225
|
-
|
|
225
|
+
l || m.push(g.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, ...s } = q(r.style);
|
|
261
|
+
let a = G(t, s);
|
|
262
|
+
a = V(a, o, h);
|
|
263
|
+
let g = a.reduce((e, i) => e + i.maxCharWidth * i.getComputedStyle().lineHeight, 0), B = 0, p = 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 = p;
|
|
267
|
+
e.fragments.forEach((n, C) => {
|
|
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 w = e.maxCharWidth, k = w * d.lineHeight;
|
|
273
|
+
C || (f = 0, d.writingMode === "vertical-rl" && (g -= k, c = g));
|
|
274
|
+
const A = n.content.length, M = A * d.fontSize + (A - 1) * d.letterSpacing;
|
|
275
|
+
n.contentBox.x = c + (k - w) / 2, n.contentBox.y = f, n.contentBox.width = w, n.contentBox.height = M, n.inlineBox.x = c, n.inlineBox.y = f, n.inlineBox.width = k, n.inlineBox.height = M, n.glyphBox.x = c + (k - w) / 2, n.glyphBox.y = f, n.glyphBox.width = w, n.glyphBox.height = M, n.baseline = 0, n.centerX = c + k / 2, f += M + d.letterSpacing;
|
|
276
276
|
break;
|
|
277
277
|
}
|
|
278
278
|
case "horizontal-tb": {
|
|
279
|
-
|
|
279
|
+
C || (c = 0);
|
|
280
280
|
const {
|
|
281
|
-
fontBoundingBoxAscent:
|
|
282
|
-
fontBoundingBoxDescent:
|
|
283
|
-
actualBoundingBoxAscent:
|
|
284
|
-
actualBoundingBoxDescent:
|
|
285
|
-
actualBoundingBoxLeft:
|
|
286
|
-
actualBoundingBoxRight:
|
|
287
|
-
width:
|
|
281
|
+
fontBoundingBoxAscent: w,
|
|
282
|
+
fontBoundingBoxDescent: k,
|
|
283
|
+
actualBoundingBoxAscent: A,
|
|
284
|
+
actualBoundingBoxDescent: M,
|
|
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 = w + k, L = A + M, N = O + D, R = f + (T - P) / 2 + w;
|
|
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, p += e.lineBox.height;
|
|
298
|
+
const x = z("x", {
|
|
299
|
+
...(S ?? e).getComputedStyle(),
|
|
300
300
|
textAlign: "left",
|
|
301
301
|
verticalAlign: "baseline"
|
|
302
302
|
});
|
|
303
|
-
e.xHeight =
|
|
303
|
+
e.xHeight = x.actualBoundingBoxAscent, e.baseline = e.lineBox.y + (e.lineBox.height - (x.fontBoundingBoxAscent + x.fontBoundingBoxDescent)) / 2 + x.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 x = 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
|
+
x += b - e.contentBox.width;
|
|
331
331
|
break;
|
|
332
332
|
case "center":
|
|
333
|
-
|
|
333
|
+
x += (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,106 @@ function j(r) {
|
|
|
363
363
|
break;
|
|
364
364
|
}
|
|
365
365
|
}
|
|
366
|
-
const
|
|
367
|
-
i.inlineBox.move(
|
|
368
|
-
}), e.contentBox =
|
|
366
|
+
const C = x - c, d = n - f;
|
|
367
|
+
i.inlineBox.move(C, d), i.contentBox.move(C, d), i.glyphBox.move(C, d), i.baseline += d, i.centerX += C;
|
|
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 l = 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: l,
|
|
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: s, x1: a, y1: g, stops: B } = Q(t, o.left, o.top, o.width, o.height), p = r.createLinearGradient(h, s, a, g);
|
|
382
|
+
return B.forEach((m) => p.addColorStop(m.offset, m.color)), p;
|
|
383
383
|
}
|
|
384
384
|
return t;
|
|
385
385
|
}
|
|
386
|
-
function
|
|
387
|
-
var
|
|
388
|
-
const a = ((
|
|
386
|
+
function Q(r, t, o, h, s) {
|
|
387
|
+
var v;
|
|
388
|
+
const a = ((v = r.match(/linear-gradient\((.+)\)$/)) == null ? void 0 : v[1]) ?? "", g = a.split(",")[0], B = g.includes("deg") ? g : "0deg", p = a.replace(B, "").matchAll(/(#|rgba|rgb)(.+?) ([\d.]+?%)/gi), b = (Number(B.replace("deg", "")) || 0) * Math.PI / 180, y = h * Math.sin(b), l = s * Math.cos(b);
|
|
389
389
|
return {
|
|
390
|
-
x0: t +
|
|
391
|
-
y0: o +
|
|
392
|
-
x1: t +
|
|
393
|
-
y1: o +
|
|
394
|
-
stops: Array.from(p).map((
|
|
395
|
-
let
|
|
396
|
-
return
|
|
397
|
-
offset: Number(
|
|
398
|
-
color:
|
|
390
|
+
x0: t + h / 2 - y,
|
|
391
|
+
y0: o + s / 2 + l,
|
|
392
|
+
x1: t + h / 2 + y,
|
|
393
|
+
y1: o + s / 2 - l,
|
|
394
|
+
stops: Array.from(p).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: s = 1
|
|
412
|
+
} = r, a = h.length > 0 ? h : [{}], { viewBox: g, paragraphs: B } = J(r), { x: p, y: m, width: b, height: y } = g, l = t.getContext("2d");
|
|
413
|
+
t.style.width = `${b}px`, t.style.height = `${y}px`, t.dataset.viewbox = `${p} ${m} ${b} ${y}`, t.dataset.pixelRatio = String(s), t.width = Math.max(1, Math.floor(b * s)), t.height = Math.max(1, Math.floor(y * s)), l.scale(s, s), l.clearRect(0, 0, t.width, t.height);
|
|
414
|
+
const v = { ...o };
|
|
415
|
+
return E(l, new u({ width: b, height: y }), v), B.forEach((e) => {
|
|
416
|
+
E(l, e.contentBox, e.style), e.fragments.forEach((i) => {
|
|
417
|
+
E(l, 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(l, new u({ width: b, height: y }), i);
|
|
422
|
+
const S = { ...v, ...i };
|
|
423
|
+
S != null && S.backgroundColor && (l.fillStyle = S.backgroundColor, l.fillRect(0, 0, t.width, t.height)), B.forEach((c) => {
|
|
424
|
+
var f;
|
|
425
|
+
(f = c.style) != null && f.backgroundColor && (l.fillStyle = c.style.backgroundColor, l.fillRect(c.lineBox.x, c.lineBox.y, c.lineBox.width, c.lineBox.height)), c.fragments.forEach((x) => {
|
|
426
|
+
var n;
|
|
427
|
+
(n = x.style) != null && n.backgroundColor && (l.fillStyle = x.style.backgroundColor, l.fillRect(x.inlineBox.x, x.inlineBox.y, x.inlineBox.width, x.inlineBox.height));
|
|
428
428
|
});
|
|
429
|
-
}),
|
|
430
|
-
|
|
431
|
-
const x = { ...
|
|
432
|
-
|
|
429
|
+
}), B.forEach((c) => {
|
|
430
|
+
c.fragments.forEach((f) => {
|
|
431
|
+
const x = { ...f.getComputedStyle(), ...i };
|
|
432
|
+
Y(l, {
|
|
433
433
|
...x,
|
|
434
434
|
textAlign: "left",
|
|
435
|
-
verticalAlign: "
|
|
435
|
+
verticalAlign: "baseline"
|
|
436
436
|
});
|
|
437
|
-
|
|
438
|
-
|
|
437
|
+
const { width: n, height: C } = f.contentBox;
|
|
438
|
+
let d = -g.x, w = -g.y;
|
|
439
|
+
switch (x.writingMode) {
|
|
440
|
+
case "vertical-rl":
|
|
441
|
+
case "vertical-lr":
|
|
442
|
+
d += f.contentBox.x, w += (x.fontSize - c.xHeight) / 2 + c.xHeight + 1;
|
|
443
|
+
break;
|
|
444
|
+
case "horizontal-tb":
|
|
445
|
+
d += f.contentBox.x, w += f.baseline;
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
switch (i.offsetX && (d += i.offsetX), i.offsetY && (w += i.offsetY), x.writingMode) {
|
|
439
449
|
case "vertical-rl":
|
|
440
450
|
case "vertical-lr": {
|
|
441
|
-
let
|
|
442
|
-
for (const
|
|
443
|
-
|
|
451
|
+
let k = 0;
|
|
452
|
+
for (const A of f.content)
|
|
453
|
+
l.fillText(A, d, w + k), x.textStrokeWidth && l.strokeText(A, d, w + k), k += x.fontSize + x.letterSpacing;
|
|
444
454
|
break;
|
|
445
455
|
}
|
|
446
456
|
case "horizontal-tb":
|
|
447
|
-
|
|
457
|
+
l.fillText(f.content, d, w), x.textStrokeWidth && l.strokeText(f.content, d, w);
|
|
448
458
|
break;
|
|
449
459
|
}
|
|
450
460
|
switch (x.textDecoration) {
|
|
451
461
|
case "underline":
|
|
452
|
-
|
|
462
|
+
l.beginPath(), l.moveTo(d, w + C - 2), l.lineTo(d + n, w + C - 2), l.stroke();
|
|
453
463
|
break;
|
|
454
464
|
case "line-through":
|
|
455
|
-
|
|
465
|
+
l.beginPath(), l.moveTo(d, w + C / 2), l.lineTo(d + n, w + C / 2), l.stroke();
|
|
456
466
|
break;
|
|
457
467
|
}
|
|
458
468
|
});
|
|
@@ -460,6 +470,6 @@ function V(r) {
|
|
|
460
470
|
}), t;
|
|
461
471
|
}
|
|
462
472
|
export {
|
|
463
|
-
|
|
464
|
-
|
|
473
|
+
J as measureText,
|
|
474
|
+
Z as renderText
|
|
465
475
|
};
|
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
|
};
|