(Copy of email sent to cairo list. It applies to FT too. Plus -- any suggestions about how to access FT using ctypes in Python would be good.)
Hello, I am completely out of my depth here really. I have been hacking together the attached code for days. Everything about C/C++ bewilders me, but I make notes and I have a manual! I'm on Kubuntu Dapper Drake. I have source-installed the latest cairo, pycairo, pygtk -- all seems good. Anyway, on to the issue: 1. I want to use pure python. 2. I want to use Cairo to draw strings of text to a surface. 3. I want to specify the file (or files) of what font I want it to use. Number 3 in the list is the rub. I see there is a "cairo_ft_font_face_create_for_ft_face" function in the c/c++ API, but nothing like it in pycairo. I have searched around and it seems to be a grey area of controversy for lofty reasons I will never grasp. So, like a fool, I thought, why not just extend Python with a module that will open font Blah with Freetype2, get the Cairo font whatsis from that and then pass it back up to Python! Yeah, right ... My basic idea was to be able to say: import cairo import Foo font = Foo.getfont("some/path/to/font.ttf") cr = cairo.Context(self.surface) cr.select_font_face(font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD) cr.set_font_face(font) cr.set_font_size(0.5) cr.etc( :) ) I want it to open Type1 fonts too, but that's another story, I don't yet know how Cairo handles those kinds of fonts. I have attached the Makefile (which I cobbled-together randomly over a long period of time) too. I hope someone can point me in the right direction. /d
#include <Python.h> #include <stdio.h> #include <cairo.h> #include <cairo-ft.h> // typedef struct { // PyObject_HEAD // cairo_font_face_t *cairoface; // } pFT_Face; // // PyObject *getface(PyObject *self, PyObject *args); // void killface (pFT_Face *self); // // PyTypeObject pFT_Face_Type = { // PyObject_HEAD_INIT(NULL) // 0, // "Face", // sizeof(pFT_Face), // 0, // (destructor) killface, /* tp_dealloc */ // 0, /* tp_print */ // 0, /* tp_getattr */ // 0, /* tp_setattr */ // 0, /* tp_compare */ // 0, /* tp_repr */ // 0, /* tp_as_number */ // 0, /* tp_as_sequence */ // 0, /* tp_as_mapping */ // 0, /* tp_hash */ // }; PyObject* getface(PyObject *self, PyObject *args){ FT_Library library; FT_Face face; int error; //pFT_Face *pFace; char *paf; if (!PyArg_ParseTuple(args, "s", &paf)) return NULL; // pFace = PyObject_New(pFT_Face, &pFT_Face_Type); // if (!pFace) { // return NULL; // } error = FT_Init_FreeType( &library ); if ( error ) { PyErr_SetString(PyExc_TypeError, "Error initializing FreeType library"); return NULL; } error = FT_New_Face( library,paf,0,&face ); if ( error == FT_Err_Unknown_File_Format ) { PyErr_SetString(PyExc_TypeError, "The font file could be opened and read, \n but it appears that its font format is unsupported\n"); return NULL; } else if ( error ) { PyErr_SetString(PyExc_TypeError, "Font file could not be opened or otherwise is broken.\n"); return NULL; } cairo_font_face_t *cairoface; cairoface = cairo_ft_font_face_create_for_ft_face(face,0); //Py_INCREF(pFace); /* FT_Done_Face ( face ); FT_Done_FreeType( library );*/ return (PyObject*) cairoface; //Py_BuildValue("s", paf) } PyMethodDef Foo_methods[] = { { "getface", getface, METH_VARARGS }, { NULL } }; void initFoo(void) { Py_InitModule("Foo", Foo_methods); }
CFLAGS=-I/usr/local/include/freetype2/ -I/usr/include/freetype2/ -I/usr/include/python2.4 -I/usr/local/include/cairo -Wall CPLUS_INCLUDE_PATH=/usr/include/freetype2 export CPLUS_INCLUDE_PATH Foomodule.so: foo.o gcc -shared -L/usr/local/lib -lfreetype -lcairo foo.o -o Foomodule.so foo.o: foo.c gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -c foo.c -o foo.o $(CFLAGS)
_______________________________________________ Freetype-devel mailing list Freetype-devel@nongnu.org http://lists.nongnu.org/mailman/listinfo/freetype-devel