4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
/*****************************************
20
* Copyright (C) 2002 Bertrand 'blam' LAMY
21
*****************************************/
31
static void PyP3FontRaster_Dealloc (P3_font_raster* f) {
32
if (engine_option & P3_GL_INITED && glIsList (f->call_list) == GL_TRUE) { glDeleteLists (f->call_list, f->nb_chars); }
34
f->ob_type->tp_free ((PyObject*) f);
37
static int PyP3FontRaster_Init (P3_font_raster* self, PyObject* args, PyObject* kwds) {
38
if (PySequence_Size (args) == 5) {
39
P3_font_raster_from_freetype (self, PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
40
PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)), PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
41
PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 3)), PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 4)));
43
P3_font_raster_from_image (self,
44
(P3_image*) PySequence_Fast_GET_ITEM (args, 0),
45
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
46
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
47
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 3)));
52
static PyObject* PyP3FontRaster_Print2D (P3_font_raster* f, PyObject* args) {
55
P3_font_raster_print_2i (f,
56
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
57
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
58
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
60
tuple = PyTuple_New (2);
61
PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong ((long) rw));
62
PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong ((long) rh));
66
static PyObject* PyP3FontRaster_PrintArea2D (P3_font_raster* f, PyObject* args) {
67
return PyInt_FromLong ((long) P3_font_raster_print_area (f,
68
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
69
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
70
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
71
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 3)),
72
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 4)),
73
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 5))));
76
static PyObject* PyP3FontRaster_Print3D (P3_font_raster* f, PyObject* args) {
77
P3_font_raster_print_3f (f,
78
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
79
(GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1)),
80
(GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 2)),
81
(GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 3)));
86
static PyObject* PyP3FontRaster_Save (P3_font_raster* f, PyObject* arg) {
87
P3_font_raster_save (f, PyString_AS_STRING (arg));
92
static PyMethodDef PyP3FontRaster_Methods[] = {
93
{ "draw_area", (PyCFunction) PyP3FontRaster_PrintArea2D, METH_VARARGS },
94
{ "draw_2D", (PyCFunction) PyP3FontRaster_Print2D, METH_VARARGS },
95
{ "draw_3D", (PyCFunction) PyP3FontRaster_Print3D, METH_VARARGS },
96
{ "save", (PyCFunction) PyP3FontRaster_Save, METH_O },
104
static PyObject* PyP3FontRaster_GetCharHeight (P3_font_raster* a, void* context) {
105
return PyInt_FromLong((long) a->height);
108
static PyGetSetDef PyP3FontRaster_GetSets[] = {
109
{ "height", (getter) PyP3FontRaster_GetCharHeight, (setter) 0, NULL },
117
PyTypeObject PyP3FontRaster_Type = {
118
PyObject_HEAD_INIT(NULL)
121
sizeof(P3_font_raster),
123
(destructor) PyP3FontRaster_Dealloc,/* tp_dealloc */
130
0,/* tp_as_sequence */
131
0,/* tp_as_mapping */
135
PYP3_GENERIC_GETATTR,/* tp_getattro */
138
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
140
(traverseproc) 0,/* tp_traverse */
141
(inquiry) 0,/* tp_clear */
142
0,/* tp_richcompare */
143
0,/* tp_weaklistoffset */
146
(PyMethodDef*) &PyP3FontRaster_Methods,/* tp_methods */
148
(PyGetSetDef*) &PyP3FontRaster_GetSets,/* tp_getset */
153
0,/* tp_dictoffset */
154
(initproc) PyP3FontRaster_Init,/* tp_init */
155
PYP3_GENERIC_ALLOC,/* tp_alloc */
156
(newfunc) PyP3Object_New,/* tp_new */
157
PYP3_GENERIC_FREE,/* tp_free */
169
static void PyP3FontTexture_Dealloc (P3_font_texture* f) {
170
P3_font_texture_dealloc (f);
171
f->ob_type->tp_free ((PyObject*) f);
174
static int PyP3FontTexture_Init (P3_font_texture* self, PyObject* args, PyObject* kwds) {
175
GLubyte color_limit[4];
178
if (PySequence_Size (args) == 5) {
179
P3_font_texture_from_freetype (self, PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
180
PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)), PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
181
PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 3)), PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 4)));
183
o = PySequence_Fast_GET_ITEM (args, 2);
184
for (i = 0; i < PySequence_Size (o); i++) {
185
color_limit[i] = (GLubyte) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (o, i));
187
P3_font_texture_from_image (self,
188
(P3_image*) PySequence_Fast_GET_ITEM (args, 0),
189
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
195
static PyObject* PyP3FontTexture_GetPrintSize (P3_font_texture* f, PyObject* args) {
198
char* s = PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0));
199
if (PySequence_Size (args) == 1) {
200
P3_font_texture_get_size (f, s, -1, &w, &h, NULL);
201
tuple = PyTuple_New (2);
202
PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong ((long) w));
203
PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong ((long) h));
206
txt = (char*) malloc ((strlen (s) + 1) * sizeof (char));
207
txt = P3_font_texture_get_size (f, s, (int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)), &w, &h, txt);
208
tuple = PyTuple_New (3);
209
PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong ((long) w));
210
PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong ((long) h));
211
PyTuple_SET_ITEM (tuple, 2, PyString_FromString (txt));
217
static PyObject* PyP3FontTexture_Print2D (P3_font_texture* f, PyObject* args) {
222
i = PySequence_Size (args);
224
P3_font_texture_print_2i (f,
225
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
226
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
227
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
230
o = PySequence_Fast_GET_ITEM (args, 3);
231
P3_font_texture_print_2i_s (f,
232
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
233
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
234
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
235
(float) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (o, 0)),
236
(float) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (o, 1)),
241
PY_TUPLE_FLOAT_TO_ARRAY_4 (color1, PySequence_Fast_GET_ITEM (args, 3));
242
PY_TUPLE_FLOAT_TO_ARRAY_4 (color2, PySequence_Fast_GET_ITEM (args, 4));
243
P3_font_texture_print_2i_c (f,
244
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
245
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
246
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
247
color1, color2, &rw, &rh);
251
PY_TUPLE_FLOAT_TO_ARRAY_4 (color1, PySequence_Fast_GET_ITEM (args, 4));
252
PY_TUPLE_FLOAT_TO_ARRAY_4 (color2, PySequence_Fast_GET_ITEM (args, 5));
253
o = PySequence_Fast_GET_ITEM (args, 3);
254
P3_font_texture_print_2i_sc (f,
255
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
256
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
257
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
258
(float) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (o, 0)),
259
(float) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (o, 1)),
260
color1, color2, &rw, &rh);
262
tuple = PyTuple_New (2);
263
PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong ((long) rw));
264
PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong ((long) rh));
268
static PyObject* PyP3FontTexture_PrintArea2D (P3_font_texture* f, PyObject* args) {
269
return PyInt_FromLong ((long) P3_font_texture_print_area (f,
270
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
271
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)),
272
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)),
273
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 3)),
274
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 4)),
275
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 5))));
278
static PyObject* PyP3FontTexture_Print3D (P3_font_texture* f, PyObject* args) {
280
P3_font_texture_print_3f (f,
281
PyString_AS_STRING (PySequence_Fast_GET_ITEM (args, 0)),
282
(GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1)),
283
(GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 2)),
284
(GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 3)));
290
static PyObject* PyP3FontTexture_Save (P3_font_texture* f, PyObject* arg) {
291
P3_font_texture_save (f, PyString_AS_STRING (arg));
296
static PyObject* PyP3FontTexture_SetPosition (P3_font_texture* f, PyObject* args) {
297
static GLfloat matrix[16] = { 1.0, 0.0, 0.0, 0.0,
300
0.0, 0.0, 0.0, 1.0 };
301
if (args == NULL || args == Py_None || PySequence_Size (args) == 0) {
304
matrix[12] = (GLfloat) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 0));
305
matrix[13] = (GLfloat) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1));
306
glLoadMatrixf (matrix);
312
static PyMethodDef PyP3FontTexture_Methods[] = {
313
{ "draw_area", (PyCFunction) PyP3FontTexture_PrintArea2D, METH_VARARGS },
314
{ "draw_2D", (PyCFunction) PyP3FontTexture_Print2D, METH_VARARGS },
315
{ "draw_3D", (PyCFunction) PyP3FontTexture_Print3D, METH_VARARGS },
316
{ "set_position", (PyCFunction) PyP3FontTexture_SetPosition, METH_VARARGS },
317
{ "get_print_size", (PyCFunction) PyP3FontTexture_GetPrintSize, METH_VARARGS },
318
{ "save", (PyCFunction) PyP3FontTexture_Save, METH_O },
326
static PyObject* PyP3FontTexture_GetCharHeight (P3_font_texture* a, void* context) {
327
return PyInt_FromLong((long) a->char_height);
330
static PyGetSetDef PyP3FontTexture_GetSets[] = {
331
{ "height", (getter) PyP3FontTexture_GetCharHeight, (setter) 0, NULL },
339
PyTypeObject PyP3FontTexture_Type = {
340
PyObject_HEAD_INIT(NULL)
343
sizeof(P3_font_texture),
345
(destructor) PyP3FontTexture_Dealloc,/* tp_dealloc */
352
0,/* tp_as_sequence */
353
0,/* tp_as_mapping */
357
PYP3_GENERIC_GETATTR,/* tp_getattro */
360
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
362
(traverseproc) 0,/* tp_traverse */
363
(inquiry) 0,/* tp_clear */
364
0,/* tp_richcompare */
365
0,/* tp_weaklistoffset */
368
(PyMethodDef*) &PyP3FontTexture_Methods,/* tp_methods */
370
(PyGetSetDef*) &PyP3FontTexture_GetSets,/* tp_getset */
375
0,/* tp_dictoffset */
376
(initproc) PyP3FontTexture_Init,/* tp_init */
377
PYP3_GENERIC_ALLOC,/* tp_alloc */
378
(newfunc) PyP3Object_New,/* tp_new */
379
PYP3_GENERIC_FREE,/* tp_free */
382
static PyObject* PyP3_LoadFont (PyObject* module, PyObject* arg) {
387
filename = PyString_AS_STRING (arg);
388
file = fopen (filename, "rb");
390
P3_error ("can't open file %s", filename);
394
fread (&i, 1, sizeof (int), file);
395
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
398
if (i == P3_FONT_RASTER) {
399
f = PyP3FontRaster_Type.tp_alloc (&PyP3FontRaster_Type, 0);
400
P3_font_raster_load ((P3_font_raster*) f, file);
401
} else if (i == P3_FONT_TEXTURE) {
402
f = PyP3FontTexture_Type.tp_alloc (&PyP3FontTexture_Type, 0);
403
P3_font_texture_load ((P3_font_texture*) f, file);
405
P3_error ("unknown font type (%i)", i);