88#include " log.h"
99#include " utils.h"
1010
11- intraFont *font ;
11+ intraFont *fonts[NUM_FONTS] ;
1212
1313namespace 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}
0 commit comments