schellingb / ZillaLib

Sleek multiplatform C++ 2D and 3D game creation!
https://zillalib.github.io/
zlib License
109 stars 8 forks source link

how to draw CJK characters? #13

Closed manuel76413 closed 2 years ago

manuel76413 commented 2 years ago

I have a question, how to draw Chinese, Japanese, and Korea characters in ZillaLib, I test with bellow code

ZL_Font fntTTF;
fntTTF = ZL_Font("Data/simfang.zip", 18);
fntTTF.Draw(20, 140, "TRUE TYPE FONT TEXT");
fntTTF.Draw(20, 340, u8"ZillaLib库测试",ZL_Color::Red);

and put simfang.ttf in Data/simfang.zip the result in Debug x64 configuration is just I want, right English Character, right Chinese Character. However, the fourth-row code cannot compile in Webassembly Configuration.

How to draw Chinese characters in Webassembly, I need to solve this problem.

schellingb commented 2 years ago

Hi there

The code compiles for me in Webassembly and displays the characters correctly. Even without the u8 prefix it works for me.

Make sure you save the file with the correct encoding. It needs to be UTF-8 without signature.

If you are using Visual Studio, you can do this with File -> Save .cpp as -> click on next to Save -> Save with Encoding ... -> Encoding: Unicode (UTF-8 without signature) - Codepage 65001 -> OK.

If this does not work for you, can you copy the compile error you get regarding the fourth-row code in your example?

manuel76413 commented 2 years ago

@schellingb , Wow! thanks for your rapid reply, I tested it, your answer hits the point in webassembly version.

As you said, I use Visual Studio 2017 in Windows 11 Home Edition, my test file is '\ZillaLibSamples\05-2d-geometry-drawing.inl'. (1) When I changed this file Encode to UTF-8 without signature-Codepage 65001, It passed compile and run correctly in webassembly version, the Chinese character draw correctly.

However, codepage UTF-8 failed in Debug win64 configuration. the compile error about the fourth-row code: 1>.\zillalibtest\zillalibsamples\05-2d-geometry-drawing.inl(84): error C2001: 常量中有换行符 1>.\zillalibtest\zillalibsamples\05-2d-geometry-drawing.inl(84): fatal error C1075: “{”: 未找到匹配令牌

(2) When I save drawing.inl file in 简体中文(GB2312)-Codepage 936, and use u8 string literal in the fourth-row code, it can be compiled correctly in Debug win64 configuration, and drawing OK in the popup window.

But, in this case, webassembly configuration cannot pass compile, the VS C++ compiler report such error: 1>In file included from ZillaLibSampleMain.cpp(59) : 1>./05-2d-geometry-drawing.inl(84) :37: error: illegal character encoding in string literal 1> fntTTF.Draw(20, 340, u8"ZillaLib", ZL_Color::Red); 1> ^~~~~~~~ 1>1 error generated. 1>../ZillaLib/WebAssembly/ZillaLibWasm.mk(181) : recipe for target 'Debug-wasm/ZillaLibSampleMain.o' failed 1>make.exe[1]: [Debug-wasm/ZillaLibSampleMain.o] Error 1 1> 1>../ZillaLib/WebAssembly/ZillaLibWasm.mk:139: recipe for target 'all' failed 1>make.exe: [all] Error 2 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Microsoft.MakeFile.Targets(44,5): error MSB3073: 命令“"../ZillaLib/Tools/make.exe" --no-print-directory -f "../ZillaLib/WebAssembly/ZillaLibWasm.mk" "ZillaApp=ZillaLibSamples" MSVC=1”已退出,代码为 2。

If there is a way to modify the draw function and pass all Configuration in different codepage cross-platform?

manuel76413 commented 2 years ago

I solved by using encoding conversion between GB2312 and UTF-8, Now drawing CJK font in both Debug and WebAssembly configuration is right.