Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » About utf8 fonts

This thread is locked; no one can reply to it. rss feed Print
About utf8 fonts
keprast
Member #16,794
January 2018

I try to use my native language: Chinese.
It doesn't show up.
English can be displayed.

??? Utf8 supports Chinese, why so?

torhu
Member #2,727
September 2002
avatar

Are you using a font that supports Chinese characters?

beoran
Member #12,636
March 2011

Also, make sure that the input text really is in utf8 format. If you use visual studio, constant C strings will normally not be utf8. Try to read the string from a file in stead.

keprast
Member #16,794
January 2018

text?

Rodolfo Lam
Member #16,045
August 2015

Input text here refers in a very general way to any string that you might display in your program.

This string could either be generated by your own program or input by the user through some sort of textbox you design.

Unfortunately, normal C-style strings are not really UTF-8 compatible. It just so happens that normal English characters map to the same values in both ASCII and UTF-8 encodings.

torhu
Member #2,727
September 2002
avatar

I don't think that he understands a lot of English, maybe it's better to rephrase a bit?

Rodolfo Lam
Member #16,045
August 2015

Yeah... I figured that might be part of the issue.

Still, UTF-8 is somewhat complicated to understand at first. Many pieces need to be fitted in the correct order and way for it to work as intended.

torhu
Member #2,727
September 2002
avatar

This probably a smart guy, but he lives in a different world language-wise then either of us. People from South Asia have on average higher IQ than Europeans, you know :p

Neil Roy
Member #2,229
April 2002
avatar

C strings are 1 byte, UTF-8 needs much more to store the larger numbers.

Allegro has UTF-8 functions. Also, the IDE you use to program with may need to have UTF8 enabled. I don't know about Visual Studio, but with Code::Blocks you go to SETTINGS->EDITOR and under the ENCODING SETTINGS tab you can set the encoding it uses to UTF-8.

Notepad++ also has a similar option. I use it sometimes as well. I would imagine Visual Studio has such an option, I just wouldn't know where.

In my Deluxe Pacman 2 game, which supports UTF-8, I use functions in my Highscore routine like:

al_fread32le() to read in names etc... from the high score file.
al_ustr_new_from_buffer() etc...

here's a short snippit from my highscore function which reads in names from the file...

cstrsize = al_fread32le(hiscore_file[i]);
cstrname = (char *)malloc(cstrsize);  // allocate memory for name
if(cstrname == NULL) {
   al_fclose(hiscore_file[i]);
   printf("%s(%d): memory allocation failed.\n", __FILE__, __LINE__);
   return true;
}
al_fread(hiscore_file[i], cstrname, cstrsize);
// assign the c string to the ustr variable from buffer
hiscore[i][j].name = al_ustr_new_from_buffer(cstrname, cstrsize);
free(cstrname);

And my hiscore[].name is an ALLEGRO_USTR variable.

---
“I love you too.” - last words of Wanda Roy

Peter Hull
Member #1,136
March 2001

Hi Keprast,
This was discussed a while ago - see https://www.allegro.cc/forums/thread/611547

I was able to display some chinese characters on Windows using the following program:

#SelectExpand
1#include "stdafx.h" 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_font.h> 4#include <allegro5/allegro_ttf.h> 5 6int main() 7{ 8 al_init(); 9 al_init_font_addon(); 10 al_init_ttf_addon(); 11 ALLEGRO_DISPLAY* display = al_create_display(320, 240); 12 ALLEGRO_FONT* font = al_load_font("c:\\windows\\fonts\\simsun.ttc", 12, 0); 13 al_draw_text(font, al_map_rgb(255, 255, 255), 10.0f, 100.0f, 0, "你好,世界"); 14 al_flip_display(); 15 al_rest(10.0); 16 return 0; 17}

{"name":"611509","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/4\/148e9c34c58c0c2586c120b96142ba5e.jpg","w":343,"h":296,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/4\/148e9c34c58c0c2586c120b96142ba5e"}611509

The important thing was to follow this advice: https://docs.microsoft.com/en-gb/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8
which was to add /utf-8 to the options for the project.

Please note I don't speak Chinese so I apologise if the text is wrong!
[edit]: I think the first font I put up was Japanese, sorry. Sim Sun is a Chinese font though, isn't it? (https://www.pinyinjoe.com/windows-7/win7-chinese-fonts.htm)

Go to: