001: /*
002: * $Id: GlyfTable.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 Glyf table
028: */
029: public class GlyfTable extends TrueTypeTable {
030: /**
031: * the glyph data, as either a byte buffer (unparsed) or a
032: * glyph object (parsed)
033: */
034: private Object[] glyphs;
035:
036: /**
037: * The glyph location table
038: */
039: private LocaTable loca;
040:
041: /** Creates a new instance of HmtxTable */
042: protected GlyfTable(TrueTypeFont ttf) {
043: super (TrueTypeTable.GLYF_TABLE);
044:
045: loca = (LocaTable) ttf.getTable("loca");
046:
047: MaxpTable maxp = (MaxpTable) ttf.getTable("maxp");
048: int numGlyphs = maxp.getNumGlyphs();
049:
050: glyphs = new Object[numGlyphs];
051: }
052:
053: /**
054: * Get the glyph at a given index, parsing it as needed
055: */
056: public Glyf getGlyph(int index) {
057: Object o = glyphs[index];
058: if (o == null) {
059: return null;
060: }
061:
062: if (o instanceof ByteBuffer) {
063: Glyf g = Glyf.getGlyf((ByteBuffer) o);
064: glyphs[index] = g;
065:
066: return g;
067: } else {
068: return (Glyf) o;
069: }
070: }
071:
072: /** get the data in this map as a ByteBuffer */
073: public ByteBuffer getData() {
074: int size = getLength();
075:
076: ByteBuffer buf = ByteBuffer.allocate(size);
077:
078: // write the offsets
079: for (int i = 0; i < glyphs.length; i++) {
080: Object o = glyphs[i];
081: if (o == null) {
082: continue;
083: }
084:
085: ByteBuffer glyfData = null;
086: if (o instanceof ByteBuffer) {
087: glyfData = (ByteBuffer) o;
088: } else {
089: glyfData = ((Glyf) o).getData();
090: }
091:
092: glyfData.rewind();
093: buf.put(glyfData);
094: glyfData.flip();
095: }
096:
097: // reset the start pointer
098: buf.flip();
099:
100: return buf;
101: }
102:
103: /** Initialize this structure from a ByteBuffer */
104: public void setData(ByteBuffer data) {
105: for (int i = 0; i < glyphs.length; i++) {
106: int location = loca.getOffset(i);
107: int length = loca.getSize(i);
108:
109: if (length == 0) {
110: // undefined glyph
111: continue;
112: }
113:
114: data.position(location);
115: ByteBuffer glyfData = data.slice();
116: glyfData.limit(length);
117:
118: glyphs[i] = glyfData;
119: }
120: }
121:
122: /**
123: * Get the length of this table
124: */
125: public int getLength() {
126: int length = 0;
127:
128: for (int i = 0; i < glyphs.length; i++) {
129: Object o = glyphs[i];
130: if (o == null) {
131: continue;
132: }
133:
134: if (o instanceof ByteBuffer) {
135: length += ((ByteBuffer) o).remaining();
136: } else {
137: length += ((Glyf) o).getLength();
138: }
139: }
140:
141: return length;
142: }
143:
144: /**
145: * Create a pretty String
146: */
147: public String toString() {
148: StringBuffer buf = new StringBuffer();
149: String indent = " ";
150:
151: buf.append(indent + "Glyf Table: (" + glyphs.length
152: + " glyphs)\n");
153: buf.append(indent + " Glyf 0: " + getGlyph(0));
154:
155: return buf.toString();
156: }
157: }
|