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
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// Ported from https://github.com/kiwi515/ogws/blob/master/src/nw4r/ut/ut_RomFont.cpp --- Mostly unused
#include "nw4r/ut.h"
namespace nw4r {
namespace ut {
u16 RomFont::mFontEncode = 0xFFFF;
namespace {
bool IsCP1252Char(u16 c) {
return c >= 0x20 && c <= 0xFF;
}
bool IsSJISHalfWidthChar(u16 c) {
if (c > 0xFF) {
return false;
}
return (c >= 0x20 && c <= 0x7E) || (c >= 0xA1 && c <= 0xDF);
}
bool IsSJISFullWidthChar(u16 c) {
u8 hi = BitExtract<u16>(c, 8, 8);
u8 lo = BitExtract<u16>(c, 0, 8);
return hi >= 0x81 && hi <= 0x98 && lo >= 0x40 && lo <= 0xFC;
}
} // namespace
RomFont::RomFont() : mFontHeader(NULL), mAlternateChar('?') {
mDefaultWidths.leftSpacing = 0;
mDefaultWidths.glyphWidth = 0;
mDefaultWidths.charWidth = 0;
}
RomFont::~RomFont() {}
bool RomFont::Load(void *buffer) {
if (mFontHeader != NULL) {
return false;
}
BOOL success = OSInitFont(static_cast<OSFontHeader *>(buffer));
if (success) {
mFontEncode = OSGetFontEncode();
mFontHeader = static_cast<OSFontHeader *>(buffer);
mDefaultWidths.leftSpacing = 0;
mDefaultWidths.glyphWidth = GetCellWidth();
mDefaultWidths.charWidth = GetMaxCharWidth();
InitReaderFunc(GetEncoding());
}
return success;
}
u32 RomFont::GetRequireBufferSize() {
switch (OSGetFontEncode()) {
case OS_FONT_ENCODE_ANSI: return 0x00020120;
case OS_FONT_ENCODE_SJIS: return 0x00120F00;
}
return 0;
}
int RomFont::GetWidth() const {
return mFontHeader->width;
}
int RomFont::GetHeight() const {
return GetAscent() + GetDescent();
}
int RomFont::GetAscent() const {
return mFontHeader->ascent;
}
int RomFont::GetDescent() const {
return mFontHeader->descent;
}
int RomFont::GetBaselinePos() const {
return mFontHeader->ascent;
}
int RomFont::GetCellHeight() const {
return mFontHeader->cellHeight;
}
int RomFont::GetCellWidth() const {
return mFontHeader->cellWidth;
}
int RomFont::GetMaxCharWidth() const {
return mFontHeader->width;
}
Font::Type RomFont::GetType() const {
return TYPE_ROM;
}
GXTexFmt RomFont::GetTextureFormat() const {
return GX_TF_I4;
}
int RomFont::GetLineFeed() const {
return mFontHeader->leading;
}
CharWidths RomFont::GetDefaultCharWidths() const {
return mDefaultWidths;
}
void RomFont::SetDefaultCharWidths(const CharWidths &widths) {
mDefaultWidths = widths;
}
bool RomFont::SetAlternateChar(u16 c) {
const u16 prev = mAlternateChar;
mAlternateChar = 0xFFFF;
u16 undef = HandleUndefinedChar(c);
if (undef != 0xFFFF) {
mAlternateChar = c;
return true;
} else {
mAlternateChar = prev;
return false;
}
}
void RomFont::SetLineFeed(int lf) {
mFontHeader->leading = lf;
}
int RomFont::GetCharWidth(u16 c) const {
u32 width;
char buffer[4];
MakeCharPtr(buffer, c);
OSGetFontWidth(buffer, &width);
return width;
}
CharWidths RomFont::GetCharWidths(u16 c) const {
int width = GetCharWidth(c);
CharWidths widths;
widths.leftSpacing = 0;
widths.glyphWidth = width;
widths.charWidth = width;
return widths;
}
void RomFont::GetGlyph(Glyph *out, u16 c) const {
void *texture;
u32 x, y, width;
char buffer[4];
MakeCharPtr(buffer, c);
OSGetFontTexture(buffer, &texture, &x, &y, &width);
out->texture = texture;
out->widths.leftSpacing = 0;
out->widths.glyphWidth = width;
out->widths.charWidth = width;
out->height = mFontHeader->cellHeight;
out->format = GX_TF_I4;
out->texWidth = mFontHeader->sheetWidth;
out->texHeight = mFontHeader->sheetHeight;
out->cellX = x;
out->cellY = y;
}
FontEncoding RomFont::GetEncoding() const {
switch (mFontEncode) {
case OS_FONT_ENCODE_ANSI: return FONT_ENCODE_CP1252;
case OS_FONT_ENCODE_SJIS: return FONT_ENCODE_SJIS;
}
return FONT_ENCODE_CP1252;
}
void RomFont::MakeCharPtr(char *buffer, u16 c) const {
c = HandleUndefinedChar(c);
if (BitExtract<u16>(c, 8, 8) == 0) {
buffer[0] = c & 0x00FF;
buffer[1] = '\0';
} else {
buffer[0] = BitExtract<u16>(c, 8, 8);
buffer[1] = c & 0x00FF;
buffer[2] = '\0';
}
}
u16 RomFont::HandleUndefinedChar(u16 c) const {
bool valid;
switch (mFontEncode) {
case OS_FONT_ENCODE_ANSI: valid = IsCP1252Char(c); break;
case OS_FONT_ENCODE_SJIS: valid = IsSJISHalfWidthChar(c) || IsSJISFullWidthChar(c); break;
}
return valid ? c : mAlternateChar;
}
} // namespace ut
} // namespace nw4r
|