modern-text 0.3.6 → 0.3.8
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 +76 -73
- package/dist/index.d.cts +117 -122
- package/dist/index.d.mts +117 -122
- package/dist/index.d.ts +117 -122
- package/dist/index.js +4 -4
- package/dist/index.mjs +78 -74
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -174,12 +174,21 @@ class Character {
|
|
|
174
174
|
get fontHeight() {
|
|
175
175
|
return this.fontSize * this.computedStyle.lineHeight;
|
|
176
176
|
}
|
|
177
|
-
_getFontSfnt(
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
177
|
+
_getFontSfnt(fonts) {
|
|
178
|
+
const fontFamily = this.computedStyle.fontFamily;
|
|
179
|
+
let sfnt;
|
|
180
|
+
if (fontFamily) {
|
|
181
|
+
if (!sfnt) {
|
|
182
|
+
const font = fonts?.[fontFamily];
|
|
183
|
+
if (font instanceof modernFont.Ttf || font instanceof modernFont.Woff) {
|
|
184
|
+
sfnt = font.sfnt;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!sfnt) {
|
|
188
|
+
sfnt = modernFont.fonts.get(fontFamily)?.getSfnt();
|
|
189
|
+
}
|
|
181
190
|
}
|
|
182
|
-
return
|
|
191
|
+
return sfnt ?? modernFont.fonts.fallbackFont?.getSfnt();
|
|
183
192
|
}
|
|
184
193
|
updateGlyph(sfnt = this._getFontSfnt()) {
|
|
185
194
|
if (!sfnt) {
|
|
@@ -206,8 +215,8 @@ class Character {
|
|
|
206
215
|
this.centerDiviation = advanceHeight / 2 - baseline;
|
|
207
216
|
return this;
|
|
208
217
|
}
|
|
209
|
-
update(
|
|
210
|
-
const sfnt = this._getFontSfnt(
|
|
218
|
+
update(fonts) {
|
|
219
|
+
const sfnt = this._getFontSfnt(fonts);
|
|
211
220
|
if (!sfnt) {
|
|
212
221
|
return this;
|
|
213
222
|
}
|
|
@@ -447,6 +456,10 @@ class Paragraph {
|
|
|
447
456
|
}
|
|
448
457
|
}
|
|
449
458
|
|
|
459
|
+
function definePlugin(options) {
|
|
460
|
+
return options;
|
|
461
|
+
}
|
|
462
|
+
|
|
450
463
|
class Measurer {
|
|
451
464
|
constructor(_text) {
|
|
452
465
|
this._text = _text;
|
|
@@ -642,65 +655,6 @@ class Measurer {
|
|
|
642
655
|
}
|
|
643
656
|
}
|
|
644
657
|
|
|
645
|
-
class Parser {
|
|
646
|
-
constructor(_text) {
|
|
647
|
-
this._text = _text;
|
|
648
|
-
}
|
|
649
|
-
parse() {
|
|
650
|
-
let { content, computedStyle: style } = this._text;
|
|
651
|
-
const paragraphs = [];
|
|
652
|
-
if (typeof content === "string") {
|
|
653
|
-
const paragraph = new Paragraph({}, style);
|
|
654
|
-
paragraph.addFragment(content);
|
|
655
|
-
paragraphs.push(paragraph);
|
|
656
|
-
} else {
|
|
657
|
-
content = Array.isArray(content) ? content : [content];
|
|
658
|
-
for (const p of content) {
|
|
659
|
-
if (typeof p === "string") {
|
|
660
|
-
const paragraph = new Paragraph({}, style);
|
|
661
|
-
paragraph.addFragment(p);
|
|
662
|
-
paragraphs.push(paragraph);
|
|
663
|
-
} else if (Array.isArray(p)) {
|
|
664
|
-
const paragraph = new Paragraph({}, style);
|
|
665
|
-
p.forEach((f) => {
|
|
666
|
-
if (typeof f === "string") {
|
|
667
|
-
paragraph.addFragment(f);
|
|
668
|
-
} else {
|
|
669
|
-
const { content: content2, ...fStyle } = f;
|
|
670
|
-
if (content2 !== void 0) {
|
|
671
|
-
paragraph.addFragment(content2, fStyle);
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
});
|
|
675
|
-
paragraphs.push(paragraph);
|
|
676
|
-
} else if ("fragments" in p) {
|
|
677
|
-
const { fragments, ...pStyle } = p;
|
|
678
|
-
const paragraph = new Paragraph(pStyle, style);
|
|
679
|
-
fragments.forEach((f) => {
|
|
680
|
-
const { content: content2, ...fStyle } = f;
|
|
681
|
-
if (content2 !== void 0) {
|
|
682
|
-
paragraph.addFragment(content2, fStyle);
|
|
683
|
-
}
|
|
684
|
-
});
|
|
685
|
-
paragraphs.push(paragraph);
|
|
686
|
-
} else if ("content" in p) {
|
|
687
|
-
const { content: pData, ...pStyle } = p;
|
|
688
|
-
if (pData !== void 0) {
|
|
689
|
-
const paragraph = new Paragraph(pStyle, style);
|
|
690
|
-
paragraph.addFragment(pData);
|
|
691
|
-
paragraphs.push(paragraph);
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
return paragraphs;
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
function definePlugin(options) {
|
|
701
|
-
return options;
|
|
702
|
-
}
|
|
703
|
-
|
|
704
658
|
const defaultReferImage = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI3MiIgaGVpZ2h0PSI3MiIgdmlld0JveD0iMCAwIDcyIDcyIiBmaWxsPSJub25lIj48cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTMyLjQwMjkgMjhIMzUuMTU5NFYzMy4xNzcxQzM1Ljk4MjEgMzIuMzExNSAzNi45NzEgMzEuODczNyAzOC4wOTQ4IDMxLjg3MzdDMzkuNjY3NiAzMS44NzM3IDQwLjkxNjYgMzIuNDI5NSA0MS44MzkgMzMuNTQzN0w0MS44NDAzIDMzLjU0NTNDNDIuNjcxNyAzNC41NzA1IDQzLjA5MTUgMzUuODU1OSA0My4wOTE1IDM3LjM4NzdDNDMuMDkxNSAzOC45NzYxIDQyLjY3MjkgNDAuMzAyOCA0MS44MTgzIDQxLjMzMDRMNDEuODE3MSA0MS4zMzE4QzQwLjg3MzEgNDIuNDQ2MSAzOS41ODMyIDQzIDM3Ljk3MjEgNDNDMzYuNzQ3NyA0MyAzNS43ND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c1M1pNMjIuMjEyNSAzNi45MDc2TDI0LjI1OTYgMzEuNDUzOUwyNi4yODg1IDM2LjkwNzZIMjIuMjEyNVoiIGZpbGw9IiMyMjI1MjkiLz48L3N2Zz4=";
|
|
705
659
|
function parseCharsPerRepeat(size, fontSize, total) {
|
|
706
660
|
if (size === "cover") {
|
|
@@ -1055,7 +1009,7 @@ const defaultTextStyles = {
|
|
|
1055
1009
|
// font
|
|
1056
1010
|
fontSize: 14,
|
|
1057
1011
|
fontWeight: "normal",
|
|
1058
|
-
fontFamily: "
|
|
1012
|
+
fontFamily: "",
|
|
1059
1013
|
fontStyle: "normal",
|
|
1060
1014
|
fontKerning: "normal",
|
|
1061
1015
|
// text
|
|
@@ -1106,15 +1060,15 @@ class Text {
|
|
|
1106
1060
|
__publicField(this, "glyphBox", new modernPath2d.BoundingBox());
|
|
1107
1061
|
__publicField(this, "pathBox", new modernPath2d.BoundingBox());
|
|
1108
1062
|
__publicField(this, "boundingBox", new modernPath2d.BoundingBox());
|
|
1109
|
-
__publicField(this, "parser", new Parser(this));
|
|
1110
1063
|
__publicField(this, "measurer", new Measurer(this));
|
|
1111
1064
|
__publicField(this, "plugins", /* @__PURE__ */ new Map());
|
|
1112
|
-
__publicField(this, "
|
|
1113
|
-
const { content = "", style = {}, measureDom, effects } = options;
|
|
1065
|
+
__publicField(this, "fonts");
|
|
1066
|
+
const { content = "", style = {}, measureDom, effects, fonts } = options;
|
|
1114
1067
|
this.content = content;
|
|
1115
1068
|
this.style = style;
|
|
1116
1069
|
this.measureDom = measureDom;
|
|
1117
1070
|
this.effects = effects;
|
|
1071
|
+
this.fonts = fonts;
|
|
1118
1072
|
this.use(render()).use(highlight()).use(listStyle());
|
|
1119
1073
|
}
|
|
1120
1074
|
get fontSize() {
|
|
@@ -1130,6 +1084,56 @@ class Text {
|
|
|
1130
1084
|
this.plugins.set(plugin.name, plugin);
|
|
1131
1085
|
return this;
|
|
1132
1086
|
}
|
|
1087
|
+
updateParagraphs() {
|
|
1088
|
+
let { content, computedStyle: style } = this;
|
|
1089
|
+
const paragraphs = [];
|
|
1090
|
+
if (typeof content === "string") {
|
|
1091
|
+
const paragraph = new Paragraph({}, style);
|
|
1092
|
+
paragraph.addFragment(content);
|
|
1093
|
+
paragraphs.push(paragraph);
|
|
1094
|
+
} else {
|
|
1095
|
+
content = Array.isArray(content) ? content : [content];
|
|
1096
|
+
for (const p of content) {
|
|
1097
|
+
if (typeof p === "string") {
|
|
1098
|
+
const paragraph = new Paragraph({}, style);
|
|
1099
|
+
paragraph.addFragment(p);
|
|
1100
|
+
paragraphs.push(paragraph);
|
|
1101
|
+
} else if (Array.isArray(p)) {
|
|
1102
|
+
const paragraph = new Paragraph({}, style);
|
|
1103
|
+
p.forEach((f) => {
|
|
1104
|
+
if (typeof f === "string") {
|
|
1105
|
+
paragraph.addFragment(f);
|
|
1106
|
+
} else {
|
|
1107
|
+
const { content: content2, ...fStyle } = f;
|
|
1108
|
+
if (content2 !== void 0) {
|
|
1109
|
+
paragraph.addFragment(content2, fStyle);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
});
|
|
1113
|
+
paragraphs.push(paragraph);
|
|
1114
|
+
} else if ("fragments" in p) {
|
|
1115
|
+
const { fragments, ...pStyle } = p;
|
|
1116
|
+
const paragraph = new Paragraph(pStyle, style);
|
|
1117
|
+
fragments.forEach((f) => {
|
|
1118
|
+
const { content: content2, ...fStyle } = f;
|
|
1119
|
+
if (content2 !== void 0) {
|
|
1120
|
+
paragraph.addFragment(content2, fStyle);
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
paragraphs.push(paragraph);
|
|
1124
|
+
} else if ("content" in p) {
|
|
1125
|
+
const { content: pData, ...pStyle } = p;
|
|
1126
|
+
if (pData !== void 0) {
|
|
1127
|
+
const paragraph = new Paragraph(pStyle, style);
|
|
1128
|
+
paragraph.addFragment(pData);
|
|
1129
|
+
paragraphs.push(paragraph);
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
this.paragraphs = paragraphs;
|
|
1135
|
+
return this;
|
|
1136
|
+
}
|
|
1133
1137
|
measure(dom = this.measureDom) {
|
|
1134
1138
|
this.computedStyle = { ...defaultTextStyles, ...this.style };
|
|
1135
1139
|
const old = {
|
|
@@ -1140,12 +1144,12 @@ class Text {
|
|
|
1140
1144
|
pathBox: this.pathBox,
|
|
1141
1145
|
boundingBox: this.boundingBox
|
|
1142
1146
|
};
|
|
1143
|
-
this.
|
|
1147
|
+
this.updateParagraphs();
|
|
1144
1148
|
const result = this.measurer.measure(dom);
|
|
1145
1149
|
this.paragraphs = result.paragraphs;
|
|
1146
1150
|
this.lineBox = result.boundingBox;
|
|
1147
1151
|
this.characters.forEach((c) => {
|
|
1148
|
-
c.update(this.
|
|
1152
|
+
c.update(this.fonts);
|
|
1149
1153
|
});
|
|
1150
1154
|
this.rawGlyphBox = this.getGlyphBox();
|
|
1151
1155
|
const plugins = [...this.plugins.values()];
|
|
@@ -1255,7 +1259,6 @@ exports.Character = Character;
|
|
|
1255
1259
|
exports.Fragment = Fragment;
|
|
1256
1260
|
exports.Measurer = Measurer;
|
|
1257
1261
|
exports.Paragraph = Paragraph;
|
|
1258
|
-
exports.Parser = Parser;
|
|
1259
1262
|
exports.Text = Text;
|
|
1260
1263
|
exports.defaultTextStyles = defaultTextStyles;
|
|
1261
1264
|
exports.definePlugin = definePlugin;
|
package/dist/index.d.cts
CHANGED
|
@@ -3,100 +3,6 @@ export * from 'modern-path2d';
|
|
|
3
3
|
import { Font, Sfnt, GlyphPathCommand } from 'modern-font';
|
|
4
4
|
export * from 'modern-font';
|
|
5
5
|
|
|
6
|
-
type Sizeable = `${number}%` | `${number}rem` | number;
|
|
7
|
-
type WritingMode = 'horizontal-tb' | 'vertical-lr' | 'vertical-rl';
|
|
8
|
-
type TextOrientation = 'mixed' | 'upright' | 'sideways-right' | 'sideways';
|
|
9
|
-
type FontWeight = 'normal' | 'bold' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
10
|
-
type FontStyle = 'normal' | 'italic' | 'oblique' | `oblique ${string}`;
|
|
11
|
-
type FontKerning = 'none' | 'auto' | 'normal';
|
|
12
|
-
type TextWrap = 'wrap' | 'nowrap';
|
|
13
|
-
type TextAlign = 'center' | 'end' | 'left' | 'right' | 'start';
|
|
14
|
-
type VerticalAlign = 'baseline' | 'top' | 'middle' | 'bottom' | 'sub' | 'super' | 'text-top' | 'text-bottom';
|
|
15
|
-
type TextTransform = 'none' | 'uppercase' | 'lowercase';
|
|
16
|
-
type TextDecoration = 'none' | 'underline' | 'line-through';
|
|
17
|
-
type ListStyleType = 'none' | 'disc';
|
|
18
|
-
type ListStyleImage = 'none' | string;
|
|
19
|
-
type ListStyleSize = 'cover' | Sizeable;
|
|
20
|
-
type ListStylePosition = 'inside' | 'outside';
|
|
21
|
-
type HighlightImage = 'none' | string;
|
|
22
|
-
type HighlightSize = 'cover' | Sizeable;
|
|
23
|
-
type HighlightStrokeWidth = Sizeable;
|
|
24
|
-
type HighlightOverflow = 'none' | 'visible' | 'hidden';
|
|
25
|
-
interface TextLineStyle {
|
|
26
|
-
writingMode: WritingMode;
|
|
27
|
-
textWrap: TextWrap;
|
|
28
|
-
textAlign: TextAlign;
|
|
29
|
-
textIndent: number;
|
|
30
|
-
lineHeight: number;
|
|
31
|
-
listStyleType: ListStyleType;
|
|
32
|
-
listStyleImage: ListStyleImage;
|
|
33
|
-
listStyleSize: ListStyleSize;
|
|
34
|
-
listStylePosition: ListStylePosition;
|
|
35
|
-
}
|
|
36
|
-
interface TextInlineStyle {
|
|
37
|
-
verticalAlign: VerticalAlign;
|
|
38
|
-
letterSpacing: number;
|
|
39
|
-
fontSize: number;
|
|
40
|
-
fontWeight: FontWeight;
|
|
41
|
-
fontFamily: string;
|
|
42
|
-
fontStyle: FontStyle;
|
|
43
|
-
fontKerning?: FontKerning;
|
|
44
|
-
textTransform: TextTransform;
|
|
45
|
-
textOrientation: TextOrientation;
|
|
46
|
-
textDecoration: TextDecoration;
|
|
47
|
-
highlightReferImage: HighlightImage;
|
|
48
|
-
highlightImage: HighlightImage;
|
|
49
|
-
highlightSize: HighlightSize;
|
|
50
|
-
highlightStrokeWidth: HighlightStrokeWidth;
|
|
51
|
-
highlightOverflow: HighlightOverflow;
|
|
52
|
-
}
|
|
53
|
-
interface TextDrawStyle {
|
|
54
|
-
color: string | CanvasGradient | CanvasPattern;
|
|
55
|
-
backgroundColor: string | CanvasGradient | CanvasPattern;
|
|
56
|
-
textStrokeWidth: number;
|
|
57
|
-
textStrokeColor: string | CanvasGradient | CanvasPattern;
|
|
58
|
-
shadowColor: string;
|
|
59
|
-
shadowOffsetX: number;
|
|
60
|
-
shadowOffsetY: number;
|
|
61
|
-
shadowBlur: number;
|
|
62
|
-
translateX: number;
|
|
63
|
-
translateY: number;
|
|
64
|
-
skewX: number;
|
|
65
|
-
skewY: number;
|
|
66
|
-
}
|
|
67
|
-
interface TextStyle extends TextLineStyle, TextInlineStyle, TextDrawStyle {
|
|
68
|
-
}
|
|
69
|
-
interface FragmentContent extends Partial<TextStyle> {
|
|
70
|
-
content: string;
|
|
71
|
-
}
|
|
72
|
-
interface ParagraphContent extends Partial<TextStyle> {
|
|
73
|
-
fragments: FragmentContent[];
|
|
74
|
-
}
|
|
75
|
-
type TextContent = string | FragmentContent | ParagraphContent | (string | FragmentContent | ParagraphContent | (string | FragmentContent)[])[];
|
|
76
|
-
|
|
77
|
-
declare function parseColor(ctx: CanvasRenderingContext2D, source: string | CanvasGradient | CanvasPattern, box: BoundingBox): string | CanvasGradient | CanvasPattern;
|
|
78
|
-
declare function uploadColor(style: Partial<TextStyle>, box: BoundingBox, ctx: CanvasRenderingContext2D): void;
|
|
79
|
-
interface LinearGradient {
|
|
80
|
-
x0: number;
|
|
81
|
-
y0: number;
|
|
82
|
-
x1: number;
|
|
83
|
-
y1: number;
|
|
84
|
-
stops: {
|
|
85
|
-
offset: number;
|
|
86
|
-
color: string;
|
|
87
|
-
}[];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface DrawShapePathsOptions extends Partial<TextStyle> {
|
|
91
|
-
ctx: CanvasRenderingContext2D;
|
|
92
|
-
path: Path2D;
|
|
93
|
-
fontSize: number;
|
|
94
|
-
clipRect?: BoundingBox;
|
|
95
|
-
}
|
|
96
|
-
declare function drawPath(options: DrawShapePathsOptions): void;
|
|
97
|
-
|
|
98
|
-
declare function setupView(ctx: CanvasRenderingContext2D, pixelRatio: number, boundingBox: BoundingBox): void;
|
|
99
|
-
|
|
100
6
|
declare class Fragment {
|
|
101
7
|
content: string;
|
|
102
8
|
style: Partial<TextStyle>;
|
|
@@ -130,9 +36,9 @@ declare class Character {
|
|
|
130
36
|
get fontSize(): number;
|
|
131
37
|
get fontHeight(): number;
|
|
132
38
|
constructor(content: string, index: number, parent: Fragment);
|
|
133
|
-
protected _getFontSfnt(
|
|
39
|
+
protected _getFontSfnt(fonts?: Record<string, Font>): Sfnt | undefined;
|
|
134
40
|
updateGlyph(sfnt?: Sfnt | undefined): this;
|
|
135
|
-
update(
|
|
41
|
+
update(fonts?: Record<string, Font>): this;
|
|
136
42
|
protected _decoration(): GlyphPathCommand[];
|
|
137
43
|
protected _italic(path: Path2D, startPoint?: VectorLike): void;
|
|
138
44
|
getGlyphMinMax(min?: Vector2, max?: Vector2, withStyle?: boolean): {
|
|
@@ -154,18 +60,6 @@ declare class Paragraph {
|
|
|
154
60
|
addFragment(content: string, style?: Partial<TextStyle>): Fragment;
|
|
155
61
|
}
|
|
156
62
|
|
|
157
|
-
type PromiseLike<T> = T | Promise<T>;
|
|
158
|
-
interface Plugin {
|
|
159
|
-
name: string;
|
|
160
|
-
paths?: Path2D[];
|
|
161
|
-
getBoundingBox?: (text: Text) => BoundingBox | undefined;
|
|
162
|
-
updateOrder?: number;
|
|
163
|
-
update?: (text: Text) => PromiseLike<void>;
|
|
164
|
-
renderOrder?: number;
|
|
165
|
-
render?: (ctx: CanvasRenderingContext2D, text: Text) => PromiseLike<void>;
|
|
166
|
-
}
|
|
167
|
-
declare function definePlugin(options: Plugin): Plugin;
|
|
168
|
-
|
|
169
63
|
interface MeasuredParagraph {
|
|
170
64
|
paragraphIndex: number;
|
|
171
65
|
left: number;
|
|
@@ -225,12 +119,6 @@ declare class Measurer {
|
|
|
225
119
|
measure(dom?: HTMLElement): MeasureDomResult;
|
|
226
120
|
}
|
|
227
121
|
|
|
228
|
-
declare class Parser {
|
|
229
|
-
protected _text: Text;
|
|
230
|
-
constructor(_text: Text);
|
|
231
|
-
parse(): Paragraph[];
|
|
232
|
-
}
|
|
233
|
-
|
|
234
122
|
interface TextRenderOptions {
|
|
235
123
|
view: HTMLCanvasElement;
|
|
236
124
|
pixelRatio?: number;
|
|
@@ -240,6 +128,7 @@ interface TextOptions {
|
|
|
240
128
|
style?: Partial<TextStyle>;
|
|
241
129
|
measureDom?: HTMLElement;
|
|
242
130
|
effects?: Partial<TextStyle>[];
|
|
131
|
+
fonts?: Record<string, Font>;
|
|
243
132
|
}
|
|
244
133
|
interface MeasureResult {
|
|
245
134
|
paragraphs: Paragraph[];
|
|
@@ -263,15 +152,15 @@ declare class Text {
|
|
|
263
152
|
glyphBox: BoundingBox;
|
|
264
153
|
pathBox: BoundingBox;
|
|
265
154
|
boundingBox: BoundingBox;
|
|
266
|
-
parser: Parser;
|
|
267
155
|
measurer: Measurer;
|
|
268
|
-
plugins: Map<string,
|
|
269
|
-
|
|
156
|
+
plugins: Map<string, TextPlugin>;
|
|
157
|
+
fonts?: Record<string, Font>;
|
|
270
158
|
get fontSize(): number;
|
|
271
159
|
get isVertical(): boolean;
|
|
272
160
|
get characters(): Character[];
|
|
273
161
|
constructor(options?: TextOptions);
|
|
274
|
-
use(plugin:
|
|
162
|
+
use(plugin: TextPlugin): this;
|
|
163
|
+
updateParagraphs(): this;
|
|
275
164
|
measure(dom?: HTMLElement | undefined): MeasureResult;
|
|
276
165
|
getGlyphBox(): BoundingBox;
|
|
277
166
|
updatePathBox(): this;
|
|
@@ -281,15 +170,121 @@ declare class Text {
|
|
|
281
170
|
render(options: TextRenderOptions): this;
|
|
282
171
|
}
|
|
283
172
|
|
|
173
|
+
type Sizeable = `${number}%` | `${number}rem` | number;
|
|
174
|
+
type WritingMode = 'horizontal-tb' | 'vertical-lr' | 'vertical-rl';
|
|
175
|
+
type TextOrientation = 'mixed' | 'upright' | 'sideways-right' | 'sideways';
|
|
176
|
+
type FontWeight = 'normal' | 'bold' | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
|
|
177
|
+
type FontStyle = 'normal' | 'italic' | 'oblique' | `oblique ${string}`;
|
|
178
|
+
type FontKerning = 'none' | 'auto' | 'normal';
|
|
179
|
+
type TextWrap = 'wrap' | 'nowrap';
|
|
180
|
+
type TextAlign = 'center' | 'end' | 'left' | 'right' | 'start';
|
|
181
|
+
type VerticalAlign = 'baseline' | 'top' | 'middle' | 'bottom' | 'sub' | 'super' | 'text-top' | 'text-bottom';
|
|
182
|
+
type TextTransform = 'none' | 'uppercase' | 'lowercase';
|
|
183
|
+
type TextDecoration = 'none' | 'underline' | 'line-through';
|
|
184
|
+
type ListStyleType = 'none' | 'disc';
|
|
185
|
+
type ListStyleImage = 'none' | string;
|
|
186
|
+
type ListStyleSize = 'cover' | Sizeable;
|
|
187
|
+
type ListStylePosition = 'inside' | 'outside';
|
|
188
|
+
type HighlightImage = 'none' | string;
|
|
189
|
+
type HighlightSize = 'cover' | Sizeable;
|
|
190
|
+
type HighlightStrokeWidth = Sizeable;
|
|
191
|
+
type HighlightOverflow = 'none' | 'visible' | 'hidden';
|
|
192
|
+
interface TextLineStyle {
|
|
193
|
+
writingMode: WritingMode;
|
|
194
|
+
textWrap: TextWrap;
|
|
195
|
+
textAlign: TextAlign;
|
|
196
|
+
textIndent: number;
|
|
197
|
+
lineHeight: number;
|
|
198
|
+
listStyleType: ListStyleType;
|
|
199
|
+
listStyleImage: ListStyleImage;
|
|
200
|
+
listStyleSize: ListStyleSize;
|
|
201
|
+
listStylePosition: ListStylePosition;
|
|
202
|
+
}
|
|
203
|
+
interface TextInlineStyle {
|
|
204
|
+
verticalAlign: VerticalAlign;
|
|
205
|
+
letterSpacing: number;
|
|
206
|
+
fontSize: number;
|
|
207
|
+
fontWeight: FontWeight;
|
|
208
|
+
fontFamily: string;
|
|
209
|
+
fontStyle: FontStyle;
|
|
210
|
+
fontKerning?: FontKerning;
|
|
211
|
+
textTransform: TextTransform;
|
|
212
|
+
textOrientation: TextOrientation;
|
|
213
|
+
textDecoration: TextDecoration;
|
|
214
|
+
highlightReferImage: HighlightImage;
|
|
215
|
+
highlightImage: HighlightImage;
|
|
216
|
+
highlightSize: HighlightSize;
|
|
217
|
+
highlightStrokeWidth: HighlightStrokeWidth;
|
|
218
|
+
highlightOverflow: HighlightOverflow;
|
|
219
|
+
}
|
|
220
|
+
interface TextDrawStyle {
|
|
221
|
+
color: string | CanvasGradient | CanvasPattern;
|
|
222
|
+
backgroundColor: string | CanvasGradient | CanvasPattern;
|
|
223
|
+
textStrokeWidth: number;
|
|
224
|
+
textStrokeColor: string | CanvasGradient | CanvasPattern;
|
|
225
|
+
shadowColor: string;
|
|
226
|
+
shadowOffsetX: number;
|
|
227
|
+
shadowOffsetY: number;
|
|
228
|
+
shadowBlur: number;
|
|
229
|
+
translateX: number;
|
|
230
|
+
translateY: number;
|
|
231
|
+
skewX: number;
|
|
232
|
+
skewY: number;
|
|
233
|
+
}
|
|
234
|
+
interface TextStyle extends TextLineStyle, TextInlineStyle, TextDrawStyle {
|
|
235
|
+
}
|
|
236
|
+
interface FragmentContent extends Partial<TextStyle> {
|
|
237
|
+
content: string;
|
|
238
|
+
}
|
|
239
|
+
interface ParagraphContent extends Partial<TextStyle> {
|
|
240
|
+
fragments: FragmentContent[];
|
|
241
|
+
}
|
|
242
|
+
type TextContent = string | FragmentContent | ParagraphContent | (string | FragmentContent | ParagraphContent | (string | FragmentContent)[])[];
|
|
243
|
+
type PromiseLike<T> = T | Promise<T>;
|
|
244
|
+
interface TextPlugin {
|
|
245
|
+
name: string;
|
|
246
|
+
paths?: Path2D[];
|
|
247
|
+
getBoundingBox?: (text: Text) => BoundingBox | undefined;
|
|
248
|
+
updateOrder?: number;
|
|
249
|
+
update?: (text: Text) => PromiseLike<void>;
|
|
250
|
+
renderOrder?: number;
|
|
251
|
+
render?: (ctx: CanvasRenderingContext2D, text: Text) => PromiseLike<void>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
declare function parseColor(ctx: CanvasRenderingContext2D, source: string | CanvasGradient | CanvasPattern, box: BoundingBox): string | CanvasGradient | CanvasPattern;
|
|
255
|
+
declare function uploadColor(style: Partial<TextStyle>, box: BoundingBox, ctx: CanvasRenderingContext2D): void;
|
|
256
|
+
interface LinearGradient {
|
|
257
|
+
x0: number;
|
|
258
|
+
y0: number;
|
|
259
|
+
x1: number;
|
|
260
|
+
y1: number;
|
|
261
|
+
stops: {
|
|
262
|
+
offset: number;
|
|
263
|
+
color: string;
|
|
264
|
+
}[];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
interface DrawShapePathsOptions extends Partial<TextStyle> {
|
|
268
|
+
ctx: CanvasRenderingContext2D;
|
|
269
|
+
path: Path2D;
|
|
270
|
+
fontSize: number;
|
|
271
|
+
clipRect?: BoundingBox;
|
|
272
|
+
}
|
|
273
|
+
declare function drawPath(options: DrawShapePathsOptions): void;
|
|
274
|
+
|
|
275
|
+
declare function setupView(ctx: CanvasRenderingContext2D, pixelRatio: number, boundingBox: BoundingBox): void;
|
|
276
|
+
|
|
284
277
|
declare function uploadColors(ctx: CanvasRenderingContext2D, text: Text): void;
|
|
285
278
|
|
|
279
|
+
declare function definePlugin(options: TextPlugin): TextPlugin;
|
|
280
|
+
|
|
286
281
|
declare function measureText(options: TextOptions): MeasureResult;
|
|
287
282
|
|
|
288
|
-
declare function highlight():
|
|
283
|
+
declare function highlight(): TextPlugin;
|
|
289
284
|
|
|
290
|
-
declare function listStyle():
|
|
285
|
+
declare function listStyle(): TextPlugin;
|
|
291
286
|
|
|
292
|
-
declare function render():
|
|
287
|
+
declare function render(): TextPlugin;
|
|
293
288
|
declare function getTransform2D(text: Text, style: Partial<TextStyle>): Matrix3;
|
|
294
289
|
|
|
295
290
|
declare function renderText(options: TextOptions & TextRenderOptions): Text;
|
|
@@ -297,4 +292,4 @@ declare function renderText(options: TextOptions & TextRenderOptions): Text;
|
|
|
297
292
|
declare function isNone(val: string | undefined): boolean;
|
|
298
293
|
declare function filterEmpty(val: Record<string, any> | undefined): Record<string, any> | undefined;
|
|
299
294
|
|
|
300
|
-
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasureDomResult, type MeasureResult, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, Measurer, Paragraph, type ParagraphContent,
|
|
295
|
+
export { Character, type DrawShapePathsOptions, type FontKerning, type FontStyle, type FontWeight, Fragment, type FragmentContent, type HighlightImage, type HighlightOverflow, type HighlightSize, type HighlightStrokeWidth, type LinearGradient, type ListStyleImage, type ListStylePosition, type ListStyleSize, type ListStyleType, type MeasureDomResult, type MeasureResult, type MeasuredCharacter, type MeasuredFragment, type MeasuredParagraph, Measurer, Paragraph, type ParagraphContent, type Sizeable, Text, type TextAlign, type TextContent, type TextDecoration, type TextDrawStyle, type TextInlineStyle, type TextLineStyle, type TextOptions, type TextOrientation, type TextPlugin, type TextRenderOptions, type TextStyle, type TextTransform, type TextWrap, type VerticalAlign, type WritingMode, defaultTextStyles, definePlugin, drawPath, filterEmpty, getTransform2D, highlight, isNone, listStyle, measureText, parseColor, render, renderText, setupView, uploadColor, uploadColors };
|