5

I want to be able to get the absolute path to a ttf font file on Linux when given a font name (if it exists). Is there a command or API that will return that information?

For example, given "arial.ttf" I want to find the absolute path (e.g. /usr/share/fonts/truetype/msttcorefonts/arial.ttf) wherever that may be.

Kazade
  • 1,297
  • 2
  • 15
  • 25
  • possible duplicate of [How to get a list of installed True Type Fonts on Linux using C or C++?](http://stackoverflow.com/questions/203257/how-to-get-a-list-of-installed-true-type-fonts-on-linux-using-c-or-c) – Martin York Dec 03 '10 at 16:17

2 Answers2

4

The easiest way is probably to use fontconfig with the --format option:

$ fc-match --format=%{file} LiberationSans-Regular.ttf

will result in the output

/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf

Instead of the font file name, you can also supply a font description that may be something like mono, DejaVu, :weight=bold, DejaVu-12, or DejaVu:weight=bold. So, for example,

$ fc-match --format=%{file} :weight=bold

results in

/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf

on my system. The only thing that does not seem to work is giving the complete path of the font file.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
0

Arguably, it's a really bad idea to try to access fonts by path, the linux fonts stack is not designed this way. There won't be any stability in locations and a lot of the "fonts" users expect in font lists do not map to a single font file but are composites of multiple font files.

If you want to be consistent with all the other desktop apps (and users expect the font choices to work the same way in all apps) you need to access fonts by patterns through the fontconfig indirection layer (using pango-cairo, or QT) and accept the result may be complex and not intuitive.

It does not matter that it seems less than intuitive to you, it only matters that font selections work the same way as in all other apps. And usually there are very good reasons for fontconfig mapping choices.

nim
  • 2,345
  • 14
  • 13