1 17 18 19 20 package org.apache.fop.fonts.truetype; 21 22 import java.io.InputStream ; 23 import java.io.File ; 24 import java.io.IOException ; 25 26 import org.apache.commons.io.IOUtils; 27 28 32 public class FontFileReader { 33 34 private int fsize; private int current; private byte[] file; 37 38 44 private void init(InputStream in) throws java.io.IOException { 45 this.file = IOUtils.toByteArray(in); 46 this.fsize = this.file.length; 47 this.current = 0; 48 } 49 50 56 public FontFileReader(String fileName) throws IOException { 57 final File f = new File (fileName); 58 InputStream in = new java.io.FileInputStream (f); 59 try { 60 init(in); 61 } finally { 62 in.close(); 63 } 64 } 65 66 67 73 public FontFileReader(InputStream in) throws IOException { 74 init(in); 75 } 76 77 78 84 public void seekSet(long offset) throws IOException { 85 if (offset >= fsize || offset < 0) { 86 throw new java.io.EOFException ("Reached EOF, file size=" + fsize 87 + " offset=" + offset); 88 } 89 current = (int)offset; 90 } 91 92 98 public void seekAdd(long add) throws IOException { 99 seekSet(current + add); 100 } 101 102 108 public void skip(long add) throws IOException { 109 seekAdd(add); 110 } 111 112 117 public int getCurrentPos() { 118 return current; 119 } 120 121 126 public int getFileSize() { 127 return fsize; 128 } 129 130 136 public byte read() throws IOException { 137 if (current >= fsize) { 138 throw new java.io.EOFException ("Reached EOF, file size=" + fsize); 139 } 140 141 final byte ret = file[current++]; 142 return ret; 143 } 144 145 151 public final byte readTTFByte() throws IOException { 152 return read(); 153 } 154 155 161 public final int readTTFUByte() throws IOException { 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 177 public final short readTTFShort() throws IOException { 178 final int ret = (readTTFUByte() << 8) + readTTFUByte(); 179 final short sret = (short)ret; 180 return sret; 181 } 182 183 189 public final int readTTFUShort() throws IOException { 190 final int ret = (readTTFUByte() << 8) + readTTFUByte(); 191 return (int)ret; 192 } 193 194 201 public final void writeTTFUShort(int pos, int val) throws IOException { 202 if ((pos + 2) > fsize) { 203 throw new java.io.EOFException ("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 218 public final short readTTFShort(long pos) throws IOException { 219 final long cp = getCurrentPos(); 220 seekSet(pos); 221 final short ret = readTTFShort(); 222 seekSet(cp); 223 return ret; 224 } 225 226 233 public final int readTTFUShort(long pos) throws IOException { 234 long cp = getCurrentPos(); 235 seekSet(pos); 236 int ret = readTTFUShort(); 237 seekSet(cp); 238 return ret; 239 } 240 241 247 public final int readTTFLong() throws IOException { 248 long ret = readTTFUByte(); ret = (ret << 8) + readTTFUByte(); 250 ret = (ret << 8) + readTTFUByte(); 251 ret = (ret << 8) + readTTFUByte(); 252 253 return (int)ret; 254 } 255 256 262 public final long readTTFULong() throws IOException { 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 277 public final String readTTFString() throws IOException { 278 int i = current; 279 while (file[i++] != 0) { 280 if (i > fsize) { 281 throw new java.io.EOFException ("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 (tmp, "ISO-8859-1"); 289 } 290 291 292 299 public final String readTTFString(int len) throws IOException { 300 if ((len + current) > fsize) { 301 throw new java.io.EOFException ("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 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 (tmp, encoding); 314 } 315 316 324 public byte[] getBytes(int offset, 325 int length) throws IOException { 326 if ((offset + length) > fsize) { 327 throw new java.io.IOException ("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 |