aboutsummaryrefslogtreecommitdiff
path: root/modules/libfont/libfont.c
blob: b934b4e601d671e5d200d7df191334381b8d0c91 (plain)
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
/*
 *  Copyright � 2006-2016 SplinterGU (Fenix/Bennugd)
 *  Copyright � 2002-2006 Fenix Team (Fenix)
 *  Copyright � 1999-2002 Jos� Luis Cebri�n Pag�e (Fenix)
 *
 *  This file is part of Bennu - Game Development
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty. In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *     2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *     3. This notice may not be removed or altered from any source
 *     distribution.
 *
 */

/* --------------------------------------------------------------------------- */

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

#include "bgdrtm.h"

#define __LIB_FONT
#include "libfont.h"

/* --------------------------------------------------------------------------- */

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

unsigned char default_font[256*8] =
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x7F, 0xC1, 0xF7, 0xC1, 0xFF, 0xDD, 0xC1, 0x7F,
    0x7F, 0xFF, 0xFF, 0xFF, 0xE3, 0xF7, 0xFF, 0x7F,
    0x7E, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18, 0x00,
    0x18, 0x3C, 0x7E, 0xFF, 0x7E, 0x3C, 0x18, 0x00,
    0x3C, 0x7E, 0x3C, 0xFF, 0xFF, 0x7E, 0x3C, 0x7E,
    0x18, 0x18, 0x3C, 0x7E, 0xFF, 0x7E, 0x3C, 0x7E,
    0x00, 0x00, 0x1C, 0x3E, 0x3E, 0x1C, 0x00, 0x00,
    0xFF, 0xFF, 0xF7, 0xE3, 0xE3, 0xF7, 0xFF, 0xFF,
    0x00, 0x3E, 0x77, 0x63, 0x63, 0x77, 0x3E, 0x00,
    0xFF, 0xE3, 0xDD, 0xFF, 0xFF, 0xDD, 0xE3, 0xFF,
    0x0F, 0x07, 0x0F, 0x7F, 0xEE, 0xEE, 0xEE, 0x7C,
    0x3E, 0x77, 0x77, 0x77, 0x3E, 0x1C, 0x7F, 0x1C,
    0x3F, 0x3B, 0x3F, 0x38, 0x38, 0x78, 0xF8, 0xF0,
    0x7F, 0x73, 0x7F, 0x73, 0x73, 0x77, 0xF7, 0xE0,
    0xDD, 0x7F, 0x3E, 0xF7, 0xF7, 0x3E, 0x7F, 0xDD,
    0xC0, 0xF0, 0xFC, 0xFF, 0xFC, 0xF0, 0xC0, 0x00,
    0x03, 0x0F, 0x3F, 0xFF, 0x3F, 0x0F, 0x03, 0x00,
    0x1C, 0x3E, 0x7F, 0x1C, 0x1C, 0x7F, 0x3E, 0x1C,
    0x77, 0x77, 0x77, 0x77, 0x77, 0x00, 0x77, 0x00,
    0x7F, 0xFF, 0xFF, 0x7F, 0x1F, 0x1F, 0x1F, 0x00,
    0x3F, 0x73, 0x3C, 0x7E, 0x7E, 0x3C, 0xEE, 0x7C,
    0x00, 0x00, 0x00, 0x00, 0x7F, 0x7F, 0x7F, 0x00,
    0x1C, 0x3E, 0x7F, 0x1C, 0x7F, 0x3E, 0x1C, 0xFF,
    0x1C, 0x3E, 0x7F, 0x1C, 0x1C, 0x1C, 0x1C, 0x00,
    0x1C, 0x1C, 0x1C, 0x1C, 0x7F, 0x3E, 0x1C, 0x00,
    0x00, 0x1C, 0x0E, 0xFF, 0x0E, 0x1C, 0x00, 0x00,
    0x00, 0x38, 0x70, 0xFF, 0x70, 0x38, 0x00, 0x00,
    0x00, 0x00, 0xE0, 0xE0, 0xE0, 0xFF, 0x00, 0x00,
    0x00, 0x36, 0x77, 0xFF, 0x77, 0x36, 0x00, 0x00,
    0x00, 0x1C, 0x3E, 0x7F, 0xFF, 0xFF, 0x00, 0x00,
    0x00, 0xFF, 0xFF, 0x7F, 0x3E, 0x1C, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x00,
    0x00, 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x36, 0x7F, 0x36, 0x36, 0x7F, 0x36, 0x00,
    0x00, 0x0C, 0x3F, 0x3C, 0x3F, 0x0F, 0x3F, 0x0C,
    0x00, 0x73, 0x76, 0x0C, 0x18, 0x37, 0x67, 0x00,
    0x00, 0x18, 0x3C, 0x18, 0x3F, 0x66, 0x3F, 0x00,
    0x00, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x06, 0x0C, 0x0C, 0x0C, 0x0C, 0x06, 0x00,
    0x00, 0x30, 0x18, 0x18, 0x18, 0x18, 0x30, 0x00,
    0x00, 0x00, 0x1E, 0x0C, 0x3F, 0x0C, 0x1E, 0x00,
    0x00, 0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x18,
    0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x1C, 0x00,
    0x00, 0x00, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x00,
    0x00, 0x3E, 0x67, 0x6F, 0x7B, 0x73, 0x3E, 0x00,
    0x00, 0x18, 0x78, 0x18, 0x18, 0x18, 0xFE, 0x00,
    0x00, 0x3E, 0x63, 0x03, 0x3E, 0x60, 0x7F, 0x00,
    0x00, 0x7E, 0x03, 0x3E, 0x03, 0x03, 0x7E, 0x00,
    0x00, 0x0E, 0x1E, 0x36, 0x66, 0x7F, 0x06, 0x00,
    0x00, 0x7F, 0x60, 0x7E, 0x03, 0x03, 0x7E, 0x00,
    0x00, 0x3F, 0x60, 0x7E, 0x63, 0x63, 0x3E, 0x00,
    0x00, 0x7F, 0x03, 0x06, 0x0C, 0x18, 0x18, 0x00,
    0x00, 0x3E, 0x63, 0x3E, 0x63, 0x63, 0x3E, 0x00,
    0x00, 0x3E, 0x63, 0x63, 0x3F, 0x03, 0x03, 0x00,
    0x00, 0x00, 0x00, 0x18, 0x00, 0x18, 0x00, 0x00,
    0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30,
    0x00, 0x00, 0x06, 0x0C, 0x18, 0x0C, 0x06, 0x00,
    0x00, 0x00, 0x00, 0x3F, 0x00, 0x3F, 0x00, 0x00,
    0x00, 0x00, 0x18, 0x0C, 0x06, 0x0C, 0x18, 0x00,
    0x00, 0x3E, 0x63, 0x06, 0x0C, 0x00, 0x0C, 0x00,
    0x00, 0x3E, 0x6F, 0x7F, 0x7F, 0x60, 0x3E, 0x00,
    0x00, 0x3E, 0x63, 0x63, 0x7F, 0x63, 0x63, 0x00,
    0x00, 0x7E, 0x63, 0x7E, 0x63, 0x63, 0x7E, 0x00,
    0x00, 0x3F, 0x60, 0x60, 0x60, 0x60, 0x3F, 0x00,
    0x00, 0x7E, 0x63, 0x63, 0x63, 0x63, 0x7E, 0x00,
    0x00, 0x7F, 0x60, 0x7C, 0x60, 0x60, 0x7F, 0x00,
    0x00, 0x7F, 0x60, 0x7F, 0x60, 0x60, 0x60, 0x00,
    0x00, 0x3F, 0x60, 0x60, 0x6F, 0x63, 0x3F, 0x00,
    0x00, 0x63, 0x63, 0x7F, 0x63, 0x63, 0x63, 0x00,
    0x00, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00,
    0x00, 0x03, 0x03, 0x03, 0x03, 0x63, 0x3E, 0x00,
    0x00, 0x66, 0x6C, 0x7C, 0x66, 0x63, 0x63, 0x00,
    0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7F, 0x00,
    0x00, 0x63, 0x77, 0x7F, 0x63, 0x63, 0x63, 0x00,
    0x00, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x63, 0x00,
    0x00, 0x3E, 0x63, 0x63, 0x63, 0x63, 0x3E, 0x00,
    0x00, 0x7E, 0x63, 0x63, 0x7E, 0x60, 0x60, 0x00,
    0x00, 0x3E, 0x63, 0x63, 0x6F, 0x67, 0x3F, 0x00,
    0x00, 0x7E, 0x63, 0x63, 0x7E, 0x66, 0x63, 0x00,
    0x00, 0x3F, 0x60, 0x3E, 0x03, 0x03, 0x7E, 0x00,
    0x00, 0xFF, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
    0x00, 0x63, 0x63, 0x63, 0x63, 0x63, 0x3E, 0x00,
    0x00, 0x63, 0x63, 0x63, 0x36, 0x36, 0x1C, 0x00,
    0x00, 0x63, 0x63, 0x6B, 0x6B, 0x6B, 0x36, 0x00,
    0x00, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00,
    0x00, 0xC3, 0xC3, 0x7E, 0x18, 0x18, 0x18, 0x00,
    0x00, 0x7F, 0x06, 0x0C, 0x18, 0x30, 0x7F, 0x00,
    0x00, 0x0F, 0x0C, 0x0C, 0x0C, 0x0C, 0x0F, 0x00,
    0x00, 0x00, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x00,
    0x00, 0x78, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00,
    0x00, 0x18, 0x3C, 0x7E, 0x18, 0x18, 0x18, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
    0x00, 0x1E, 0x33, 0x7C, 0x30, 0x30, 0x7F, 0x00,
    0x00, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00,
    0x00, 0x60, 0x60, 0x7C, 0x66, 0x66, 0x7C, 0x00,
    0x00, 0x00, 0x3E, 0x60, 0x60, 0x60, 0x3E, 0x00,
    0x00, 0x03, 0x03, 0x1F, 0x33, 0x33, 0x1F, 0x00,
    0x00, 0x00, 0x1E, 0x33, 0x3E, 0x30, 0x1F, 0x00,
    0x00, 0x1F, 0x30, 0x3C, 0x30, 0x30, 0x30, 0x00,
    0x00, 0x00, 0x1F, 0x33, 0x33, 0x1F, 0x03, 0x3E,
    0x00, 0x30, 0x30, 0x3E, 0x33, 0x33, 0x33, 0x00,
    0x0C, 0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x00,
    0x06, 0x00, 0x06, 0x06, 0x06, 0x06, 0x66, 0x3C,
    0x00, 0x30, 0x36, 0x36, 0x3E, 0x33, 0x33, 0x00,
    0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0E, 0x00,
    0x00, 0x00, 0x7E, 0xDB, 0xDB, 0xDB, 0xDB, 0x00,
    0x00, 0x00, 0x3E, 0x33, 0x33, 0x33, 0x33, 0x00,
    0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x00, 0x00, 0x3E, 0x33, 0x33, 0x3E, 0x30, 0x30,
    0x00, 0x00, 0x1F, 0x33, 0x33, 0x1F, 0x03, 0x03,
    0x00, 0x00, 0x36, 0x38, 0x30, 0x30, 0x30, 0x00,
    0x00, 0x00, 0x1F, 0x30, 0x1E, 0x03, 0x3E, 0x00,
    0x00, 0x18, 0x1E, 0x18, 0x18, 0x18, 0x0E, 0x00,
    0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x00, 0x00, 0x33, 0x33, 0x1E, 0x1E, 0x0C, 0x00,
    0x00, 0x00, 0x63, 0x63, 0x6B, 0x6B, 0x36, 0x00,
    0x00, 0x00, 0x33, 0x1E, 0x0C, 0x1E, 0x33, 0x00,
    0x00, 0x00, 0x33, 0x33, 0x33, 0x1F, 0x03, 0x3E,
    0x00, 0x00, 0x3F, 0x07, 0x0C, 0x38, 0x3F, 0x00,
    0x00, 0x07, 0x06, 0x1C, 0x06, 0x06, 0x07, 0x00,
    0x00, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x00,
    0x00, 0xF0, 0x30, 0x1C, 0x30, 0x30, 0xF0, 0x00,
    0x00, 0x3B, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x3E, 0x63, 0xDD, 0xF1, 0xF1, 0xDD, 0x63, 0x3E,
    0x7C, 0xC6, 0xC0, 0xC6, 0x7C, 0x0C, 0x06, 0x7C,
    0x3F, 0x00, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x07, 0x00, 0x1E, 0x33, 0x3E, 0x30, 0x1F, 0x00,
    0x7E, 0xC3, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00,
    0x7E, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00,
    0x70, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00,
    0x18, 0x18, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00,
    0x00, 0x00, 0x7C, 0xE0, 0xE0, 0x7C, 0x0E, 0x3C,
    0x3F, 0x61, 0x1E, 0x33, 0x3E, 0x30, 0x1E, 0x00,
    0x3F, 0x00, 0x1E, 0x33, 0x3E, 0x30, 0x1E, 0x00,
    0x38, 0x00, 0x1E, 0x33, 0x3E, 0x30, 0x1E, 0x00,
    0x00, 0x3F, 0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0x00,
    0x00, 0x1E, 0x33, 0x0C, 0x0C, 0x0C, 0x0C, 0x00,
    0x00, 0x38, 0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0x00,
    0xE7, 0x3C, 0x66, 0xC3, 0xFF, 0xC3, 0xC3, 0x00,
    0x38, 0x38, 0x00, 0x7C, 0xC6, 0xFE, 0xC6, 0x00,
    0x1E, 0x00, 0xFE, 0x70, 0x7C, 0x70, 0xFE, 0x00,
    0x00, 0x00, 0x7F, 0x0E, 0x7F, 0xEE, 0x7F, 0x00,
    0x3F, 0x7E, 0xEE, 0xFF, 0xEE, 0xEE, 0xEF, 0x00,
    0x3F, 0x61, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x3F, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x38, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x1E, 0x33, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x38, 0x00, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x3F, 0x00, 0x36, 0x36, 0x1E, 0x06, 0x06, 0x3C,
    0xE3, 0x1C, 0x3E, 0x77, 0x77, 0x3E, 0x1C, 0x00,
    0xEE, 0x00, 0xEE, 0xEE, 0xEE, 0xEE, 0x7C, 0x00,
    0x1C, 0x1C, 0x7F, 0xC0, 0xC0, 0x7F, 0x1C, 0x1C,
    0x1C, 0x36, 0x33, 0x7C, 0x30, 0x63, 0x7E, 0x00,
    0xEE, 0xEE, 0x7C, 0xFE, 0x38, 0xFE, 0x38, 0x38,
    0xF8, 0xCC, 0xCC, 0xF8, 0xC6, 0xCF, 0xC6, 0xC7,
    0x0E, 0x1B, 0x18, 0x3E, 0x18, 0x18, 0xD8, 0x70,
    0x0E, 0x00, 0x3C, 0x06, 0x3E, 0x66, 0x3E, 0x00,
    0x00, 0x07, 0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0x00,
    0x07, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x07, 0x00, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x00,
    0x1E, 0x00, 0x3E, 0x33, 0x33, 0x33, 0x33, 0x00,
    0xFE, 0x00, 0xEE, 0xFE, 0xFE, 0xFE, 0xEE, 0x00,
    0x3C, 0x6C, 0x6C, 0x3F, 0x00, 0x7F, 0x00, 0x00,
    0x3C, 0x66, 0x66, 0x3C, 0x00, 0x7E, 0x00, 0x00,
    0x00, 0x18, 0x00, 0x18, 0x30, 0x63, 0x3E, 0x00,
    0x00, 0x00, 0x00, 0xFE, 0xE0, 0xE0, 0x00, 0x00,
    0x00, 0x00, 0x00, 0xFE, 0x0E, 0x0E, 0x00, 0x00,
    0xC3, 0xC6, 0xCC, 0xDF, 0x31, 0x63, 0xC6, 0x07,
    0xC3, 0xC6, 0xCC, 0xDB, 0x37, 0x6D, 0xCF, 0x01,
    0x00, 0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00,
    0x00, 0x3F, 0x66, 0xCC, 0x66, 0x3F, 0x00, 0x00,
    0x00, 0xFC, 0x66, 0x33, 0x66, 0xFC, 0x00, 0x00,
    0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC,
    0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF, 0x7F, 0xFF,
    0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF,
    0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C, 0x1C, 0x1C, 0xFC, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C, 0xFC, 0x1C, 0xFC, 0x1C, 0x1C, 0x1C,
    0x3F, 0x3F, 0x3F, 0x3F, 0xFF, 0x3F, 0x3F, 0x3F,
    0x00, 0x00, 0x00, 0x00, 0xFF, 0x3F, 0x3F, 0x3F,
    0x00, 0x00, 0xFC, 0x1C, 0xFC, 0x1C, 0x1C, 0x1C,
    0x3F, 0x3F, 0xFF, 0x07, 0xFF, 0x3F, 0x3F, 0x3F,
    0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
    0x00, 0x00, 0xFF, 0x07, 0xFF, 0x3F, 0x3F, 0x3F,
    0x3F, 0x3F, 0xFF, 0x07, 0xFF, 0x00, 0x00, 0x00,
    0x3F, 0x3F, 0x3F, 0x3F, 0xFF, 0x00, 0x00, 0x00,
    0x1C, 0x1C, 0xFC, 0x1C, 0xFC, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xFC, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C, 0x1C, 0x1C, 0x1F, 0x00, 0x00, 0x00,
    0x1C, 0x1C, 0x1C, 0x1C, 0xFF, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xFF, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C, 0x1C, 0x1C, 0x1F, 0x1C, 0x1C, 0x1C,
    0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
    0x1C, 0x1C, 0x1C, 0x1C, 0xFF, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C, 0x1F, 0x1C, 0x1F, 0x1C, 0x1C, 0x1C,
    0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
    0x3F, 0x3F, 0x3F, 0x38, 0x3F, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x3F, 0x38, 0x3F, 0x3F, 0x3F, 0x3F,
    0x3F, 0x3F, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00,
    0x00, 0x00, 0xFF, 0x00, 0xFF, 0x3F, 0x3F, 0x3F,
    0x3F, 0x3F, 0x3F, 0x38, 0x3F, 0x3F, 0x3F, 0x3F,
    0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00,
    0x3F, 0x3F, 0xFF, 0x00, 0xFF, 0x3F, 0x3F, 0x3F,
    0x1C, 0x1C, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0x00,
    0x3F, 0x3F, 0x3F, 0x3F, 0xFF, 0x00, 0x00, 0x00,
    0x00, 0x00, 0xFF, 0x00, 0xFF, 0x1C, 0x1C, 0x1C,
    0x00, 0x00, 0x00, 0x00, 0xFF, 0x3F, 0x3F, 0x3F,
    0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x00, 0x00,
    0x1C, 0x1C, 0x1F, 0x1C, 0x1F, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x1F, 0x1C, 0x1F, 0x1C, 0x1C, 0x1C,
    0x00, 0x00, 0x00, 0x00, 0x3F, 0x3F, 0x3F, 0x3F,
    0x3F, 0x3F, 0x3F, 0x3F, 0xFF, 0x3F, 0x3F, 0x3F,
    0x1C, 0x1C, 0xFF, 0x1C, 0xFF, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C, 0x1C, 0x1C, 0xFC, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x1F, 0x1C, 0x1C, 0x1C,
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8,
    0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
    0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x7F, 0xFE, 0xEC, 0xFE, 0x7F, 0x00,
    0x00, 0x7C, 0xEE, 0xFC, 0xEE, 0xFC, 0xE0, 0xE0,
    0x00, 0xFE, 0xEE, 0xE0, 0xE0, 0xE0, 0xE0, 0x00,
    0x00, 0xFF, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x00,
    0xFE, 0xEE, 0x70, 0x38, 0x70, 0xEE, 0xFE, 0x00,
    0x00, 0x00, 0x7F, 0xFC, 0xFC, 0xFC, 0x78, 0x00,
    0x00, 0x77, 0x77, 0x77, 0x77, 0x7E, 0x70, 0xE0,
    0x00, 0x7F, 0xFE, 0x1C, 0x1C, 0x1C, 0x1C, 0x00,
    0xFE, 0x38, 0x7C, 0xEE, 0xEE, 0x7C, 0x38, 0xFE,
    0x3C, 0x7E, 0xE7, 0xFF, 0xE7, 0x7E, 0x3C, 0x00,
    0x3C, 0x7E, 0xE7, 0xE7, 0x7E, 0x7E, 0xFF, 0x00,
    0x1E, 0x38, 0x1C, 0x7E, 0xEE, 0xEE, 0x7C, 0x00,
    0x00, 0x00, 0x7F, 0xFF, 0xFF, 0x7F, 0x00, 0x00,
    0x07, 0x0E, 0x7F, 0xFF, 0xFF, 0x7F, 0x70, 0xE0,
    0x3C, 0x70, 0xE0, 0xFC, 0xE0, 0x70, 0x3C, 0x00,
    0x7C, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x00,
    0x00, 0xFE, 0x00, 0xFE, 0x00, 0xFE, 0x00, 0x00,
    0x38, 0x38, 0xFE, 0x38, 0x38, 0x00, 0xFE, 0x00,
    0x70, 0x38, 0x1C, 0x38, 0x70, 0x00, 0xFE, 0x00,
    0x1C, 0x38, 0x70, 0x38, 0x1C, 0x00, 0xFE, 0x00,
    0x0F, 0x1F, 0x1F, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
    0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0xFC, 0xFC, 0x78,
    0x38, 0x38, 0x00, 0xFE, 0x00, 0x38, 0x38, 0x00,
    0x00, 0x7F, 0xFE, 0x00, 0x7F, 0xFE, 0x00, 0x00,
    0x3C, 0x7E, 0x7E, 0x3C, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x1C, 0x1C, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00,
    0x0F, 0x0E, 0x0E, 0x0E, 0xFE, 0x7E, 0x3E, 0x1E,
    0x7C, 0x7E, 0x7E, 0x7E, 0x7E, 0x00, 0x00, 0x00,
    0x78, 0x1C, 0x38, 0x70, 0x7C, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x3E, 0x3E, 0x3E, 0x3E, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

/* --------------------------------------------------------------------------- */

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

/* --------------------------------------------------------------------------- */
/*
 *  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 :
 *      charset
 *      bpp
 *
 *  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( int charset, uint32_t bpp )
{
    FONT * f = ( FONT * )malloc( sizeof( FONT ) );

    if ( f == NULL ) return -1; // No memory

    if ( font_count == MAX_FONTS - 1 ) // Too much fonts
    {
        free( f );
        return -1;
    }

    memset( f, 0, sizeof( FONT ) );
    f->charset = charset; // CHARSET_CP850
    f->bpp = 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.
 *  For 1bpp fonts, 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 :
 *  map         Pointer to the bitmap
 *  charset     Charset
 *  width       Width of each character, in bits (pixels)
 *  height      Height of each character
 *  first       First char
 *  last        Last char
 *  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 Error, id otherwise
 *
 */

int gr_font_newfrombitmap( GRAPH * map, int charset, int width, int height, int first, int last, int options )
{
    char * chardata ;
    FONT * f ;
    char * ptr, * charptr ;
    int i, y, id ;
    int charsize ;
    int linesize = 0 ;
    int w, h, cw, ch;
    GRAPH * bitmap;

    id = gr_font_new( charset, map->format->depth ) ;
    if ( id == -1 ) return -1;

    f = fonts[id];

    charsize = map->pitch * height;

    switch ( map->format->depth )
    {
        case    1:
                linesize = width / 8;
                break;

        case    8:
                linesize = width;
                break;

        case    16:
                linesize = width * sizeof( uint16_t );
                break;

        case    32:
                linesize = width * sizeof( uint32_t );
                break;
    }

    ch = map->height / height;
    cw = map->width / width;

    i = first;

    for ( h = 0; h < ch; h++ )
    {
        if ( i > last ) break;

        chardata = map->data + h * charsize;

        for ( charptr = chardata, w = 0; w < cw; w++, charptr += linesize, i++ )
        {
            int align = 0;

            if ( options != NFB_FIXEDWIDTH ) align = align_bitmap_char_left( ( unsigned char * ) charptr, width, height, map->pitch, map->format->depth ) * map->format->depthb ;

            bitmap = bitmap_new( i, width, height, map->format->depth );
            if ( !bitmap )
            {
                gr_font_destroy( id );
                return -1; // No memory
            }

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

            bitmap_add_cpoint( bitmap, 0, 0 ) ;

            ptr = bitmap->data ;
            memset( bitmap->data, '\0', bitmap->pitch * bitmap->height );

            for ( y = 0 ; y < height ; y++, ptr += bitmap->pitch ) memcpy( ptr, charptr + map->pitch * y + align, bitmap->widthb - align );

            if ( options != NFB_FIXEDWIDTH )
            {
                if ( map->format->depth == 1 )
                    f->glyph[i].xadvance = get_bitmap_char_width( ( unsigned char * ) charptr, width, height, map->pitch, map->format->depth ) + 1;
                else
                    f->glyph[i].xadvance = get_bitmap_char_width( bitmap->data, width, height, bitmap->pitch, bitmap->format->depth ) + 1;
            }
            else
                f->glyph[i].xadvance = width + 1;

            bitmap->modified = 0 ;
            bitmap->info_flags = 0 ;
        }
    }

    /* Set a reasonable size for the space */

    f->glyph[32].xadvance = width * 65 / 100 ;
    f->maxwidth = width;
    f->maxheight = height;

    return id ;
}

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

static int align_bitmap_char_left( unsigned char *data, int width, int height, int pitch, int bpp )
{
    int        leftest, n, c, x ;
    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 ;
        }
    }

    if ( bpp == 1 )
    {
        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 ;

        return 0;
    }
    else
    {
        int align = width;

        switch( bpp )
        {
            case    8:
               {
                    uint8_t * p = ( uint8_t * ) data;
                    while ( height-- )
                    {
                        p = ( uint8_t * ) ( data + height * pitch );
                        for ( x = 0; x < width && !*p++; x++ );
                        if ( align > x ) align = x;
                    }
                    return ( align == width ) ? 0 : align;
                }

            case    16:
               {
                    uint16_t * p = ( uint16_t * ) data;
                    while ( height-- )
                    {
                        p = ( uint16_t * ) ( data + height * pitch );
                        for ( x = 0; x < width && !*p++; x++ );
                        if ( align > x ) align = x;
                    }
                    return ( align == width ) ? 0 : align;
                }

            case    32:
               {
                    uint32_t * p = ( uint32_t * ) data;
                    while ( height-- )
                    {
                        p = ( uint32_t * ) ( data + height * pitch );
                        for ( x = 0; x < width && !*p++; x++ );
                        if ( align > x ) align = x;
                    }
                    return ( align == width ) ? 0 : align;
                }
        }

        return 0;
    }
}

/* --------------------------------------------------------------------------- */
/* 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 pitch, int bpp )
{
    int x, c, d, max = 0, w ;

    switch( bpp )
    {
        case    1:
                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 < 4 ) ? 4 : max;

        case    8:
                {
                    uint8_t * p = ( uint8_t * ) data;
                    while ( height-- )
                    {
                        w = 0;
                        p = ( uint8_t * ) ( data + height * pitch );
                        for ( x = 0; x < width; x++ )
                        {
                            if ( *p++ ) w = x;
                        }
                        if ( max < w ) max = w;
                    }
                    return ( !max ) ? width * 65 / 100 : max;
                }

        case    16:
            {
                uint16_t * p;
                while ( height-- )
                {
                    w = 0;
                    p = ( uint16_t * ) ( data + height * pitch );
                    for ( x = 0; x < width; x++ )
                    {
                        if ( *p++ ) w = x;
                    }
                    if ( max < w ) max = w;
                }
                return ( !max ) ? width * 65 / 100 : max;
            }

        case    32:
            {
                uint32_t * p;
                while ( height-- )
                {
                    w = 0;
                    p = ( uint32_t * ) ( data + height * pitch );
                    for ( x = 0; x < width; x++ )
                    {
                        if ( *p++ ) w = x;
                    }
                    if ( max < w ) max = w;
                }
                return ( !max ) ? width * 65 / 100 : max;
            }
    }

    return width;
}

/* --------------------------------------------------------------------------- */
/*
 *  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 )
{
    GRAPH * map = bitmap_new_ex( 0, 8, 8 * 256, 1, chardata, 1 );
    if ( !map ) return -1;

    int last_count = font_count ;
    if ( fonts[0] ) gr_font_destroy( 0 ) ;
    font_count = 0 ;

    gr_font_newfrombitmap( map, CHARSET_CP850, 8, 8, 0, 255, 0 );
    if ( last_count ) font_count = last_count ;

    bitmap_destroy( map );

    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 < MAX_FONTS ; 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_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 < MAX_FONTS ) return fonts[id];
    return NULL;
}

/* --------------------------------------------------------------------------- */
/* exports                                                                     */
/* --------------------------------------------------------------------------- */

#include "libfont_exports.h"

/* --------------------------------------------------------------------------- */