-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBMFontDrawer.js
207 lines (185 loc) · 4.87 KB
/
BMFontDrawer.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//=============================================================================
// RPG Maker MZ - BMFont Drawer
//=============================================================================
/*:
* @target MZ
* @plugindesc [MZ] [BMFont Support]
* @author Eden
* @url https://github.com/ashifolfi/RMMZ-Plugins
*
* @help
* This plugin adds support for text format BMFont fonts
* for bitmap font drawing.
*
* Currently Supports:
* - Basic text drawing in all scenes and windows
*
* WIP/Broken:
* - Colors
* - Size changing
*/
function BMFont() {
throw new Error("This is a static class");
}
(function() {
BMFont.getChar = function(font, code)
{
for (let i = 0; i < font.chars._count; i++)
{
if (font.chars.char[i]._id == code)
{
return font.chars.char[i];
}
}
return null;
};
FontManager.BMFontCache = {};
FontManager.loadOrig = FontManager.load;
FontManager.load = function(family, filename)
{
if (filename.endsWith(".fnt"))
{
// this is a BMFont file in json format
const xhr = new XMLHttpRequest();
const url = "fonts/" + filename;
this.BMFontCache[family] = null;
xhr.open("GET", url);
xhr.overrideMimeType("application/json");
xhr.onload = () => this.bmOnXhrLoad(xhr, family, filename, url);
xhr.onerror = () => this.onXhrError(family, filename, url);
xhr.send();
}
else
{
// this is not a BMFont file
this.loadOrig(family, filename);
}
};
FontManager.bmOnXhrLoad = function(xhr, name, src, url)
{
if (xhr.status < 400)
{
this.BMFontCache[name] = JSON.parse(xhr.responseText);
// load pages into memory
this.BMFontCache[name].font.pageBMP = [];
for (let i = 0; i < this.BMFontCache[name].font.pages._count; i++)
{
const pageInf = this.BMFontCache[name].font.pages.page[i];
this.BMFontCache[name].font.pageBMP[pageInf._id] = ImageManager.loadBitmap("fonts/", pageInf._file);
}
}
else
{
this.onXhrError(name, src, url);
}
};
FontManager.onXhrError = function(name, src, url)
{
const error = { name: name, src: src, url: url };
this._errors.push(error);
};
Bitmap.prototype.measureTextWidthOrig = Bitmap.prototype.measureTextWidth;
Bitmap.prototype.measureTextWidth = function(text)
{
// check the font face cache in BMFontCache
const fonts = this.fontFace.split(", ");
for (const fontFace in fonts)
{
if (FontManager.BMFontCache[fonts[fontFace]] != null)
{
const font = FontManager.BMFontCache[fonts[fontFace]].font;
let width = 0;
let x_off = 0;
let y_off = 0;
for (let i = 0; i < text.length; i++)
{
if (text[i] == "\n")
{
x_off = 0;
y_off += lineHeight;
continue;
}
let char = BMFont.getChar(font, text.charCodeAt(i));
if (char != null)
{
x_off += Number(char._width);
}
if (x_off > width)
{
width = x_off;
}
}
return width;
}
// if the font is loaded as a non BMP then draw it
if (FontManager._states[fonts[fontFace]] == "loaded")
{
break;
}
}
return this.measureTextWidthOrig(text);
};
Bitmap.prototype.drawTextOrig = Bitmap.prototype.drawText;
Bitmap.prototype.drawText = function(text, x, y, maxWidth, lineHeight, align)
{
// check the font face cache in BMFontCache
const fonts = this.fontFace.split(", ");
for (const fontFace in fonts)
{
if (FontManager.BMFontCache[fonts[fontFace]] != null)
{
this.drawBMText(FontManager.BMFontCache[fonts[fontFace]].font, text, x, y, maxWidth, lineHeight, align);
return;
}
// if the font is loaded as a non BMP then draw it
if (FontManager._states[fonts[fontFace]] == "loaded")
{
break;
}
}
this.drawTextOrig(text, x, y, maxWidth, lineHeight, align);
};
Bitmap.prototype.drawBMText = function(font, text, x, y, maxWidth, lineHeight, align)
{
maxWidth = maxWidth || 0xffffffff;
let tx = x;
if (align === "center") {
tx += maxWidth / 2;
}
if (align === "right") {
tx += maxWidth;
}
let y_off = 0;
let lines = text.split("\n");
for (const idx in lines)
{
let width = this.measureTextWidth(lines[idx]);
let x_off = 0;
if (align == "center")
{
x_off -= width / 2;
}
else if (align == "right")
{
x_off -= width;
}
for (let i = 0; i < lines[idx].length; i++)
{
let char = BMFont.getChar(font, lines[idx].charCodeAt(i));
if (char != null)
{
// this exists within the font
// cursed drawing method
this.blt(
font.pageBMP[char._page],
char._x, char._y, char._width, char._height,
tx + x_off + Number(char._xoffset),
y + y_off + Number(char._yoffset)
);
x_off += Number(char._width);
}
}
y_off += lineHeight;
}
};
})();