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