File: g_font.c

package info (click to toggle)
fenix 0.92a.dfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,492 kB
  • sloc: ansic: 42,357; sh: 3,474; perl: 527; makefile: 180; cpp: 19
file content (882 lines) | stat: -rw-r--r-- 19,382 bytes parent folder | download | duplicates (4)
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
 *  Fenix - Videogame compiler/interpreter
 *  Current release       : FENIX - PROJECT 1.0 - R 0.84
 *  Last stable release   :
 *  Project documentation : http://fenix.divsite.net
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *  Copyright � 1999 Jos� Luis Cebri�n Pag�e
 *  Copyright � 2002 Fenix Team
 *
 */

/*
 * FILE        : g_font.c
 * DESCRIPTION : Font loading and manipulation
 *
 * HISTORY:	0.82 - First version (used to be in g_texts.c)
 */

#ifdef TARGET_BEOS
#include <posix/assert.h>
#else
#include <assert.h>
#endif

#include <stdlib.h>
#include <string.h>

#ifdef USE_GETTEXT
#include <libintl.h>
#define _(String) gettext (String)
#else
#define _(String) String
#endif

#include "fxi.h"

FONT * fonts[256] = { 0 } ;
int    font_count = 0 ;		/* Fuente 0 reservada para sistema */

/*
 *  FUNCTION : gr_font_new
 *
 *  Create a new font, with no characters in it.
 *  The font uses the MS-DOS charset and 8bpp by default.
 *
 *  PARAMS :
 *		None
 *
 *  RETURN VALUE :
 *      Code of the new font or -1 if error
 *      The font data is in the global array fonts[index]
 *
 */

int gr_font_new ()
{
	FONT * f = (FONT *)malloc(sizeof(FONT));

	if (f == NULL)
	{
		gr_error (_("gr_font_new: out of memory"));
		return -1;
	}
	if (font_count == 255)
	{
		gr_error (_("gr_font_new: too many fonts"));
		return -1;
	}

	memset (f, 0, sizeof(FONT));
	f->charset = CHARSET_CP850;
	f->bpp = 8;
	f->maxwidth = 0;
	f->maxheight = 0;

	fonts[font_count] = f ;
	return font_count++ ;
}

/*
 *  FUNCTION : gr_font_newfrombitmap
 *
 *  Create a new font using a bitmap as source. The bitmap should be
 *  in black and white (1 bit per pixel) with a fixed character size
 *  and a character width of 8, 16, 24...
 *
 *  PARAMS :
 *		chardata		Pointer to the bitmap
 *		width			Width of each character, in bits (pixels)
 *		height			Height of each character
 *		options			Can be 0 or a combination of the following flags:
 *
 *	NFB_FIXEDWIDTH		Create a fixed width font
 *						(the default is a propotional width one)
 *
 *  RETURN VALUE :
 *      1 if MMX available, 0 otherwise
 *
 */

static void align_bitmap_char_left (unsigned char *data, int width, int height);
static int get_bitmap_char_width (unsigned char *data, int width, int height);

int gr_font_newfrombitmap (char * chardata, int width, int height, int options)
{
	FONT * f ;
	char * ptr, * charptr ;
	int i, y, id ;
	int charsize ;
	int linesize ;
	GRAPH * bitmap;

	id = gr_font_new() ;
	if (id == -1) return -1;
	f = fonts[id];
	f->bpp = 1;

	charsize = width/8 * height;
	linesize = width/8;

	for (charptr = chardata, i = 0 ; i < 256 ; i++, charptr += charsize)
	{
		if (options != NFB_FIXEDWIDTH)
			align_bitmap_char_left (charptr, width, height) ;

		bitmap = bitmap_new (i, width, height, 1, 1);
		if (bitmap == NULL)
		{
			gr_error (_("gr_font_newfrombitmap: out of memory"));
			return id;
		}

		f->glyph[i].bitmap = bitmap;
		f->glyph[i].xoffset = 0;
		f->glyph[i].yoffset = 0;

		if (options != NFB_FIXEDWIDTH)
			f->glyph[i].xadvance = get_bitmap_char_width (charptr, width, height);
		else
			f->glyph[i].xadvance = width;

		bitmap_add_cpoint (bitmap, 0, 0) ;
		ptr = bitmap->data ;
		for (y = 0 ; y < height ; y++, ptr += bitmap->pitch)
			memcpy (ptr, charptr + linesize*y, linesize);

		if (bitmap->modified > 0) bitmap->modified = 0 ;
		bitmap->info_flags = 0 ;
	}

	/* Set a reasonable size for the space */

	f->glyph[32].xadvance = 5 ;
	f->maxwidth = width;
	f->maxheight = height;

	return id ;
}

/* Utility function used by gr_new_fontfrombitmap to align characters */

static void align_bitmap_char_left (unsigned char *data, int width, int height)
{
	int        leftest, n, c ;
	static int leftest_table[256] = { 0 } ;

	if (leftest_table[0] == 0)
	{
		for (n = 0 ; n < 256 ; n++)
		{
			if (n & 0x80) leftest_table[n] = 0 ;
			else if (n & 0x40) leftest_table[n] = 1 ;
			else if (n & 0x20) leftest_table[n] = 2 ;
			else if (n & 0x10) leftest_table[n] = 3 ;
			else if (n & 0x08) leftest_table[n] = 4 ;
			else if (n & 0x04) leftest_table[n] = 5 ;
			else if (n & 0x02) leftest_table[n] = 6 ;
			else if (n & 0x01) leftest_table[n] = 7 ;
			else               leftest_table[n] = 8 ;
		}
	}

	leftest = 8 ;
	for (n = 0 ; n < height ; n++)
	{
		for (c = 0 ; c < width ; c += 8)
		{
			if (leftest > c+leftest_table[data[(width*n+c)/8]])
				leftest = c+leftest_table[data[(width*n+c)/8]] ;
		}
	}

	if (leftest > 7)
	{
		if (width > leftest)
		{
			for (n = 0 ; n < height ; n++)
			{
				memmove (data + n*width/8, data + (n*width+leftest)/8,
					(width - leftest)/8);
			}
		}
		leftest &= 7;
	}

	for (n = 0 ; n < height*width/8 ; n++)
		data[n] <<= leftest ;
}

/* Utility function used by gr_new_fontfrombitmap to calculate char widths */

static int get_bitmap_char_width (unsigned char *data, int width, int height)
{
	int x, c, d, max = 0 ;

	while (height--)
	{
		for (x = 0 ; x < width ; x += 8)
		{
			c = *data++ ;
			for (d = 8 ; d > 0 ; d--, c >>= 1)
			{
				if (c & 0x01) break ;
			}
      		if (x*8+d > max) max = x*8+d ;
		}
	}
	return max+1 ;
}


/*
 *  FUNCTION : gr_font_systemfont
 *
 *  Create the system font. This function should be called once.
 *
 *  PARAMS :
 *		chardata		Pointer to the system font data
 *
 *  RETURN VALUE :
 *      Always returns 1
 *
 */

int gr_font_systemfont (char * chardata)
{
	int last_count = font_count ;
	if (fonts[0]) gr_font_destroy(0) ;
	font_count = 0 ;
	gr_font_newfrombitmap (chardata, 8, 8, 0);
	if (last_count) font_count = last_count ;

	return 1;
}


/*
 *  FUNCTION : gr_font_destroy
 *
 *  Destroy a font and all the internal bitmap data
 *
 *  PARAMS :
 *		fontid		ID of the font
 *
 *  RETURN VALUE :
 *      None
 *
 */

void gr_font_destroy (int fontid)
{
	int n ;

	if (fontid >= 0 && fontid < font_count)
	{
		if (!fonts[fontid]) return ;
		for (n = 0 ; n < 256 ; n++)
		{
			if (fonts[fontid]->glyph[n].bitmap)
				bitmap_destroy (fonts[fontid]->glyph[n].bitmap) ;
		}
		free (fonts[fontid]) ;
		fonts[fontid] = NULL ;
		while (font_count > 0 && fonts[font_count-1] == 0)
			font_count-- ;
	}
}


/*
 *  FUNCTION : gr_font_load
 *
 *  Load a font from a given file, in FNT (DIV) format
 *
 *  PARAMS :
 *		filename		Name of the file
 *
 *  RETURN VALUE :
 *      ID of the new font, or -1 if error
 *
 */

static int gr_font_loadfrom (file * fp);

int gr_font_load (char * filename)
{
	file * fp ;
	int result;

	fp = file_open (filename, "rb") ;
	if (!fp) return -1 ;
	result = gr_font_loadfrom(fp);
	file_close (fp);
	return result;
}

static int gr_font_loadfrom (file * fp)
{
	char header[8];
	int bpp;
	int types, i, id;
	Uint32 y;
	FONT * f;
	PALETTE * pal = NULL;

	struct
	{
		int	width ;
		int	height ;
		int	yoffset ;
		int	fileoffset ;
	}
	oldchardata[256];

	struct
	{
		int	width;
		int	height;
		int xadvance;
		int yadvance;
		int xoffset;
		int	yoffset;
		int	fileoffset ;
	}
	chardata[256] ;

	if (font_count == 256) return -1 ;

	/* Read the file header */

	if (!file_read(fp, header,8)) return -1;

	if (memcmp (header, "fnt\x1a\x0d\x0a", 7) != 0 &&
		memcmp (header, "fnx\x1a\x0d\x0a", 7) != 0)
	{
		gr_error (_("gr_font_load: unknown format"));
		return -1;
	}

	bpp = header[7];
	if (bpp == 0) bpp = 8;

	/* Read or ignore the palette */

	if (bpp == 8)
	    if (!(pal = gr_read_pal_with_gamma (fp))) return -1 ;

	/* Read the character data (detect old format) */

	if (header[2] == 'x')
	{
		if (!file_readSint32(fp, &types)) {
		    pal_destroy(pal);
			return -1 ;
		}
		if (!file_read(fp, chardata, sizeof(chardata))){
		    pal_destroy(pal);
		    return -1 ;
		}
		for (i = 0 ; i < 256 ; i++)
		{
			ARRANGE_DWORD (&chardata[i].width);
			ARRANGE_DWORD (&chardata[i].height);
			ARRANGE_DWORD (&chardata[i].xadvance);
			ARRANGE_DWORD (&chardata[i].yadvance);
			ARRANGE_DWORD (&chardata[i].xoffset);
			ARRANGE_DWORD (&chardata[i].yoffset);
			ARRANGE_DWORD (&chardata[i].fileoffset);
		}
	}
	else
	{
		if (!file_readSint32(fp, &types)) {
		    pal_destroy(pal);
			return -1 ;
		}
		if (!file_read(fp, oldchardata, sizeof(oldchardata))){
		    pal_destroy(pal);
		    return -1 ;
		}
		for (i = 0 ; i < 256 ; i++)
		{
			ARRANGE_DWORD (&oldchardata[i].width);
			ARRANGE_DWORD (&oldchardata[i].height);
			ARRANGE_DWORD (&oldchardata[i].yoffset);
			ARRANGE_DWORD (&oldchardata[i].fileoffset);

			chardata[i].width      = oldchardata[i].width;
			chardata[i].height     = oldchardata[i].height;
			chardata[i].xoffset    = 0;
			chardata[i].yoffset    = oldchardata[i].yoffset;
			chardata[i].xadvance   = oldchardata[i].width;
			chardata[i].yadvance   = oldchardata[i].height + oldchardata[i].yoffset;
			chardata[i].fileoffset = oldchardata[i].fileoffset;
		}
	}

	/* Create the font */

	id = gr_font_new() ;
	if (id == -1) {
	    pal_destroy(pal);
	    return -1 ;
	}

	f = fonts[id];
	assert (f != 0) ;

	if (header[2] == 'x')
	{
		f->bpp = header[7];
		f->charset = types;
	}
	else
	{
		f->bpp = 8;
		f->charset = CHARSET_CP850;
	}

	/* Load the character bitmaps */

	for (i = 0 ; i < 256 ; i++)
	{
		GRAPH * gr;
		Uint8 * ptr;

		f->glyph[i].xadvance = chardata[i].xadvance ;
		f->glyph[i].yadvance = chardata[i].yadvance ;

		if (chardata[i].fileoffset == 0 || chardata[i].width == 0 || chardata[i].height == 0) continue ;

		f->glyph[i].xoffset = chardata[i].xoffset ;
		f->glyph[i].yoffset = chardata[i].yoffset ;

		file_seek (fp, chardata[i].fileoffset, SEEK_SET) ;
		f->glyph[i].bitmap = gr = bitmap_new (i, chardata[i].width, chardata[i].height, f->bpp, 1) ;
		assert (gr) ;
		bitmap_add_cpoint (gr, 0, 0) ;
		gr->palette = pal;
		pal_use(pal);

		for (y = 0, ptr = gr->data ; y < gr->height ; y++, ptr += gr->pitch)
		{
			if (!file_read (fp, ptr, gr->widthb)) break ;
			if (gr->depth == 16) gr_convert16_565ToScreen ((Uint16 *)ptr, gr->width);
		}

		f->glyph[i].yoffset = chardata[i].yoffset ;
	}
	if (f->glyph[32].xadvance == 0) f->glyph[32].xadvance = 4 ;

    pal_destroy(pal); // Elimino la instancia inicial

	return id ;
}


/*
 *  FUNCTION : gr_font_save
 *
 *  Write a font to disk, in FNT/FNX format
 *
 *  PARAMS :
 *		fontid			ID of the font to save
 *		filename		Name of the file to create
 *
 *  RETURN VALUE :
 *      1 if succeded or 0 otherwise
 *
 */

int gr_font_save (int fontid, const char * filename)
{
	char     fullname[1024];
	char *   ptr;
	gzFile * file;
	int      n;
	Uint32   y;
	long     offset;
	Uint8 *  block = NULL ;
	Uint8 *  lineptr;

	FONT *   font;
	Uint8    header[8];
	struct
	{
		int	width;
		int	height;
		int xadvance;
		int yadvance;
		int xoffset;
		int	yoffset;
		int	fileoffset ;
	}
	chardata[256] ;
	int palette_saved = 0;

	if (fontid < 0 || fontid > 255 || !fonts[fontid])
	{
		gr_error (_("gr_font_save: invalid font"));
		return 0;
	}
	font = fonts[fontid];

	/* If the file does not have an extension, append ".FNT" */

	memset (fullname, 0, sizeof(fullname));
	strncpy (fullname, filename, 1000);
	ptr = fullname + strlen(fullname) - 1;
	while (ptr > fullname && !strchr("/\\.", *ptr))
		ptr--;
	if (*ptr != '.')
		strcat (fullname, ".fnt");

	/* Open the file */

	file = gzopen (fullname, "wb");
	if (!file)
	{
		gr_error (_("gr_font_save: file '%s' could not be created"), fullname);
		return 0;
	}

	/* Write the header */

	memcpy (header, "fnx\x1a\x0d\x0a\x00", 7);
	header[7] = font->bpp;
	gzwrite (file, &header, 8);

	/* Write the character information */

	memset (chardata, 0, sizeof(chardata));
	offset = 8 + 4 + ((font->bpp == 8) ? 576+768:0) + sizeof(chardata);

	for (n = 0 ; n < 256 ; n++)
	{
		chardata[n].xadvance   = font->glyph[n].xadvance;
		chardata[n].yadvance   = font->glyph[n].yadvance;

		if (font->glyph[n].bitmap)
		{
	/* Write the palette */

           	if (!palette_saved && font->bpp == 8)
	{
		Uint8   colors[256][3];
		Uint8 * block = calloc(576,1) ;
        		SDL_Color * gpal = palette ;
        		int k;

                if (font->glyph[n].bitmap->palette) gpal = font->glyph[n].bitmap->palette->rgb; else gpal = palette;

                /* Generate palette info */
                for (k = 0 ; k < 256 ; k++) {
                    colors[k][0] = gpal[k].r >> 2 ;
                    colors[k][1] = gpal[k].g >> 2 ;
                    colors[k][2] = gpal[k].b >> 2 ;
		}

		gzwrite (file, &colors, 768) ;
		gzwrite (file, block, 576) ;
		free(block) ;
        		palette_saved = 1;
	}

			chardata[n].width      = font->glyph[n].bitmap->width;
			chardata[n].height     = font->glyph[n].bitmap->height;
			chardata[n].xadvance   = font->glyph[n].xadvance;
			chardata[n].yadvance   = font->glyph[n].yadvance;
			chardata[n].xoffset    = font->glyph[n].xoffset;
			chardata[n].yoffset    = font->glyph[n].yoffset;
			chardata[n].fileoffset = offset;

			offset += font->glyph[n].bitmap->widthb * chardata[n].height;
		}

		ARRANGE_DWORD (&chardata[n].xadvance);
		ARRANGE_DWORD (&chardata[n].yadvance);
		ARRANGE_DWORD (&chardata[n].width);
		ARRANGE_DWORD (&chardata[n].width);
		ARRANGE_DWORD (&chardata[n].xoffset);
		ARRANGE_DWORD (&chardata[n].yoffset);
		ARRANGE_DWORD (&chardata[n].fileoffset);
	}

	ARRANGE_DWORD (&font->charset);
	gzwrite (file, &font->charset, 4);
	ARRANGE_DWORD (&font->charset);

	gzwrite (file, &chardata, sizeof(chardata));

	/* Write the character bitmaps */

	for (n = 0 ; n < 256 ; n++)
	{
		if (font->glyph[n].bitmap)
		{
			GRAPH * gr = font->glyph[n].bitmap;

			if (gr->depth != font->bpp)
			{
				gr_error (_("gr_font_save: corrupt font"));
				gzclose (file);
				return 0;
			}

			if (gr->depth == 16)
			{
				if ( (block = malloc(gr->widthb)) == NULL)
				{
					gr_error (_("gr_font_save: out of memory"));
					gzclose (file);
					return 0;
				}
			}

			lineptr = gr->data;

			for (y = 0 ; y < gr->height ; y++, lineptr += gr->pitch)
			{
				if (gr->depth == 16)
				{
					memcpy (block, lineptr, gr->widthb);
					ARRANGE_WORDS (block, (int)gr->width);
					gr_convert16_ScreenTo565 ((Uint16 *)block, gr->width);
					gzwrite (file, block, gr->widthb);
				}
				else
				{
					gzwrite (file, lineptr, gr->widthb);
				}
			}

			if (gr->depth == 16) free(block);
		}
	}

	gzclose(file);
	return 1;
}

/*
 *  FUNCTION : gr_load_bdf
 *
 *  Load a BDF font from disk. This is a very simple loader that ignores
 *  anything that is not relevant to screen display or non-horizontal
 *  writing fonts.
 *
 *  PARAMS :
 *		filename		Name of the BDF file
 *
 *  RETURN VALUE :
 *      ID of the font if succeded or -1 otherwise
 *
 */

int gr_load_bdf (const char * filename)
{
	file * fp;
	Uint8 line[2048];
	Uint8 * ptr;
	Uint8 * optr;
	FONT * font;
	int id, x, y, i;
	int error = 0;

	Uint8 nibbleh[256];
	Uint8 nibblel[256];

	int default_xadvance = 0;
	int default_yadvance = 0;
	int in_char = 0;
	int encoding = -1;
	int width = 0;
	int height = 0;
	int xoffset = 0;
	int yoffset = 0;
	int xadvance = 0;
	int yadvance = 0;
	int minyoffset = 0;
	int len;

	/* Arrays used to convert hex ASCII to binary */

	memset (nibbleh, 0, 256);
	memset (nibblel, 0, 256);

	for (i = '0' ; i <= '9' ; i++)
	{
		nibbleh[i] = ((i - '0') << 4);
		nibblel[i] = i - '0';
	}
	for (i = 10 ; i <= 15 ; i++)
	{
		nibbleh['A' + i - 10] = (i << 4);
		nibbleh['a' + i - 10] = (i << 4);
		nibblel['A' + i - 10] = i;
		nibblel['a' + i - 10] = i;
	}

	/* Open the file and create the font */

	fp = file_open (filename, "r");
	if (!fp) return -1;

	id = gr_font_new ();
	if (id < 0) return -1;
	font = fonts[id];
	font->bpp = 1;
	font->charset = CHARSET_ISO8859;
	font->maxwidth = 0;
	font->maxheight = 0;

	/* Process the file, a line each time */

	for (line[2047] = 0 ; ; )
	{
		if (!(len = file_gets (fp, line, 2047))) break;
		if (line[len-1] == '\n') line[len-1] = '\0';

		/* Handle global-level commands */

		if (strncmp (line, "DWIDTH ", 7) == 0 && !in_char)
		{
			default_xadvance = atoi(line+7);
			ptr = strchr (line+7, ' ');
			if (ptr) default_yadvance = atoi(ptr+1);
		}
		else if (strncmp (line, "STARTCHAR", 9) == 0)
		{
			in_char = 1;
			encoding = -1;
			height = 0;
			xadvance = default_xadvance;
			yadvance = default_yadvance;
		}
		else if (strncmp (line, "ENDCHAR", 7) == 0)
		{
			in_char = 0;
		}

		/* Handle character-level commands */

		else if (strncmp (line, "DWIDTH ", 7) == 0 && in_char)
		{
			xadvance = atoi(line+7);
			ptr = strchr (line+7, ' ');
			if (ptr) yadvance = atoi(ptr+1);
		}
		else if (strncmp (line, "ENCODING ", 9) == 0 && in_char)
		{
			encoding = atoi(line+9);
			if (encoding == -1)
			{
				ptr = strchr (line+7, ' ');
				if (ptr) encoding = atoi(ptr+1);
			}
		}
		else if (strncmp (line, "BBX ", 4) == 0 && in_char)
		{
			width = atoi(line+4);
			if (width & 7) width = (width & ~7)+8;
			if ((ptr = strchr (line+4, ' ')) == NULL) continue;
			height = atoi(ptr+1);
			if ((ptr = strchr (ptr+1, ' ')) == NULL) continue;
			xoffset = atoi(ptr+1);
			if ((ptr = strchr (ptr+1, ' ')) == NULL) continue;
			yoffset = atoi(ptr+1);
		}
		else if (strncmp (line, "BITMAP", 6) == 0)
		{
			/* Read bitmap data */
			if (encoding >= 0 && encoding < 256 && height > 0)
			{
				font->glyph[encoding].xadvance = xadvance;
				font->glyph[encoding].yadvance = yadvance;
				font->glyph[encoding].xoffset  = xoffset;
				font->glyph[encoding].yoffset  = -yoffset-height;

				if (minyoffset > -yoffset-height) minyoffset = -yoffset-height;

				error = 1;
				font->glyph[encoding].bitmap = bitmap_new (encoding, width, height, 1, 1);
				if (font->glyph[encoding].bitmap == 0) break;
				bitmap_add_cpoint (font->glyph[encoding].bitmap, 0, 0) ;

				if (font->maxwidth < width) font->maxwidth = width;
				if (font->maxheight < height) font->maxheight = height;

				for (y = 0 ; y < height ; y++)
				{
					if (!(len=file_gets (fp, line, 2047))) break;
					if (line[len-1] == '\n') line[len-1] = '\0';
					ptr  = line;
					optr = (Uint8 *)font->glyph[encoding].bitmap->data + font->glyph[encoding].bitmap->pitch * y;

					for (x = 0 ; x < width ; x += 8)
					{
						if (!ptr[0] || !ptr[1]) break;
						*optr++ = nibbleh[ptr[0]] | nibblel[ptr[1]];
						ptr += 2;
					}
				}
				if (y != height) break;
				error = 0;
			}
		}
	}

	file_close (fp);

	if (error)
	{
		gr_error (_("gr_load_bdf: incorrect BDF file"));
		return -1;
	}

	/* Adjust yoffsets to positive */

	for (i = 0 ; i < 256 ; i++)
		font->glyph[i].yoffset -= minyoffset;

	if (font->glyph[32].xadvance == 0)
		font->glyph[32].xadvance = font->glyph['j'].xadvance;

	fonts[font_count] = font ;
	return font_count++ ;
}

/*
 *  FUNCTION : gr_font_get
 *
 *  Return a font object, given an ID
 *
 *  PARAMS :
 *		id		id of the font
 *
 *  RETURN VALUE :
 *      Pointer to the font object or NULL if it does not exist
 *
 */

FONT * gr_font_get (int id)
{
	if (id >= 0 && id <= 255) return fonts[id];
	return NULL;
}