-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextrender.ts
More file actions
137 lines (110 loc) · 3.6 KB
/
textrender.ts
File metadata and controls
137 lines (110 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
enum TextAlignment {
Left,
Center,
Right
}
class TextRender {
private _text: string;
private _color: number;
private _outlineColor?: number;
private _font: image.Font;
private _dirty: boolean;
private _lastRender: Image;
constructor(
text: string,
color: number = 1,
outlineColor?: number,
font?: image.Font
) {
this._text = text;
this._color = color;
this._outlineColor = outlineColor;
if (!font)
this._font = image.font8;
else
this._font = font;
this._dirty = true;
}
public setText(text: string): void {
this._text = text;
this._dirty = true;
}
public text(): string {
return this._text;
}
public setColor(color: number): void {
this._color = color;
this._dirty = true;
}
public color(): number {
return this._color;
}
public setOutlineColor(color?: number): void {
this._outlineColor = color;
this._dirty = true;
}
public outlineColor(): number {
return this._outlineColor;
}
public setFont(font: image.Font): void {
this._font = font;
this._dirty = true;
}
public font(): image.Font {
return this._font;
}
public width(): number {
if (this._dirty)
this.render();
return this._lastRender.width;
}
public height(): number {
if (this._dirty)
this.render();
return this._lastRender.height;
}
public draw(targetImg: Image, x: number, y: number, alignment: TextAlignment = TextAlignment.Left): void {
if (this._dirty)
this.render();
switch(alignment) {
case TextAlignment.Center:
targetImg.drawTransparentImage(this._lastRender, x - Math.idiv(this._lastRender.width, 2), y);
break;
case TextAlignment.Right:
targetImg.drawTransparentImage(this._lastRender, x - this._lastRender.width, y);
break;
default:
targetImg.drawTransparentImage(this._lastRender, x, y);
break;
}
}
private render(): void {
const width = this._font.charWidth * this._text.length;
const height = this._font.charHeight;
let textImg = image.create(width, height);
textImg.print(this._text, 0, 0, this._color, this._font,);
if (this._outlineColor)
textImg = this.outline(textImg);
this._lastRender = textImg;
this._dirty = false;
}
private outline(sourceImg: Image): Image {
const thickness = this._font.multiplier ? Math.round(this._font.multiplier) : 1;
const doubleThickness = thickness << 1;
const resultImg = image.create(sourceImg.width + doubleThickness, sourceImg.height);
const hl = 0;
const hc = thickness;
const hr = doubleThickness;
resultImg.drawImage(sourceImg, hl, 0);
resultImg.drawTransparentImage(sourceImg, hr, 0);
resultImg.drawTransparentImage(sourceImg, hl, -thickness);
resultImg.drawTransparentImage(sourceImg, hr, -thickness);
resultImg.drawTransparentImage(sourceImg, hl, thickness);
resultImg.drawTransparentImage(sourceImg, hr, thickness);
resultImg.drawTransparentImage(sourceImg, hc, -thickness);
resultImg.drawTransparentImage(sourceImg, hc, thickness);
resultImg.replace(this._color, this._outlineColor);
resultImg.drawTransparentImage(sourceImg, hc, 0);
return resultImg;
}
}