Imgui Custom Chinese character set

Although Imgui does not support rich text and can not display the same time two or more of size, only a simple tune-up GUI with use, but this does not prevent it popular, some things still need easy to win public affection.

Back to the topic.

Examples Imgui default is English font to display the Chinese need to add Chinese font, such as

io.Fonts->AddFontFromFileTTF("Asset/Font/SourceHanSansCN-Normal.otf", 14.0f, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());

 

C ++ 11 then calls the code similar to the following

:: Text ImGui (U8 " display Chinese try to show " );

 

Then you will encounter situations Sao word did not show up

F12 to enter this function found this function to provide only 2,500 Chinese characters, I would like to display the full you need to call the following

io.Fonts->AddFontFromFileTTF("Asset/Font/SourceHanSansCN-Normal.otf", 14.0f, nullptr, io.Fonts->GetGlyphRangesChineseFull());

 

It is to

GetGlyphRangesChineseSimplifiedCommon

Replaced

GetGlyphRangesChineseFull

Dropouts problem is solved, but brought new problems, run it you will find a lot slower, because many of them have endured more than my range, so a little simple research under the custom character set.

========================================

First you need a commonly used Chinese characters table file, this thing is whether Baidu or gayhub can be found, to my knowledge there are 3500,7000,9000 and other characters table, the longer the more compile time, choose according to their needs.

Imgui because this character is a static one-time texture cache, this really sucks, casual look SFML2 have a dynamic character set cache code.

Then add the following test code, do not Tucao style, written test, did not mind too

FILE* inFile = fopen("Asset/Font/chinese3500.txt", "rb");
//FILE* inFile = fopen("Asset/Font/chinese9000.txt", "rb");

unsigned char *charBuf;

fseek(inFile, 0, SEEK_END);
int fileLen = ftell(inFile);
charBuf = new unsigned char[fileLen];
fseek(inFile, 0, SEEK_SET);
fread(charBuf, fileLen, 1, inFile);

fclose(inFile);

ImVector<ImWchar> myRange;
ImFontGlyphRangesBuilder myGlyph;

//ImWchar base_ranges[] = // not zero-terminated
//{
//    0x0020, 0x00FF, // Basic Latin + Latin Supplement
//    0x2000, 0x206F, // General Punctuation
//    0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana
//    0x31F0, 0x31FF, // Katakana Phonetic Extensions
//    0xFF00, 0xFFEF  // Half-width characters
//};

//myGlyph.AddRanges(base_ranges);

//myGlyph.AddText(u8"显示中文试试骚");

myGlyph.AddText((const char*)charBuf); myGlyph.BuildRanges(&myRange); delete[] charBuf; ImFont* font1 = io.Fonts->AddFontFromFileTTF("Asset/Font/SourceHanSansCN-Normal.otf", 14.0f, nullptr, myRange.Data); ImFont* font2 = io.Fonts->AddFontFromFileTTF("Asset/Font/Alibaba-PuHuiTi-Bold.ttf", 18.0f, nullptr, myRange.Data);

 

Such re-testing, it can be found faster than the GetGlyphRangesChineseFull, increase or decrease the text characters according to their needs on the line.

 

Guess you like

Origin www.cnblogs.com/kileyi/p/12347037.html