Search in sources :

Example 6 with FreeTypeFontGenerator

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

the class DesktopLauncher method main.

public static void main(String[] arg) {
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.title = "FontWriter";
    config.width = 666;
    config.height = 444;
    final ArrayList<String[]> files = new ArrayList<>();
    for (String a : arg) files.add(a.split("---"));
    new LwjglApplication(new ApplicationAdapter() {

        public void create() {
            for (String[] commands : files) {
                boolean properlyCreated = false;
                int resX = 256;
                int resY = 256;
                while (!properlyCreated) {
                    // Supply the full path
                    String inputFontPath = commands[0];
                    FileHandle outputFolder = Gdx.files.absolute(commands[1]);
                    String fileName = outputFolder.nameWithoutExtension();
                    outputFolder = outputFolder.parent();
                    int fontSize = Integer.valueOf(commands[2]);
                    int shadowOffsetX = Integer.valueOf(commands[3]);
                    int shadowOffsetY = Integer.valueOf(commands[4]);
                    Color shadowColor = new Color(Float.valueOf(commands[5]), Float.valueOf(commands[6]), Float.valueOf(commands[7]), Float.valueOf(commands[8]));
                    int outlineThickness = Integer.valueOf(commands[9]);
                    Color outlineColor = new Color(Float.valueOf(commands[10]), Float.valueOf(commands[11]), Float.valueOf(commands[12]), Float.valueOf(commands[13]));
                    boolean outlineRounded = Boolean.valueOf(commands[14]);
                    BitmapFontWriter.FontInfo fontInfo = new BitmapFontWriter.FontInfo();
                    fontInfo.padding = new BitmapFontWriter.Padding(1, 1, 1, 1);
                    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.absolute(inputFontPath));
                    FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
                    parameter.color = new Color(Float.valueOf(commands[15]), Float.valueOf(commands[16]), Float.valueOf(commands[17]), Float.valueOf(commands[18]));
                    parameter.borderColor = outlineColor;
                    parameter.borderWidth = outlineThickness;
                    parameter.borderStraight = !outlineRounded;
                    parameter.shadowOffsetX = shadowOffsetX;
                    parameter.shadowOffsetY = shadowOffsetY;
                    parameter.shadowColor = shadowColor;
                    parameter.size = fontSize;
                    parameter.packer = new PixmapPacker(resX, resY, Pixmap.Format.RGBA8888, 2, false, new PixmapPacker.SkylineStrategy());
                    FreeTypeFontGenerator.FreeTypeBitmapFontData data = generator.generateData(parameter);
                    // Writes the .fnt file, I guess
                    BitmapFontWriter.writeFont(data, new String[] { fileName + ".png" }, outputFolder.child(fileName + ".fnt"), fontInfo, resX, resY);
                    BitmapFontWriter.writePixmaps(parameter.packer.getPages(), outputFolder, fileName);
                    generator.dispose();
                    FileHandle path = outputFolder.child(fileName + ".png");
                    if (path.exists())
                        properlyCreated = true;
                    else {
                        // BitmapFontWriter generated multiple bitmaps; can't use them.
                        // We re-run with higher source texture res so they should fit
                        resX *= 2;
                        // on a single texture, continuously as necessary.
                        resY *= 2;
                        for (FileHandle f : outputFolder.list()) {
                            if (f.nameWithoutExtension().contains(fileName + "_") && f.extension().equals("png"))
                                f.delete();
                        }
                    }
                }
            }
            Gdx.app.exit();
        }
    }, config);
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) Color(com.badlogic.gdx.graphics.Color) ArrayList(java.util.ArrayList) LwjglApplicationConfiguration(com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration) BitmapFontWriter(com.badlogic.gdx.tools.bmfont.BitmapFontWriter) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) LwjglApplication(com.badlogic.gdx.backends.lwjgl.LwjglApplication) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) ApplicationAdapter(com.badlogic.gdx.ApplicationAdapter)

Example 7 with FreeTypeFontGenerator

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

the class FreeTypeMetricsTest method create.

@Override
public void create() {
    spriteBatch = new SpriteBatch();
    atlas = new TextureAtlas("data/pack");
    smallFont = new BitmapFont();
    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.size = 60;
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/arial.ttf"));
    font = generator.generateFont(parameter);
    generator.dispose();
    renderer = new ShapeRenderer();
    renderer.setProjectionMatrix(spriteBatch.getProjectionMatrix());
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer)

Example 8 with FreeTypeFontGenerator

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

the class InternationalFontsTest method create.

@Override
public void create() {
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("data/unbom.ttf"));
    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.size = 18;
    parameter.characters = "한국어/조선�?";
    koreanFont = generator.generateFont(parameter);
    generator.dispose();
    parameter.characters = FreeTypeFontGenerator.DEFAULT_CHARS;
    generator = new FreeTypeFontGenerator(Gdx.files.internal("data/russkij.ttf"));
    cyrillicFont = generator.generateFont(parameter);
    generator.dispose();
    parameter.characters = "วรณยุ�?ต์";
    generator = new FreeTypeFontGenerator(Gdx.files.internal("data/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();
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 9 with FreeTypeFontGenerator

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

the class FreeTypeAtlasTest method createFonts.

protected int createFonts() {
    // This test uses a less efficient but more convenient way to pack multiple generated fonts into a single
    // texture atlas.
    //
    // 1. Create a new PixmapPacker big enough to fit all your desired glyphs
    // 2. Create a new FreeTypeFontGenerator for each TTF file (i.e. font styles/families)
    // 3. For each size and style, call generator.generateFont() with the packer set on the parameter
    // 4. Generate the texture atlas using packer.generateTextureAtlas or packer.updateTextureAtlas.
    // 5. Dispose of the atlas upon application exit or when you are done using the fonts
    // //////////////////////////////////////////////////////////////////////////////////////////////////////
    // create the pixmap packer
    packer = new PixmapPacker(FONT_ATLAS_WIDTH, FONT_ATLAS_HEIGHT, Format.RGBA8888, 2, false);
    fontMap = new FontMap<BitmapFont>();
    int fontCount = 0;
    // 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;
            BitmapFont bmFont = gen.generateFont(fontParameter);
            fontMap.get(style).put(size, bmFont);
            fontCount++;
        }
        // dispose of the generator once we're finished with this family
        gen.dispose();
    }
    // for the demo, show how many glyphs we loaded
    return fontCount * CHARACTERS.length();
}
Also used : FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Aggregations

FreeTypeFontGenerator (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator)9 FreeTypeFontParameter (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter)6 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 FileHandle (com.badlogic.gdx.files.FileHandle)3 PixmapPacker (com.badlogic.gdx.graphics.g2d.PixmapPacker)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 FreeTypeBitmapFontData (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData)2 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)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 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1 GlyphLayout (com.badlogic.gdx.graphics.g2d.GlyphLayout)1 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)1 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)1 BitmapFontWriter (com.badlogic.gdx.tools.bmfont.BitmapFontWriter)1 Array (com.badlogic.gdx.utils.Array)1 ArrayList (java.util.ArrayList)1