modern-text 0.0.2 → 0.0.4

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
@@ -28,22 +28,22 @@ const text = new Text({
28
28
  width: 100,
29
29
  height: 200,
30
30
  fontSize: 22,
31
- backgroundColor: '#0000FF',
32
31
  textDecoration: 'underline',
33
32
  },
34
33
  data: [
35
34
  {
35
+ letterSpacing: 3,
36
36
  fragments: [
37
- { data: 'He', style: { color: 'red', fontSize: 12 } },
38
- { data: 'llo', style: { color: 'black' } },
37
+ { data: 'He', color: 'red', fontSize: 12 },
38
+ { data: 'llo', color: 'black' },
39
39
  ],
40
40
  },
41
- { data: ', ', style: { color: 'grey' } },
42
- { data: 'World!', style: { color: 'black' } },
41
+ { data: ', ', color: 'grey' },
42
+ { data: 'World!', color: 'black' },
43
43
  ],
44
44
  })
45
45
 
46
46
  document.body.append(text.view) // canvas 2d
47
47
 
48
- console.log(text.measure()) // boundingBox with paragraphs
48
+ console.log(text.measure()) // boundingBox with computed paragraphs
49
49
  ```
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: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;
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;
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,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"})});
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"})});
package/dist/index.mjs CHANGED
@@ -1,14 +1,8 @@
1
- class X {
2
- constructor(t = {}) {
3
- const {
4
- view: e = document.createElement("canvas"),
5
- pixelRatio: a = window.devicePixelRatio || 1,
6
- data: o = "",
7
- style: n
8
- } = t;
9
- this.view = e, this.context = e.getContext("2d"), this.pixelRatio = a, this.data = o, this.style = {
10
- width: 0,
11
- height: 0,
1
+ class p {
2
+ static get defaultStyle() {
3
+ return {
4
+ width: "auto",
5
+ height: "auto",
12
6
  color: "#000000",
13
7
  fontSize: 14,
14
8
  fontWeight: "normal",
@@ -27,185 +21,201 @@ class X {
27
21
  shadowColor: "#000000",
28
22
  shadowOffsetX: 0,
29
23
  shadowOffsetY: 0,
30
- shadowBlur: 0,
24
+ shadowBlur: 0
25
+ };
26
+ }
27
+ constructor(a = {}) {
28
+ const {
29
+ view: t = document.createElement("canvas"),
30
+ pixelRatio: i = window.devicePixelRatio || 1,
31
+ data: o = "",
32
+ style: n
33
+ } = a;
34
+ this.view = t, this.context = t.getContext("2d"), this.pixelRatio = i, this.data = o, this.style = {
35
+ ...p.defaultStyle,
31
36
  ...n
32
37
  }, this.update();
33
38
  }
34
- measure(t = 0, e = 0) {
35
- let a = this._createParagraphs(this.data);
36
- a = this._createWrapedParagraphs(a, t);
39
+ measure(a = 0, t = 0) {
40
+ let i = this._createParagraphs(this.data);
41
+ i = this._createWrapedParagraphs(i, a);
37
42
  const o = this.context;
38
43
  let n = 0;
39
- for (let h = a.length, r = 0; r < h; r++) {
40
- const s = a[r];
41
- s.relativeX = 0, s.relativeY = n, s.width = 0, s.height = 0;
42
- let i = 0, u;
43
- for (const c of s.fragments) {
44
- const f = this._getFragmentStyle(c.style);
44
+ for (let h = i.length, r = 0; r < h; r++) {
45
+ const e = i[r];
46
+ e.relativeX = 0, e.relativeY = n, e.width = 0, e.height = 0;
47
+ let s = 0, u;
48
+ for (const c of e.fragments) {
49
+ const f = c.style;
45
50
  this._setContextStyle(f);
46
51
  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);
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);
48
53
  }
49
- s.width = u ? u.relativeX + Math.max(u.width, u.actualBoundingBoxWidth) : 0, n += s.height;
54
+ e.width = u ? u.relativeX + Math.max(u.width, u.actualBoundingBoxWidth) : 0, n += e.height;
50
55
  }
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
- t = t || l.width, e = Math.max(e, l.height);
53
- for (let h = a.length, r = 0; r < h; r++) {
54
- const s = a[r];
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];
55
60
  switch (this.style.textAlign) {
56
61
  case "center":
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;
62
+ e.absoluteX = (a - l.width) / 2, e.fragments.forEach((s) => {
63
+ s.absoluteX = e.absoluteX + s.relativeX, s.fillX = s.absoluteX + s.width / 2;
59
64
  });
60
65
  break;
61
66
  case "end":
62
67
  case "right":
63
- s.absoluteX = t - l.width, s.fragments.forEach((i) => {
64
- i.absoluteX = s.absoluteX + i.relativeX, i.fillX = i.absoluteX + i.width;
68
+ e.absoluteX = a - l.width, e.fragments.forEach((s) => {
69
+ s.absoluteX = e.absoluteX + s.relativeX, s.fillX = s.absoluteX + s.width;
65
70
  });
66
71
  break;
67
72
  case "start":
68
73
  case "left":
69
74
  default:
70
- s.absoluteX = 0, s.fragments.forEach((i) => {
71
- i.absoluteX = s.absoluteX + i.relativeX, i.fillX = i.absoluteX;
75
+ e.absoluteX = 0, e.fragments.forEach((s) => {
76
+ s.absoluteX = e.absoluteX + s.relativeX, s.fillX = s.absoluteX;
72
77
  });
73
78
  break;
74
79
  }
75
80
  switch (this.style.textBaseline) {
76
81
  case "top":
77
82
  case "hanging":
78
- s.absoluteY = s.relativeY, s.fragments.forEach((i) => {
79
- i.absoluteY = s.absoluteY, i.fillY = i.absoluteY;
83
+ e.absoluteY = e.relativeY, e.fragments.forEach((s) => {
84
+ s.absoluteY = e.absoluteY, s.fillY = s.absoluteY;
80
85
  });
81
86
  break;
82
87
  case "middle":
83
88
  case "alphabetic":
84
89
  case "ideographic":
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;
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;
87
92
  });
88
93
  break;
89
94
  case "bottom":
90
- s.absoluteY = s.relativeY + e - l.height, s.fragments.forEach((i) => {
91
- i.absoluteY = s.absoluteY, i.fillY = i.absoluteY + s.height;
95
+ e.absoluteY = e.relativeY + t - l.height, e.fragments.forEach((s) => {
96
+ s.absoluteY = e.absoluteY, s.fillY = s.absoluteY + e.height;
92
97
  });
93
98
  break;
94
99
  }
95
100
  }
96
- return { ...l, paragraphs: a };
101
+ return { ...l, paragraphs: i };
97
102
  }
98
- _createParagraphs(t) {
99
- const e = {
103
+ _createParagraphs(a) {
104
+ const t = {
100
105
  width: 0,
101
- actualBoundingBoxWidth: 0,
102
106
  height: 0,
107
+ actualBoundingBoxWidth: 0,
103
108
  relativeX: 0,
104
109
  relativeY: 0,
105
110
  absoluteX: 0,
106
111
  absoluteY: 0
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;
112
+ }, i = (l = {}) => ({ ...t, fragments: [], ...l }), o = (l = {}) => {
113
+ const h = [], { width: r, height: e, ...s } = this.style;
114
+ return h.push({
115
+ fillX: 0,
116
+ fillY: 0,
117
+ ...t,
118
+ ...l,
119
+ style: {
120
+ ...s,
121
+ ...l.style
122
+ },
123
+ data: l.data ?? ""
124
+ }), h;
110
125
  }, n = [];
111
- if (typeof t == "string")
112
- t && n.push(
113
- a({
114
- fragments: o({ data: t })
115
- })
116
- );
126
+ if (typeof a == "string")
127
+ n.push(i({ fragments: o({ data: a }) }));
117
128
  else {
118
- t = Array.isArray(t) ? t : [t];
119
- for (const l of t) {
120
- const h = a();
121
- if (l.fragments)
122
- for (const r of l.fragments)
123
- h.fragments.push(
124
- ...o({ data: r.data, style: { ...l.style, ...r.style } })
125
- );
126
- else
127
- l.data && h.fragments.push(
128
- ...o({ data: l.data, style: l.style })
129
- );
129
+ a = Array.isArray(a) ? a : [a];
130
+ for (const l of a) {
131
+ const h = i();
132
+ if ("fragments" in l) {
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 } }));
137
+ }
138
+ } else if ("data" in l) {
139
+ const { data: r, ...e } = l;
140
+ h.fragments.push(...o({ data: r, style: e }));
141
+ }
130
142
  n.push(h);
131
143
  }
132
144
  }
133
145
  return n;
134
146
  }
135
- _createWrapedParagraphs(t, e) {
136
- const a = [], o = t.slice();
147
+ _createWrapedParagraphs(a, t) {
148
+ const i = [], o = a.slice();
137
149
  let n, l;
138
150
  for (; n = o.shift(); ) {
139
151
  const h = n.fragments.slice();
140
152
  let r = 0;
141
- const s = [];
142
- let i = !0;
153
+ const e = [];
154
+ let s = !0;
143
155
  for (; l = h.shift(); ) {
144
- const u = this._getFragmentStyle(l.style);
156
+ const u = l.style;
145
157
  this._setContextStyle(u);
146
158
  let c = "", f = !1;
147
159
  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
- let b = x ? c.length + 1 : c.length;
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
- (w.length || h.length) && o.unshift({
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({
154
166
  ...n,
155
- fragments: (w.length ? [{ ...l, data: w }] : []).concat(h.slice())
167
+ fragments: (b.length ? [{ ...l, data: b }] : []).concat(h.slice())
156
168
  }), h.length = 0, f = !0;
157
169
  break;
158
170
  } else
159
171
  r += g;
160
172
  c += d;
161
173
  }
162
- f || s.push({ ...l }), i = !1;
174
+ f || e.push({ ...l }), s = !1;
163
175
  }
164
- s.length && a.push({ ...n, fragments: s });
176
+ e.length && i.push({ ...n, fragments: e });
165
177
  }
166
- return a;
178
+ return i;
167
179
  }
168
- _draw(t) {
169
- const e = this.context;
170
- t.forEach((a) => {
171
- a.fragments.forEach((o) => {
172
- const n = this._getFragmentStyle(o.style);
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) {
180
+ _draw(a) {
181
+ const t = this.context;
182
+ a.forEach((i) => {
183
+ i.fragments.forEach((o) => {
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) {
174
186
  case "underline":
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();
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();
176
188
  break;
177
189
  case "line-through":
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();
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();
179
191
  break;
180
192
  }
181
193
  });
182
194
  });
183
195
  }
184
- _resizeCanvas(t, e) {
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
- }
188
- _getFragmentStyle(t = {}) {
189
- return { ...this.style, ...t };
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));
190
199
  }
191
- _setContextStyle(t) {
192
- const e = this.context;
193
- 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 = [
194
- t.fontStyle,
195
- t.fontWeight,
196
- `${t.fontSize}px`,
197
- t.fontFamily
198
- ].join(" "), e.fontKerning = t.fontKerning, e.letterSpacing = `${t.letterSpacing}px`;
200
+ _setContextStyle(a) {
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`;
199
208
  }
200
209
  update() {
201
- const t = this.context;
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);
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);
205
215
  const n = this.pixelRatio;
206
- t.scale(n, n), t.clearRect(0, 0, t.canvas.width, t.canvas.height), this._draw(o.paragraphs);
216
+ a.scale(n, n), a.clearRect(0, 0, a.canvas.width, a.canvas.height), this._draw(o.paragraphs);
207
217
  }
208
218
  }
209
219
  export {
210
- X as Text
220
+ p as Text
211
221
  };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "modern-text",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "packageManager": "pnpm@8.14.1",
6
- "description": "Starter template for TypeScript library",
6
+ "description": "Text measurement and rendering of canvas 2d",
7
7
  "author": "wxm",
8
8
  "license": "MIT",
9
9
  "homepage": "https://github.com/qq15725/modern-text",
package/types/Text.d.ts CHANGED
@@ -5,15 +5,6 @@ export type TextWrap = 'wrap' | 'nowrap';
5
5
  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
- export interface TextParagraphWithStyle {
9
- data?: string;
10
- style?: Partial<TextFragmentStyle>;
11
- fragments?: Array<TextFragmentWithStyle>;
12
- }
13
- export interface TextFragmentWithStyle {
14
- data: string;
15
- style?: Partial<TextFragmentStyle>;
16
- }
17
8
  export interface TextParagraph {
18
9
  width: number;
19
10
  height: number;
@@ -25,8 +16,8 @@ export interface TextParagraph {
25
16
  }
26
17
  export interface TextFragment {
27
18
  width: number;
28
- actualBoundingBoxWidth: number;
29
19
  height: number;
20
+ actualBoundingBoxWidth: number;
30
21
  relativeX: number;
31
22
  relativeY: number;
32
23
  absoluteX: number;
@@ -34,11 +25,9 @@ export interface TextFragment {
34
25
  fillX: number;
35
26
  fillY: number;
36
27
  data: string;
37
- style: Partial<TextFragmentStyle>;
28
+ style: TextFragmentStyle;
38
29
  }
39
- export interface TextStyle {
40
- width: number;
41
- height: number;
30
+ export interface TextFragmentStyle {
42
31
  color: string;
43
32
  fontSize: number;
44
33
  fontWeight: FontWeight;
@@ -59,9 +48,21 @@ export interface TextStyle {
59
48
  shadowOffsetY: number;
60
49
  shadowBlur: number;
61
50
  }
62
- export type TextFragmentStyle = Omit<TextStyle, 'width' | 'height'>;
63
- export type TextData = string | TextParagraphWithStyle | Array<TextParagraphWithStyle>;
64
- export interface TextProperties {
51
+ export interface TextStyle extends TextFragmentStyle {
52
+ width: number | 'auto';
53
+ height: number | 'auto';
54
+ }
55
+ export interface TextParagraphWithDataAndStyle extends Partial<TextFragmentStyle> {
56
+ data: string;
57
+ }
58
+ export interface TextParagraphWithFragmentsAndStyle extends Partial<TextFragmentStyle> {
59
+ fragments: Array<TextFragmentWithStyle>;
60
+ }
61
+ export interface TextFragmentWithStyle extends Partial<TextFragmentStyle> {
62
+ data: string;
63
+ }
64
+ export type TextData = string | TextParagraphWithDataAndStyle | TextParagraphWithFragmentsAndStyle | Array<TextParagraphWithDataAndStyle | TextParagraphWithFragmentsAndStyle>;
65
+ export interface TextOptions {
65
66
  view?: HTMLCanvasElement;
66
67
  pixelRatio?: number;
67
68
  data?: TextData;
@@ -75,18 +76,18 @@ export interface MeasureResult {
75
76
  paragraphs: Array<TextParagraph>;
76
77
  }
77
78
  export declare class Text {
79
+ static get defaultStyle(): TextStyle;
78
80
  readonly view: HTMLCanvasElement;
79
81
  readonly context: CanvasRenderingContext2D;
80
82
  pixelRatio: number;
81
83
  style: TextStyle;
82
84
  data: TextData;
83
- constructor(properties?: TextProperties);
85
+ constructor(options?: TextOptions);
84
86
  measure(width?: number, height?: number): MeasureResult;
85
87
  protected _createParagraphs(data: TextData): Array<TextParagraph>;
86
88
  protected _createWrapedParagraphs(paragraphs: Array<TextParagraph>, width: number): Array<TextParagraph>;
87
89
  protected _draw(paragraphs: Array<TextParagraph>): void;
88
- protected _resizeCanvas(width: number, height: number): void;
89
- protected _getFragmentStyle(style?: Partial<TextStyle>): TextStyle;
90
- protected _setContextStyle(style: TextStyle): void;
90
+ protected _resizeView(width: number, height: number): void;
91
+ protected _setContextStyle(style: TextFragmentStyle): void;
91
92
  update(): void;
92
93
  }