From 631f9e6b8e09578e3717eb5e84e41a286b9d4cbd Mon Sep 17 00:00:00 2001 From: natinusala Date: Sun, 17 Feb 2019 16:54:24 +0100 Subject: [PATCH] font driver: expose line height --- gfx/drivers_font/ctr_font.c | 11 +++++++++++ gfx/drivers_font/d3d10_font.c | 11 +++++++++++ gfx/drivers_font/d3d11_font.c | 11 +++++++++++ gfx/drivers_font/d3d12_font.c | 11 +++++++++++ gfx/drivers_font/gl1_raster_font.c | 14 +++++++++++++- gfx/drivers_font/gl_raster_font.c | 13 ++++++++++++- gfx/drivers_font/switch_font.c | 10 ++++++++++ gfx/drivers_font/vita2d_font.c | 11 +++++++++++ gfx/drivers_font/vulkan_raster_font.c | 13 ++++++++++++- gfx/drivers_font/wiiu_font.c | 11 +++++++++++ gfx/font_driver.c | 14 ++++++++++++++ gfx/font_driver.h | 3 +++ 12 files changed, 130 insertions(+), 3 deletions(-) diff --git a/gfx/drivers_font/ctr_font.c b/gfx/drivers_font/ctr_font.c index 08af4e75d664..0443204ac511 100644 --- a/gfx/drivers_font/ctr_font.c +++ b/gfx/drivers_font/ctr_font.c @@ -446,6 +446,16 @@ static const struct font_glyph* ctr_font_get_glyph( return font->font_driver->get_glyph((void*)font->font_driver, code); } +static int ctr_font_get_line_height(void *data) +{ + ctr_font_t* font = (ctr_font_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t ctr_font = { ctr_font_init_font, @@ -456,4 +466,5 @@ font_renderer_t ctr_font = NULL, /* bind_block */ NULL, /* flush_block */ ctr_font_get_message_width, + ctr_font_get_line_height }; diff --git a/gfx/drivers_font/d3d10_font.c b/gfx/drivers_font/d3d10_font.c index e272a5f79391..363008c6c3ce 100644 --- a/gfx/drivers_font/d3d10_font.c +++ b/gfx/drivers_font/d3d10_font.c @@ -362,6 +362,16 @@ static const struct font_glyph* d3d10_font_get_glyph(void *data, uint32_t code) return font->font_driver->get_glyph((void*)font->font_driver, code); } +static int d3d10_font_get_line_height(void *data) +{ + d3d10_font_t* font = (d3d10_font_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t d3d10_font = { d3d10_font_init_font, d3d10_font_free_font, @@ -371,4 +381,5 @@ font_renderer_t d3d10_font = { NULL, /* bind_block */ NULL, /* flush */ d3d10_font_get_message_width, + d3d10_font_get_line_height }; diff --git a/gfx/drivers_font/d3d11_font.c b/gfx/drivers_font/d3d11_font.c index f72de3563a0d..5ef55b956f3e 100644 --- a/gfx/drivers_font/d3d11_font.c +++ b/gfx/drivers_font/d3d11_font.c @@ -359,6 +359,16 @@ static const struct font_glyph* d3d11_font_get_glyph(void *data, uint32_t code) return font->font_driver->get_glyph((void*)font->font_driver, code); } +static int d3d11_font_get_line_height(void *data) +{ + d3d11_font_t* font = (d3d11_font_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t d3d11_font = { d3d11_font_init_font, d3d11_font_free_font, @@ -368,4 +378,5 @@ font_renderer_t d3d11_font = { NULL, /* bind_block */ NULL, /* flush */ d3d11_font_get_message_width, + d3d11_font_get_line_height }; diff --git a/gfx/drivers_font/d3d12_font.c b/gfx/drivers_font/d3d12_font.c index 6812c3b043ee..c37b56b37491 100644 --- a/gfx/drivers_font/d3d12_font.c +++ b/gfx/drivers_font/d3d12_font.c @@ -376,6 +376,16 @@ static const struct font_glyph* d3d12_font_get_glyph( return font->font_driver->get_glyph((void*)font->font_driver, code); } +static int d3d12_font_get_line_height(void *data) +{ + d3d12_font_t* font = (d3d12_font_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t d3d12_font = { d3d12_font_init_font, d3d12_font_free_font, @@ -385,4 +395,5 @@ font_renderer_t d3d12_font = { NULL, /* bind_block */ NULL, /* flush */ d3d12_font_get_message_width, + d3d12_font_get_line_height }; diff --git a/gfx/drivers_font/gl1_raster_font.c b/gfx/drivers_font/gl1_raster_font.c index 5a4a4b4a679b..fcd14c8476dd 100644 --- a/gfx/drivers_font/gl1_raster_font.c +++ b/gfx/drivers_font/gl1_raster_font.c @@ -560,6 +560,17 @@ static void gl1_raster_font_bind_block(void *data, void *userdata) font->block = block; } + +static int gl1_get_line_height(void *data) +{ + gl1_raster_t *font = (gl1_raster_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t gl1_raster_font = { gl1_raster_font_init_font, gl1_raster_font_free_font, @@ -568,5 +579,6 @@ font_renderer_t gl1_raster_font = { gl1_raster_font_get_glyph, gl1_raster_font_bind_block, gl1_raster_font_flush_block, - gl1_get_message_width + gl1_get_message_width, + gl1_get_line_height }; diff --git a/gfx/drivers_font/gl_raster_font.c b/gfx/drivers_font/gl_raster_font.c index 70e9f5c86767..9246d7d68914 100644 --- a/gfx/drivers_font/gl_raster_font.c +++ b/gfx/drivers_font/gl_raster_font.c @@ -549,6 +549,16 @@ static void gl_raster_font_bind_block(void *data, void *userdata) font->block = block; } +static int gl_get_line_height(void *data) +{ + gl_raster_t *font = (gl_raster_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t gl_raster_font = { gl_raster_font_init_font, gl_raster_font_free_font, @@ -557,5 +567,6 @@ font_renderer_t gl_raster_font = { gl_raster_font_get_glyph, gl_raster_font_bind_block, gl_raster_font_flush_block, - gl_get_message_width + gl_get_message_width, + gl_get_line_height }; diff --git a/gfx/drivers_font/switch_font.c b/gfx/drivers_font/switch_font.c index d5f3a468974b..313f31d4d5f3 100644 --- a/gfx/drivers_font/switch_font.c +++ b/gfx/drivers_font/switch_font.c @@ -341,6 +341,15 @@ static void switch_font_bind_block(void *data, void *userdata) (void)data; } +static int switch_font_get_line_height(void *data) +{ + switch_font_t *font = (switch_font_t *)data; + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t switch_font = { switch_font_init_font, @@ -351,4 +360,5 @@ font_renderer_t switch_font = switch_font_bind_block, NULL, /* flush_block */ switch_font_get_message_width, + switch_font_get_line_height }; diff --git a/gfx/drivers_font/vita2d_font.c b/gfx/drivers_font/vita2d_font.c index 72c3d847f118..b333c6ff5177 100644 --- a/gfx/drivers_font/vita2d_font.c +++ b/gfx/drivers_font/vita2d_font.c @@ -347,6 +347,16 @@ static const struct font_glyph *vita2d_font_get_glyph( return font->font_driver->get_glyph((void*)font->font_driver, code); } +static int vita2d_font_get_line_height(void *data) +{ + vita_font_t *font = (vita_font_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t vita2d_vita_font = { vita2d_font_init_font, vita2d_font_free_font, @@ -356,4 +366,5 @@ font_renderer_t vita2d_vita_font = { NULL, /* bind_block */ NULL, /* flush */ vita2d_font_get_message_width, + vita2d_font_get_line_height }; diff --git a/gfx/drivers_font/vulkan_raster_font.c b/gfx/drivers_font/vulkan_raster_font.c index bfdfadbc4ca8..aae54e604ad5 100644 --- a/gfx/drivers_font/vulkan_raster_font.c +++ b/gfx/drivers_font/vulkan_raster_font.c @@ -435,6 +435,16 @@ static const struct font_glyph *vulkan_raster_font_get_glyph( return glyph; } +static int vulkan_get_line_height(void *data) +{ + vulkan_raster_t *font = (vulkan_raster_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t vulkan_raster_font = { vulkan_raster_font_init_font, vulkan_raster_font_free_font, @@ -443,5 +453,6 @@ font_renderer_t vulkan_raster_font = { vulkan_raster_font_get_glyph, NULL, /* bind_block */ NULL, /* flush_block */ - vulkan_get_message_width + vulkan_get_message_width, + vulkan_get_line_height }; diff --git a/gfx/drivers_font/wiiu_font.c b/gfx/drivers_font/wiiu_font.c index 74246e68cabc..dcc908a065d1 100644 --- a/gfx/drivers_font/wiiu_font.c +++ b/gfx/drivers_font/wiiu_font.c @@ -374,6 +374,16 @@ static const struct font_glyph* wiiu_font_get_glyph( return font->font_driver->get_glyph((void*)font->font_driver, code); } +static int wiiu_font_get_line_height(void *data) +{ + wiiu_font_t* font = (wiiu_font_t*)data; + + if (!font || !font->font_driver || !font->font_data) + return -1; + + return font->font_driver->get_line_height(font->font_data); +} + font_renderer_t wiiu_font = { wiiu_font_init_font, @@ -384,4 +394,5 @@ font_renderer_t wiiu_font = NULL, /* bind_block */ NULL, /* flush */ wiiu_font_get_message_width, + wiiu_font_get_line_height }; diff --git a/gfx/font_driver.c b/gfx/font_driver.c index 65677bbad5fc..9bf5abbcbfcf 100644 --- a/gfx/font_driver.c +++ b/gfx/font_driver.c @@ -1043,6 +1043,20 @@ int font_driver_get_message_width(void *font_data, return -1; } +int font_driver_get_line_height(void *font_data, float scale) +{ + font_data_t *font = (font_data_t*)(font_data ? font_data : video_font_driver); + int line_height; + + /* First try the line height implementation */ + if (font && font->renderer && font->renderer->get_line_height) + if ((line_height = font->renderer->get_line_height(font->renderer_data)) != -1) + return line_height * round(scale); + + /* Else return an approximation (width of 'a') */ + return font_driver_get_message_width(font_data, "a", 1, scale); +} + void font_driver_free(void *font_data) { font_data_t *font = (font_data_t*)font_data; diff --git a/gfx/font_driver.h b/gfx/font_driver.h index 7e808311a143..1f64ce20f1b0 100644 --- a/gfx/font_driver.h +++ b/gfx/font_driver.h @@ -96,6 +96,7 @@ typedef struct font_renderer video_frame_info_t *video_info); int (*get_message_width)(void *data, const char *msg, unsigned msg_len_full, float scale); + int (*get_line_height)(void* data); } font_renderer_t; typedef struct font_renderer_driver @@ -156,6 +157,8 @@ void font_driver_init_osd( enum font_driver_render_api api); void font_driver_free_osd(void); +int font_driver_get_line_height(void *font_data, float scale); + extern font_renderer_t gl_raster_font; extern font_renderer_t gl1_raster_font; extern font_renderer_t libdbg_font;