[cairo] Freetype to Cairo - newb needs help
Behdad Esfahbod
behdad at behdad.org
Tue Oct 23 17:52:03 PDT 2007
On Tue, 2007-10-23 at 14:48 -0400, Bertram Felgenhauer wrote:
> 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.
Thanks. Got that working, and then did a simpler approach myself. Put
it here:
http://cairographics.org/freetypepython/
Though now I see that you uploaded your own version. Feel free to clean
up the wiki (raise instead of assert, etc).
There is a bug left though. When I run it, it randomly works, or draws
nothing, or draws in vertical mode. I'm fairly sure it's some kind of
refcounting issue. If I remove the function definition and "if
__name__" block and put everything straight, it works all the time. Any
idea?
behdad
> 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")
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo
--
behdad
http://behdad.org/
"Those who would give up Essential Liberty to purchase a little
Temporary Safety, deserve neither Liberty nor Safety."
-- Benjamin Franklin, 1759
More information about the cairo
mailing list