-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfont.c
5058 lines (4406 loc) · 144 KB
/
alfont.c
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Modified Alfont 2.0.9
*
* The original AllegroFont v2.0.9 has a few problems.
* This modification solves them.
*
* Modification by: Alexandre Martins (thanks to http://allegro.cc forums)
* Modification date: 2010-10-30
*
* Original Alfont website: http://chernsha.sitesled.com/
* AllegroFont (c) 2001, 2002 Javier Gonzalez
* Enhanced by Chernsha since 2004 year
*/
#if defined(__GNUC__) && !defined(__MINGW32__)
#define _msize malloc_usable_size
#endif
/* AllegroFont - a wrapper for FreeType 2 */
/* to render TTF and other font formats with Allegro */
/* FreeType 2 is copyright (c) 1996-2000 */
/* David Turner, Robert Wilhelm, and Werner Lemberg */
/* AllegroFont is copyright (c) 2001, 2002 Javier Gonz lez */
/* Enhanced by Chernsha since 2004 year */
/* See FTL.txt (FreeType License) for license */
// todo (char map null check)
#include <allegro.h>
#include <stdlib.h>
#include <string.h>
#include <alfont.h>
#include <ft2build.h>
#ifdef ALFONT_DOS //run in DOS
#include <iconv.h>
#else //run in Other
#include <locale.h>
#endif
#ifdef ALFONT_LINUX //run in LINUX
#include <wchar.h>
#endif
#include FT_FREETYPE_H
#include FT_GLYPH_H
/* structs */
struct ALFONT_FONT {
FT_Face face; /* face */
int face_h; /* face height */
int real_face_h; /* real face height */
int face_ascender; /* face ascender */
char *data; /* if loaded from memory, the data chunk */
int data_size; /* and its size */
int ch_spacing; /* extra spacing */
int num_fixed_sizes; /* -1 if scalable, >=0 if fixed */
struct _ALFONT_CACHED_GLYPH {
char is_cached;
int width, height, aawidth, aaheight;
int left, top, aaleft, aatop;
int advancex, advancey;
int mono_available, aa_available;
unsigned char *bmp;
unsigned char *aabmp;
} *cached_glyphs; /* array to know which glyphs have been cached */
int *fixed_sizes; /* array with the fixed sizes */
char *language; /* language */
int type; /* Code Convert(Please Use TYPE_WIDECHAR for ASCII to UNICODE) */
int outline_top; /* Font top outline width */
int outline_bottom; /* Font bottom outline width */
int outline_right; /* Font right outline width */
int outline_left; /* Font left outline width */
int outline_color; /* Font outline color */
int outline_hollow; /* Font hollow(TRUE/FALSE) */
int style; /* Font Style(STYLE_STANDARD/STYLE_ITALIC/STYLE_BOLD/STYLE_BOLDITALIC) */
int underline; /* Font underline(TRUE/FALSE) */
int underline_right; /* Extend right underline(TRUE/FALSE) */
int underline_left; /* Extend left underline(TRUE/FALSE) */
int background; /* Font Background Color(TRUE/FALSE) */
int transparency; /* Font transparency(0-255) */
int autofix; /* Font autofix(TRUE/FALSE) */
int precedingchar; /* preceding character for autofix*/
int fixed_width; /* Font fixed width(TRUE/FALSE) */
};
/* global vars */
BITMAP *default_bmp; //Draw Font on default BITMAP;
static FT_Library ft_library;
static int alfont_textmode = 0;
static int alfont_inited = 0;
/* helpers */
static void _alfont_reget_fixed_sizes(ALFONT_FONT *f) {
if (f->num_fixed_sizes < 0) {
/* scalable font */
f->fixed_sizes[0] = -1;
}
else {
/* fixed */
int i;
for (i = 0; i < f->num_fixed_sizes; i++) {
f->fixed_sizes[i] = f->face->available_sizes[i].height;
}
/* set last one to -1 */
f->fixed_sizes[f->num_fixed_sizes] = -1;
}
}
static void _alfont_uncache_glyphs(ALFONT_FONT *f) {
if (f->cached_glyphs) {
int i;
for (i = 0; i < f->face->num_glyphs; i++) {
if (f->cached_glyphs[i].is_cached) {
f->cached_glyphs[i].is_cached = 0;
if (f->cached_glyphs[i].bmp) {
free(f->cached_glyphs[i].bmp);
f->cached_glyphs[i].bmp = NULL;
}
if (f->cached_glyphs[i].aabmp) {
free(f->cached_glyphs[i].aabmp);
f->cached_glyphs[i].aabmp = NULL;
}
}
}
}
}
static void _alfont_uncache_glyph_number(ALFONT_FONT *f, int glyph_number) {
if (f->cached_glyphs) {
if (f->cached_glyphs[glyph_number].is_cached) {
f->cached_glyphs[glyph_number].is_cached = 0;
if (f->cached_glyphs[glyph_number].bmp) {
free(f->cached_glyphs[glyph_number].bmp);
f->cached_glyphs[glyph_number].bmp = NULL;
}
if (f->cached_glyphs[glyph_number].aabmp) {
free(f->cached_glyphs[glyph_number].aabmp);
f->cached_glyphs[glyph_number].aabmp = NULL;
}
}
}
}
static void _alfont_delete_glyphs(ALFONT_FONT *f) {
_alfont_uncache_glyphs(f);
if (f->cached_glyphs) {
free(f->cached_glyphs);
f->cached_glyphs = NULL;
}
}
static void _alfont_cache_glyph(ALFONT_FONT *f, int glyph_number) {
/* if glyph not cached yet */
if (!f->cached_glyphs[glyph_number].is_cached) {
FT_Glyph new_glyph;
/* load the font glyph */
FT_Load_Glyph(f->face, glyph_number, FT_LOAD_DEFAULT);
FT_Get_Glyph(f->face->glyph, &new_glyph);
/* ok, this glyph is now cached */
f->cached_glyphs[glyph_number].is_cached = 1;
f->cached_glyphs[glyph_number].mono_available = 0;
f->cached_glyphs[glyph_number].aa_available = 0;
/* render the mono bmp */
{
FT_Bitmap *ft_bmp;
FT_Glyph glyph;
FT_BitmapGlyph bmp_glyph;
FT_Glyph_Copy(new_glyph, &glyph);
/* only render glyph if it is not already a bitmap */
if (glyph->format != ft_glyph_format_bitmap)
FT_Glyph_To_Bitmap(&glyph, ft_render_mode_mono, NULL, 1);
/* the FT rendered bitmap */
bmp_glyph = (FT_BitmapGlyph)glyph;
ft_bmp = &bmp_glyph->bitmap;
/* save only if the bitmap is really 1 bit */
if (ft_bmp->pixel_mode == ft_pixel_mode_mono) {
int memsize;
f->cached_glyphs[glyph_number].mono_available = 1;
/* set width, height, left, top */
f->cached_glyphs[glyph_number].width = ft_bmp->width;
f->cached_glyphs[glyph_number].height = ft_bmp->rows;
f->cached_glyphs[glyph_number].left = bmp_glyph->left;
f->cached_glyphs[glyph_number].top = bmp_glyph->top;
/* allocate bitmap */
memsize = ft_bmp->width * ft_bmp->rows * sizeof(unsigned char);
if (memsize > 0)
f->cached_glyphs[glyph_number].bmp = malloc(memsize);
else
f->cached_glyphs[glyph_number].bmp = NULL;
/* monochrome drawing */
if (memsize > 0) {
unsigned char *outbmp_p = f->cached_glyphs[glyph_number].bmp;
unsigned char *bmp_p;
int bmp_x, bmp_y, bit;
/* copy the FT character bitmap to ours */
bmp_p = ft_bmp->buffer;
for (bmp_y = 0; bmp_y < ft_bmp->rows; bmp_y++) {
unsigned char *next_bmp_p;
next_bmp_p = bmp_p + ft_bmp->pitch;
bit = 7;
for (bmp_x = 0; bmp_x < ft_bmp->width; bmp_x++) {
*outbmp_p = *bmp_p & (1 << bit);
outbmp_p++;
if (bit == 0) {
bit = 7;
bmp_p++;
}
else
bit--;
}
bmp_p = next_bmp_p;
}
}
}
FT_Done_Glyph(glyph);
}
/* render the aa bmp */
{
FT_Bitmap *ft_bmp;
FT_Glyph glyph;
FT_BitmapGlyph bmp_glyph;
FT_Glyph_Copy(new_glyph, &glyph);
/* only render glyph if it is not already a bitmap */
if (glyph->format != ft_glyph_format_bitmap)
FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, NULL, 1);
/* the FT rendered bitmap */
bmp_glyph = (FT_BitmapGlyph)glyph;
ft_bmp = &bmp_glyph->bitmap;
/* save only if the bitmap is really 8 bit */
if (ft_bmp->pixel_mode == ft_pixel_mode_grays) {
int memsize;
f->cached_glyphs[glyph_number].aa_available = 1;
/* set width, height, left, top */
f->cached_glyphs[glyph_number].aawidth = ft_bmp->width;
f->cached_glyphs[glyph_number].aaheight = ft_bmp->rows;
f->cached_glyphs[glyph_number].aaleft = bmp_glyph->left;
f->cached_glyphs[glyph_number].aatop = bmp_glyph->top;
/* allocate bitmap */
memsize = ft_bmp->width * ft_bmp->rows * sizeof(unsigned char);
if (memsize > 0)
f->cached_glyphs[glyph_number].aabmp = malloc(memsize);
else
f->cached_glyphs[glyph_number].aabmp = NULL;
/* aa drawing */
if (memsize > 0) {
unsigned char *outbmp_p = f->cached_glyphs[glyph_number].aabmp;
unsigned char *bmp_p;
int bmp_y;
unsigned char mul = 256 / ft_bmp->num_grays;
/* we set it to 0 because it is faster to test for false */
if (mul == 1)
mul = 0;
/* copy the FT character bitmap to ours */
bmp_p = ft_bmp->buffer;
for (bmp_y = 0; bmp_y < ft_bmp->rows; bmp_y++) {
unsigned char *next_bmp_p;
next_bmp_p = bmp_p + ft_bmp->pitch;
memcpy(outbmp_p, bmp_p, ft_bmp->width * sizeof(unsigned char));
/* we have to change our pixels if the numgrays is not 256 */
if (mul) {
unsigned char *p = outbmp_p;
unsigned char *p_end = p + ft_bmp->width;
for (; p < p_end; p++)
*p *= mul;
}
outbmp_p += ft_bmp->width;
bmp_p = next_bmp_p;
}
}
}
FT_Done_Glyph(glyph);
}
f->cached_glyphs[glyph_number].advancex = f->face->glyph->advance.x >> 6;
f->cached_glyphs[glyph_number].advancey = f->face->glyph->advance.y >> 6;
/* delete the glyph */
FT_Done_Glyph(new_glyph);
}
}
static void _alfont_new_cache_glyph(ALFONT_FONT *f) {
int i;
if (!f->cached_glyphs)
f->cached_glyphs = malloc(f->face->num_glyphs * sizeof(struct _ALFONT_CACHED_GLYPH));
for (i = 0; i < f->face->num_glyphs; i++) {
f->cached_glyphs[i].is_cached = 0;
f->cached_glyphs[i].bmp = NULL;
f->cached_glyphs[i].aabmp = NULL;
}
}
/* API */
int alfont_set_font_size(ALFONT_FONT *f, int h) {
int error, test_h, direction;
/* check the font doesn't already use that w and h */
if (h == f->face_h)
return ALFONT_OK;
else if (h <= 0)
return ALFONT_ERROR;
/* keep changing the size until the real size is not the one */
/* we want */
test_h = h;
direction = 0;
while (1) {
int real_height;
error = FT_Set_Pixel_Sizes(f->face, 0, test_h);
if (error)
break;
/* compare real height with asked height */
real_height = abs(f->face->size->metrics.ascender >> 6) + abs(f->face->size->metrics.descender >> 6);
if (real_height == h) {
/* we found the wanted height */
break;
}
/* check the direction */
if (direction == 0) {
/* direction still not set */
if (real_height > h)
direction = -1;
else
direction = 1;
}
/* check we didn't overpass it */
else if ((direction > 0) && (real_height > h)) {
/* decrease one and found */
test_h--;
FT_Set_Pixel_Sizes(f->face, 0, test_h);
break;
}
/* check we didn't surpass it */
else if ((direction < 0) && (real_height < h)) {
break;
}
test_h += direction;
/* check we arent at 0 */
if (test_h <= 0) {
error = TRUE;
break;
}
}
if (!error) {
_alfont_uncache_glyphs(f);
f->face_h = h;
f->real_face_h = test_h;
f->face_ascender = f->face->size->metrics.ascender >> 6;
return ALFONT_OK;
}
else {
FT_Set_Pixel_Sizes(f->face, 0, f->real_face_h);
return ALFONT_ERROR;
}
}
int alfont_get_font_height(ALFONT_FONT *f) {
return f->face_h;
}
void alfont_exit(void) {
if (alfont_inited) {
alfont_inited = 0;
FT_Done_FreeType(ft_library);
memset(&ft_library, 0, sizeof(ft_library));
}
}
int alfont_init(void) {
if (alfont_inited)
return 0;
else {
int error;
memset(&ft_library, 0, sizeof(ft_library));
error = FT_Init_FreeType(&ft_library);
if (!error)
alfont_inited = 1;
return error;
}
}
ALFONT_FONT *alfont_load_font(const char *filepathname) {
int error;
/* try to allocate the memory */
ALFONT_FONT *font = malloc(sizeof(ALFONT_FONT));
if (font == NULL)
return NULL;
/* clear the struct */
memset(font, 0, sizeof(ALFONT_FONT));
font->cached_glyphs = NULL;
/* we are loading from file, no mem buffer needed */
font->data = NULL;
font->data_size = 0;
/* load the font */
error = FT_New_Face(ft_library, filepathname, 0, &font->face);
if (error) {
free(font);
return NULL;
}
/* get if the font contains only fixed sizes */
if (!(font->face->face_flags & FT_FACE_FLAG_SCALABLE))
font->num_fixed_sizes = font->face->num_fixed_sizes;
else
font->num_fixed_sizes = -1;
_alfont_new_cache_glyph(font);
if (font->num_fixed_sizes < 0) {
font->fixed_sizes = malloc(sizeof(int));
_alfont_reget_fixed_sizes(font);
alfont_set_font_size(font, 8);
}
else {
font->fixed_sizes = malloc(sizeof(int) * (font->num_fixed_sizes + 1));
_alfont_reget_fixed_sizes(font);
/* set as current size the first found fixed size */
alfont_set_font_size(font, font->fixed_sizes[0]);
}
alfont_set_char_extra_spacing(font, 0);
//Initial Font attribute
font->language=NULL; /* Initial Language */
font->type=0; /* Initial Code Convert */
font->outline_top=0; /* Initial Font top outline width */
font->outline_bottom=0; /* Initial Font bottom outline width */
font->outline_left=0; /* Initial Font left outline width */
font->outline_right=0; /* Initial Font right outline width */
font->outline_color=0; /* Initial Font outline color */
font->outline_hollow=FALSE; /* Initial Font hollow(TRUE/FALSE) */
font->style=0; /* Initial Font Style */
font->underline=FALSE; /* Initial Font underline(TRUE/FALSE) */
font->underline_right=FALSE; /* Initial Extend right underline(TRUE/FALSE) */
font->underline_left=FALSE; /* Initial Extend left underline(TRUE/FALSE) */
font->background=FALSE; /* Initial Font Background Color(TRUE/FALSE) */
font->transparency=255; /* Initial Font transparency(0-255) */
font->autofix=FALSE; /* Initial Font autofix(TRUE/FALSE) */
font->precedingchar=0; /* Initial preceding character */
return font;
}
ALFONT_FONT *alfont_load_font_from_mem(const char *data, int data_len) {
int error;
char *new_data;
/* try to allocate the memory */
ALFONT_FONT *font = malloc(sizeof(ALFONT_FONT));
new_data = malloc(data_len);
if ((font == NULL) || (new_data == NULL)) {
if (font)
free(font);
if (new_data)
free(new_data);
return NULL;
}
/* clear the struct */
memset(font, 0, sizeof(ALFONT_FONT));
font->cached_glyphs = NULL;
/* copy user data to internal buffer */
font->data = new_data;
font->data_size = data_len;
memcpy((void *)font->data, (void *)data, data_len);
/* load the font */
error = FT_New_Memory_Face(ft_library, font->data, font->data_size, 0, &font->face);
if (error) {
free(font->data);
free(font);
return NULL;
}
/* get if the font contains only fixed sizes */
if (!(font->face->face_flags & FT_FACE_FLAG_SCALABLE))
font->num_fixed_sizes = font->face->num_fixed_sizes;
else
font->num_fixed_sizes = -1;
_alfont_new_cache_glyph(font);
if (font->num_fixed_sizes < 0) {
font->fixed_sizes = malloc(sizeof(int));
_alfont_reget_fixed_sizes(font);
alfont_set_font_size(font, 8);
}
else {
font->fixed_sizes = malloc(sizeof(int) * (font->num_fixed_sizes + 1));
_alfont_reget_fixed_sizes(font);
/* set as current size the first found fixed size */
alfont_set_font_size(font, font->fixed_sizes[0]);
}
alfont_set_char_extra_spacing(font, 0);
//Initial Font attribute
font->language=NULL; /* Initial Language */
font->type=0; /* Initial Code Convert */
font->outline_top=0; /* Initial Font top outline width */
font->outline_bottom=0; /* Initial Font bottom outline width */
font->outline_left=0; /* Initial Font left outline width */
font->outline_right=0; /* Initial Font right outline width */
font->outline_color=0; /* Initial Font outline color */
font->outline_hollow=FALSE; /* Initial Font hollow(TRUE/FALSE) */
font->style=0; /* Initial Font Style */
font->underline=FALSE; /* Initial Font underline(TRUE/FALSE) */
font->underline_right=FALSE; /* Initial Extend right underline(TRUE/FALSE) */
font->underline_left=FALSE; /* Initial Extend left underline(TRUE/FALSE) */
font->background=FALSE; /* Initial Font Background Color(TRUE/FALSE) */
font->transparency=255; /* Initial Font transparency(0-255) */
font->autofix=FALSE; /* Initial Font autofix(TRUE/FALSE) */
font->precedingchar=0; /* Initial preceding character */
return font;
}
int alfont_text_mode(int mode) {
int old_mode = alfont_textmode;
alfont_textmode = mode;
return old_mode;
}
void alfont_destroy_font(ALFONT_FONT *f) {
if (f == NULL)
return;
/* delete old glyphs */
_alfont_delete_glyphs(f);
/* delete the face */
FT_Done_Face(f->face);
if (f->fixed_sizes)
free(f->fixed_sizes);
/* deallocate the data */
if (f->data)
free(f->data);
/* deallocate the language string*/
if (f->language)
free(f->language);
free(f);
}
void alfont_textout_aa(BITMAP *bmp, ALFONT_FONT *f, const char *s, int x, int y, int color) {
alfont_textout_aa_ex(bmp, f, s, x, y, color, alfont_textmode);
}
void alfont_textout_aa_ex(BITMAP *bmp, ALFONT_FONT *f, const char *s, int x, int y, int color, int backg) {
char *lpszW;
char *lpszW_tmp;
int x_tmp;
int max_advancex;
char *lpszW_pointer=NULL; //used for freeing string
char *s_pointer=NULL; //used for original string fixed by autofix
char *s_pointer_temp=NULL; //temporary used for autofix string
char *precedingchar_pointer=NULL; //used for precedingchar character
int nLen;
int ret; //decide that if the ASCII Code convert to Unicode Code is all OK when used for autofix string or used for general convert.
int character;
int alpha_table[256];
int last_glyph_index;
int first_x=0, final_x=0, final_y=0;
int curr_uformat;
int first_flag=TRUE; //First Char flag
BITMAP *masked_bmp; //the masked bmp used by Font hollow
#ifdef ALFONT_DOS
iconv_t c_pt;
size_t fromlen, tolen;
char *sin, *sout;
#endif
if (s == NULL) {
return;
}
nLen = strlen(s) + 1;
s_pointer = (char *)malloc(nLen*sizeof(char));
memset(s_pointer, 0, nLen);
strcpy(s_pointer, s);
//Auto Fix for cutted string
//For ASCII convert to unicode
//Add the previous character to the s string
//If find the cutted character, store it from the converted s string and remove it from the original s string
if (f->autofix==TRUE) {
if (f->type==2) {
curr_uformat=get_uformat();
#ifdef ALFONT_DOS
if ((c_pt = iconv_open("UTF-16LE", f->language)) != (iconv_t)-1) {
fromlen = strlen(s) + 1;
tolen = MB_CUR_MAX * fromlen * (sizeof(wchar_t) + 1);
//add the previous character to the s string
if (f->precedingchar != 0) {
free(s_pointer);
fromlen = strlen(s) + 1 + 1;
tolen = MB_CUR_MAX * fromlen * (sizeof(wchar_t) + 1);
s_pointer = (char *)malloc(tolen*sizeof(char));
memset(s_pointer, 0, tolen);
precedingchar_pointer=(char *)malloc(2*sizeof(char));
memset(precedingchar_pointer, 0, 2);
sprintf(precedingchar_pointer, "%c", f->precedingchar);
strcpy(s_pointer,precedingchar_pointer);
if (precedingchar_pointer) {
free(precedingchar_pointer);
precedingchar_pointer = NULL;
}
strcat(s_pointer, s);
f->precedingchar = 0;
}
iconv(c_pt, NULL, NULL, NULL, NULL);
lpszW = (char *)malloc(tolen*sizeof(char));
memset(lpszW, 0, tolen);
sin = s;
sout = lpszW;
ret = iconv(c_pt, &sin, &fromlen, &sout, &tolen);
iconv_close(c_pt);
s_pointer_temp = s_pointer;
if (ret == -1) { //If the ret is -1, the final one will can be a shortcutted character.
//store the last character to precedingchar character
//get the final character
set_uformat(curr_uformat);
while (*s_pointer_temp != '\0') {
f->precedingchar = *s_pointer_temp;
s_pointer_temp++;
}
//remove the final character
s_pointer_temp--;
*s_pointer_temp = '\0';
}
if (lpszW) {
free(lpszW);
lpszW = NULL;
}
}
#else
#ifdef ALFONT_LINUX
nLen = strlen(s_pointer) * 5 + 1;
#else
nLen = strlen(s_pointer) + 1;
#endif
//add the previous character to the s string
if (f->precedingchar != 0) {
free(s_pointer);
nLen = strlen(s) + 1 + 1;
s_pointer = (char *)malloc(nLen*sizeof(char));
memset(s_pointer, 0, nLen);
precedingchar_pointer=(char *)malloc(2*sizeof(char));
memset(precedingchar_pointer, 0, 2);
sprintf(precedingchar_pointer, "%c", f->precedingchar);
strcpy(s_pointer,precedingchar_pointer);
if (precedingchar_pointer) {
free(precedingchar_pointer);
precedingchar_pointer = NULL;
}
strcat(s_pointer, s);
f->precedingchar = 0;
}
setlocale(LC_CTYPE,f->language);
set_uformat(U_UNICODE);
lpszW = (char *)malloc(nLen*sizeof(wchar_t));
memset(lpszW, 0, nLen);
ret = mbstowcs((wchar_t *)lpszW, s_pointer, nLen);
s_pointer_temp = s_pointer;
if (ret == -1) { //If the ret is -1, the final one will can be a shortcutted character.
//store the last character to precedingchar character
//get the final character
set_uformat(curr_uformat);
while (*s_pointer_temp != '\0') {
f->precedingchar = *s_pointer_temp;
s_pointer_temp++;
}
//remove the final character
s_pointer_temp--;
*s_pointer_temp = '\0';
}
if (lpszW) {
free(lpszW);
lpszW = NULL;
}
#endif
//recover to original codepage
set_uformat(curr_uformat);
}
}
//Font Code Convert
if (f->type==1) {
#ifdef ALFONT_DOS
if ((c_pt = iconv_open(f->language, "UTF-16LE")) == (iconv_t)-1) {
lpszW = (char *)s_pointer;
}
else {
iconv(c_pt, NULL, NULL, NULL, NULL);
fromlen = strlen(s_pointer) + 1;
tolen = MB_CUR_MAX * fromlen * (sizeof(wchar_t) + 1);
lpszW = (char *)malloc(tolen*sizeof(char));
memset(lpszW, 0, tolen);
lpszW_pointer = lpszW;
sin = s_pointer;
sout = lpszW;
ret = iconv(c_pt, &sin, &fromlen, &sout, &tolen);
iconv_close(c_pt);
if (ret == -1) {
lpszW = (char *)s_pointer;
}
}
#else
setlocale(LC_CTYPE,f->language);
nLen= MB_CUR_MAX * wcslen((const wchar_t*)s_pointer) + 1;
lpszW = (char *)malloc(nLen*sizeof(char));
memset(lpszW, 0, nLen);
lpszW_pointer = lpszW;
wcstombs(lpszW, (const wchar_t *)s_pointer, nLen);
#endif
}
else if(f->type==2) {
curr_uformat=get_uformat();
#ifdef ALFONT_DOS
if ((c_pt = iconv_open("UTF-16LE", f->language)) == (iconv_t)-1) {
lpszW = (char *)s_pointer;
}
else {
iconv(c_pt, NULL, NULL, NULL, NULL);
fromlen = strlen(s_pointer) + 1;
tolen = MB_CUR_MAX * fromlen * (sizeof(wchar_t) + 1);
lpszW = (char *)malloc(tolen*sizeof(char));
memset(lpszW, 0, tolen);
lpszW_pointer = lpszW;
sin = s_pointer;
sout = lpszW;
ret = iconv(c_pt, &sin, &fromlen, &sout, &tolen);
iconv_close(c_pt);
if (ret == -1) {
lpszW = (char *)s_pointer;
}
else {
set_uformat(U_UNICODE);
}
}
#else
setlocale(LC_CTYPE,f->language);
set_uformat(U_UNICODE);
#ifdef ALFONT_LINUX
nLen = strlen(s_pointer) * 5 + 1;
#else
nLen= strlen(s_pointer) + 1;
#endif
lpszW = (char *)malloc(nLen*sizeof(wchar_t));
memset(lpszW, 0, nLen);
lpszW_pointer = lpszW;
mbstowcs((wchar_t *)lpszW, s_pointer, nLen);
#endif
}
else {
#ifdef ALFONT_LINUX
set_uformat(U_UTF8);
nLen= ustrlen(s_pointer) + 1;
#endif
lpszW = (char *)s_pointer;
}
/* is it under or over or too far to the right of the clipping rect then
we can assume the string is clipped */
if ((y + f->face_h < bmp->ct) || (y > bmp->cb) || (x > bmp->cr))
return;
//build transparency
if (f->transparency!=255) {
if (bitmap_color_depth(bmp)>8) {
drawing_mode(DRAW_MODE_TRANS,NULL,0,0);
set_trans_blender(0,0,0,f->transparency);
}
}
else {
drawing_mode(DRAW_MODE_SOLID,NULL,0,0);
}
/* if we are doing opaque mode, draw a rect and init our table*/
if (backg >= 0) {
int i, r, g, b, br, bg, bb, ir, ig, ib;
int blendr, blendg, blendb;
if(f->background==TRUE) {
rectfill(bmp, x, y, x + alfont_text_length(f, s_pointer) - 1, y + f->face_h - 1, backg);
}
/* get the color rgb */
r = getr(color); g = getg(color); b = getb(color);
/* get the background rgb */
br = getr(backg); bg = getg(backg); bb = getb(backg);
/* get increments */
ir = (r == br) ? 0 : (r + 1) - br;
ig = (g == bg) ? 0 : (g + 1) - bg;
ib = (b == bb) ? 0 : (b + 1) - bb;
blendr = br << 8;
blendg = bg << 8;
blendb = bb << 8;
/* blend both values and make our alpha table */
for (i = 0; i < 256; i++) {
alpha_table[i] = makecol(blendr >> 8, blendg >> 8, blendb >> 8);
blendr += ir;
blendg += ig;
blendb += ib;
}
}
/* draw char by char (using allegro unicode funcs) */
acquire_bitmap(bmp);
last_glyph_index = 0;
if (f->fixed_width == TRUE)
{
lpszW_tmp = lpszW;
x_tmp = x;
max_advancex = 0;
_alfont_uncache_glyphs(f);
#ifdef ALFONT_LINUX //Fix for Linux Unicode System(be converted)
for (character = ugetxc((const char**)&lpszW_tmp); character != 0; character = ugetxc((const char**)&lpszW_tmp),character = ugetxc((const char**)&lpszW_tmp)) {
#else
for (character = ugetxc((const char**)&lpszW_tmp); character != 0; character = ugetxc((const char**)&lpszW_tmp)) {
#endif
int glyph_index_tmp;
struct _ALFONT_CACHED_GLYPH cglyph_tmp;
#ifdef ALFONT_LINUX //Recover for Linux Unicode System Fixed
if(f->type!=2) {
lpszW_tmp--;
}
#endif
/* if left side of char farther than right side of clipping, we are done */
if (x_tmp > bmp->cr)
break;
/* get the character out of the font */
if (f->face->charmap)
glyph_index_tmp = FT_Get_Char_Index(f->face, character);
else
glyph_index_tmp = character;
/* cache the glyph */
_alfont_cache_glyph(f, glyph_index_tmp);
cglyph_tmp = f->cached_glyphs[glyph_index_tmp];
if (max_advancex < f->cached_glyphs[glyph_index_tmp].advancex)
max_advancex = f->cached_glyphs[glyph_index_tmp].advancex;
/* advance */
if (cglyph_tmp.advancex)
x_tmp += cglyph_tmp.advancex + f->ch_spacing;
}
}
#ifdef ALFONT_LINUX //Fix for Linux Unicode System(be converted)
for (character = ugetxc((const char**)&lpszW); character != 0; character = ugetxc((const char**)&lpszW),character = ugetxc((const char**)&lpszW)) {
#else
for (character = ugetxc((const char**)&lpszW); character != 0; character = ugetxc((const char**)&lpszW)) {
#endif
int real_x, real_y, glyph_index;
struct _ALFONT_CACHED_GLYPH cglyph;
#ifdef ALFONT_LINUX //Recover for Linux Unicode System Fixed
if(f->type!=2) {
lpszW--;
}
#endif
/* if left side of char farther than right side of clipping, we are done */
if (x > bmp->cr)
break;
/* get the character out of the font */
if (f->face->charmap)
glyph_index = FT_Get_Char_Index(f->face, character);
else
glyph_index = character;
/* cache the glyph */
_alfont_cache_glyph(f, glyph_index);
if (f->fixed_width == TRUE)
f->cached_glyphs[glyph_index].advancex = max_advancex;
cglyph = f->cached_glyphs[glyph_index];
/* calculate drawing coords */
real_x = x + cglyph.aaleft;
real_y = (y - cglyph.aatop) + f->face_ascender;
/* apply kerning */
/*if (last_glyph_index) {
FT_Vector v;
FT_Get_Kerning(f->face, last_glyph_index, glyph_index, ft_kerning_default, &v);
real_x += v.x >> 6;
real_y += v.y >> 6;
}*/
last_glyph_index = glyph_index;