@@ -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/* *********************************************************************
@@ -217,7 +207,7 @@ static PyObject *PyGlyph_get_bbox(PyGlyph *self, void *closure)
217207 " llll" , self->bbox .xMin , self->bbox .yMin , self->bbox .xMax , self->bbox .yMax );
218208}
219209
220- static PyTypeObject *PyGlyph_init_type (PyObject *m, PyTypeObject *type )
210+ static PyTypeObject *PyGlyph_init_type ()
221211{
222212 static PyMemberDef members[] = {
223213 {(char *)" width" , T_LONG, offsetof (PyGlyph, width), READONLY, (char *)" " },
@@ -237,22 +227,14 @@ static PyTypeObject *PyGlyph_init_type(PyObject *m, PyTypeObject *type)
237227 {NULL }
238228 };
239229
240- memset (type, 0 , sizeof (PyTypeObject));
241- type->tp_name = " matplotlib.ft2font.Glyph" ;
242- type->tp_basicsize = sizeof (PyGlyph);
243- type->tp_dealloc = (destructor)PyGlyph_dealloc;
244- type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
245- type->tp_members = members;
246- type->tp_getset = getset;
247-
248- if (PyType_Ready (type) < 0 ) {
249- return NULL ;
250- }
251-
252- /* Don't need to add to module, since you can't create glyphs
253- directly from Python */
230+ PyGlyphType.tp_name = " matplotlib.ft2font.Glyph" ;
231+ PyGlyphType.tp_basicsize = sizeof (PyGlyph);
232+ PyGlyphType.tp_dealloc = (destructor)PyGlyph_dealloc;
233+ PyGlyphType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
234+ PyGlyphType.tp_members = members;
235+ PyGlyphType.tp_getset = getset;
254236
255- return type ;
237+ return &PyGlyphType ;
256238}
257239
258240/* *********************************************************************
@@ -270,6 +252,8 @@ typedef struct
270252 Py_ssize_t suboffsets[2 ];
271253} PyFT2Font;
272254
255+ static PyTypeObject PyFT2FontType;
256+
273257static unsigned long read_from_file_callback (FT_Stream stream,
274258 unsigned long offset,
275259 unsigned char *buffer,
@@ -317,8 +301,6 @@ static void close_file_callback(FT_Stream stream)
317301 PyErr_Restore (type, value, traceback);
318302}
319303
320- static PyTypeObject PyFT2FontType;
321-
322304static PyObject *PyFT2Font_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
323305{
324306 PyFT2Font *self;
@@ -1448,7 +1430,7 @@ static int PyFT2Font_get_buffer(PyFT2Font *self, Py_buffer *buf, int flags)
14481430 return 1 ;
14491431}
14501432
1451- static PyTypeObject *PyFT2Font_init_type (PyObject *m, PyTypeObject *type )
1433+ static PyTypeObject *PyFT2Font_init_type ()
14521434{
14531435 static PyGetSetDef getset[] = {
14541436 {(char *)" postscript_name" , (getter)PyFT2Font_postscript_name, NULL , NULL , NULL },
@@ -1503,30 +1485,20 @@ static PyTypeObject *PyFT2Font_init_type(PyObject *m, PyTypeObject *type)
15031485 };
15041486
15051487 static PyBufferProcs buffer_procs;
1506- memset (&buffer_procs, 0 , sizeof (PyBufferProcs));
15071488 buffer_procs.bf_getbuffer = (getbufferproc)PyFT2Font_get_buffer;
15081489
1509- memset (type, 0 , sizeof (PyTypeObject));
1510- type->tp_name = " matplotlib.ft2font.FT2Font" ;
1511- type->tp_doc = PyFT2Font_init__doc__;
1512- type->tp_basicsize = sizeof (PyFT2Font);
1513- type->tp_dealloc = (destructor)PyFT2Font_dealloc;
1514- type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1515- type->tp_methods = methods;
1516- type->tp_getset = getset;
1517- type->tp_new = PyFT2Font_new;
1518- type->tp_init = (initproc)PyFT2Font_init;
1519- type->tp_as_buffer = &buffer_procs;
1520-
1521- if (PyType_Ready (type) < 0 ) {
1522- return NULL ;
1523- }
1524-
1525- if (PyModule_AddObject (m, " FT2Font" , (PyObject *)type)) {
1526- return NULL ;
1527- }
1490+ PyFT2FontType.tp_name = " matplotlib.ft2font.FT2Font" ;
1491+ PyFT2FontType.tp_doc = PyFT2Font_init__doc__;
1492+ PyFT2FontType.tp_basicsize = sizeof (PyFT2Font);
1493+ PyFT2FontType.tp_dealloc = (destructor)PyFT2Font_dealloc;
1494+ PyFT2FontType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1495+ PyFT2FontType.tp_methods = methods;
1496+ PyFT2FontType.tp_getset = getset;
1497+ PyFT2FontType.tp_new = PyFT2Font_new;
1498+ PyFT2FontType.tp_init = (initproc)PyFT2Font_init;
1499+ PyFT2FontType.tp_as_buffer = &buffer_procs;
15281500
1529- return type ;
1501+ return &PyFT2FontType ;
15301502}
15311503
15321504static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, " ft2font" };
@@ -1546,11 +1518,12 @@ PyMODINIT_FUNC PyInit_ft2font(void)
15461518 FT_Library_Version (_ft2Library, &major, &minor, &patch);
15471519 snprintf (version_string, sizeof (version_string), " %d.%d.%d" , major, minor, patch);
15481520
1549- PyObject *m = PyModule_Create (&moduledef);
1550- if (!m ||
1551- !PyFT2Image_init_type (m, &PyFT2ImageType) ||
1552- !PyGlyph_init_type (m, &PyGlyphType) ||
1553- !PyFT2Font_init_type (m, &PyFT2FontType) ||
1521+ PyObject *m;
1522+ if (!(m = PyModule_Create (&moduledef)) ||
1523+ prepare_and_add_type (PyFT2Image_init_type (), m) ||
1524+ prepare_and_add_type (PyFT2Font_init_type (), m) ||
1525+ // Glyph is not constructible from Python, thus not added to the module.
1526+ PyType_Ready (PyGlyph_init_type ()) ||
15541527 PyModule_AddStringConstant (m, " __freetype_version__" , version_string) ||
15551528 PyModule_AddStringConstant (m, " __freetype_build_type__" , STRINGIFY (FREETYPE_BUILD_TYPE)) ||
15561529 PyModule_AddIntConstant (m, " SCALABLE" , FT_FACE_FLAG_SCALABLE) ||
0 commit comments