Skip to content
Snippets Groups Projects
Commit 96506ca1 authored by Simon McVittie's avatar Simon McVittie
Browse files

dialog-ui: Try to load fonts provided by Steam


These contain glyphs for all languages supported by Steam.

steamrt/tasks#462

Signed-off-by: default avatarSimon McVittie <smcv@collabora.com>
parent e5fafc26
No related branches found
No related tags found
1 merge request!718dialog-ui: Try to load fonts provided by Steam
Pipeline #92464 passed
This commit is part of merge request !718. Comments created here will be created in the context of that merge request.
......@@ -506,11 +506,28 @@ dialog_new(const char *title)
title_font_size = (24.0 * self->h) / 480.0;
message_font_size = (18.0 * self->h) / 480.0;
self->title_font = ttf_load_font_family("sans-serif", "bold",
title_font_size);
self->title_font = ttf_load_steam_ui_font("GoNotoKurrent-Bold.ttf",
title_font_size);
if (self->title_font == NULL) {
debug("%s, falling back to sans-serif", SDL_GetError());
debug("%s, falling back to GoNotoKurrent-Regular with artificial bold", SDL_GetError());
self->title_font = ttf_load_steam_ui_font("GoNotoKurrent-Regular.ttf",
title_font_size);
if (self->title_font != NULL) {
TTF_SetFontStyle(self->title_font, TTF_STYLE_BOLD);
}
}
if (self->title_font == NULL) {
debug("%s, falling back to sans-serif bold", SDL_GetError());
self->title_font = ttf_load_font_family("sans-serif", "bold",
title_font_size);
}
if (self->title_font == NULL) {
debug("%s, falling back to sans-serif with artificial bold", SDL_GetError());
self->title_font = ttf_load_font_family("sans-serif", NULL,
title_font_size);
......@@ -524,8 +541,14 @@ dialog_new(const char *title)
return NULL;
}
self->message_font = ttf_load_font_family("sans-serif", NULL,
message_font_size);
self->message_font = ttf_load_steam_ui_font("GoNotoKurrent-Regular.ttf",
title_font_size);
if (self->message_font == NULL) {
debug("%s, falling back to sans-serif", SDL_GetError());
self->message_font = ttf_load_font_family("sans-serif", NULL,
message_font_size);
}
if (self->message_font == NULL) {
prefix_sdl_error ("Failed to load message font");
......
......@@ -16,6 +16,8 @@ void global_shutdown_ttf(void);
TTF_Font *ttf_load_font_family(const char *family,
const char *style,
int size);
TTF_Font *ttf_load_steam_ui_font(const char *basename,
int size);
static inline void clear_font(TTF_Font **p)
{
......
......@@ -25,6 +25,11 @@ void global_shutdown_sdl (void);
#define prefix_sdl_error(format, ...) \
SDL_SetError(format ": %s", ## __VA_ARGS__, SDL_GetError())
static inline void clear_sdl_free(char **p)
{
clear_pointer(p, SDL_free);
}
static inline void clear_renderer(SDL_Renderer **p)
{
clear_pointer(p, SDL_DestroyRenderer);
......
......@@ -6,7 +6,11 @@
#include "sdl-utils-internal.h"
#include "sdl-ttf-utils-internal.h"
#include <errno.h>
#include <pwd.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <dbus/dbus.h>
#include <fontconfig/fontconfig.h>
......@@ -37,6 +41,93 @@ static bool global_sdl_inited = false;
static bool global_ttf_inited = false;
static bool global_fontconfig_inited = false;
/*
* Like SDL_strdup, but sets error if it returns NULL.
*/
static inline char *sdl_strdup_or_set_error(const char *s)
{
char *ret;
if (s == NULL) {
SDL_InvalidParamError("s");
return NULL;
}
ret = SDL_strdup(s);
if (ret == NULL) {
SDL_OutOfMemory();
}
return ret;
}
/*
* Like g_get_home_dir(), but with SDL_malloc and SDL_SetError.
*/
static char *get_home_dir(char *buf,
size_t len)
{
const char *home = NULL;
struct passwd pwd;
struct passwd *result;
uid_t uid;
home = SDL_getenv("HOME");
if (home != NULL) {
return sdl_strdup_or_set_error(home);
}
uid = getuid();
if (getpwuid_r(uid, &pwd, buf, len, &result) != 0) {
SDL_SetError("Failed to look up uid %lu: %s", (unsigned long) uid, strerror(errno));
return NULL;
}
if (result == NULL) {
SDL_SetError("uid %lu not found in system user database", (unsigned long) uid);
return NULL;
}
if (result->pw_dir == NULL) {
SDL_SetError("uid %lu has no home directory", (unsigned long) uid);
}
return sdl_strdup_or_set_error(result->pw_dir);
}
TTF_Font *ttf_load_steam_ui_font(const char *basename,
int size)
{
__attribute__((cleanup(clear_font))) TTF_Font *font = NULL;
__attribute__((cleanup(clear_sdl_free))) char *home = NULL;
autofree char *filename = NULL;
/* Arbitrary size, probably enough for a line from /etc/passwd */
char buf[4096];
home = get_home_dir(buf, sizeof(buf));
if (home == NULL) {
return NULL;
}
if (asprintf(&filename, "%s/.steam/steam/clientui/fonts/%s", home, basename) < 0) {
SDL_SetError("%s", strerror(errno));
return NULL;
}
font = TTF_OpenFontIndex(filename, size, 0);
if (font == NULL) {
SDL_SetError ("Couldn't load font \"%s\" #0", filename);
return NULL;
}
return steal_pointer(&font);
}
/*
* Use fontconfig and SDL_ttf to load @family in style @style.
* (Is it really meant to be this complicated?)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment