Home >> Blog >> Changing the GTK 1 font

Saturday, 15 November 2008 19:15

Changing the GTK 1 font

Written by  Nicholas K. Dionysopoulos
Rate this item
(0 votes)

Since the advent of GTK2 I hadn't really bothered with this old, seemingly outdated, beast called GTK 1. However, there are some useful applications which are linked against it and I'd like to use them. Most prominently, it's the Lazarus IDE (the GTK2 interface is buggy and the Qt interface requires tons of hacks to work). The most proinent problem I just couldn't stand is that the default font used in GTK1 apps looks ugly, so ugly it hurts my eyes and renders GTK1 applications unusuable. Fortunately, changing the default font is almost easy; you do have to edit some configuration files.

These instructions have been tested with Mandriva Linux 2009.0, but similar - if not identical - steps should be performed on other Linux distributions as well.

GTK1 stores its master configuration files in /etc/gtk. There are dozens of files in there. To find the correct one, you must know your locale settings. To do so, open a console and issue the locale command. The output on my system looks like this:

LANG=el_GR.UTF-8
LC_CTYPE=el_GR.UTF-8
LC_NUMERIC=el_GR.UTF-8
LC_TIME=el_GR.UTF-8
LC_COLLATE=el_GR.UTF-8
LC_MONETARY=el_GR.UTF-8
LC_MESSAGES=el_GR.UTF-8
LC_PAPER=el_GR.UTF-8
LC_NAME=el_GR.UTF-8
LC_ADDRESS=el_GR.UTF-8
LC_TELEPHONE=el_GR.UTF-8
LC_MEASUREMENT=el_GR.UTF-8
LC_IDENTIFICATION=el_GR.UTF-8
LC_ALL=

Find the LC_MESSAGES line. The part after the equals sign is your locale. On my system it is el_GR.UTF-8 as you can see. Now, go to the /etc/gtk directory and edit the file named gtkrc.yourlocale where yourlocale is the locale you found above! On my system this would be gtkrc.el_GR.UTF-8.

This file's contents look like this:

 style "gtk-default" {
       fontset = "
        -*-kerkis-medium-r-normal--12-*-*-*-p-*-iso10646-1,\
        -*-clearlyu-medium-r-normal--17-*-*-*-p-*-iso10646-1,\
        -*-r-*-iso10646-1,*"
}
class "GtkWidget" style "gtk-default"

As you can see, the font definitions are in an obscure format which was used many, many years ago. Finding the correct font definition for your system looks hard, but no, it's just tricky.

Your system stores fonts definitions inside the fonts.dir files which reside in your fonts' directories. On Mandriva Linux 2009.0 I found that the great-looking Liberation family's font files reside in /usr/share/fonts/TTF/liberation. So, all I had to do was to open the fonts.dir file from that directory!

The next step is locating the correct font definition. If your locale is using the UTF-8 encoding (the most common these days), you have to find a font definition for the iso10646-1 encoding. The reason is simple: ISO10646-1 is the Unicode encoding! Browsing through the file, I was able to locate the definition line for Liberation Sans, regular. It looked something like:

LiberationSans-Regular.ttf -misc-Liberation Sans-medium-r-normal--0-0-0-0-p-0-iso10646-1

The part after the font file name (in this case -misc-Liberation Sans-medium-r-normal--0-0-0-0-p-0-iso10646-1) is what you need!

Go back to the GTK file you were editing. Right below the fontset=" line, paste the font definition you just found and append the ,\ line continuation sequence. If you fail to do this last part, you're screwed. So, the file would now look something like this:

 style "gtk-default" {
       fontset = "
        misc-Liberation Sans-medium-r-normal--0-0-0-0-p-0-iso10646-1,\
        -*-kerkis-medium-r-normal--12-*-*-*-p-*-iso10646-1,\
        -*-clearlyu-medium-r-normal--17-*-*-*-p-*-iso10646-1,\
        -*-r-*-iso10646-1,*"
}
class "GtkWidget" style "gtk-default"

Still, this is not 100% correct because we haven't defined a font size and we are forcing other properties (bold, italics) to be cleared as well. In order to fix this, we have to change the 0-0-0-0-p-0 part of the font definition to 12-*-*-*-p-* (you can use different values than 12 to suit your taste, it's the font's height in pixels so just toy around). The file would now look something like this:

 style "gtk-default" {
       fontset = "
        misc-Liberation Sans-medium-r-normal--12-*-*-*-p-*-iso10646-1,\
        -*-kerkis-medium-r-normal--12-*-*-*-p-*-iso10646-1,\
        -*-clearlyu-medium-r-normal--17-*-*-*-p-*-iso10646-1,\
        -*-r-*-iso10646-1,*"
}
class "GtkWidget" style "gtk-default"

You are done! Save the file and (re)start your GTK1-based apps!

Last modified on Saturday, 03 April 2010 12:11

6 comments

  • Comment Link Nicholas K. Dionysopoulos Saturday, 25 June 2011 15:02 posted by Nicholas K. Dionysopoulos

    Well, it is off-topic and I almost marked your comment as spam :D The hosts that I recommend are:
    1. Rochen (they are very good and they offer me free hosting for AkeebaBackup.com)
    2. CloudAccess.net (I have worked with them and they have blazing fast, very secure servers)
    3. iRedHOST (Joomla!-optimized host, I have many trustworthy people vouch for them)
    4. RackSpace CloudSites (very fast!)

  • Comment Link Miraclesuit Friday, 24 June 2011 07:14 posted by Miraclesuit

    hey there, this might be little offtopic, but i am hosting my site on hostgator and they will suspend my hosting in 4days, so i would like to ask you which hosting do you use or recommend?

  • Comment Link Paul Nichols Sunday, 05 June 2011 21:00 posted by Paul Nichols

    I followed the steps 3 times but I can't get this to work. Guess I'll have to do some more reading in this. Thanks anyway for the expanation.

  • Comment Link Kitesurf Saturday, 04 June 2011 18:57 posted by Kitesurf

    Great post. I'm subscribing now for future reads.

  • Comment Link Kitesurf Saturday, 04 June 2011 18:56 posted by Kitesurf

    You make some good points. I like your point of view.

  • Comment Link  j2me porting Friday, 03 June 2011 08:42 posted by j2me porting

    Why is it not working with the browser/web server?
    How do I know which GTK Classes are supported?
    Can I use Themes under Win32?
    How do I port from one GTK+ version to another?
    How do I use non-ASCII characters in GTK+ programs ?

Leave a comment

Make sure you enter the (*) required information where indicated.
Basic HTML code is allowed.
All comments are auto-published. Unless you are a spammer, violate any laws or ask me to unpublish your comment, I won't unpublish any comments. Feel free to say anything you like.