001: /*
002: * $Id: HmtxTable.java,v 1.2 2007/12/20 18:33:31 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview.font.ttf;
023:
024: import java.nio.ByteBuffer;
025:
026: /**
027: * Model the TrueType Post table
028: *
029: * @author jkaplan
030: */
031: public class HmtxTable extends TrueTypeTable {
032: /** advance widths for any glyphs that have one */
033: short advanceWidths[];
034:
035: /** left side bearings for each glyph */
036: short leftSideBearings[];
037:
038: /** Creates a new instance of HmtxTable */
039: protected HmtxTable(TrueTypeFont ttf) {
040: super (TrueTypeTable.HMTX_TABLE);
041:
042: MaxpTable maxp = (MaxpTable) ttf.getTable("maxp");
043: int numGlyphs = maxp.getNumGlyphs();
044:
045: HheaTable hhea = (HheaTable) ttf.getTable("hhea");
046: int numOfLongHorMetrics = hhea.getNumOfLongHorMetrics();
047:
048: advanceWidths = new short[numOfLongHorMetrics];
049: leftSideBearings = new short[numGlyphs];
050: }
051:
052: /** get the advance of a given glyph */
053: public short getAdvance(int glyphID) {
054: if (glyphID < advanceWidths.length) {
055: return advanceWidths[glyphID];
056: } else {
057: return advanceWidths[advanceWidths.length - 1];
058: }
059: }
060:
061: /** get the left side bearing of a given glyph */
062: public short getLeftSideBearing(int glyphID) {
063: return leftSideBearings[glyphID];
064: }
065:
066: /** get the data in this map as a ByteBuffer */
067: public ByteBuffer getData() {
068: int size = getLength();
069:
070: ByteBuffer buf = ByteBuffer.allocate(size);
071:
072: // write the metrics
073: for (int i = 0; i < leftSideBearings.length; i++) {
074: if (i < advanceWidths.length) {
075: buf.putShort(advanceWidths[i]);
076: }
077:
078: buf.putShort(leftSideBearings[i]);
079: }
080:
081: // reset the start pointer
082: buf.flip();
083:
084: return buf;
085: }
086:
087: /** Initialize this structure from a ByteBuffer */
088: public void setData(ByteBuffer data) {
089: for (int i = 0; i < leftSideBearings.length; i++) {
090: if (i < advanceWidths.length) {
091: advanceWidths[i] = data.getShort();
092: }
093:
094: leftSideBearings[i] = data.getShort();
095: }
096: }
097:
098: /**
099: * Get the length of this table
100: */
101: public int getLength() {
102: return (advanceWidths.length * 2)
103: + (leftSideBearings.length * 2);
104: }
105: }
|