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 +6 -6
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +117 -107
- package/package.json +2 -2
- package/types/Text.d.ts +22 -21
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',
|
|
38
|
-
{ data: 'llo',
|
|
37
|
+
{ data: 'He', color: 'red', fontSize: 12 },
|
|
38
|
+
{ data: 'llo', color: 'black' },
|
|
39
39
|
],
|
|
40
40
|
},
|
|
41
|
-
{ data: ', ',
|
|
42
|
-
{ data: 'World!',
|
|
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{
|
|
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(
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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(
|
|
35
|
-
let
|
|
36
|
-
|
|
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 =
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
let
|
|
43
|
-
for (const c of
|
|
44
|
-
const f =
|
|
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 =
|
|
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
|
-
|
|
54
|
+
e.width = u ? u.relativeX + Math.max(u.width, u.actualBoundingBoxWidth) : 0, n += e.height;
|
|
50
55
|
}
|
|
51
|
-
const l =
|
|
52
|
-
|
|
53
|
-
for (let h =
|
|
54
|
-
const
|
|
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
|
-
|
|
58
|
-
|
|
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
|
-
|
|
64
|
-
|
|
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
|
-
|
|
71
|
-
|
|
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
|
-
|
|
79
|
-
|
|
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
|
-
|
|
86
|
-
|
|
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
|
-
|
|
91
|
-
|
|
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:
|
|
101
|
+
return { ...l, paragraphs: i };
|
|
97
102
|
}
|
|
98
|
-
_createParagraphs(
|
|
99
|
-
const
|
|
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
|
-
},
|
|
108
|
-
const h = [], r =
|
|
109
|
-
return h.push({
|
|
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
|
|
112
|
-
|
|
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
|
-
|
|
119
|
-
for (const l of
|
|
120
|
-
const h =
|
|
121
|
-
if (l
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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(
|
|
136
|
-
const
|
|
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
|
|
142
|
-
let
|
|
153
|
+
const e = [];
|
|
154
|
+
let s = !0;
|
|
143
155
|
for (; l = h.shift(); ) {
|
|
144
|
-
const u =
|
|
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 + (
|
|
149
|
-
if (x || u.textWrap === "wrap" &&
|
|
150
|
-
let
|
|
151
|
-
!r && !
|
|
152
|
-
const
|
|
153
|
-
(
|
|
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: (
|
|
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 ||
|
|
174
|
+
f || e.push({ ...l }), s = !1;
|
|
163
175
|
}
|
|
164
|
-
|
|
176
|
+
e.length && i.push({ ...n, fragments: e });
|
|
165
177
|
}
|
|
166
|
-
return
|
|
178
|
+
return i;
|
|
167
179
|
}
|
|
168
|
-
_draw(
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const n =
|
|
173
|
-
switch (this._setContextStyle(n), n.textStrokeWidth &&
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
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(
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
`${
|
|
197
|
-
|
|
198
|
-
].join(" "),
|
|
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
|
|
202
|
-
let { width:
|
|
203
|
-
|
|
204
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"packageManager": "pnpm@8.14.1",
|
|
6
|
-
"description": "
|
|
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:
|
|
28
|
+
style: TextFragmentStyle;
|
|
38
29
|
}
|
|
39
|
-
export interface
|
|
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
|
|
63
|
-
|
|
64
|
-
|
|
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(
|
|
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
|
|
89
|
-
protected
|
|
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
|
}
|