modern-text 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class p{static get defaultStyle(){return{width:"auto",height:"auto",color:"#000000",fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",textBaseline:"middle",textDecoration:null,textStrokeWidth:0,textStrokeColor:"#000000",direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:"#000000",shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0}}constructor(a={}){const{view:t=document.createElement("canvas"),pixelRatio:i=window.devicePixelRatio||1,data:o="",style:n}=a;this.view=t,this.context=t.getContext("2d"),this.pixelRatio=i,this.data=o,this.style={...p.defaultStyle,...n},this.update()}measure(a=0,t=0){let i=this._createParagraphs(this.data);i=this._createWrapedParagraphs(i,a);const o=this.context;let n=0;for(let h=i.length,r=0;r<h;r++){const e=i[r];e.relativeX=0,e.relativeY=n,e.width=0,e.height=0;let s=0,u;for(const c of e.fragments){const f=c.style;this._setContextStyle(f);const d=o.measureText(c.data),g=d.width,x=d.actualBoundingBoxRight+d.actualBoundingBoxLeft;c.relativeX=s,c.relativeY=e.relativeY,c.width=g,c.actualBoundingBoxWidth=x,c.height=f.fontSize*f.lineHeight,s+=g+f.letterSpacing,u=c,e.height=Math.max(e.height,c.height)}e.width=u?u.relativeX+Math.max(u.width,u.actualBoundingBoxWidth):0,n+=e.height}const l=i.reduce((h,r)=>(h.x=Math.min(h.x,r.relativeX),h.y=Math.min(h.y,r.relativeY),h.width=Math.max(h.width,r.width),h.height+=r.height,h),{x:0,y:0,width:0,height:0});a=a||l.width,t=Math.max(t,l.height);for(let h=i.length,r=0;r<h;r++){const e=i[r];switch(this.style.textAlign){case"center":e.absoluteX=(a-l.width)/2,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX+s.width/2});break;case"end":case"right":e.absoluteX=a-l.width,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX+s.width});break;case"start":case"left":default:e.absoluteX=0,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":e.absoluteY=e.relativeY,e.fragments.forEach(s=>{s.absoluteY=e.absoluteY,s.fillY=s.absoluteY});break;case"middle":case"alphabetic":case"ideographic":e.absoluteY=e.relativeY+(t-l.height)/2,e.fragments.forEach(s=>{s.absoluteY=e.absoluteY,s.fillY=s.absoluteY+e.height/2});break;case"bottom":e.absoluteY=e.relativeY+t-l.height,e.fragments.forEach(s=>{s.absoluteY=e.absoluteY,s.fillY=s.absoluteY+e.height});break}}return{...l,paragraphs:i}}_createParagraphs(a){const t={width:0,height:0,actualBoundingBoxWidth:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},i=(l={})=>({...t,fragments:[],...l}),o=(l={})=>{const h=[],{width:r,height:e,...s}=this.style;return h.push({fillX:0,fillY:0,...t,...l,style:{...s,...l.style},data:l.data??""}),h},n=[];if(typeof a=="string")n.push(i({fragments:o({data:a})}));else{a=Array.isArray(a)?a:[a];for(const l of a){const h=i();if("fragments"in l){const{fragments:r,...e}=l;for(const s of r){const{data:u,...c}=s;h.fragments.push(...o({data:u,style:{...e,...c}}))}}else if("data"in l){const{data:r,...e}=l;h.fragments.push(...o({data:r,style:e}))}n.push(h)}}return n}_createWrapedParagraphs(a,t){const i=[],o=a.slice();let n,l;for(;n=o.shift();){const h=n.fragments.slice();let r=0;const e=[];let s=!0;for(;l=h.shift();){const u=l.style;this._setContextStyle(u);let c="",f=!1;for(const d of l.data){const g=this.context.measureText(d).width+(s?0:u.letterSpacing),x=/^[\r\n]$/.test(d);if(x||u.textWrap==="wrap"&&t&&r+g>t){let w=x?c.length+1:c.length;!r&&!w&&(c+=d,w++),c.length&&e.push({...l,text:c}),e.length&&(i.push({...n,fragments:e.slice()}),e.length=0);const b=l.data.substring(w);(b.length||h.length)&&o.unshift({...n,fragments:(b.length?[{...l,data:b}]:[]).concat(h.slice())}),h.length=0,f=!0;break}else r+=g;c+=d}f||e.push({...l}),s=!1}e.length&&i.push({...n,fragments:e})}return i}_draw(a){const t=this.context;a.forEach(i=>{i.fragments.forEach(o=>{const n=o.style;switch(this._setContextStyle(n),n.textStrokeWidth&&t.strokeText(o.data,o.fillX,o.fillY),t.fillText(o.data,o.fillX,o.fillY),n.textDecoration){case"underline":t.beginPath(),t.moveTo(o.absoluteX,i.absoluteY+i.height-2),t.lineTo(o.absoluteX+o.width,i.absoluteY+i.height-2),t.stroke();break;case"line-through":t.beginPath(),t.moveTo(o.absoluteX,i.absoluteY+i.height/2),t.lineTo(o.absoluteX+o.width,i.absoluteY+i.height/2),t.stroke();break}})})}_resizeView(a,t){const i=this.view;i.style.width=`${a}px`,i.style.height=`${t}px`,i.dataset.width=String(a),i.dataset.height=String(t),i.width=Math.max(1,Math.floor(a*this.pixelRatio)),i.height=Math.max(1,Math.floor(t*this.pixelRatio))}_setContextStyle(a){const t=this.context;t.shadowColor=a.shadowColor,t.shadowOffsetX=a.shadowOffsetX,t.shadowOffsetY=a.shadowOffsetY,t.shadowBlur=a.shadowBlur,t.strokeStyle=a.textStrokeColor,t.lineWidth=a.textStrokeWidth,t.fillStyle=a.color,t.direction=a.direction,t.textAlign=a.textAlign,t.textBaseline=a.textBaseline,t.font=[a.fontStyle,a.fontWeight,`${a.fontSize}px`,a.fontFamily].join(" "),t.fontKerning=a.fontKerning,t.letterSpacing=`${a.letterSpacing}px`}update(){const a=this.context;let{width:t,height:i}=this.style;t==="auto"&&(t=0),i==="auto"&&(i=0);const o=this.measure(t,i);t||(t=o.width),i=Math.max(i,o.height),this._resizeView(t,i);const n=this.pixelRatio;a.scale(n,n),a.clearRect(0,0,a.canvas.width,a.canvas.height),this._draw(o.paragraphs)}}exports.Text=p;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class p{static get defaultStyle(){return{width:"auto",height:"auto",color:"#000000",fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",textBaseline:"middle",textDecoration:null,textStrokeWidth:0,textStrokeColor:"#000000",direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:"#000000",shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0}}constructor(i={}){const{view:t=document.createElement("canvas"),pixelRatio:a=window.devicePixelRatio||1,data:o="",style:n}=i;this.view=t,this.context=t.getContext("2d"),this.pixelRatio=a,this.data=o,this.style={...p.defaultStyle,...n},this.update()}measure(i=0,t=0){let a=this._createParagraphs(this.data);a=this._createWrapedParagraphs(a,i);const o=this.context;let n=0;for(let h=a.length,r=0;r<h;r++){const e=a[r];e.relativeX=0,e.relativeY=n,e.width=0,e.height=0;let u=0,s;for(const c of e.fragments){const f=c.style;this._setContextStyle(f);const d=o.measureText(c.data),g=d.width,x=d.actualBoundingBoxRight+d.actualBoundingBoxLeft;c.relativeX=u,c.relativeY=e.relativeY,c.width=g,c.actualBoundingBoxWidth=x,c.data.match(/^\s$/)?c.height=0:c.height=f.fontSize*f.lineHeight,u+=g+f.letterSpacing,s=c,e.height=Math.max(e.height,c.height)}e.width=s?s.relativeX+Math.max(s.width,s.actualBoundingBoxWidth):0,n+=e.height}const l=a.reduce((h,r)=>(h.x=Math.min(h.x,r.relativeX),h.y=Math.min(h.y,r.relativeY),h.width=Math.max(h.width,r.width),h.height+=r.height,h),{x:0,y:0,width:0,height:0});i=i||l.width,t=Math.max(t,l.height);for(let h=a.length,r=0;r<h;r++){const e=a[r];switch(this.style.textAlign){case"center":e.absoluteX=(i-l.width)/2,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX+s.width/2});break;case"end":case"right":e.absoluteX=i-l.width,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX+s.width});break;case"start":case"left":default:e.absoluteX=0,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX});break}const u=t-l.height;switch(this.style.textBaseline){case"bottom":e.absoluteY=e.relativeY+u,e.fragments.forEach(s=>{s.absoluteY=e.absoluteY+(e.height-s.height),s.fillY=s.absoluteY+s.height});break;case"middle":case"alphabetic":case"ideographic":e.absoluteY=e.relativeY+u/2,e.fragments.forEach(s=>{s.absoluteY=e.absoluteY+(e.height-s.height)/2,s.fillY=s.absoluteY+s.height/2});break;case"top":case"hanging":default:e.absoluteY=e.relativeY,e.fragments.forEach(s=>s.fillY=s.absoluteY=e.absoluteY);break}}return{...l,paragraphs:a}}_createParagraphs(i){const t={width:0,height:0,actualBoundingBoxWidth:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},a=(l={})=>({...t,fragments:[],...l}),o=(l={})=>{const h=[],{width:r,height:e,...u}=this.style;return h.push({fillX:0,fillY:0,...t,...l,style:{...u,...l.style},data:l.data??""}),h},n=[];if(typeof i=="string")n.push(a({fragments:o({data:i})}));else{i=Array.isArray(i)?i:[i];for(const l of i){const h=a();if("fragments"in l){const{fragments:r,...e}=l;for(const u of r){const{data:s,...c}=u;h.fragments.push(...o({data:s,style:{...e,...c}}))}}else if("data"in l){const{data:r,...e}=l;h.fragments.push(...o({data:r,style:e}))}n.push(h)}}return n}_createWrapedParagraphs(i,t){const a=[],o=i.slice();let n,l;for(;n=o.shift();){const h=n.fragments.slice();let r=0;const e=[];let u=!0;for(;l=h.shift();){const s=l.style;this._setContextStyle(s);let c="",f=!1;for(const d of l.data){const g=this.context.measureText(d).width+(u?0:s.letterSpacing),x=/^[\r\n]$/.test(d);if(x||s.textWrap==="wrap"&&t&&r+g>t){let b=x?c.length+1:c.length;!r&&!b&&(c+=d,b++),c.length&&e.push({...l,text:c}),e.length&&(a.push({...n,fragments:e.slice()}),e.length=0);const w=l.data.substring(b);(w.length||h.length)&&o.unshift({...n,fragments:(w.length?[{...l,data:w}]:[]).concat(h.slice())}),h.length=0,f=!0;break}else r+=g;c+=d}f||e.push({...l}),u=!1}e.length&&a.push({...n,fragments:e})}return a}_draw(i){const t=this.context;this.style.backgroundColor&&(t.fillStyle=this.style.backgroundColor,t.fillRect(0,0,t.canvas.width,t.canvas.height)),i.forEach(a=>{a.fragments.forEach(o=>{const n=o.style;switch(n.backgroundColor&&(t.fillStyle=n.backgroundColor,t.fillRect(o.absoluteX,o.absoluteY,o.width,o.height)),this._setContextStyle(n),n.textStrokeWidth&&t.strokeText(o.data,o.fillX,o.fillY),t.fillText(o.data,o.fillX,o.fillY),n.textDecoration){case"underline":t.beginPath(),t.moveTo(o.absoluteX,a.absoluteY+a.height-2),t.lineTo(o.absoluteX+o.width,a.absoluteY+a.height-2),t.stroke();break;case"line-through":t.beginPath(),t.moveTo(o.absoluteX,a.absoluteY+a.height/2),t.lineTo(o.absoluteX+o.width,a.absoluteY+a.height/2),t.stroke();break}})})}_resizeView(i,t){const a=this.view;a.style.width=`${i}px`,a.style.height=`${t}px`,a.dataset.width=String(i),a.dataset.height=String(t),a.width=Math.max(1,Math.floor(i*this.pixelRatio)),a.height=Math.max(1,Math.floor(t*this.pixelRatio))}_setContextStyle(i){const t=this.context;t.shadowColor=i.shadowColor,t.shadowOffsetX=i.shadowOffsetX,t.shadowOffsetY=i.shadowOffsetY,t.shadowBlur=i.shadowBlur,t.strokeStyle=i.textStrokeColor,t.lineWidth=i.textStrokeWidth,t.fillStyle=i.color,t.direction=i.direction,t.textAlign=i.textAlign,t.textBaseline=i.textBaseline,t.font=[i.fontStyle,i.fontWeight,`${i.fontSize}px`,i.fontFamily].join(" "),t.fontKerning=i.fontKerning,t.letterSpacing=`${i.letterSpacing}px`}update(){const i=this.context;let{width:t,height:a}=this.style;t==="auto"&&(t=0),a==="auto"&&(a=0);const o=this.measure(t,a);t||(t=o.width),a=Math.max(a,o.height),this._resizeView(t,a);const n=this.pixelRatio;i.scale(n,n),i.clearRect(0,0,i.canvas.width,i.canvas.height),this._draw(o.paragraphs)}}exports.Text=p;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- (function(x,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(x=typeof globalThis<"u"?globalThis:x||self,f(x.modernText={}))})(this,function(x){"use strict";class f{static get defaultStyle(){return{width:"auto",height:"auto",color:"#000000",fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",textBaseline:"middle",textDecoration:null,textStrokeWidth:0,textStrokeColor:"#000000",direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:"#000000",shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0}}constructor(i={}){const{view:t=document.createElement("canvas"),pixelRatio:s=window.devicePixelRatio||1,data:o="",style:n}=i;this.view=t,this.context=t.getContext("2d"),this.pixelRatio=s,this.data=o,this.style={...f.defaultStyle,...n},this.update()}measure(i=0,t=0){let s=this._createParagraphs(this.data);s=this._createWrapedParagraphs(s,i);const o=this.context;let n=0;for(let h=s.length,r=0;r<h;r++){const e=s[r];e.relativeX=0,e.relativeY=n,e.width=0,e.height=0;let a=0,d;for(const c of e.fragments){const g=c.style;this._setContextStyle(g);const u=o.measureText(c.data),b=u.width,w=u.actualBoundingBoxRight+u.actualBoundingBoxLeft;c.relativeX=a,c.relativeY=e.relativeY,c.width=b,c.actualBoundingBoxWidth=w,c.height=g.fontSize*g.lineHeight,a+=b+g.letterSpacing,d=c,e.height=Math.max(e.height,c.height)}e.width=d?d.relativeX+Math.max(d.width,d.actualBoundingBoxWidth):0,n+=e.height}const l=s.reduce((h,r)=>(h.x=Math.min(h.x,r.relativeX),h.y=Math.min(h.y,r.relativeY),h.width=Math.max(h.width,r.width),h.height+=r.height,h),{x:0,y:0,width:0,height:0});i=i||l.width,t=Math.max(t,l.height);for(let h=s.length,r=0;r<h;r++){const e=s[r];switch(this.style.textAlign){case"center":e.absoluteX=(i-l.width)/2,e.fragments.forEach(a=>{a.absoluteX=e.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width/2});break;case"end":case"right":e.absoluteX=i-l.width,e.fragments.forEach(a=>{a.absoluteX=e.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width});break;case"start":case"left":default:e.absoluteX=0,e.fragments.forEach(a=>{a.absoluteX=e.absoluteX+a.relativeX,a.fillX=a.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":e.absoluteY=e.relativeY,e.fragments.forEach(a=>{a.absoluteY=e.absoluteY,a.fillY=a.absoluteY});break;case"middle":case"alphabetic":case"ideographic":e.absoluteY=e.relativeY+(t-l.height)/2,e.fragments.forEach(a=>{a.absoluteY=e.absoluteY,a.fillY=a.absoluteY+e.height/2});break;case"bottom":e.absoluteY=e.relativeY+t-l.height,e.fragments.forEach(a=>{a.absoluteY=e.absoluteY,a.fillY=a.absoluteY+e.height});break}}return{...l,paragraphs:s}}_createParagraphs(i){const t={width:0,height:0,actualBoundingBoxWidth:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},s=(l={})=>({...t,fragments:[],...l}),o=(l={})=>{const h=[],{width:r,height:e,...a}=this.style;return h.push({fillX:0,fillY:0,...t,...l,style:{...a,...l.style},data:l.data??""}),h},n=[];if(typeof i=="string")n.push(s({fragments:o({data:i})}));else{i=Array.isArray(i)?i:[i];for(const l of i){const h=s();if("fragments"in l){const{fragments:r,...e}=l;for(const a of r){const{data:d,...c}=a;h.fragments.push(...o({data:d,style:{...e,...c}}))}}else if("data"in l){const{data:r,...e}=l;h.fragments.push(...o({data:r,style:e}))}n.push(h)}}return n}_createWrapedParagraphs(i,t){const s=[],o=i.slice();let n,l;for(;n=o.shift();){const h=n.fragments.slice();let r=0;const e=[];let a=!0;for(;l=h.shift();){const d=l.style;this._setContextStyle(d);let c="",g=!1;for(const u of l.data){const b=this.context.measureText(u).width+(a?0:d.letterSpacing),w=/^[\r\n]$/.test(u);if(w||d.textWrap==="wrap"&&t&&r+b>t){let p=w?c.length+1:c.length;!r&&!p&&(c+=u,p++),c.length&&e.push({...l,text:c}),e.length&&(s.push({...n,fragments:e.slice()}),e.length=0);const X=l.data.substring(p);(X.length||h.length)&&o.unshift({...n,fragments:(X.length?[{...l,data:X}]:[]).concat(h.slice())}),h.length=0,g=!0;break}else r+=b;c+=u}g||e.push({...l}),a=!1}e.length&&s.push({...n,fragments:e})}return s}_draw(i){const t=this.context;i.forEach(s=>{s.fragments.forEach(o=>{const n=o.style;switch(this._setContextStyle(n),n.textStrokeWidth&&t.strokeText(o.data,o.fillX,o.fillY),t.fillText(o.data,o.fillX,o.fillY),n.textDecoration){case"underline":t.beginPath(),t.moveTo(o.absoluteX,s.absoluteY+s.height-2),t.lineTo(o.absoluteX+o.width,s.absoluteY+s.height-2),t.stroke();break;case"line-through":t.beginPath(),t.moveTo(o.absoluteX,s.absoluteY+s.height/2),t.lineTo(o.absoluteX+o.width,s.absoluteY+s.height/2),t.stroke();break}})})}_resizeView(i,t){const s=this.view;s.style.width=`${i}px`,s.style.height=`${t}px`,s.dataset.width=String(i),s.dataset.height=String(t),s.width=Math.max(1,Math.floor(i*this.pixelRatio)),s.height=Math.max(1,Math.floor(t*this.pixelRatio))}_setContextStyle(i){const t=this.context;t.shadowColor=i.shadowColor,t.shadowOffsetX=i.shadowOffsetX,t.shadowOffsetY=i.shadowOffsetY,t.shadowBlur=i.shadowBlur,t.strokeStyle=i.textStrokeColor,t.lineWidth=i.textStrokeWidth,t.fillStyle=i.color,t.direction=i.direction,t.textAlign=i.textAlign,t.textBaseline=i.textBaseline,t.font=[i.fontStyle,i.fontWeight,`${i.fontSize}px`,i.fontFamily].join(" "),t.fontKerning=i.fontKerning,t.letterSpacing=`${i.letterSpacing}px`}update(){const i=this.context;let{width:t,height:s}=this.style;t==="auto"&&(t=0),s==="auto"&&(s=0);const o=this.measure(t,s);t||(t=o.width),s=Math.max(s,o.height),this._resizeView(t,s);const n=this.pixelRatio;i.scale(n,n),i.clearRect(0,0,i.canvas.width,i.canvas.height),this._draw(o.paragraphs)}}x.Text=f,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"})});
1
+ (function(x,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(x=typeof globalThis<"u"?globalThis:x||self,f(x.modernText={}))})(this,function(x){"use strict";class f{static get defaultStyle(){return{width:"auto",height:"auto",color:"#000000",fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",textBaseline:"middle",textDecoration:null,textStrokeWidth:0,textStrokeColor:"#000000",direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:"#000000",shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0}}constructor(i={}){const{view:t=document.createElement("canvas"),pixelRatio:a=window.devicePixelRatio||1,data:o="",style:n}=i;this.view=t,this.context=t.getContext("2d"),this.pixelRatio=a,this.data=o,this.style={...f.defaultStyle,...n},this.update()}measure(i=0,t=0){let a=this._createParagraphs(this.data);a=this._createWrapedParagraphs(a,i);const o=this.context;let n=0;for(let h=a.length,r=0;r<h;r++){const e=a[r];e.relativeX=0,e.relativeY=n,e.width=0,e.height=0;let d=0,s;for(const c of e.fragments){const g=c.style;this._setContextStyle(g);const u=o.measureText(c.data),b=u.width,w=u.actualBoundingBoxRight+u.actualBoundingBoxLeft;c.relativeX=d,c.relativeY=e.relativeY,c.width=b,c.actualBoundingBoxWidth=w,c.data.match(/^\s$/)?c.height=0:c.height=g.fontSize*g.lineHeight,d+=b+g.letterSpacing,s=c,e.height=Math.max(e.height,c.height)}e.width=s?s.relativeX+Math.max(s.width,s.actualBoundingBoxWidth):0,n+=e.height}const l=a.reduce((h,r)=>(h.x=Math.min(h.x,r.relativeX),h.y=Math.min(h.y,r.relativeY),h.width=Math.max(h.width,r.width),h.height+=r.height,h),{x:0,y:0,width:0,height:0});i=i||l.width,t=Math.max(t,l.height);for(let h=a.length,r=0;r<h;r++){const e=a[r];switch(this.style.textAlign){case"center":e.absoluteX=(i-l.width)/2,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX+s.width/2});break;case"end":case"right":e.absoluteX=i-l.width,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX+s.width});break;case"start":case"left":default:e.absoluteX=0,e.fragments.forEach(s=>{s.absoluteX=e.absoluteX+s.relativeX,s.fillX=s.absoluteX});break}const d=t-l.height;switch(this.style.textBaseline){case"bottom":e.absoluteY=e.relativeY+d,e.fragments.forEach(s=>{s.absoluteY=e.absoluteY+(e.height-s.height),s.fillY=s.absoluteY+s.height});break;case"middle":case"alphabetic":case"ideographic":e.absoluteY=e.relativeY+d/2,e.fragments.forEach(s=>{s.absoluteY=e.absoluteY+(e.height-s.height)/2,s.fillY=s.absoluteY+s.height/2});break;case"top":case"hanging":default:e.absoluteY=e.relativeY,e.fragments.forEach(s=>s.fillY=s.absoluteY=e.absoluteY);break}}return{...l,paragraphs:a}}_createParagraphs(i){const t={width:0,height:0,actualBoundingBoxWidth:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},a=(l={})=>({...t,fragments:[],...l}),o=(l={})=>{const h=[],{width:r,height:e,...d}=this.style;return h.push({fillX:0,fillY:0,...t,...l,style:{...d,...l.style},data:l.data??""}),h},n=[];if(typeof i=="string")n.push(a({fragments:o({data:i})}));else{i=Array.isArray(i)?i:[i];for(const l of i){const h=a();if("fragments"in l){const{fragments:r,...e}=l;for(const d of r){const{data:s,...c}=d;h.fragments.push(...o({data:s,style:{...e,...c}}))}}else if("data"in l){const{data:r,...e}=l;h.fragments.push(...o({data:r,style:e}))}n.push(h)}}return n}_createWrapedParagraphs(i,t){const a=[],o=i.slice();let n,l;for(;n=o.shift();){const h=n.fragments.slice();let r=0;const e=[];let d=!0;for(;l=h.shift();){const s=l.style;this._setContextStyle(s);let c="",g=!1;for(const u of l.data){const b=this.context.measureText(u).width+(d?0:s.letterSpacing),w=/^[\r\n]$/.test(u);if(w||s.textWrap==="wrap"&&t&&r+b>t){let p=w?c.length+1:c.length;!r&&!p&&(c+=u,p++),c.length&&e.push({...l,text:c}),e.length&&(a.push({...n,fragments:e.slice()}),e.length=0);const X=l.data.substring(p);(X.length||h.length)&&o.unshift({...n,fragments:(X.length?[{...l,data:X}]:[]).concat(h.slice())}),h.length=0,g=!0;break}else r+=b;c+=u}g||e.push({...l}),d=!1}e.length&&a.push({...n,fragments:e})}return a}_draw(i){const t=this.context;this.style.backgroundColor&&(t.fillStyle=this.style.backgroundColor,t.fillRect(0,0,t.canvas.width,t.canvas.height)),i.forEach(a=>{a.fragments.forEach(o=>{const n=o.style;switch(n.backgroundColor&&(t.fillStyle=n.backgroundColor,t.fillRect(o.absoluteX,o.absoluteY,o.width,o.height)),this._setContextStyle(n),n.textStrokeWidth&&t.strokeText(o.data,o.fillX,o.fillY),t.fillText(o.data,o.fillX,o.fillY),n.textDecoration){case"underline":t.beginPath(),t.moveTo(o.absoluteX,a.absoluteY+a.height-2),t.lineTo(o.absoluteX+o.width,a.absoluteY+a.height-2),t.stroke();break;case"line-through":t.beginPath(),t.moveTo(o.absoluteX,a.absoluteY+a.height/2),t.lineTo(o.absoluteX+o.width,a.absoluteY+a.height/2),t.stroke();break}})})}_resizeView(i,t){const a=this.view;a.style.width=`${i}px`,a.style.height=`${t}px`,a.dataset.width=String(i),a.dataset.height=String(t),a.width=Math.max(1,Math.floor(i*this.pixelRatio)),a.height=Math.max(1,Math.floor(t*this.pixelRatio))}_setContextStyle(i){const t=this.context;t.shadowColor=i.shadowColor,t.shadowOffsetX=i.shadowOffsetX,t.shadowOffsetY=i.shadowOffsetY,t.shadowBlur=i.shadowBlur,t.strokeStyle=i.textStrokeColor,t.lineWidth=i.textStrokeWidth,t.fillStyle=i.color,t.direction=i.direction,t.textAlign=i.textAlign,t.textBaseline=i.textBaseline,t.font=[i.fontStyle,i.fontWeight,`${i.fontSize}px`,i.fontFamily].join(" "),t.fontKerning=i.fontKerning,t.letterSpacing=`${i.letterSpacing}px`}update(){const i=this.context;let{width:t,height:a}=this.style;t==="auto"&&(t=0),a==="auto"&&(a=0);const o=this.measure(t,a);t||(t=o.width),a=Math.max(a,o.height),this._resizeView(t,a);const n=this.pixelRatio;i.scale(n,n),i.clearRect(0,0,i.canvas.width,i.canvas.height),this._draw(o.paragraphs)}}x.Text=f,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"})});
package/dist/index.mjs CHANGED
@@ -24,48 +24,48 @@ class p {
24
24
  shadowBlur: 0
25
25
  };
26
26
  }
27
- constructor(a = {}) {
27
+ constructor(i = {}) {
28
28
  const {
29
29
  view: t = document.createElement("canvas"),
30
- pixelRatio: i = window.devicePixelRatio || 1,
30
+ pixelRatio: a = window.devicePixelRatio || 1,
31
31
  data: o = "",
32
32
  style: n
33
- } = a;
34
- this.view = t, this.context = t.getContext("2d"), this.pixelRatio = i, this.data = o, this.style = {
33
+ } = i;
34
+ this.view = t, this.context = t.getContext("2d"), this.pixelRatio = a, this.data = o, this.style = {
35
35
  ...p.defaultStyle,
36
36
  ...n
37
37
  }, this.update();
38
38
  }
39
- measure(a = 0, t = 0) {
40
- let i = this._createParagraphs(this.data);
41
- i = this._createWrapedParagraphs(i, a);
39
+ measure(i = 0, t = 0) {
40
+ let a = this._createParagraphs(this.data);
41
+ a = this._createWrapedParagraphs(a, i);
42
42
  const o = this.context;
43
43
  let n = 0;
44
- for (let h = i.length, r = 0; r < h; r++) {
45
- const e = i[r];
44
+ for (let h = a.length, r = 0; r < h; r++) {
45
+ const e = a[r];
46
46
  e.relativeX = 0, e.relativeY = n, e.width = 0, e.height = 0;
47
- let s = 0, u;
47
+ let d = 0, s;
48
48
  for (const c of e.fragments) {
49
49
  const f = c.style;
50
50
  this._setContextStyle(f);
51
- const d = o.measureText(c.data), g = d.width, x = d.actualBoundingBoxRight + d.actualBoundingBoxLeft;
52
- c.relativeX = s, c.relativeY = e.relativeY, c.width = g, c.actualBoundingBoxWidth = x, c.height = f.fontSize * f.lineHeight, s += g + f.letterSpacing, u = c, e.height = Math.max(e.height, c.height);
51
+ const u = o.measureText(c.data), g = u.width, x = u.actualBoundingBoxRight + u.actualBoundingBoxLeft;
52
+ c.relativeX = d, c.relativeY = e.relativeY, c.width = g, c.actualBoundingBoxWidth = x, c.data.match(/^\s$/) ? c.height = 0 : c.height = f.fontSize * f.lineHeight, d += g + f.letterSpacing, s = c, e.height = Math.max(e.height, c.height);
53
53
  }
54
- e.width = u ? u.relativeX + Math.max(u.width, u.actualBoundingBoxWidth) : 0, n += e.height;
54
+ e.width = s ? s.relativeX + Math.max(s.width, s.actualBoundingBoxWidth) : 0, n += e.height;
55
55
  }
56
- const l = i.reduce((h, r) => (h.x = Math.min(h.x, r.relativeX), h.y = Math.min(h.y, r.relativeY), h.width = Math.max(h.width, r.width), h.height += r.height, h), { x: 0, y: 0, width: 0, height: 0 });
57
- a = a || l.width, t = Math.max(t, l.height);
58
- for (let h = i.length, r = 0; r < h; r++) {
59
- const e = i[r];
56
+ const l = a.reduce((h, r) => (h.x = Math.min(h.x, r.relativeX), h.y = Math.min(h.y, r.relativeY), h.width = Math.max(h.width, r.width), h.height += r.height, h), { x: 0, y: 0, width: 0, height: 0 });
57
+ i = i || l.width, t = Math.max(t, l.height);
58
+ for (let h = a.length, r = 0; r < h; r++) {
59
+ const e = a[r];
60
60
  switch (this.style.textAlign) {
61
61
  case "center":
62
- e.absoluteX = (a - l.width) / 2, e.fragments.forEach((s) => {
62
+ e.absoluteX = (i - l.width) / 2, e.fragments.forEach((s) => {
63
63
  s.absoluteX = e.absoluteX + s.relativeX, s.fillX = s.absoluteX + s.width / 2;
64
64
  });
65
65
  break;
66
66
  case "end":
67
67
  case "right":
68
- e.absoluteX = a - l.width, e.fragments.forEach((s) => {
68
+ e.absoluteX = i - l.width, e.fragments.forEach((s) => {
69
69
  s.absoluteX = e.absoluteX + s.relativeX, s.fillX = s.absoluteX + s.width;
70
70
  });
71
71
  break;
@@ -77,30 +77,30 @@ class p {
77
77
  });
78
78
  break;
79
79
  }
80
+ const d = t - l.height;
80
81
  switch (this.style.textBaseline) {
81
- case "top":
82
- case "hanging":
83
- e.absoluteY = e.relativeY, e.fragments.forEach((s) => {
84
- s.absoluteY = e.absoluteY, s.fillY = s.absoluteY;
82
+ case "bottom":
83
+ e.absoluteY = e.relativeY + d, e.fragments.forEach((s) => {
84
+ s.absoluteY = e.absoluteY + (e.height - s.height), s.fillY = s.absoluteY + s.height;
85
85
  });
86
86
  break;
87
87
  case "middle":
88
88
  case "alphabetic":
89
89
  case "ideographic":
90
- e.absoluteY = e.relativeY + (t - l.height) / 2, e.fragments.forEach((s) => {
91
- s.absoluteY = e.absoluteY, s.fillY = s.absoluteY + e.height / 2;
90
+ e.absoluteY = e.relativeY + d / 2, e.fragments.forEach((s) => {
91
+ s.absoluteY = e.absoluteY + (e.height - s.height) / 2, s.fillY = s.absoluteY + s.height / 2;
92
92
  });
93
93
  break;
94
- case "bottom":
95
- e.absoluteY = e.relativeY + t - l.height, e.fragments.forEach((s) => {
96
- s.absoluteY = e.absoluteY, s.fillY = s.absoluteY + e.height;
97
- });
94
+ case "top":
95
+ case "hanging":
96
+ default:
97
+ e.absoluteY = e.relativeY, e.fragments.forEach((s) => s.fillY = s.absoluteY = e.absoluteY);
98
98
  break;
99
99
  }
100
100
  }
101
- return { ...l, paragraphs: i };
101
+ return { ...l, paragraphs: a };
102
102
  }
103
- _createParagraphs(a) {
103
+ _createParagraphs(i) {
104
104
  const t = {
105
105
  width: 0,
106
106
  height: 0,
@@ -109,31 +109,31 @@ class p {
109
109
  relativeY: 0,
110
110
  absoluteX: 0,
111
111
  absoluteY: 0
112
- }, i = (l = {}) => ({ ...t, fragments: [], ...l }), o = (l = {}) => {
113
- const h = [], { width: r, height: e, ...s } = this.style;
112
+ }, a = (l = {}) => ({ ...t, fragments: [], ...l }), o = (l = {}) => {
113
+ const h = [], { width: r, height: e, ...d } = this.style;
114
114
  return h.push({
115
115
  fillX: 0,
116
116
  fillY: 0,
117
117
  ...t,
118
118
  ...l,
119
119
  style: {
120
- ...s,
120
+ ...d,
121
121
  ...l.style
122
122
  },
123
123
  data: l.data ?? ""
124
124
  }), h;
125
125
  }, n = [];
126
- if (typeof a == "string")
127
- n.push(i({ fragments: o({ data: a }) }));
126
+ if (typeof i == "string")
127
+ n.push(a({ fragments: o({ data: i }) }));
128
128
  else {
129
- a = Array.isArray(a) ? a : [a];
130
- for (const l of a) {
131
- const h = i();
129
+ i = Array.isArray(i) ? i : [i];
130
+ for (const l of i) {
131
+ const h = a();
132
132
  if ("fragments" in l) {
133
133
  const { fragments: r, ...e } = l;
134
- for (const s of r) {
135
- const { data: u, ...c } = s;
136
- h.fragments.push(...o({ data: u, style: { ...e, ...c } }));
134
+ for (const d of r) {
135
+ const { data: s, ...c } = d;
136
+ h.fragments.push(...o({ data: s, style: { ...e, ...c } }));
137
137
  }
138
138
  } else if ("data" in l) {
139
139
  const { data: r, ...e } = l;
@@ -144,76 +144,76 @@ class p {
144
144
  }
145
145
  return n;
146
146
  }
147
- _createWrapedParagraphs(a, t) {
148
- const i = [], o = a.slice();
147
+ _createWrapedParagraphs(i, t) {
148
+ const a = [], o = i.slice();
149
149
  let n, l;
150
150
  for (; n = o.shift(); ) {
151
151
  const h = n.fragments.slice();
152
152
  let r = 0;
153
153
  const e = [];
154
- let s = !0;
154
+ let d = !0;
155
155
  for (; l = h.shift(); ) {
156
- const u = l.style;
157
- this._setContextStyle(u);
156
+ const s = l.style;
157
+ this._setContextStyle(s);
158
158
  let c = "", f = !1;
159
- for (const d of l.data) {
160
- const g = this.context.measureText(d).width + (s ? 0 : u.letterSpacing), x = /^[\r\n]$/.test(d);
161
- if (x || u.textWrap === "wrap" && t && r + g > t) {
162
- let w = x ? c.length + 1 : c.length;
163
- !r && !w && (c += d, w++), c.length && e.push({ ...l, text: c }), e.length && (i.push({ ...n, fragments: e.slice() }), e.length = 0);
164
- const b = l.data.substring(w);
165
- (b.length || h.length) && o.unshift({
159
+ for (const u of l.data) {
160
+ const g = this.context.measureText(u).width + (d ? 0 : s.letterSpacing), x = /^[\r\n]$/.test(u);
161
+ if (x || s.textWrap === "wrap" && t && r + g > t) {
162
+ let b = x ? c.length + 1 : c.length;
163
+ !r && !b && (c += u, b++), c.length && e.push({ ...l, text: c }), e.length && (a.push({ ...n, fragments: e.slice() }), e.length = 0);
164
+ const w = l.data.substring(b);
165
+ (w.length || h.length) && o.unshift({
166
166
  ...n,
167
- fragments: (b.length ? [{ ...l, data: b }] : []).concat(h.slice())
167
+ fragments: (w.length ? [{ ...l, data: w }] : []).concat(h.slice())
168
168
  }), h.length = 0, f = !0;
169
169
  break;
170
170
  } else
171
171
  r += g;
172
- c += d;
172
+ c += u;
173
173
  }
174
- f || e.push({ ...l }), s = !1;
174
+ f || e.push({ ...l }), d = !1;
175
175
  }
176
- e.length && i.push({ ...n, fragments: e });
176
+ e.length && a.push({ ...n, fragments: e });
177
177
  }
178
- return i;
178
+ return a;
179
179
  }
180
- _draw(a) {
180
+ _draw(i) {
181
181
  const t = this.context;
182
- a.forEach((i) => {
183
- i.fragments.forEach((o) => {
182
+ this.style.backgroundColor && (t.fillStyle = this.style.backgroundColor, t.fillRect(0, 0, t.canvas.width, t.canvas.height)), i.forEach((a) => {
183
+ a.fragments.forEach((o) => {
184
184
  const n = o.style;
185
- switch (this._setContextStyle(n), n.textStrokeWidth && t.strokeText(o.data, o.fillX, o.fillY), t.fillText(o.data, o.fillX, o.fillY), n.textDecoration) {
185
+ switch (n.backgroundColor && (t.fillStyle = n.backgroundColor, t.fillRect(o.absoluteX, o.absoluteY, o.width, o.height)), this._setContextStyle(n), n.textStrokeWidth && t.strokeText(o.data, o.fillX, o.fillY), t.fillText(o.data, o.fillX, o.fillY), n.textDecoration) {
186
186
  case "underline":
187
- t.beginPath(), t.moveTo(o.absoluteX, i.absoluteY + i.height - 2), t.lineTo(o.absoluteX + o.width, i.absoluteY + i.height - 2), t.stroke();
187
+ t.beginPath(), t.moveTo(o.absoluteX, a.absoluteY + a.height - 2), t.lineTo(o.absoluteX + o.width, a.absoluteY + a.height - 2), t.stroke();
188
188
  break;
189
189
  case "line-through":
190
- t.beginPath(), t.moveTo(o.absoluteX, i.absoluteY + i.height / 2), t.lineTo(o.absoluteX + o.width, i.absoluteY + i.height / 2), t.stroke();
190
+ t.beginPath(), t.moveTo(o.absoluteX, a.absoluteY + a.height / 2), t.lineTo(o.absoluteX + o.width, a.absoluteY + a.height / 2), t.stroke();
191
191
  break;
192
192
  }
193
193
  });
194
194
  });
195
195
  }
196
- _resizeView(a, t) {
197
- const i = this.view;
198
- i.style.width = `${a}px`, i.style.height = `${t}px`, i.dataset.width = String(a), i.dataset.height = String(t), i.width = Math.max(1, Math.floor(a * this.pixelRatio)), i.height = Math.max(1, Math.floor(t * this.pixelRatio));
196
+ _resizeView(i, t) {
197
+ const a = this.view;
198
+ a.style.width = `${i}px`, a.style.height = `${t}px`, a.dataset.width = String(i), a.dataset.height = String(t), a.width = Math.max(1, Math.floor(i * this.pixelRatio)), a.height = Math.max(1, Math.floor(t * this.pixelRatio));
199
199
  }
200
- _setContextStyle(a) {
200
+ _setContextStyle(i) {
201
201
  const t = this.context;
202
- t.shadowColor = a.shadowColor, t.shadowOffsetX = a.shadowOffsetX, t.shadowOffsetY = a.shadowOffsetY, t.shadowBlur = a.shadowBlur, t.strokeStyle = a.textStrokeColor, t.lineWidth = a.textStrokeWidth, t.fillStyle = a.color, t.direction = a.direction, t.textAlign = a.textAlign, t.textBaseline = a.textBaseline, t.font = [
203
- a.fontStyle,
204
- a.fontWeight,
205
- `${a.fontSize}px`,
206
- a.fontFamily
207
- ].join(" "), t.fontKerning = a.fontKerning, t.letterSpacing = `${a.letterSpacing}px`;
202
+ t.shadowColor = i.shadowColor, t.shadowOffsetX = i.shadowOffsetX, t.shadowOffsetY = i.shadowOffsetY, t.shadowBlur = i.shadowBlur, t.strokeStyle = i.textStrokeColor, t.lineWidth = i.textStrokeWidth, t.fillStyle = i.color, t.direction = i.direction, t.textAlign = i.textAlign, t.textBaseline = i.textBaseline, t.font = [
203
+ i.fontStyle,
204
+ i.fontWeight,
205
+ `${i.fontSize}px`,
206
+ i.fontFamily
207
+ ].join(" "), t.fontKerning = i.fontKerning, t.letterSpacing = `${i.letterSpacing}px`;
208
208
  }
209
209
  update() {
210
- const a = this.context;
211
- let { width: t, height: i } = this.style;
212
- t === "auto" && (t = 0), i === "auto" && (i = 0);
213
- const o = this.measure(t, i);
214
- t || (t = o.width), i = Math.max(i, o.height), this._resizeView(t, i);
210
+ const i = this.context;
211
+ let { width: t, height: a } = this.style;
212
+ t === "auto" && (t = 0), a === "auto" && (a = 0);
213
+ const o = this.measure(t, a);
214
+ t || (t = o.width), a = Math.max(a, o.height), this._resizeView(t, a);
215
215
  const n = this.pixelRatio;
216
- a.scale(n, n), a.clearRect(0, 0, a.canvas.width, a.canvas.height), this._draw(o.paragraphs);
216
+ i.scale(n, n), i.clearRect(0, 0, i.canvas.width, i.canvas.height), this._draw(o.paragraphs);
217
217
  }
218
218
  }
219
219
  export {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.5",
5
5
  "packageManager": "pnpm@8.14.1",
6
6
  "description": "Text measurement and rendering of canvas 2d",
7
7
  "author": "wxm",
package/types/Text.d.ts CHANGED
@@ -29,6 +29,7 @@ export interface TextFragment {
29
29
  }
30
30
  export interface TextFragmentStyle {
31
31
  color: string;
32
+ backgroundColor?: string;
32
33
  fontSize: number;
33
34
  fontWeight: FontWeight;
34
35
  fontFamily: string;