[cairo] Freetype to Cairo - newb needs help

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Tue Oct 23 15:43:25 PDT 2007


Behdad Esfahbod wrote:
> On Tue, 2007-10-23 at 18:02 -0400, Bertram Felgenhauer wrote:
> > 
> > 
> > I'd resort to C at this point; pycairo.h provides the wrapper we need
> > in the Pycairo_CAPI interface. (Of course it's possible to wrap that
> > struct with  ctypes  and query python for it with
> > ctypes.pythonapi.PyCObject_Import("cairo", "CAPI"). But it feels
> > wrong.)
> 
> Humm, you can get the CAPI object using cairo._cairo.CAPI.  I just
> didn't know what to do with it without spending hours.

Hmm, but we need a call to pythonapi anyway, either to look up the
API or to extract it from the PyCObject object. The code below shows
what I meant in my previous mail.

HTH,

Bertram

########################################################################
import cairo
import ctypes

# find DLLs
cairo_dll    = ctypes.CDLL("libcairo.so")
freetype_dll = ctypes.CDLL("libfreetype.so")

# Pycairo's API representation (from pycairo.h)
class Pycairo_CAPI(ctypes.Structure):
   _fields_ = [
      ('Context_Type', ctypes.py_object),
      ('Context_FromContext', ctypes.c_void_p),
      ('FontFace_Type', ctypes.py_object),
      ('FontFace_FromFontFace', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
      ('FontOptions_Type', ctypes.py_object),
      ('FontOptions_FromFontOptions', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
      ('Matrix_Type', ctypes.py_object),
      ('Matrix_FromMatrix', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
      ('Path_Type', ctypes.py_object),
      ('Path_FromPath', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
      ('Pattern_Type', ctypes.py_object),
      ('SolidPattern_Type', ctypes.py_object),
      ('SurfacePattern_Type', ctypes.py_object),
      ('Gradient_Type', ctypes.py_object),
      ('LinearGradient_Type', ctypes.py_object),
      ('RadialGradient_Type', ctypes.py_object),
      ('Pattern_FromPattern', ctypes.c_void_p),
      ('ScaledFont_Type', ctypes.py_object),
      ('ScaledFont_FromScaledFont', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
      ('Surface_Type', ctypes.py_object),
      ('ImageSurface_Type', ctypes.py_object),
      ('PDFSurface_Type', ctypes.py_object),
      ('PSSurface_Type', ctypes.py_object),
      ('SVGSurface_Type', ctypes.py_object),
      ('Win32Surface_Type', ctypes.py_object),
      ('XlibSurface_Type', ctypes.py_object),
      ('Surface_FromSurface', ctypes.PYFUNCTYPE(ctypes.py_object, ctypes.c_void_p)),
      ('Check_Status', ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_int)]

# look up the API
ctypes.pythonapi.PyCObject_Import.restype = ctypes.POINTER(Pycairo_CAPI)
pycairo_api = ctypes.pythonapi.PyCObject_Import("cairo", "CAPI").contents;

# specify some types
cairo_dll.cairo_ft_font_face_create_for_ft_face.restype = ctypes.c_void_p

# initialise freetype
ft_lib = ctypes.c_void_p()
freetype_dll.FT_Init_FreeType(ctypes.byref(ft_lib))

# load font
ft_face = ctypes.c_void_p()
freetype_dll.FT_New_Face(ft_lib, "F.TTF", 0, ctypes.byref(ft_face))

# create cairo font face
cr_face_raw = cairo_dll.cairo_ft_font_face_create_for_ft_face(ft_face, 0)
cairo_dll.cairo_font_face_reference(cr_face_raw)

# now wrap it properly using pycairo
cr_face = pycairo_api.FontFace_FromFontFace(cr_face_raw)


More information about the cairo mailing list