diff options
author | Campbell Barton <ideasman42@gmail.com> | 2020-04-28 06:30:59 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2020-04-28 06:48:15 +0300 |
commit | ef1187387bda6ede146a366042e82890bc157127 (patch) | |
tree | e2e66d8c931b27ffe0f96706e38e715fb77af60b /source/blender/editors/interface/interface_widgets.c | |
parent | e78470d95411764c59af58dc996398a02bc40ae6 (diff) |
Fix T76152: Shortcut underline under wrong letter
Use glyph bounds to calculate a better underline position.
Diffstat (limited to 'source/blender/editors/interface/interface_widgets.c')
-rw-r--r-- | source/blender/editors/interface/interface_widgets.c | 59 |
1 files changed, 42 insertions, 17 deletions
diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c index ae40b01c77d..2ac84b4b043 100644 --- a/source/blender/editors/interface/interface_widgets.c +++ b/source/blender/editors/interface/interface_widgets.c @@ -2092,6 +2092,22 @@ static void widget_draw_text_ime_underline(const uiFontStyle *fstyle, } #endif /* WITH_INPUT_IME */ +static bool widget_draw_text_underline_calc_center_x(const char *UNUSED(str), + const size_t str_ofs, + const rcti *glyph_bounds, + const int glyph_advance_x, + void *user_data) +{ + /* The index of the character to get, set to the x-position. */ + int *ul_data = user_data; + if (ul_data[0] == (int)str_ofs) { + ul_data[1] = glyph_bounds->xmin + (glyph_advance_x / 2); + /* Early exit. */ + return false; + } + return true; +} + static void widget_draw_text(const uiFontStyle *fstyle, const uiWidgetColors *wcol, uiBut *but, @@ -2320,31 +2336,40 @@ static void widget_draw_text(const uiFontStyle *fstyle, NULL); if (but->menu_key != '\0') { - char fixedbuf[128]; - const char *str; - - BLI_strncpy(fixedbuf, drawstr + but->ofs, min_ii(sizeof(fixedbuf), drawlen)); - - str = strchr(fixedbuf, but->menu_key - 32); /* upper case */ - if (str == NULL) { - str = strchr(fixedbuf, but->menu_key); + const char *drawstr_ofs = drawstr + but->ofs; + int ul_index = -1; + + { + /* Find upper case, fallback to lower case. */ + const char *drawstr_end = drawstr_ofs + drawlen; + const char keys[] = {but->menu_key - 32, but->menu_key}; + for (int i = 0; i < ARRAY_SIZE(keys); i++) { + const char *drawstr_menu = strchr(drawstr_ofs, keys[i]); + if (drawstr_menu != NULL && drawstr_menu < drawstr_end) { + ul_index = (int)(drawstr_menu - drawstr_ofs); + break; + } + } } - if (str) { - int ul_index = -1; - float ul_advance; - - ul_index = (int)(str - fixedbuf); - + if (ul_index != -1) { if (fstyle->kerning == 1) { BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT); } - fixedbuf[ul_index] = '\0'; - ul_advance = BLF_width(fstyle->uifont_id, fixedbuf, ul_index) + (1.0f * UI_DPI_FAC); + int ul_data[2] = { + ul_index, /* Character index to test. */ + 0, /* Write the x-offset here. */ + }; + BLF_boundbox_foreach_glyph(fstyle->uifont_id, + drawstr_ofs, + ul_index + 1, + widget_draw_text_underline_calc_center_x, + ul_data); + ul_data[1] -= BLF_width(fstyle->uifont_id, "_", 2) / 2.0f; BLF_position(fstyle->uifont_id, - rect->xmin + font_xofs + (int)ul_advance, + rect->xmin + font_xofs + ul_data[1], rect->ymin + font_yofs, 0.0f); BLF_color4ubv(fstyle->uifont_id, wcol->text); |