modern-text 0.0.1 → 0.0.2

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/README.md CHANGED
@@ -31,15 +31,15 @@ const text = new Text({
31
31
  backgroundColor: '#0000FF',
32
32
  textDecoration: 'underline',
33
33
  },
34
- content: [
34
+ data: [
35
35
  {
36
36
  fragments: [
37
- { content: 'He', style: { color: 'red', fontSize: 12 } },
38
- { content: 'llo', style: { color: 'black' } },
37
+ { data: 'He', style: { color: 'red', fontSize: 12 } },
38
+ { data: 'llo', style: { color: 'black' } },
39
39
  ],
40
40
  },
41
- { content: ', ', style: { color: 'grey' } },
42
- { content: 'World!', style: { color: 'black' } },
41
+ { data: ', ', style: { color: 'grey' } },
42
+ { data: 'World!', style: { color: 'black' } },
43
43
  ],
44
44
  })
45
45
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class p{constructor(t={}){const{view:e=document.createElement("canvas"),pixelRatio:i=window.devicePixelRatio||1,content:o="",style:h}=t;this.view=e,this.context=e.getContext("2d"),this.pixelRatio=i,this.content=o,this.style={width:0,height:0,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,...h},this.update()}measure(t=0,e=0){let i=this._createParagraphs(this.content);i=this._createWrapedParagraphs(i,t);const o=this.context;let h=0;for(let n=i.length,r=0;r<n;r++){const s=i[r];s.relativeX=0,s.relativeY=h,s.width=0,s.height=0;let a=0,u;for(const c of s.fragments){const g=this._getFragmentStyle(c.style);this._setContextStyle(g);const f=o.measureText(c.content),d=f.width,x=f.actualBoundingBoxRight+f.actualBoundingBoxLeft;c.relativeX=a,c.relativeY=s.relativeY,c.width=d,c.actualBoundingBoxWidth=x,c.height=g.fontSize*g.lineHeight,a+=d+g.letterSpacing,u=c,s.height=Math.max(s.height,c.height)}s.width=u?u.relativeX+Math.max(u.width,u.actualBoundingBoxWidth):0,h+=s.height}const l=i.reduce((n,r)=>(n.x=Math.min(n.x,r.relativeX),n.y=Math.min(n.y,r.relativeY),n.width=Math.max(n.width,r.width),n.height+=r.height,n),{x:0,y:0,width:0,height:0});t=t||l.width,e=Math.max(e,l.height);for(let n=i.length,r=0;r<n;r++){const s=i[r];switch(this.style.textAlign){case"center":s.absoluteX=(t-l.width)/2,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width/2});break;case"end":case"right":s.absoluteX=t-l.width,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width});break;case"start":case"left":default:s.absoluteX=0,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":s.absoluteY=s.relativeY,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY});break;case"middle":case"alphabetic":case"ideographic":s.absoluteY=s.relativeY+(e-l.height)/2,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY+s.height/2});break;case"bottom":s.absoluteY=s.relativeY+e-l.height,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY+s.height});break}}return{...l,paragraphs:i}}_createParagraphs(t){const e={width:0,actualBoundingBoxWidth:0,height:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},i=(l={})=>({...e,fragments:[],...l}),o=(l={})=>{const n=[],r=l.content??"";return n.push({fillX:0,fillY:0,style:{},...e,...l,content:r}),n},h=[];if(typeof t=="string")t&&h.push(i({fragments:o({content:t})}));else{t=Array.isArray(t)?t:[t];for(const l of t){const n=i();if(l.fragments)for(const r of l.fragments)n.fragments.push(...o({content:r.content,style:{...l.style,...r.style}}));else l.content&&n.fragments.push(...o({content:l.content,style:l.style}));h.push(n)}}return h}_createWrapedParagraphs(t,e){const i=[],o=t.slice();let h,l;for(;h=o.shift();){const n=h.fragments.slice();let r=0;const s=[];let a=!0;for(;l=n.shift();){const u=this._getFragmentStyle(l.style);this._setContextStyle(u);let c="",g=!1;for(const f of l.content){const d=this.context.measureText(f).width+(a?0:u.letterSpacing),x=/^[\r\n]$/.test(f);if(x||u.textWrap==="wrap"&&e&&r+d>e){let b=x?c.length+1:c.length;!r&&!b&&(c+=f,b++),c.length&&s.push({...l,text:c}),s.length&&(i.push({...h,fragments:s.slice()}),s.length=0);const w=l.content.substring(b);(w.length||n.length)&&o.unshift({...h,fragments:(w.length?[{...l,content:w}]:[]).concat(n.slice())}),n.length=0,g=!0;break}else r+=d;c+=f}g||s.push({...l}),a=!1}s.length&&i.push({...h,fragments:s})}return i}_draw(t){const e=this.context;t.forEach(i=>{i.fragments.forEach(o=>{const h=this._getFragmentStyle(o.style);switch(this._setContextStyle(h),h.textStrokeWidth&&e.strokeText(o.content,o.fillX,o.fillY),e.fillText(o.content,o.fillX,o.fillY),h.textDecoration){case"underline":e.beginPath(),e.moveTo(o.absoluteX,i.absoluteY+i.height-2),e.lineTo(o.absoluteX+o.width,i.absoluteY+i.height-2),e.stroke();break;case"line-through":e.beginPath(),e.moveTo(o.absoluteX,i.absoluteY+i.height/2),e.lineTo(o.absoluteX+o.width,i.absoluteY+i.height/2),e.stroke();break}})})}_resizeCanvas(t,e){const i=this.view;i.style.width=`${t}px`,i.style.height=`${e}px`,i.dataset.width=String(t),i.dataset.height=String(e),i.width=Math.max(1,Math.floor(t*this.pixelRatio)),i.height=Math.max(1,Math.floor(e*this.pixelRatio))}_getFragmentStyle(t={}){return{...this.style,...t}}_setContextStyle(t){const e=this.context;e.shadowColor=t.shadowColor,e.shadowOffsetX=t.shadowOffsetX,e.shadowOffsetY=t.shadowOffsetY,e.shadowBlur=t.shadowBlur,e.strokeStyle=t.textStrokeColor,e.lineWidth=t.textStrokeWidth,e.fillStyle=t.color,e.direction=t.direction,e.textAlign=t.textAlign,e.textBaseline=t.textBaseline,e.font=[t.fontStyle,t.fontWeight,`${t.fontSize}px`,t.fontFamily].join(" "),e.fontKerning=t.fontKerning,e.letterSpacing=`${t.letterSpacing}px`}update(){const t=this.context;let{width:e,height:i}=this.style;const o=this.measure(e,i);e||(e=o.width),i=Math.max(i,o.height),this._resizeCanvas(e,i);const h=this.pixelRatio;t.scale(h,h),t.clearRect(0,0,t.canvas.width,t.canvas.height),this._draw(o.paragraphs)}}exports.Text=p;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class p{constructor(t={}){const{view:e=document.createElement("canvas"),pixelRatio:a=window.devicePixelRatio||1,data:o="",style:n}=t;this.view=e,this.context=e.getContext("2d"),this.pixelRatio=a,this.data=o,this.style={width:0,height:0,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,...n},this.update()}measure(t=0,e=0){let a=this._createParagraphs(this.data);a=this._createWrapedParagraphs(a,t);const o=this.context;let n=0;for(let h=a.length,r=0;r<h;r++){const s=a[r];s.relativeX=0,s.relativeY=n,s.width=0,s.height=0;let i=0,u;for(const c of s.fragments){const f=this._getFragmentStyle(c.style);this._setContextStyle(f);const d=o.measureText(c.data),g=d.width,x=d.actualBoundingBoxRight+d.actualBoundingBoxLeft;c.relativeX=i,c.relativeY=s.relativeY,c.width=g,c.actualBoundingBoxWidth=x,c.height=f.fontSize*f.lineHeight,i+=g+f.letterSpacing,u=c,s.height=Math.max(s.height,c.height)}s.width=u?u.relativeX+Math.max(u.width,u.actualBoundingBoxWidth):0,n+=s.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});t=t||l.width,e=Math.max(e,l.height);for(let h=a.length,r=0;r<h;r++){const s=a[r];switch(this.style.textAlign){case"center":s.absoluteX=(t-l.width)/2,s.fragments.forEach(i=>{i.absoluteX=s.absoluteX+i.relativeX,i.fillX=i.absoluteX+i.width/2});break;case"end":case"right":s.absoluteX=t-l.width,s.fragments.forEach(i=>{i.absoluteX=s.absoluteX+i.relativeX,i.fillX=i.absoluteX+i.width});break;case"start":case"left":default:s.absoluteX=0,s.fragments.forEach(i=>{i.absoluteX=s.absoluteX+i.relativeX,i.fillX=i.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":s.absoluteY=s.relativeY,s.fragments.forEach(i=>{i.absoluteY=s.absoluteY,i.fillY=i.absoluteY});break;case"middle":case"alphabetic":case"ideographic":s.absoluteY=s.relativeY+(e-l.height)/2,s.fragments.forEach(i=>{i.absoluteY=s.absoluteY,i.fillY=i.absoluteY+s.height/2});break;case"bottom":s.absoluteY=s.relativeY+e-l.height,s.fragments.forEach(i=>{i.absoluteY=s.absoluteY,i.fillY=i.absoluteY+s.height});break}}return{...l,paragraphs:a}}_createParagraphs(t){const e={width:0,actualBoundingBoxWidth:0,height:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},a=(l={})=>({...e,fragments:[],...l}),o=(l={})=>{const h=[],r=l.data??"";return h.push({fillX:0,fillY:0,style:{},...e,...l,data:r}),h},n=[];if(typeof t=="string")t&&n.push(a({fragments:o({data:t})}));else{t=Array.isArray(t)?t:[t];for(const l of t){const h=a();if(l.fragments)for(const r of l.fragments)h.fragments.push(...o({data:r.data,style:{...l.style,...r.style}}));else l.data&&h.fragments.push(...o({data:l.data,style:l.style}));n.push(h)}}return n}_createWrapedParagraphs(t,e){const a=[],o=t.slice();let n,l;for(;n=o.shift();){const h=n.fragments.slice();let r=0;const s=[];let i=!0;for(;l=h.shift();){const u=this._getFragmentStyle(l.style);this._setContextStyle(u);let c="",f=!1;for(const d of l.data){const g=this.context.measureText(d).width+(i?0:u.letterSpacing),x=/^[\r\n]$/.test(d);if(x||u.textWrap==="wrap"&&e&&r+g>e){let b=x?c.length+1:c.length;!r&&!b&&(c+=d,b++),c.length&&s.push({...l,text:c}),s.length&&(a.push({...n,fragments:s.slice()}),s.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||s.push({...l}),i=!1}s.length&&a.push({...n,fragments:s})}return a}_draw(t){const e=this.context;t.forEach(a=>{a.fragments.forEach(o=>{const n=this._getFragmentStyle(o.style);switch(this._setContextStyle(n),n.textStrokeWidth&&e.strokeText(o.data,o.fillX,o.fillY),e.fillText(o.data,o.fillX,o.fillY),n.textDecoration){case"underline":e.beginPath(),e.moveTo(o.absoluteX,a.absoluteY+a.height-2),e.lineTo(o.absoluteX+o.width,a.absoluteY+a.height-2),e.stroke();break;case"line-through":e.beginPath(),e.moveTo(o.absoluteX,a.absoluteY+a.height/2),e.lineTo(o.absoluteX+o.width,a.absoluteY+a.height/2),e.stroke();break}})})}_resizeCanvas(t,e){const a=this.view;a.style.width=`${t}px`,a.style.height=`${e}px`,a.dataset.width=String(t),a.dataset.height=String(e),a.width=Math.max(1,Math.floor(t*this.pixelRatio)),a.height=Math.max(1,Math.floor(e*this.pixelRatio))}_getFragmentStyle(t={}){return{...this.style,...t}}_setContextStyle(t){const e=this.context;e.shadowColor=t.shadowColor,e.shadowOffsetX=t.shadowOffsetX,e.shadowOffsetY=t.shadowOffsetY,e.shadowBlur=t.shadowBlur,e.strokeStyle=t.textStrokeColor,e.lineWidth=t.textStrokeWidth,e.fillStyle=t.color,e.direction=t.direction,e.textAlign=t.textAlign,e.textBaseline=t.textBaseline,e.font=[t.fontStyle,t.fontWeight,`${t.fontSize}px`,t.fontFamily].join(" "),e.fontKerning=t.fontKerning,e.letterSpacing=`${t.letterSpacing}px`}update(){const t=this.context;let{width:e,height:a}=this.style;const o=this.measure(e,a);e||(e=o.width),a=Math.max(a,o.height),this._resizeCanvas(e,a);const n=this.pixelRatio;t.scale(n,n),t.clearRect(0,0,t.canvas.width,t.canvas.height),this._draw(o.paragraphs)}}exports.Text=p;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- (function(g,x){typeof exports=="object"&&typeof module<"u"?x(exports):typeof define=="function"&&define.amd?define(["exports"],x):(g=typeof globalThis<"u"?globalThis:g||self,x(g.modernText={}))})(this,function(g){"use strict";class x{constructor(t={}){const{view:e=document.createElement("canvas"),pixelRatio:i=window.devicePixelRatio||1,content:a="",style:h}=t;this.view=e,this.context=e.getContext("2d"),this.pixelRatio=i,this.content=a,this.style={width:0,height:0,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,...h},this.update()}measure(t=0,e=0){let i=this._createParagraphs(this.content);i=this._createWrapedParagraphs(i,t);const a=this.context;let h=0;for(let n=i.length,r=0;r<n;r++){const s=i[r];s.relativeX=0,s.relativeY=h,s.width=0,s.height=0;let o=0,u;for(const c of s.fragments){const d=this._getFragmentStyle(c.style);this._setContextStyle(d);const f=a.measureText(c.content),b=f.width,w=f.actualBoundingBoxRight+f.actualBoundingBoxLeft;c.relativeX=o,c.relativeY=s.relativeY,c.width=b,c.actualBoundingBoxWidth=w,c.height=d.fontSize*d.lineHeight,o+=b+d.letterSpacing,u=c,s.height=Math.max(s.height,c.height)}s.width=u?u.relativeX+Math.max(u.width,u.actualBoundingBoxWidth):0,h+=s.height}const l=i.reduce((n,r)=>(n.x=Math.min(n.x,r.relativeX),n.y=Math.min(n.y,r.relativeY),n.width=Math.max(n.width,r.width),n.height+=r.height,n),{x:0,y:0,width:0,height:0});t=t||l.width,e=Math.max(e,l.height);for(let n=i.length,r=0;r<n;r++){const s=i[r];switch(this.style.textAlign){case"center":s.absoluteX=(t-l.width)/2,s.fragments.forEach(o=>{o.absoluteX=s.absoluteX+o.relativeX,o.fillX=o.absoluteX+o.width/2});break;case"end":case"right":s.absoluteX=t-l.width,s.fragments.forEach(o=>{o.absoluteX=s.absoluteX+o.relativeX,o.fillX=o.absoluteX+o.width});break;case"start":case"left":default:s.absoluteX=0,s.fragments.forEach(o=>{o.absoluteX=s.absoluteX+o.relativeX,o.fillX=o.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":s.absoluteY=s.relativeY,s.fragments.forEach(o=>{o.absoluteY=s.absoluteY,o.fillY=o.absoluteY});break;case"middle":case"alphabetic":case"ideographic":s.absoluteY=s.relativeY+(e-l.height)/2,s.fragments.forEach(o=>{o.absoluteY=s.absoluteY,o.fillY=o.absoluteY+s.height/2});break;case"bottom":s.absoluteY=s.relativeY+e-l.height,s.fragments.forEach(o=>{o.absoluteY=s.absoluteY,o.fillY=o.absoluteY+s.height});break}}return{...l,paragraphs:i}}_createParagraphs(t){const e={width:0,actualBoundingBoxWidth:0,height:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},i=(l={})=>({...e,fragments:[],...l}),a=(l={})=>{const n=[],r=l.content??"";return n.push({fillX:0,fillY:0,style:{},...e,...l,content:r}),n},h=[];if(typeof t=="string")t&&h.push(i({fragments:a({content:t})}));else{t=Array.isArray(t)?t:[t];for(const l of t){const n=i();if(l.fragments)for(const r of l.fragments)n.fragments.push(...a({content:r.content,style:{...l.style,...r.style}}));else l.content&&n.fragments.push(...a({content:l.content,style:l.style}));h.push(n)}}return h}_createWrapedParagraphs(t,e){const i=[],a=t.slice();let h,l;for(;h=a.shift();){const n=h.fragments.slice();let r=0;const s=[];let o=!0;for(;l=n.shift();){const u=this._getFragmentStyle(l.style);this._setContextStyle(u);let c="",d=!1;for(const f of l.content){const b=this.context.measureText(f).width+(o?0:u.letterSpacing),w=/^[\r\n]$/.test(f);if(w||u.textWrap==="wrap"&&e&&r+b>e){let p=w?c.length+1:c.length;!r&&!p&&(c+=f,p++),c.length&&s.push({...l,text:c}),s.length&&(i.push({...h,fragments:s.slice()}),s.length=0);const X=l.content.substring(p);(X.length||n.length)&&a.unshift({...h,fragments:(X.length?[{...l,content:X}]:[]).concat(n.slice())}),n.length=0,d=!0;break}else r+=b;c+=f}d||s.push({...l}),o=!1}s.length&&i.push({...h,fragments:s})}return i}_draw(t){const e=this.context;t.forEach(i=>{i.fragments.forEach(a=>{const h=this._getFragmentStyle(a.style);switch(this._setContextStyle(h),h.textStrokeWidth&&e.strokeText(a.content,a.fillX,a.fillY),e.fillText(a.content,a.fillX,a.fillY),h.textDecoration){case"underline":e.beginPath(),e.moveTo(a.absoluteX,i.absoluteY+i.height-2),e.lineTo(a.absoluteX+a.width,i.absoluteY+i.height-2),e.stroke();break;case"line-through":e.beginPath(),e.moveTo(a.absoluteX,i.absoluteY+i.height/2),e.lineTo(a.absoluteX+a.width,i.absoluteY+i.height/2),e.stroke();break}})})}_resizeCanvas(t,e){const i=this.view;i.style.width=`${t}px`,i.style.height=`${e}px`,i.dataset.width=String(t),i.dataset.height=String(e),i.width=Math.max(1,Math.floor(t*this.pixelRatio)),i.height=Math.max(1,Math.floor(e*this.pixelRatio))}_getFragmentStyle(t={}){return{...this.style,...t}}_setContextStyle(t){const e=this.context;e.shadowColor=t.shadowColor,e.shadowOffsetX=t.shadowOffsetX,e.shadowOffsetY=t.shadowOffsetY,e.shadowBlur=t.shadowBlur,e.strokeStyle=t.textStrokeColor,e.lineWidth=t.textStrokeWidth,e.fillStyle=t.color,e.direction=t.direction,e.textAlign=t.textAlign,e.textBaseline=t.textBaseline,e.font=[t.fontStyle,t.fontWeight,`${t.fontSize}px`,t.fontFamily].join(" "),e.fontKerning=t.fontKerning,e.letterSpacing=`${t.letterSpacing}px`}update(){const t=this.context;let{width:e,height:i}=this.style;const a=this.measure(e,i);e||(e=a.width),i=Math.max(i,a.height),this._resizeCanvas(e,i);const h=this.pixelRatio;t.scale(h,h),t.clearRect(0,0,t.canvas.width,t.canvas.height),this._draw(a.paragraphs)}}g.Text=x,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
1
+ (function(g,x){typeof exports=="object"&&typeof module<"u"?x(exports):typeof define=="function"&&define.amd?define(["exports"],x):(g=typeof globalThis<"u"?globalThis:g||self,x(g.modernText={}))})(this,function(g){"use strict";class x{constructor(t={}){const{view:e=document.createElement("canvas"),pixelRatio:i=window.devicePixelRatio||1,data:o="",style:n}=t;this.view=e,this.context=e.getContext("2d"),this.pixelRatio=i,this.data=o,this.style={width:0,height:0,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,...n},this.update()}measure(t=0,e=0){let i=this._createParagraphs(this.data);i=this._createWrapedParagraphs(i,t);const o=this.context;let n=0;for(let h=i.length,r=0;r<h;r++){const s=i[r];s.relativeX=0,s.relativeY=n,s.width=0,s.height=0;let a=0,d;for(const c of s.fragments){const f=this._getFragmentStyle(c.style);this._setContextStyle(f);const u=o.measureText(c.data),b=u.width,w=u.actualBoundingBoxRight+u.actualBoundingBoxLeft;c.relativeX=a,c.relativeY=s.relativeY,c.width=b,c.actualBoundingBoxWidth=w,c.height=f.fontSize*f.lineHeight,a+=b+f.letterSpacing,d=c,s.height=Math.max(s.height,c.height)}s.width=d?d.relativeX+Math.max(d.width,d.actualBoundingBoxWidth):0,n+=s.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});t=t||l.width,e=Math.max(e,l.height);for(let h=i.length,r=0;r<h;r++){const s=i[r];switch(this.style.textAlign){case"center":s.absoluteX=(t-l.width)/2,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width/2});break;case"end":case"right":s.absoluteX=t-l.width,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width});break;case"start":case"left":default:s.absoluteX=0,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":s.absoluteY=s.relativeY,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY});break;case"middle":case"alphabetic":case"ideographic":s.absoluteY=s.relativeY+(e-l.height)/2,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY+s.height/2});break;case"bottom":s.absoluteY=s.relativeY+e-l.height,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY+s.height});break}}return{...l,paragraphs:i}}_createParagraphs(t){const e={width:0,actualBoundingBoxWidth:0,height:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},i=(l={})=>({...e,fragments:[],...l}),o=(l={})=>{const h=[],r=l.data??"";return h.push({fillX:0,fillY:0,style:{},...e,...l,data:r}),h},n=[];if(typeof t=="string")t&&n.push(i({fragments:o({data:t})}));else{t=Array.isArray(t)?t:[t];for(const l of t){const h=i();if(l.fragments)for(const r of l.fragments)h.fragments.push(...o({data:r.data,style:{...l.style,...r.style}}));else l.data&&h.fragments.push(...o({data:l.data,style:l.style}));n.push(h)}}return n}_createWrapedParagraphs(t,e){const i=[],o=t.slice();let n,l;for(;n=o.shift();){const h=n.fragments.slice();let r=0;const s=[];let a=!0;for(;l=h.shift();){const d=this._getFragmentStyle(l.style);this._setContextStyle(d);let c="",f=!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"&&e&&r+b>e){let p=w?c.length+1:c.length;!r&&!p&&(c+=u,p++),c.length&&s.push({...l,text:c}),s.length&&(i.push({...n,fragments:s.slice()}),s.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,f=!0;break}else r+=b;c+=u}f||s.push({...l}),a=!1}s.length&&i.push({...n,fragments:s})}return i}_draw(t){const e=this.context;t.forEach(i=>{i.fragments.forEach(o=>{const n=this._getFragmentStyle(o.style);switch(this._setContextStyle(n),n.textStrokeWidth&&e.strokeText(o.data,o.fillX,o.fillY),e.fillText(o.data,o.fillX,o.fillY),n.textDecoration){case"underline":e.beginPath(),e.moveTo(o.absoluteX,i.absoluteY+i.height-2),e.lineTo(o.absoluteX+o.width,i.absoluteY+i.height-2),e.stroke();break;case"line-through":e.beginPath(),e.moveTo(o.absoluteX,i.absoluteY+i.height/2),e.lineTo(o.absoluteX+o.width,i.absoluteY+i.height/2),e.stroke();break}})})}_resizeCanvas(t,e){const i=this.view;i.style.width=`${t}px`,i.style.height=`${e}px`,i.dataset.width=String(t),i.dataset.height=String(e),i.width=Math.max(1,Math.floor(t*this.pixelRatio)),i.height=Math.max(1,Math.floor(e*this.pixelRatio))}_getFragmentStyle(t={}){return{...this.style,...t}}_setContextStyle(t){const e=this.context;e.shadowColor=t.shadowColor,e.shadowOffsetX=t.shadowOffsetX,e.shadowOffsetY=t.shadowOffsetY,e.shadowBlur=t.shadowBlur,e.strokeStyle=t.textStrokeColor,e.lineWidth=t.textStrokeWidth,e.fillStyle=t.color,e.direction=t.direction,e.textAlign=t.textAlign,e.textBaseline=t.textBaseline,e.font=[t.fontStyle,t.fontWeight,`${t.fontSize}px`,t.fontFamily].join(" "),e.fontKerning=t.fontKerning,e.letterSpacing=`${t.letterSpacing}px`}update(){const t=this.context;let{width:e,height:i}=this.style;const o=this.measure(e,i);e||(e=o.width),i=Math.max(i,o.height),this._resizeCanvas(e,i);const n=this.pixelRatio;t.scale(n,n),t.clearRect(0,0,t.canvas.width,t.canvas.height),this._draw(o.paragraphs)}}g.Text=x,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
package/dist/index.mjs CHANGED
@@ -2,11 +2,11 @@ class X {
2
2
  constructor(t = {}) {
3
3
  const {
4
4
  view: e = document.createElement("canvas"),
5
- pixelRatio: i = window.devicePixelRatio || 1,
6
- content: o = "",
5
+ pixelRatio: a = window.devicePixelRatio || 1,
6
+ data: o = "",
7
7
  style: n
8
8
  } = t;
9
- this.view = e, this.context = e.getContext("2d"), this.pixelRatio = i, this.content = o, this.style = {
9
+ this.view = e, this.context = e.getContext("2d"), this.pixelRatio = a, this.data = o, this.style = {
10
10
  width: 0,
11
11
  height: 0,
12
12
  color: "#000000",
@@ -32,68 +32,68 @@ class X {
32
32
  }, this.update();
33
33
  }
34
34
  measure(t = 0, e = 0) {
35
- let i = this._createParagraphs(this.content);
36
- i = this._createWrapedParagraphs(i, t);
35
+ let a = this._createParagraphs(this.data);
36
+ a = this._createWrapedParagraphs(a, t);
37
37
  const o = this.context;
38
38
  let n = 0;
39
- for (let h = i.length, r = 0; r < h; r++) {
40
- const s = i[r];
39
+ for (let h = a.length, r = 0; r < h; r++) {
40
+ const s = a[r];
41
41
  s.relativeX = 0, s.relativeY = n, s.width = 0, s.height = 0;
42
- let a = 0, u;
42
+ let i = 0, u;
43
43
  for (const c of s.fragments) {
44
- const g = this._getFragmentStyle(c.style);
45
- this._setContextStyle(g);
46
- const f = o.measureText(c.content), d = f.width, x = f.actualBoundingBoxRight + f.actualBoundingBoxLeft;
47
- c.relativeX = a, c.relativeY = s.relativeY, c.width = d, c.actualBoundingBoxWidth = x, c.height = g.fontSize * g.lineHeight, a += d + g.letterSpacing, u = c, s.height = Math.max(s.height, c.height);
44
+ const f = this._getFragmentStyle(c.style);
45
+ this._setContextStyle(f);
46
+ const d = o.measureText(c.data), g = d.width, x = d.actualBoundingBoxRight + d.actualBoundingBoxLeft;
47
+ c.relativeX = i, c.relativeY = s.relativeY, c.width = g, c.actualBoundingBoxWidth = x, c.height = f.fontSize * f.lineHeight, i += g + f.letterSpacing, u = c, s.height = Math.max(s.height, c.height);
48
48
  }
49
49
  s.width = u ? u.relativeX + Math.max(u.width, u.actualBoundingBoxWidth) : 0, n += s.height;
50
50
  }
51
- 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 });
51
+ 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 });
52
52
  t = t || l.width, e = Math.max(e, l.height);
53
- for (let h = i.length, r = 0; r < h; r++) {
54
- const s = i[r];
53
+ for (let h = a.length, r = 0; r < h; r++) {
54
+ const s = a[r];
55
55
  switch (this.style.textAlign) {
56
56
  case "center":
57
- s.absoluteX = (t - l.width) / 2, s.fragments.forEach((a) => {
58
- a.absoluteX = s.absoluteX + a.relativeX, a.fillX = a.absoluteX + a.width / 2;
57
+ s.absoluteX = (t - l.width) / 2, s.fragments.forEach((i) => {
58
+ i.absoluteX = s.absoluteX + i.relativeX, i.fillX = i.absoluteX + i.width / 2;
59
59
  });
60
60
  break;
61
61
  case "end":
62
62
  case "right":
63
- s.absoluteX = t - l.width, s.fragments.forEach((a) => {
64
- a.absoluteX = s.absoluteX + a.relativeX, a.fillX = a.absoluteX + a.width;
63
+ s.absoluteX = t - l.width, s.fragments.forEach((i) => {
64
+ i.absoluteX = s.absoluteX + i.relativeX, i.fillX = i.absoluteX + i.width;
65
65
  });
66
66
  break;
67
67
  case "start":
68
68
  case "left":
69
69
  default:
70
- s.absoluteX = 0, s.fragments.forEach((a) => {
71
- a.absoluteX = s.absoluteX + a.relativeX, a.fillX = a.absoluteX;
70
+ s.absoluteX = 0, s.fragments.forEach((i) => {
71
+ i.absoluteX = s.absoluteX + i.relativeX, i.fillX = i.absoluteX;
72
72
  });
73
73
  break;
74
74
  }
75
75
  switch (this.style.textBaseline) {
76
76
  case "top":
77
77
  case "hanging":
78
- s.absoluteY = s.relativeY, s.fragments.forEach((a) => {
79
- a.absoluteY = s.absoluteY, a.fillY = a.absoluteY;
78
+ s.absoluteY = s.relativeY, s.fragments.forEach((i) => {
79
+ i.absoluteY = s.absoluteY, i.fillY = i.absoluteY;
80
80
  });
81
81
  break;
82
82
  case "middle":
83
83
  case "alphabetic":
84
84
  case "ideographic":
85
- s.absoluteY = s.relativeY + (e - l.height) / 2, s.fragments.forEach((a) => {
86
- a.absoluteY = s.absoluteY, a.fillY = a.absoluteY + s.height / 2;
85
+ s.absoluteY = s.relativeY + (e - l.height) / 2, s.fragments.forEach((i) => {
86
+ i.absoluteY = s.absoluteY, i.fillY = i.absoluteY + s.height / 2;
87
87
  });
88
88
  break;
89
89
  case "bottom":
90
- s.absoluteY = s.relativeY + e - l.height, s.fragments.forEach((a) => {
91
- a.absoluteY = s.absoluteY, a.fillY = a.absoluteY + s.height;
90
+ s.absoluteY = s.relativeY + e - l.height, s.fragments.forEach((i) => {
91
+ i.absoluteY = s.absoluteY, i.fillY = i.absoluteY + s.height;
92
92
  });
93
93
  break;
94
94
  }
95
95
  }
96
- return { ...l, paragraphs: i };
96
+ return { ...l, paragraphs: a };
97
97
  }
98
98
  _createParagraphs(t) {
99
99
  const e = {
@@ -104,28 +104,28 @@ class X {
104
104
  relativeY: 0,
105
105
  absoluteX: 0,
106
106
  absoluteY: 0
107
- }, i = (l = {}) => ({ ...e, fragments: [], ...l }), o = (l = {}) => {
108
- const h = [], r = l.content ?? "";
109
- return h.push({ fillX: 0, fillY: 0, style: {}, ...e, ...l, content: r }), h;
107
+ }, a = (l = {}) => ({ ...e, fragments: [], ...l }), o = (l = {}) => {
108
+ const h = [], r = l.data ?? "";
109
+ return h.push({ fillX: 0, fillY: 0, style: {}, ...e, ...l, data: r }), h;
110
110
  }, n = [];
111
111
  if (typeof t == "string")
112
112
  t && n.push(
113
- i({
114
- fragments: o({ content: t })
113
+ a({
114
+ fragments: o({ data: t })
115
115
  })
116
116
  );
117
117
  else {
118
118
  t = Array.isArray(t) ? t : [t];
119
119
  for (const l of t) {
120
- const h = i();
120
+ const h = a();
121
121
  if (l.fragments)
122
122
  for (const r of l.fragments)
123
123
  h.fragments.push(
124
- ...o({ content: r.content, style: { ...l.style, ...r.style } })
124
+ ...o({ data: r.data, style: { ...l.style, ...r.style } })
125
125
  );
126
126
  else
127
- l.content && h.fragments.push(
128
- ...o({ content: l.content, style: l.style })
127
+ l.data && h.fragments.push(
128
+ ...o({ data: l.data, style: l.style })
129
129
  );
130
130
  n.push(h);
131
131
  }
@@ -133,57 +133,57 @@ class X {
133
133
  return n;
134
134
  }
135
135
  _createWrapedParagraphs(t, e) {
136
- const i = [], o = t.slice();
136
+ const a = [], o = t.slice();
137
137
  let n, l;
138
138
  for (; n = o.shift(); ) {
139
139
  const h = n.fragments.slice();
140
140
  let r = 0;
141
141
  const s = [];
142
- let a = !0;
142
+ let i = !0;
143
143
  for (; l = h.shift(); ) {
144
144
  const u = this._getFragmentStyle(l.style);
145
145
  this._setContextStyle(u);
146
- let c = "", g = !1;
147
- for (const f of l.content) {
148
- const d = this.context.measureText(f).width + (a ? 0 : u.letterSpacing), x = /^[\r\n]$/.test(f);
149
- if (x || u.textWrap === "wrap" && e && r + d > e) {
146
+ let c = "", f = !1;
147
+ for (const d of l.data) {
148
+ const g = this.context.measureText(d).width + (i ? 0 : u.letterSpacing), x = /^[\r\n]$/.test(d);
149
+ if (x || u.textWrap === "wrap" && e && r + g > e) {
150
150
  let b = x ? c.length + 1 : c.length;
151
- !r && !b && (c += f, b++), c.length && s.push({ ...l, text: c }), s.length && (i.push({ ...n, fragments: s.slice() }), s.length = 0);
152
- const w = l.content.substring(b);
151
+ !r && !b && (c += d, b++), c.length && s.push({ ...l, text: c }), s.length && (a.push({ ...n, fragments: s.slice() }), s.length = 0);
152
+ const w = l.data.substring(b);
153
153
  (w.length || h.length) && o.unshift({
154
154
  ...n,
155
- fragments: (w.length ? [{ ...l, content: w }] : []).concat(h.slice())
156
- }), h.length = 0, g = !0;
155
+ fragments: (w.length ? [{ ...l, data: w }] : []).concat(h.slice())
156
+ }), h.length = 0, f = !0;
157
157
  break;
158
158
  } else
159
- r += d;
160
- c += f;
159
+ r += g;
160
+ c += d;
161
161
  }
162
- g || s.push({ ...l }), a = !1;
162
+ f || s.push({ ...l }), i = !1;
163
163
  }
164
- s.length && i.push({ ...n, fragments: s });
164
+ s.length && a.push({ ...n, fragments: s });
165
165
  }
166
- return i;
166
+ return a;
167
167
  }
168
168
  _draw(t) {
169
169
  const e = this.context;
170
- t.forEach((i) => {
171
- i.fragments.forEach((o) => {
170
+ t.forEach((a) => {
171
+ a.fragments.forEach((o) => {
172
172
  const n = this._getFragmentStyle(o.style);
173
- switch (this._setContextStyle(n), n.textStrokeWidth && e.strokeText(o.content, o.fillX, o.fillY), e.fillText(o.content, o.fillX, o.fillY), n.textDecoration) {
173
+ switch (this._setContextStyle(n), n.textStrokeWidth && e.strokeText(o.data, o.fillX, o.fillY), e.fillText(o.data, o.fillX, o.fillY), n.textDecoration) {
174
174
  case "underline":
175
- e.beginPath(), e.moveTo(o.absoluteX, i.absoluteY + i.height - 2), e.lineTo(o.absoluteX + o.width, i.absoluteY + i.height - 2), e.stroke();
175
+ e.beginPath(), e.moveTo(o.absoluteX, a.absoluteY + a.height - 2), e.lineTo(o.absoluteX + o.width, a.absoluteY + a.height - 2), e.stroke();
176
176
  break;
177
177
  case "line-through":
178
- e.beginPath(), e.moveTo(o.absoluteX, i.absoluteY + i.height / 2), e.lineTo(o.absoluteX + o.width, i.absoluteY + i.height / 2), e.stroke();
178
+ e.beginPath(), e.moveTo(o.absoluteX, a.absoluteY + a.height / 2), e.lineTo(o.absoluteX + o.width, a.absoluteY + a.height / 2), e.stroke();
179
179
  break;
180
180
  }
181
181
  });
182
182
  });
183
183
  }
184
184
  _resizeCanvas(t, e) {
185
- const i = this.view;
186
- i.style.width = `${t}px`, i.style.height = `${e}px`, i.dataset.width = String(t), i.dataset.height = String(e), i.width = Math.max(1, Math.floor(t * this.pixelRatio)), i.height = Math.max(1, Math.floor(e * this.pixelRatio));
185
+ const a = this.view;
186
+ a.style.width = `${t}px`, a.style.height = `${e}px`, a.dataset.width = String(t), a.dataset.height = String(e), a.width = Math.max(1, Math.floor(t * this.pixelRatio)), a.height = Math.max(1, Math.floor(e * this.pixelRatio));
187
187
  }
188
188
  _getFragmentStyle(t = {}) {
189
189
  return { ...this.style, ...t };
@@ -199,9 +199,9 @@ class X {
199
199
  }
200
200
  update() {
201
201
  const t = this.context;
202
- let { width: e, height: i } = this.style;
203
- const o = this.measure(e, i);
204
- e || (e = o.width), i = Math.max(i, o.height), this._resizeCanvas(e, i);
202
+ let { width: e, height: a } = this.style;
203
+ const o = this.measure(e, a);
204
+ e || (e = o.width), a = Math.max(a, o.height), this._resizeCanvas(e, a);
205
205
  const n = this.pixelRatio;
206
206
  t.scale(n, n), t.clearRect(0, 0, t.canvas.width, t.canvas.height), this._draw(o.paragraphs);
207
207
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "packageManager": "pnpm@8.14.1",
6
6
  "description": "Starter template for TypeScript library",
7
7
  "author": "wxm",
package/types/Text.d.ts CHANGED
@@ -6,12 +6,12 @@ export type TextAlign = 'center' | 'end' | 'left' | 'right' | 'start';
6
6
  export type TextBaseline = 'alphabetic' | 'bottom' | 'hanging' | 'ideographic' | 'middle' | 'top';
7
7
  export type TextDecoration = 'underline' | 'line-through';
8
8
  export interface TextParagraphWithStyle {
9
- content?: string;
9
+ data?: string;
10
10
  style?: Partial<TextFragmentStyle>;
11
11
  fragments?: Array<TextFragmentWithStyle>;
12
12
  }
13
13
  export interface TextFragmentWithStyle {
14
- content: string;
14
+ data: string;
15
15
  style?: Partial<TextFragmentStyle>;
16
16
  }
17
17
  export interface TextParagraph {
@@ -33,7 +33,7 @@ export interface TextFragment {
33
33
  absoluteY: number;
34
34
  fillX: number;
35
35
  fillY: number;
36
- content: string;
36
+ data: string;
37
37
  style: Partial<TextFragmentStyle>;
38
38
  }
39
39
  export interface TextStyle {
@@ -60,10 +60,11 @@ export interface TextStyle {
60
60
  shadowBlur: number;
61
61
  }
62
62
  export type TextFragmentStyle = Omit<TextStyle, 'width' | 'height'>;
63
+ export type TextData = string | TextParagraphWithStyle | Array<TextParagraphWithStyle>;
63
64
  export interface TextProperties {
64
65
  view?: HTMLCanvasElement;
65
66
  pixelRatio?: number;
66
- content?: string | Array<TextParagraphWithStyle>;
67
+ data?: TextData;
67
68
  style?: Partial<TextStyle>;
68
69
  }
69
70
  export interface MeasureResult {
@@ -78,10 +79,10 @@ export declare class Text {
78
79
  readonly context: CanvasRenderingContext2D;
79
80
  pixelRatio: number;
80
81
  style: TextStyle;
81
- content: string | Array<TextParagraphWithStyle>;
82
+ data: TextData;
82
83
  constructor(properties?: TextProperties);
83
84
  measure(width?: number, height?: number): MeasureResult;
84
- protected _createParagraphs(content: string | TextParagraphWithStyle | Array<TextParagraphWithStyle>): Array<TextParagraph>;
85
+ protected _createParagraphs(data: TextData): Array<TextParagraph>;
85
86
  protected _createWrapedParagraphs(paragraphs: Array<TextParagraph>, width: number): Array<TextParagraph>;
86
87
  protected _draw(paragraphs: Array<TextParagraph>): void;
87
88
  protected _resizeCanvas(width: number, height: number): void;