Skip to content

no support for BMFont's (.fnt) padding - xHeight & capHeight invalid for padded fonts #3074

@FyiurAmron

Description

@FyiurAmron

Current BitmapFont.java implementation just ignores the padding= field; it makes using fonts with e.g. shadow/glow very hard, since both xHeight & capHeight are calculated based on actual texture size, not the size minus padding one. Since for shadowed/glowed fonts the texture size is often 2-3x bigger than the proper xHeight/capHeight calculated from of the "core" of the glyph, this results in very poor vertical positioning of the glyphs during text compositing (i.e. it's almost impossible to calculate where the actual glyph will appear due to offseting from padding). Horizontal positioning is unaffected, since it's directly based on raw .fnt data.

The solution I used to work around this was replacing https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/BitmapFont.java#L472 line with

// private static final int paddingCount = 4;
                String line = reader.readLine();
                String keyword = "padding=";
                String substr = line.substring( line.indexOf( keyword ) + keyword.length() );
                String[] paddingStr = substr.substring( 0, substr.indexOf( ' ' ) ).split( ",", paddingCount );
                if ( paddingStr.length != paddingCount ) {
                    throw new IOException();
                }
                int[] padding = new int[paddingCount];
                for( int i = 0; i < paddingCount; i++ ) {
                    padding[i] = Integer.parseInt( paddingStr[i] );
                }
                // up, right, down, left
                int paddingHorizontal = padding[1] + padding[3], // currently unused
                        paddingVertical = padding[0] + padding[2];

and then, in lines 606 through 628, all occurences of xGlyph.height/capGlyph.height/glyph.height with the respective .height - paddingVertical.

I don't know if this is the best way to do this, but it seems that it fixes the issue. Any suggestions/patches are most welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions