Skip to content

Commit

Permalink
Fix fonts list menu not saving custom added fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Apr 29, 2024
1 parent d602cad commit cc18241
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/Menus.c
Expand Up @@ -1655,8 +1655,18 @@ static void FontListScreen_LoadEntries(struct ListScreen* s) {
ListScreen_Select(s, SysFonts_UNSAFE_GetDefault());
}

static void FontListScreen_RegisterCallback(const cc_string* path) {
Chat_Add1("Loaded font from %s", path);
}

static void FontListScreen_UploadCallback(const cc_string* path) {
SysFonts_Register(path);
cc_result res = SysFonts_Register(path, FontListScreen_RegisterCallback);

if (res) {
Logger_SimpleWarn2(res, "loading font from", path);
} else {
SysFonts_SaveCache();
}
}

static void FontListScreen_ActionFunc(void* s, void* w) {
Expand Down
2 changes: 1 addition & 1 deletion src/Platform_Posix.c
Expand Up @@ -497,7 +497,7 @@ void Waitable_WaitFor(void* handle, cc_uint32 milliseconds) {
*--------------------------------------------------------Font/Text--------------------------------------------------------*
*#########################################################################################################################*/
static void FontDirCallback(const cc_string* path, void* obj) {
SysFonts_Register(path);
SysFonts_Register(path, NULL);
}

void Platform_LoadSysFonts(void) {
Expand Down
2 changes: 1 addition & 1 deletion src/Platform_Windows.c
Expand Up @@ -360,7 +360,7 @@ static void FontDirCallback(const cc_string* path, void* obj) {
static const cc_string fonExt = String_FromConst(".fon");
/* Completely skip windows .FON files */
if (String_CaselessEnds(path, &fonExt)) return;
SysFonts_Register(path);
SysFonts_Register(path, NULL);
}

void Platform_LoadSysFonts(void) {
Expand Down
34 changes: 25 additions & 9 deletions src/SystemFonts.c
Expand Up @@ -191,7 +191,7 @@ static void SysFonts_LoadPlatform(void) {
InitFreeTypeLibrary();
Platform_LoadSysFonts();

if (fonts_changed) EntryList_Save(&font_list, FONT_CACHE_FILE);
if (fonts_changed) SysFonts_SaveCache();
}

static cc_bool loadedCachedFonts;
Expand All @@ -202,6 +202,10 @@ static void SysFonts_LoadCached(void) {
EntryList_UNSAFE_Load(&font_list, FONT_CACHE_FILE);
}

void SysFonts_SaveCache(void) {
EntryList_Save(&font_list, FONT_CACHE_FILE);
}


/* Some language-specific fonts don't support English letters */
/* and show entirely as '[]' - better off ignoring such fonts */
Expand Down Expand Up @@ -240,7 +244,7 @@ static void SysFonts_Add(const cc_string* path, FT_Face face, int index, char ty
fonts_changed = true;
}

static int SysFonts_DoRegister(const cc_string* path, int faceIndex) {
static int SysFonts_DoRegister(const cc_string* path, int faceIndex, SysFont_RegisterCallback callback) {
struct SysFont font;
FT_Open_Args args;
FT_Error err;
Expand All @@ -263,11 +267,12 @@ static int SysFonts_DoRegister(const cc_string* path, int faceIndex) {
SysFonts_Add(path, font.face, faceIndex, 'R', "Regular");
}

if (callback) callback(path);
FT_Done_Face(font.face);
return count;
}

void SysFonts_Register(const cc_string* path) {
cc_result SysFonts_Register(const cc_string* path, SysFont_RegisterCallback callback) {
cc_string entry, name, value;
cc_string fontPath, index;
int i, count;
Expand All @@ -278,14 +283,15 @@ void SysFonts_Register(const cc_string* path) {
String_UNSAFE_Separate(&entry, '=', &name, &value);

String_UNSAFE_Separate(&value, ',', &fontPath, &index);
if (String_CaselessEquals(path, &fontPath)) return;
if (String_CaselessEquals(path, &fontPath)) return 0;
}

count = SysFonts_DoRegister(path, 0);
count = SysFonts_DoRegister(path, 0, callback);
/* there may be more than one font in a font file */
for (i = 1; i < count; i++) {
SysFonts_DoRegister(path, i);
SysFonts_DoRegister(path, i, callback);
}
return 0;
}


Expand Down Expand Up @@ -631,7 +637,11 @@ void SysFont_Free(struct FontDesc* desc) {
Mem_Free(desc->handle);
}

void SysFonts_Register(const cc_string* path) { }
void SysFonts_SaveCache(void) { }
cc_result SysFonts_Register(const cc_string* path, SysFont_RegisterCallback callback) {
return ERR_NOT_SUPPORTED;
}

extern void interop_SetFont(const char* font, int size, int flags);
extern double interop_TextWidth(const char* text, const int len);
extern double interop_TextDraw(const char* text, const int len, struct Bitmap* bmp, int x, int y, cc_bool shadow, const char* hex);
Expand Down Expand Up @@ -692,7 +702,10 @@ extern void interop_SysFontFree(void* handle);
extern int interop_SysTextWidth(struct DrawTextArgs* args);
extern void interop_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow);

void SysFonts_Register(const cc_string* path) { }
void SysFonts_SaveCache(void) { }
cc_result SysFonts_Register(const cc_string* path, SysFont_RegisterCallback callback) {
return ERR_NOT_SUPPORTED;
}

const cc_string* SysFonts_UNSAFE_GetDefault(void) {
return &String_Empty;
Expand Down Expand Up @@ -834,7 +847,10 @@ static cc_uint8 font_bitmap[][CELL_SIZE] = {
};


void SysFonts_Register(const cc_string* path) { }
void SysFonts_SaveCache(void) { }
cc_result SysFonts_Register(const cc_string* path, SysFont_RegisterCallback callback) {
return ERR_NOT_SUPPORTED;
}

const cc_string* SysFonts_UNSAFE_GetDefault(void) { return &String_Empty; }

Expand Down
4 changes: 3 additions & 1 deletion src/SystemFonts.h
Expand Up @@ -26,11 +26,13 @@ int SysFont_TextWidth(struct DrawTextArgs* args);
/* Draws the given text with the given system font onto the given bitmap */
void SysFont_DrawText(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow);

typedef void (*SysFont_RegisterCallback)(const cc_string* path);
/* Gets the name of the default system font used */
const cc_string* SysFonts_UNSAFE_GetDefault(void);
/* Gets the list of all supported system font names on this platform */
CC_API void SysFonts_GetNames(struct StringsBuffer* buffer);
/* Attempts to decode one or more fonts from the given file */
/* NOTE: If this file has been decoded before (fontscache.txt), does nothing */
void SysFonts_Register(const cc_string* path);
cc_result SysFonts_Register(const cc_string* path, SysFont_RegisterCallback callback);
void SysFonts_SaveCache(void);
#endif

0 comments on commit cc18241

Please sign in to comment.