text-to-canvas 1.0.0 → 1.1.0
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/CHANGELOG.md +6 -0
- package/dist/text-to-canvas.cjs +22 -12
- package/dist/text-to-canvas.esm.min.js +151 -146
- package/dist/text-to-canvas.esm.min.js.map +1 -1
- package/dist/text-to-canvas.min.js +1 -1
- package/dist/text-to-canvas.min.js.map +1 -1
- package/dist/text-to-canvas.mjs +22 -12
- package/dist/text-to-canvas.umd.min.js +1 -1
- package/dist/text-to-canvas.umd.min.js.map +1 -1
- package/dist/types/model.d.ts +10 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## v1.1.0
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- New `overflow?: boolean` config parameter for `drawText()` API: True (default) to allow overflow; false to clip text to box.
|
|
13
|
+
|
|
8
14
|
## v1.0.0
|
|
9
15
|
|
|
10
16
|
- First official release 🎉
|
package/dist/text-to-canvas.cjs
CHANGED
|
@@ -475,6 +475,12 @@ const drawText = (ctx, text, config) => {
|
|
|
475
475
|
fontVariant: config.fontVariant,
|
|
476
476
|
fontWeight: config.fontWeight
|
|
477
477
|
});
|
|
478
|
+
const {
|
|
479
|
+
width: boxWidth,
|
|
480
|
+
height: boxHeight,
|
|
481
|
+
x: boxX = 0,
|
|
482
|
+
y: boxY = 0
|
|
483
|
+
} = config;
|
|
478
484
|
const {
|
|
479
485
|
lines: richLines,
|
|
480
486
|
height: totalHeight,
|
|
@@ -485,8 +491,8 @@ const drawText = (ctx, text, config) => {
|
|
|
485
491
|
words: Array.isArray(text) ? text : textToWords(text),
|
|
486
492
|
inferWhitespace: Array.isArray(text) ? config.inferWhitespace === void 0 || config.inferWhitespace : void 0,
|
|
487
493
|
// ignore since `text` is a string; we assume it already has all the whitespace it needs
|
|
488
|
-
x:
|
|
489
|
-
y:
|
|
494
|
+
x: boxX,
|
|
495
|
+
y: boxY,
|
|
490
496
|
width: config.width,
|
|
491
497
|
height: config.height,
|
|
492
498
|
align: config.align,
|
|
@@ -499,6 +505,11 @@ const drawText = (ctx, text, config) => {
|
|
|
499
505
|
ctx.textBaseline = textBaseline;
|
|
500
506
|
ctx.font = getTextStyle(baseFormat);
|
|
501
507
|
ctx.fillStyle = baseFormat.fontColor || DEFAULT_FONT_COLOR;
|
|
508
|
+
if (config.overflow === false) {
|
|
509
|
+
ctx.beginPath();
|
|
510
|
+
ctx.rect(boxX, boxY, boxWidth, boxHeight);
|
|
511
|
+
ctx.clip();
|
|
512
|
+
}
|
|
502
513
|
richLines.forEach((line) => {
|
|
503
514
|
line.forEach((pw) => {
|
|
504
515
|
if (!pw.isWhitespace) {
|
|
@@ -517,39 +528,38 @@ const drawText = (ctx, text, config) => {
|
|
|
517
528
|
});
|
|
518
529
|
});
|
|
519
530
|
if (config.debug) {
|
|
520
|
-
const
|
|
521
|
-
const
|
|
522
|
-
const yEnd = y + height;
|
|
531
|
+
const xEnd = boxX + boxWidth;
|
|
532
|
+
const yEnd = boxY + boxHeight;
|
|
523
533
|
let textAnchor;
|
|
524
534
|
if (config.align === "right") {
|
|
525
535
|
textAnchor = xEnd;
|
|
526
536
|
} else if (config.align === "left") {
|
|
527
|
-
textAnchor =
|
|
537
|
+
textAnchor = boxX;
|
|
528
538
|
} else {
|
|
529
|
-
textAnchor =
|
|
539
|
+
textAnchor = boxX + boxWidth / 2;
|
|
530
540
|
}
|
|
531
|
-
let debugY =
|
|
541
|
+
let debugY = boxY;
|
|
532
542
|
if (config.vAlign === "bottom") {
|
|
533
543
|
debugY = yEnd;
|
|
534
544
|
} else if (config.vAlign === "middle") {
|
|
535
|
-
debugY =
|
|
545
|
+
debugY = boxY + boxHeight / 2;
|
|
536
546
|
}
|
|
537
547
|
const debugColor = "#0C8CE9";
|
|
538
548
|
ctx.lineWidth = 1;
|
|
539
549
|
ctx.strokeStyle = debugColor;
|
|
540
|
-
ctx.strokeRect(
|
|
550
|
+
ctx.strokeRect(boxX, boxY, boxWidth, boxHeight);
|
|
541
551
|
ctx.lineWidth = 1;
|
|
542
552
|
if (!config.align || config.align === "center") {
|
|
543
553
|
ctx.strokeStyle = debugColor;
|
|
544
554
|
ctx.beginPath();
|
|
545
|
-
ctx.moveTo(textAnchor,
|
|
555
|
+
ctx.moveTo(textAnchor, boxY);
|
|
546
556
|
ctx.lineTo(textAnchor, yEnd);
|
|
547
557
|
ctx.stroke();
|
|
548
558
|
}
|
|
549
559
|
if (!config.vAlign || config.vAlign === "middle") {
|
|
550
560
|
ctx.strokeStyle = debugColor;
|
|
551
561
|
ctx.beginPath();
|
|
552
|
-
ctx.moveTo(
|
|
562
|
+
ctx.moveTo(boxX, debugY);
|
|
553
563
|
ctx.lineTo(xEnd, debugY);
|
|
554
564
|
ctx.stroke();
|
|
555
565
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
const
|
|
1
|
+
const D = "Arial";
|
|
2
2
|
const $ = "black", _ = (t, n) => Object.assign(
|
|
3
3
|
{},
|
|
4
4
|
{
|
|
5
|
-
fontFamily:
|
|
5
|
+
fontFamily: D,
|
|
6
6
|
fontSize: 14,
|
|
7
7
|
fontWeight: "400",
|
|
8
8
|
fontStyle: "",
|
|
@@ -15,49 +15,49 @@ const $ = "black", _ = (t, n) => Object.assign(
|
|
|
15
15
|
fontFamily: t,
|
|
16
16
|
fontSize: n,
|
|
17
17
|
fontStyle: e,
|
|
18
|
-
fontVariant:
|
|
19
|
-
fontWeight:
|
|
20
|
-
}) => `${e || ""} ${
|
|
18
|
+
fontVariant: s,
|
|
19
|
+
fontWeight: i
|
|
20
|
+
}) => `${e || ""} ${s || ""} ${i || ""} ${n ?? 14}px ${t || D}`.trim(), T = (t) => !!t.match(/^\s+$/), M = (t) => t.filter((n) => !T(n.text)), V = (t) => {
|
|
21
21
|
const n = { ...t };
|
|
22
22
|
return t.format && (n.format = { ...t.format }), n;
|
|
23
|
-
},
|
|
23
|
+
}, H = (t, n) => {
|
|
24
24
|
if (t.length <= 1 || n.length < 1)
|
|
25
25
|
return [...t];
|
|
26
26
|
const e = [];
|
|
27
|
-
return t.forEach((
|
|
28
|
-
e.push(
|
|
27
|
+
return t.forEach((s, i) => {
|
|
28
|
+
e.push(s), i < t.length - 1 && n.forEach((r) => e.push(V(r)));
|
|
29
29
|
}), e;
|
|
30
30
|
}, Z = ({
|
|
31
31
|
line: t,
|
|
32
32
|
spaceWidth: n,
|
|
33
33
|
spaceChar: e,
|
|
34
|
-
boxWidth:
|
|
34
|
+
boxWidth: s
|
|
35
35
|
}) => {
|
|
36
|
-
const
|
|
37
|
-
if (
|
|
36
|
+
const i = M(t);
|
|
37
|
+
if (i.length <= 1)
|
|
38
38
|
return t.concat();
|
|
39
|
-
const r =
|
|
40
|
-
(
|
|
41
|
-
var
|
|
42
|
-
return
|
|
39
|
+
const r = i.reduce(
|
|
40
|
+
(a, u) => {
|
|
41
|
+
var d;
|
|
42
|
+
return a + (((d = u.metrics) == null ? void 0 : d.width) ?? 0);
|
|
43
43
|
},
|
|
44
44
|
0
|
|
45
|
-
),
|
|
46
|
-
if (
|
|
47
|
-
const
|
|
45
|
+
), l = (s - r) / n;
|
|
46
|
+
if (i.length > 2) {
|
|
47
|
+
const a = Math.ceil(l / (i.length - 1)), u = Array.from({ length: a }, () => ({
|
|
48
48
|
text: e
|
|
49
|
-
})),
|
|
49
|
+
})), d = i.slice(0, i.length - 1), y = H(d, u), c = u.slice(
|
|
50
50
|
0,
|
|
51
|
-
Math.floor(
|
|
52
|
-
),
|
|
53
|
-
return [...
|
|
51
|
+
Math.floor(l) - (d.length - 1) * u.length
|
|
52
|
+
), h = i[i.length - 1];
|
|
53
|
+
return [...y, ...c, h];
|
|
54
54
|
}
|
|
55
|
-
const
|
|
56
|
-
{ length: Math.floor(
|
|
55
|
+
const o = Array.from(
|
|
56
|
+
{ length: Math.floor(l) },
|
|
57
57
|
() => ({ text: e })
|
|
58
58
|
);
|
|
59
|
-
return
|
|
60
|
-
},
|
|
59
|
+
return H(i, o);
|
|
60
|
+
}, x = (t, n = "both") => {
|
|
61
61
|
let e = 0;
|
|
62
62
|
if (n === "left" || n === "both") {
|
|
63
63
|
for (; e < t.length && T(t[e].text); e++)
|
|
@@ -69,11 +69,11 @@ const $ = "black", _ = (t, n) => Object.assign(
|
|
|
69
69
|
trimmedLine: []
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
|
-
let
|
|
72
|
+
let s = t.length;
|
|
73
73
|
if (n === "right" || n === "both") {
|
|
74
|
-
for (
|
|
74
|
+
for (s--; s >= 0 && T(t[s].text); s--)
|
|
75
75
|
;
|
|
76
|
-
if (
|
|
76
|
+
if (s++, s <= 0)
|
|
77
77
|
return {
|
|
78
78
|
trimmedLeft: [],
|
|
79
79
|
trimmedRight: t.concat(),
|
|
@@ -82,61 +82,61 @@ const $ = "black", _ = (t, n) => Object.assign(
|
|
|
82
82
|
}
|
|
83
83
|
return {
|
|
84
84
|
trimmedLeft: t.slice(0, e),
|
|
85
|
-
trimmedRight: t.slice(
|
|
86
|
-
trimmedLine: t.slice(e,
|
|
85
|
+
trimmedRight: t.slice(s),
|
|
86
|
+
trimmedLine: t.slice(e, s)
|
|
87
87
|
};
|
|
88
|
-
},
|
|
89
|
-
let
|
|
90
|
-
const I = (t) => `${t.text}${t.format ? JSON.stringify(t.format) : ""}`,
|
|
88
|
+
}, k = " ", Y = " ";
|
|
89
|
+
let v;
|
|
90
|
+
const I = (t) => `${t.text}${t.format ? JSON.stringify(t.format) : ""}`, X = (t, n = !0) => {
|
|
91
91
|
const e = [[]];
|
|
92
|
-
let
|
|
93
|
-
return t.forEach((
|
|
94
|
-
var
|
|
95
|
-
if (
|
|
96
|
-
for (let
|
|
92
|
+
let s = !1;
|
|
93
|
+
return t.forEach((i, r) => {
|
|
94
|
+
var l, o, a;
|
|
95
|
+
if (i.text.match(/^\n+$/)) {
|
|
96
|
+
for (let u = 0; u < i.text.length; u++)
|
|
97
97
|
e.push([]);
|
|
98
|
-
|
|
98
|
+
s = !0;
|
|
99
99
|
return;
|
|
100
100
|
}
|
|
101
|
-
if (T(
|
|
102
|
-
(
|
|
101
|
+
if (T(i.text)) {
|
|
102
|
+
(l = e.at(-1)) == null || l.push(i), s = !0;
|
|
103
103
|
return;
|
|
104
104
|
}
|
|
105
|
-
|
|
105
|
+
i.text !== "" && (n && !s && r > 0 && ((o = e.at(-1)) == null || o.push({ text: Y })), (a = e.at(-1)) == null || a.push(i), s = !1);
|
|
106
106
|
}), e;
|
|
107
|
-
},
|
|
107
|
+
}, q = ({
|
|
108
108
|
wrappedLines: t,
|
|
109
109
|
wordMap: n,
|
|
110
110
|
positioning: {
|
|
111
111
|
width: e,
|
|
112
|
-
height:
|
|
113
|
-
x:
|
|
112
|
+
height: s,
|
|
113
|
+
x: i = 0,
|
|
114
114
|
y: r = 0,
|
|
115
|
-
align:
|
|
116
|
-
vAlign:
|
|
115
|
+
align: l,
|
|
116
|
+
vAlign: o
|
|
117
117
|
}
|
|
118
118
|
}) => {
|
|
119
|
-
const
|
|
119
|
+
const a = i + e, u = r + s, d = (f) => (
|
|
120
120
|
// NOTE: `metrics` must exist as every `word` MUST have been measured at this point
|
|
121
121
|
f.metrics.fontBoundingBoxAscent + f.metrics.fontBoundingBoxDescent
|
|
122
|
-
),
|
|
123
|
-
(f) => f.reduce((
|
|
124
|
-
),
|
|
125
|
-
let
|
|
126
|
-
return
|
|
127
|
-
lines: t.map((f,
|
|
122
|
+
), y = t.map(
|
|
123
|
+
(f) => f.reduce((p, A) => Math.max(p, d(A)), 0)
|
|
124
|
+
), c = y.reduce((f, p) => f + p, 0);
|
|
125
|
+
let h, m;
|
|
126
|
+
return o === "top" ? (m = "top", h = r) : o === "bottom" ? (m = "bottom", h = u - c) : (m = "top", h = r + s / 2 - c / 2), {
|
|
127
|
+
lines: t.map((f, p) => {
|
|
128
128
|
const A = f.reduce(
|
|
129
129
|
// NOTE: `metrics` must exist as every `word` MUST have been measured at this point
|
|
130
|
-
(W,
|
|
130
|
+
(W, b) => W + b.metrics.width,
|
|
131
131
|
0
|
|
132
|
-
), L = p
|
|
132
|
+
), L = y[p];
|
|
133
133
|
let B;
|
|
134
|
-
|
|
135
|
-
let
|
|
134
|
+
l === "right" ? B = a - A : l === "left" ? B = i : B = i + e / 2 - A / 2;
|
|
135
|
+
let F = B;
|
|
136
136
|
const z = f.map((W) => {
|
|
137
|
-
const
|
|
137
|
+
const b = I(W), { format: J } = n.get(b), U = F, O = d(W);
|
|
138
138
|
let E;
|
|
139
|
-
return
|
|
139
|
+
return o === "top" ? E = h : o === "bottom" ? E = h + L : E = h + (L - O) / 2, F += W.metrics.width, {
|
|
140
140
|
word: W,
|
|
141
141
|
format: J,
|
|
142
142
|
// undefined IF base formatting should be used when rendering (i.e. `word.format` is undefined)
|
|
@@ -147,13 +147,13 @@ const I = (t) => `${t.text}${t.format ? JSON.stringify(t.format) : ""}`, q = (t,
|
|
|
147
147
|
isWhitespace: T(W.text)
|
|
148
148
|
};
|
|
149
149
|
});
|
|
150
|
-
return
|
|
150
|
+
return h += L, z;
|
|
151
151
|
}),
|
|
152
|
-
textBaseline:
|
|
152
|
+
textBaseline: m,
|
|
153
153
|
textAlign: "left",
|
|
154
154
|
// always per current algorithm
|
|
155
155
|
width: e,
|
|
156
|
-
height:
|
|
156
|
+
height: c
|
|
157
157
|
};
|
|
158
158
|
}, N = function(t, n) {
|
|
159
159
|
if (t === "metrics" && n && typeof n == "object") {
|
|
@@ -165,50 +165,50 @@ const I = (t) => `${t.text}${t.format ? JSON.stringify(t.format) : ""}`, q = (t,
|
|
|
165
165
|
};
|
|
166
166
|
}
|
|
167
167
|
return n;
|
|
168
|
-
},
|
|
168
|
+
}, G = (t) => JSON.stringify(t, N), K = (t) => JSON.stringify(t, N), C = ({
|
|
169
169
|
ctx: t,
|
|
170
170
|
word: n,
|
|
171
171
|
wordMap: e,
|
|
172
|
-
baseTextFormat:
|
|
172
|
+
baseTextFormat: s
|
|
173
173
|
}) => {
|
|
174
|
-
const
|
|
174
|
+
const i = I(n);
|
|
175
175
|
if (n.metrics) {
|
|
176
|
-
if (!e.has(
|
|
177
|
-
let
|
|
178
|
-
n.format && (
|
|
176
|
+
if (!e.has(i)) {
|
|
177
|
+
let a;
|
|
178
|
+
n.format && (a = _(n.format, s)), e.set(i, { metrics: n.metrics, format: a });
|
|
179
179
|
}
|
|
180
180
|
return n.metrics.width;
|
|
181
181
|
}
|
|
182
|
-
if (e.has(
|
|
183
|
-
const { metrics:
|
|
184
|
-
return n.metrics =
|
|
182
|
+
if (e.has(i)) {
|
|
183
|
+
const { metrics: a } = e.get(i);
|
|
184
|
+
return n.metrics = a, a.width;
|
|
185
185
|
}
|
|
186
|
-
let r = !1,
|
|
187
|
-
n.format && (t.save(), r = !0,
|
|
188
|
-
const
|
|
189
|
-
return typeof
|
|
190
|
-
},
|
|
186
|
+
let r = !1, l;
|
|
187
|
+
n.format && (t.save(), r = !0, l = _(n.format, s), t.font = S(l)), v || (r || (t.save(), r = !0), t.textBaseline = "bottom");
|
|
188
|
+
const o = t.measureText(n.text);
|
|
189
|
+
return typeof o.fontBoundingBoxAscent == "number" ? v = !0 : (v = !1, o.fontBoundingBoxAscent = o.actualBoundingBoxAscent, o.fontBoundingBoxDescent = 0), n.metrics = o, e.set(i, { metrics: o, format: l }), r && t.restore(), o.width;
|
|
190
|
+
}, P = ({
|
|
191
191
|
ctx: t,
|
|
192
192
|
words: n,
|
|
193
193
|
justify: e,
|
|
194
|
-
format:
|
|
195
|
-
inferWhitespace:
|
|
194
|
+
format: s,
|
|
195
|
+
inferWhitespace: i = !0,
|
|
196
196
|
...r
|
|
197
197
|
// rest of params are related to positioning
|
|
198
198
|
}) => {
|
|
199
|
-
const
|
|
200
|
-
let f = 0,
|
|
201
|
-
return
|
|
202
|
-
const B =
|
|
203
|
-
return !g && f + B >
|
|
204
|
-
}), { lineWidth: f, splitPoint:
|
|
199
|
+
const l = /* @__PURE__ */ new Map(), o = _(s), { width: a } = r, u = (m, g = !1) => {
|
|
200
|
+
let f = 0, p = 0;
|
|
201
|
+
return m.every((A, L) => {
|
|
202
|
+
const B = C({ ctx: t, word: A, wordMap: l, baseTextFormat: o });
|
|
203
|
+
return !g && f + B > a ? (L === 0 && (p = 1, f = B), !1) : (p++, f += B, !0);
|
|
204
|
+
}), { lineWidth: f, splitPoint: p };
|
|
205
205
|
};
|
|
206
206
|
t.save();
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
207
|
+
const d = X(
|
|
208
|
+
x(n).trimmedLine,
|
|
209
|
+
i
|
|
210
210
|
);
|
|
211
|
-
if (
|
|
211
|
+
if (d.length <= 0 || a <= 0 || r.height <= 0 || s && typeof s.fontSize == "number" && s.fontSize <= 0)
|
|
212
212
|
return {
|
|
213
213
|
lines: [],
|
|
214
214
|
textAlign: "center",
|
|
@@ -216,62 +216,62 @@ const I = (t) => `${t.text}${t.format ? JSON.stringify(t.format) : ""}`, q = (t,
|
|
|
216
216
|
width: r.width,
|
|
217
217
|
height: 0
|
|
218
218
|
};
|
|
219
|
-
t.font = S(
|
|
220
|
-
const
|
|
221
|
-
for (const
|
|
222
|
-
let { splitPoint: g } =
|
|
223
|
-
if (g >=
|
|
224
|
-
|
|
219
|
+
t.font = S(o);
|
|
220
|
+
const y = e ? C({ ctx: t, word: { text: k }, wordMap: l, baseTextFormat: o }) : 0, c = [];
|
|
221
|
+
for (const m of d) {
|
|
222
|
+
let { splitPoint: g } = u(m);
|
|
223
|
+
if (g >= m.length)
|
|
224
|
+
c.push(m);
|
|
225
225
|
else {
|
|
226
|
-
let f =
|
|
226
|
+
let f = m.concat();
|
|
227
227
|
for (; g < f.length; ) {
|
|
228
|
-
const
|
|
228
|
+
const p = x(
|
|
229
229
|
f.slice(0, g),
|
|
230
230
|
"right"
|
|
231
231
|
).trimmedLine;
|
|
232
|
-
|
|
232
|
+
c.push(p), f = x(f.slice(g), "left").trimmedLine, { splitPoint: g } = u(f);
|
|
233
233
|
}
|
|
234
|
-
|
|
234
|
+
c.push(f);
|
|
235
235
|
}
|
|
236
236
|
}
|
|
237
|
-
e &&
|
|
238
|
-
if (g <
|
|
237
|
+
e && c.length > 1 && c.forEach((m, g) => {
|
|
238
|
+
if (g < c.length - 1) {
|
|
239
239
|
const f = Z({
|
|
240
|
-
line:
|
|
241
|
-
spaceWidth:
|
|
242
|
-
spaceChar:
|
|
243
|
-
boxWidth:
|
|
240
|
+
line: m,
|
|
241
|
+
spaceWidth: y,
|
|
242
|
+
spaceChar: k,
|
|
243
|
+
boxWidth: a
|
|
244
244
|
});
|
|
245
|
-
|
|
245
|
+
u(f, !0), c[g] = f;
|
|
246
246
|
}
|
|
247
247
|
});
|
|
248
|
-
const
|
|
249
|
-
wrappedLines:
|
|
250
|
-
wordMap:
|
|
248
|
+
const h = q({
|
|
249
|
+
wrappedLines: c,
|
|
250
|
+
wordMap: l,
|
|
251
251
|
positioning: r
|
|
252
252
|
});
|
|
253
|
-
return t.restore(),
|
|
254
|
-
},
|
|
253
|
+
return t.restore(), h;
|
|
254
|
+
}, j = (t) => {
|
|
255
255
|
const n = [];
|
|
256
|
-
let e,
|
|
257
|
-
return Array.from(t.trim()).forEach((
|
|
258
|
-
const r = T(
|
|
259
|
-
r && !
|
|
256
|
+
let e, s = !1;
|
|
257
|
+
return Array.from(t.trim()).forEach((i) => {
|
|
258
|
+
const r = T(i);
|
|
259
|
+
r && !s || !r && s ? (s = r, e && n.push(e), e = { text: i }) : (e || (e = { text: "" }), e.text += i);
|
|
260
260
|
}), e && n.push(e), n;
|
|
261
|
-
},
|
|
262
|
-
const e =
|
|
263
|
-
return
|
|
261
|
+
}, Q = ({ text: t, ...n }) => {
|
|
262
|
+
const e = j(t);
|
|
263
|
+
return P({
|
|
264
264
|
...n,
|
|
265
265
|
words: e,
|
|
266
266
|
inferWhitespace: !1
|
|
267
267
|
}).lines.map(
|
|
268
|
-
(
|
|
268
|
+
(i) => i.map(({ word: { text: r } }) => r).join("")
|
|
269
269
|
);
|
|
270
270
|
}, R = (t, n, e) => {
|
|
271
|
-
const
|
|
271
|
+
const s = t.textBaseline, i = t.font;
|
|
272
272
|
t.textBaseline = "bottom", e && (t.font = e);
|
|
273
273
|
const { actualBoundingBoxAscent: r } = t.measureText(n);
|
|
274
|
-
return t.textBaseline =
|
|
274
|
+
return t.textBaseline = s, e && (t.font = i), r;
|
|
275
275
|
}, w = ({
|
|
276
276
|
ctx: t,
|
|
277
277
|
word: n
|
|
@@ -280,45 +280,50 @@ const I = (t) => `${t.text}${t.format ? JSON.stringify(t.format) : ""}`, q = (t,
|
|
|
280
280
|
text: n,
|
|
281
281
|
style: e
|
|
282
282
|
}) => R(t, n, e), et = (t, n, e) => {
|
|
283
|
-
const
|
|
283
|
+
const s = _({
|
|
284
284
|
fontFamily: e.fontFamily,
|
|
285
285
|
fontSize: e.fontSize,
|
|
286
286
|
fontStyle: e.fontStyle,
|
|
287
287
|
fontVariant: e.fontVariant,
|
|
288
288
|
fontWeight: e.fontWeight
|
|
289
289
|
}), {
|
|
290
|
-
|
|
290
|
+
width: i,
|
|
291
291
|
height: r,
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
} =
|
|
292
|
+
x: l = 0,
|
|
293
|
+
y: o = 0
|
|
294
|
+
} = e, {
|
|
295
|
+
lines: a,
|
|
296
|
+
height: u,
|
|
297
|
+
textBaseline: d,
|
|
298
|
+
textAlign: y
|
|
299
|
+
} = P({
|
|
295
300
|
ctx: t,
|
|
296
|
-
words: Array.isArray(n) ? n :
|
|
301
|
+
words: Array.isArray(n) ? n : j(n),
|
|
297
302
|
inferWhitespace: Array.isArray(n) ? e.inferWhitespace === void 0 || e.inferWhitespace : void 0,
|
|
298
303
|
// ignore since `text` is a string; we assume it already has all the whitespace it needs
|
|
299
|
-
x:
|
|
300
|
-
y:
|
|
304
|
+
x: l,
|
|
305
|
+
y: o,
|
|
301
306
|
width: e.width,
|
|
302
307
|
height: e.height,
|
|
303
308
|
align: e.align,
|
|
304
309
|
vAlign: e.vAlign,
|
|
305
310
|
justify: e.justify,
|
|
306
|
-
format:
|
|
311
|
+
format: s
|
|
307
312
|
});
|
|
308
|
-
if (t.save(), t.textAlign =
|
|
309
|
-
|
|
310
|
-
|
|
313
|
+
if (t.save(), t.textAlign = y, t.textBaseline = d, t.font = S(s), t.fillStyle = s.fontColor || $, e.overflow === !1 && (t.beginPath(), t.rect(l, o, i, r), t.clip()), a.forEach((c) => {
|
|
314
|
+
c.forEach((h) => {
|
|
315
|
+
h.isWhitespace || (h.format && (t.save(), t.font = S(h.format), h.format.fontColor && (t.fillStyle = h.format.fontColor)), t.fillText(h.word.text, h.x, h.y), h.format && t.restore());
|
|
311
316
|
});
|
|
312
317
|
}), e.debug) {
|
|
313
|
-
const
|
|
314
|
-
let
|
|
315
|
-
e.align === "right" ?
|
|
316
|
-
let g =
|
|
317
|
-
e.vAlign === "bottom" ? g =
|
|
318
|
+
const c = l + i, h = o + r;
|
|
319
|
+
let m;
|
|
320
|
+
e.align === "right" ? m = c : e.align === "left" ? m = l : m = l + i / 2;
|
|
321
|
+
let g = o;
|
|
322
|
+
e.vAlign === "bottom" ? g = h : e.vAlign === "middle" && (g = o + r / 2);
|
|
318
323
|
const f = "#0C8CE9";
|
|
319
|
-
t.lineWidth = 1, t.strokeStyle = f, t.strokeRect(
|
|
324
|
+
t.lineWidth = 1, t.strokeStyle = f, t.strokeRect(l, o, i, r), t.lineWidth = 1, (!e.align || e.align === "center") && (t.strokeStyle = f, t.beginPath(), t.moveTo(m, o), t.lineTo(m, h), t.stroke()), (!e.vAlign || e.vAlign === "middle") && (t.strokeStyle = f, t.beginPath(), t.moveTo(l, g), t.lineTo(c, g), t.stroke());
|
|
320
325
|
}
|
|
321
|
-
return t.restore(), { height:
|
|
326
|
+
return t.restore(), { height: u };
|
|
322
327
|
};
|
|
323
328
|
export {
|
|
324
329
|
et as drawText,
|
|
@@ -326,10 +331,10 @@ export {
|
|
|
326
331
|
tt as getTextHeight,
|
|
327
332
|
S as getTextStyle,
|
|
328
333
|
w as getWordHeight,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
+
G as specToJson,
|
|
335
|
+
Q as splitText,
|
|
336
|
+
P as splitWords,
|
|
337
|
+
j as textToWords,
|
|
338
|
+
K as wordsToJson
|
|
334
339
|
};
|
|
335
340
|
//# sourceMappingURL=text-to-canvas.esm.min.js.map
|