Skip to content

Commit 21cd00b

Browse files
committed
fonts: Use alt fonts for other languages and other consistency changes
1 parent 1f3c968 commit 21cd00b

14 files changed

Lines changed: 401 additions & 264 deletions

app/include/g2d.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@
44
#include <intraFont.h>
55
#include <string>
66

7-
extern intraFont *font;
7+
enum FontType {
8+
FONT_DEFAULT, // ltn8.pgf (Latin)
9+
FONT_JPN, // jpn0.pgf (Japanese)
10+
FONT_CN, // gb3s1518.bwfon (Chinese)
11+
FONT_KOR, // kr0.pgf (Korean)
12+
FONT_SYM, // arib.pgf (Symbols)
13+
NUM_FONTS
14+
};
15+
16+
extern intraFont *fonts[NUM_FONTS];
817
extern char font_size_cache[256];
918

1019
namespace G2D {
1120
void DrawRect(float x, float y, float width, float height, g2dColor colour);
1221
void DrawImage(g2dTexture *tex, float x, float y);
1322
void DrawImageScale(g2dTexture *tex, float x, float y, float w, float h);
14-
char *KeyboardGetText(const std::string &desc_msg, const std::string &initial_msg);
23+
char *KeyboardGetText(const std::string &desc_msg, const std::string &initialMsg);
24+
int LoadFonts(void);
25+
void UnloadFonts(void);
1526
void FontSetStyle(float size, unsigned int colour, unsigned int options);
1627
float GetTextHeight(void);
1728
float DrawText(float x, float y, const std::string &text);

app/source/g2d.cpp

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,22 @@
88
#include "log.h"
99
#include "utils.h"
1010

11-
intraFont *font;
11+
intraFont *fonts[NUM_FONTS];
1212

1313
namespace G2D {
14+
struct FontInfo {
15+
const char *filename;
16+
int encodingFlag;
17+
};
18+
19+
const FontInfo fontInfo[NUM_FONTS] = {
20+
{ "flash0:/font/ltn8.pgf", INTRAFONT_STRING_UTF8 },
21+
{ "flash0:/font/jpn0.pgf", INTRAFONT_STRING_SJIS },
22+
{ "flash0:/font/gb3s1518.bwfon", INTRAFONT_STRING_GBK },
23+
{ "flash0:/font/kr0.pgf", INTRAFONT_STRING_KOR },
24+
{ "flash0:/font/arib.pgf", INTRAFONT_STRING_UTF8 }
25+
};
26+
1427
void DrawRect(float x, float y, float width, float height, g2dColor colour) {
1528
g2dBeginRects(nullptr); {
1629
g2dSetColor(colour);
@@ -117,16 +130,16 @@ namespace G2D {
117130
return 0;
118131
}
119132

120-
char *KeyboardGetText(const std::string &desc_msg, const std::string &initial_msg) {
133+
char *KeyboardGetText(const std::string &desc_msg, const std::string &initialMsg) {
121134
int ret = 0;
122135
size_t i = 0;
123136
static char str[128];
124137
unsigned short initial[128] = { 0 };
125138
unsigned short desc[128] = { 0 };
126139

127-
if (initial_msg.c_str()[0] != 0) {
128-
for (i = 0; i <= initial_msg.length(); i++)
129-
initial[i] = static_cast<unsigned short>(initial_msg.c_str()[i]);
140+
if (initialMsg.c_str()[0] != 0) {
141+
for (i = 0; i <= initialMsg.length(); i++)
142+
initial[i] = static_cast<unsigned short>(initialMsg.c_str()[i]);
130143
}
131144

132145
if (desc_msg.c_str()[0] != 0) {
@@ -140,15 +153,67 @@ namespace G2D {
140153
return 0;
141154
}
142155

156+
int LoadFonts(void) {
157+
int ret = 0;
158+
159+
if (R_FAILED(ret = intraFontInit())) {
160+
Log::Error("intraFontInit failed: 0x%08x\n", ret);
161+
return ret;
162+
}
163+
164+
for (int i = 0; i < NUM_FONTS; i++) {
165+
fonts[i] = intraFontLoad(fontInfo[i].filename, fontInfo[i].encodingFlag);
166+
if (!fonts[i]) {
167+
Log::Error("intraFontLoad() failed: %s\n", fontInfo[i].filename);
168+
}
169+
}
170+
171+
for (int i = 0; i < NUM_FONTS - 1; i++) {
172+
if (fonts[i] && fonts[i + 1]) {
173+
intraFontSetAltFont(fonts[i], fonts[i + 1]);
174+
}
175+
}
176+
177+
G2D::FontSetStyle(1.f, WHITE, INTRAFONT_ALIGN_LEFT);
178+
179+
// Font size cache
180+
for (int i = 0; i < 256; i++) {
181+
char character[2] = {0};
182+
character[0] = i;
183+
character[1] = '\0';
184+
font_size_cache[i] = intraFontMeasureText(fonts[FONT_DEFAULT], character);
185+
}
186+
187+
return 0;
188+
}
189+
190+
void UnloadFonts(void) {
191+
for (int i = 0; i < NUM_FONTS; i++) {
192+
if (fonts[i]) {
193+
intraFontUnload(fonts[i]);
194+
}
195+
}
196+
197+
intraFontShutdown();
198+
}
199+
143200
void FontSetStyle(float size, unsigned int colour, unsigned int options) {
144-
intraFontSetStyle(font, size, colour, G2D_RGBA(0, 0, 0, 0), 0.f, options);
201+
for (int i = 0; i < NUM_FONTS; i++) {
202+
if (!fonts[i]) {
203+
continue;
204+
}
205+
206+
// If it's the default font, use the full size, else use 80% of the size.
207+
float scaleSize = (i == FONT_DEFAULT) ? size : size * 0.8f;
208+
intraFontSetStyle(fonts[i], scaleSize, colour, G2D_RGBA(0, 0, 0, 0), 0.f, options);
209+
}
145210
}
146211

147212
float GetTextHeight(void) {
148-
return font->advancey * font->size / 4.f + 2.f;
213+
return fonts[FONT_DEFAULT]->advancey * fonts[FONT_DEFAULT]->size / 4.f + 2.f;
149214
}
150215

151216
float DrawText(float x, float y, const std::string &text) {
152-
return intraFontPrintf(font, x, y, text.c_str());
217+
return intraFontPrintf(fonts[FONT_DEFAULT], x, y, text.c_str());
153218
}
154219
}

app/source/gui.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ namespace GUI {
4646

4747
static char percent_string[10];
4848
std::snprintf(percent_string, 10, "%d", percent);
49-
int percent_width = intraFontMeasureText(font, percent_string);
50-
intraFontPrintf(font, 475 - percent_width - 15, 14, "%s%%", percent_string);
49+
int percent_width = intraFontMeasureText(fonts[FONT_DEFAULT], percent_string);
50+
intraFontPrintf(fonts[FONT_DEFAULT], 475 - percent_width - 15, 14, "%s%%", percent_string);
5151

5252
G2D::DrawImage(state != 0? battery_charging[battery_val] : battery[battery_val], 475 - percent_width - battery[battery_val]->w - 15, 2);
5353
}
@@ -70,7 +70,7 @@ namespace GUI {
7070
G2D::FontSetStyle(1.f, TITLE_COLOUR, INTRAFONT_ALIGN_LEFT);
7171
G2D::DrawText(((480 - (dialog[0]->w)) / 2) + 10, ((272 - (dialog[0]->h)) / 2) + 20, title.c_str());
7272

73-
int text_width = intraFontMeasureText(font, message.c_str());
73+
int text_width = intraFontMeasureText(fonts[FONT_DEFAULT], message.c_str());
7474
G2D::FontSetStyle(1.f, TEXT_COLOUR, INTRAFONT_ALIGN_LEFT);
7575
G2D::DrawText(((480 - (text_width)) / 2), ((272 - (dialog[0]->h)) / 2) + 60, message.c_str());
7676

0 commit comments

Comments
 (0)