[cairo] Freetype to Cairo - newb needs help
Bertram Felgenhauer
bertram.felgenhauer at googlemail.com
Tue Oct 23 11:48:20 PDT 2007
Donn wrote:
> > Fixing those, it works:
> Ah, I see. A little dizzy, but glad it's getting further. Now the thing, as
> you mentioned, is casting the returned pointer, cairo_font_face_t, to a
> cairo.FontFace object ... I'm all out.
It's not exactly trivial. The code below works for me. I'm not sure how
brittle it is.
HTH,
Bertram
########################################################################
import cairo
import ctypes
# find DLLs
cairo_dll = ctypes.CDLL("libcairo.so")
freetype_dll = ctypes.CDLL("libfreetype.so")
# this makes pycairo's internals available.
pycairo_dll = ctypes.PyDLL(cairo._cairo.__file__)
# specify some types
cairo_dll.cairo_ft_font_face_create_for_ft_face.restype = ctypes.c_void_p
pycairo_dll.PycairoFontFace_FromFontFace.restype = ctypes.py_object
# 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_dll.PycairoFontFace_FromFontFace(cr_face_raw)
# the rest is pretty easy
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 128, 128)
ctx = cairo.Context(surface)
ctx.set_font_face(cr_face)
ctx.set_font_size(30)
ctx.move_to(0, 44)
ctx.show_text("Hello,")
ctx.move_to(30, 74)
ctx.show_text("world!")
del ctx
surface.write_to_png("hello.png")
More information about the cairo
mailing list