How to render Hindi text in libgdx using mangal.ttf or any other font. Square boxes are coming when try to print hindi text.
Asked
Active
Viewed 636 times
2 Answers
0
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 :

Abhishek Aryan
- 19,936
- 8
- 46
- 65
-
While running your code .. got this Exception in thread "LWJGL Application" java.lang.RuntimeException: Key with name '?' is already in map at com.badlogic.gdx.graphics.g2d.PixmapPacker.pack(PixmapPacker.java:162) at com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.generateData(FreeTypeFontGenerator.java:404) at com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.generateFont(FreeTypeFontGenerator.java:153) at com.vector.science11.MyGdxGame.create(MyGdxGame.java:37) – arv Apr 12 '17 at 07:13
-
Thanks for the answer but your input and output character are not exactly same . Like प्रभात word in output is different. – arv Apr 12 '17 at 07:16
-
even स्वागत is not similar as in my text – Abhishek Aryan Apr 12 '17 at 07:17
-
What about the error? And how we get the same hind text. – arv Apr 12 '17 at 07:23
-
i think half of hindi character is not included in .ttf font like ( `प् + र = प्र` or `स् + व = स्व`) – Abhishek Aryan Apr 12 '17 at 09:02
-
it is included in .ttf. because when i am using in android project there is no such issue. So, i think problem is in libgdx or, lack of knowledge to implement hindi text in libgdx. – arv Apr 12 '17 at 10:09
-
may be due to lack of knowledge or if you think it's problem is in libGDX then you may create an issue on libGDX repo. – Abhishek Aryan Apr 12 '17 at 10:13
-
@ Abhishek Aryan Chanakya font solution is working fine. – arv Apr 13 '17 at 10:28
-
what i told you in previous comment problem is in .ttf font(`half of hindi character is not included in Mangal.ttf font`) – Abhishek Aryan Apr 13 '17 at 10:32
-
No its included in mangal.ttf. If you use mangal.ttf with android project everything is fine. In libgdx problem is different.If you use Chanakya.ttf directly without following the above step ...then the problem is not resolved. – arv Apr 13 '17 at 11:34
-
Any one has the solution for this issue. I can not use the Chanakya.ttf in my project as it doesn't render % characters properly and I need this character very often. – VP4Android Dec 20 '18 at 21:43
0
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 :)

arv
- 250
- 3
- 15