~ubuntu-branches/ubuntu/saucy/python-imaging/saucy-proposed

« back to all changes in this revision

Viewing changes to _imagingft.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include <freetype/freetype.h>
36
36
#endif
37
37
 
38
 
#if PY_VERSION_HEX < 0x01060000
39
 
#define PyObject_New PyObject_NEW
40
 
#define PyObject_Del PyMem_DEL
41
 
#endif
42
 
 
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) */
46
 
#define HAVE_UNICODE
47
 
#endif
48
 
#endif
49
 
 
50
 
#if !defined(Py_RETURN_NONE)
51
 
#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
52
 
#endif
 
38
#define KEEP_PY_UNICODE
 
39
#include "py3.h"
53
40
 
54
41
#if !defined(FT_LOAD_TARGET_MONO)
55
42
#define FT_LOAD_TARGET_MONO  FT_LOAD_MONOCHROME
82
69
    FT_Face face;
83
70
} FontObject;
84
71
 
85
 
staticforward PyTypeObject Font_Type;
 
72
static PyTypeObject Font_Type;
86
73
 
87
74
/* round a 26.6 pixel coordinate to the nearest larger integer */
88
75
#define PIXEL(x) ((((x)+63) & -64)>>6)
118
105
        "filename", "size", "index", "encoding", NULL
119
106
    };
120
107
 
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))
125
111
        return NULL;
126
 
#else
127
 
    if (!PyArg_ParseTupleAndKeywords(args, kw, "si|is", kwlist,
128
 
                                     &filename, &size, &index, &encoding))
129
 
        return NULL;
130
 
#endif
131
112
 
132
113
    if (!library) {
133
114
        PyErr_SetString(
164
145
static int
165
146
font_getchar(PyObject* string, int index, FT_ULong* char_out)
166
147
{
167
 
#if defined(HAVE_UNICODE)
168
148
    if (PyUnicode_Check(string)) {
169
149
        Py_UNICODE* p = PyUnicode_AS_UNICODE(string);
170
150
        int size = PyUnicode_GET_SIZE(string);
173
153
        *char_out = p[index];
174
154
        return 1;
175
155
    }
176
 
#endif
 
156
 
 
157
#if PY_VERSION_HEX < 0x03000000
177
158
    if (PyString_Check(string)) {
178
159
        unsigned char* p = (unsigned char*) PyString_AS_STRING(string);
179
160
        int size = PyString_GET_SIZE(string);
182
163
        *char_out = (unsigned char) p[index];
183
164
        return 1;
184
165
    }
 
166
#endif
 
167
 
185
168
    return 0;
186
169
}
187
170
 
201
184
    if (!PyArg_ParseTuple(args, "O:getsize", &string))
202
185
        return NULL;
203
186
 
204
 
#if defined(HAVE_UNICODE)
 
187
#if PY_VERSION_HEX >= 0x03000000
 
188
    if (!PyUnicode_Check(string)) {
 
189
#else
205
190
    if (!PyUnicode_Check(string) && !PyString_Check(string)) {
206
 
#else
207
 
    if (!PyString_Check(string)) {
208
191
#endif
209
192
        PyErr_SetString(PyExc_TypeError, "expected string");
210
193
        return NULL;
267
250
    if (!PyArg_ParseTuple(args, "O:getabc", &string))
268
251
        return NULL;
269
252
 
270
 
#if defined(HAVE_UNICODE)
 
253
#if PY_VERSION_HEX >= 0x03000000
 
254
    if (!PyUnicode_Check(string)) {
 
255
#else
271
256
    if (!PyUnicode_Check(string) && !PyString_Check(string)) {
272
 
#else
273
 
    if (!PyString_Check(string)) {
274
257
#endif
275
258
        PyErr_SetString(PyExc_TypeError, "expected string");
276
259
        return NULL;
315
298
    if (!PyArg_ParseTuple(args, "Ol|i:render", &string, &id, &mask))
316
299
        return NULL;
317
300
 
318
 
#if defined(HAVE_UNICODE)
 
301
#if PY_VERSION_HEX >= 0x03000000
 
302
    if (!PyUnicode_Check(string)) {
 
303
#else
319
304
    if (!PyUnicode_Check(string) && !PyString_Check(string)) {
320
 
#else
321
 
    if (!PyString_Check(string)) {
322
305
#endif
323
306
        PyErr_SetString(PyExc_TypeError, "expected string");
324
307
        return NULL;
420
403
    {NULL, NULL}
421
404
};
422
405
 
423
 
static PyObject*  
424
 
font_getattr(FontObject* self, char* name)
425
 
{
426
 
    PyObject* res;
427
 
 
428
 
    res = Py_FindMethod(font_methods, (PyObject*) self, name);
429
 
 
430
 
    if (res)
431
 
        return res;
432
 
 
433
 
    PyErr_Clear();
434
 
 
435
 
    /* attributes */
436
 
    if (!strcmp(name, "family")) {
437
 
        if (self->face->family_name)
438
 
            return PyString_FromString(self->face->family_name);
439
 
        Py_RETURN_NONE;
440
 
    }
441
 
    if (!strcmp(name, "style")) {
442
 
        if (self->face->style_name)
443
 
            return PyString_FromString(self->face->style_name);
444
 
        Py_RETURN_NONE;
445
 
    }
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));
450
 
 
451
 
    if (!strcmp(name, "glyphs"))
452
 
        /* number of glyphs provided by this font */
453
 
        return PyInt_FromLong(self->face->num_glyphs);
454
 
 
455
 
    PyErr_SetString(PyExc_AttributeError, name);
456
 
    return NULL;
457
 
}
458
 
 
459
 
statichere PyTypeObject Font_Type = {
460
 
    PyObject_HEAD_INIT(NULL)
461
 
    0, "Font", sizeof(FontObject), 0,
 
406
static PyObject*
 
407
font_getattr_family(FontObject* self, void* closure)
 
408
{
 
409
#if PY_VERSION_HEX >= 0x03000000
 
410
    if (self->face->family_name)
 
411
        return PyUnicode_FromString(self->face->family_name);
 
412
#else
 
413
    if (self->face->family_name)
 
414
        return PyString_FromString(self->face->family_name);
 
415
#endif
 
416
    Py_RETURN_NONE;
 
417
}
 
418
 
 
419
static PyObject*
 
420
font_getattr_style(FontObject* self, void* closure)
 
421
{
 
422
#if PY_VERSION_HEX >= 0x03000000
 
423
    if (self->face->style_name)
 
424
        return PyUnicode_FromString(self->face->style_name);
 
425
#else
 
426
    if (self->face->style_name)
 
427
        return PyString_FromString(self->face->style_name);
 
428
#endif
 
429
    Py_RETURN_NONE;
 
430
}
 
431
 
 
432
static PyObject*
 
433
font_getattr_ascent(FontObject* self, void* closure)
 
434
{
 
435
    return PyInt_FromLong(PIXEL(self->face->size->metrics.ascender));
 
436
}
 
437
 
 
438
static PyObject*
 
439
font_getattr_descent(FontObject* self, void* closure)
 
440
{
 
441
    return PyInt_FromLong(-PIXEL(self->face->size->metrics.descender));
 
442
}
 
443
 
 
444
static PyObject*
 
445
font_getattr_glyphs(FontObject* self, void* closure)
 
446
{
 
447
    return PyInt_FromLong(self->face->num_glyphs);
 
448
}
 
449
 
 
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 },
 
456
    { NULL }
 
457
};
 
458
 
 
459
static PyTypeObject Font_Type = {
 
460
    PyVarObject_HEAD_INIT(NULL, 0)
 
461
    "Font", sizeof(FontObject), 0,
462
462
    /* methods */
463
463
    (destructor)font_dealloc, /* tp_dealloc */
464
464
    0, /* tp_print */
465
 
    (getattrfunc)font_getattr, /* tp_getattr */
 
465
    0,                          /*tp_getattr*/
 
466
    0,                          /*tp_setattr*/
 
467
    0,                          /*tp_compare*/
 
468
    0,                          /*tp_repr*/
 
469
    0,                          /*tp_as_number */
 
470
    0,                          /*tp_as_sequence */
 
471
    0,                          /*tp_as_mapping */
 
472
    0,                          /*tp_hash*/
 
473
    0,                          /*tp_call*/
 
474
    0,                          /*tp_str*/
 
475
    0,                          /*tp_getattro*/
 
476
    0,                          /*tp_setattro*/
 
477
    0,                          /*tp_as_buffer*/
 
478
    Py_TPFLAGS_DEFAULT,         /*tp_flags*/
 
479
    0,                          /*tp_doc*/
 
480
    0,                          /*tp_traverse*/
 
481
    0,                          /*tp_clear*/
 
482
    0,                          /*tp_richcompare*/
 
483
    0,                          /*tp_weaklistoffset*/
 
484
    0,                          /*tp_iter*/
 
485
    0,                          /*tp_iternext*/
 
486
    font_methods,               /*tp_methods*/
 
487
    0,                          /*tp_members*/
 
488
    font_getsetters,            /*tp_getset*/
466
489
};
467
490
 
468
491
static PyMethodDef _functions[] = {
470
493
    {NULL, NULL}
471
494
};
472
495
 
473
 
DL_EXPORT(void)
474
 
init_imagingft(void)
475
 
{
476
 
    PyObject* m;
 
496
static int
 
497
setup_module(PyObject* m) {
477
498
    PyObject* d;
478
499
    PyObject* v;
479
500
    int major, minor, patch;
480
501
 
481
 
    /* Patch object type */
482
 
    Font_Type.ob_type = &PyType_Type;
483
 
 
484
 
    m = Py_InitModule("_imagingft", _functions);
485
502
    d = PyModule_GetDict(m);
486
503
 
 
504
    /* Ready object type */
 
505
    PyType_Ready(&Font_Type);
 
506
 
487
507
    if (FT_Init_FreeType(&library))
488
 
        return; /* leave it uninitalized */
 
508
        return 0; /* leave it uninitalized */
489
509
 
490
510
    FT_Library_Version(library, &major, &minor, &patch);
491
511
 
492
 
#if PY_VERSION_HEX >= 0x02020000
 
512
#if PY_VERSION_HEX >= 0x03000000
 
513
    v = PyUnicode_FromFormat("%d.%d.%d", major, minor, patch);
 
514
#else
493
515
    v = PyString_FromFormat("%d.%d.%d", major, minor, patch);
494
 
#else
495
 
    {
496
 
        char buffer[100];
497
 
        sprintf(buffer, "%d.%d.%d", major, minor, patch);
498
 
        v = PyString_FromString(buffer);
499
 
    }
500
516
#endif
501
517
    PyDict_SetItemString(d, "freetype2_version", v);
502
 
}
 
518
 
 
519
    return 0;
 
520
}
 
521
 
 
522
#if PY_VERSION_HEX >= 0x03000000
 
523
PyMODINIT_FUNC
 
524
PyInit__imagingft(void) {
 
525
    PyObject* m;
 
526
 
 
527
    static PyModuleDef module_def = {
 
528
        PyModuleDef_HEAD_INIT,
 
529
        "_imagingft",       /* m_name */
 
530
        NULL,               /* m_doc */
 
531
        -1,                 /* m_size */
 
532
        _functions,         /* m_methods */
 
533
    };
 
534
 
 
535
    m = PyModule_Create(&module_def);
 
536
 
 
537
    if (setup_module(m) < 0)
 
538
        return NULL;
 
539
 
 
540
    return m;
 
541
}
 
542
#else
 
543
PyMODINIT_FUNC
 
544
init_imagingft(void)
 
545
{
 
546
    PyObject* m = Py_InitModule("_imagingft", _functions);
 
547
    setup_module(m);
 
548
}
 
549
#endif
 
550