Source

pygame / src / font.c

Full commit
ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 


ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume d38f864 
ahvezda 99d32cc 
pygame ae12316 
ahvezda 99d32cc 

pygame 4f34da3 
pygame c48cde4 
pygame ae12316 
ahvezda 99d32cc 
Lenard Lindstrom ebd5456 
lorenz 301bc61 
pygame 7df4b80 
ahvezda 99d32cc 
Lenard Lindstrom ebd5456 
illume 8749d7c 
ahvezda 99d32cc 


illume 9f910ed 





Lenard Lindstrom 87155b4 


illume 9f910ed 

bob 951a91f 
illume 8749d7c 
Lenard Lindstrom ebd5456 
illume 8749d7c 




Lenard Lindstrom ebd5456 


bob 951a91f 
illume 8749d7c 


bob 951a91f 
illume 8749d7c 


bob 951a91f 
illume 8749d7c 


bob 951a91f 
Lenard Lindstrom ebd5456 









illume 8749d7c 
illume 9f910ed 

illume 8749d7c 

bob f384e54 

Lenard Lindstrom ebd5456 
bob f384e54 
bob 9ee5c22 
illume 8749d7c 



bob 951a91f 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 




ahvezda 99d32cc 


illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 


ahvezda 99d32cc 
illume 8749d7c 


pygame 4b4bcf4 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 


illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 





ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 


illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 




ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 





ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 




ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 





ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 


ahvezda 99d32cc 

illume 8749d7c 




ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 





ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 











ahvezda 99d32cc 
illume 8749d7c 


illume d1ad733 
marcus c4c38b9 
illume 8749d7c 





marcus c4c38b9 
illume 8749d7c 












illume d1ad733 
illume 8749d7c 


ahvezda 99d32cc 
illume 8749d7c 









illume d38f864 
illume 8749d7c 






Lenard Lindstrom ebd5456 
ahvezda 99d32cc 
illume 8749d7c 








pygame e11c6ad 
illume 8749d7c 

Lenard Lindstrom ebd5456 
illume 8749d7c 
Lenard Lindstrom ebd5456 
illume d1ad733 
illume 8749d7c 











illume d1ad733 
illume 8749d7c 

illume d1ad733 
illume 8749d7c 






illume d1ad733 
illume 8749d7c 



ahvezda 99d32cc 

illume 8749d7c 





ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 



Lenard Lindstrom ebd5456 
ahvezda 99d32cc 
illume 8749d7c 


Lenard Lindstrom ebd5456 
illume 8749d7c 
Lenard Lindstrom ebd5456 
illume 8749d7c 



pygame 9d8ed32 
illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 

illume 7b1c1b7 












ahvezda 99d32cc 
illume 8749d7c 
illume 7b1c1b7 






Lenard Lindstrom ebd5456 

illume 7b1c1b7 








Lenard Lindstrom ebd5456 
illume 7b1c1b7 



illume 8749d7c 
illume 7b1c1b7 









illume 8749d7c 



illume 7b1c1b7 
illume 8749d7c 



illume 7b1c1b7 











illume 8749d7c 



illume 7b1c1b7 
illume 8749d7c 



illume 7b1c1b7 



ahvezda 99d32cc 
pygame 7df4b80 
ahvezda 99d32cc 
illume 8749d7c 







ahvezda 99d32cc 
illume 8749d7c 








ahvezda 99d32cc 
illume 8749d7c 


ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 




ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

Lenard Lindstrom ebd5456 
ahvezda 99d32cc 

illume 8749d7c 





illume 9f910ed 
illume 8749d7c 


ahvezda 99d32cc 
illume 8749d7c 




ahvezda 99d32cc 
illume 8749d7c 
bob 9ee5c22 
illume 8749d7c 

pygame 7df4b80 
illume 8749d7c 

Lenard Lindstrom ebd5456 
illume 8749d7c 



illume 9f910ed 
illume 8749d7c 

Lenard Lindstrom ebd5456 
illume 8749d7c 




illume 9f910ed 
Lenard Lindstrom ebd5456 









illume 8749d7c 
illume 9f910ed 
Lenard Lindstrom ebd5456 
illume 9f910ed 
Lenard Lindstrom ebd5456 


illume 9f910ed 
illume 8749d7c 



Lenard Lindstrom 6a2f740 

Lenard Lindstrom ebd5456 
Lenard Lindstrom 6a2f740 
Lenard Lindstrom ebd5456 
Lenard Lindstrom 6a2f740 
illume 8749d7c 
Lenard Lindstrom 6a2f740 

Lenard Lindstrom ebd5456 
illume 8749d7c 
Lenard Lindstrom 6a2f740 

illume 8749d7c 






illume 9f910ed 
illume 8749d7c 







Lenard Lindstrom ebd5456 
illume 8749d7c 






Lenard Lindstrom ebd5456 
illume 8749d7c 

pygame 7df4b80 
illume 8749d7c 


Lenard Lindstrom ebd5456 
illume 8749d7c 
pygame 7df4b80 
Lenard Lindstrom ebd5456 
illume 8749d7c 

Lenard Lindstrom ebd5456 

Lenard Lindstrom 6a2f740 
Lenard Lindstrom ebd5456 
ahvezda 99d32cc 

pygame 4c398dd 
ahvezda 99d32cc 
Lenard Lindstrom ebd5456 
illume 8749d7c 


































ahvezda 99d32cc 

illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 

pygame 81809b2 
Lenard Lindstrom 87155b4 
pygame 81809b2 

Lenard Lindstrom ebd5456 
ahvezda 99d32cc 
illume 8749d7c 







ahvezda 99d32cc 



illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 
pygame 4c398dd 
illume 8749d7c 


pygame 7df4b80 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 

Lenard Lindstrom ebd5456 
illume 8749d7c 


ahvezda 99d32cc 
Lenard Lindstrom ebd5456 










illume 8749d7c 
ahvezda 99d32cc 
Lenard Lindstrom 65e001d 




Lenard Lindstrom ebd5456 
Lenard Lindstrom 65e001d 


Lenard Lindstrom ebd5456 
Lenard Lindstrom 65e001d 


Lenard Lindstrom ebd5456 
Lenard Lindstrom 65e001d 


Lenard Lindstrom ebd5456 
Lenard Lindstrom 65e001d 


Lenard Lindstrom ebd5456 



pygame 92ba355 
Lenard Lindstrom ebd5456 


illume 9f910ed 


Lenard Lindstrom ebd5456 



illume 5acb34f 
illume 8749d7c 
Lenard Lindstrom ebd5456 







illume 8749d7c 
Lenard Lindstrom ebd5456 






ahvezda 99d32cc 
illume 8749d7c 




Lenard Lindstrom ebd5456 


Lenard Lindstrom 65e001d 
Lenard Lindstrom ebd5456 
zeroth cdea134 
Lenard Lindstrom ebd5456 

Lenard Lindstrom 65e001d 
Lenard Lindstrom ebd5456 
ahvezda 99d32cc 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*
  pygame - Python Game Library
  Copyright (C) 2000-2001  Pete Shinners

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Pete Shinners
  pete@shinners.org
*/


/*
 *  font module for pygame
 */
#define PYGAMEAPI_FONT_INTERNAL
#include "font.h"
#include <stdio.h>
#include <string.h>
#include "pygame.h"
#include "pgcompat.h"
#include "doc/font_doc.h"
#include "structmember.h"

static PyTypeObject PyFont_Type;
static PyObject* PyFont_New (TTF_Font*);
#define PyFont_Check(x) ((x)->ob_type == &PyFont_Type)

static int font_initialized = 0;
#ifndef __SYMBIAN32__
static const char const *font_defaultname = "freesansbold.ttf";
static const char const *pkgdatamodule_name = "pygame.pkgdata";
static const char const *resourcefunc_name = "getResource";
#else
/// Symbian GCCE does not like the second const
static const char *font_defaultname = "freesansbold.ttf";
static const char *pkgdatamodule_name = "pygame.pkgdata";
static const char *resourcefunc_name = "getResource";
#endif


static PyObject*
font_resource (const char *filename)
{
    PyObject* load_basicfunc = NULL;
    PyObject* pkgdatamodule = NULL;
    PyObject* resourcefunc = NULL;
    PyObject* result = NULL;
#if PY3
    PyObject* tmp;
#endif

    pkgdatamodule = PyImport_ImportModule (pkgdatamodule_name);
    if (!pkgdatamodule)
        goto font_resource_end;

    resourcefunc = PyObject_GetAttrString (pkgdatamodule, resourcefunc_name);
    if (!resourcefunc)
        goto font_resource_end;

    result = PyObject_CallFunction (resourcefunc, "s", filename);
    if (!result)
        goto font_resource_end;

#if PY3
    tmp = PyObject_GetAttrString (result, "name");
    if (tmp != NULL) {
        Py_DECREF (result);
        result = tmp;
    }
    else {
        PyErr_Clear ();
    }
#else
    if (PyFile_Check (result))
    {		
        PyObject *tmp = PyFile_Name (result);        
        Py_INCREF (tmp);
        Py_DECREF (result);
        result = tmp;
    }
#endif

font_resource_end:
    Py_XDECREF (pkgdatamodule);
    Py_XDECREF (resourcefunc);
    Py_XDECREF (load_basicfunc);
    return result;
}

static void
font_autoquit (void)
{
    if (font_initialized)
    {
        font_initialized = 0;
        TTF_Quit ();
    }
}


static PyObject*
font_autoinit (PyObject* self)
{
    if (!font_initialized)
    {
        PyGame_RegisterQuit (font_autoquit);

        if (TTF_Init ())
            return PyInt_FromLong (0);
        font_initialized = 1;

    }
    return PyInt_FromLong (font_initialized);
}

static PyObject*
fontmodule_quit (PyObject* self)
{
    font_autoquit ();
    Py_RETURN_NONE;
}


static PyObject*
fontmodule_init (PyObject* self)
{
    PyObject* result;
    int istrue;

    result = font_autoinit (self);
    istrue = PyObject_IsTrue (result);
    Py_DECREF (result);
    if (!istrue)
        return RAISE (PyExc_SDLError, SDL_GetError ());
    Py_RETURN_NONE;
}

static PyObject*
get_init (PyObject* self)
{
    return PyInt_FromLong (font_initialized);
}

/* font object methods */
static PyObject*
font_get_height (PyObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);
    return PyInt_FromLong (TTF_FontHeight (font));
}

static PyObject*
font_get_descent (PyObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);
    return PyInt_FromLong (TTF_FontDescent (font));
}

static PyObject*
font_get_ascent (PyObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);
    return PyInt_FromLong (TTF_FontAscent (font));
}

static PyObject*
font_get_linesize (PyObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);
    return PyInt_FromLong (TTF_FontLineSkip (font));
}

static PyObject*
font_get_bold (PyObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);
    return PyInt_FromLong ((TTF_GetFontStyle (font) & TTF_STYLE_BOLD) != 0);
}

static PyObject*
font_set_bold (PyObject* self, PyObject* args)
{
    TTF_Font* font = PyFont_AsFont (self);
    int style, val;

    if (!PyArg_ParseTuple (args, "i", &val))
        return NULL;

    style = TTF_GetFontStyle (font);
    if (val)
        style |= TTF_STYLE_BOLD;
    else
        style &= ~TTF_STYLE_BOLD;
    TTF_SetFontStyle (font, style);

    Py_RETURN_NONE;
}

static PyObject*
font_get_italic (PyObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);
    return PyInt_FromLong ((TTF_GetFontStyle (font) & TTF_STYLE_ITALIC) != 0);
}

static PyObject*
font_set_italic (PyObject* self, PyObject* args)
{
    TTF_Font* font = PyFont_AsFont (self);
    int style, val;

    if (!PyArg_ParseTuple (args, "i", &val))
        return NULL;

    style = TTF_GetFontStyle (font);
    if(val)
        style |= TTF_STYLE_ITALIC;
    else
        style &= ~TTF_STYLE_ITALIC;
    TTF_SetFontStyle (font, style);

    Py_RETURN_NONE;
}

static PyObject*
font_get_underline (PyObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);
    return PyInt_FromLong
        ((TTF_GetFontStyle (font) & TTF_STYLE_UNDERLINE) != 0);
}

static PyObject*
font_set_underline (PyObject* self, PyObject* args)
{
    TTF_Font* font = PyFont_AsFont (self);
    int style, val;

    if (!PyArg_ParseTuple (args, "i", &val))
        return NULL;

    style = TTF_GetFontStyle (font);
    if(val)
        style |= TTF_STYLE_UNDERLINE;
    else
        style &= ~TTF_STYLE_UNDERLINE;
    TTF_SetFontStyle (font, style);

    Py_RETURN_NONE;
}

static PyObject*
font_render (PyObject* self, PyObject* args)
{
    TTF_Font* font = PyFont_AsFont (self);
    int aa;
    PyObject* text, *final;
    PyObject* fg_rgba_obj, *bg_rgba_obj = NULL;
    Uint8 rgba[4];
    SDL_Surface* surf;
    SDL_Color foreg, backg;
    int just_return;
    just_return = 0;

    if (!PyArg_ParseTuple (args, "OiO|O", &text, &aa, &fg_rgba_obj,
                           &bg_rgba_obj))
        return NULL;

    if (!RGBAFromColorObj (fg_rgba_obj, rgba))
        return RAISE (PyExc_TypeError, "Invalid foreground RGBA argument");
    foreg.r = rgba[0];
    foreg.g = rgba[1];
    foreg.b = rgba[2];
    if (bg_rgba_obj)
    {
        if (!RGBAFromColorObj (bg_rgba_obj, rgba))
            return RAISE (PyExc_TypeError, "Invalid background RGBA argument");
        backg.r = rgba[0];
        backg.g = rgba[1];
        backg.b = rgba[2];
        backg.unused = 0;
    }
    else
    {
        backg.r = 0;
        backg.g = 0;
        backg.b = 0;
        backg.unused = 0;
    }

    if (!PyObject_IsTrue (text))
    {
        int height = TTF_FontHeight (font);

        surf = SDL_CreateRGBSurface (SDL_SWSURFACE, 1, height, 32,
                                     0xff<<16, 0xff<<8, 0xff, 0);
        if (!surf)
            return RAISE (PyExc_SDLError, "SDL_CreateRGBSurface failed");

        if (bg_rgba_obj)
        {

            Uint32 c = SDL_MapRGB (surf->format, backg.r, backg.g, backg.b);
            SDL_FillRect (surf, NULL, c);
        }
        else
            SDL_SetColorKey (surf, SDL_SRCCOLORKEY, 0);
        just_return = 1;
    }
    else if (PyUnicode_Check (text))
    {
        PyObject* strob = PyUnicode_AsEncodedString (text, "utf-8", "replace");
        char *astring = Bytes_AsString (strob);

        if (aa)
        {
            if (!bg_rgba_obj)
                surf = TTF_RenderUTF8_Blended (font, astring, foreg);
            else
                surf = TTF_RenderUTF8_Shaded (font, astring, foreg, backg);
        }
        else
            surf = TTF_RenderUTF8_Solid (font, astring, foreg);

        Py_DECREF (strob);
    }
    else if (Bytes_Check (text))
    {
        char* astring = Bytes_AsString (text);

        if (aa)
        {
            if (!bg_rgba_obj)
                surf = TTF_RenderText_Blended (font, astring, foreg);
            else
                surf = TTF_RenderText_Shaded (font, astring, foreg, backg);
        }
        else
            surf = TTF_RenderText_Solid (font, astring, foreg);
    }
    else
        return RAISE (PyExc_TypeError, "text must be a string or unicode");

    if (!surf)
        return RAISE (PyExc_SDLError, TTF_GetError());

    if (!aa && bg_rgba_obj && !just_return) /*turn off transparancy*/
    {
        SDL_SetColorKey (surf, 0, 0);
        surf->format->palette->colors[0].r = backg.r;
        surf->format->palette->colors[0].g = backg.g;
        surf->format->palette->colors[0].b = backg.b;
    }

    final = PySurface_New (surf);
    if (!final)
        SDL_FreeSurface (surf);
    return final;
}

static PyObject*
font_size (PyObject* self, PyObject* args)
{
    TTF_Font* font = PyFont_AsFont (self);
    int w, h;
    PyObject* text;

    if (!PyArg_ParseTuple (args, "O", &text))
        return NULL;

    if (PyUnicode_Check (text))
    {
        //PyObject* strob = PyUnicode_AsEncodedObject(text, "utf-8", "replace");
        PyObject* strob = PyUnicode_AsEncodedString (text, "utf-8", "replace");
        char *string = Bytes_AsString (strob);

        TTF_SizeUTF8 (font, string, &w, &h);
        Py_DECREF (strob);
    }
    else if (Bytes_Check (text))
    {
        char* string = Bytes_AsString (text);
        TTF_SizeText (font, string, &w, &h);
    }
    else
        return RAISE (PyExc_TypeError, "text must be a string or unicode");

    return Py_BuildValue ("(ii)", w, h);
}

static PyObject*
font_metrics (PyObject* self, PyObject* args)
{
    TTF_Font *font = PyFont_AsFont (self);
    PyObject *list;
    PyObject *textobj;
    int length;
    int i;
    int minx;
    int maxx;
    int miny;
    int maxy;
    int advance;
    void *buf;
    int isunicode = 0;

    if (!PyArg_ParseTuple (args, "O", &textobj))
        return NULL;

    if (PyUnicode_Check (textobj))
    {
        buf = PyUnicode_AsUnicode (textobj);
        isunicode = 1;
    }
    else if (Bytes_Check (textobj))
        buf = Bytes_AsString (textobj);
    else
        return RAISE (PyExc_TypeError, "text must be a string or unicode");

    if (!buf)
        return NULL;

    if (isunicode)
        length = PyUnicode_GetSize (textobj);
    else
        length = Bytes_Size (textobj);

    if (length == 0)
        Py_RETURN_NONE;

    list = PyList_New (length);
    if (isunicode)
    {
        for (i = 0; i < length; i++)
        {
            /* TODO:
             * TTF_GlyphMetrics() seems to returns a value for any character,
             * using the default invalid character, if the char is not found.
             */
            if (TTF_GlyphMetrics (font, (Uint16) ((Py_UNICODE*) buf)[i], &minx,
                                  &maxx, &miny, &maxy, &advance) == -1)
            {
                Py_INCREF (Py_None);
                PyList_SetItem (list, i, Py_None); /* No matching metrics. */
            }
            else
            {
                PyList_SetItem (list, i, Py_BuildValue
                                ("(iiiii)", minx, maxx, miny, maxy, advance));
            }
        }
    }
    else
    {
        for (i = 0; i < length; i++)
        {
            /* TODO:
             * TTF_GlyphMetrics() seems to returns a value for any character,
             * using the default invalid character, if the char is not found.
             */
            if (TTF_GlyphMetrics (font, (Uint16) ((char*) buf)[i], &minx,
                                  &maxx, &miny, &maxy, &advance) == -1)
            {
                Py_INCREF (Py_None);
                PyList_SetItem (list, i, Py_None); /* No matching metrics. */
            }
            else
            {
                PyList_SetItem (list, i, Py_BuildValue
                                ("(iiiii)", minx, maxx, miny, maxy, advance));
            }
        }
    }
    return list;
}

static PyMethodDef font_methods[] =
{
    { "get_height", (PyCFunction) font_get_height, METH_NOARGS,
      DOC_FONTGETHEIGHT },
    { "get_descent", (PyCFunction) font_get_descent, METH_NOARGS,
      DOC_FONTGETDESCENT },
    { "get_ascent", (PyCFunction) font_get_ascent, METH_NOARGS,
      DOC_FONTGETASCENT },
    { "get_linesize", (PyCFunction) font_get_linesize, METH_NOARGS,
      DOC_FONTGETLINESIZE },

    { "get_bold", (PyCFunction) font_get_bold, METH_NOARGS,
      DOC_FONTGETBOLD },
    { "set_bold", font_set_bold, METH_VARARGS, DOC_FONTSETBOLD },
    { "get_italic", (PyCFunction) font_get_italic, METH_NOARGS,
      DOC_FONTGETITALIC },
    { "set_italic", font_set_italic, METH_VARARGS, DOC_FONTSETITALIC },
    { "get_underline", (PyCFunction) font_get_underline, METH_NOARGS,
      DOC_FONTGETUNDERLINE },
    { "set_underline", font_set_underline, METH_VARARGS, DOC_FONTSETUNDERLINE },

    { "metrics", font_metrics, METH_VARARGS, DOC_FONTMETRICS },
    { "render", font_render, METH_VARARGS, DOC_FONTRENDER },
    { "size", font_size, METH_VARARGS, DOC_FONTSIZE },

    { NULL, NULL, 0, NULL }
};

/*font object internals*/
static void
font_dealloc (PyFontObject* self)
{
    TTF_Font* font = PyFont_AsFont (self);

    if (font && font_initialized)
        TTF_CloseFont (font);

    if (self->weakreflist)
        PyObject_ClearWeakRefs ((PyObject*) self);
    Py_TYPE(self)->tp_free ((PyObject*) self);
}

static int
font_init (PyFontObject *self, PyObject *args, PyObject *kwds)
{
    int fontsize;
    TTF_Font* font = NULL;
    PyObject* fileobj;
    
    self->font = NULL;
    if (!PyArg_ParseTuple (args, "Oi", &fileobj, &fontsize))
        return -1;

    if (!font_initialized)
    {
        RAISE (PyExc_SDLError, "font not initialized");
        return -1;
    }

    Py_INCREF (fileobj);

    if (fontsize <= 1)
        fontsize = 1;

    if (fileobj == Py_None)
    {

        fileobj = font_resource (font_defaultname);
        if (!fileobj)
        {
            char error[1024];
            PyOS_snprintf (error, 1024, "default font not found '%s'",
                      font_defaultname);
            RAISE (PyExc_RuntimeError, error);
            goto error;
        }
        fontsize = (int) (fontsize * .6875);
        if (fontsize <= 1)
            fontsize = 1;
    }
     
    if (PyUnicode_Check (fileobj)) {
        PyObject* tmp = PyUnicode_AsASCIIString (fileobj);

        if (tmp == NULL) {
            goto error;
        }
        fileobj = tmp;
    }

    if (Bytes_Check (fileobj))
    {
        FILE* test;        
        char* filename = Bytes_AsString (fileobj);
       		
        if (!filename) {
            goto error;
        }
                
        /*check if it is a valid file, else SDL_ttf segfaults*/
        test = fopen (filename, "rb");
        if(!test)
        {
            PyObject *tmp = NULL;

            if (!strcmp (filename, font_defaultname)) {
                tmp = font_resource (font_defaultname);
            }
            if (!tmp)
            {
                PyErr_SetString (PyExc_IOError,
                                 "unable to read font filename");
                goto error;
            }
            Py_DECREF (fileobj);
            fileobj = tmp;
        }
        else
        {
            fclose (test);
            Py_BEGIN_ALLOW_THREADS;
            font = TTF_OpenFont(filename, fontsize);
            Py_END_ALLOW_THREADS;
        }	
    }
    if (!font)
    {
#ifdef TTF_MAJOR_VERSION
        SDL_RWops *rw;
        rw = RWopsFromPython (fileobj);
        if (!rw)
        {
            goto error;
        }
        Py_BEGIN_ALLOW_THREADS;
        font = TTF_OpenFontIndexRW (rw, 1, fontsize, 0);
        Py_END_ALLOW_THREADS;
#else
        RAISE (PyExc_NotImplementedError,
               "nonstring fonts require SDL_ttf-2.0.6");
        goto error;
#endif
    }

    if (!font)
    {
        RAISE (PyExc_RuntimeError, SDL_GetError ());
        goto error;
    }

    Py_DECREF (fileobj);
    self->font = font;
    return 0;

error:
    Py_DECREF (fileobj);
    return -1;
}

static PyTypeObject PyFont_Type =
{
    TYPE_HEAD (NULL, 0)
    "pygame.font.Font",
    sizeof(PyFontObject),
    0,
    (destructor)font_dealloc,
    0,
    0, /*getattr*/
    0,
    0,
    0,
    0,
    NULL,
    0,
    (hashfunc)NULL,
    (ternaryfunc)NULL,
    (reprfunc)NULL,
    0L,0L,0L,
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
    DOC_PYGAMEFONTFONT, /* Documentation string */
    0,					/* tp_traverse */
    0,					/* tp_clear */
    0,					/* tp_richcompare */
    offsetof(PyFontObject, weakreflist),    /* tp_weaklistoffset */
    0,					/* tp_iter */
    0,					/* tp_iternext */
    font_methods,			        /* tp_methods */
    0,				        /* tp_members */
    0,				        /* tp_getset */
    0,					/* tp_base */
    0,					/* tp_dict */
    0,					/* tp_descr_get */
    0,					/* tp_descr_set */
    0,					/* tp_dictoffset */
    (initproc)font_init,			/* tp_init */
    0,					/* tp_alloc */
    0,	                /* tp_new */
};

//PyType_GenericNew,	                /* tp_new */

/*font module methods*/
static PyObject*
get_default_font (PyObject* self)
{
    return Text_FromUTF8 (font_defaultname);
}

static PyMethodDef _font_methods[] =
{
    { "__PYGAMEinit__", (PyCFunction) font_autoinit, METH_NOARGS,
      "auto initialize function for font" },
    { "init", (PyCFunction) fontmodule_init, METH_NOARGS, DOC_PYGAMEFONTINIT },
    { "quit", (PyCFunction) fontmodule_quit, METH_NOARGS, DOC_PYGAMEFONTQUIT },
    { "get_init", (PyCFunction) get_init, METH_NOARGS, DOC_PYGAMEFONTGETINIT },
    { "get_default_font", (PyCFunction) get_default_font, METH_NOARGS,
      DOC_PYGAMEFONTGETDEFAULTFONT },
    { NULL, NULL, 0, NULL }
};



static PyObject*
PyFont_New (TTF_Font* font)
{
    PyFontObject* fontobj;

    if (!font)
        return RAISE (PyExc_RuntimeError, "unable to load font.");
    fontobj = (PyFontObject *) PyFont_Type.tp_new (&PyFont_Type, NULL, NULL);

    if (fontobj)
        fontobj->font = font;

    return (PyObject*) fontobj;
}

MODINIT_DEFINE (font)
{
    PyObject *module, *apiobj;
    static void* c_api[PYGAMEAPI_FONT_NUMSLOTS];

#if PY3
    static struct PyModuleDef _module = {
        PyModuleDef_HEAD_INIT,
        "font",
        DOC_PYGAMEFONT,
        -1,
        _font_methods,
        NULL, NULL, NULL, NULL
    };
#endif

    PyFONT_C_API[0] = PyFONT_C_API[0]; /*clean an unused warning*/

    /* imported needed apis; Do this first so if there is an error
       the module is not loaded.
    */
    import_pygame_base ();
    if (PyErr_Occurred ()) {
	MODINIT_ERROR;
    }
    import_pygame_color ();
    if (PyErr_Occurred ()) {
	MODINIT_ERROR;
    }
    import_pygame_surface ();
    if (PyErr_Occurred ()) {
	MODINIT_ERROR;
    }
    import_pygame_rwobject ();
    if (PyErr_Occurred ()) {
	MODINIT_ERROR;
    }

    /* type preparation */
    if (PyType_Ready (&PyFont_Type) < 0) {
        MODINIT_ERROR;
    }
    PyFont_Type.tp_new = PyType_GenericNew;

#if PY3
    module = PyModule_Create (&_module);
#else
    module = Py_InitModule3 (MODPREFIX "font", 
                             _font_methods, 
                             DOC_PYGAMEFONT);
#endif
    if (module == NULL) {
        MODINIT_ERROR;
    }

    Py_INCREF ((PyObject*) &PyFont_Type);
    if (PyModule_AddObject (module,
                            "FontType",
                            (PyObject *) &PyFont_Type) == -1) {
        Py_DECREF ((PyObject *) &PyFont_Type);
        DECREF_MOD (module);
        MODINIT_ERROR;
    }

    Py_INCREF ((PyObject*) &PyFont_Type);
    if (PyModule_AddObject (module,
                            "Font",
                            (PyObject *) &PyFont_Type) == -1) {
        Py_DECREF ((PyObject *) &PyFont_Type);
        DECREF_MOD (module);
        MODINIT_ERROR;
    }

    /* export the c api */
    c_api[0] = &PyFont_Type;
    c_api[1] = PyFont_New;
    c_api[2] = &font_initialized;
    apiobj = PyCObject_FromVoidPtr (c_api, NULL);
    if (apiobj == NULL) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    if (PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj) == -1) {
        Py_DECREF (apiobj);
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    MODINIT_RETURN (module);
}