godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.75k stars 578 forks source link

The Chinese encoding cannot be displayed properly in UtilityFunctions::print And draw_string by godot-cpp4.3 #1571

Closed Caesar454905970 closed 2 months ago

Caesar454905970 commented 2 months ago

Tested versions

godot-cpp4.3

System information

Window10 godot-cpp4.3

Issue description

image

Steps to reproduce

void waveformRendering::_draw() { // 设置文本位置 // 获取当前视口的大小 Vector2 viewport_size = get_viewport_rect().size;

            // 设置文本位置为视口中心
            Vector2 text_position = Vector2(viewport_size.x / 2, viewport_size.y / 2);

            godot::String message = "你好,世界!"; // 使用 Godot 的 String 类型
            godot::UtilityFunctions::print(message); // 正确调用 print 方法

            // 打印文本位置
            UtilityFunctions::print("文本位置: " + String::num(text_position.x) + ", " + String::num(text_position.y));
            // 初始化默认字体和字体大小
            // 获取当前主题
            Ref<Theme> current_theme = get_theme();

            // 从主题中获取默认字体
            // 从主题中获取默认字体
            if (current_theme.is_valid()) {
                default_font = current_theme->get_font("font", get_class()); // 使用当前组件的类名
                if (default_font.is_valid()) {
                    String font_name = default_font->get_name(); // 获取字体名称
                    UtilityFunctions::print("component_font component_font: " + font_name);
                } else {
                    UtilityFunctions::print("component_font is not a valid font");
                }
            } else {
                UtilityFunctions::print("current_theme is not a valid theme");
            }

            // 绘制 "Hello World" 文字
            String hello_text =  "Hello World得吃";
            draw_string(default_font, text_position, hello_text, HORIZONTAL_ALIGNMENT_CENTER, -1, 50.0, Color(0, 1, 0)); // 50.0 为字体大小,Color(0, 1, 0) 为绿色
        }
                    }

Minimal reproduction project (MRP)

src.zip

DCTewi commented 2 months ago

This seems to be a godot-cpp issue which should be created in that repository.

Are you using Visual Studio? The msvc compiler needs UTF8-BOM encoding to compile correctly with CJK characters.

timothyqiu commented 2 months ago

String(const char *from) expects latin1. You can use U"字符串" instead.

https://github.com/godotengine/godot-cpp/blob/d47758910428242169ebe59329b449edf16036e0/src/variant/char_string.cpp#L159-L173

Caesar454905970 commented 2 months ago
            godot::String message = U"你好,世界!";  is successeful !
            thanks you so much!

image

Caesar454905970 commented 2 months ago

This seems to be a godot-cpp issue which should be created in that repository.

Are you using Visual Studio? The msvc compiler needs UTF8-BOM encoding to compile correctly with CJK characters.

c0fb70521da21b19a4776076fcf8edb

maybe wrong.

dsnopek commented 2 months ago
        godot::String message = U"你好,世界!";  is successeful !
       thanks you so much!

Glad you worked it out!

Closing this issue.