[cairo] Loading font from a file in pycairo

Michael Droettboom mdroe at stsci.edu
Mon Aug 13 04:58:02 PDT 2007


Steven Chaplin wrote:
> On Fri, 2007-08-10 at 14:36 -0400, Michael Droettboom wrote:
>> I'm making some improvements to the Cairo backend in matplotlib and I 
>> have question about font loading.
>>
>> matplotlib has its own font-finding infrastructure, and most of the 
>> rendering backends load fonts by a filename returned from that system. 
> The Matplotlib font_manager has reinvented the wheel - fontconfig could
> have been used instead (by writing Python bindings) giving a font
> interface which is consistent to that used by many other applications
> (and probably solving your current problem).

I had the same thought myself, but adding a new dependency (remember, 
this is cross-platform) is something to consider carefully.

>> Is there any way to load a font by its filename in pycairo?
>>
>> It looks like it is possible in C to load a freetype font with 
>> FT_Open_Face and then use cairo_ft_font_face_create_for_ft_face.  Is 
>> freetype always assumed to be on all platforms, or would one have to use 
>> cairo_win32_font_face_create_for_hfont, for example, on Windows?
>>
>> Are there any thoughts about exposing this in pycairo?  Is this just a 
>> bad idea for reasons that I might be missing?
 >
> For pycairo to expose the cairo/FreeType functions there would need to be
> a Python binding for FreeType which provides a C API which pycairo could
> use (FreeType bindings do not belong in pycairo, the same way that the
> FreeType C library does not belong in cairo).

I agree with you.  What I had in mind was a single pycairo call that 
would handle this internally -- and do the correct thing depending on 
the particular font backend that cairo was built with.  While I 
understand the advantages of a full and proper font-lookup system 
(fontconfig), not being able to bypass it and load by filename seems 
like a hole in the API, IMHO.

> There have been a couple
> of FreeType python bindings written in the past but I don't think they
> are still maintained.

Matplotlib has its own freetype binding, though it is designed fairly 
specifically around the needs of matplotlib.


Thanks,
Mike


More information about the cairo mailing list