How to render Hindi Text in Libgdx?

656 Views Asked by At

How to render Hindi text in libgdx using mangal.ttf or any other font. Square boxes are coming when try to print hindi text.

2

There are 2 best solutions below

0
arv On BEST ANSWER

We can use "Chanakya" font for render the Hindi Text in correct way. Download the chanyaka.ttf from hera :

http://indiatyping.com/index.php/download/199-chanakya-font

After installing the font . You should encode your Hindi text using the coverter :

http://indiatyping.com/index.php/font-converter/unicode-to-chanakya-font-converter

Once converted the text, simply copy it to your destination and you should be good to go!

Enjoy :)

11
AAryan On

Try FreeTypeFont, FreetypeFont is available in maven repository so you can easily inject dependency through gradle in your project and use in your code.

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

Output looks like this :

enter image description here