Example usage for com.badlogic.gdx.graphics.g2d.freetype FreeType FT_RENDER_MODE_NORMAL

List of usage examples for com.badlogic.gdx.graphics.g2d.freetype FreeType FT_RENDER_MODE_NORMAL

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d.freetype FreeType FT_RENDER_MODE_NORMAL.

Prototype

int FT_RENDER_MODE_NORMAL

To view the source code for com.badlogic.gdx.graphics.g2d.freetype FreeType FT_RENDER_MODE_NORMAL.

Click Source Link

Usage

From source file:darkyenus.resourcepacker.util.FreeTypePacker.java

License:Apache License

/** @return null if glyph was not found. */
CharacterData createGlyph(int codePoint, int glyphIndex, FreeTypeFontParameter parameter,
        FreeType.Stroker stroker) {//from w  w w.  ja v  a2 s .  com
    if (!face.loadGlyph(glyphIndex, parameter.loadingFlags))
        return null;

    FreeType.GlyphSlot slot = face.getGlyph();
    FreeType.Glyph mainGlyph = slot.getGlyph();
    try {
        mainGlyph.toBitmap(parameter.mono ? FreeType.FT_RENDER_MODE_MONO : FreeType.FT_RENDER_MODE_NORMAL);
    } catch (GdxRuntimeException e) {
        mainGlyph.dispose();
        Gdx.app.log("FreeTypeFontGenerator", "Couldn't render char: " + codePoint);
        return null;
    }
    FreeType.Bitmap mainBitmap = mainGlyph.getBitmap();
    Pixmap mainPixmap = mainBitmap.getPixmap(Pixmap.Format.RGBA8888, parameter.color, parameter.gamma);

    if (mainBitmap.getWidth() != 0 && mainBitmap.getRows() != 0) {
        int offsetX, offsetY;
        if (parameter.borderWidth > 0) {
            // execute stroker; this generates a glyph "extended" along the outline
            int top = mainGlyph.getTop(), left = mainGlyph.getLeft();
            FreeType.Glyph borderGlyph = slot.getGlyph();
            borderGlyph.strokeBorder(stroker, false);
            borderGlyph
                    .toBitmap(parameter.mono ? FreeType.FT_RENDER_MODE_MONO : FreeType.FT_RENDER_MODE_NORMAL);
            offsetX = left - borderGlyph.getLeft();
            offsetY = -(top - borderGlyph.getTop());

            // Render border (pixmap is bigger than main).
            FreeType.Bitmap borderBitmap = borderGlyph.getBitmap();
            Pixmap borderPixmap = borderBitmap.getPixmap(Pixmap.Format.RGBA8888, parameter.borderColor,
                    parameter.borderGamma);

            // Draw main glyph on top of border.
            for (int i = 0, n = parameter.renderCount; i < n; i++)
                borderPixmap.drawPixmap(mainPixmap, offsetX, offsetY);

            mainPixmap.dispose();
            mainGlyph.dispose();
            mainPixmap = borderPixmap;
            mainGlyph = borderGlyph;
        }

        if (parameter.shadowOffsetX != 0 || parameter.shadowOffsetY != 0) {
            int mainW = mainPixmap.getWidth(), mainH = mainPixmap.getHeight();
            int shadowOffsetX = Math.max(parameter.shadowOffsetX, 0),
                    shadowOffsetY = Math.max(parameter.shadowOffsetY, 0);
            int shadowW = mainW + Math.abs(parameter.shadowOffsetX),
                    shadowH = mainH + Math.abs(parameter.shadowOffsetY);
            Pixmap shadowPixmap = new Pixmap(shadowW, shadowH, mainPixmap.getFormat());

            Color shadowColor = parameter.shadowColor;
            byte r = (byte) (shadowColor.r * 255), g = (byte) (shadowColor.g * 255),
                    b = (byte) (shadowColor.b * 255);
            float a = shadowColor.a;

            ByteBuffer mainPixels = mainPixmap.getPixels();
            ByteBuffer shadowPixels = shadowPixmap.getPixels();
            for (int y = 0; y < mainH; y++) {
                int shadowRow = shadowW * (y + shadowOffsetY) + shadowOffsetX;
                for (int x = 0; x < mainW; x++) {
                    int mainPixel = (mainW * y + x) * 4;
                    byte mainA = mainPixels.get(mainPixel + 3);
                    if (mainA == 0)
                        continue;
                    int shadowPixel = (shadowRow + x) * 4;
                    shadowPixels.put(shadowPixel, r);
                    shadowPixels.put(shadowPixel + 1, g);
                    shadowPixels.put(shadowPixel + 2, b);
                    shadowPixels.put(shadowPixel + 3, (byte) ((mainA & 0xff) * a));
                }
            }

            // Draw main glyph (with any border) on top of shadow.
            for (int i = 0, n = parameter.renderCount; i < n; i++)
                shadowPixmap.drawPixmap(mainPixmap, Math.max(-parameter.shadowOffsetX, 0),
                        Math.max(-parameter.shadowOffsetY, 0));
            mainPixmap.dispose();
            mainPixmap = shadowPixmap;
        } else if (parameter.borderWidth == 0) {
            // No shadow and no border, draw glyph additional times.
            for (int i = 0, n = parameter.renderCount - 1; i < n; i++)
                mainPixmap.drawPixmap(mainPixmap, 0, 0);
        }
    }

    final CharacterData data = new CharacterData(glyphIndex, codePoint);
    data.pixmap = mainPixmap;
    data.advanceX = FreeType.toInt(slot.getAdvanceX());
    data.offsetX = mainGlyph.getLeft();
    data.offsetY = mainGlyph.getTop();

    mainGlyph.dispose();

    return data;
}