2
\$\begingroup\$

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();
    }
}
\$\endgroup\$
  • \$\begingroup\$ What did you try? \$\endgroup\$ – Vaillancourt Apr 12 '17 at 13:40
  • \$\begingroup\$ Just like any other font... There are several questions addressing the topic of rendering a font from a ttf file. \$\endgroup\$ – Charanor Apr 12 '17 at 17:52
  • \$\begingroup\$ @AlexandreVaillancourt i edited my post that i tried. \$\endgroup\$ – arv Apr 13 '17 at 5:22
  • \$\begingroup\$ @Charanor then please give me some link or code example. \$\endgroup\$ – arv Apr 13 '17 at 5:23
  • 1
    \$\begingroup\$ @Charanor, if you think this has been asked before, suggest a duplicate. Simply stating the fact without any reference is quite unhelpful. \$\endgroup\$ – Gnemlock Apr 13 '17 at 5:38
1
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.