libgdx 绘制汉字

2022-01-12 00:00:00 fonts android java libgdx chinese-locale

我喜欢在我的应用程序中打印中文文本.

I like to print Chinese text in my application.

1.当我尝试这个时,屏幕将是空的.控制台没有错误.

1.When I try this, the screen will be empty. There is no error at the console.

创建方法:

FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40, "好", false);

渲染方法:

spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "好", 10, 100);
spriteBatch.end();

2.当我尝试这个时,屏幕上出现了 3 个不同的汉字,但我不知道为什么这些汉字在哪里画.asd和这三个字符没有联系

2.When I try this, 3 different Chinese characters show up on screen but I have no idea why these characters where draw. There is no connection between asd and the three characters

创建方法:

FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40);

渲染方法:

spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "asd", 10, 100);
spriteBatch.end();

有谁知道如何在libgdx 中正确绘制汉字(我使用当前版本的libgdx)?例如:你好吗?- 你好吗?- 你好吗?

Does anyone know how to draw Chinese character in libgdx correct (I use the current version of libgdx)? For example: How are you? - Ni hao ma? - 你 好 吗?

问候

这是一个完整的示例,它将在屏幕上显示预期的中文字符.我从这里下载了字体:http://www.study-area.org/apt/firefly-font/

Here is a full example which will show expected Chinese characters on the screen. I have downloaded the font from here: http://www.study-area.org/apt/firefly-font/

package com.mytest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class ChineseFontTest implements ApplicationListener {

private Stage stage;
private SpriteBatch spriteBatch;
public BitmapFont font;

@Override
public void create() {
    stage = new Stage(800, 800);
    spriteBatch = new SpriteBatch();

    FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/fireflysung.ttf"));
    font = gen.generateFont(40, "好你吗", false);

}

@Override
public void dispose() {
    stage.dispose();
}

@Override
public void render() {      
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    spriteBatch.setColor(1, 1, 1, 1);
    spriteBatch.begin();
    font.draw(spriteBatch, "你好吗", 10, 100);
    spriteBatch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
    }

推荐答案

转到 http://www.angelcode.com/products/bmfont/ 并获取 BMFont 并使用它来查看您的字体.您还可以使用它预生成 libgdx 位图字体文件.问题是您的字体不是 unicode 字体.

Go to http://www.angelcode.com/products/bmfont/ and get BMFont and use it to look at your font. You can also pregenerate libgdx bitmap font files with it. The problem is that your font is not a unicode font.

当您输入 Java 代码时,好"符号被转换为值 U+597D.您使用的字体 ttf 文件是老式的非 unicode 字体.这些旧字体文件的工作方式是用不同的图片替换像a"这样的字母.a"是 U+61.所以在你的程序中,字母a"被转换为 U+61,它映射到一个汉字符号,但值为 U+597D 的好"没有.

When you are typing Java code the "好" symbol is translated to the value U+597D. The font ttf file you are using is an old-school non-unicode font. The way these older font files work is that they replace a letter like "a" with a different picture. "a" is U+61. So in your program the letter "a" is converted to U+61 which maps to a chinese character symbol but "好" with a value of U+597D does not.

您可以继续使用您的字体并查找每个字符的位置,以便使用正确的数字.不要使用a",而是使用 (char)0x61 之类的东西,这样它就不会那么混乱了.或者……

You can either continue to use your font and look up the position of every character so that you can use the correct number. Don't use "a" instead use something like (char)0x61 so it is a little less confusing. Or...

只需在 U+597D 处获取包含好"符号的有效 unicode 字体即可.

Just get a valid unicode font that contains the "好" symbol at U+597D.

相关文章