"uifont" option: tell us what we wanted & what we got

The requested font & actual font loaded will be echo'd to STDOUT
when specifying the `uifont` option.

This uses Pango library directly (I was unable to convice SDL2_Pango.h
to actually notice that pango.h was #include'd before it, and expose
its SDLPango_GetPangoFontDescription() function!).  This is okay,
since we want to also add a "--uifont list" option (or similar),
that will list all of the available fonts, if a user wishes to
query them.  (And similar code will be added to tuxpaint-config to make
the setting available as a pull-down, rather than (or as well as) a
freeform type-in field.)
This commit is contained in:
Bill Kendrick 2023-06-01 00:20:41 -07:00
parent 64eeec1a98
commit 1b6ee72e87
5 changed files with 133 additions and 24 deletions

View file

@ -19,7 +19,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
(See COPYING.txt)
Last modified: April 30, 2023
Last modified: June 1, 2023
*/
#include <stdio.h>
@ -38,9 +38,9 @@
#endif
/*
The following section renames global variables defined in SDL_Pango.h to avoid errors during linking.
The following section renames global variables defined in SDL2_Pango.h to avoid errors during linking.
It is okay to rename these variables because they are constants.
SDL_Pang.h is included by fonts.h.
SDL2_Pango.h is included by fonts.h.
*/
#define _MATRIX_WHITE_BACK _MATRIX_WHITE_BACK2
#define MATRIX_WHITE_BACK MATRIX_WHITE_BACK2
@ -119,12 +119,22 @@ static const char *problemFontExtensions[] = {
SDL_Thread *font_thread;
#endif
#include "pango/pango.h"
#include "pango/pangoft2.h"
#if !defined(__PANGO_H__)
#error "---------------------------------------------------"
#error "If you installed pango from a package, be sure"
#error "to get the development package, as well!"
#error "(e.g., 'libpango1.0-dev.rpm')"
#error "---------------------------------------------------"
#endif
#include "SDL2_Pango.h"
#if !defined(SDL_PANGO_H)
#error "---------------------------------------------------"
#error "If you installed SDL_Pango from a package, be sure"
#error "If you installed SDL2_Pango from a package, be sure"
#error "to get the development package, as well!"
#error "(e.g., 'libsdl-pango1-dev.rpm')"
#error "(e.g., 'SDL2_Pango-2.1.5-dev.rpm')"
#error "---------------------------------------------------"
#endif
@ -1668,3 +1678,80 @@ void sdl_color_to_pango_color(SDL_Color sdl_color, SDLPango_Matrix * pango_color
memcpy(pango_color, pc, 16);
}
/**
* Given a font description, do the very bare minimum to
* have Pango library try to load it, then retrieve the description
* of the font that was _actually_ loaded.
* (e.g., "tuxpaint --uifont bookman" finds and uses "URW Bookman"
* on my system -bjk 2023.06.01)
*/
char * ask_pango_for_font(char * pangodesc)
{
PangoFontMap * font_map;
PangoContext * context;
PangoFontDescription * font_desc;
PangoFont *font;
PangoFontDescription * pfd;
char * descr;
font_map = pango_ft2_font_map_new();
if (font_map == NULL)
{
fprintf(stderr, "pango_ft2_font_map_new() failed\n");
fflush(stderr);
return NULL;
}
context = pango_font_map_create_context(font_map);
if (context == NULL)
{
fprintf(stderr, "pango_font_map_create_context() failed\n");
fflush(stderr);
g_object_unref(font_map);
return NULL;
}
font_desc = pango_font_description_from_string(pangodesc);
if (font_desc == NULL)
{
fprintf(stderr, "pango_font_description_from_string() failed\n");
fflush(stderr);
g_object_unref(font_map);
g_object_unref(context);
return NULL;
}
font = pango_font_map_load_font(font_map, context, font_desc);
if (font == NULL)
{
fprintf(stderr, "pango_font_map_load_font() failed\n");
fflush(stderr);
g_object_unref(font_map);
g_object_unref(context);
pango_font_description_free(font_desc);
return NULL;
}
pfd = pango_font_describe(font);
if (pfd == NULL)
{
fprintf(stderr, "pango_font_describe() failed\n");
fflush(stderr);
g_object_unref(font_map);
g_object_unref(context);
pango_font_description_free(font_desc);
g_object_unref(font);
return NULL;
}
descr = strdup(pango_font_description_get_family(pfd));
g_object_unref(font_map);
g_object_unref(context);
pango_font_description_free(font_desc);
g_object_unref(font);
/* N.B. Not free'ing `pfd`: The data is owned by the instance */
return descr;
}

View file

@ -19,7 +19,7 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
(See COPYING.txt)
Last updated: May 29, 2023
Last updated: May 31, 2023
*/
#ifndef FONTS_H
@ -164,5 +164,6 @@ void sdl_color_to_pango_color(SDL_Color sdl_color, SDLPango_Matrix * pango_color
int load_user_fonts(SDL_Surface * screen, SDL_Texture * texture,
SDL_Renderer * renderer, void *vp, const char *restrict const locale);
char * ask_pango_for_font(char * pangodesc);
#endif

View file

@ -27879,15 +27879,28 @@ static void setup_config(char *argv[])
if (tmpcfg.tp_ui_font)
{
char * tmp_str;
if (strcmp(tmpcfg.tp_ui_font, "default") == 0)
{
printf/*DEBUG_PRINTF*/("UI font will be default: %s\n", PANGO_DEFAULT_FONT);
printf/*DEBUG_PRINTF*/("Requested default UI font, \"%s\"\n", PANGO_DEFAULT_FONT);
tp_ui_font = strdup(PANGO_DEFAULT_FONT);
}
else
{
tp_ui_font = strdup(tmpcfg.tp_ui_font);
printf/*DEBUG_PRINTF*/("UI font will be: %s\n", tp_ui_font);
printf/*DEBUG_PRINTF*/("Requested UI font described by \"%s\"\n", tp_ui_font);
}
tmp_str = ask_pango_for_font(tp_ui_font);
if (tmp_str != NULL)
{
printf("Actual UI font will be \"%s\"\n", tmp_str);
free(tmp_str);
}
else
{
printf("Error asking pango for actual font!\n");
}
}
else