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 +5 -5
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +62 -62
- package/package.json +1 -1
- package/types/Text.d.ts +7 -6
package/README.md
CHANGED
|
@@ -31,15 +31,15 @@ const text = new Text({
|
|
|
31
31
|
backgroundColor: '#0000FF',
|
|
32
32
|
textDecoration: 'underline',
|
|
33
33
|
},
|
|
34
|
-
|
|
34
|
+
data: [
|
|
35
35
|
{
|
|
36
36
|
fragments: [
|
|
37
|
-
{
|
|
38
|
-
{
|
|
37
|
+
{ data: 'He', style: { color: 'red', fontSize: 12 } },
|
|
38
|
+
{ data: 'llo', style: { color: 'black' } },
|
|
39
39
|
],
|
|
40
40
|
},
|
|
41
|
-
{
|
|
42
|
-
{
|
|
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:
|
|
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,
|
|
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:
|
|
6
|
-
|
|
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 =
|
|
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
|
|
36
|
-
|
|
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 =
|
|
40
|
-
const s =
|
|
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
|
|
42
|
+
let i = 0, u;
|
|
43
43
|
for (const c of s.fragments) {
|
|
44
|
-
const
|
|
45
|
-
this._setContextStyle(
|
|
46
|
-
const
|
|
47
|
-
c.relativeX =
|
|
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 =
|
|
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 =
|
|
54
|
-
const s =
|
|
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((
|
|
58
|
-
|
|
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((
|
|
64
|
-
|
|
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((
|
|
71
|
-
|
|
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((
|
|
79
|
-
|
|
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((
|
|
86
|
-
|
|
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((
|
|
91
|
-
|
|
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:
|
|
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
|
-
},
|
|
108
|
-
const h = [], r = l.
|
|
109
|
-
return h.push({ fillX: 0, fillY: 0, style: {}, ...e, ...l,
|
|
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
|
-
|
|
114
|
-
fragments: o({
|
|
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 =
|
|
120
|
+
const h = a();
|
|
121
121
|
if (l.fragments)
|
|
122
122
|
for (const r of l.fragments)
|
|
123
123
|
h.fragments.push(
|
|
124
|
-
...o({
|
|
124
|
+
...o({ data: r.data, style: { ...l.style, ...r.style } })
|
|
125
125
|
);
|
|
126
126
|
else
|
|
127
|
-
l.
|
|
128
|
-
...o({
|
|
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
|
|
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
|
|
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 = "",
|
|
147
|
-
for (const
|
|
148
|
-
const
|
|
149
|
-
if (x || u.textWrap === "wrap" && e && r +
|
|
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 +=
|
|
152
|
-
const w = l.
|
|
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,
|
|
156
|
-
}), h.length = 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 +=
|
|
160
|
-
c +=
|
|
159
|
+
r += g;
|
|
160
|
+
c += d;
|
|
161
161
|
}
|
|
162
|
-
|
|
162
|
+
f || s.push({ ...l }), i = !1;
|
|
163
163
|
}
|
|
164
|
-
s.length &&
|
|
164
|
+
s.length && a.push({ ...n, fragments: s });
|
|
165
165
|
}
|
|
166
|
-
return
|
|
166
|
+
return a;
|
|
167
167
|
}
|
|
168
168
|
_draw(t) {
|
|
169
169
|
const e = this.context;
|
|
170
|
-
t.forEach((
|
|
171
|
-
|
|
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.
|
|
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,
|
|
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,
|
|
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
|
|
186
|
-
|
|
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:
|
|
203
|
-
const o = this.measure(e,
|
|
204
|
-
e || (e = o.width),
|
|
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
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
|
-
|
|
9
|
+
data?: string;
|
|
10
10
|
style?: Partial<TextFragmentStyle>;
|
|
11
11
|
fragments?: Array<TextFragmentWithStyle>;
|
|
12
12
|
}
|
|
13
13
|
export interface TextFragmentWithStyle {
|
|
14
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
82
|
+
data: TextData;
|
|
82
83
|
constructor(properties?: TextProperties);
|
|
83
84
|
measure(width?: number, height?: number): MeasureResult;
|
|
84
|
-
protected _createParagraphs(
|
|
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;
|