Merge branch 'pypy-imagefont' of github.com:wiredfool/Pillow into pypy-imagefont

This commit is contained in:
wiredfool 2014-01-31 14:04:32 -08:00
commit e9029380dd

View File

@ -73,6 +73,7 @@ static FT_Library library;
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
FT_Face face; FT_Face face;
unsigned char *font_bytes;
} FontObject; } FontObject;
static PyTypeObject Font_Type; static PyTypeObject Font_Type;
@ -101,7 +102,7 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
/* create a font object from a file name and a size (in pixels) */ /* create a font object from a file name and a size (in pixels) */
FontObject* self; FontObject* self;
int error; int error = 0;
char* filename = NULL; char* filename = NULL;
int size; int size;
@ -120,11 +121,13 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
); );
return NULL; return NULL;
} }
if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|iss#", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|iss#", kwlist,
Py_FileSystemDefaultEncoding, &filename, Py_FileSystemDefaultEncoding, &filename,
&size, &index, &encoding, &font_bytes, &size, &index, &encoding, &font_bytes,
&font_bytes_size)) &font_bytes_size)) {
return NULL; return NULL;
}
self = PyObject_New(FontObject, &Font_Type); self = PyObject_New(FontObject, &Font_Type);
if (!self) { if (!self) {
@ -134,9 +137,20 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
} }
if (filename && font_bytes_size <= 0) { if (filename && font_bytes_size <= 0) {
self->font_bytes = NULL;
error = FT_New_Face(library, filename, index, &self->face); error = FT_New_Face(library, filename, index, &self->face);
} else { } else {
error = FT_New_Memory_Face(library, (FT_Byte*)font_bytes, font_bytes_size, index, &self->face); /* need to have allocated storage for font_bytes for the life of the object.*/
/* Don't free this before FT_Done_Face */
self->font_bytes = PyMem_Malloc(font_bytes_size);
if (!self->font_bytes) {
error = 65; // Out of Memory in Freetype.
}
if (!error) {
memcpy(self->font_bytes, font_bytes, (size_t)font_bytes_size);
error = FT_New_Memory_Face(library, (FT_Byte*)self->font_bytes,
font_bytes_size, index, &self->face);
}
} }
if (!error) if (!error)
@ -152,6 +166,9 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
PyMem_Free(filename); PyMem_Free(filename);
if (error) { if (error) {
if (self->font_bytes) {
PyMem_Free(self->font_bytes);
}
PyObject_Del(self); PyObject_Del(self);
return geterror(error); return geterror(error);
} }
@ -435,6 +452,9 @@ static void
font_dealloc(FontObject* self) font_dealloc(FontObject* self)
{ {
FT_Done_Face(self->face); FT_Done_Face(self->face);
if (self->font_bytes) {
PyMem_Free(self->font_bytes);
}
PyObject_Del(self); PyObject_Del(self);
} }