2

I'm making an app to help me remember some Thai words I'm learning.

However I can't get text to render correctly.

I used this example to create a basic scene. This is what I have so far.

public class ThaiWords extends ApplicationAdapter {

    OrthographicCamera cam;
    SpriteBatch batch;
    BitmapFont thaiFont;

    @Override
    public void create () {

        FreeTypeFontGenerator generator;

        FreeTypeFontParameter parameter = new FreeTypeFontParameter();
        parameter.size = 18;
        parameter.characters = "ก";

        generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/garuda.ttf"));
        thaiFont = generator.generateFont(parameter);
        generator.dispose();

        batch = new SpriteBatch();

        cam = new OrthographicCamera();
        cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.update();
    }

    @Override
    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(cam.combined);
        batch.begin();
        thaiFont.draw(batch, "ก", 0, 66);
        batch.end();
    }

    @Override
    public void resize (int width, int height) {
        cam.setToOrtho(false, width, height);
    }
}

For testing, I'd just like to display the "ก" character. The code above produces a "?" symbol.

I directly downloaded the garuda.tff font from the libGDX directory.

I'm really not sure what I'm missing! I even tried to generate a bitmap before run time but that produced a blank screen.

Any advice would be great!

edit: I've noticed on Android Studio, when I close and re-open the project the actual code changes from "ก" to "?". It may be an encoding issue but I have no idea how to fix it.

KlemenPl
  • 354
  • 4
  • 21
Johnny
  • 35
  • 4
  • Are you sure Garuda has Thai characters? – Tenfour04 Apr 05 '16 at 00:24
  • I'm certain it does. It's specifically designed to have easy to read Thai characters. I've also added it to my fonts in Windows and typed out the characters. More information can be found here http://slice-of-thai.com/fonts/#garuda – Johnny Apr 05 '16 at 07:18

2 Answers2

2

I would suggest you to use Stage and Label to render text.
in create:

Stage stage = new Stage();
Label label = new Label("วรณ", new Label.LabelStyle(thaiFont, Color.YELLOW));
label.setPosition(200,10);
stage.addActor(label);

and then in render method:

stage.act(delta);
stage.draw();

I tested it and it worked just fine.

enter image description here

Enigo
  • 3,685
  • 5
  • 29
  • 54
  • Thank you for your reply. Hm I'm still getting "?" on my system. Are you using Android Studio? Could you share your encoding settings is Settings -> Editor -> File Encodings please? – Johnny Apr 05 '16 at 09:21
  • Updated answer. I use IntelliJ IDEA Community Edition 15.0.3. My OS is Windows 10 and system language is Russian. – Enigo Apr 05 '16 at 09:24
  • Thank you for your help. I changed the encoding from windows-1251 to UTF-8 for my main class as well as the .ttf file and now both methods of rendering work. – Johnny Apr 05 '16 at 09:29
2

I had the same problem. This is not something wrong with your code nor libGdx font generator. Font generator and libgdx Freetype extension works fine with UTF-8 character set including special characters. You just need to save your property files in UTF-8 encoding.

you can use any other text editor to save property file in UTF-8 encoding or If you are using Android Studio go to File->Setting->Editor->File Encoding. Then change "Default encoding for properties files" into UTF-8.

Then you need to retype all the special characters on every property file. because previous encoding messed-up the unknown characters.

Than all should works fine!

enter image description here

mili
  • 3,502
  • 1
  • 29
  • 29