// Copyright 2001, FreeHEP.
package org.freehep.graphicsio.font.truetype;

import java.io.IOException;

/**
 * HMTX Table.
 *
 *  @author Simon Fischer
 *  @version $Id: TTFHMtxTable.java,v 1.1.1.1 2004/05/31 05:08:52 kuromaru Exp $
 */
public class TTFHMtxTable extends TTFTable {

    public int[] advanceWidth;
    public short[] leftSideBearing;
    public short[] leftSideBearing2;

    public String getTag() { return "hmtx"; }

    public void readTable() throws IOException{
	int numberOfHMetrics = ((TTFHHeaTable)getTable("hhea")).numberOfHMetrics;
	int numGlyphs = ((TTFMaxPTable)getTable("maxp")).numGlyphs;

	advanceWidth = new int[numberOfHMetrics];
	leftSideBearing = new short[numberOfHMetrics];
	for (int i = 0; i < numberOfHMetrics; i++) {
	    advanceWidth[i] = ttf.readUFWord();
	    leftSideBearing[i] = ttf.readFWord();
	}

	leftSideBearing2 = ttf.readShortArray(numGlyphs-numberOfHMetrics);
    }

    public String toString() {
	String str = super.toString();
	str += "\n  hMetrics["+advanceWidth.length+"] = {";
	for (int i = 0; i < advanceWidth.length; i++) {
	    if (i % 8 == 0) str += "\n    ";
	    str += "("+advanceWidth[i]+","+leftSideBearing[i]+") ";
	}
	str += "\n  }";
	str += "\n  lsb["+leftSideBearing2.length+"] = {";
	for (int i = 0; i < leftSideBearing2.length; i++) {
	    if (i % 16 == 0) str += "\n    ";
	    str += leftSideBearing2[i]+" ";
	}
	str += "\n  }";	
	return str;
    }
}
