Example usage for org.apache.commons.lang IntHashMap IntHashMap

List of usage examples for org.apache.commons.lang IntHashMap IntHashMap

Introduction

In this page you can find the example usage for org.apache.commons.lang IntHashMap IntHashMap.

Prototype

public IntHashMap(int initialCapacity) 

Source Link

Document

Constructs a new, empty hashtable with the specified initial capacity and default load factor, which is 0.75.

Usage

From source file:net.sergetk.mobile.lcdui.BitmapFont.java

/**
 * Creates a new font from the resource. The capacity of the color cache defines maximum size of
 * the color cache./*from   ww  w  .ja  va 2s  . c om*/
 * 
 * @param fontPath
 *            the resource name
 * @param colorCacheCapacity
 *            the maximum color cache size
 */
public BitmapFont(String fontPath, int colorCacheCapacity) {
    this.style = Font.STYLE_PLAIN;
    this.currentColor = 0;
    this.colorCache = new CacheEntry[colorCacheCapacity];
    this.colorUsageCounts = new IntHashMap(colorCacheCapacity * 2);

    try {
        InputStream input = new Object().getClass().getResourceAsStream(fontPath);
        if (input == null) {
            throw new IOException();
        }

        DataInputStream data = new DataInputStream(input);

        int streamLen = data.available();

        this.fontFilePath = fontPath;

        this.version = data.readByte();
        this.height = data.readByte();
        this.baseline = data.readByte();
        this.xIndent = data.readByte();
        this.yIndent = data.readByte();
        this.spaceWidth = data.readByte();

        characterMap = data.readUTF();
        int count = characterMap.length();

        // read characters widthes
        this.widths = new int[count];
        this.x = new int[count];
        this.y = new int[count];

        for (int i = 0; i < count; i++) {
            widths[i] = data.readByte();
        }

        baseImage = null;

        // the original implementation supported multiple-images
        // in the font file, but this is not necessary. Because I do
        // not want to change the encoding, I am leaving this byte that
        // used to represent the number of PNGs in the file
        data.skipBytes(1);

        short pngLen = data.readShort();
        byte[] buffer = new byte[pngLen];

        data.read(buffer, 0, pngLen);
        this.pngOffset = (short) (streamLen - pngLen);
        baseImage = Image.createImage(buffer, 0, pngLen);
        currentImage = baseImage;

        // calculate characters coordinates
        int curX = 0, curY = 0;
        for (int i = 0; i < count; i++) {
            if (widths[i] < 0) {
                // negative width points to another character
                int sourceIndex = -widths[i];
                widths[i] = widths[sourceIndex];
                x[i] = x[sourceIndex];
                y[i] = y[sourceIndex];
            } else {
                x[i] = curX;
                y[i] = curY;
                curX += widths[i];
            }
        }

        if (defaultFont == null)
            defaultFont = this;
    } catch (IOException e) {
        // Log.warn("IOException reading font: ", e);
        System.err.println("IOException reading font: " + e.getMessage());
        e.printStackTrace();
    }
}