001: /*
002: * $Id: CMapFormat0.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: *
028: * @author jkaplan
029: */
030: public class CMapFormat0 extends CMap {
031:
032: /**
033: * The glyph index array
034: */
035: private byte[] glyphIndex;
036:
037: /** Creates a new instance of CMapFormat0 */
038: protected CMapFormat0(short language) {
039: super ((short) 0, language);
040:
041: byte[] initialIndex = new byte[256];
042: for (int i = 0; i < initialIndex.length; i++) {
043: initialIndex[i] = (byte) i;
044: }
045: setMap(initialIndex);
046: }
047:
048: /**
049: * Get the length of this table
050: */
051: public short getLength() {
052: return (short) 262;
053: }
054:
055: /**
056: * Map from a byte
057: */
058: public byte map(byte src) {
059: int i = 0xff & src;
060:
061: return glyphIndex[i];
062: }
063:
064: /**
065: * Cannot map from short
066: */
067: public char map(char src) {
068: if (src < 0 || src > 255) {
069: // out of range
070: return (char) 0;
071: }
072:
073: return (char) (map((byte) src) & 0xff);
074: }
075:
076: /**
077: * Get the src code which maps to the given glyphID
078: */
079: public char reverseMap(short glyphID) {
080: for (int i = 0; i < glyphIndex.length; i++) {
081: if ((glyphIndex[i] & 0xff) == glyphID) {
082: return (char) i;
083: }
084: }
085:
086: return (char) 0;
087: }
088:
089: /**
090: * Set the entire map
091: */
092: public void setMap(byte[] glyphIndex) {
093: if (glyphIndex.length != 256) {
094: throw new IllegalArgumentException(
095: "Glyph map must be size 256!");
096: }
097:
098: this .glyphIndex = glyphIndex;
099: }
100:
101: /**
102: * Set a single mapping entry
103: */
104: public void setMap(byte src, byte dest) {
105: int i = 0xff & src;
106:
107: glyphIndex[i] = dest;
108: }
109:
110: /**
111: * Get the whole map
112: */
113: protected byte[] getMap() {
114: return glyphIndex;
115: }
116:
117: /**
118: * Get the data in this map as a ByteBuffer
119: */
120: public ByteBuffer getData() {
121: ByteBuffer buf = ByteBuffer.allocate(262);
122:
123: buf.putShort(getFormat());
124: buf.putShort(getLength());
125: buf.putShort(getLanguage());
126: buf.put(getMap());
127:
128: // reset the position to the beginning of the buffer
129: buf.flip();
130:
131: return buf;
132: }
133:
134: /**
135: * Read the map in from a byte buffer
136: */
137: public void setData(int length, ByteBuffer data) {
138: if (length != 262) {
139: throw new IllegalArgumentException(
140: "Bad length for CMap format 0");
141: }
142:
143: if (data.remaining() != 256) {
144: throw new IllegalArgumentException(
145: "Wrong amount of data for CMap format 0");
146: }
147:
148: byte[] map = new byte[256];
149: data.get(map);
150:
151: setMap(map);
152: }
153: }
|