KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fonts > truetype > FontFileReader


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: FontFileReader.java 489450 2006-12-21 19:54:40Z spepping $ */
19  
20 package org.apache.fop.fonts.truetype;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.commons.io.IOUtils;
27
28 /**
29  * Reads a TrueType font file into a byte array and
30  * provides file like functions for array access.
31  */

32 public class FontFileReader {
33
34     private int fsize; // file size
35
private int current; // current position in file
36
private byte[] file;
37
38     /**
39      * Initializes class and reads stream. Init does not close stream.
40      *
41      * @param in InputStream to read from new array with size + inc
42      * @throws IOException In case of an I/O problem
43      */

44     private void init(InputStream JavaDoc in) throws java.io.IOException JavaDoc {
45         this.file = IOUtils.toByteArray(in);
46         this.fsize = this.file.length;
47         this.current = 0;
48     }
49
50     /**
51      * Constructor
52      *
53      * @param fileName filename to read
54      * @throws IOException In case of an I/O problem
55      */

56     public FontFileReader(String JavaDoc fileName) throws IOException JavaDoc {
57         final File JavaDoc f = new File JavaDoc(fileName);
58         InputStream JavaDoc in = new java.io.FileInputStream JavaDoc(f);
59         try {
60             init(in);
61         } finally {
62             in.close();
63         }
64     }
65
66
67     /**
68      * Constructor
69      *
70      * @param in InputStream to read from
71      * @throws IOException In case of an I/O problem
72      */

73     public FontFileReader(InputStream JavaDoc in) throws IOException JavaDoc {
74         init(in);
75     }
76
77
78     /**
79      * Set current file position to offset
80      *
81      * @param offset The new offset to set
82      * @throws IOException In case of an I/O problem
83      */

84     public void seekSet(long offset) throws IOException JavaDoc {
85         if (offset >= fsize || offset < 0) {
86             throw new java.io.EOFException JavaDoc("Reached EOF, file size=" + fsize
87                                            + " offset=" + offset);
88         }
89         current = (int)offset;
90     }
91
92     /**
93      * Set current file position to offset
94      *
95      * @param add The number of bytes to advance
96      * @throws IOException In case of an I/O problem
97      */

98     public void seekAdd(long add) throws IOException JavaDoc {
99         seekSet(current + add);
100     }
101
102     /**
103      * Skip a given number of bytes.
104      *
105      * @param add The number of bytes to advance
106      * @throws IOException In case of an I/O problem
107      */

108     public void skip(long add) throws IOException JavaDoc {
109         seekAdd(add);
110     }
111
112     /**
113      * Returns current file position.
114      *
115      * @return int The current position.
116      */

117     public int getCurrentPos() {
118         return current;
119     }
120
121     /**
122      * Returns the size of the file.
123      *
124      * @return int The filesize
125      */

126     public int getFileSize() {
127         return fsize;
128     }
129
130     /**
131      * Read 1 byte.
132      *
133      * @return One byte
134      * @throws IOException If EOF is reached
135      */

136     public byte read() throws IOException JavaDoc {
137         if (current >= fsize) {
138             throw new java.io.EOFException JavaDoc("Reached EOF, file size=" + fsize);
139         }
140
141         final byte ret = file[current++];
142         return ret;
143     }
144
145     /**
146      * Read 1 signed byte.
147      *
148      * @return One byte
149      * @throws IOException If EOF is reached
150      */

151     public final byte readTTFByte() throws IOException JavaDoc {
152         return read();
153     }
154
155     /**
156      * Read 1 unsigned byte.
157      *
158      * @return One unsigned byte
159      * @throws IOException If EOF is reached
160      */

161     public final int readTTFUByte() throws IOException JavaDoc {
162         final byte buf = read();
163
164         if (buf < 0) {
165             return (int)(256 + buf);
166         } else {
167             return (int)buf;
168         }
169     }
170
171     /**
172      * Read 2 bytes signed.
173      *
174      * @return One signed short
175      * @throws IOException If EOF is reached
176      */

177     public final short readTTFShort() throws IOException JavaDoc {
178         final int ret = (readTTFUByte() << 8) + readTTFUByte();
179         final short sret = (short)ret;
180         return sret;
181     }
182
183     /**
184      * Read 2 bytes unsigned.
185      *
186      * @return One unsigned short
187      * @throws IOException If EOF is reached
188      */

189     public final int readTTFUShort() throws IOException JavaDoc {
190         final int ret = (readTTFUByte() << 8) + readTTFUByte();
191         return (int)ret;
192     }
193
194     /**
195      * Write a USHort at a given position.
196      *
197      * @param pos The absolute position to write to
198      * @param val The value to write
199      * @throws IOException If EOF is reached
200      */

201     public final void writeTTFUShort(int pos, int val) throws IOException JavaDoc {
202         if ((pos + 2) > fsize) {
203             throw new java.io.EOFException JavaDoc("Reached EOF");
204         }
205         final byte b1 = (byte)((val >> 8) & 0xff);
206         final byte b2 = (byte)(val & 0xff);
207         file[pos] = b1;
208         file[pos + 1] = b2;
209     }
210
211     /**
212      * Read 2 bytes signed at position pos without changing current position.
213      *
214      * @param pos The absolute position to read from
215      * @return One signed short
216      * @throws IOException If EOF is reached
217      */

218     public final short readTTFShort(long pos) throws IOException JavaDoc {
219         final long cp = getCurrentPos();
220         seekSet(pos);
221         final short ret = readTTFShort();
222         seekSet(cp);
223         return ret;
224     }
225
226     /**
227      * Read 2 bytes unsigned at position pos without changing current position.
228      *
229      * @param pos The absolute position to read from
230      * @return One unsigned short
231      * @throws IOException If EOF is reached
232      */

233     public final int readTTFUShort(long pos) throws IOException JavaDoc {
234         long cp = getCurrentPos();
235         seekSet(pos);
236         int ret = readTTFUShort();
237         seekSet(cp);
238         return ret;
239     }
240
241     /**
242      * Read 4 bytes.
243      *
244      * @return One signed integer
245      * @throws IOException If EOF is reached
246      */

247     public final int readTTFLong() throws IOException JavaDoc {
248         long ret = readTTFUByte(); // << 8;
249
ret = (ret << 8) + readTTFUByte();
250         ret = (ret << 8) + readTTFUByte();
251         ret = (ret << 8) + readTTFUByte();
252
253         return (int)ret;
254     }
255
256     /**
257      * Read 4 bytes.
258      *
259      * @return One unsigned integer
260      * @throws IOException If EOF is reached
261      */

262     public final long readTTFULong() throws IOException JavaDoc {
263         long ret = readTTFUByte();
264         ret = (ret << 8) + readTTFUByte();
265         ret = (ret << 8) + readTTFUByte();
266         ret = (ret << 8) + readTTFUByte();
267
268         return ret;
269     }
270
271     /**
272      * Read a NUL terminated ISO-8859-1 string.
273      *
274      * @return A String
275      * @throws IOException If EOF is reached
276      */

277     public final String JavaDoc readTTFString() throws IOException JavaDoc {
278         int i = current;
279         while (file[i++] != 0) {
280             if (i > fsize) {
281                 throw new java.io.EOFException JavaDoc("Reached EOF, file size="
282                                                + fsize);
283             }
284         }
285
286         byte[] tmp = new byte[i - current];
287         System.arraycopy(file, current, tmp, 0, i - current);
288         return new String JavaDoc(tmp, "ISO-8859-1");
289     }
290
291
292     /**
293      * Read an ISO-8859-1 string of len bytes.
294      *
295      * @param len The length of the string to read
296      * @return A String
297      * @throws IOException If EOF is reached
298      */

299     public final String JavaDoc readTTFString(int len) throws IOException JavaDoc {
300         if ((len + current) > fsize) {
301             throw new java.io.EOFException JavaDoc("Reached EOF, file size=" + fsize);
302         }
303
304         byte[] tmp = new byte[len];
305         System.arraycopy(file, current, tmp, 0, len);
306         current += len;
307         final String JavaDoc encoding;
308         if ((tmp.length > 0) && (tmp[0] == 0)) {
309             encoding = "UTF-16BE";
310         } else {
311             encoding = "ISO-8859-1";
312         }
313         return new String JavaDoc(tmp, encoding);
314     }
315
316     /**
317      * Return a copy of the internal array
318      *
319      * @param offset The absolute offset to start reading from
320      * @param length The number of bytes to read
321      * @return An array of bytes
322      * @throws IOException if out of bounds
323      */

324     public byte[] getBytes(int offset,
325                            int length) throws IOException JavaDoc {
326         if ((offset + length) > fsize) {
327             throw new java.io.IOException JavaDoc("Reached EOF");
328         }
329
330         byte[] ret = new byte[length];
331         System.arraycopy(file, offset, ret, 0, length);
332         return ret;
333     }
334
335
336 }
Popular Tags