Skip to content

Commit aeb2a15

Browse files
committed
rg_gui: Fixed crash when configured font index is removed
1 parent b24be6d commit aeb2a15

2 files changed

Lines changed: 19 additions & 18 deletions

File tree

components/retro-go/fonts/fonts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ enum {
2020
RG_FONT_VERA_11,
2121
RG_FONT_VERA_14,
2222
RG_FONT_MAX,
23+
24+
RG_FONT_DEFAULT = RG_FONT_VERA_11,
2325
};
2426

2527
static const rg_font_t *fonts[RG_FONT_MAX] = {

components/retro-go/rg_gui.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ static struct
1919
struct {int left, top, right, bottom;} margins;
2020
struct
2121
{
22-
const rg_font_t *font;
23-
int font_height;
2422
rg_color_t box_background;
2523
rg_color_t box_header;
2624
rg_color_t box_border;
@@ -32,7 +30,9 @@ static struct
3230
} style;
3331
char theme_name[32];
3432
cJSON *theme_obj;
33+
const rg_font_t *font;
3534
int font_index;
35+
int font_height;
3636
bool show_clock;
3737
bool initialized;
3838
} gui;
@@ -108,10 +108,12 @@ void rg_gui_init(void)
108108
// because of how this is defined in config.h. It should be documented somewhere...
109109
gui.margins = (__typeof__(gui.margins))RG_SCREEN_SAFE_AREA;
110110
gui.draw_buffer = get_draw_buffer(gui.screen_width, 18, C_BLACK);
111-
rg_gui_set_language_id(rg_settings_get_number(NS_GLOBAL, SETTING_LANGUAGE, RG_LANG_EN));
112-
rg_gui_set_font(rg_settings_get_number(NS_GLOBAL, SETTING_FONTTYPE, RG_FONT_VERA_11));
113-
rg_gui_set_theme(rg_settings_get_string(NS_GLOBAL, SETTING_THEME, NULL));
114111
gui.show_clock = rg_settings_get_boolean(NS_GLOBAL, SETTING_CLOCK, false);
112+
if (!rg_gui_set_language_id(rg_settings_get_number(NS_GLOBAL, SETTING_LANGUAGE, RG_LANG_EN)))
113+
rg_localization_set_language_id(RG_LANG_EN);
114+
if (!rg_gui_set_font(rg_settings_get_number(NS_GLOBAL, SETTING_FONTTYPE, RG_FONT_DEFAULT)))
115+
rg_gui_set_font(0);
116+
rg_gui_set_theme(rg_settings_get_string(NS_GLOBAL, SETTING_THEME, NULL));
115117
gui.initialized = true;
116118
}
117119

@@ -123,7 +125,6 @@ bool rg_gui_set_language_id(int index)
123125
RG_LOGI("Language set to: %s (%d)", rg_localization_get_language_name(index), index);
124126
return true;
125127
}
126-
rg_localization_set_language_id(RG_LANG_EN);
127128
RG_LOGE("Invalid language id %d!", index);
128129
return false;
129130
}
@@ -221,16 +222,14 @@ bool rg_gui_set_font(int index)
221222
if (index < 0 || index > RG_FONT_MAX - 1)
222223
return false;
223224

224-
const rg_font_t *font = fonts[index];
225-
225+
gui.font = fonts[index];
226226
gui.font_index = index;
227-
gui.style.font = font;
228-
gui.style.font_height = (index < 3) ? (8 + index * 4) : font->height;
227+
gui.font_height = (index < 3) ? (8 + index * 4) : gui.font->height;
229228

230229
rg_settings_set_number(NS_GLOBAL, SETTING_FONTTYPE, index);
231230

232231
RG_LOGI("Font set to: %s (height=%d, scaling=%.2f)\n",
233-
gui.style.font->name, gui.style.font_height, (float)gui.style.font_height / font->height);
232+
gui.font->name, gui.font_height, (float)gui.font_height / gui.font->height);
234233

235234
return true;
236235
}
@@ -353,9 +352,9 @@ static size_t get_glyph(uint32_t *output, const rg_font_t *font, int points, int
353352
rg_rect_t rg_gui_draw_text(int x_pos, int y_pos, int width, const char *text, // const rg_font_t *font,
354353
rg_color_t color_fg, rg_color_t color_bg, uint32_t flags)
355354
{
356-
const rg_font_t *font = gui.style.font;
355+
const rg_font_t *font = gui.font;
357356
int padding = (flags & RG_TEXT_NO_PADDING) ? 0 : 1;
358-
int font_height = (flags & RG_TEXT_BIGGER) ? gui.style.font_height * 2 : gui.style.font_height;
357+
int font_height = (flags & RG_TEXT_BIGGER) ? gui.font_height * 2 : gui.font_height;
359358
int monospace = ((flags & RG_TEXT_MONOSPACE) || font->type == 0) ? font->width : 0;
360359
int line_height = font_height + padding * 2;
361360
int line_count = 0;
@@ -649,7 +648,7 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
649648
{
650649
const size_t options_count = get_dialog_items_count(options);
651650
const int sep_width = TEXT_RECT(": ", 0).width;
652-
const int font_height = gui.style.font_height;
651+
const int font_height = gui.font_height;
653652
const int max_box_width = 0.82f * gui.screen_width;
654653
const int max_box_height = 0.82f * gui.screen_height;
655654
const int box_padding = 6;
@@ -1626,10 +1625,10 @@ static rg_gui_event_t font_type_cb(rg_gui_option_t *option, rg_gui_event_t event
16261625
return RG_DIALOG_REDRAW;
16271626
if (event == RG_DIALOG_NEXT && rg_gui_set_font(gui.font_index + 1))
16281627
return RG_DIALOG_REDRAW;
1629-
if (gui.style.font_height != gui.style.font->height)
1630-
sprintf(option->value, "%s (%d)", gui.style.font->name, gui.style.font_height);
1628+
if (gui.font_height != gui.font->height)
1629+
sprintf(option->value, "%s (%d)", gui.font->name, gui.font_height);
16311630
else
1632-
sprintf(option->value, "%s", gui.style.font->name);
1631+
sprintf(option->value, "%s", gui.font->name);
16331632
return RG_DIALOG_VOID;
16341633
}
16351634

@@ -2180,7 +2179,7 @@ static rg_gui_event_t slot_select_cb(rg_gui_option_t *option, rg_gui_event_t eve
21802179
}
21812180
rg_gui_draw_image(0, margin, gui.screen_width, gui.screen_height - margin * 2, true, preview);
21822181
rg_gui_draw_rect(0, margin, gui.screen_width, gui.screen_height - margin * 2, border, color, C_NONE);
2183-
rg_gui_draw_rect(border, margin + border, gui.screen_width - border * 2, gui.style.font_height * 2 + 6, 0, C_BLACK, C_BLACK);
2182+
rg_gui_draw_rect(border, margin + border, gui.screen_width - border * 2, gui.font_height * 2 + 6, 0, C_BLACK, C_BLACK);
21842183
rg_gui_draw_text(border + 60, margin + border + 5, gui.screen_width - border * 2 - 120, buffer, C_WHITE, C_BLACK, RG_TEXT_ALIGN_CENTER|RG_TEXT_BIGGER|RG_TEXT_NO_PADDING);
21852184
rg_surface_free(preview);
21862185
}

0 commit comments

Comments
 (0)