Search in sources :

Example 1 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project libgdx by libgdx.

the class UnicodeFont method setRenderType.

public void setRenderType(RenderType renderType) {
    this.renderType = renderType;
    if (renderType != RenderType.FreeType) {
        if (bitmapFont != null) {
            bitmapFont.dispose();
            generator.dispose();
        }
    } else {
        String fontFile = getFontFile();
        if (fontFile != null) {
            generator = new FreeTypeFontGenerator(Gdx.files.absolute(fontFile));
            FreeTypeFontParameter param = new FreeTypeFontParameter();
            param.size = font.getSize();
            param.incremental = true;
            param.flip = true;
            param.mono = mono;
            param.gamma = gamma;
            bitmapFont = generator.generateFont(param);
            if (bitmapFont.getData().missingGlyph == null)
                bitmapFont.getData().missingGlyph = bitmapFont.getData().getGlyph('�');
            cache = bitmapFont.newFontCache();
            layout = new GlyphLayout();
        }
    }
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) GlyphLayout(com.badlogic.gdx.graphics.g2d.GlyphLayout)

Example 2 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project libgdx by libgdx.

the class FreeTypeDisposeTest method render.

public void render() {
    if (Gdx.input.justTouched()) {
        for (int i = 0; i < 10; i++) {
            if (font != null) {
                font.dispose();
            }
            FileHandle fontFile = Gdx.files.internal("data/arial.ttf");
            FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
            FreeTypeFontParameter parameter = new FreeTypeFontParameter();
            parameter.size = 15;
            font = generator.generateFont(parameter);
            generator.dispose();
        }
        for (int i = 0; i < 10; i++) System.gc();
        Gdx.app.log("FreeTypeDisposeTest", "generated 10 fonts");
        Gdx.app.log("FreeTypeDisposeTest", Gdx.app.getJavaHeap() + ", " + Gdx.app.getNativeHeap());
    }
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 3 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project libgdx by libgdx.

the class FreeTypeIncrementalTest method create.

public void create() {
    batch = new SpriteBatch();
    shapes = new ShapeRenderer();
    shapes.setColor(Color.RED);
    FreeTypeFontGenerator.setMaxTextureSize(128);
    generator = new FreeTypeFontGenerator(Gdx.files.internal("data/arial.ttf"));
    FreeTypeFontParameter param = new FreeTypeFontParameter();
    param.incremental = true;
    param.size = 24;
    param.characters = "howdY";
    FreeTypeBitmapFontData data = new FreeTypeBitmapFontData() {

        public int getWrapIndex(Array<Glyph> glyphs, int start) {
            return SimplifiedChinese.getWrapIndex(glyphs, start);
        }
    };
    // By default latin chars are used for x and cap height, causing some fonts to display non-latin chars out of bounds.
    data.xChars = new char[] { '动' };
    data.capChars = new char[] { '动' };
    font = generator.generateFont(param, data);
}
Also used : Array(com.badlogic.gdx.utils.Array) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) FreeTypeBitmapFontData(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer)

Example 4 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project libgdx by libgdx.

the class FreeTypePackTest method createFonts.

protected int createFonts() {
    // //////////////////////////////////////////////////////////////////////////////////////////////////////
    // //////Steps to use multiple FreeTypeFontGenerators with a single texture atlas://////////////////////
    // 1. Create a new PixmapPacker big enough to fit all your desired glyphs
    // 2. Create a new FreeTypeFontGenerator for each file (i.e. font styles/families)
    // 3. Pack the data by specifying the PixmapPacker parameter to generateData
    // Keep hold of the returned BitmapFontData for later
    // 4. Repeat for other sizes.
    // 5. Dispose the generator and repeat for other font styles/families
    // 6. Get the TextureRegion(s) from the packer using packer.updateTextureRegions()
    // 7. Dispose the PixmapPacker
    // 8. Use each BitmapFontData to construct a new BitmapFont, and specify your TextureRegion(s) to the font constructor
    // 9. Dispose of the Texture upon application exit or when you are done using the font atlas
    // //////////////////////////////////////////////////////////////////////////////////////////////////////
    // create the pixmap packer
    PixmapPacker packer = new PixmapPacker(FONT_ATLAS_WIDTH, FONT_ATLAS_HEIGHT, Format.RGBA8888, 2, false);
    // we need to load all the BitmapFontDatas before we can start loading BitmapFonts
    FontMap<BitmapFontData> dataMap = new FontMap<BitmapFontData>();
    // for each style...
    for (FontStyle style : FontStyle.values()) {
        // get the file for this style
        FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal(style.path));
        // For each size...
        for (FontSize size : FontSize.values()) {
            // pack the glyphs into the atlas using the default chars
            FreeTypeFontGenerator.FreeTypeFontParameter fontParameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
            fontParameter.size = size.size;
            fontParameter.packer = packer;
            fontParameter.characters = CHARACTERS;
            BitmapFontData data = gen.generateData(fontParameter);
            // store the info for later, when we generate the texture
            dataMap.get(style).put(size, data);
        }
        // dispose of the generator once we're finished with this family
        gen.dispose();
    }
    // Get regions from our packer
    regions = new Array<TextureRegion>();
    packer.updateTextureRegions(regions, TextureFilter.Nearest, TextureFilter.Nearest, false);
    // No more need for our CPU-based pixmap packer, as our textures are now on GPU
    packer.dispose();
    // Now we can create our fonts...
    fontMap = new FontMap<BitmapFont>();
    int fontCount = 0;
    // for each style...
    for (FontStyle style : FontStyle.values()) {
        // For each size...
        for (FontSize size : FontSize.values()) {
            // get the data for this style/size pair
            BitmapFontData data = dataMap.get(style).get(size);
            // create a BitmapFont from the data and shared texture
            BitmapFont bmFont = new BitmapFont(data, regions, INTEGER);
            // place the font into our map of loaded fonts
            fontMap.get(style).put(size, bmFont);
            fontCount++;
        }
    }
    // for the demo, show how many glyphs we loaded
    return fontCount * CHARACTERS.length();
}
Also used : BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 5 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project libgdx by libgdx.

the class FreeTypeTest method create.

@Override
public void create() {
    boolean flip = false;
    batch = new SpriteBatch();
    if (flip) {
        OrthographicCamera cam = new OrthographicCamera();
        cam.setToOrtho(flip);
        cam.update();
        batch.setProjectionMatrix(cam.combined);
    }
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), flip);
    FileHandle fontFile = Gdx.files.internal("data/arial.ttf");
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.size = 15;
    parameter.flip = flip;
    parameter.genMipMaps = true;
    // parameter.shadowOffsetX = 1;
    // parameter.shadowOffsetY = 1;
    // parameter.shadowColor = Color.GREEN;
    // parameter.borderWidth = 1f;
    // parameter.borderColor = Color.PURPLE;
    FreeTypeBitmapFontData fontData = generator.generateData(parameter);
    ftFont = generator.generateFont(parameter);
    generator.dispose();
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) FileHandle(com.badlogic.gdx.files.FileHandle) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) FreeTypeBitmapFontData(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Aggregations

FreeTypeFontGenerator (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator)13 FreeTypeFontParameter (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 FileHandle (com.badlogic.gdx.files.FileHandle)6 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 PixmapPacker (com.badlogic.gdx.graphics.g2d.PixmapPacker)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)2 FreeTypeBitmapFontData (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData)2 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)2 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)2 ApplicationAdapter (com.badlogic.gdx.ApplicationAdapter)1 LwjglApplication (com.badlogic.gdx.backends.lwjgl.LwjglApplication)1 LwjglApplicationConfiguration (com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration)1 Color (com.badlogic.gdx.graphics.Color)1 Texture (com.badlogic.gdx.graphics.Texture)1 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1 GlyphLayout (com.badlogic.gdx.graphics.g2d.GlyphLayout)1 CheckBox (com.badlogic.gdx.scenes.scene2d.ui.CheckBox)1