UI: Correction & Improvements to File Browser Font Tooltips

The tooltips for fonts in the file browser, while in list view, now
shows either no preview or a blank white square (depending if you
viewed them with thumbnails in the current session). This PR corrects
that to always show a preview. It also shows this preview while in
thumbnail view.

Pull Request: https://projects.blender.org/blender/blender/pulls/145685
This commit is contained in:
Harley Acheson
2025-09-05 18:41:18 +02:00
committed by Harley Acheson
parent b910e04a2a
commit 68e5851615
4 changed files with 39 additions and 10 deletions

View File

@@ -1897,7 +1897,7 @@ static void ui_tooltip_from_vfont(const VFont &font, uiTooltipData &data)
float color[4];
const uiWidgetColors *theme = ui_tooltip_get_theme();
rgba_uchar_to_float(color, theme->text);
ImBuf *ibuf = IMB_font_preview(filepath_abs, 200 * UI_SCALE_FAC, color);
ImBuf *ibuf = IMB_font_preview(filepath_abs, 256 * UI_SCALE_FAC, color, "ABCDabefg&0123");
if (ibuf) {
uiTooltipImage image_data;
image_data.width = ibuf->x;

View File

@@ -279,6 +279,16 @@ static void file_draw_tooltip_custom_func(bContext & /*C*/,
}
}
}
else if (file->typeflag & FILE_TYPE_FTFONT) {
float color[4];
bTheme *btheme = UI_GetTheme();
rgba_uchar_to_float(color, btheme->tui.wcol_tooltip.text);
thumb = IMB_font_preview(full_path,
512 * UI_SCALE_FAC,
color,
TIP_("The five boxing wizards jump quickly! 0123456789"));
free_imbuf = true;
}
char date_str[FILELIST_DIRENTRY_DATE_LEN], time_str[FILELIST_DIRENTRY_TIME_LEN];
bool is_today, is_yesterday;
@@ -321,7 +331,20 @@ static void file_draw_tooltip_custom_func(bContext & /*C*/,
}
}
if (thumb && params->display != FILE_IMGDISPLAY) {
if (thumb && file->typeflag & FILE_TYPE_FTFONT) {
const float scale = (512.0f * UI_SCALE_FAC) / float(std::max(thumb->x, thumb->y));
uiTooltipImage image_data;
image_data.ibuf = thumb;
image_data.width = short(float(thumb->x) * scale);
image_data.height = short(float(thumb->y) * scale);
image_data.background = uiTooltipImageBackground::None;
image_data.premultiplied = false;
image_data.text_color = true;
image_data.border = false;
UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL);
UI_tooltip_image_field_add(tip, image_data);
}
else if (thumb && params->display != FILE_IMGDISPLAY) {
UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL);
UI_tooltip_text_field_add(tip, {}, {}, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL);

View File

@@ -93,7 +93,10 @@ ImBuf *IMB_thumb_load_blend(const char *blen_path, const char *blen_group, const
ImBuf *IMB_thumb_load_font(const char *filepath, unsigned int x, unsigned int y);
bool IMB_thumb_load_font_get_hash(char *r_hash);
ImBuf *IMB_font_preview(const char *filepath, unsigned int width, const float color[4]);
ImBuf *IMB_font_preview(const char *filepath,
unsigned int width,
const float color[4],
const char *sample_text = nullptr);
/* Threading */

View File

@@ -45,27 +45,30 @@ bool IMB_thumb_load_font_get_hash(char *r_hash)
return true;
}
ImBuf *IMB_font_preview(const char *filepath, uint width, const float color[4])
ImBuf *IMB_font_preview(const char *filepath,
uint width,
const float color[4],
const char *sample_text)
{
int font_id = (filepath[0] != '<') ? BLF_load(filepath) : 0;
if (font_id == -1) {
return nullptr;
}
const char sample[] = "ABCDEFGH\nabcdefg123";
const char default_sample[] = "ABCDabefg&0123";
const char *sample = sample_text ? sample_text : default_sample;
BLF_buffer_col(font_id, color);
BLF_size(font_id, 50.0f);
BLF_enable(font_id, BLF_WORD_WRAP);
float name_w;
float name_h;
BLF_width_and_height(font_id, sample, sizeof(sample), &name_w, &name_h);
float scale = float(width) / name_w;
BLF_width_and_height(font_id, sample, strlen(sample), &name_w, &name_h);
const float scale = float(width) / name_w * 0.98f;
BLF_size(font_id, scale * 50.0f);
name_w *= scale;
name_h *= scale;
int height = int(name_h * 1.3f);
const int height = int(name_h * 1.8f);
ImBuf *ibuf = IMB_allocImBuf(width, height, 32, IB_byte_data);
/* fill with white and zero alpha */
const float col[4] = {1.0f, 1.0f, 1.0f, 0.0f};
@@ -73,7 +76,7 @@ ImBuf *IMB_font_preview(const char *filepath, uint width, const float color[4])
BLF_buffer(font_id, ibuf->float_buffer.data, ibuf->byte_buffer.data, width, height, nullptr);
BLF_position(font_id, 0.0f, name_h * 0.8f, 0.0f);
BLF_position(font_id, 0.0f, height * 0.3f, 0.0f);
BLF_draw_buffer(font_id, sample, 1024);
BLF_buffer(font_id, nullptr, nullptr, 0, 0, nullptr);