001: /*
002: * $Id: LocaTable.java,v 1.2 2007/12/20 18:33:30 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 Loca table
028: */
029: public class LocaTable extends TrueTypeTable {
030: /** if true, the table stores glyphs in long format */
031: private boolean isLong;
032:
033: /** the offsets themselves */
034: private int offsets[];
035:
036: /** Creates a new instance of HmtxTable */
037: protected LocaTable(TrueTypeFont ttf) {
038: super (TrueTypeTable.LOCA_TABLE);
039:
040: MaxpTable maxp = (MaxpTable) ttf.getTable("maxp");
041: int numGlyphs = maxp.getNumGlyphs();
042:
043: HeadTable head = (HeadTable) ttf.getTable("head");
044: short format = head.getIndexToLocFormat();
045: isLong = (format == 1);
046:
047: offsets = new int[numGlyphs + 1];
048: }
049:
050: /**
051: * get the offset, in bytes, of a given glyph from the start of
052: * the glyph table
053: */
054: public int getOffset(int glyphID) {
055: return offsets[glyphID];
056: }
057:
058: /**
059: * get the size, in bytes, of the given glyph
060: */
061: public int getSize(int glyphID) {
062: return offsets[glyphID + 1] - offsets[glyphID];
063: }
064:
065: /**
066: * Return true if the glyphs arte in long (int) format, or
067: * false if they are in short (short) format
068: */
069: public boolean isLongFormat() {
070: return isLong;
071: }
072:
073: /** get the data in this map as a ByteBuffer */
074: public ByteBuffer getData() {
075: int size = getLength();
076:
077: ByteBuffer buf = ByteBuffer.allocate(size);
078:
079: // write the offsets
080: for (int i = 0; i < offsets.length; i++) {
081: if (isLongFormat()) {
082: buf.putInt(offsets[i]);
083: } else {
084: buf.putShort((short) (offsets[i] / 2));
085: }
086: }
087:
088: // reset the start pointer
089: buf.flip();
090:
091: return buf;
092: }
093:
094: /** Initialize this structure from a ByteBuffer */
095: public void setData(ByteBuffer data) {
096: for (int i = 0; i < offsets.length; i++) {
097: if (isLongFormat()) {
098: offsets[i] = data.getInt();
099: } else {
100: offsets[i] = 2 * data.getShort();
101: }
102: }
103: }
104:
105: /**
106: * Get the length of this table
107: */
108: public int getLength() {
109: if (isLongFormat()) {
110: return offsets.length * 4;
111: } else {
112: return offsets.length * 2;
113: }
114: }
115: }
|