35
35
#include <freetype/freetype.h>
38
#if PY_VERSION_HEX < 0x01060000
39
#define PyObject_New PyObject_NEW
40
#define PyObject_Del PyMem_DEL
43
#if PY_VERSION_HEX >= 0x01060000
44
#if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE)
45
/* defining this enables unicode support (default under 1.6a1 and later) */
50
#if !defined(Py_RETURN_NONE)
51
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
38
#define KEEP_PY_UNICODE
54
41
#if !defined(FT_LOAD_TARGET_MONO)
55
42
#define FT_LOAD_TARGET_MONO FT_LOAD_MONOCHROME
118
105
"filename", "size", "index", "encoding", NULL
121
#if defined(HAVE_UNICODE) && PY_VERSION_HEX >= 0x02020000
122
108
if (!PyArg_ParseTupleAndKeywords(args, kw, "eti|is", kwlist,
123
109
Py_FileSystemDefaultEncoding, &filename,
124
110
&size, &index, &encoding))
127
if (!PyArg_ParseTupleAndKeywords(args, kw, "si|is", kwlist,
128
&filename, &size, &index, &encoding))
424
font_getattr(FontObject* self, char* name)
428
res = Py_FindMethod(font_methods, (PyObject*) self, name);
436
if (!strcmp(name, "family")) {
437
if (self->face->family_name)
438
return PyString_FromString(self->face->family_name);
441
if (!strcmp(name, "style")) {
442
if (self->face->style_name)
443
return PyString_FromString(self->face->style_name);
446
if (!strcmp(name, "ascent"))
447
return PyInt_FromLong(PIXEL(self->face->size->metrics.ascender));
448
if (!strcmp(name, "descent"))
449
return PyInt_FromLong(-PIXEL(self->face->size->metrics.descender));
451
if (!strcmp(name, "glyphs"))
452
/* number of glyphs provided by this font */
453
return PyInt_FromLong(self->face->num_glyphs);
455
PyErr_SetString(PyExc_AttributeError, name);
459
statichere PyTypeObject Font_Type = {
460
PyObject_HEAD_INIT(NULL)
461
0, "Font", sizeof(FontObject), 0,
407
font_getattr_family(FontObject* self, void* closure)
409
#if PY_VERSION_HEX >= 0x03000000
410
if (self->face->family_name)
411
return PyUnicode_FromString(self->face->family_name);
413
if (self->face->family_name)
414
return PyString_FromString(self->face->family_name);
420
font_getattr_style(FontObject* self, void* closure)
422
#if PY_VERSION_HEX >= 0x03000000
423
if (self->face->style_name)
424
return PyUnicode_FromString(self->face->style_name);
426
if (self->face->style_name)
427
return PyString_FromString(self->face->style_name);
433
font_getattr_ascent(FontObject* self, void* closure)
435
return PyInt_FromLong(PIXEL(self->face->size->metrics.ascender));
439
font_getattr_descent(FontObject* self, void* closure)
441
return PyInt_FromLong(-PIXEL(self->face->size->metrics.descender));
445
font_getattr_glyphs(FontObject* self, void* closure)
447
return PyInt_FromLong(self->face->num_glyphs);
450
static struct PyGetSetDef font_getsetters[] = {
451
{ "family", (getter) font_getattr_family },
452
{ "style", (getter) font_getattr_style },
453
{ "ascent", (getter) font_getattr_ascent },
454
{ "descent", (getter) font_getattr_descent },
455
{ "glyphs", (getter) font_getattr_glyphs },
459
static PyTypeObject Font_Type = {
460
PyVarObject_HEAD_INIT(NULL, 0)
461
"Font", sizeof(FontObject), 0,
463
463
(destructor)font_dealloc, /* tp_dealloc */
464
464
0, /* tp_print */
465
(getattrfunc)font_getattr, /* tp_getattr */
470
0, /*tp_as_sequence */
471
0, /*tp_as_mapping */
478
Py_TPFLAGS_DEFAULT, /*tp_flags*/
482
0, /*tp_richcompare*/
483
0, /*tp_weaklistoffset*/
486
font_methods, /*tp_methods*/
488
font_getsetters, /*tp_getset*/
468
491
static PyMethodDef _functions[] = {
497
setup_module(PyObject* m) {
479
500
int major, minor, patch;
481
/* Patch object type */
482
Font_Type.ob_type = &PyType_Type;
484
m = Py_InitModule("_imagingft", _functions);
485
502
d = PyModule_GetDict(m);
504
/* Ready object type */
505
PyType_Ready(&Font_Type);
487
507
if (FT_Init_FreeType(&library))
488
return; /* leave it uninitalized */
508
return 0; /* leave it uninitalized */
490
510
FT_Library_Version(library, &major, &minor, &patch);
492
#if PY_VERSION_HEX >= 0x02020000
512
#if PY_VERSION_HEX >= 0x03000000
513
v = PyUnicode_FromFormat("%d.%d.%d", major, minor, patch);
493
515
v = PyString_FromFormat("%d.%d.%d", major, minor, patch);
497
sprintf(buffer, "%d.%d.%d", major, minor, patch);
498
v = PyString_FromString(buffer);
501
517
PyDict_SetItemString(d, "freetype2_version", v);
522
#if PY_VERSION_HEX >= 0x03000000
524
PyInit__imagingft(void) {
527
static PyModuleDef module_def = {
528
PyModuleDef_HEAD_INIT,
529
"_imagingft", /* m_name */
532
_functions, /* m_methods */
535
m = PyModule_Create(&module_def);
537
if (setup_module(m) < 0)
546
PyObject* m = Py_InitModule("_imagingft", _functions);