Search in sources :

Example 1 with GlyphPage

use of com.badlogic.gdx.tools.hiero.unicodefont.GlyphPage in project gdx-skineditor by cobolfoo.

the class BMFontUtil method save.

public void save(File outputBMFontFile) throws IOException {
    File outputDir = outputBMFontFile.getParentFile();
    String outputName = outputBMFontFile.getName();
    if (outputName.endsWith(".fnt"))
        outputName = outputName.substring(0, outputName.length() - 4);
    unicodeFont.setGlyphPageHeight(1024);
    unicodeFont.loadGlyphs();
    // This is a tweak to limit texture height to minimum
    GlyphPage p = (GlyphPage) unicodeFont.getGlyphPages().get(0);
    // keep space for shadow
    int realHeight = p.getPageY() + unicodeFont.getLineHeight() + 5;
    Gdx.app.log("BMFontUtil", "Real texture height: " + realHeight);
    PrintStream out = new PrintStream(new FileOutputStream(new File(outputDir, outputName + ".fnt")));
    Font font = unicodeFont.getFont();
    int pageWidth = unicodeFont.getGlyphPageWidth();
    int pageHeight = unicodeFont.getGlyphPageHeight();
    out.println("info face=\"" + font.getFontName() + "\" size=" + font.getSize() + " bold=" + (font.isBold() ? 1 : 0) + " italic=" + (font.isItalic() ? 1 : 0) + " charset=\"\" unicode=0 stretchH=100 smooth=1 aa=1 padding=" + unicodeFont.getPaddingTop() + "," + unicodeFont.getPaddingLeft() + "," + unicodeFont.getPaddingBottom() + "," + unicodeFont.getPaddingRight() + " spacing=" + unicodeFont.getPaddingAdvanceX() + "," + unicodeFont.getPaddingAdvanceY());
    out.println("common lineHeight=" + unicodeFont.getLineHeight() + " base=" + unicodeFont.getAscent() + " scaleW=" + pageWidth + " scaleH=" + pageHeight + " pages=" + unicodeFont.getGlyphPages().size() + " packed=0");
    int pageIndex = 0, glyphCount = 0;
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        String fileName;
        if (pageIndex == 0 && !pageIter.hasNext())
            fileName = outputName + ".png";
        else
            fileName = outputName + (pageIndex + 1) + ".png";
        out.println("page id=" + pageIndex + " file=\"" + fileName + "\"");
        glyphCount += page.getGlyphs().size();
        pageIndex++;
    }
    out.println("chars count=" + glyphCount);
    // Always output space entry (codepoint 32).
    int[] glyphMetrics = getGlyphMetrics(font, 32);
    int xAdvance = glyphMetrics[1];
    out.println("char id=32   x=0     y=0     width=0     height=0     xoffset=0     yoffset=" + unicodeFont.getAscent() + "    xadvance=" + xAdvance + "     page=0  chnl=0 ");
    pageIndex = 0;
    List allGlyphs = new ArrayList(512);
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        for (Iterator glyphIter = page.getGlyphs().iterator(); glyphIter.hasNext(); ) {
            Glyph glyph = (Glyph) glyphIter.next();
            glyphMetrics = getGlyphMetrics(font, glyph.getCodePoint());
            int xOffset = glyphMetrics[0];
            xAdvance = glyphMetrics[1];
            out.println("char id=" + glyph.getCodePoint() + "   " + "x=" + (int) (glyph.getU() * pageWidth) + "     y=" + (int) (glyph.getV() * pageHeight) + "     width=" + glyph.getWidth() + "     height=" + glyph.getHeight() + "     xoffset=" + xOffset + "     yoffset=" + glyph.getYOffset() + "    xadvance=" + xAdvance + "     page=" + pageIndex + "  chnl=0 ");
        }
        allGlyphs.addAll(page.getGlyphs());
        pageIndex++;
    }
    String ttfFileRef = unicodeFont.getFontFile();
    if (ttfFileRef == null)
        System.out.println("Kerning information could not be output because a TTF font file was not specified.");
    else {
        Kerning kerning = new Kerning();
        try {
            kerning.load(Gdx.files.internal(ttfFileRef).read(), font.getSize());
        } catch (IOException ex) {
            System.out.println("Unable to read kerning information from font: " + ttfFileRef);
        }
        Map glyphCodeToCodePoint = new HashMap();
        for (Iterator iter = allGlyphs.iterator(); iter.hasNext(); ) {
            Glyph glyph = (Glyph) iter.next();
            glyphCodeToCodePoint.put(new Integer(getGlyphCode(font, glyph.getCodePoint())), new Integer(glyph.getCodePoint()));
        }
        List kernings = new ArrayList(256);
        class KerningPair {

            public int firstCodePoint, secondCodePoint, offset;
        }
        for (Iterator iter1 = allGlyphs.iterator(); iter1.hasNext(); ) {
            Glyph firstGlyph = (Glyph) iter1.next();
            int firstGlyphCode = getGlyphCode(font, firstGlyph.getCodePoint());
            int[] values = kerning.getValues(firstGlyphCode);
            if (values == null)
                continue;
            for (int i = 0; i < values.length; i++) {
                Integer secondCodePoint = (Integer) glyphCodeToCodePoint.get(new Integer(values[i] & 0xffff));
                // We may not be outputting the second character.
                if (secondCodePoint == null)
                    continue;
                int offset = values[i] >> 16;
                KerningPair pair = new KerningPair();
                pair.firstCodePoint = firstGlyph.getCodePoint();
                pair.secondCodePoint = secondCodePoint.intValue();
                pair.offset = offset;
                kernings.add(pair);
            }
        }
        out.println("kernings count=" + kerning.getCount());
        for (Iterator iter = kernings.iterator(); iter.hasNext(); ) {
            KerningPair pair = (KerningPair) iter.next();
            out.println("kerning first=" + pair.firstCodePoint + "  second=" + pair.secondCodePoint + "  amount=" + pair.offset);
        }
    }
    out.close();
    int width = unicodeFont.getGlyphPageWidth();
    int height = unicodeFont.getGlyphPageHeight();
    IntBuffer buffer = BufferUtils.createIntBuffer(width * height);
    BufferedImage pageImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    int[] row = new int[width];
    pageIndex = 0;
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        String fileName;
        if (pageIndex == 0 && !pageIter.hasNext())
            fileName = outputName + ".png";
        else
            fileName = outputName + (pageIndex + 1) + ".png";
        page.getTexture().bind();
        buffer.clear();
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buffer);
        WritableRaster raster = pageImage.getRaster();
        for (int y = 0; y < height; y++) {
            buffer.get(row);
            raster.setDataElements(0, y, width, 1, row);
        }
        File imageOutputFile = new File(outputDir, fileName);
        if (realHeight < height) {
            ImageIO.write(pageImage.getSubimage(0, 0, width, realHeight), "png", imageOutputFile);
        } else {
            ImageIO.write(pageImage, "png", imageOutputFile);
        }
        pageIndex++;
    }
}
Also used : PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Font(java.awt.Font) UnicodeFont(com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont) BufferedImage(java.awt.image.BufferedImage) GlyphPage(com.badlogic.gdx.tools.hiero.unicodefont.GlyphPage) WritableRaster(java.awt.image.WritableRaster) FileOutputStream(java.io.FileOutputStream) Glyph(com.badlogic.gdx.tools.hiero.unicodefont.Glyph) IntBuffer(java.nio.IntBuffer) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with GlyphPage

use of com.badlogic.gdx.tools.hiero.unicodefont.GlyphPage in project libgdx by libgdx.

the class BMFontUtil method save.

public void save(File outputBMFontFile) throws IOException {
    File outputDir = outputBMFontFile.getParentFile();
    String outputName = outputBMFontFile.getName();
    if (outputName.endsWith(".fnt"))
        outputName = outputName.substring(0, outputName.length() - 4);
    // Always include space and the missing gyph.
    getGlyph(' ');
    getGlyph('');
    unicodeFont.loadGlyphs();
    PrintStream out = new PrintStream(new FileOutputStream(new File(outputDir, outputName + ".fnt")));
    Font font = unicodeFont.getFont();
    int pageWidth = unicodeFont.getGlyphPageWidth();
    int pageHeight = unicodeFont.getGlyphPageHeight();
    out.println("info face=\"" + font.getFontName() + "\" size=" + font.getSize() + " bold=" + (font.isBold() ? 1 : 0) + " italic=" + (font.isItalic() ? 1 : 0) + " charset=\"\" unicode=0 stretchH=100 smooth=1 aa=1 padding=" + unicodeFont.getPaddingTop() + "," + unicodeFont.getPaddingRight() + "," + unicodeFont.getPaddingBottom() + "," + unicodeFont.getPaddingLeft() + " spacing=" + unicodeFont.getPaddingAdvanceX() + "," + unicodeFont.getPaddingAdvanceY());
    out.println("common lineHeight=" + unicodeFont.getLineHeight() + " base=" + unicodeFont.getAscent() + " scaleW=" + pageWidth + " scaleH=" + pageHeight + " pages=" + unicodeFont.getGlyphPages().size() + " packed=0");
    int pageIndex = 0, glyphCount = 0;
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        String fileName;
        if (pageIndex == 0 && !pageIter.hasNext())
            fileName = outputName + ".png";
        else
            fileName = outputName + (pageIndex + 1) + ".png";
        out.println("page id=" + pageIndex + " file=\"" + fileName + "\"");
        glyphCount += page.getGlyphs().size();
        pageIndex++;
    }
    out.println("chars count=" + glyphCount);
    pageIndex = 0;
    List allGlyphs = new ArrayList(512);
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        List<Glyph> glyphs = page.getGlyphs();
        Collections.sort(glyphs, new Comparator<Glyph>() {

            public int compare(Glyph o1, Glyph o2) {
                return o1.getCodePoint() - o2.getCodePoint();
            }
        });
        for (Iterator glyphIter = page.getGlyphs().iterator(); glyphIter.hasNext(); ) {
            Glyph glyph = (Glyph) glyphIter.next();
            writeGlyph(out, pageWidth, pageHeight, pageIndex, glyph);
        }
        allGlyphs.addAll(page.getGlyphs());
        pageIndex++;
    }
    String ttfFileRef = unicodeFont.getFontFile();
    if (ttfFileRef == null)
        System.out.println("Kerning information could not be output because a TTF font file was not specified.");
    else {
        Kerning kerning = new Kerning();
        try {
            kerning.load(Gdx.files.internal(ttfFileRef).read(), font.getSize());
        } catch (IOException ex) {
            System.out.println("Unable to read kerning information from font: " + ttfFileRef);
            ex.printStackTrace();
        }
        IntIntMap glyphCodeToCodePoint = new IntIntMap();
        for (Iterator iter = allGlyphs.iterator(); iter.hasNext(); ) {
            Glyph glyph = (Glyph) iter.next();
            glyphCodeToCodePoint.put(new Integer(getGlyphCode(font, glyph.getCodePoint())), new Integer(glyph.getCodePoint()));
        }
        List kernings = new ArrayList(256);
        class KerningPair {

            public int firstCodePoint, secondCodePoint, offset;
        }
        for (IntIntMap.Entry entry : kerning.getKernings()) {
            int firstGlyphCode = entry.key >> 16;
            int secondGlyphCode = entry.key & 0xffff;
            int offset = entry.value;
            int firstCodePoint = glyphCodeToCodePoint.get(firstGlyphCode, -1);
            int secondCodePoint = glyphCodeToCodePoint.get(secondGlyphCode, -1);
            if (firstCodePoint == -1 || secondCodePoint == -1 || offset == 0) {
                // We are not outputting one or both of these glyphs, or the offset is zero anyway.
                continue;
            }
            KerningPair pair = new KerningPair();
            pair.firstCodePoint = firstCodePoint;
            pair.secondCodePoint = secondCodePoint;
            pair.offset = offset;
            kernings.add(pair);
        }
        out.println("kernings count=" + kernings.size());
        for (Iterator iter = kernings.iterator(); iter.hasNext(); ) {
            KerningPair pair = (KerningPair) iter.next();
            out.println("kerning first=" + pair.firstCodePoint + " second=" + pair.secondCodePoint + " amount=" + pair.offset);
        }
    }
    out.close();
    int width = unicodeFont.getGlyphPageWidth();
    int height = unicodeFont.getGlyphPageHeight();
    IntBuffer buffer = BufferUtils.createIntBuffer(width * height);
    BufferedImage pageImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    int[] row = new int[width];
    pageIndex = 0;
    for (Iterator pageIter = unicodeFont.getGlyphPages().iterator(); pageIter.hasNext(); ) {
        GlyphPage page = (GlyphPage) pageIter.next();
        String fileName;
        if (pageIndex == 0 && !pageIter.hasNext())
            fileName = outputName + ".png";
        else
            fileName = outputName + (pageIndex + 1) + ".png";
        page.getTexture().bind();
        buffer.clear();
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buffer);
        WritableRaster raster = pageImage.getRaster();
        for (int y = 0; y < height; y++) {
            buffer.get(row);
            raster.setDataElements(0, y, width, 1, row);
        }
        File imageOutputFile = new File(outputDir, fileName);
        ImageIO.write(pageImage, "png", imageOutputFile);
        pageIndex++;
    }
}
Also used : PrintStream(java.io.PrintStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Font(java.awt.Font) UnicodeFont(com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont) BufferedImage(java.awt.image.BufferedImage) IntIntMap(com.badlogic.gdx.utils.IntIntMap) GlyphPage(com.badlogic.gdx.tools.hiero.unicodefont.GlyphPage) WritableRaster(java.awt.image.WritableRaster) FileOutputStream(java.io.FileOutputStream) Glyph(com.badlogic.gdx.tools.hiero.unicodefont.Glyph) IntBuffer(java.nio.IntBuffer) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

Glyph (com.badlogic.gdx.tools.hiero.unicodefont.Glyph)2 GlyphPage (com.badlogic.gdx.tools.hiero.unicodefont.GlyphPage)2 UnicodeFont (com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont)2 Font (java.awt.Font)2 BufferedImage (java.awt.image.BufferedImage)2 WritableRaster (java.awt.image.WritableRaster)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 IntBuffer (java.nio.IntBuffer)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 IntIntMap (com.badlogic.gdx.utils.IntIntMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1