6

I have a font file /path/to/app/fonts/custom-font.ttf and I want to use it. How do you import a custom TTF for use in a GTK+3.0 app?

from gi.repository import Gtk, Pango

# ...

lbl = Gtk.Label()
lbl.modify_font(Pango.FontDescription("sans 48"))
# lbl.modify_font(Pango.FontDescription("custom-font 48"))
rkmax
  • 17,633
  • 23
  • 91
  • 176

2 Answers2

1

Pango searches FontConfig for fonts in Linux. For Windows, it queries the Windows API. For macOS, it does something similar as in Windows. So, something that will add the font file to search path of Pango before Pango getting a list of fonts and caching it will help. For Linux, I found this blog post which seem to work. For Windows, you should use AddFontResourceEx to add the font before starting GTK. I don't know about macOS.

As this question is tagged python, I have seen a library which does this job, called ManimPango, where doing something like below will work

import manimpango
manimpango.register_font(<path-to-font-file>)
naveen521kk
  • 140
  • 1
  • 10
-2

If you want to using it as a preview to your own app, install the font and uncomment the last line (provide the right name). If you want to use it as custom font for final release, i suggest you package with the font and make a script to install it. A little note, (in C), ".modify_font" deprecated since Gtk 3.0, use .override_font instead.

luciomrx
  • 25
  • 1