Search in sources :

Example 1 with PixmapPacker

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

the class PixmapPackerTest method create.

@Override
public void create() {
    batch = new SpriteBatch();
    shapeRenderer = new ShapeRenderer();
    camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
    camera.update();
    Pixmap pixmap1 = new Pixmap(Gdx.files.internal("data/badlogic.jpg"));
    Pixmap pixmap2 = new Pixmap(Gdx.files.internal("data/particle-fire.png"));
    Pixmap pixmap3 = new Pixmap(Gdx.files.internal("data/isotile.png"));
    PixmapPacker packer = new PixmapPacker(1024, 1024, Format.RGBA8888, 8, false);
    for (int count = 1; count <= 3; ++count) {
        packer.pack("badlogic " + count, pixmap1);
        packer.pack("fire " + count, pixmap2);
        packer.pack("isotile " + count, pixmap3);
    }
    atlas = packer.generateTextureAtlas(TextureFilter.Nearest, TextureFilter.Nearest, false);
    Gdx.app.log("PixmapPackerTest", "Number of initial textures: " + atlas.getTextures().size);
    packer.setPackToTexture(true);
    for (int count = 4; count <= 10; ++count) {
        packer.pack("badlogic " + count, pixmap1);
        packer.pack("fire " + count, pixmap2);
        packer.pack("isotile " + count, pixmap3);
    }
    pixmap1.dispose();
    pixmap2.dispose();
    pixmap3.dispose();
    packer.updateTextureAtlas(atlas, TextureFilter.Nearest, TextureFilter.Nearest, false);
    textureRegions = new Array<TextureRegion>();
    packer.updateTextureRegions(textureRegions, TextureFilter.Nearest, TextureFilter.Nearest, false);
    Gdx.app.log("PixmapPackerTest", "Number of updated textures: " + atlas.getTextures().size);
    Gdx.input.setInputProcessor(new InputAdapter() {

        @Override
        public boolean keyDown(int keycode) {
            if (keycode >= Input.Keys.NUM_0 && keycode <= Input.Keys.NUM_9) {
                int number = keycode - Input.Keys.NUM_0;
                if (number < textureRegions.size) {
                    pageToShow = number;
                }
            }
            return super.keyDown(keycode);
        }
    });
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) InputAdapter(com.badlogic.gdx.InputAdapter) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 2 with PixmapPacker

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

the class FreeTypeFontGenerator method generateData.

/** Generates a new {@link BitmapFontData} instance, expert usage only. Throws a GdxRuntimeException if something went wrong.
	 * @param parameter configures how the font is generated */
public FreeTypeBitmapFontData generateData(FreeTypeFontParameter parameter, FreeTypeBitmapFontData data) {
    parameter = parameter == null ? new FreeTypeFontParameter() : parameter;
    char[] characters = parameter.characters.toCharArray();
    int charactersLength = characters.length;
    boolean incremental = parameter.incremental;
    int flags = getLoadingFlags(parameter);
    setPixelSizes(0, parameter.size);
    // set general font data
    SizeMetrics fontMetrics = face.getSize().getMetrics();
    data.flipped = parameter.flip;
    data.ascent = FreeType.toInt(fontMetrics.getAscender());
    data.descent = FreeType.toInt(fontMetrics.getDescender());
    data.lineHeight = FreeType.toInt(fontMetrics.getHeight());
    float baseLine = data.ascent;
    // if bitmapped
    if (bitmapped && (data.lineHeight == 0)) {
        for (int c = 32; c < (32 + face.getNumGlyphs()); c++) {
            if (loadChar(c, flags)) {
                int lh = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
                data.lineHeight = (lh > data.lineHeight) ? lh : data.lineHeight;
            }
        }
    }
    data.lineHeight += parameter.spaceY;
    // determine space width
    if (loadChar(' ', flags) || loadChar('l', flags)) {
        data.spaceWidth = FreeType.toInt(face.getGlyph().getMetrics().getHoriAdvance());
    } else {
        // Possibly very wrong.
        data.spaceWidth = face.getMaxAdvanceWidth();
    }
    // determine x-height
    for (char xChar : data.xChars) {
        if (!loadChar(xChar, flags))
            continue;
        data.xHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
        break;
    }
    if (data.xHeight == 0)
        throw new GdxRuntimeException("No x-height character found in font");
    // determine cap height
    for (char capChar : data.capChars) {
        if (!loadChar(capChar, flags))
            continue;
        data.capHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight());
        break;
    }
    if (!bitmapped && data.capHeight == 1)
        throw new GdxRuntimeException("No cap character found in font");
    data.ascent -= data.capHeight;
    data.down = -data.lineHeight;
    if (parameter.flip) {
        data.ascent = -data.ascent;
        data.down = -data.down;
    }
    boolean ownsAtlas = false;
    PixmapPacker packer = parameter.packer;
    if (packer == null) {
        // Create a packer.
        int size;
        PackStrategy packStrategy;
        if (incremental) {
            size = maxTextureSize;
            packStrategy = new GuillotineStrategy();
        } else {
            int maxGlyphHeight = (int) Math.ceil(data.lineHeight);
            size = MathUtils.nextPowerOfTwo((int) Math.sqrt(maxGlyphHeight * maxGlyphHeight * charactersLength));
            if (maxTextureSize > 0)
                size = Math.min(size, maxTextureSize);
            packStrategy = new SkylineStrategy();
        }
        ownsAtlas = true;
        packer = new PixmapPacker(size, size, Format.RGBA8888, 1, false, packStrategy);
        packer.setTransparentColor(parameter.color);
        packer.getTransparentColor().a = 0;
        if (parameter.borderWidth > 0) {
            packer.setTransparentColor(parameter.borderColor);
            packer.getTransparentColor().a = 0;
        }
    }
    if (incremental)
        data.glyphs = new Array(charactersLength + 32);
    Stroker stroker = null;
    if (parameter.borderWidth > 0) {
        stroker = library.createStroker();
        stroker.set((int) (parameter.borderWidth * 64f), parameter.borderStraight ? FreeType.FT_STROKER_LINECAP_BUTT : FreeType.FT_STROKER_LINECAP_ROUND, parameter.borderStraight ? FreeType.FT_STROKER_LINEJOIN_MITER_FIXED : FreeType.FT_STROKER_LINEJOIN_ROUND, 0);
    }
    Glyph missingGlyph = createGlyph('\0', data, parameter, stroker, baseLine, packer);
    if (missingGlyph != null && missingGlyph.width != 0 && missingGlyph.height != 0) {
        data.setGlyph('\0', missingGlyph);
        if (incremental)
            data.glyphs.add(missingGlyph);
    }
    // Create glyphs largest height first for best packing.
    int[] heights = new int[charactersLength];
    for (int i = 0, n = charactersLength; i < n; i++) {
        int height = loadChar(characters[i], flags) ? FreeType.toInt(face.getGlyph().getMetrics().getHeight()) : 0;
        heights[i] = height;
    }
    int heightsCount = heights.length;
    while (heightsCount > 0) {
        int best = 0, maxHeight = heights[0];
        for (int i = 1; i < heightsCount; i++) {
            int height = heights[i];
            if (height > maxHeight) {
                maxHeight = height;
                best = i;
            }
        }
        char c = characters[best];
        Glyph glyph = createGlyph(c, data, parameter, stroker, baseLine, packer);
        if (glyph != null) {
            data.setGlyph(c, glyph);
            if (incremental)
                data.glyphs.add(glyph);
        }
        heightsCount--;
        heights[best] = heights[heightsCount];
        char tmpChar = characters[best];
        characters[best] = characters[heightsCount];
        characters[heightsCount] = tmpChar;
    }
    if (stroker != null && !incremental)
        stroker.dispose();
    if (incremental) {
        data.generator = this;
        data.parameter = parameter;
        data.stroker = stroker;
        data.packer = packer;
    }
    // Generate kerning.
    parameter.kerning &= face.hasKerning();
    if (parameter.kerning) {
        for (int i = 0; i < charactersLength; i++) {
            char firstChar = characters[i];
            Glyph first = data.getGlyph(firstChar);
            if (first == null)
                continue;
            int firstIndex = face.getCharIndex(firstChar);
            for (int ii = i; ii < charactersLength; ii++) {
                char secondChar = characters[ii];
                Glyph second = data.getGlyph(secondChar);
                if (second == null)
                    continue;
                int secondIndex = face.getCharIndex(secondChar);
                int kerning = face.getKerning(firstIndex, secondIndex, 0);
                if (kerning != 0)
                    first.setKerning(secondChar, FreeType.toInt(kerning));
                kerning = face.getKerning(secondIndex, firstIndex, 0);
                if (kerning != 0)
                    second.setKerning(firstChar, FreeType.toInt(kerning));
            }
        }
    }
    // Generate texture regions.
    if (ownsAtlas) {
        data.regions = new Array();
        packer.updateTextureRegions(data.regions, parameter.minFilter, parameter.magFilter, parameter.genMipMaps);
    }
    // Set space glyph.
    Glyph spaceGlyph = data.getGlyph(' ');
    if (spaceGlyph == null) {
        spaceGlyph = new Glyph();
        spaceGlyph.xadvance = (int) data.spaceWidth + parameter.spaceX;
        spaceGlyph.id = (int) ' ';
        data.setGlyph(' ', spaceGlyph);
    }
    if (spaceGlyph.width == 0)
        spaceGlyph.width = (int) (spaceGlyph.xadvance + data.padRight);
    return data;
}
Also used : SizeMetrics(com.badlogic.gdx.graphics.g2d.freetype.FreeType.SizeMetrics) Stroker(com.badlogic.gdx.graphics.g2d.freetype.FreeType.Stroker) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) GuillotineStrategy(com.badlogic.gdx.graphics.g2d.PixmapPacker.GuillotineStrategy) SkylineStrategy(com.badlogic.gdx.graphics.g2d.PixmapPacker.SkylineStrategy) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) Glyph(com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph) PackStrategy(com.badlogic.gdx.graphics.g2d.PixmapPacker.PackStrategy)

Example 3 with PixmapPacker

use of com.badlogic.gdx.graphics.g2d.PixmapPacker 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 4 with PixmapPacker

use of com.badlogic.gdx.graphics.g2d.PixmapPacker in project var3dframe by Var3D.

the class FreeBitmapFont method createForEmoji.

private void createForEmoji(String characters, boolean haveMinPageSize) {
    // characters = characters.replaceAll("[\\t\\n\\x0B\\f\\r]", "");
    cs.clear();
    char[] chars = characters.toCharArray();
    for (int i = 0, len = chars.length; i < len; i++) {
        char ch = chars[i];
        String txt = String.valueOf(ch);
        boolean isEmoji = isEmojiCharacter(ch);
        if (isEmoji) {
            if (i + 1 < len) {
                txt += chars[i + 1];
                i++;
            }
        }
        if (charSet.add(txt))
            cs.add(txt);
    }
    // 根据字符参数和字符数量计算一个最小尺寸
    if (haveMinPageSize) {
        pageWidth = (paint.getTextSize() + 2) * (int) (Math.sqrt(cs.size) + 1);
    }
    if (packer == null) {
        packer = new PixmapPacker(pageWidth, pageWidth, Format.RGBA8888, 4, false);
    }
    for (int i = 0; i < cs.size; i++) {
        String txt = cs.get(i);
        char c;
        if (txt.length() == 1) {
            // 判断该符号是否在key库中
            boolean isNotKey = emojiKey.indexOf(txt) < 0;
            if (isNotKey) {
                c = txt.charAt(0);
                if (data.getGlyph(c) != null)
                    continue;
            } else {
                // 如果是key库的一员,那么给它映射一下
                boolean isCreated = isCreatedEmoji2(txt);
                if (isCreated)
                    // 如果创建过的,就跳过
                    continue;
                if (ekid > emojiKey.length() - 1)
                    continue;
                // 如果没有创建过,就取一个key来用
                String key = emojiKey.substring(ekid++, ekid);
                c = key.charAt(0);
                Emoji emoj = new Emoji();
                emoj.text = txt;
                emoj.key = key;
                emojis2.add(emoj);
            // Gdx.app.log("aaaaaaa", "keyd=" + emoj.key);
            }
        } else {
            // 说明是emoji
            boolean isCreated = isCreatedEmoji4(txt);
            if (isCreated)
                // 如果创建过的,就跳过
                continue;
            if (ekid > emojiKey.length() - 1)
                continue;
            // 如果没有创建过,就取一个key来用
            String key = emojiKey.substring(ekid++, ekid);
            c = key.charAt(0);
            Emoji emoj = new Emoji();
            emoj.text = txt;
            emoj.key = key;
            emojis4.add(emoj);
        }
        if (data.getGlyph(c) != null)
            continue;
        Pixmap pixmap = listener.getFontPixmap(txt, paint);
        putGlyph(c, pixmap);
    }
    upData();
    // 如果只有一张纹理,则设置使用一张纹理,提高运行速度
    if (getRegions().size == 1) {
        setOwnsTexture(true);
    } else {
        setOwnsTexture(false);
    }
}
Also used : PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 5 with PixmapPacker

use of com.badlogic.gdx.graphics.g2d.PixmapPacker in project var3dframe by Var3D.

the class FreeBitmapFont method create.

private void create(String characters, boolean haveMinPageSize) {
    if (!isEmoji) {
        characters = characters.replaceAll("[\\t\\n\\x0B\\f\\r]", "");
        cs.clear();
        char[] chars = characters.toCharArray();
        for (char c : chars) {
            if (charSet.add(c + "")) {
                cs.add(c + "");
            }
        }
        // 根据字符参数和字符数量计算一个最小尺寸
        if (haveMinPageSize) {
            pageWidth = (paint.getTextSize() + 2) * (int) (Math.sqrt(cs.size) + 1);
        }
        if (packer == null) {
            packer = new PixmapPacker(pageWidth, pageWidth, Format.RGBA8888, 4, false);
        }
        for (int i = 0; i < cs.size; i++) {
            String txt = cs.get(i);
            char c = txt.charAt(0);
            // 如果该字符存在于emoji地址库里,则创建这个emoji的纹理
            String css = c + "";
            if (emojiSet.get(css) != null) {
                charSet.remove(css);
                EmojiDate date = emojiSet.get(css);
                appendEmoji("" + c, date.path, date.size);
                continue;
            }
            Pixmap pixmap = listener.getFontPixmap(txt, paint);
            putGlyph(c, pixmap);
        }
        // updataSize(size);
        upData();
        // 如果只有一张纹理,则设置使用一张纹理,提高运行速度
        if (getRegions().size == 1) {
            setOwnsTexture(true);
        } else {
            setOwnsTexture(false);
        }
    } else {
        createForEmoji(characters, haveMinPageSize);
    }
}
Also used : PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Aggregations

PixmapPacker (com.badlogic.gdx.graphics.g2d.PixmapPacker)9 Pixmap (com.badlogic.gdx.graphics.Pixmap)4 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 FreeTypeFontGenerator (com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator)3 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 Array (com.badlogic.gdx.utils.Array)2 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)2 ApplicationAdapter (com.badlogic.gdx.ApplicationAdapter)1 InputAdapter (com.badlogic.gdx.InputAdapter)1 LwjglApplication (com.badlogic.gdx.backends.lwjgl.LwjglApplication)1 LwjglApplicationConfiguration (com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration)1 FileHandle (com.badlogic.gdx.files.FileHandle)1 Color (com.badlogic.gdx.graphics.Color)1 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 Texture (com.badlogic.gdx.graphics.Texture)1 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1 Glyph (com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)1 GuillotineStrategy (com.badlogic.gdx.graphics.g2d.PixmapPacker.GuillotineStrategy)1 PackStrategy (com.badlogic.gdx.graphics.g2d.PixmapPacker.PackStrategy)1 Page (com.badlogic.gdx.graphics.g2d.PixmapPacker.Page)1