How do I render Hindi text in Libgdx using any font? For example, I want to render the word "प्रकार".
Here is what I have, so far:
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
public class MyGdxGame extends Game
{
SpriteBatch spriteBatch;
BitmapFont font;
OrthographicCamera camera;
String text, text1;
private byte[] myBytes = { (byte) 0xe0, (byte) 0xa4, (byte) 0xa8,
(byte) 0xe0, (byte) 0xa4, (byte) 0xbf, (byte) 0xe0, (byte) 0xa4,
(byte) 0xb6, (byte) 0xe0, (byte) 0xa5, (byte) 0x89, (byte) 0xe0,
(byte) 0xa4, (byte) 0xa4 };
private String myName = new String(myBytes, Charset.forName("UTF-8"));
private String hindiStr = "ँंःअआइईउऊऋऎएऐऍऒओऔऑकखगघङचछजझञटठडढणतथदधनऩप"
+ "फबभमयय़रऱलळऴवशषसहािीुूृॆेैॅॊोौॉ़्।०१२३४५६७८९INV";
@Override
public void create()
{
camera = new OrthographicCamera();
camera.setToOrtho(false, 400, 640);
spriteBatch = new SpriteBatch();
FreeTypeFontGenerator generator
= new FreeTypeFontGenerator(Gdx.files.internal("font/mangal.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter
= new FreeTypeFontGenerator.FreeTypeFontParameter();
// +FreeTypeFontGenerator.DEFAULT_CHARS; The characters the font should contain
parameter.characters = hindiStr;
parameter.size = 15;
font = generator.generateFont(parameter);
font.setColor(1.0f, 0.0f, 0.0f, 1.0f);
generator.dispose();
text = "शुभ प्रभात";
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, "सूक्ष्मजीवों के विभिन्न प्रकार ", 10, 30);
font.draw(spriteBatch, myName, 10, 100);
spriteBatch.end();
}
@Override
public void resize(int width, int height)
{
camera.setToOrtho(false, width, height);
}
@Override
public void dispose()
{
font.dispose();
spriteBatch.dispose();
}
}