added support for UTF-8
This commit is contained in:
parent
475b8272d0
commit
8ba5f99dc8
3 changed files with 41 additions and 1 deletions
21
src/font.cpp
21
src/font.cpp
|
@ -16,6 +16,7 @@
|
|||
#include "config.hpp"
|
||||
#include "font.hpp"
|
||||
#include "game_config.hpp"
|
||||
#include "language.hpp"
|
||||
#include "tooltips.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
@ -131,6 +132,24 @@ const SDL_Color& get_side_colour(int side)
|
|||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
SDL_Surface* render_text(TTF_Font* font,const std::string& str,
|
||||
const SDL_Color& colour)
|
||||
{
|
||||
switch(charset())
|
||||
{
|
||||
case CHARSET_UTF8:
|
||||
return TTF_RenderUTF8_Blended(font,str.c_str(),colour);
|
||||
case CHARSET_LATIN1:
|
||||
return TTF_RenderText_Blended(font,str.c_str(),colour);
|
||||
default:
|
||||
std::cerr << "Unrecognized charset\n";
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
SDL_Rect draw_text_line(display* gui, const SDL_Rect& area, int size,
|
||||
const SDL_Color& colour, const std::string& text,
|
||||
int x, int y, SDL_Surface* bg, bool use_tooltips)
|
||||
|
@ -143,7 +162,7 @@ SDL_Rect draw_text_line(display* gui, const SDL_Rect& area, int size,
|
|||
return res;
|
||||
}
|
||||
|
||||
scoped_sdl_surface surface(TTF_RenderText_Blended(font,text.c_str(),colour));
|
||||
scoped_sdl_surface surface(render_text(font,text.c_str(),colour));
|
||||
if(surface == NULL) {
|
||||
std::cerr << "Could not render ttf: '" << text << "'\n";
|
||||
SDL_Rect res;
|
||||
|
|
|
@ -20,6 +20,12 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
CHARSET charset_used = CHARSET_LATIN1;
|
||||
}
|
||||
|
||||
CHARSET charset() { return charset_used; }
|
||||
|
||||
std::map<std::string,std::string> string_table;
|
||||
|
||||
std::vector<std::string> get_languages(config& cfg)
|
||||
|
@ -43,6 +49,17 @@ bool internal_set_language(const std::string& locale, config& cfg)
|
|||
i != lang.end(); ++i) {
|
||||
if((*i)->values["id"] == locale || (*i)->values["language"] == locale) {
|
||||
|
||||
const std::string& enc = (**i)["encoding"];
|
||||
if(enc == "UTF-8") {
|
||||
charset_used = CHARSET_UTF8;
|
||||
} else if(enc == "LATIN1" || enc == "") {
|
||||
charset_used = CHARSET_LATIN1;
|
||||
} else {
|
||||
std::cerr << "Unrecognized character set: '" << enc
|
||||
<< "' (defaulting to LATIN1)\n";
|
||||
charset_used = CHARSET_LATIN1;
|
||||
}
|
||||
|
||||
for(std::map<std::string,std::string>::const_iterator j =
|
||||
(*i)->values.begin(); j != (*i)->values.end(); ++j) {
|
||||
string_table[j->first] = j->second;
|
||||
|
@ -89,3 +106,4 @@ std::string get_locale()
|
|||
std::cerr << "locale could not be determined; defaulting to locale 'en'\n";
|
||||
return "en";
|
||||
}
|
||||
|
||||
|
|
|
@ -26,4 +26,7 @@ bool set_language(const std::string& locale, config& cfg);
|
|||
|
||||
std::string get_locale();
|
||||
|
||||
enum CHARSET { CHARSET_LATIN1, CHARSET_UTF8 };
|
||||
CHARSET charset();
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue