Search in sources :

Example 11 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)

Example 12 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 13 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project skin-composer by raeleus.

the class FreeTypeFontData method createBitmapFont.

public void createBitmapFont(Main main) {
    if (bitmapFont != null) {
        bitmapFont.dispose();
        bitmapFont = null;
    }
    if (!useCustomSerializer) {
        if (previewTTF == null)
            return;
        FreeTypeFontParameter parameter = new FreeTypeFontParameter();
        parameter.color = Color.BLACK;
        FileHandle previewFontPath = Gdx.files.local("preview fonts/" + previewTTF + ".ttf");
        if (previewFontPath.exists()) {
            FreeTypeFontGenerator generator = new FreeTypeFontGenerator(previewFontPath);
            bitmapFont = generator.generateFont(parameter);
            generator.dispose();
        }
    } else {
        if (file == null)
            return;
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(file);
        FreeTypeFontParameter parameter = new FreeTypeFontParameter();
        if (borderColor != null)
            for (ColorData colorData : main.getJsonData().getColors()) {
                if (colorData.getName().equals(borderColor)) {
                    parameter.borderColor = colorData.color;
                    break;
                }
            }
        parameter.borderGamma = borderGamma;
        parameter.borderStraight = borderStraight;
        parameter.borderWidth = borderWidth;
        parameter.characters = characters.equals("") ? FreeTypeFontGenerator.DEFAULT_CHARS : characters;
        if (color != null)
            for (ColorData colorData : main.getJsonData().getColors()) {
                if (colorData.getName().equals(color)) {
                    parameter.color = colorData.color;
                    break;
                }
            }
        parameter.flip = flip;
        parameter.gamma = gamma;
        parameter.genMipMaps = genMipMaps;
        parameter.hinting = hinting == null ? Hinting.AutoMedium : Hinting.valueOf(hinting);
        parameter.incremental = false;
        parameter.kerning = kerning;
        parameter.magFilter = magFilter == null ? TextureFilter.Nearest : TextureFilter.valueOf(magFilter);
        parameter.minFilter = minFilter == null ? TextureFilter.Nearest : TextureFilter.valueOf(minFilter);
        parameter.mono = mono;
        parameter.renderCount = renderCount;
        if (shadowColor != null)
            for (ColorData colorData : main.getJsonData().getColors()) {
                if (colorData.getName().equals(shadowColor)) {
                    parameter.shadowColor = colorData.color;
                    break;
                }
            }
        parameter.shadowOffsetX = shadowOffsetX;
        parameter.shadowOffsetY = shadowOffsetY;
        parameter.size = size;
        parameter.spaceX = spaceX;
        parameter.spaceY = spaceY;
        bitmapFont = 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)

Example 14 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project bladecoder-adventure-engine by bladecoder.

the class BladeSkin method getJsonLoader.

/**
 * Override BitmapFont.class serializer to support TTF fonts
 *
 * Also add the size parameter to support bitmaps font size in pt
 */
@Override
protected Json getJsonLoader(final FileHandle skinFile) {
    Json json = super.getJsonLoader(skinFile);
    final Skin skin = this;
    json.setSerializer(Skin.class, new ReadOnlySerializer<Skin>() {

        public Skin read(Json json, JsonValue typeToValueMap, @SuppressWarnings("rawtypes") Class ignored) {
            for (JsonValue valueMap = typeToValueMap.child; valueMap != null; valueMap = valueMap.next) {
                try {
                    Class<?> type = json.getClass(valueMap.name());
                    if (type == null)
                        type = ClassReflection.forName(valueMap.name());
                    readNamedObjects(json, type, valueMap);
                } catch (ReflectionException ex) {
                    throw new SerializationException(ex);
                }
            }
            return skin;
        }

        private void readNamedObjects(Json json, Class<?> type, JsonValue valueMap) {
            Class<?> addType = type == TintedDrawable.class ? Drawable.class : type;
            for (JsonValue valueEntry = valueMap.child; valueEntry != null; valueEntry = valueEntry.next) {
                Object object = json.readValue(type, valueEntry);
                if (object == null)
                    continue;
                try {
                    add(valueEntry.name, object, addType);
                    if (addType != Drawable.class && ClassReflection.isAssignableFrom(Drawable.class, addType))
                        add(valueEntry.name, object, Drawable.class);
                } catch (Exception ex) {
                    throw new SerializationException("Error reading " + ClassReflection.getSimpleName(type) + ": " + valueEntry.name, ex);
                }
            }
        }
    });
    json.setSerializer(BitmapFont.class, new ReadOnlySerializer<BitmapFont>() {

        public BitmapFont read(Json json, JsonValue jsonData, @SuppressWarnings("rawtypes") Class type) {
            String path = json.readValue("file", String.class, jsonData);
            int scaledSize = json.readValue("scaledSize", int.class, -1, jsonData);
            Boolean flip = json.readValue("flip", Boolean.class, false, jsonData);
            int size = json.readValue("size", int.class, -1, jsonData);
            FileHandle fontFile = skinFile.parent().child(path);
            if (!FileUtils.exists(fontFile))
                fontFile = Gdx.files.internal(path);
            if (!FileUtils.exists(fontFile))
                throw new SerializationException("Font file not found: " + fontFile);
            BitmapFont font;
            if (fontFile.extension().equalsIgnoreCase("ttf")) {
                if (size == -1)
                    throw new SerializationException("'size' mandatory parameter for .ttf fonts");
                long initTime = System.currentTimeMillis();
                FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
                FreeTypeFontParameter parameter = new FreeTypeFontParameter();
                parameter.size = (int) (DPIUtils.dpToPixels(size) * DPIUtils.getSizeMultiplier());
                parameter.incremental = json.readValue("incremental", boolean.class, true, jsonData);
                parameter.borderWidth = json.readValue("borderWidth", int.class, 0, jsonData);
                parameter.borderColor = json.readValue("borderColor", Color.class, Color.BLACK, jsonData);
                parameter.borderStraight = json.readValue("borderStraight", boolean.class, false, jsonData);
                parameter.shadowOffsetX = json.readValue("shadowOffsetX", int.class, 0, jsonData);
                parameter.shadowOffsetY = json.readValue("shadowOffsetY", int.class, 0, jsonData);
                parameter.shadowColor = json.readValue("shadowColor", Color.class, Color.BLACK, jsonData);
                if (parameter.incremental)
                    parameter.characters = "";
                // parameter.hinting = Hinting.Medium;
                // parameter.mono = false;
                font = generator.generateFont(parameter);
                EngineLogger.debug(path + " TIME (ms): " + (System.currentTimeMillis() - initTime));
            // TODO Dispose all generators.
            } else {
                // Use a region with the same name as the font, else use a
                // PNG file in the same directory as the FNT file.
                String regionName = fontFile.nameWithoutExtension();
                try {
                    TextureRegion region = skin.optional(regionName, TextureRegion.class);
                    if (region != null)
                        font = new BitmapFont(fontFile, region, flip);
                    else {
                        FileHandle imageFile = fontFile.parent().child(regionName + ".png");
                        if (FileUtils.exists(imageFile))
                            font = new BitmapFont(fontFile, imageFile, flip);
                        else
                            font = new BitmapFont(fontFile, flip);
                    }
                    // font to.
                    if (scaledSize != -1)
                        font.getData().setScale(scaledSize / font.getCapHeight());
                    else if (// TODO set size in points (dpi
                    size != -1)
                        // independent)
                        font.getData().setScale((DPIUtils.dpToPixels(size) * DPIUtils.getSizeMultiplier()) / font.getCapHeight());
                } catch (RuntimeException ex) {
                    throw new SerializationException("Error loading bitmap font: " + fontFile, ex);
                }
            }
            font.getData().markupEnabled = true;
            return font;
        }
    });
    for (Class<?> cls : TAGGED_STYLES) {
        json.addClassTag(cls.getSimpleName(), cls);
    }
    return json;
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) SerializationException(com.badlogic.gdx.utils.SerializationException) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) FileHandle(com.badlogic.gdx.files.FileHandle) JsonValue(com.badlogic.gdx.utils.JsonValue) TextureRegionDrawable(com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable) TiledDrawable(com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable) Json(com.badlogic.gdx.utils.Json) SerializationException(com.badlogic.gdx.utils.SerializationException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 15 with FreeTypeFontGenerator

use of com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator in project skin-composer by raeleus.

the class FreeTypeFontData method createBitmapFont.

public void createBitmapFont() {
    if (bitmapFont != null) {
        bitmapFont.dispose();
        bitmapFont = null;
    }
    if (!useCustomSerializer) {
        if (previewTTF == null)
            return;
        FreeTypeFontParameter parameter = new FreeTypeFontParameter();
        parameter.color = Color.BLACK;
        FileHandle previewFontPath = appFolder.child("preview fonts/" + previewTTF + ".ttf");
        if (previewFontPath.exists()) {
            FreeTypeFontGenerator generator = new FreeTypeFontGenerator(previewFontPath);
            generator.setMaxTextureSize(FreeTypeFontGenerator.NO_MAXIMUM);
            bitmapFont = generator.generateFont(parameter);
            generator.dispose();
        }
    } else {
        if (file == null || !file.exists())
            return;
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(file);
        generator.setMaxTextureSize(FreeTypeFontGenerator.NO_MAXIMUM);
        FreeTypeFontParameter parameter = new FreeTypeFontParameter();
        if (borderColor != null)
            for (ColorData colorData : jsonData.getColors()) {
                if (colorData.getName().equals(borderColor)) {
                    parameter.borderColor = colorData.color;
                    break;
                }
            }
        parameter.borderGamma = borderGamma;
        parameter.borderStraight = borderStraight;
        parameter.borderWidth = borderWidth;
        parameter.characters = characters.equals("") ? DEFAULT_CHARS : characters;
        if (color != null)
            for (ColorData colorData : jsonData.getColors()) {
                if (colorData.getName().equals(color)) {
                    parameter.color = colorData.color;
                    break;
                }
            }
        parameter.flip = flip;
        parameter.gamma = gamma;
        parameter.genMipMaps = genMipMaps;
        parameter.hinting = hinting == null ? Hinting.AutoMedium : Hinting.valueOf(hinting);
        parameter.incremental = false;
        parameter.kerning = kerning;
        parameter.magFilter = magFilter == null ? TextureFilter.Nearest : TextureFilter.valueOf(magFilter);
        parameter.minFilter = minFilter == null ? TextureFilter.Nearest : TextureFilter.valueOf(minFilter);
        parameter.mono = mono;
        parameter.renderCount = renderCount;
        if (shadowColor != null)
            for (ColorData colorData : jsonData.getColors()) {
                if (colorData.getName().equals(shadowColor)) {
                    parameter.shadowColor = colorData.color;
                    break;
                }
            }
        parameter.shadowOffsetX = shadowOffsetX;
        parameter.shadowOffsetY = shadowOffsetY;
        parameter.size = size;
        parameter.spaceX = spaceX;
        parameter.spaceY = spaceY;
        try {
            bitmapFont = generator.generateFont(parameter);
        } catch (GdxRuntimeException e) {
            bitmapFont = null;
        }
        generator.dispose();
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) FreeTypeFontParameter(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter) FileHandle(com.badlogic.gdx.files.FileHandle)

Aggregations

FreeTypeFontGenerator (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator)15 FreeTypeFontParameter (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter)10 FileHandle (com.badlogic.gdx.files.FileHandle)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 PixmapPacker (com.badlogic.gdx.graphics.g2d.PixmapPacker)4 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 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