- Online Community Forums » Allegro Development » letters are rendered in their seperated form, this may require glyph hook maybe?

This thread is locked; no one can reply to it. rss feed Print
letters are rendered in their seperated form, this may require glyph hook maybe?
dhia hassen
Member #16,240
March 2016

Hello everyone, i need some help :)

Trying to add arabic language to an "Text2Image" API I developed

as indicated in the attached image , arabic letters are rendered seperated by allegro , that's now how you expect arabic letters to look ,the renderer draws letters in their standalone form. Correct Arabic is written in a script, in which certain letters are joined, according to rules. Therefore, each letter has up to 4 forms:


Attached only to previous letter

Attached only to next letter

Attached to both

A proper Arabic word processor automatically chooses the correct form of each letter, according to the rules. In fact, as you type you'll often see the previous letter change form as you type the next letter.

It would be great is i can use some existing allegro API calls to pick the current glyphto be rendered based on the previous and next letter ??? a sort a a hook maybe. is such API doesnt exist , any idea of how i may be able to add it ? not assuming it doesnt exist , thanks alot

code used to produce the attached image

1#include <allegro5/allegro.h> 2#include <allegro5/allegro_font.h> 3#include <allegro5/allegro_ttf.h>// zawsze dodajemy plik nagłówkowy ttf 4ALLEGRO_USTR * Tekst = al_ustr_new ("حبك أنا كثير ، يا حبيبي ضلك طل علي "); // Kodowanie UTF8 w C::B 5int main() 6{ 7 al_init(); 8 al_init_font_addon(); 9 al_init_ttf_addon(); // pamiętaj o inicjowaniu! 10 al_install_keyboard(); 11 ALLEGRO_KEYBOARD_STATE klawiatura; 12 al_set_new_display_flags(ALLEGRO_WINDOWED); 13 ALLEGRO_DISPLAY *okno = al_create_display(450,300); 14 al_set_window_title(okno,"Font TTF"); 15 ALLEGRO_FONT * font_ttf = al_load_ttf_font("fonts/DejaVuSans.ttf",24, 0);// wskaźnik do czcionki ttf 16 ALLEGRO_FONT * font_ttf_16 = al_load_ttf_font("fonts/DejaVuSans.ttf",16, 1); 17 ALLEGRO_FONT * font_ttf_24 = al_load_ttf_font("fonts/DejaVuSans.ttf",24, 2); 18 ALLEGRO_FONT * font_ttf_30 = al_load_ttf_font("fonts/DejaVuSans.ttf",30, 4); 19 while(!al_key_down(&klawiatura, ALLEGRO_KEY_ESCAPE)) 20 { 21 al_get_keyboard_state(&klawiatura); 22 al_clear_to_color(al_map_rgb_f(0.5,0.5,0.5)); 23 al_draw_ustr(font_ttf,al_map_rgb (0,0,255), 15, 40, 0,Tekst); 24 al_draw_textf(font_ttf_16,al_map_rgb(255,255,255), 10, 142,0,"حبك أنا كثير ، يا حبيبي ضلك طل علي %s",ALLEGRO_VERSION_STR"!"); 25 al_draw_textf(font_ttf_24,al_map_rgb(255,255,255), 10, 158,0," حبك أنا كثير ، يا حبيبي ضلك طل علي %s",ALLEGRO_VERSION_STR"!"); 26 al_draw_textf(font_ttf_30,al_map_rgb(255,255,255), 10, 180,0," حبك أنا كثير ، يا حبيبي ضلك طل علي %s",ALLEGRO_VERSION_STR"!"); 27 al_draw_text (font_ttf_30,al_map_rgb(255,255, 0), 55, 220,0,"I حبك أنا كثير ، يا حبيبي ضلك طل علي ."); 28 al_flip_display(); 29 } 30 al_destroy_font(font_ttf); 31 al_destroy_font(font_ttf_16); 32 al_destroy_font(font_ttf_24); 33 al_destroy_font(font_ttf_30); 34 al_destroy_display(okno); 35 return 0; 36}

SDL 2 + SDL ttf seem to handle this pretty fine as indicated in the second attached image

Peter Hull
Member #1,136
March 2001

I think (but am not sure) that Freetype on its own won't do this. See for example:
so you would need a text shaping engine like HarfBuzz.

Where did you get the SDL screenshot from? - it's always worth finding out how they did it.

dhia hassen
Member #16,240
March 2016

You are right , no other graphics library seem to do that work even for other languages , each time a seperate font engine is used.

I have done some manual tweaking and was able to render some specific arabic text in allegro properly. ( works only with CONST char * now -- const only -- ).

to automate the process an algorithm is needed , a simple symbole replacement preprocessing

This is where i got the sdl image from

I talked to them and they seem to simple perform that preprocessing i just mentioned , nothing fancy

I didnt want to automate the preprocessing, i found a php library that does it ( PHP same to have the exact problem as allegro, so allegro is doing good )

after some deep understanding of this whole thing , and 12 hours of investigation i found that the best choice is to use pygame , pygame uses sdl2 internally , and i then have access so a python library that does the preprocessing

If we add some glyph hooks to allegro i will be able to implement this for allegro

I think I'd follow

HarfBuzz to translate utf8 string to a list of glyphs and position

FreeType to rasterize the needed glyphs with SDF in a texture atlas

And in a custom shader, choose output alpha using the SDF distances info in the texture

Good discussion about SDF

Member #7,827
October 2006

Can we just add the same sort of code in that example into Allegro? It seems the main workhorse functions are hb_buffer_get_glyph_infos and hb_buffer_get_glyph_positions, after that it's the usual FT_Load_Glyph.

I'm not sure what the glyph callback would look like, can you explain what it would do?

Anyway, I filed so we don't forget about this.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Edgar Reynaldo
Major Reynaldo
May 2007

I'm not sure if this is what you're talking about, but it would be nice to have a glyph hook for drawing each glyph in a custom color or shading, as well as with the proper spacing for things like the suggested languages.

Bump for attn. :-*

Go to: