@@ -33,6 +33,8 @@ typedef struct
3333 Py_ssize_t suboffsets[2 ];
3434} PyFT2Image;
3535
36+ static PyTypeObject PyFT2ImageType;
37+
3638static PyObject *PyFT2Image_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
3739{
3840 PyFT2Image *self;
@@ -121,9 +123,7 @@ static int PyFT2Image_get_buffer(PyFT2Image *self, Py_buffer *buf, int flags)
121123 return 1 ;
122124}
123125
124- static PyTypeObject PyFT2ImageType;
125-
126- static PyTypeObject *PyFT2Image_init_type (PyObject *m, PyTypeObject *type)
126+ static PyTypeObject* PyFT2Image_init_type ()
127127{
128128 static PyMethodDef methods[] = {
129129 {" draw_rect" , (PyCFunction)PyFT2Image_draw_rect, METH_VARARGS, PyFT2Image_draw_rect__doc__},
@@ -132,28 +132,18 @@ static PyTypeObject *PyFT2Image_init_type(PyObject *m, PyTypeObject *type)
132132 };
133133
134134 static PyBufferProcs buffer_procs;
135- memset (&buffer_procs, 0 , sizeof (PyBufferProcs));
136135 buffer_procs.bf_getbuffer = (getbufferproc)PyFT2Image_get_buffer;
137136
138- memset (type, 0 , sizeof (PyTypeObject));
139- type->tp_name = " matplotlib.ft2font.FT2Image" ;
140- type->tp_basicsize = sizeof (PyFT2Image);
141- type->tp_dealloc = (destructor)PyFT2Image_dealloc;
142- type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
143- type->tp_methods = methods;
144- type->tp_new = PyFT2Image_new;
145- type->tp_init = (initproc)PyFT2Image_init;
146- type->tp_as_buffer = &buffer_procs;
147-
148- if (PyType_Ready (type) < 0 ) {
149- return NULL ;
150- }
137+ PyFT2ImageType.tp_name = " matplotlib.ft2font.FT2Image" ;
138+ PyFT2ImageType.tp_basicsize = sizeof (PyFT2Image);
139+ PyFT2ImageType.tp_dealloc = (destructor)PyFT2Image_dealloc;
140+ PyFT2ImageType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
141+ PyFT2ImageType.tp_methods = methods;
142+ PyFT2ImageType.tp_new = PyFT2Image_new;
143+ PyFT2ImageType.tp_init = (initproc)PyFT2Image_init;
144+ PyFT2ImageType.tp_as_buffer = &buffer_procs;
151145
152- if (PyModule_AddObject (m, " FT2Image" , (PyObject *)type)) {
153- return NULL ;
154- }
155-
156- return type;
146+ return &PyFT2ImageType;
157147}
158148
159149/* *********************************************************************
@@ -212,7 +202,7 @@ static PyObject *PyGlyph_get_bbox(PyGlyph *self, void *closure)
212202 " llll" , self->bbox .xMin , self->bbox .yMin , self->bbox .xMax , self->bbox .yMax );
213203}
214204
215- static PyTypeObject *PyGlyph_init_type (PyObject *m, PyTypeObject *type )
205+ static PyTypeObject *PyGlyph_init_type ()
216206{
217207 static PyMemberDef members[] = {
218208 {(char *)" width" , T_LONG, offsetof (PyGlyph, width), READONLY, (char *)" " },
@@ -232,22 +222,14 @@ static PyTypeObject *PyGlyph_init_type(PyObject *m, PyTypeObject *type)
232222 {NULL }
233223 };
234224
235- memset (type, 0 , sizeof (PyTypeObject));
236- type->tp_name = " matplotlib.ft2font.Glyph" ;
237- type->tp_basicsize = sizeof (PyGlyph);
238- type->tp_dealloc = (destructor)PyGlyph_dealloc;
239- type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
240- type->tp_members = members;
241- type->tp_getset = getset;
242-
243- if (PyType_Ready (type) < 0 ) {
244- return NULL ;
245- }
246-
247- /* Don't need to add to module, since you can't create glyphs
248- directly from Python */
225+ PyGlyphType.tp_name = " matplotlib.ft2font.Glyph" ;
226+ PyGlyphType.tp_basicsize = sizeof (PyGlyph);
227+ PyGlyphType.tp_dealloc = (destructor)PyGlyph_dealloc;
228+ PyGlyphType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
229+ PyGlyphType.tp_members = members;
230+ PyGlyphType.tp_getset = getset;
249231
250- return type ;
232+ return &PyGlyphType ;
251233}
252234
253235/* *********************************************************************
@@ -265,6 +247,8 @@ typedef struct
265247 Py_ssize_t suboffsets[2 ];
266248} PyFT2Font;
267249
250+ static PyTypeObject PyFT2FontType;
251+
268252static unsigned long read_from_file_callback (FT_Stream stream,
269253 unsigned long offset,
270254 unsigned char *buffer,
@@ -312,8 +296,6 @@ static void close_file_callback(FT_Stream stream)
312296 PyErr_Restore (type, value, traceback);
313297}
314298
315- static PyTypeObject PyFT2FontType;
316-
317299static PyObject *PyFT2Font_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
318300{
319301 PyFT2Font *self;
@@ -1449,7 +1431,7 @@ static int PyFT2Font_get_buffer(PyFT2Font *self, Py_buffer *buf, int flags)
14491431 return 1 ;
14501432}
14511433
1452- static PyTypeObject *PyFT2Font_init_type (PyObject *m, PyTypeObject *type )
1434+ static PyTypeObject *PyFT2Font_init_type ()
14531435{
14541436 static PyGetSetDef getset[] = {
14551437 {(char *)" postscript_name" , (getter)PyFT2Font_postscript_name, NULL , NULL , NULL },
@@ -1504,30 +1486,20 @@ static PyTypeObject *PyFT2Font_init_type(PyObject *m, PyTypeObject *type)
15041486 };
15051487
15061488 static PyBufferProcs buffer_procs;
1507- memset (&buffer_procs, 0 , sizeof (PyBufferProcs));
15081489 buffer_procs.bf_getbuffer = (getbufferproc)PyFT2Font_get_buffer;
15091490
1510- memset (type, 0 , sizeof (PyTypeObject));
1511- type->tp_name = " matplotlib.ft2font.FT2Font" ;
1512- type->tp_doc = PyFT2Font_init__doc__;
1513- type->tp_basicsize = sizeof (PyFT2Font);
1514- type->tp_dealloc = (destructor)PyFT2Font_dealloc;
1515- type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1516- type->tp_methods = methods;
1517- type->tp_getset = getset;
1518- type->tp_new = PyFT2Font_new;
1519- type->tp_init = (initproc)PyFT2Font_init;
1520- type->tp_as_buffer = &buffer_procs;
1521-
1522- if (PyType_Ready (type) < 0 ) {
1523- return NULL ;
1524- }
1525-
1526- if (PyModule_AddObject (m, " FT2Font" , (PyObject *)type)) {
1527- return NULL ;
1528- }
1491+ PyFT2FontType.tp_name = " matplotlib.ft2font.FT2Font" ;
1492+ PyFT2FontType.tp_doc = PyFT2Font_init__doc__;
1493+ PyFT2FontType.tp_basicsize = sizeof (PyFT2Font);
1494+ PyFT2FontType.tp_dealloc = (destructor)PyFT2Font_dealloc;
1495+ PyFT2FontType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1496+ PyFT2FontType.tp_methods = methods;
1497+ PyFT2FontType.tp_getset = getset;
1498+ PyFT2FontType.tp_new = PyFT2Font_new;
1499+ PyFT2FontType.tp_init = (initproc)PyFT2Font_init;
1500+ PyFT2FontType.tp_as_buffer = &buffer_procs;
15291501
1530- return type ;
1502+ return &PyFT2FontType ;
15311503}
15321504
15331505static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, " ft2font" };
@@ -1547,11 +1519,12 @@ PyMODINIT_FUNC PyInit_ft2font(void)
15471519 FT_Library_Version (_ft2Library, &major, &minor, &patch);
15481520 snprintf (version_string, sizeof (version_string), " %d.%d.%d" , major, minor, patch);
15491521
1550- PyObject *m = PyModule_Create (&moduledef);
1551- if (!m ||
1552- !PyFT2Image_init_type (m, &PyFT2ImageType) ||
1553- !PyGlyph_init_type (m, &PyGlyphType) ||
1554- !PyFT2Font_init_type (m, &PyFT2FontType) ||
1522+ PyObject *m;
1523+ if (!(m = PyModule_Create (&moduledef)) ||
1524+ prepare_and_add_type (PyFT2Image_init_type (), m) ||
1525+ prepare_and_add_type (PyFT2Font_init_type (), m) ||
1526+ // Glyph is not constructible from Python, thus not added to the module.
1527+ PyType_Ready (PyGlyph_init_type ()) ||
15551528 PyModule_AddStringConstant (m, " __freetype_version__" , version_string) ||
15561529 PyModule_AddStringConstant (m, " __freetype_build_type__" , STRINGIFY (FREETYPE_BUILD_TYPE)) ||
15571530 PyModule_AddIntConstant (m, " SCALABLE" , FT_FACE_FLAG_SCALABLE) ||
0 commit comments