Dimitar Panayotov shares experience

Everything which eases programmer’s life and satisfies the clients

GNU Emacs: Use TTF font even if it is looks like not installed

Posted by Dimitar Panayotov on 30 November 2006, 2:48

Since I switched to KDE 3.5.5 (on Debian 4.0, othewise known as “Etch”), I have been trying to use DejaVu Sans Mono font in Emacs — it is a TTF font, it is beautiful, and supports many more encodings than everyday monospace fonts around.

Just for your information, If you want to install those fonts on Debian, do this:

# login as root before executing this command.
apt-get install ttf-dejavu

If you downloaded your fonts other way, you should install them in subdirectory into /usr/share/fonts/truetype (at least in KDE 3.5.5), like this:


So, for the example how to make Emacs to use TTF font which you cannot see in set-default-font possible choices, I will use ttf-dejavu font name.

# login as root before executing those commands.
cd /usr/share/fonts/truetype/ttf-dejavu
fc-cache -fv

Now you should edit your Xorg configuration file (/etc/X11/xorg.conf) like this:

Section "Files"
  FontPath  "/usr/share/fonts/truetype/ttf-dejavu"

Section "Module"
  Load  "freetype"

Now restart the X server (in this case Xorg). I do it this way:

# go to the text console -- for example with CTRL+ALT+F1
# login as root
/etc/init.d/kdm stop
/etc/init.d/kdm start

Now you should look as to how will your desired font name look like. Open the file "/usr/share/fonts/truetype/ttf-dejavu/fonts.dir“. It should look like that:

425 # some count

DejaVuSans-Bold.ttf -misc-dejavu sans-bold-r-normal--0-0-0-0-p-0-adobe-standard
DejaVuSans-Bold.ttf -misc-dejavu sans-bold-r-normal--0-0-0-0-p-0-adobe-symbol
DejaVuSans-Bold.ttf -misc-dejavu sans-bold-r-normal--0-0-0-0-p-0-ascii-0
DejaVuSans-Bold.ttf -misc-dejavu sans-bold-r-normal--0-0-0-0-p-0-iso10646-1
DejaVuSans-Bold.ttf -misc-dejavu sans-bold-r-normal--0-0-0-0-p-0-iso8859-10
DejaVuSans-Bold.ttf -misc-dejavu sans-bold-r-normal--0-0-0-0-p-0-iso8859-13
DejaVuSans-Bold.ttf -misc-dejavu sans-bold-r-normal--0-0-0-0-p-0-iso8859-15

Pick one from there, for example I picked this:

-misc-dejavu sans mono-medium-r-normal--0-0-0-0-m-0-iso10646-1

There are two ways to proceed ahead: “dialogous” and “copy-paste in your .emacs file”.

Dialogous way:

In Emacs do this:

M-x set-default-font RET
[copy and paste your selected font PostScript name here] RET

(Clarification: press ALT+X, then type “set-default-font”, then press Enter/Return key. Then paste the PostScript name of the font, copied earlier, then press Enter/Return key. Same rules apply for all Emacs instructions.)

If you choose this way (the dialogous), you are in trouble — now the font is too big. But fear not, do this:

M-x customize-face RET
default RET

Go to “height” field. Now change the value and press “Set for Current Session” after every change; do it as many times as necessary to pick up a good font size for your taste. Afterward, if you press “Save for Future Sessions”, changes in font face and font size will be written to your .emacs file and thus kept for future use.

Copy-paste in your .emacs file:

Copy and paste this (or your font PostScript name choice):

'(default ((t (:stipple nil :background "#ffffff" :foreground "#000000" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 80 :width normal :family "misc-dejavu sans mono")))))

Of course, the second approach requires either restarting of Emacs or Lisp-evaluating the line in place (with C-x C-e).

And that’s it. I have still not figured out a way to make Emacs show the names of all TTF fonts in set-default-font command’s possible choices. But since the scenarios described here are working, it is not a big concern. Yes, I would like to know, since the choosing from a visible list seems more clean and right for me; it means that you have done all the things properly and thus Emacs sees your fonts. If somebody knows how to do this on Emacs 21.4a, please tell me.

EDIT (2007-Mar-06; my birthday, by the way):

On Emacs 22.0.95 (got from CVS) invoking set-default-font and pressing TAB/Space to show possible choices, the fonts in the new directory actually appear. So, it seems that Emacs is doing what it should: get the available fonts from the underlying desktop/windowing system.


3 Responses to “GNU Emacs: Use TTF font even if it is looks like not installed”

  1. Дидо said

    Hi, 10x, it worked just as you had described it.
    I have a very beautifully looking emacs, thanks to you.
    Its readability is now amazing.

  2. vmuriel said

    Really really useful, thanks man.

  3. thanks !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: