如何在 Libgdx 中呈现印地语文本?
How to render Hindi Text in Libgdx?
如何使用 mangal.ttf 或任何其他字体在 libgdx 中呈现 印地语 文本。
尝试打印印地语文本时会出现方框。
在您的项目中尝试 FreeTypeFont, FreetypeFont is available in maven repository so you can easily inject dependency 到 gradle 并在您的代码中使用。
public class MyGdxGame extends Game {
SpriteBatch spriteBatch;
BitmapFont font;
OrthographicCamera camera;
GlyphLayout glyphLayout,glyphLayout1;
String text,text1;
@Override
public void create() {
camera=new OrthographicCamera();
camera.setToOrtho(false,400,640);
spriteBatch = new SpriteBatch();
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Mangal.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.color = Color.WHITE;
parameter.magFilter = Texture.TextureFilter.Linear; // used for resizing quality
parameter.minFilter = Texture.TextureFilter.Linear;
//ँंंःअआइईउऊऋएऐऑओऔकखगघचछजझञटठडढणतथदधनपफबभमयरलवशषसह़ािीुूृॅेैॉोौ्
parameter.characters = "शुभ प्रभात आप का स्वागत है"; //The characters the font should contain
parameter.size=15;
font=generator.generateFont(parameter);
font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
font.setColor(1.0f, 0.0f, 0.0f, 1.0f);
generator.dispose();
text="शुभ प्रभात";
text1="आप का स्वागत है";
glyphLayout=new GlyphLayout(font,text);
glyphLayout1=new GlyphLayout(font,text1);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(1,1,0,1);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
font.draw(spriteBatch,text,camera.viewportWidth*.5f-glyphLayout.width/2f,camera.viewportHeight*.5f);
font.draw(spriteBatch,text1,camera.viewportWidth*.5f-glyphLayout1.width/2f,camera.viewportHeight*.4f);
spriteBatch.end();
}
@Override
public void resize(int width, int height) {
camera.setToOrtho(false,width,height);
}
@Override
public void dispose() {
font.dispose();
spriteBatch.dispose();
}
}
输出如下所示:
我们可以使用 "Chanakya" 字体以正确的方式呈现印地语文本。从 hera 下载 chanyaka.ttf :
http://indiatyping.com/index.php/download/199-chanakya-font
安装字体后。您应该使用转换器对您的印地语文本进行编码:
http://indiatyping.com/index.php/font-converter/unicode-to-chanakya-font-converter
转换文本后,只需将其复制到目标位置即可!
尽情享受吧:)
如何使用 mangal.ttf 或任何其他字体在 libgdx 中呈现 印地语 文本。 尝试打印印地语文本时会出现方框。
在您的项目中尝试 FreeTypeFont, FreetypeFont is available in maven repository so you can easily inject dependency 到 gradle 并在您的代码中使用。
public class MyGdxGame extends Game {
SpriteBatch spriteBatch;
BitmapFont font;
OrthographicCamera camera;
GlyphLayout glyphLayout,glyphLayout1;
String text,text1;
@Override
public void create() {
camera=new OrthographicCamera();
camera.setToOrtho(false,400,640);
spriteBatch = new SpriteBatch();
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Mangal.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.color = Color.WHITE;
parameter.magFilter = Texture.TextureFilter.Linear; // used for resizing quality
parameter.minFilter = Texture.TextureFilter.Linear;
//ँंंःअआइईउऊऋएऐऑओऔकखगघचछजझञटठडढणतथदधनपफबभमयरलवशषसह़ािीुूृॅेैॉोौ्
parameter.characters = "शुभ प्रभात आप का स्वागत है"; //The characters the font should contain
parameter.size=15;
font=generator.generateFont(parameter);
font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
font.setColor(1.0f, 0.0f, 0.0f, 1.0f);
generator.dispose();
text="शुभ प्रभात";
text1="आप का स्वागत है";
glyphLayout=new GlyphLayout(font,text);
glyphLayout1=new GlyphLayout(font,text1);
}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(1,1,0,1);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
font.draw(spriteBatch,text,camera.viewportWidth*.5f-glyphLayout.width/2f,camera.viewportHeight*.5f);
font.draw(spriteBatch,text1,camera.viewportWidth*.5f-glyphLayout1.width/2f,camera.viewportHeight*.4f);
spriteBatch.end();
}
@Override
public void resize(int width, int height) {
camera.setToOrtho(false,width,height);
}
@Override
public void dispose() {
font.dispose();
spriteBatch.dispose();
}
}
输出如下所示:
我们可以使用 "Chanakya" 字体以正确的方式呈现印地语文本。从 hera 下载 chanyaka.ttf :
http://indiatyping.com/index.php/download/199-chanakya-font
安装字体后。您应该使用转换器对您的印地语文本进行编码:
http://indiatyping.com/index.php/font-converter/unicode-to-chanakya-font-converter
转换文本后,只需将其复制到目标位置即可!
尽情享受吧:)