~ubuntu-branches/ubuntu/feisty/pygame/feisty

« back to all changes in this revision

Viewing changes to src/font.c

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    pygame - Python Game Library
 
3
    Copyright (C) 2000-2001  Pete Shinners
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License as published by the Free Software Foundation; either
 
8
    version 2 of the License, or (at your option) any later version.
 
9
 
 
10
    This library is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
    Library General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU Library General Public
 
16
    License along with this library; if not, write to the Free
 
17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
    Pete Shinners
 
20
    pete@shinners.org
 
21
*/
 
22
 
 
23
/*
 
24
 *  font module for pygame
 
25
 */
 
26
#define PYGAMEAPI_FONT_INTERNAL
 
27
#include <string.h>
 
28
#include "pygame.h"
 
29
#include "font.h"
 
30
 
 
31
 
 
32
 
 
33
staticforward PyTypeObject PyFont_Type;
 
34
static PyObject* PyFont_New(TTF_Font*);
 
35
#define PyFont_Check(x) ((x)->ob_type == &PyFont_Type)
 
36
 
 
37
static int font_initialized = 0;
 
38
static char* font_defaultname = "bluebold.ttf";
 
39
static char* font_defaultpath = NULL;
 
40
static PyObject* self_module = NULL;
 
41
 
 
42
static void font_autoquit(void)
 
43
{
 
44
        if(font_initialized)
 
45
        {
 
46
                font_initialized = 0;
 
47
                TTF_Quit();
 
48
        }
 
49
        if(font_defaultpath)
 
50
        {
 
51
                PyMem_Free(font_defaultpath);
 
52
                font_defaultpath = NULL;
 
53
        }
 
54
}
 
55
 
 
56
 
 
57
static PyObject* font_autoinit(PyObject* self, PyObject* arg)
 
58
{
 
59
        if(!PyArg_ParseTuple(arg, ""))
 
60
                return NULL;
 
61
 
 
62
        if(!font_initialized)
 
63
        {
 
64
                PyGame_RegisterQuit(font_autoquit);
 
65
 
 
66
                if(TTF_Init())
 
67
                        return PyInt_FromLong(0);
 
68
                font_initialized = 1;
 
69
 
 
70
                if(!font_defaultpath)
 
71
                {
 
72
                        char* path = PyModule_GetFilename(self_module);
 
73
                        if(!path)
 
74
                        {
 
75
                                PyErr_Clear();
 
76
                        }
 
77
                        else
 
78
                        {
 
79
                                char* end = strstr(path, "font.");
 
80
                                if(end)
 
81
                                {
 
82
                                        font_defaultpath = PyMem_Malloc(strlen(path) + 16);
 
83
                                        if(font_defaultpath)
 
84
                                        {
 
85
                                                strcpy(font_defaultpath, path);
 
86
                                                end = strstr(font_defaultpath, "font.");
 
87
                                                strcpy(end, font_defaultname);
 
88
                                        }
 
89
                                }
 
90
                        }
 
91
 
 
92
                        if(!font_defaultpath)
 
93
                        {
 
94
                                font_defaultpath = PyMem_Malloc(strlen(font_defaultname) + 1);
 
95
                                if(font_defaultpath)
 
96
                                        strcpy(font_defaultpath, font_defaultname);
 
97
                        }
 
98
                }
 
99
        }
 
100
        return PyInt_FromLong(font_defaultpath != NULL);
 
101
}
 
102
 
 
103
 
 
104
    /*DOC*/ static char doc_quit[] =
 
105
    /*DOC*/    "pygame.font.quit() -> none\n"
 
106
    /*DOC*/    "uninitialize the font module\n"
 
107
    /*DOC*/    "\n"
 
108
    /*DOC*/    "Manually uninitialize SDL's video subsystem. It is safe to call\n"
 
109
    /*DOC*/    "this if font is currently not initialized.\n"
 
110
    /*DOC*/ ;
 
111
 
 
112
static PyObject* font_quit(PyObject* self, PyObject* arg)
 
113
{
 
114
        if(!PyArg_ParseTuple(arg, ""))
 
115
                return NULL;
 
116
 
 
117
        font_autoquit();
 
118
 
 
119
        RETURN_NONE
 
120
}
 
121
 
 
122
 
 
123
    /*DOC*/ static char doc_init[] =
 
124
    /*DOC*/    "pygame.font.init() -> None\n"
 
125
    /*DOC*/    "initialize the display module\n"
 
126
    /*DOC*/    "\n"
 
127
    /*DOC*/    "Manually initialize the font module. Will raise an exception if\n"
 
128
    /*DOC*/    "it cannot be initialized. It is safe to call this function if\n"
 
129
    /*DOC*/    "font is currently initialized.\n"
 
130
    /*DOC*/ ;
 
131
 
 
132
static PyObject* font_init(PyObject* self, PyObject* arg)
 
133
{
 
134
        PyObject* result;
 
135
        int istrue;
 
136
 
 
137
        if(!PyArg_ParseTuple(arg, ""))
 
138
                return NULL;
 
139
 
 
140
        result = font_autoinit(self, arg);
 
141
        istrue = PyObject_IsTrue(result);
 
142
        Py_DECREF(result);
 
143
        if(!istrue)
 
144
                return RAISE(PyExc_SDLError, SDL_GetError());
 
145
 
 
146
        RETURN_NONE
 
147
}
 
148
 
 
149
 
 
150
 
 
151
    /*DOC*/ static char doc_get_init[] =
 
152
    /*DOC*/    "pygame.font.get_init() -> bool\n"
 
153
    /*DOC*/    "get status of font module initialization\n"
 
154
    /*DOC*/    "\n"
 
155
    /*DOC*/    "Returns true if the font module is currently intialized.\n"
 
156
    /*DOC*/ ;
 
157
 
 
158
static PyObject* get_init(PyObject* self, PyObject* arg)
 
159
{
 
160
        if(!PyArg_ParseTuple(arg, ""))
 
161
                return NULL;
 
162
 
 
163
        return PyInt_FromLong(font_initialized);
 
164
}
 
165
 
 
166
 
 
167
 
 
168
 
 
169
 
 
170
/* font object methods */
 
171
 
 
172
    /*DOC*/ static char doc_font_get_height[] =
 
173
    /*DOC*/    "Font.get_height() -> int\n"
 
174
    /*DOC*/    "average height of font glyph\n"
 
175
    /*DOC*/    "\n"
 
176
    /*DOC*/    "Returns the average size of each glyph in the font.\n"
 
177
    /*DOC*/ ;
 
178
 
 
179
static PyObject* font_get_height(PyObject* self, PyObject* args)
 
180
{
 
181
        TTF_Font* font = PyFont_AsFont(self);
 
182
 
 
183
        if(!PyArg_ParseTuple(args, ""))
 
184
                return NULL;
 
185
 
 
186
        return PyInt_FromLong(TTF_FontHeight(font));
 
187
}
 
188
 
 
189
 
 
190
 
 
191
    /*DOC*/ static char doc_font_get_descent[] =
 
192
    /*DOC*/    "Font.get_descent() -> int\n"
 
193
    /*DOC*/    "gets the font descent\n"
 
194
    /*DOC*/    "\n"
 
195
    /*DOC*/    "Returns the descent for the font. The descent is the number of\n"
 
196
    /*DOC*/    "pixels from the font baseline to the bottom of the font.\n"
 
197
    /*DOC*/ ;
 
198
 
 
199
static PyObject* font_get_descent(PyObject* self, PyObject* args)
 
200
{
 
201
        TTF_Font* font = PyFont_AsFont(self);
 
202
 
 
203
        if(!PyArg_ParseTuple(args, ""))
 
204
                return NULL;
 
205
 
 
206
        return PyInt_FromLong(TTF_FontDescent(font));
 
207
}
 
208
 
 
209
 
 
210
 
 
211
    /*DOC*/ static char doc_font_get_ascent[] =
 
212
    /*DOC*/    "Font.get_ascent() -> int\n"
 
213
    /*DOC*/    "gets the font ascent\n"
 
214
    /*DOC*/    "\n"
 
215
    /*DOC*/    "Returns the ascent for the font. The ascent is the number of\n"
 
216
    /*DOC*/    "pixels from the font baseline to the top of the font.\n"
 
217
    /*DOC*/ ;
 
218
 
 
219
static PyObject* font_get_ascent(PyObject* self, PyObject* args)
 
220
{
 
221
        TTF_Font* font = PyFont_AsFont(self);
 
222
 
 
223
        if(!PyArg_ParseTuple(args, ""))
 
224
                return NULL;
 
225
 
 
226
        return PyInt_FromLong(TTF_FontAscent(font));
 
227
}
 
228
 
 
229
 
 
230
 
 
231
    /*DOC*/ static char doc_font_get_linesize[] =
 
232
    /*DOC*/    "Font.get_linesize() -> int\n"
 
233
    /*DOC*/    "gets the font recommended linesize\n"
 
234
    /*DOC*/    "\n"
 
235
    /*DOC*/    "Returns the linesize for the font. Each font comes with it's own\n"
 
236
    /*DOC*/    "recommendation for the spacing number of pixels between each line\n"
 
237
    /*DOC*/    "of the font.\n"
 
238
    /*DOC*/ ;
 
239
 
 
240
static PyObject* font_get_linesize(PyObject* self, PyObject* args)
 
241
{
 
242
        TTF_Font* font = PyFont_AsFont(self);
 
243
 
 
244
        if(!PyArg_ParseTuple(args, ""))
 
245
                return NULL;
 
246
 
 
247
        return PyInt_FromLong(TTF_FontLineSkip(font));
 
248
}
 
249
 
 
250
 
 
251
 
 
252
    /*DOC*/ static char doc_font_get_bold[] =
 
253
    /*DOC*/    "Font.get_bold() -> bool\n"
 
254
    /*DOC*/    "status of the bold attribute\n"
 
255
    /*DOC*/    "\n"
 
256
    /*DOC*/    "Get the current status of the font's bold attribute\n"
 
257
    /*DOC*/ ;
 
258
 
 
259
static PyObject* font_get_bold(PyObject* self, PyObject* args)
 
260
{
 
261
        TTF_Font* font = PyFont_AsFont(self);
 
262
 
 
263
        if(!PyArg_ParseTuple(args, ""))
 
264
                return NULL;
 
265
 
 
266
        return PyInt_FromLong((TTF_GetFontStyle(font)&TTF_STYLE_BOLD) != 0);
 
267
}
 
268
 
 
269
 
 
270
 
 
271
    /*DOC*/ static char doc_font_set_bold[] =
 
272
    /*DOC*/    "Font.set_bold(bool) -> None\n"
 
273
    /*DOC*/    "assign the bold attribute\n"
 
274
    /*DOC*/    "\n"
 
275
    /*DOC*/    "Enables or disables the bold attribute for the font. Making the\n"
 
276
    /*DOC*/    "font bold does not work as well as you expect.\n"
 
277
    /*DOC*/ ;
 
278
 
 
279
static PyObject* font_set_bold(PyObject* self, PyObject* args)
 
280
{
 
281
        TTF_Font* font = PyFont_AsFont(self);
 
282
        int style, val;
 
283
 
 
284
        if(!PyArg_ParseTuple(args, "i", &val))
 
285
                return NULL;
 
286
 
 
287
        style = TTF_GetFontStyle(font);
 
288
        if(val)
 
289
                style |= TTF_STYLE_BOLD;
 
290
        else
 
291
                style &= ~TTF_STYLE_BOLD;
 
292
        TTF_SetFontStyle(font, style);
 
293
 
 
294
        RETURN_NONE
 
295
}
 
296
 
 
297
 
 
298
 
 
299
    /*DOC*/ static char doc_font_get_italic[] =
 
300
    /*DOC*/    "Font.get_italic() -> bool\n"
 
301
    /*DOC*/    "status of the italic attribute\n"
 
302
    /*DOC*/    "\n"
 
303
    /*DOC*/    "Get the current status of the font's italic attribute\n"
 
304
    /*DOC*/ ;
 
305
 
 
306
static PyObject* font_get_italic(PyObject* self, PyObject* args)
 
307
{
 
308
        TTF_Font* font = PyFont_AsFont(self);
 
309
 
 
310
        if(!PyArg_ParseTuple(args, ""))
 
311
                return NULL;
 
312
 
 
313
        return PyInt_FromLong((TTF_GetFontStyle(font)&TTF_STYLE_ITALIC) != 0);
 
314
}
 
315
 
 
316
 
 
317
 
 
318
    /*DOC*/ static char doc_font_set_italic[] =
 
319
    /*DOC*/    "Font.set_italic(bool) -> None\n"
 
320
    /*DOC*/    "assign the italic attribute\n"
 
321
    /*DOC*/    "\n"
 
322
    /*DOC*/    "Enables or disables the italic attribute for the font.\n"
 
323
    /*DOC*/ ;
 
324
 
 
325
static PyObject* font_set_italic(PyObject* self, PyObject* args)
 
326
{
 
327
        TTF_Font* font = PyFont_AsFont(self);
 
328
        int style, val;
 
329
 
 
330
        if(!PyArg_ParseTuple(args, "i", &val))
 
331
                return NULL;
 
332
 
 
333
        style = TTF_GetFontStyle(font);
 
334
        if(val)
 
335
                style |= TTF_STYLE_ITALIC;
 
336
        else
 
337
                style &= ~TTF_STYLE_ITALIC;
 
338
        TTF_SetFontStyle(font, style);
 
339
 
 
340
        RETURN_NONE
 
341
}
 
342
 
 
343
 
 
344
 
 
345
    /*DOC*/ static char doc_font_get_underline[] =
 
346
    /*DOC*/    "Font.get_underline() -> bool\n"
 
347
    /*DOC*/    "status of the underline attribute\n"
 
348
    /*DOC*/    "\n"
 
349
    /*DOC*/    "Get the current status of the font's underline attribute\n"
 
350
    /*DOC*/ ;
 
351
 
 
352
static PyObject* font_get_underline(PyObject* self, PyObject* args)
 
353
{
 
354
        TTF_Font* font = PyFont_AsFont(self);
 
355
 
 
356
        if(!PyArg_ParseTuple(args, ""))
 
357
                return NULL;
 
358
 
 
359
        return PyInt_FromLong((TTF_GetFontStyle(font)&TTF_STYLE_UNDERLINE) != 0);
 
360
}
 
361
 
 
362
 
 
363
 
 
364
    /*DOC*/ static char doc_font_set_underline[] =
 
365
    /*DOC*/    "Font.set_underline(bool) -> None\n"
 
366
    /*DOC*/    "assign the underline attribute\n"
 
367
    /*DOC*/    "\n"
 
368
    /*DOC*/    "Enables or disables the underline attribute for the font.\n"
 
369
    /*DOC*/ ;
 
370
 
 
371
static PyObject* font_set_underline(PyObject* self, PyObject* args)
 
372
{
 
373
        TTF_Font* font = PyFont_AsFont(self);
 
374
        int style, val;
 
375
 
 
376
        if(!PyArg_ParseTuple(args, "i", &val))
 
377
                return NULL;
 
378
 
 
379
        style = TTF_GetFontStyle(font);
 
380
        if(val)
 
381
                style |= TTF_STYLE_UNDERLINE;
 
382
        else
 
383
                style &= ~TTF_STYLE_UNDERLINE;
 
384
        TTF_SetFontStyle(font, style);
 
385
 
 
386
        RETURN_NONE
 
387
}
 
388
 
 
389
 
 
390
 
 
391
    /*DOC*/ static char doc_font_render[] =
 
392
    /*DOC*/    "Font.render(text, antialias, fore_RGBA, [back_RGBA]) -> Surface\n"
 
393
    /*DOC*/    "render text to a new image\n"
 
394
    /*DOC*/    "\n"
 
395
    /*DOC*/    "Render the given text onto a new image surface. The given text\n"
 
396
    /*DOC*/    "can be standard python text or unicode. Antialiasing will smooth\n"
 
397
    /*DOC*/    "the edges of the font for a much cleaner look. The foreground\n"
 
398
    /*DOC*/    "and background color are both RGBA, the alpha component is ignored\n"
 
399
    /*DOC*/    "if given. If the background color is omitted, the text will have a\n"
 
400
    /*DOC*/    "transparent background.\n"
 
401
    /*DOC*/    "\n"
 
402
    /*DOC*/    "Note that font rendering is not thread safe, therefore only one\n"
 
403
    /*DOC*/    "thread can render text at any given time.\n"
 
404
    /*DOC*/    "\n"
 
405
    /*DOC*/    "Also, rendering smooth text with underlines will crash with SDL_ttf\n"
 
406
    /*DOC*/    "less that version 2.0, be careful.\n"
 
407
    /*DOC*/ ;
 
408
 
 
409
static PyObject* font_render(PyObject* self, PyObject* args)
 
410
{
 
411
        TTF_Font* font = PyFont_AsFont(self);
 
412
        int aa;
 
413
        PyObject* text, *final;
 
414
        PyObject* fg_rgba_obj, *bg_rgba_obj = NULL;
 
415
        Uint8 rgba[4];
 
416
        SDL_Surface* surf;
 
417
        SDL_Color foreg, backg;
 
418
        if(!PyArg_ParseTuple(args, "OiO|O", &text, &aa, &fg_rgba_obj, &bg_rgba_obj))
 
419
                return NULL;
 
420
 
 
421
        if(!RGBAFromObj(fg_rgba_obj, rgba))
 
422
                return RAISE(PyExc_TypeError, "Invalid foreground RGBA argument");
 
423
        foreg.r = rgba[0]; foreg.g = rgba[1]; foreg.b = rgba[2];
 
424
        if(bg_rgba_obj)
 
425
        {
 
426
                if(!RGBAFromObj(bg_rgba_obj, rgba))
 
427
                        return RAISE(PyExc_TypeError, "Invalid background RGBA argument");
 
428
                backg.r = rgba[0]; backg.g = rgba[1]; backg.b = rgba[2];
 
429
        }       
 
430
 
 
431
        if(PyUnicode_Check(text))
 
432
        {
 
433
                Py_UNICODE* string = PyUnicode_AsUnicode(text);
 
434
                if(aa)
 
435
                {
 
436
                        if(!bg_rgba_obj)
 
437
                                surf = TTF_RenderUNICODE_Blended(font, string, foreg);
 
438
                        else
 
439
                                surf = TTF_RenderUNICODE_Shaded(font, string, foreg, backg);
 
440
                }
 
441
                else
 
442
                        surf = TTF_RenderUNICODE_Solid(font, string, foreg);
 
443
        }
 
444
        else if(PyString_Check(text))
 
445
        {
 
446
                char* string = PyString_AsString(text);
 
447
                if(aa)
 
448
                {
 
449
                        if(!bg_rgba_obj)
 
450
                                surf = TTF_RenderText_Blended(font, string, foreg);
 
451
                        else
 
452
                                surf = TTF_RenderText_Shaded(font, string, foreg, backg);
 
453
                }
 
454
                else
 
455
                        surf = TTF_RenderText_Solid(font, string, foreg);
 
456
        }
 
457
        else
 
458
                return RAISE(PyExc_TypeError, "text must be a string or unicode");
 
459
 
 
460
        if(!surf)
 
461
                return RAISE(PyExc_SDLError, SDL_GetError());
 
462
 
 
463
        if(!aa && bg_rgba_obj) /*turn off transparancy*/
 
464
        {                       
 
465
                SDL_SetColorKey(surf, 0, 0);
 
466
                surf->format->palette->colors[0].r = backg.r;
 
467
                surf->format->palette->colors[0].g = backg.g;
 
468
                surf->format->palette->colors[0].b = backg.b;
 
469
        }
 
470
 
 
471
        final = PySurface_New(surf);
 
472
        if(!final)
 
473
                SDL_FreeSurface(surf);
 
474
        return final;
 
475
}
 
476
 
 
477
 
 
478
 
 
479
    /*DOC*/ static char doc_font_size[] =
 
480
    /*DOC*/    "Font.size(text) -> width, height\n"
 
481
    /*DOC*/    "size of rendered text\n"
 
482
    /*DOC*/    "\n"
 
483
    /*DOC*/    "Computes the rendered size of the given text. The text can be\n"
 
484
    /*DOC*/    "standard python text or unicode. Changing the bold and italic\n"
 
485
    /*DOC*/    "attributes can change the size of the rendered text.\n"
 
486
    /*DOC*/ ;
 
487
 
 
488
static PyObject* font_size(PyObject* self, PyObject* args)
 
489
{
 
490
        TTF_Font* font = PyFont_AsFont(self);
 
491
        int w, h;
 
492
        PyObject* text;
 
493
 
 
494
        if(!PyArg_ParseTuple(args, "O", &text))
 
495
                return NULL;
 
496
 
 
497
        if(PyUnicode_Check(text))
 
498
        {
 
499
                Py_UNICODE* string = PyUnicode_AsUnicode(text);
 
500
                TTF_SizeUNICODE(font, string, &w, &h);
 
501
        }
 
502
        else if(PyString_Check(text))
 
503
        {
 
504
                char* string = PyString_AsString(text);
 
505
                TTF_SizeText(font, string, &w, &h);
 
506
        }
 
507
        else
 
508
                return RAISE(PyExc_TypeError, "text must be a string or unicode");
 
509
 
 
510
        return Py_BuildValue("(ii)", w, h);
 
511
}
 
512
 
 
513
 
 
514
 
 
515
static PyMethodDef fontobj_builtins[] =
 
516
{
 
517
        { "get_height", font_get_height, 1, doc_font_get_height },
 
518
        { "get_descent", font_get_descent, 1, doc_font_get_descent },
 
519
        { "get_ascent", font_get_ascent, 1, doc_font_get_ascent },
 
520
        { "get_linesize", font_get_linesize, 1, doc_font_get_linesize },
 
521
 
 
522
        { "get_bold", font_get_bold, 1, doc_font_get_bold },
 
523
        { "set_bold", font_set_bold, 1, doc_font_set_bold },
 
524
        { "get_italic", font_get_italic, 1, doc_font_get_italic },
 
525
        { "set_italic", font_set_italic, 1, doc_font_set_italic },
 
526
        { "get_underline", font_get_underline, 1, doc_font_get_underline },
 
527
        { "set_underline", font_set_underline, 1, doc_font_set_underline },
 
528
 
 
529
        { "render", font_render, 1, doc_font_render },
 
530
        { "size", font_size, 1, doc_font_size },
 
531
 
 
532
        { NULL, NULL }
 
533
};
 
534
 
 
535
 
 
536
 
 
537
/*font object internals*/
 
538
 
 
539
static void font_dealloc(PyObject* self)
 
540
{
 
541
        TTF_Font* font = PyFont_AsFont(self);
 
542
 
 
543
        if(font_initialized)
 
544
                TTF_CloseFont(font);
 
545
 
 
546
        PyObject_DEL(self);
 
547
}
 
548
 
 
549
 
 
550
static PyObject* font_getattr(PyObject* self, char* attrname)
 
551
{
 
552
        if(font_initialized)
 
553
                return Py_FindMethod(fontobj_builtins, self, attrname);
 
554
 
 
555
        PyErr_SetString(PyExc_NameError, attrname);
 
556
        return NULL; 
 
557
}
 
558
 
 
559
 
 
560
    /*DOC*/ static char doc_Font_MODULE[] =
 
561
    /*DOC*/    "The font object is created only from pygame.font.Font(). Once a\n"
 
562
    /*DOC*/    "font is created it's size and TTF file cannot be changed. The\n"
 
563
    /*DOC*/    "Font objects are mainly used to render() text into a new Surface.\n"
 
564
    /*DOC*/    "The Font objects also have a few states that can be set with\n"
 
565
    /*DOC*/    "set_underline(bool), set_bold(bool), set_italic(bool). Each of\n"
 
566
    /*DOC*/    "these functions contains an equivalent get_XXX() routine to find\n"
 
567
    /*DOC*/    "the current state. There are also many routines to query the\n"
 
568
    /*DOC*/    "dimensions of the text. The rendering functions work with both\n"
 
569
    /*DOC*/    "normal python strings, as well as with unicode strings.\n"
 
570
    /*DOC*/ ;
 
571
 
 
572
static PyTypeObject PyFont_Type = 
 
573
{
 
574
        PyObject_HEAD_INIT(NULL)
 
575
        0,
 
576
        "Font",
 
577
        sizeof(PyFontObject),
 
578
        0,
 
579
        font_dealloc,   
 
580
        0,
 
581
        font_getattr,
 
582
        0,
 
583
        0,
 
584
        0,
 
585
        0,
 
586
        NULL,
 
587
        0, 
 
588
        (hashfunc)NULL,
 
589
        (ternaryfunc)NULL,
 
590
        (reprfunc)NULL,
 
591
        0L,0L,0L,0L,
 
592
        doc_Font_MODULE /* Documentation string */
 
593
};
 
594
 
 
595
 
 
596
 
 
597
/*font module methods*/
 
598
 
 
599
    /*DOC*/ static char doc_Font[] =
 
600
    /*DOC*/    "pygame.font.Font(file, size) -> Font\n"
 
601
    /*DOC*/    "create a new font object\n"
 
602
    /*DOC*/    "\n"
 
603
    /*DOC*/    "This will create a new font object. The given file must be a\n"
 
604
    /*DOC*/    "filename to a TTF file. The font loader does not work with python\n"
 
605
    /*DOC*/    "file-like objects. The size represents the height of the font in\n"
 
606
    /*DOC*/    "pixels. The file argument can be 'None', which will use a plain\n"
 
607
    /*DOC*/    "default font.\n"
 
608
    /*DOC*/ ;
 
609
 
 
610
static PyObject* Font(PyObject* self, PyObject* args)
 
611
{
 
612
        PyObject* fileobj;
 
613
        char* filename;
 
614
        int fontsize;
 
615
        TTF_Font* font;
 
616
        PyObject* fontobj;
 
617
        if(!PyArg_ParseTuple(args, "Oi", &fileobj, &fontsize))
 
618
                return NULL;
 
619
 
 
620
        if(!font_initialized)
 
621
                return RAISE(PyExc_SDLError, "font not initialized");
 
622
 
 
623
        if(fileobj == Py_None)
 
624
        {
 
625
                if(!font_defaultpath)
 
626
                        return RAISE(PyExc_RuntimeError, "default font not found");
 
627
                filename = font_defaultpath;
 
628
        }
 
629
        else if(PyString_Check(fileobj) || PyUnicode_Check(fileobj))
 
630
        {
 
631
                if(!PyArg_ParseTuple(args, "si", &filename, &fontsize))
 
632
                        return NULL;
 
633
        }
 
634
        else
 
635
                return RAISE(PyExc_TypeError, "font name must be string or None");
 
636
 
 
637
        Py_BEGIN_ALLOW_THREADS
 
638
        font = TTF_OpenFont(filename, fontsize);
 
639
        Py_END_ALLOW_THREADS
 
640
        
 
641
        if(!font)
 
642
                return RAISE(PyExc_RuntimeError, SDL_GetError());
 
643
 
 
644
        fontobj = PyFont_New(font);
 
645
        if(!fontobj)
 
646
                TTF_CloseFont(font);
 
647
        return fontobj;
 
648
}
 
649
 
 
650
 
 
651
 
 
652
static PyMethodDef font_builtins[] =
 
653
{
 
654
        { "__PYGAMEinit__", font_autoinit, 1, doc_init },
 
655
        { "init", font_init, 1, doc_init },
 
656
        { "quit", font_quit, 1, doc_quit },
 
657
        { "get_init", get_init, 1, doc_get_init },
 
658
 
 
659
        { "Font", Font, 1, doc_Font },
 
660
 
 
661
        { NULL, NULL }
 
662
};
 
663
 
 
664
 
 
665
 
 
666
static PyObject* PyFont_New(TTF_Font* font)
 
667
{
 
668
        PyFontObject* fontobj;
 
669
        
 
670
        if(!font)
 
671
                return RAISE(PyExc_RuntimeError, "unable to load font.");
 
672
 
 
673
        fontobj = PyObject_NEW(PyFontObject, &PyFont_Type);
 
674
        if(fontobj)
 
675
                fontobj->font = font;
 
676
 
 
677
        return (PyObject*)fontobj;
 
678
}
 
679
 
 
680
 
 
681
 
 
682
    /*DOC*/ static char doc_pygame_font_MODULE[] =
 
683
    /*DOC*/    "The font module allows for rendering TrueType fonts into a new\n"
 
684
    /*DOC*/    "Surface object. This module is optional and requires SDL_ttf as a\n"
 
685
    /*DOC*/    "dependency. You may want to check for pygame.font to import and\n"
 
686
    /*DOC*/    "initialize before attempting to use the module.\n"
 
687
    /*DOC*/    "\n"
 
688
    /*DOC*/    "Most of the work done with fonts are done by using the actual\n"
 
689
    /*DOC*/    "Font objects. The module by itself only has routines to\n"
 
690
    /*DOC*/    "initialize the module and create Font objects with\n"
 
691
    /*DOC*/    "pygame.font.Font().\n"
 
692
    /*DOC*/ ;
 
693
 
 
694
PYGAME_EXPORT
 
695
void initfont(void)
 
696
{
 
697
        PyObject *module, *dict, *apiobj;
 
698
        static void* c_api[PYGAMEAPI_FONT_NUMSLOTS];
 
699
 
 
700
        PyFONT_C_API[0] = PyFONT_C_API[0]; /*clean an unused warning*/
 
701
 
 
702
        PyType_Init(PyFont_Type);
 
703
 
 
704
    /* create the module */
 
705
        module = Py_InitModule3("font", font_builtins, doc_pygame_font_MODULE);
 
706
        dict = PyModule_GetDict(module);
 
707
        self_module = module;
 
708
 
 
709
        PyDict_SetItemString(dict, "FontType", (PyObject *)&PyFont_Type);
 
710
 
 
711
        /* export the c api */
 
712
        c_api[0] = &PyFont_Type;
 
713
        c_api[1] = PyFont_New;
 
714
        c_api[2] = &font_initialized;
 
715
        apiobj = PyCObject_FromVoidPtr(c_api, NULL);
 
716
        PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
 
717
        Py_DECREF(apiobj);
 
718
 
 
719
        /*imported needed apis*/
 
720
        import_pygame_base();
 
721
        import_pygame_surface();
 
722
}
 
723