[cairo-commit] [cairo-www] src/freetypepython.mdwn
Carl Worth
cworth at freedesktop.org
Tue Oct 23 17:43:39 PDT 2007
src/freetypepython.mdwn | 66 ++++++++++++++++--------------------------------
1 file changed, 22 insertions(+), 44 deletions(-)
New commits:
commit 2b03c245665a3f119e6583f469785e907afeec03
Author: Carl Worth <cworth at annarchy.freedesktop.org>
Date: Tue Oct 23 17:43:39 2007 -0700
web commit by BehdadEsfahbod: Simplified version
diff --git a/src/freetypepython.mdwn b/src/freetypepython.mdwn
index 8333270..260d3a3 100644
--- a/src/freetypepython.mdwn
+++ b/src/freetypepython.mdwn
@@ -13,68 +13,47 @@ The resulting cairo font face however is used normally with pycairo.
_initialized = False
def create_cairo_font_face_for_file (filename, faceindex):
global _initialized
- global _freetype_so, _cairo_so
- global _pycairo_api
+ global _freetype_so
+ global _cairo_so
global _ft_lib
+ global _surface
+
+ STATUS_SUCCESS = 0
if not _initialized:
# find shared objects
- _freetype_so = ctypes.CDLL("libfreetype.so.6")
- _cairo_so = ctypes.CDLL("libcairo.so.2")
-
- # 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;
+ _freetype_so = ctypes.CDLL ("libfreetype.so.6")
+ _cairo_so = ctypes.CDLL ("libcairo.so.2")
# initialize freetype
- _ft_lib = ctypes.c_void_p()
+ _ft_lib = ctypes.c_void_p ()
assert 0 == _freetype_so.FT_Init_FreeType (ctypes.byref (_ft_lib))
+ class PycairoContext(ctypes.Structure):
+ _fields_ = [("PyObject_HEAD", ctypes.c_byte * object.__basicsize__),
+ ("ctx", ctypes.c_void_p),
+ ("base", ctypes.c_void_p)]
+
+ _surface = cairo.ImageSurface (cairo.FORMAT_A8, 0, 0)
+
_initialized = True
# create freetype face
ft_face = ctypes.c_void_p()
+ cairo_ctx = cairo.Context (_surface)
+ cairo_t = PycairoContext.from_address(id(cairo_ctx)).ctx
_cairo_so.cairo_ft_font_face_create_for_ft_face.restype = ctypes.c_void_p
assert 0 == _freetype_so.FT_New_Face (_ft_lib, filename, faceindex, ctypes.byref(ft_face))
# create cairo font face for freetype face
cr_face = _cairo_so.cairo_ft_font_face_create_for_ft_face (ft_face)
- assert 0 != cr_face
+ assert STATUS_SUCCESS == _cairo_so.cairo_font_face_status (cr_face)
+
+ _cairo_so.cairo_set_font_face (cairo_t, cr_face)
+ assert STATUS_SUCCESS == _cairo_so.cairo_status (cairo_t)
- # now wrap it properly using pycairo
- face = _pycairo_api.FontFace_FromFontFace (cr_face)
+ face = cairo_ctx.get_font_face ()
return face
@@ -82,7 +61,6 @@ The resulting cairo font face however is used normally with pycairo.
face = create_cairo_font_face_for_file ("/usr/share/fonts/dejavu-lgc/DejaVuLGCSerif.ttf", 0)
- # the rest is pretty easy
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 128, 128)
ctx = cairo.Context(surface)
More information about the cairo-commit
mailing list