@@ -495,44 +495,81 @@ static int get_kerning(ALLEGRO_TTF_FONT_DATA const *data, FT_Face face,
495
495
}
496
496
497
497
498
- static int render_glyph (ALLEGRO_FONT const * f , ALLEGRO_COLOR color ,
499
- int prev_ft_index , int ft_index , int32_t ch , float xpos , float ypos )
498
+ static bool ttf_get_glyph_worker (ALLEGRO_FONT const * f , int prev_ft_index , int ft_index , int prev_codepoint , int codepoint , ALLEGRO_GLYPH * info )
500
499
{
501
500
ALLEGRO_TTF_FONT_DATA * data = f -> data ;
502
501
FT_Face face = data -> face ;
503
502
ALLEGRO_TTF_GLYPH_DATA * glyph ;
504
503
int advance = 0 ;
505
504
506
505
if (!get_glyph (data , ft_index , & glyph )) {
507
- if (f -> fallback ) {
508
- al_draw_glyph (f -> fallback , color , xpos , ypos , ch );
509
- return al_get_glyph_advance (f -> fallback , ch ,
510
- ALLEGRO_NO_KERNING );
511
- }
506
+ if (f -> fallback )
507
+ return f -> fallback -> vtable -> get_glyph (f -> fallback , prev_codepoint , codepoint , info );
512
508
else {
513
509
get_glyph (data , 0 , & glyph );
514
510
ft_index = 0 ;
515
511
}
516
512
}
513
+
517
514
cache_glyph (data , face , ft_index , glyph , false);
518
515
519
516
advance += get_kerning (data , face , prev_ft_index , ft_index );
520
517
521
518
if (glyph -> page_bitmap ) {
522
- /* Each glyph has a 1-pixel border all around. */
523
- al_draw_tinted_bitmap_region (glyph -> page_bitmap , color ,
524
- glyph -> region .x + 1 , glyph -> region .y + 1 ,
525
- glyph -> region .w - 2 , glyph -> region .h - 2 ,
526
- xpos + glyph -> offset_x + advance ,
527
- ypos + glyph -> offset_y , 0 );
519
+ info -> bitmap = glyph -> page_bitmap ;
520
+ info -> x = glyph -> region .x + 1 ;
521
+ info -> y = glyph -> region .y + 1 ;
522
+ info -> w = glyph -> region .w - 2 ;
523
+ info -> h = glyph -> region .h - 2 ;
524
+ info -> kerning = advance ;
525
+ info -> offset_x = glyph -> offset_x ;
526
+ info -> offset_y = glyph -> offset_y ;
528
527
}
529
528
else if (glyph -> region .x > 0 ) {
530
529
ALLEGRO_ERROR ("Glyph %d not on any page.\n" , ft_index );
530
+ return false;
531
+ }
532
+ else {
533
+ info -> bitmap = 0 ;
531
534
}
532
535
533
536
advance += glyph -> advance ;
534
537
535
- return advance ;
538
+ info -> advance = advance ;
539
+
540
+ return true;
541
+ }
542
+
543
+
544
+ static bool ttf_get_glyph (ALLEGRO_FONT const * f , int prev_codepoint , int codepoint , ALLEGRO_GLYPH * glyph )
545
+ {
546
+ ALLEGRO_TTF_FONT_DATA * data = f -> data ;
547
+ FT_Face face = data -> face ;
548
+ int prev_ft_index = (prev_codepoint == -1 ) ? -1 : FT_Get_Char_Index (face , prev_codepoint );
549
+ int ft_index = FT_Get_Char_Index (face , codepoint );
550
+ return ttf_get_glyph_worker (f , prev_ft_index , ft_index , prev_codepoint , codepoint , glyph );
551
+ }
552
+
553
+
554
+ static int render_glyph (ALLEGRO_FONT const * f , ALLEGRO_COLOR color ,
555
+ int prev_ft_index , int ft_index , int32_t prev_ch , int32_t ch , float xpos , float ypos )
556
+ {
557
+ ALLEGRO_GLYPH glyph ;
558
+
559
+ if (ttf_get_glyph_worker (f , prev_ft_index , ft_index , prev_ch , ch , & glyph ) == false)
560
+ return 0 ;
561
+
562
+ if (glyph .bitmap != NULL ) {
563
+ al_draw_tinted_bitmap_region (
564
+ glyph .bitmap , color ,
565
+ glyph .x , glyph .y , glyph .w , glyph .h ,
566
+ xpos + glyph .offset_x + glyph .kerning ,
567
+ ypos + glyph .offset_y ,
568
+ 0
569
+ );
570
+ }
571
+
572
+ return glyph .advance ;
536
573
}
537
574
538
575
@@ -578,10 +615,10 @@ static int ttf_render_char(ALLEGRO_FONT const *f, ALLEGRO_COLOR color,
578
615
FT_Face face = data -> face ;
579
616
int advance = 0 ;
580
617
int32_t ch32 = (int32_t ) ch ;
581
-
618
+
582
619
int ft_index = FT_Get_Char_Index (face , ch32 );
583
- advance = render_glyph (f , color , -1 , ft_index , ch , xpos , ypos );
584
-
620
+ advance = render_glyph (f , color , -1 , ft_index , -1 , ch , xpos , ypos );
621
+
585
622
return advance ;
586
623
}
587
624
@@ -591,7 +628,7 @@ static int ttf_char_length(ALLEGRO_FONT const *f, int ch)
591
628
int result ;
592
629
ALLEGRO_TTF_FONT_DATA * data = f -> data ;
593
630
ALLEGRO_TTF_GLYPH_DATA * glyph ;
594
- FT_Face face = data -> face ;
631
+ FT_Face face = data -> face ;
595
632
int ft_index = FT_Get_Char_Index (face , ch );
596
633
if (!get_glyph (data , ft_index , & glyph )) {
597
634
if (f -> fallback ) {
@@ -604,7 +641,7 @@ static int ttf_char_length(ALLEGRO_FONT const *f, int ch)
604
641
}
605
642
cache_glyph (data , face , ft_index , glyph , false);
606
643
result = glyph -> region .w - 2 ;
607
-
644
+
608
645
return result ;
609
646
}
610
647
@@ -617,6 +654,7 @@ static int ttf_render(ALLEGRO_FONT const *f, ALLEGRO_COLOR color,
617
654
int pos = 0 ;
618
655
int advance = 0 ;
619
656
int prev_ft_index = -1 ;
657
+ int32_t prev_ch = -1 ;
620
658
int32_t ch ;
621
659
bool hold ;
622
660
@@ -625,9 +663,10 @@ static int ttf_render(ALLEGRO_FONT const *f, ALLEGRO_COLOR color,
625
663
626
664
while ((ch = al_ustr_get_next (text , & pos )) >= 0 ) {
627
665
int ft_index = FT_Get_Char_Index (face , ch );
628
- advance += render_glyph (f , color , prev_ft_index , ft_index , ch ,
666
+ advance += render_glyph (f , color , prev_ft_index , ft_index , prev_ch , ch ,
629
667
x + advance , y );
630
668
prev_ft_index = ft_index ;
669
+ prev_ch = ch ;
631
670
}
632
671
633
672
al_hold_bitmap_drawing (hold );
@@ -699,7 +738,7 @@ static void ttf_get_text_dimensions(ALLEGRO_FONT const *f,
699
738
700
739
* bby = ymin ;
701
740
* bbw = x - * bbx ;
702
- * bbh = ymax - ymin ;
741
+ * bbh = ymax - ymin ;
703
742
}
704
743
705
744
@@ -803,7 +842,7 @@ ALLEGRO_FONT *al_load_ttf_font_stretch_f(ALLEGRO_FILE *file,
803
842
al_get_config_value (system_cfg , "ttf" , "max_page_size" );
804
843
const char * cache_str =
805
844
al_get_config_value (system_cfg , "ttf" , "cache_text" );
806
- const char * skip_cache_misses_str =
845
+ const char * skip_cache_misses_str =
807
846
al_get_config_value (system_cfg , "ttf" , "skip_cache_misses" );
808
847
809
848
if ((h > 0 && w < 0 ) || (h < 0 && w > 0 )) {
@@ -904,7 +943,7 @@ ALLEGRO_FONT *al_load_ttf_font_stretch_f(ALLEGRO_FILE *file,
904
943
905
944
_al_vector_init (& data -> glyph_ranges , sizeof (ALLEGRO_TTF_GLYPH_RANGE ));
906
945
_al_vector_init (& data -> page_bitmaps , sizeof (ALLEGRO_BITMAP * ));
907
-
946
+
908
947
if (data -> skip_cache_misses ) {
909
948
cache_glyphs (data , "\0" , 1 );
910
949
}
@@ -992,7 +1031,7 @@ static bool ttf_get_glyph_dimensions(ALLEGRO_FONT const *f,
992
1031
{
993
1032
ALLEGRO_TTF_FONT_DATA * data = f -> data ;
994
1033
ALLEGRO_TTF_GLYPH_DATA * glyph ;
995
- FT_Face face = data -> face ;
1034
+ FT_Face face = data -> face ;
996
1035
int ft_index = FT_Get_Char_Index (face , codepoint );
997
1036
if (!get_glyph (data , ft_index , & glyph )) {
998
1037
if (f -> fallback ) {
@@ -1009,7 +1048,7 @@ static bool ttf_get_glyph_dimensions(ALLEGRO_FONT const *f,
1009
1048
* bbw = glyph -> region .w - 2 ;
1010
1049
* bbh = glyph -> region .h - 2 ;
1011
1050
* bby = glyph -> offset_y ;
1012
-
1051
+
1013
1052
return true;
1014
1053
}
1015
1054
@@ -1019,10 +1058,10 @@ static int ttf_get_glyph_advance(ALLEGRO_FONT const *f, int codepoint1,
1019
1058
ALLEGRO_TTF_FONT_DATA * data = f -> data ;
1020
1059
FT_Face face = data -> face ;
1021
1060
int ft_index = FT_Get_Char_Index (face , codepoint1 );
1022
- ALLEGRO_TTF_GLYPH_DATA * glyph ;
1061
+ ALLEGRO_TTF_GLYPH_DATA * glyph ;
1023
1062
int kerning = 0 ;
1024
1063
int advance = 0 ;
1025
-
1064
+
1026
1065
if (codepoint1 == ALLEGRO_NO_KERNING ) {
1027
1066
return 0 ;
1028
1067
}
@@ -1037,13 +1076,13 @@ static int ttf_get_glyph_advance(ALLEGRO_FONT const *f, int codepoint1,
1037
1076
}
1038
1077
}
1039
1078
cache_glyph (data , face , ft_index , glyph , false);
1040
-
1041
- if (codepoint2 != ALLEGRO_NO_KERNING ) {
1079
+
1080
+ if (codepoint2 != ALLEGRO_NO_KERNING ) {
1042
1081
int ft_index1 = FT_Get_Char_Index (face , codepoint1 );
1043
- int ft_index2 = FT_Get_Char_Index (face , codepoint2 );
1082
+ int ft_index2 = FT_Get_Char_Index (face , codepoint2 );
1044
1083
kerning = get_kerning (data , face , ft_index1 , ft_index2 );
1045
1084
}
1046
-
1085
+
1047
1086
advance = glyph -> advance ;
1048
1087
return advance + kerning ;
1049
1088
}
@@ -1072,6 +1111,7 @@ bool al_init_ttf_addon(void)
1072
1111
vt .get_font_ranges = ttf_get_font_ranges ;
1073
1112
vt .get_glyph_dimensions = ttf_get_glyph_dimensions ;
1074
1113
vt .get_glyph_advance = ttf_get_glyph_advance ;
1114
+ vt .get_glyph = ttf_get_glyph ;
1075
1115
1076
1116
al_register_font_loader (".ttf" , al_load_ttf_font );
1077
1117
0 commit comments