libgdx draw chinese characters

主宰稳场 提交于 2019-12-04 02:46:22

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.

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.

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...

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

Junxiao Wang

This is my answer:

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();
}

}

this is result of the program. enter image description here

Laur Ivan

The freetype loader in libGDX actually creates a BitmapFont. To do that, it'll need a list of characters. The default list is only alphanumerics. To render unicode, you'll need to replace/append that list with the unicode characters you need.

Following code should be some help (I'm using the 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);

Note: If you use the asset manager you'll need to set the loaders like in their example:

FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class,
    new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".ttf", 
    new FreetypeFontLoader(resolver));

I've also found this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!