mirror of
https://forgejo.xatacraft.ru/Sunshine/mkxp-sunshine.git
synced 2026-08-20 13:59:57 +00:00
-settings menu now loads font name and font size from 2 .ini files, so that these can be set for new languages that need to be added
This commit is contained in:
parent
9960d49d00
commit
6e197943c0
4 changed files with 146 additions and 33 deletions
156
src/i18n.cpp
156
src/i18n.cpp
|
|
@ -7,16 +7,23 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* LOC_HEADER = "ONELOC\x10";
|
||||
|
||||
char** strdict = 0;
|
||||
unsigned int nStr = 0;
|
||||
int family = LOCALE_FAMILY_LATIN;
|
||||
size_t readSize;
|
||||
|
||||
int getLocaleFamily() {
|
||||
return family;
|
||||
}
|
||||
const int MAX_LANGUAGES = 20;
|
||||
|
||||
const int LANGCODE_SIZE = 16;
|
||||
const int LANGFONT_SIZE = 128;
|
||||
|
||||
char* currentLocale = 0;
|
||||
|
||||
struct LanguageFontAndSize {
|
||||
char* lang_code = 0;
|
||||
int size = 0;
|
||||
char* font_name = 0;
|
||||
};
|
||||
|
||||
LanguageFontAndSize** languageMetadata = 0;
|
||||
|
||||
const char* findtext(unsigned int msgid, const char* fallback) {
|
||||
if (msgid >= nStr) {
|
||||
|
|
@ -31,24 +38,136 @@ void unloadLocale() {
|
|||
free(strdict[i]);
|
||||
}
|
||||
free(strdict);
|
||||
free(currentLocale);
|
||||
strdict = 0;
|
||||
nStr = 0;
|
||||
}
|
||||
|
||||
void _setLocaleFamily(const char* locale) {
|
||||
if (!strcmp(locale, "ja")
|
||||
|| !strcmp(locale, "ko")
|
||||
|| !strcmp(locale, "zh_CN")) {
|
||||
family = LOCALE_FAMILY_ASIAN;
|
||||
} else {
|
||||
family = LOCALE_FAMILY_LATIN;
|
||||
|
||||
void unloadLanguageMetadata() {
|
||||
if (languageMetadata) {
|
||||
for (int i = 0; i < MAX_LANGUAGES; ++i) {
|
||||
LanguageFontAndSize* ldata = languageMetadata[i];
|
||||
if (ldata) {
|
||||
if (ldata->font_name) {
|
||||
free(ldata->font_name);
|
||||
}
|
||||
if (ldata->lang_code) {
|
||||
free(ldata->lang_code);
|
||||
}
|
||||
free(ldata);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(languageMetadata);
|
||||
}
|
||||
|
||||
void loadLanguageMetadata() {
|
||||
if (languageMetadata) {
|
||||
unloadLanguageMetadata();
|
||||
}
|
||||
|
||||
char line[256];
|
||||
|
||||
languageMetadata = (LanguageFontAndSize**) calloc(MAX_LANGUAGES, sizeof(LanguageFontAndSize*));
|
||||
FILE* fontsFile = fopen("Languages/internal/language_fonts.ini", "r");
|
||||
if (fontsFile) {
|
||||
|
||||
int languageMetadataIndex = 0;
|
||||
|
||||
while (fgets(line, 1024, fontsFile)) {
|
||||
char* indexOfEquals = strchr(line, '=');
|
||||
if (indexOfEquals) {
|
||||
// splitting the string in place here
|
||||
indexOfEquals[0] = 0;
|
||||
char* indexOfFontName = indexOfEquals + 1;
|
||||
|
||||
// remove new line from end of font name
|
||||
char* indexOfNewLine = strchr(indexOfFontName, '\n');
|
||||
if (indexOfNewLine) {
|
||||
indexOfNewLine[0] = 0;
|
||||
}
|
||||
|
||||
// make new strings for code and font name
|
||||
char* langCode = (char*)calloc(LANGCODE_SIZE, sizeof(char));
|
||||
char* langFont = (char*)calloc(LANGFONT_SIZE, sizeof(char));
|
||||
|
||||
strcpy(langCode, line);
|
||||
strcpy(langFont, indexOfFontName);
|
||||
|
||||
// allocate metadata mem
|
||||
LanguageFontAndSize* metadata = (LanguageFontAndSize*) calloc(1, sizeof(LanguageFontAndSize));
|
||||
metadata->font_name = langFont;
|
||||
metadata->lang_code = langCode;
|
||||
|
||||
// put into metadata array and check if we're going to go over the language limit
|
||||
languageMetadata[languageMetadataIndex] = metadata;
|
||||
languageMetadataIndex++;
|
||||
if (languageMetadataIndex >= MAX_LANGUAGES) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fontsFile);
|
||||
}
|
||||
|
||||
|
||||
FILE* fontSizesFile = fopen("Languages/internal/language_sizes.ini", "r");
|
||||
if (fontSizesFile) {
|
||||
while (fgets(line, 1024, fontSizesFile)) {
|
||||
int languageMetadataIndex = 0;
|
||||
char* indexOfEquals = strchr(line, '=');
|
||||
if (indexOfEquals) {
|
||||
// splitting the string in place here
|
||||
indexOfEquals[0] = 0;
|
||||
char* indexOfFontSize = indexOfEquals + 1;
|
||||
|
||||
// remove new line from end of font size
|
||||
char* indexOfNewLine = strchr(indexOfFontSize, '\n');
|
||||
if (indexOfNewLine) {
|
||||
indexOfNewLine[0] = 0;
|
||||
}
|
||||
|
||||
int fontSize = atoi(indexOfFontSize);
|
||||
|
||||
for (int i = 0; i < MAX_LANGUAGES; i++) {
|
||||
// search for corresponding langCode in metadata array to populate font size in the appropriate metadata
|
||||
LanguageFontAndSize* metadata = languageMetadata[i];
|
||||
if (metadata && strcmp(line, metadata->lang_code) == 0) {
|
||||
metadata->size = fontSize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fontSizesFile);
|
||||
}
|
||||
}
|
||||
|
||||
void _read(void * ptr, size_t size, size_t count, FILE * stream) {
|
||||
readSize = fread(ptr, size, count, stream);
|
||||
if (readSize != count) {
|
||||
int getFontSize() {
|
||||
for (int i = 0; i < MAX_LANGUAGES; i++) {
|
||||
LanguageFontAndSize* metadata = languageMetadata[i];
|
||||
if (metadata && strcmp(currentLocale, metadata->lang_code) == 0) {
|
||||
return metadata->size;
|
||||
}
|
||||
}
|
||||
|
||||
// default to 12?
|
||||
return 12;
|
||||
}
|
||||
|
||||
char* getFontName() {
|
||||
for (int i = 0; i < MAX_LANGUAGES; i++) {
|
||||
LanguageFontAndSize* metadata = languageMetadata[i];
|
||||
if (metadata && strcmp(currentLocale, metadata->lang_code) == 0) {
|
||||
return metadata->font_name;
|
||||
}
|
||||
}
|
||||
|
||||
// default to terminus?
|
||||
return "Terminus (TTF)";
|
||||
}
|
||||
|
||||
void loadLocale(const char* locale) {
|
||||
|
|
@ -58,7 +177,8 @@ void loadLocale(const char* locale) {
|
|||
|
||||
unloadLocale();
|
||||
|
||||
_setLocaleFamily(locale);
|
||||
currentLocale = (char*) calloc(128, sizeof(char));
|
||||
strncpy(currentLocale, locale, 128 - 1);
|
||||
|
||||
int dictSize = 100;
|
||||
// currently there are 52, but 100 should be plenty if we ever do add more
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@ void unloadLocale();
|
|||
void loadLocale(const char* locale);
|
||||
void decodeEscapeChars(char* s);
|
||||
const char* findtext(unsigned int msgid, const char* fallback);
|
||||
int getLocaleFamily();
|
||||
|
||||
#define LOCALE_FAMILY_LATIN 1
|
||||
#define LOCALE_FAMILY_ASIAN 2
|
||||
void loadLanguageMetadata();
|
||||
void unloadLanguageMetadata();
|
||||
int getFontSize();
|
||||
char* getFontName();
|
||||
|
||||
#endif /* end of include guard: I18N_H */
|
||||
|
|
|
|||
|
|
@ -275,6 +275,8 @@ int main(int argc, char *argv[])
|
|||
/* Initialize physfs here so that config can call PHYSFS_getPrefDir */
|
||||
PHYSFS_init(argv[0]);
|
||||
|
||||
loadLanguageMetadata();
|
||||
|
||||
/* now we load the config */
|
||||
Config conf;
|
||||
conf.read(argc, argv);
|
||||
|
|
@ -432,6 +434,7 @@ int main(int argc, char *argv[])
|
|||
Debug() << "Shutting down.";
|
||||
|
||||
unloadLocale();
|
||||
unloadLanguageMetadata();
|
||||
|
||||
alcCloseDevice(alcDev);
|
||||
SDL_DestroyWindow(win);
|
||||
|
|
|
|||
|
|
@ -47,12 +47,6 @@ const uint8_t cBgDark = 20;
|
|||
const uint8_t cLine = 0;
|
||||
const uint8_t cText = 255;
|
||||
|
||||
// const char *const fontFamilyLatin = "Terminus (TTF)";
|
||||
const char *const fontFamilyLatin = "WenQuanYi Micro Hei";
|
||||
const uint8_t fontSizeLatin = 12;
|
||||
const char *const fontFamilyAsian = "WenQuanYi Micro Hei";
|
||||
const uint8_t fontSizeAsian = 16;
|
||||
|
||||
static bool pointInRect(const SDL_Rect &r, int x, int y)
|
||||
{
|
||||
return (x >= r.x && x <= r.x+r.w && y >= r.y && y <= r.y+r.h);
|
||||
|
|
@ -1086,11 +1080,7 @@ SettingsMenu::SettingsMenu(RGSSThreadData &rtData)
|
|||
p->winSurf = SDL_GetWindowSurface(p->window);
|
||||
p->winID = SDL_GetWindowID(p->window);
|
||||
|
||||
if (getLocaleFamily() == LOCALE_FAMILY_ASIAN) {
|
||||
p->font = shState->fontState().getFont(fontFamilyAsian, fontSizeAsian);
|
||||
} else {
|
||||
p->font = shState->fontState().getFont(fontFamilyLatin, fontSizeLatin);
|
||||
}
|
||||
p->font = shState->fontState().getFont(getFontName(), getFontSize());
|
||||
|
||||
|
||||
p->rgb = p->winSurf->format;
|
||||
|
|
|
|||
Loading…
Reference in a new issue