LibGDX绘制中文字符

10
我喜欢在我的应用程序中打印中文文本。
当我尝试这样做时,屏幕会变空白。控制台没有错误提示。
创建方法:
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和这三个字符之间没有任何联系。

创建方法:

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)?例如:你好吗?- Ni hao ma? - 你好吗?
问候。
编辑: 这里有一个完整的例子,将在屏幕上显示期望的中文字符。 我已从这里下载了字体: 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() {
}
    }
3个回答

4
这是我的回答:
package com.wang.libgdx.test.my;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20; 
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.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.wang.libgdx.test.utils.GdxTest;

public class TtfFontTest extends GdxTest{

SpriteBatch spriteBatch;
BitmapFont font;

@Override
public void create() {
    spriteBatch  = new SpriteBatch();

    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.characters=FreeTypeFontGenerator.DEFAULT_CHARS + "你好";
    parameter.size = 24;
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/ui/fangzheng.ttf"));
    font = generator.generateFont(parameter);
    generator.dispose();

}

@Override
public void dispose() {
    spriteBatch.dispose();
    font.dispose();
}

@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    spriteBatch.begin();
    font.draw(spriteBatch, "helloworld", 300,300);
    font.draw(spriteBatch, "你好,世界!", 300,330);
    spriteBatch.end();
}

}

这是程序的结果。 点此查看图片


2

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

当您输入Java代码时,“好”符号将转换为值U+597D。您正在使用的字体ttf文件是一种旧式的非Unicode字体。这些较旧的字体文件的工作方式是将“A”之类的字母替换为不同的图片。“A”是U+61。因此,在您的程序中,字母“A”被转换为映射到中文字符符号的U+61,但具有U+597D值的“好”却不行。

您可以继续使用自己的字体,并查找每个字符的位置,以便使用正确的编号。不要使用“a”,而应该使用类似于(char)0x61的东西,这样会稍微清晰一些。或者......

只需获取包含“好”符号在U+597D处的有效Unicode字体即可。


您的解釋和建議,我現在已經解決了問題。我還添加了一個完整的示例,希望這對未來的某個人有所幫助。 - Michael S
DroidSansFallback字体包含中文字符。您可以从https://github.com/android/platform_frameworks_base/tree/master/data/fonts获取它(或者您应该在Android SDK文件夹中有一份副本)。如果您使用此字体,则很有可能符号看起来非常接近手机上其他Android应用程序的外观(如果这是目标)。 - Chase
啊,谢谢你提供的链接,我会去看一下的。我使用的ttf文件大小超过了12MB,但这些文件要小得多。 - Michael S

1

libGDX中的freetype加载器实际上创建了一个BitmapFont。为此,它需要一个字符列表。默认列表仅包含字母数字字符。要呈现Unicode,您需要使用所需的Unicode字符替换/附加该列表。

以下代码应该有所帮助(我正在使用AssetManager):

FreeTypeFontLoaderParameter parameter = new FreeTypeFontLoaderParameter();
parameter.fontParameters.size = size;
parameter.fontFileName = "fonts/DFLS1B.ttf";
parameter.fontParameters.color = Color.WHITE;
parameter.fontParameters.characters = characters; // <-- here you give it the unicode list
parameter.fontParameters.magFilter = Texture.TextureFilter.Linear;
manager.load(sz, BitmapFont.class, parameter);
manager.finishLoadingAsset(sz);

注意:如果您使用资产管理器,则需要像他们的示例一样设置加载程序:
FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class,
    new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".ttf", 
    new FreetypeFontLoader(resolver));

我还发现了这个答案

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接