Skip to main content

Ralsina.Me — Roberto Alsina's website

Dear Lazyweb, what's the pythonic cross-platform fc-match?

Here's what fc-­match does:

$ fc-match "Droid Sans"
DroidSans.ttf: "Droid Sans" "Regular"

Or even:

$ fc-match "Droid Sans" -v | grep file:
      file: "/usr/share/fonts/TTF/DroidSans.ttf"

So, how does one do that, go­ing from a font fam­i­ly name or font name to a font file, where there's no font­con­fig?

I found code for this in mat­plotlib's font_­man­ag­er mod­ule but it looks hard to un­tan­gle, and re­quir­ing mat­plotlib is a bit over the top.

If there's no por­ta­ble so­lu­tion, I would be hap­py enough with three stand­alone so­lu­tions in­stead, and prom­ise to pub­lish an ab­strac­tion lay­er over them ;-)

So, dear win­dows and mac python­istas, any point­er­s?

Orestis Markou / 2009-09-30 08:29:

You need to use PyObjC and NSFont to get an NSFont object from a name, but a cursory search doesn't indicate a way to get a font file from an NSFont object...

Roberto Alsina / 2009-09-30 11:37:

That's on OSX, right?

Orestis Markou / 2009-09-30 12:14:

Yes, OSX. Why do you need access to the font files themselves? If you have drawing to do OSX has very nice font mechanisms, perhaps you can use these directly.

Roberto Alsina / 2009-09-30 13:20:

I need the file to embed in a PDF.

Orestis Markou / 2009-09-30 14:18:

The closest I can see is this:

Though this probably won't be accessible through PyObjC.

The reason there's no API is that an NSFont instance does not always map to a file (could be mapped to memory, could be in another application bundle) and so on.

Annoying, isn't it? Perhaps you can use Quartz (Core Graphics) to construct the PDF. Then things should work... (Note: I'm not a PDF expert).

Roberto Alsina / 2009-09-30 15:02:

Oh, well. I think I found a way for windows, mac users are probably not the target for this app anyway.

jjconti / 2009-09-30 15:11:

De la versión en inglés te quedó un "Or even:"

Roberto Alsina / 2009-09-30 15:37:

Gracias Juanjo, corregido!

Marius Gedminas / 2009-09-30 16:30:

pygame.font does this (goes from a font name to a filename). A few years back its Linux version was actually calling fc-match in a subprocess.

Anonymous / 2009-09-30 20:20:

In windows, I believe you have to scan the registry looking for the font file thats providing your face name.

Stani / 2009-10-01 14:08:

Which solution did you find for Windows? I'd like to know.

How about this solution which should work for Windows, Mac and Linux?

This was also suggested by Marius Gedminas, but taking you to the file makes it more easy.

Roberto Alsina / 2009-10-01 14:23:

@stani I get a 404.

I did check Marius solution, it's the best so far. I'll try to extract it from pygame in a day or two.