[cairo-commit] [cairo-www] src/freetypepython.mdwn

Carl Worth cworth at freedesktop.org
Tue Oct 23 17:06:16 PDT 2007


 src/freetypepython.mdwn |   99 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)

New commits:
commit e493918121aa1eca4ceee75b7e206a83f3c6b9dd
Author: Carl Worth <cworth at annarchy.freedesktop.org>
Date:   Tue Oct 23 17:06:16 2007 -0700

    web commit by BehdadEsfahbod

diff --git a/src/freetypepython.mdwn b/src/freetypepython.mdwn
new file mode 100644
index 0000000..5e35c27
--- /dev/null
+++ b/src/freetypepython.mdwn
@@ -0,0 +1,99 @@
+Back to [[cookbook]]
+
+The following snippet uses Python's ctypes module to load a font file using FreeType,
+and creating a cairo font face using the cairo-ft API that is not part of pycairo yet.
+The resulting cairo font face however is used normally with pycairo.
+    
+
+	import ctypes
+	import cairo
+
+
+	_initialized = False
+	def create_cairo_font_face_for_file (filename, faceindex):
+		global _initialized
+		global _freetype_so, _cairo_so
+		global _pycairo_api
+		global _ft_lib
+
+		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;
+
+			# initialize freetype
+			_ft_lib = ctypes.c_void_p()
+			assert 0 == _freetype_so.FT_Init_FreeType (ctypes.byref (_ft_lib))
+
+			_initialized = True
+
+		# create freetype face
+		ft_face = ctypes.c_void_p()
+		_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
+
+		# now wrap it properly using pycairo
+		face = _pycairo_api.FontFace_FromFontFace (cr_face)
+
+		return face
+
+	if __name__ == '__main__':
+
+		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)
+
+		ctx.set_font_face(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")
\ No newline at end of file


More information about the cairo-commit mailing list