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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#include "ResFont.h"
#include "../db/assert.h"
struct CMapScanEntry {
/* 0x00 */ u16 ccode;
/* 0x02 */ u16 index;
}; // size = 0x04
struct CMapInfoScan {
/* 0x00 */ u16 num;
CMapScanEntry entries[]; // flexible, offset 0x02 (unit size 0x04)
}; // size = 0x02
namespace nw4hbm {
namespace ut {
namespace detail {
ResFontBase::ResFontBase() : mResource(NULL), mFontInfo(NULL) {}
ResFontBase::~ResFontBase() {}
void ResFontBase::SetResourceBuffer(void* pUserBuffer, FontInformation* pFontInfo) {
NW4HBM_ASSERT_VALID_PTR(79, pUserBuffer);
NW4HBM_ASSERT_VALID_PTR(80, pFontInfo);
NW4HBM_ASSERT(81, mResource == NULL);
NW4HBM_ASSERT(82, mFontInfo == NULL);
mResource = pUserBuffer;
mFontInfo = pFontInfo;
}
int ResFontBase::GetWidth() const {
NW4HBM_ASSERT_VALID_PTR(128, this);
NW4HBM_ASSERT_VALID_PTR(129, mFontInfo);
return mFontInfo->width;
}
int ResFontBase::GetHeight() const {
NW4HBM_ASSERT_VALID_PTR(145, this);
NW4HBM_ASSERT_VALID_PTR(146, mFontInfo);
return mFontInfo->height;
}
int ResFontBase::GetAscent() const {
NW4HBM_ASSERT_VALID_PTR(162, this);
NW4HBM_ASSERT_VALID_PTR(163, mFontInfo);
return mFontInfo->ascent;
}
int ResFontBase::GetDescent() const {
NW4HBM_ASSERT_VALID_PTR(179, this);
NW4HBM_ASSERT_VALID_PTR(180, mFontInfo);
return mFontInfo->height - mFontInfo->ascent;
}
int ResFontBase::GetBaselinePos() const {
NW4HBM_ASSERT_VALID_PTR(196, this);
NW4HBM_ASSERT_VALID_PTR(197, mFontInfo);
return mFontInfo->pGlyph->baselinePos;
}
int ResFontBase::GetCellHeight() const {
NW4HBM_ASSERT_VALID_PTR(213, this);
NW4HBM_ASSERT_VALID_PTR(214, mFontInfo);
return mFontInfo->pGlyph->cellHeight;
}
int ResFontBase::GetCellWidth() const {
NW4HBM_ASSERT_VALID_PTR(230, this);
NW4HBM_ASSERT_VALID_PTR(231, mFontInfo);
return mFontInfo->pGlyph->cellWidth;
}
int ResFontBase::GetMaxCharWidth() const {
NW4HBM_ASSERT_VALID_PTR(247, this);
NW4HBM_ASSERT_VALID_PTR(248, mFontInfo);
return mFontInfo->pGlyph->maxCharWidth;
}
Font::Type ResFontBase::GetType() const {
return TYPE_RESOURCE;
}
GXTexFmt ResFontBase::GetTextureFormat() const {
NW4HBM_ASSERT_VALID_PTR(279, this);
NW4HBM_ASSERT_VALID_PTR(280, mFontInfo);
return static_cast<GXTexFmt>(mFontInfo->pGlyph->sheetFormat);
}
int ResFontBase::GetLineFeed() const {
NW4HBM_ASSERT_VALID_PTR(296, this);
NW4HBM_ASSERT_VALID_PTR(297, mFontInfo);
return mFontInfo->linefeed;
}
CharWidths ResFontBase::GetDefaultCharWidths() const {
NW4HBM_ASSERT_VALID_PTR(313, this);
NW4HBM_ASSERT_VALID_PTR(314, mFontInfo);
return mFontInfo->defaultWidth;
}
void ResFontBase::SetDefaultCharWidths(const CharWidths& widths) {
// clang-format off
NW4HBM_ASSERT_VALID_PTR(330, this);
NW4HBM_ASSERT_VALID_PTR(331, mFontInfo);
NW4HBM_ASSERT_VALID_PTR(332, & widths);
mFontInfo->defaultWidth = widths;
// clang-format on
}
bool ResFontBase::SetAlternateChar(u16 c) {
NW4HBM_ASSERT_VALID_PTR(350, this);
NW4HBM_ASSERT_VALID_PTR(351, mFontInfo);
u16 index = FindGlyphIndex(c);
if (index != GLYPH_INDEX_NOT_FOUND) {
mFontInfo->alterCharIndex = index;
return true;
} else {
return false;
}
}
void ResFontBase::SetLineFeed(int linefeed) {
NW4HBM_ASSERT_VALID_PTR(375, this);
NW4HBM_ASSERT_VALID_PTR(376, mFontInfo);
NW4R_ASSERT_MINMAXLT(377, linefeed, -128, 127);
mFontInfo->linefeed = linefeed;
}
int ResFontBase::GetCharWidth(u16 c) const {
return GetCharWidths(c).charWidth;
}
CharWidths ResFontBase::GetCharWidths(u16 c) const {
u16 index = GetGlyphIndex(c);
return GetCharWidthsFromIndex(index);
}
void ResFontBase::GetGlyph(Glyph* glyph, u16 c) const {
u16 index = GetGlyphIndex(c);
GetGlyphFromIndex(glyph, index);
}
FontEncoding ResFontBase::GetEncoding() const {
NW4HBM_ASSERT_VALID_PTR(456, this);
NW4HBM_ASSERT_VALID_PTR(457, mFontInfo);
return static_cast<FontEncoding>(mFontInfo->encoding);
}
u16 ResFontBase::GetGlyphIndex(u16 c) const {
NW4HBM_ASSERT_VALID_PTR(482, this);
NW4HBM_ASSERT_VALID_PTR(483, mFontInfo);
u16 index = FindGlyphIndex(c);
return index != GLYPH_INDEX_NOT_FOUND ? index : mFontInfo->alterCharIndex;
}
u16 ResFontBase::FindGlyphIndex(u16 c) const {
NW4HBM_ASSERT_VALID_PTR(502, this);
NW4HBM_ASSERT_VALID_PTR(503, mFontInfo);
FontCodeMap* pMap;
for (pMap = mFontInfo->pMap; pMap; pMap = pMap->pNext) {
if (pMap->ccodeBegin <= c && c <= pMap->ccodeEnd) {
return FindGlyphIndex(pMap, c);
}
}
return GLYPH_INDEX_NOT_FOUND;
}
u16 ResFontBase::FindGlyphIndex(const FontCodeMap* pMap, u16 c) const {
NW4HBM_ASSERT_VALID_PTR(539, this);
NW4HBM_ASSERT_VALID_PTR(540, pMap);
u16 index = GLYPH_INDEX_NOT_FOUND;
switch (pMap->mappingMethod) {
case FONT_MAPMETHOD_DIRECT: {
u16 offset = *pMap->mapInfo;
index = c - pMap->ccodeBegin + offset;
} break;
case FONT_MAPMETHOD_TABLE: {
int table_index = c - pMap->ccodeBegin;
index = pMap->mapInfo[table_index];
} break;
case FONT_MAPMETHOD_SCAN: {
const CMapInfoScan* scanInfo =
reinterpret_cast<const CMapInfoScan*>(pMap->mapInfo);
const CMapScanEntry* first = scanInfo->entries;
const CMapScanEntry* last = scanInfo->entries + (scanInfo->num - 1);
while (first <= last) {
const CMapScanEntry* mid = first + (last - first) / 2;
if (mid->ccode < c) {
first = mid + 1;
} else if (c < mid->ccode) {
last = mid - 1;
} else {
index = mid->index;
break;
}
}
} break;
default:
nw4hbm::db::detail::Panic(__FILE__, 597, "unknwon MAPMETHOD");
break;
}
return index;
}
const CharWidths& ResFontBase::GetCharWidthsFromIndex(u16 index) const {
const FontWidth* pWidth;
NW4HBM_ASSERT_VALID_PTR(615, this);
NW4HBM_ASSERT_VALID_PTR(616, mFontInfo);
for (pWidth = mFontInfo->pWidth; pWidth; pWidth = pWidth->pNext) {
if (pWidth->indexBegin <= index && index <= pWidth->indexEnd) {
return GetCharWidthsFromIndex(pWidth, index);
}
}
return mFontInfo->defaultWidth;
}
const CharWidths& ResFontBase::GetCharWidthsFromIndex(const FontWidth* pWidth,
u16 index) const {
NW4HBM_ASSERT_VALID_PTR(651, pWidth);
return pWidth->widthTable[index - pWidth->indexBegin];
}
void ResFontBase::GetGlyphFromIndex(Glyph* glyph, u16 index) const {
NW4HBM_ASSERT_VALID_PTR(671, this);
NW4HBM_ASSERT_VALID_PTR(672, mFontInfo);
FontTextureGlyph& tg = *mFontInfo->pGlyph;
u32 cellsInASheet = tg.sheetRow * tg.sheetLine;
u32 sheetNo = index / cellsInASheet;
u32 cellNo = index % cellsInASheet;
u32 cellUnitX = cellNo % tg.sheetRow;
u32 cellUnitY = cellNo / tg.sheetRow;
u32 cellPixelX = cellUnitX * (tg.cellWidth + 1);
u32 cellPixelY = cellUnitY * (tg.cellHeight + 1);
u32 offsetBytes = sheetNo * tg.sheetSize;
void* pSheet = tg.sheetImage + offsetBytes;
glyph->pTexture = pSheet;
glyph->widths = GetCharWidthsFromIndex(index);
glyph->height = static_cast<u8>(tg.cellHeight);
glyph->texFormat = static_cast<GXTexFmt>(tg.sheetFormat);
glyph->texWidth = static_cast<u16>(tg.sheetWidth);
glyph->texHeight = static_cast<u16>(tg.sheetHeight);
glyph->cellX = cellPixelX + 1;
glyph->cellY = cellPixelY + 1;
}
} // namespace detail
} // namespace ut
} // namespace nw4hbm
|