~jtaylor/ubuntu/oneiric/soya/fix-780305

« back to all changes in this revision

Viewing changes to c_src/python/i_text.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Dequènes (Duck)
  • Date: 2005-01-30 09:55:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050130095506-f21p6v6cgaobhn5j
Tags: 0.9.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * P3 python wrapper
3
 
 *
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.
8
 
 *
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.
13
 
 *
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
17
 
 */
18
 
 
19
 
/*****************************************
20
 
 * Copyright (C) 2002 Bertrand 'blam' LAMY
21
 
 *****************************************/
22
 
 
23
 
/*=============*
24
 
 * RASTER FONT *
25
 
 *=============*/
26
 
 
27
 
/*---------+
28
 
 | Methods |
29
 
 +---------*/
30
 
 
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); }
33
 
  free (f->chars);
34
 
  f->ob_type->tp_free ((PyObject*) f);
35
 
}
36
 
 
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)));
42
 
  } else {
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)));
48
 
  }
49
 
  return 0;
50
 
}
51
 
 
52
 
static PyObject* PyP3FontRaster_Print2D (P3_font_raster* f, PyObject* args) {
53
 
  int rw; int rh;
54
 
  PyObject* tuple;
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)),
59
 
                           &rw, &rh);
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));
63
 
  return tuple;
64
 
}
65
 
 
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))));
74
 
}
75
 
 
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)));
82
 
  Py_INCREF (Py_None);
83
 
  return Py_None;
84
 
}
85
 
 
86
 
static PyObject* PyP3FontRaster_Save (P3_font_raster* f, PyObject* arg) {
87
 
  P3_font_raster_save (f, PyString_AS_STRING (arg));
88
 
  Py_INCREF (Py_None);
89
 
  return Py_None;
90
 
}
91
 
 
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 },
97
 
  { NULL, NULL }
98
 
};
99
 
 
100
 
/*---------+
101
 
 | Get Set |
102
 
 +---------*/
103
 
 
104
 
static PyObject* PyP3FontRaster_GetCharHeight (P3_font_raster* a, void* context) {
105
 
  return PyInt_FromLong((long) a->height);
106
 
}
107
 
 
108
 
static PyGetSetDef PyP3FontRaster_GetSets[] = {
109
 
  { "height", (getter) PyP3FontRaster_GetCharHeight, (setter) 0, NULL },
110
 
  { NULL }
111
 
};
112
 
 
113
 
/*------+
114
 
 | Type |
115
 
 +------*/
116
 
 
117
 
PyTypeObject PyP3FontRaster_Type = {
118
 
  PyObject_HEAD_INIT(NULL)
119
 
  0,
120
 
  "_soya.FontRaster",
121
 
  sizeof(P3_font_raster),
122
 
  0,
123
 
  (destructor) PyP3FontRaster_Dealloc,/* tp_dealloc */
124
 
  0,/* tp_print */
125
 
  0,/* tp_getattr */
126
 
  0,/* tp_setattr */
127
 
  0,/* tp_compare */
128
 
  0,/* tp_repr */
129
 
  0,/* tp_as_number */
130
 
  0,/* tp_as_sequence */
131
 
  0,/* tp_as_mapping */
132
 
  0,/* tp_hash */
133
 
  0,/* tp_call */
134
 
  0,/* tp_str */
135
 
  PYP3_GENERIC_GETATTR,/* tp_getattro */
136
 
  0,/* tp_setattro */
137
 
  0,/* tp_as_buffer */
138
 
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
139
 
  0,/* tp_doc */
140
 
  (traverseproc) 0,/* tp_traverse */
141
 
  (inquiry) 0,/* tp_clear */
142
 
  0,/* tp_richcompare */
143
 
  0,/* tp_weaklistoffset */
144
 
  0,/* tp_iter */
145
 
  0,/* tp_iternext */
146
 
  (PyMethodDef*) &PyP3FontRaster_Methods,/* tp_methods */
147
 
  0,/* tp_members */
148
 
  (PyGetSetDef*) &PyP3FontRaster_GetSets,/* tp_getset */
149
 
  0,/* tp_base */
150
 
  0,/* tp_dict */
151
 
  0,/* tp_descr_get */
152
 
  0,/* tp_descr_set */
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 */
158
 
};
159
 
 
160
 
 
161
 
/*==============*
162
 
 * TEXTURE FONT *
163
 
 *==============*/
164
 
 
165
 
/*---------+
166
 
 | Methods |
167
 
 +---------*/
168
 
 
169
 
static void PyP3FontTexture_Dealloc (P3_font_texture* f) {
170
 
  P3_font_texture_dealloc (f);
171
 
  f->ob_type->tp_free ((PyObject*) f);
172
 
}
173
 
 
174
 
static int PyP3FontTexture_Init (P3_font_texture* self, PyObject* args, PyObject* kwds) {
175
 
  GLubyte color_limit[4];
176
 
  PyObject* o;
177
 
  int i;
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)));
182
 
  } else {
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));
186
 
    }
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)),
190
 
                                color_limit);
191
 
  }
192
 
  return 0;
193
 
}
194
 
 
195
 
static PyObject* PyP3FontTexture_GetPrintSize (P3_font_texture* f, PyObject* args) {
196
 
  int w; int h;
197
 
  PyObject* tuple;
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));
204
 
  } else {
205
 
    char* txt;
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));
212
 
    free (txt);
213
 
  }
214
 
  return tuple;
215
 
}
216
 
 
217
 
static PyObject* PyP3FontTexture_Print2D (P3_font_texture* f, PyObject* args) {
218
 
  PyObject* tuple;
219
 
  PyObject* o;
220
 
  int rh; int rw;
221
 
  int i;
222
 
  i = PySequence_Size (args);
223
 
  if (i == 3) {
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)),
228
 
                              &rw, &rh);
229
 
  } else if (i == 4) {
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)),
237
 
                                &rw, &rh);
238
 
  } else if (i == 5) {
239
 
    GLfloat color1[4];
240
 
    GLfloat color2[4];
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);
248
 
  } else if (i == 6) {
249
 
    GLfloat color1[4];
250
 
    GLfloat color2[4];
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);
261
 
  }
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));
265
 
  return tuple;
266
 
}
267
 
 
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))));
276
 
}
277
 
 
278
 
static PyObject* PyP3FontTexture_Print3D (P3_font_texture* f, PyObject* args) {
279
 
/*
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)));
285
 
*/
286
 
  Py_INCREF (Py_None);
287
 
  return Py_None;
288
 
}
289
 
 
290
 
static PyObject* PyP3FontTexture_Save (P3_font_texture* f, PyObject* arg) {
291
 
  P3_font_texture_save (f, PyString_AS_STRING (arg));
292
 
  Py_INCREF (Py_None);
293
 
  return Py_None;
294
 
}
295
 
 
296
 
static PyObject* PyP3FontTexture_SetPosition (P3_font_texture* f, PyObject* args) {
297
 
  static GLfloat matrix[16] = { 1.0, 0.0, 0.0, 0.0, 
298
 
                                0.0, 1.0, 0.0, 0.0, 
299
 
                                0.0, 0.0, 1.0, 0.0, 
300
 
                                0.0, 0.0, 0.0, 1.0 };
301
 
  if (args == NULL || args == Py_None || PySequence_Size (args) == 0) {
302
 
    glLoadIdentity ();
303
 
  } else {
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);
307
 
  }
308
 
  Py_INCREF (Py_None);
309
 
  return Py_None;
310
 
}
311
 
 
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 },
319
 
  { NULL, NULL }
320
 
};
321
 
 
322
 
/*---------+
323
 
 | Get Set |
324
 
 +---------*/
325
 
 
326
 
static PyObject* PyP3FontTexture_GetCharHeight (P3_font_texture* a, void* context) {
327
 
  return PyInt_FromLong((long) a->char_height);
328
 
}
329
 
 
330
 
static PyGetSetDef PyP3FontTexture_GetSets[] = {
331
 
  { "height", (getter) PyP3FontTexture_GetCharHeight, (setter) 0, NULL },
332
 
  { NULL }
333
 
};
334
 
 
335
 
/*------+
336
 
 | Type |
337
 
 +------*/
338
 
 
339
 
PyTypeObject PyP3FontTexture_Type = {
340
 
  PyObject_HEAD_INIT(NULL)
341
 
  0,
342
 
  "_soya.FontTexture",
343
 
  sizeof(P3_font_texture),
344
 
  0,
345
 
  (destructor) PyP3FontTexture_Dealloc,/* tp_dealloc */
346
 
  0,/* tp_print */
347
 
  0,/* tp_getattr */
348
 
  0,/* tp_setattr */
349
 
  0,/* tp_compare */
350
 
  0,/* tp_repr */
351
 
  0,/* tp_as_number */
352
 
  0,/* tp_as_sequence */
353
 
  0,/* tp_as_mapping */
354
 
  0,/* tp_hash */
355
 
  0,/* tp_call */
356
 
  0,/* tp_str */
357
 
  PYP3_GENERIC_GETATTR,/* tp_getattro */
358
 
  0,/* tp_setattro */
359
 
  0,/* tp_as_buffer */
360
 
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
361
 
  0,/* tp_doc */
362
 
  (traverseproc) 0,/* tp_traverse */
363
 
  (inquiry) 0,/* tp_clear */
364
 
  0,/* tp_richcompare */
365
 
  0,/* tp_weaklistoffset */
366
 
  0,/* tp_iter */
367
 
  0,/* tp_iternext */
368
 
  (PyMethodDef*) &PyP3FontTexture_Methods,/* tp_methods */
369
 
  0,/* tp_members */
370
 
  (PyGetSetDef*) &PyP3FontTexture_GetSets,/* tp_getset */
371
 
  0,/* tp_base */
372
 
  0,/* tp_dict */
373
 
  0,/* tp_descr_get */
374
 
  0,/* tp_descr_set */
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 */
380
 
};
381
 
 
382
 
static PyObject* PyP3_LoadFont (PyObject* module, PyObject* arg) {
383
 
  PyObject* f;
384
 
  FILE* file;
385
 
  char* filename;
386
 
  int i;
387
 
  filename = PyString_AS_STRING (arg);
388
 
  file = fopen (filename, "rb");
389
 
  if (file == NULL) { 
390
 
    P3_error ("can't open file %s", filename); 
391
 
    Py_INCREF (Py_None);
392
 
    return Py_None;
393
 
  }
394
 
  fread (&i, 1, sizeof (int), file);
395
 
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
396
 
  SDL_Swap32 (i);
397
 
#endif
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);
404
 
  } else {
405
 
    P3_error ("unknown font type (%i)", i); 
406
 
    Py_INCREF (Py_None);
407
 
    return Py_None;
408
 
  }
409
 
  return f;
410
 
}