Search in sources :

Example 1 with ColorEffect

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

the class GlyphPage method renderGlyph.

/** Loads a single glyph to the backing texture, if it fits. */
private boolean renderGlyph(Glyph glyph, int pageX, int pageY, int width, int height) {
    scratchGraphics.setComposite(AlphaComposite.Clear);
    scratchGraphics.fillRect(0, 0, MAX_GLYPH_SIZE, MAX_GLYPH_SIZE);
    scratchGraphics.setComposite(AlphaComposite.SrcOver);
    ByteBuffer glyphPixels = scratchByteBuffer;
    int format;
    if (unicodeFont.getRenderType() == RenderType.FreeType && unicodeFont.bitmapFont != null) {
        BitmapFontData data = unicodeFont.bitmapFont.getData();
        BitmapFont.Glyph g = data.getGlyph((char) glyph.getCodePoint());
        Pixmap fontPixmap = unicodeFont.bitmapFont.getRegions().get(g.page).getTexture().getTextureData().consumePixmap();
        int fontWidth = fontPixmap.getWidth();
        int padTop = unicodeFont.getPaddingTop(), padBottom = unicodeFont.getPaddingBottom();
        int padLeftBytes = unicodeFont.getPaddingLeft() * 4;
        int padXBytes = padLeftBytes + unicodeFont.getPaddingRight() * 4;
        int glyphRowBytes = width * 4, fontRowBytes = g.width * 4;
        ByteBuffer fontPixels = fontPixmap.getPixels();
        byte[] row = new byte[glyphRowBytes];
        glyphPixels.position(0);
        for (int i = 0; i < padTop; i++) glyphPixels.put(row);
        glyphPixels.position((height - padBottom) * glyphRowBytes);
        for (int i = 0; i < padBottom; i++) glyphPixels.put(row);
        glyphPixels.position(padTop * glyphRowBytes);
        for (int y = 0, n = g.height; y < n; y++) {
            fontPixels.position(((g.srcY + y) * fontWidth + g.srcX) * 4);
            fontPixels.get(row, padLeftBytes, fontRowBytes);
            glyphPixels.put(row);
        }
        fontPixels.position(0);
        glyphPixels.position(height * glyphRowBytes);
        glyphPixels.flip();
        format = GL11.GL_RGBA;
    } else {
        // Draw the glyph to the scratch image using Java2D.
        if (unicodeFont.getRenderType() == RenderType.Native) {
            for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) {
                Effect effect = (Effect) iter.next();
                if (effect instanceof ColorEffect)
                    scratchGraphics.setColor(((ColorEffect) effect).getColor());
            }
            scratchGraphics.setColor(java.awt.Color.white);
            scratchGraphics.setFont(unicodeFont.getFont());
            scratchGraphics.drawString("" + (char) glyph.getCodePoint(), 0, unicodeFont.getAscent());
        } else if (unicodeFont.getRenderType() == RenderType.Java) {
            scratchGraphics.setColor(java.awt.Color.white);
            for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) ((Effect) iter.next()).draw(scratchImage, scratchGraphics, unicodeFont, glyph);
            // The shape will never be needed again.
            glyph.setShape(null);
        }
        width = Math.min(width, texture.getWidth());
        height = Math.min(height, texture.getHeight());
        WritableRaster raster = scratchImage.getRaster();
        int[] row = new int[width];
        for (int y = 0; y < height; y++) {
            raster.getDataElements(0, y, width, 1, row);
            scratchIntBuffer.put(row);
        }
        format = GL12.GL_BGRA;
    }
    // Simple deduplication, doesn't work across pages of course.
    String hash = "";
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(glyphPixels);
        BigInteger bigInt = new BigInteger(1, md.digest());
        hash = bigInt.toString(16);
    } catch (NoSuchAlgorithmException ex) {
    }
    scratchByteBuffer.clear();
    scratchIntBuffer.clear();
    try {
        for (int i = 0, n = hashes.size(); i < n; i++) {
            String other = hashes.get(i);
            if (other.equals(hash)) {
                Glyph dupe = pageGlyphs.get(i);
                glyph.setTexture(dupe.texture, dupe.u, dupe.v, dupe.u2, dupe.v2);
                return false;
            }
        }
    } finally {
        hashes.add(hash);
        pageGlyphs.add(glyph);
    }
    Gdx.gl.glTexSubImage2D(texture.glTarget, 0, pageX, pageY, width, height, format, GL11.GL_UNSIGNED_BYTE, glyphPixels);
    float u = pageX / (float) texture.getWidth();
    float v = pageY / (float) texture.getHeight();
    float u2 = (pageX + width) / (float) texture.getWidth();
    float v2 = (pageY + height) / (float) texture.getHeight();
    glyph.setTexture(texture, u, v, u2, v2);
    return true;
}
Also used : BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ByteBuffer(java.nio.ByteBuffer) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) WritableRaster(java.awt.image.WritableRaster) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) BigInteger(java.math.BigInteger) Effect(com.badlogic.gdx.tools.hiero.unicodefont.effects.Effect) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) MessageDigest(java.security.MessageDigest) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Example 2 with ColorEffect

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

the class NewFontDialog method refreshFontPreview.

/**
 */
public void refreshFontPreview() {
    try {
        String fontName = selectFonts.getSelected();
        Gdx.app.log("FontPickerDialog", "Refreshing preview for font: " + fontName);
        File fontPath = game.fm.fonts.get(selectFonts.getSelected());
        Gdx.app.log("FontPickerDialog", "Loading font from file:" + fontPath);
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontPath);
        UnicodeFont unicodeFont = new UnicodeFont(font, Integer.valueOf(selectSize.getSelected()), checkBold.isChecked(), checkItalic.isChecked());
        if (checkShadow.isChecked() == true) {
            ColorEffect colorEffect = new ColorEffect();
            colorEffect.setColor(java.awt.Color.BLACK);
            unicodeFont.getEffects().add(colorEffect);
            ShadowEffect shadow = new ShadowEffect();
            shadow.setOpacity(1.0f);
            shadow.setXDistance(1);
            shadow.setYDistance(1);
            shadow.setColor(java.awt.Color.WHITE);
            unicodeFont.getEffects().add(shadow);
        } else {
            ColorEffect colorEffect = new ColorEffect();
            colorEffect.setColor(java.awt.Color.WHITE);
            unicodeFont.getEffects().add(colorEffect);
        }
        unicodeFont.addAsciiGlyphs();
        String newFontName = generateProperFontName(fontName);
        textFontName.setText(newFontName);
        // Create bitmap font
        BMFontUtil bfu = new BMFontUtil(unicodeFont);
        FileHandle handle = new FileHandle(System.getProperty("java.io.tmpdir")).child(newFontName);
        FileHandle handleFont = new FileHandle(handle.file().getAbsolutePath() + ".fnt");
        bfu.save(handle.file());
        FileHandle handleImage = new FileHandle(System.getProperty("java.io.tmpdir")).child(newFontName + ".png");
        TextField.TextFieldStyle textStyle = new TextField.TextFieldStyle();
        textStyle.cursor = game.skin.getDrawable("cursor");
        textStyle.selection = game.skin.getDrawable("selection");
        textStyle.background = game.skin.getDrawable("textfield");
        textStyle.fontColor = Color.YELLOW;
        textStyle.font = new BitmapFont(handleFont, handleImage, false);
        textFontPreview.setStyle(textStyle);
        // Have to do this to force clipping of font
        textFontPreview.setText(textFontPreview.getText());
    } catch (Exception e) {
        e.printStackTrace();
        textFontPreview.getStyle().font = game.skin.getFont("default-font");
        // Have to do this to force clipping of font
        textFontPreview.setText(textFontPreview.getText());
    }
}
Also used : UnicodeFont(com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont) FileHandle(com.badlogic.gdx.files.FileHandle) Font(java.awt.Font) UnicodeFont(com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) ShadowEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ShadowEffect) BMFontUtil(com.badlogic.gdx.tools.hiero.BMFontUtil) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) File(java.io.File)

Example 3 with ColorEffect

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

the class GlyphPage method renderGlyph.

/**
 * Loads a single glyph to the backing texture, if it fits.
 */
private void renderGlyph(Glyph glyph, int width, int height) {
    // Draw the glyph to the scratch image using Java2D.
    scratchGraphics.setComposite(AlphaComposite.Clear);
    scratchGraphics.fillRect(0, 0, MAX_GLYPH_SIZE, MAX_GLYPH_SIZE);
    scratchGraphics.setComposite(AlphaComposite.SrcOver);
    if (unicodeFont.getNativeRendering()) {
        for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) {
            Effect effect = (Effect) iter.next();
            if (effect instanceof ColorEffect)
                scratchGraphics.setColor(((ColorEffect) effect).getColor());
        }
        scratchGraphics.setColor(java.awt.Color.white);
        scratchGraphics.setFont(unicodeFont.getFont());
        scratchGraphics.drawString("" + (char) glyph.getCodePoint(), 0, unicodeFont.getAscent());
    } else {
        scratchGraphics.setColor(java.awt.Color.white);
        for (Iterator iter = unicodeFont.getEffects().iterator(); iter.hasNext(); ) ((Effect) iter.next()).draw(scratchImage, scratchGraphics, unicodeFont, glyph);
        // The shape will never be needed again.
        glyph.setShape(null);
    }
    width = Math.min(width, texture.getWidth());
    height = Math.min(height, texture.getHeight());
    WritableRaster raster = scratchImage.getRaster();
    int[] row = new int[width];
    for (int y = 0; y < height; y++) {
        raster.getDataElements(0, y, width, 1, row);
        scratchIntBuffer.put(row);
    }
    GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, pageX, pageY, width, height, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, scratchByteBuffer);
    scratchIntBuffer.clear();
    float u = pageX / (float) texture.getWidth();
    float v = pageY / (float) texture.getHeight();
    float u2 = (pageX + width) / (float) texture.getWidth();
    float v2 = (pageY + height) / (float) texture.getHeight();
    glyph.setTexture(texture, u, v, u2, v2);
}
Also used : ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect) WritableRaster(java.awt.image.WritableRaster) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) Effect(com.badlogic.gdx.tools.hiero.unicodefont.effects.Effect) ColorEffect(com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect)

Aggregations

ColorEffect (com.badlogic.gdx.tools.hiero.unicodefont.effects.ColorEffect)3 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 Effect (com.badlogic.gdx.tools.hiero.unicodefont.effects.Effect)2 WritableRaster (java.awt.image.WritableRaster)2 Iterator (java.util.Iterator)2 ListIterator (java.util.ListIterator)2 FileHandle (com.badlogic.gdx.files.FileHandle)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1 TextField (com.badlogic.gdx.scenes.scene2d.ui.TextField)1 BMFontUtil (com.badlogic.gdx.tools.hiero.BMFontUtil)1 UnicodeFont (com.badlogic.gdx.tools.hiero.unicodefont.UnicodeFont)1 ShadowEffect (com.badlogic.gdx.tools.hiero.unicodefont.effects.ShadowEffect)1 Font (java.awt.Font)1 File (java.io.File)1 BigInteger (java.math.BigInteger)1 ByteBuffer (java.nio.ByteBuffer)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1