Re: Custom font for application



I sorted this out yesterday (see denemo-devel mailing list), what you need I suspect is along the lines of 
this:

GtkSettings *settings = gtk_settings_get_default( );
gtk_settings_set_string_property(settings, "gtk-font-name" , "FreeSerif 12", "denemo");
// Note: GTK complains that the font does not have a +ve size unless you mention, one as here.
gtk_main();

where "FreeSerif 12" indicates the font you want the application to use, and the last string can be anything.

you will also need to call something like:

#include <fontconfig/fontconfig.h>
void add_font_directory(gchar *fontpath) {
#ifdef G_OS_WIN32
  AddFontResource(fontpath);
#else
  FcConfigAppFontAddDir(NULL, fontpath);
#endif
}

if the font isn't a system one.
Creating a font = fontforge, of course.

Richard


On Mon, 2011-07-25 at 12:00 +0000, gtk-app-devel-list-request gnome org
wrote:

Does anyone here know how I can create and load a custom font for my
application from the source code? What method or methods do I need to
invoke to load the font that I can render? What rendering methods do I
need to override so that my custom fonts are the ones rendered? Are
there any examples of source code on how to achieve this? 

The GNU/Denemo music typesetter uses a custom font, called denemo.ttf
You can look at its Makefiles for installation hints and src/utils.c for
setting font load paths. It invokes this font by using gtk's markup
language thus:

/* markup the passed string to be in the denemo music font
* caller must free the returned string
*/
gchar * music_font(gchar *str) {
  return g_strdup_printf("<span font_desc=\"Denemo 12\">%s</span>",
str);
}

Where Denemo 12 is asking for denemo.ttf in 12pt.

I have all this at my fingertips because I want to change it so that I
don't have to switch fonts using markup, but instead use just one font
for both regular text and music symbols.
So I too need to know, how do you tell a gtk application to use a
specific font for all the widget labels and other text which it would
normally render in some default or use preferred font?

Richard Shann
http://www.denemo.org






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]