# LibGDX Letter spacing is reduced if I use GlyphLayout to draw a bitmap font

I need to use glyphlayout so that I can centre the text, but I found if I do the text gets distorted due to the spacing being incorrect.

public SimpleButton(String text, float x, float y, float w, float h, BitmapFont font) {
pos = new Vector2(x, y);
this.w = w;
this.h = h;
this.text = text;
this.font = font;
glyphLayout.setText(font, text);
hitbox = new Rectangle(x, y - h, w, h);
}

public void render(SpriteBatch batch) {
font.draw(batch, text, pos.x, pos.y);
}


Like this it displays fine:

But using glyphlayout to draw it the spacing for some reason is different:

public void render(SpriteBatch batch) {
font.draw(batch, glyphLayout, pos.x, pos.y);
}


Glyphlayout seems to be changing the spacing here, but I couldn't see any options for setting the spacing with it.

These are the settings for the Bitmap font in Hiero. No special fx, padding and spacing set to zero:

• What does the .fnt file for the font look like? Is there kerning information in it? Can you share the font (both .fnt and the bitmap)? – bornander Jun 10 '20 at 14:21
• @bornander Hi, I didn't think anyone would reply. I don't see how I can share the fnt file on stackoverflow? I doubt there is a problem though since if there were there would be a problem with or without glyphlayout. It's something glyphlayout is doing or not doing. – Hasen Jun 10 '20 at 16:48
• @bornander LibGDX seems to ignore the fnt file regarding spacing anyway since if I change spacing=-2,-2 to anything even like 2000 or something, it doesn't affect the spacing at all - with or without glyphlayout. It seems to be a bug with glyphlayout. I saw someone posted about this problem a few days ago at the badlogic forums but no one replied and the forum seems to be dead anyway (registration disabled, emails to the moderator fail to send). – Hasen Jun 10 '20 at 16:52
• Rendering the default.fnt font from the libgdx github results in identical text when using GlyphLayout and without, so the problem is either in the font you're using or in the way you're using it (it might still be a libgdx bug that only manifests in certain scenarios). Without seeing more of your code or the font files it is difficult to provide any help. The bitmap of the font is just an image so you can share that on stack exchange, and the .fnt file is just text so there should be a problem in sharing that either. – bornander Jun 10 '20 at 16:59
• @bornander i.imgur.com/NQ3i7ms.png I can only share the beginning of the fnt file in the next comment, I don't think you realise how long it is unless you only meant the beginning anyway. It's true the default font has no problem, maybe glyphlayout only works with the default font... – Hasen Jun 10 '20 at 17:10

I managed to solve the problem myself but it still seems like it may be a bug. It happens because glyphLayout.setText(font, text); is declared in the constructor method but it needs to be in the render method, or otherwise continually updated. I've no idea why this is, it would seem that declaring it once in the constructor method should work fine...but for some reason it doesn't.

public SimpleButton(String text, float x, float y, float w, float h, BitmapFont font) {
pos = new Vector2(x, y);
this.w = w;
this.h = h;
this.text = text;
this.font = font;
hitbox = new Rectangle(x, y - h, w, h);
}

public void render(SpriteBatch batch) {
glyphLayout.setText(font, text);
font.draw(batch, glyphLayout, pos.x, pos.y);
}


The text with glyphlayout displays with correct spacing using this code.