modern-text 0.0.1
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/LICENSE +21 -0
- package/README.md +49 -0
- package/dist/index.cjs +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +211 -0
- package/package.json +65 -0
- package/types/Text.d.ts +91 -0
- package/types/index.d.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-PRESENT wxm
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<h1 align="center">modern-text</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://unpkg.com/modern-text">
|
|
5
|
+
<img src="https://img.shields.io/bundlephobia/minzip/modern-text" alt="Minzip">
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/modern-text">
|
|
8
|
+
<img src="https://img.shields.io/npm/v/modern-text.svg" alt="Version">
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://www.npmjs.com/package/modern-text">
|
|
11
|
+
<img src="https://img.shields.io/npm/dm/modern-text" alt="Downloads">
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://github.com/qq15725/modern-text/issues">
|
|
14
|
+
<img src="https://img.shields.io/github/issues/qq15725/modern-text" alt="Issues">
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://github.com/qq15725/modern-text/blob/main/LICENSE">
|
|
17
|
+
<img src="https://img.shields.io/npm/l/modern-text.svg" alt="License">
|
|
18
|
+
</a>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { Text } from 'modern-text'
|
|
25
|
+
|
|
26
|
+
const text = new Text({
|
|
27
|
+
style: {
|
|
28
|
+
width: 100,
|
|
29
|
+
height: 200,
|
|
30
|
+
fontSize: 22,
|
|
31
|
+
backgroundColor: '#0000FF',
|
|
32
|
+
textDecoration: 'underline',
|
|
33
|
+
},
|
|
34
|
+
content: [
|
|
35
|
+
{
|
|
36
|
+
fragments: [
|
|
37
|
+
{ content: 'He', style: { color: 'red', fontSize: 12 } },
|
|
38
|
+
{ content: 'llo', style: { color: 'black' } },
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
{ content: ', ', style: { color: 'grey' } },
|
|
42
|
+
{ content: 'World!', style: { color: 'black' } },
|
|
43
|
+
],
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
document.body.append(text.view) // canvas 2d
|
|
47
|
+
|
|
48
|
+
console.log(text.measure()) // boundingBox with paragraphs
|
|
49
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class p{constructor(t={}){const{view:e=document.createElement("canvas"),pixelRatio:i=window.devicePixelRatio||1,content:o="",style:h}=t;this.view=e,this.context=e.getContext("2d"),this.pixelRatio=i,this.content=o,this.style={width:0,height:0,color:"#000000",fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",textBaseline:"middle",textDecoration:null,textStrokeWidth:0,textStrokeColor:"#000000",direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:"#000000",shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0,...h},this.update()}measure(t=0,e=0){let i=this._createParagraphs(this.content);i=this._createWrapedParagraphs(i,t);const o=this.context;let h=0;for(let n=i.length,r=0;r<n;r++){const s=i[r];s.relativeX=0,s.relativeY=h,s.width=0,s.height=0;let a=0,u;for(const c of s.fragments){const g=this._getFragmentStyle(c.style);this._setContextStyle(g);const f=o.measureText(c.content),d=f.width,x=f.actualBoundingBoxRight+f.actualBoundingBoxLeft;c.relativeX=a,c.relativeY=s.relativeY,c.width=d,c.actualBoundingBoxWidth=x,c.height=g.fontSize*g.lineHeight,a+=d+g.letterSpacing,u=c,s.height=Math.max(s.height,c.height)}s.width=u?u.relativeX+Math.max(u.width,u.actualBoundingBoxWidth):0,h+=s.height}const l=i.reduce((n,r)=>(n.x=Math.min(n.x,r.relativeX),n.y=Math.min(n.y,r.relativeY),n.width=Math.max(n.width,r.width),n.height+=r.height,n),{x:0,y:0,width:0,height:0});t=t||l.width,e=Math.max(e,l.height);for(let n=i.length,r=0;r<n;r++){const s=i[r];switch(this.style.textAlign){case"center":s.absoluteX=(t-l.width)/2,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width/2});break;case"end":case"right":s.absoluteX=t-l.width,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX+a.width});break;case"start":case"left":default:s.absoluteX=0,s.fragments.forEach(a=>{a.absoluteX=s.absoluteX+a.relativeX,a.fillX=a.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":s.absoluteY=s.relativeY,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY});break;case"middle":case"alphabetic":case"ideographic":s.absoluteY=s.relativeY+(e-l.height)/2,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY+s.height/2});break;case"bottom":s.absoluteY=s.relativeY+e-l.height,s.fragments.forEach(a=>{a.absoluteY=s.absoluteY,a.fillY=a.absoluteY+s.height});break}}return{...l,paragraphs:i}}_createParagraphs(t){const e={width:0,actualBoundingBoxWidth:0,height:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},i=(l={})=>({...e,fragments:[],...l}),o=(l={})=>{const n=[],r=l.content??"";return n.push({fillX:0,fillY:0,style:{},...e,...l,content:r}),n},h=[];if(typeof t=="string")t&&h.push(i({fragments:o({content:t})}));else{t=Array.isArray(t)?t:[t];for(const l of t){const n=i();if(l.fragments)for(const r of l.fragments)n.fragments.push(...o({content:r.content,style:{...l.style,...r.style}}));else l.content&&n.fragments.push(...o({content:l.content,style:l.style}));h.push(n)}}return h}_createWrapedParagraphs(t,e){const i=[],o=t.slice();let h,l;for(;h=o.shift();){const n=h.fragments.slice();let r=0;const s=[];let a=!0;for(;l=n.shift();){const u=this._getFragmentStyle(l.style);this._setContextStyle(u);let c="",g=!1;for(const f of l.content){const d=this.context.measureText(f).width+(a?0:u.letterSpacing),x=/^[\r\n]$/.test(f);if(x||u.textWrap==="wrap"&&e&&r+d>e){let b=x?c.length+1:c.length;!r&&!b&&(c+=f,b++),c.length&&s.push({...l,text:c}),s.length&&(i.push({...h,fragments:s.slice()}),s.length=0);const w=l.content.substring(b);(w.length||n.length)&&o.unshift({...h,fragments:(w.length?[{...l,content:w}]:[]).concat(n.slice())}),n.length=0,g=!0;break}else r+=d;c+=f}g||s.push({...l}),a=!1}s.length&&i.push({...h,fragments:s})}return i}_draw(t){const e=this.context;t.forEach(i=>{i.fragments.forEach(o=>{const h=this._getFragmentStyle(o.style);switch(this._setContextStyle(h),h.textStrokeWidth&&e.strokeText(o.content,o.fillX,o.fillY),e.fillText(o.content,o.fillX,o.fillY),h.textDecoration){case"underline":e.beginPath(),e.moveTo(o.absoluteX,i.absoluteY+i.height-2),e.lineTo(o.absoluteX+o.width,i.absoluteY+i.height-2),e.stroke();break;case"line-through":e.beginPath(),e.moveTo(o.absoluteX,i.absoluteY+i.height/2),e.lineTo(o.absoluteX+o.width,i.absoluteY+i.height/2),e.stroke();break}})})}_resizeCanvas(t,e){const i=this.view;i.style.width=`${t}px`,i.style.height=`${e}px`,i.dataset.width=String(t),i.dataset.height=String(e),i.width=Math.max(1,Math.floor(t*this.pixelRatio)),i.height=Math.max(1,Math.floor(e*this.pixelRatio))}_getFragmentStyle(t={}){return{...this.style,...t}}_setContextStyle(t){const e=this.context;e.shadowColor=t.shadowColor,e.shadowOffsetX=t.shadowOffsetX,e.shadowOffsetY=t.shadowOffsetY,e.shadowBlur=t.shadowBlur,e.strokeStyle=t.textStrokeColor,e.lineWidth=t.textStrokeWidth,e.fillStyle=t.color,e.direction=t.direction,e.textAlign=t.textAlign,e.textBaseline=t.textBaseline,e.font=[t.fontStyle,t.fontWeight,`${t.fontSize}px`,t.fontFamily].join(" "),e.fontKerning=t.fontKerning,e.letterSpacing=`${t.letterSpacing}px`}update(){const t=this.context;let{width:e,height:i}=this.style;const o=this.measure(e,i);e||(e=o.width),i=Math.max(i,o.height),this._resizeCanvas(e,i);const h=this.pixelRatio;t.scale(h,h),t.clearRect(0,0,t.canvas.width,t.canvas.height),this._draw(o.paragraphs)}}exports.Text=p;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(g,x){typeof exports=="object"&&typeof module<"u"?x(exports):typeof define=="function"&&define.amd?define(["exports"],x):(g=typeof globalThis<"u"?globalThis:g||self,x(g.modernText={}))})(this,function(g){"use strict";class x{constructor(t={}){const{view:e=document.createElement("canvas"),pixelRatio:i=window.devicePixelRatio||1,content:a="",style:h}=t;this.view=e,this.context=e.getContext("2d"),this.pixelRatio=i,this.content=a,this.style={width:0,height:0,color:"#000000",fontSize:14,fontWeight:"normal",fontFamily:"sans-serif",fontStyle:"normal",fontKerning:"normal",textWrap:"wrap",textAlign:"start",textBaseline:"middle",textDecoration:null,textStrokeWidth:0,textStrokeColor:"#000000",direction:"inherit",lineHeight:1,letterSpacing:0,shadowColor:"#000000",shadowOffsetX:0,shadowOffsetY:0,shadowBlur:0,...h},this.update()}measure(t=0,e=0){let i=this._createParagraphs(this.content);i=this._createWrapedParagraphs(i,t);const a=this.context;let h=0;for(let n=i.length,r=0;r<n;r++){const s=i[r];s.relativeX=0,s.relativeY=h,s.width=0,s.height=0;let o=0,u;for(const c of s.fragments){const d=this._getFragmentStyle(c.style);this._setContextStyle(d);const f=a.measureText(c.content),b=f.width,w=f.actualBoundingBoxRight+f.actualBoundingBoxLeft;c.relativeX=o,c.relativeY=s.relativeY,c.width=b,c.actualBoundingBoxWidth=w,c.height=d.fontSize*d.lineHeight,o+=b+d.letterSpacing,u=c,s.height=Math.max(s.height,c.height)}s.width=u?u.relativeX+Math.max(u.width,u.actualBoundingBoxWidth):0,h+=s.height}const l=i.reduce((n,r)=>(n.x=Math.min(n.x,r.relativeX),n.y=Math.min(n.y,r.relativeY),n.width=Math.max(n.width,r.width),n.height+=r.height,n),{x:0,y:0,width:0,height:0});t=t||l.width,e=Math.max(e,l.height);for(let n=i.length,r=0;r<n;r++){const s=i[r];switch(this.style.textAlign){case"center":s.absoluteX=(t-l.width)/2,s.fragments.forEach(o=>{o.absoluteX=s.absoluteX+o.relativeX,o.fillX=o.absoluteX+o.width/2});break;case"end":case"right":s.absoluteX=t-l.width,s.fragments.forEach(o=>{o.absoluteX=s.absoluteX+o.relativeX,o.fillX=o.absoluteX+o.width});break;case"start":case"left":default:s.absoluteX=0,s.fragments.forEach(o=>{o.absoluteX=s.absoluteX+o.relativeX,o.fillX=o.absoluteX});break}switch(this.style.textBaseline){case"top":case"hanging":s.absoluteY=s.relativeY,s.fragments.forEach(o=>{o.absoluteY=s.absoluteY,o.fillY=o.absoluteY});break;case"middle":case"alphabetic":case"ideographic":s.absoluteY=s.relativeY+(e-l.height)/2,s.fragments.forEach(o=>{o.absoluteY=s.absoluteY,o.fillY=o.absoluteY+s.height/2});break;case"bottom":s.absoluteY=s.relativeY+e-l.height,s.fragments.forEach(o=>{o.absoluteY=s.absoluteY,o.fillY=o.absoluteY+s.height});break}}return{...l,paragraphs:i}}_createParagraphs(t){const e={width:0,actualBoundingBoxWidth:0,height:0,relativeX:0,relativeY:0,absoluteX:0,absoluteY:0},i=(l={})=>({...e,fragments:[],...l}),a=(l={})=>{const n=[],r=l.content??"";return n.push({fillX:0,fillY:0,style:{},...e,...l,content:r}),n},h=[];if(typeof t=="string")t&&h.push(i({fragments:a({content:t})}));else{t=Array.isArray(t)?t:[t];for(const l of t){const n=i();if(l.fragments)for(const r of l.fragments)n.fragments.push(...a({content:r.content,style:{...l.style,...r.style}}));else l.content&&n.fragments.push(...a({content:l.content,style:l.style}));h.push(n)}}return h}_createWrapedParagraphs(t,e){const i=[],a=t.slice();let h,l;for(;h=a.shift();){const n=h.fragments.slice();let r=0;const s=[];let o=!0;for(;l=n.shift();){const u=this._getFragmentStyle(l.style);this._setContextStyle(u);let c="",d=!1;for(const f of l.content){const b=this.context.measureText(f).width+(o?0:u.letterSpacing),w=/^[\r\n]$/.test(f);if(w||u.textWrap==="wrap"&&e&&r+b>e){let p=w?c.length+1:c.length;!r&&!p&&(c+=f,p++),c.length&&s.push({...l,text:c}),s.length&&(i.push({...h,fragments:s.slice()}),s.length=0);const X=l.content.substring(p);(X.length||n.length)&&a.unshift({...h,fragments:(X.length?[{...l,content:X}]:[]).concat(n.slice())}),n.length=0,d=!0;break}else r+=b;c+=f}d||s.push({...l}),o=!1}s.length&&i.push({...h,fragments:s})}return i}_draw(t){const e=this.context;t.forEach(i=>{i.fragments.forEach(a=>{const h=this._getFragmentStyle(a.style);switch(this._setContextStyle(h),h.textStrokeWidth&&e.strokeText(a.content,a.fillX,a.fillY),e.fillText(a.content,a.fillX,a.fillY),h.textDecoration){case"underline":e.beginPath(),e.moveTo(a.absoluteX,i.absoluteY+i.height-2),e.lineTo(a.absoluteX+a.width,i.absoluteY+i.height-2),e.stroke();break;case"line-through":e.beginPath(),e.moveTo(a.absoluteX,i.absoluteY+i.height/2),e.lineTo(a.absoluteX+a.width,i.absoluteY+i.height/2),e.stroke();break}})})}_resizeCanvas(t,e){const i=this.view;i.style.width=`${t}px`,i.style.height=`${e}px`,i.dataset.width=String(t),i.dataset.height=String(e),i.width=Math.max(1,Math.floor(t*this.pixelRatio)),i.height=Math.max(1,Math.floor(e*this.pixelRatio))}_getFragmentStyle(t={}){return{...this.style,...t}}_setContextStyle(t){const e=this.context;e.shadowColor=t.shadowColor,e.shadowOffsetX=t.shadowOffsetX,e.shadowOffsetY=t.shadowOffsetY,e.shadowBlur=t.shadowBlur,e.strokeStyle=t.textStrokeColor,e.lineWidth=t.textStrokeWidth,e.fillStyle=t.color,e.direction=t.direction,e.textAlign=t.textAlign,e.textBaseline=t.textBaseline,e.font=[t.fontStyle,t.fontWeight,`${t.fontSize}px`,t.fontFamily].join(" "),e.fontKerning=t.fontKerning,e.letterSpacing=`${t.letterSpacing}px`}update(){const t=this.context;let{width:e,height:i}=this.style;const a=this.measure(e,i);e||(e=a.width),i=Math.max(i,a.height),this._resizeCanvas(e,i);const h=this.pixelRatio;t.scale(h,h),t.clearRect(0,0,t.canvas.width,t.canvas.height),this._draw(a.paragraphs)}}g.Text=x,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
class X {
|
|
2
|
+
constructor(t = {}) {
|
|
3
|
+
const {
|
|
4
|
+
view: e = document.createElement("canvas"),
|
|
5
|
+
pixelRatio: i = window.devicePixelRatio || 1,
|
|
6
|
+
content: o = "",
|
|
7
|
+
style: n
|
|
8
|
+
} = t;
|
|
9
|
+
this.view = e, this.context = e.getContext("2d"), this.pixelRatio = i, this.content = o, this.style = {
|
|
10
|
+
width: 0,
|
|
11
|
+
height: 0,
|
|
12
|
+
color: "#000000",
|
|
13
|
+
fontSize: 14,
|
|
14
|
+
fontWeight: "normal",
|
|
15
|
+
fontFamily: "sans-serif",
|
|
16
|
+
fontStyle: "normal",
|
|
17
|
+
fontKerning: "normal",
|
|
18
|
+
textWrap: "wrap",
|
|
19
|
+
textAlign: "start",
|
|
20
|
+
textBaseline: "middle",
|
|
21
|
+
textDecoration: null,
|
|
22
|
+
textStrokeWidth: 0,
|
|
23
|
+
textStrokeColor: "#000000",
|
|
24
|
+
direction: "inherit",
|
|
25
|
+
lineHeight: 1,
|
|
26
|
+
letterSpacing: 0,
|
|
27
|
+
shadowColor: "#000000",
|
|
28
|
+
shadowOffsetX: 0,
|
|
29
|
+
shadowOffsetY: 0,
|
|
30
|
+
shadowBlur: 0,
|
|
31
|
+
...n
|
|
32
|
+
}, this.update();
|
|
33
|
+
}
|
|
34
|
+
measure(t = 0, e = 0) {
|
|
35
|
+
let i = this._createParagraphs(this.content);
|
|
36
|
+
i = this._createWrapedParagraphs(i, t);
|
|
37
|
+
const o = this.context;
|
|
38
|
+
let n = 0;
|
|
39
|
+
for (let h = i.length, r = 0; r < h; r++) {
|
|
40
|
+
const s = i[r];
|
|
41
|
+
s.relativeX = 0, s.relativeY = n, s.width = 0, s.height = 0;
|
|
42
|
+
let a = 0, u;
|
|
43
|
+
for (const c of s.fragments) {
|
|
44
|
+
const g = this._getFragmentStyle(c.style);
|
|
45
|
+
this._setContextStyle(g);
|
|
46
|
+
const f = o.measureText(c.content), d = f.width, x = f.actualBoundingBoxRight + f.actualBoundingBoxLeft;
|
|
47
|
+
c.relativeX = a, c.relativeY = s.relativeY, c.width = d, c.actualBoundingBoxWidth = x, c.height = g.fontSize * g.lineHeight, a += d + g.letterSpacing, u = c, s.height = Math.max(s.height, c.height);
|
|
48
|
+
}
|
|
49
|
+
s.width = u ? u.relativeX + Math.max(u.width, u.actualBoundingBoxWidth) : 0, n += s.height;
|
|
50
|
+
}
|
|
51
|
+
const l = i.reduce((h, r) => (h.x = Math.min(h.x, r.relativeX), h.y = Math.min(h.y, r.relativeY), h.width = Math.max(h.width, r.width), h.height += r.height, h), { x: 0, y: 0, width: 0, height: 0 });
|
|
52
|
+
t = t || l.width, e = Math.max(e, l.height);
|
|
53
|
+
for (let h = i.length, r = 0; r < h; r++) {
|
|
54
|
+
const s = i[r];
|
|
55
|
+
switch (this.style.textAlign) {
|
|
56
|
+
case "center":
|
|
57
|
+
s.absoluteX = (t - l.width) / 2, s.fragments.forEach((a) => {
|
|
58
|
+
a.absoluteX = s.absoluteX + a.relativeX, a.fillX = a.absoluteX + a.width / 2;
|
|
59
|
+
});
|
|
60
|
+
break;
|
|
61
|
+
case "end":
|
|
62
|
+
case "right":
|
|
63
|
+
s.absoluteX = t - l.width, s.fragments.forEach((a) => {
|
|
64
|
+
a.absoluteX = s.absoluteX + a.relativeX, a.fillX = a.absoluteX + a.width;
|
|
65
|
+
});
|
|
66
|
+
break;
|
|
67
|
+
case "start":
|
|
68
|
+
case "left":
|
|
69
|
+
default:
|
|
70
|
+
s.absoluteX = 0, s.fragments.forEach((a) => {
|
|
71
|
+
a.absoluteX = s.absoluteX + a.relativeX, a.fillX = a.absoluteX;
|
|
72
|
+
});
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
switch (this.style.textBaseline) {
|
|
76
|
+
case "top":
|
|
77
|
+
case "hanging":
|
|
78
|
+
s.absoluteY = s.relativeY, s.fragments.forEach((a) => {
|
|
79
|
+
a.absoluteY = s.absoluteY, a.fillY = a.absoluteY;
|
|
80
|
+
});
|
|
81
|
+
break;
|
|
82
|
+
case "middle":
|
|
83
|
+
case "alphabetic":
|
|
84
|
+
case "ideographic":
|
|
85
|
+
s.absoluteY = s.relativeY + (e - l.height) / 2, s.fragments.forEach((a) => {
|
|
86
|
+
a.absoluteY = s.absoluteY, a.fillY = a.absoluteY + s.height / 2;
|
|
87
|
+
});
|
|
88
|
+
break;
|
|
89
|
+
case "bottom":
|
|
90
|
+
s.absoluteY = s.relativeY + e - l.height, s.fragments.forEach((a) => {
|
|
91
|
+
a.absoluteY = s.absoluteY, a.fillY = a.absoluteY + s.height;
|
|
92
|
+
});
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { ...l, paragraphs: i };
|
|
97
|
+
}
|
|
98
|
+
_createParagraphs(t) {
|
|
99
|
+
const e = {
|
|
100
|
+
width: 0,
|
|
101
|
+
actualBoundingBoxWidth: 0,
|
|
102
|
+
height: 0,
|
|
103
|
+
relativeX: 0,
|
|
104
|
+
relativeY: 0,
|
|
105
|
+
absoluteX: 0,
|
|
106
|
+
absoluteY: 0
|
|
107
|
+
}, i = (l = {}) => ({ ...e, fragments: [], ...l }), o = (l = {}) => {
|
|
108
|
+
const h = [], r = l.content ?? "";
|
|
109
|
+
return h.push({ fillX: 0, fillY: 0, style: {}, ...e, ...l, content: r }), h;
|
|
110
|
+
}, n = [];
|
|
111
|
+
if (typeof t == "string")
|
|
112
|
+
t && n.push(
|
|
113
|
+
i({
|
|
114
|
+
fragments: o({ content: t })
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
else {
|
|
118
|
+
t = Array.isArray(t) ? t : [t];
|
|
119
|
+
for (const l of t) {
|
|
120
|
+
const h = i();
|
|
121
|
+
if (l.fragments)
|
|
122
|
+
for (const r of l.fragments)
|
|
123
|
+
h.fragments.push(
|
|
124
|
+
...o({ content: r.content, style: { ...l.style, ...r.style } })
|
|
125
|
+
);
|
|
126
|
+
else
|
|
127
|
+
l.content && h.fragments.push(
|
|
128
|
+
...o({ content: l.content, style: l.style })
|
|
129
|
+
);
|
|
130
|
+
n.push(h);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return n;
|
|
134
|
+
}
|
|
135
|
+
_createWrapedParagraphs(t, e) {
|
|
136
|
+
const i = [], o = t.slice();
|
|
137
|
+
let n, l;
|
|
138
|
+
for (; n = o.shift(); ) {
|
|
139
|
+
const h = n.fragments.slice();
|
|
140
|
+
let r = 0;
|
|
141
|
+
const s = [];
|
|
142
|
+
let a = !0;
|
|
143
|
+
for (; l = h.shift(); ) {
|
|
144
|
+
const u = this._getFragmentStyle(l.style);
|
|
145
|
+
this._setContextStyle(u);
|
|
146
|
+
let c = "", g = !1;
|
|
147
|
+
for (const f of l.content) {
|
|
148
|
+
const d = this.context.measureText(f).width + (a ? 0 : u.letterSpacing), x = /^[\r\n]$/.test(f);
|
|
149
|
+
if (x || u.textWrap === "wrap" && e && r + d > e) {
|
|
150
|
+
let b = x ? c.length + 1 : c.length;
|
|
151
|
+
!r && !b && (c += f, b++), c.length && s.push({ ...l, text: c }), s.length && (i.push({ ...n, fragments: s.slice() }), s.length = 0);
|
|
152
|
+
const w = l.content.substring(b);
|
|
153
|
+
(w.length || h.length) && o.unshift({
|
|
154
|
+
...n,
|
|
155
|
+
fragments: (w.length ? [{ ...l, content: w }] : []).concat(h.slice())
|
|
156
|
+
}), h.length = 0, g = !0;
|
|
157
|
+
break;
|
|
158
|
+
} else
|
|
159
|
+
r += d;
|
|
160
|
+
c += f;
|
|
161
|
+
}
|
|
162
|
+
g || s.push({ ...l }), a = !1;
|
|
163
|
+
}
|
|
164
|
+
s.length && i.push({ ...n, fragments: s });
|
|
165
|
+
}
|
|
166
|
+
return i;
|
|
167
|
+
}
|
|
168
|
+
_draw(t) {
|
|
169
|
+
const e = this.context;
|
|
170
|
+
t.forEach((i) => {
|
|
171
|
+
i.fragments.forEach((o) => {
|
|
172
|
+
const n = this._getFragmentStyle(o.style);
|
|
173
|
+
switch (this._setContextStyle(n), n.textStrokeWidth && e.strokeText(o.content, o.fillX, o.fillY), e.fillText(o.content, o.fillX, o.fillY), n.textDecoration) {
|
|
174
|
+
case "underline":
|
|
175
|
+
e.beginPath(), e.moveTo(o.absoluteX, i.absoluteY + i.height - 2), e.lineTo(o.absoluteX + o.width, i.absoluteY + i.height - 2), e.stroke();
|
|
176
|
+
break;
|
|
177
|
+
case "line-through":
|
|
178
|
+
e.beginPath(), e.moveTo(o.absoluteX, i.absoluteY + i.height / 2), e.lineTo(o.absoluteX + o.width, i.absoluteY + i.height / 2), e.stroke();
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
_resizeCanvas(t, e) {
|
|
185
|
+
const i = this.view;
|
|
186
|
+
i.style.width = `${t}px`, i.style.height = `${e}px`, i.dataset.width = String(t), i.dataset.height = String(e), i.width = Math.max(1, Math.floor(t * this.pixelRatio)), i.height = Math.max(1, Math.floor(e * this.pixelRatio));
|
|
187
|
+
}
|
|
188
|
+
_getFragmentStyle(t = {}) {
|
|
189
|
+
return { ...this.style, ...t };
|
|
190
|
+
}
|
|
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`;
|
|
199
|
+
}
|
|
200
|
+
update() {
|
|
201
|
+
const t = this.context;
|
|
202
|
+
let { width: e, height: i } = this.style;
|
|
203
|
+
const o = this.measure(e, i);
|
|
204
|
+
e || (e = o.width), i = Math.max(i, o.height), this._resizeCanvas(e, i);
|
|
205
|
+
const n = this.pixelRatio;
|
|
206
|
+
t.scale(n, n), t.clearRect(0, 0, t.canvas.width, t.canvas.height), this._draw(o.paragraphs);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
export {
|
|
210
|
+
X as Text
|
|
211
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "modern-text",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"packageManager": "pnpm@8.14.1",
|
|
6
|
+
"description": "Starter template for TypeScript library",
|
|
7
|
+
"author": "wxm",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://github.com/qq15725/modern-text",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/qq15725/modern-text.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/qq15725/modern-text/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"text",
|
|
19
|
+
"measure",
|
|
20
|
+
"canvas-text",
|
|
21
|
+
"typescript"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./types/index.d.ts",
|
|
27
|
+
"require": "./dist/index.cjs",
|
|
28
|
+
"import": "./dist/index.mjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"main": "dist/index.mjs",
|
|
32
|
+
"module": "dist/index.mjs",
|
|
33
|
+
"types": "types/index.d.ts",
|
|
34
|
+
"browser": "dist/index.js",
|
|
35
|
+
"typesVersions": {
|
|
36
|
+
"*": {
|
|
37
|
+
"*": [
|
|
38
|
+
"./types/*",
|
|
39
|
+
"./types/index.d.ts"
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"types"
|
|
46
|
+
],
|
|
47
|
+
"scripts": {
|
|
48
|
+
"dev": "vite docs",
|
|
49
|
+
"lint": "eslint src",
|
|
50
|
+
"test": "vitest",
|
|
51
|
+
"build": "vite build && tsc --project tsconfig.build.json",
|
|
52
|
+
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
|
|
53
|
+
"release": "bumpp package.json --commit \"release: v%s\" --push --all --tag"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@qq15725/eslint-config": "^1.1.0",
|
|
57
|
+
"@types/node": "^20.11.5",
|
|
58
|
+
"bumpp": "^9.3.0",
|
|
59
|
+
"conventional-changelog-cli": "^4.1.0",
|
|
60
|
+
"eslint": "^8.56.0",
|
|
61
|
+
"typescript": "^5.3.3",
|
|
62
|
+
"vite": "^5.0.11",
|
|
63
|
+
"vitest": "^1.2.1"
|
|
64
|
+
}
|
|
65
|
+
}
|
package/types/Text.d.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export type FontWeight = 'normal' | 'bold' | 'lighter' | 'bolder' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
2
|
+
export type FontStyle = 'normal' | 'italic' | 'oblique' | `oblique ${string}`;
|
|
3
|
+
export type FontKerning = 'auto' | 'none' | 'normal';
|
|
4
|
+
export type TextWrap = 'wrap' | 'nowrap';
|
|
5
|
+
export type TextAlign = 'center' | 'end' | 'left' | 'right' | 'start';
|
|
6
|
+
export type TextBaseline = 'alphabetic' | 'bottom' | 'hanging' | 'ideographic' | 'middle' | 'top';
|
|
7
|
+
export type TextDecoration = 'underline' | 'line-through';
|
|
8
|
+
export interface TextParagraphWithStyle {
|
|
9
|
+
content?: string;
|
|
10
|
+
style?: Partial<TextFragmentStyle>;
|
|
11
|
+
fragments?: Array<TextFragmentWithStyle>;
|
|
12
|
+
}
|
|
13
|
+
export interface TextFragmentWithStyle {
|
|
14
|
+
content: string;
|
|
15
|
+
style?: Partial<TextFragmentStyle>;
|
|
16
|
+
}
|
|
17
|
+
export interface TextParagraph {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
relativeX: number;
|
|
21
|
+
relativeY: number;
|
|
22
|
+
absoluteX: number;
|
|
23
|
+
absoluteY: number;
|
|
24
|
+
fragments: Array<TextFragment>;
|
|
25
|
+
}
|
|
26
|
+
export interface TextFragment {
|
|
27
|
+
width: number;
|
|
28
|
+
actualBoundingBoxWidth: number;
|
|
29
|
+
height: number;
|
|
30
|
+
relativeX: number;
|
|
31
|
+
relativeY: number;
|
|
32
|
+
absoluteX: number;
|
|
33
|
+
absoluteY: number;
|
|
34
|
+
fillX: number;
|
|
35
|
+
fillY: number;
|
|
36
|
+
content: string;
|
|
37
|
+
style: Partial<TextFragmentStyle>;
|
|
38
|
+
}
|
|
39
|
+
export interface TextStyle {
|
|
40
|
+
width: number;
|
|
41
|
+
height: number;
|
|
42
|
+
color: string;
|
|
43
|
+
fontSize: number;
|
|
44
|
+
fontWeight: FontWeight;
|
|
45
|
+
fontFamily: string;
|
|
46
|
+
fontStyle: FontStyle;
|
|
47
|
+
fontKerning: FontKerning;
|
|
48
|
+
textWrap: TextWrap;
|
|
49
|
+
textAlign: TextAlign;
|
|
50
|
+
textBaseline: TextBaseline;
|
|
51
|
+
textDecoration: TextDecoration | null;
|
|
52
|
+
textStrokeWidth: number;
|
|
53
|
+
textStrokeColor: string;
|
|
54
|
+
direction: 'inherit' | 'ltr' | 'rtl';
|
|
55
|
+
lineHeight: number;
|
|
56
|
+
letterSpacing: number;
|
|
57
|
+
shadowColor: string;
|
|
58
|
+
shadowOffsetX: number;
|
|
59
|
+
shadowOffsetY: number;
|
|
60
|
+
shadowBlur: number;
|
|
61
|
+
}
|
|
62
|
+
export type TextFragmentStyle = Omit<TextStyle, 'width' | 'height'>;
|
|
63
|
+
export interface TextProperties {
|
|
64
|
+
view?: HTMLCanvasElement;
|
|
65
|
+
pixelRatio?: number;
|
|
66
|
+
content?: string | Array<TextParagraphWithStyle>;
|
|
67
|
+
style?: Partial<TextStyle>;
|
|
68
|
+
}
|
|
69
|
+
export interface MeasureResult {
|
|
70
|
+
x: number;
|
|
71
|
+
y: number;
|
|
72
|
+
width: number;
|
|
73
|
+
height: number;
|
|
74
|
+
paragraphs: Array<TextParagraph>;
|
|
75
|
+
}
|
|
76
|
+
export declare class Text {
|
|
77
|
+
readonly view: HTMLCanvasElement;
|
|
78
|
+
readonly context: CanvasRenderingContext2D;
|
|
79
|
+
pixelRatio: number;
|
|
80
|
+
style: TextStyle;
|
|
81
|
+
content: string | Array<TextParagraphWithStyle>;
|
|
82
|
+
constructor(properties?: TextProperties);
|
|
83
|
+
measure(width?: number, height?: number): MeasureResult;
|
|
84
|
+
protected _createParagraphs(content: string | TextParagraphWithStyle | Array<TextParagraphWithStyle>): Array<TextParagraph>;
|
|
85
|
+
protected _createWrapedParagraphs(paragraphs: Array<TextParagraph>, width: number): Array<TextParagraph>;
|
|
86
|
+
protected _draw(paragraphs: Array<TextParagraph>): void;
|
|
87
|
+
protected _resizeCanvas(width: number, height: number): void;
|
|
88
|
+
protected _getFragmentStyle(style?: Partial<TextStyle>): TextStyle;
|
|
89
|
+
protected _setContextStyle(style: TextStyle): void;
|
|
90
|
+
update(): void;
|
|
91
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Text';
|