1 51 package org.apache.fop.fonts; 52 53 import java.io.InputStream ; 54 import java.io.File ; 55 import java.io.IOException ; 56 57 import org.apache.fop.tools.IOUtil; 58 59 63 public class FontFileReader { 64 private int fsize; private int current; private byte[] file; 67 68 73 private void init(InputStream in) throws java.io.IOException { 74 java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream (); 75 try { 76 this.file = IOUtil.toByteArray(in, 50000); 77 this.fsize = this.file.length; 78 this.current = 0; 79 } finally { 80 bout.close(); 81 } 82 } 83 84 85 89 public FontFileReader(String fileName) throws java.io.IOException { 90 File f = new File (fileName); 91 InputStream in = new java.io.FileInputStream (f); 92 try { 93 init(in); 94 } finally { 95 in.close(); 96 } 97 } 98 99 100 104 public FontFileReader(InputStream in) throws java.io.IOException { 105 init(in); 106 } 107 108 109 112 public void seek_set(long offset) throws IOException { 113 if (offset > fsize || offset < 0) 114 throw new java.io.EOFException ("Reached EOF, file size=" + fsize 115 + " offset=" + offset); 116 current = (int)offset; 117 } 118 119 122 public void seek_add(long add) throws IOException { 123 seek_set(current + add); 124 } 125 126 public void skip(long add) throws IOException { 127 seek_add(add); 128 } 129 130 133 public int getCurrentPos() { 134 return current; 135 } 136 137 public int getFileSize() { 138 return fsize; 139 } 140 141 142 145 public byte read() throws IOException { 146 if (current > fsize) 147 throw new java.io.EOFException ("Reached EOF, file size=" + fsize); 148 149 byte ret = file[current++]; 150 return ret; 151 } 152 153 154 155 158 public final byte readTTFByte() throws IOException { 159 return read(); 160 } 161 162 165 public final int readTTFUByte() throws IOException { 166 byte buf = read(); 167 168 if (buf < 0) 169 return (int)(256 + buf); 170 else 171 return (int)buf; 172 } 173 174 177 public final short readTTFShort() throws IOException { 178 int ret = (readTTFUByte() << 8) + readTTFUByte(); 179 short sret = (short)ret; 180 181 return sret; 182 } 183 184 187 public final int readTTFUShort() throws IOException { 188 int ret = (readTTFUByte() << 8) + readTTFUByte(); 189 190 return (int)ret; 191 } 192 193 196 public final void writeTTFUShort(int pos, int val) throws IOException { 197 if ((pos + 2) > fsize) 198 throw new java.io.EOFException ("Reached EOF"); 199 byte b1 = (byte)((val >> 8) & 0xff); 200 byte b2 = (byte)(val & 0xff); 201 file[pos] = b1; 202 file[pos + 1] = b2; 203 } 204 205 209 public final short readTTFShort(long pos) throws IOException { 210 long cp = getCurrentPos(); 211 seek_set(pos); 212 short ret = readTTFShort(); 213 seek_set(cp); 214 return ret; 215 } 216 217 221 public final int readTTFUShort(long pos) throws IOException { 222 long cp = getCurrentPos(); 223 seek_set(pos); 224 int ret = readTTFUShort(); 225 seek_set(cp); 226 return ret; 227 } 228 229 232 public final int readTTFLong() throws IOException { 233 long ret = readTTFUByte(); ret = (ret << 8) + readTTFUByte(); 235 ret = (ret << 8) + readTTFUByte(); 236 ret = (ret << 8) + readTTFUByte(); 237 238 return (int)ret; 239 } 240 241 244 public final long readTTFULong() throws IOException { 245 long ret = readTTFUByte(); 246 ret = (ret << 8) + readTTFUByte(); 247 ret = (ret << 8) + readTTFUByte(); 248 ret = (ret << 8) + readTTFUByte(); 249 250 return ret; 251 } 252 253 256 public final String readTTFString() throws IOException { 257 int i = current; 258 while (file[i++] != 0) { 259 if (i > fsize) 260 throw new java.io.EOFException ("Reached EOF, file size=" 261 + fsize); 262 } 263 264 byte[] tmp = new byte[i - current]; 265 System.arraycopy(file, current, tmp, 0, i - current); 266 return new String (tmp, "ISO-8859-1"); 267 } 268 269 270 273 public final String readTTFString(int len) throws IOException { 274 if ((len + current) > fsize) 275 throw new java.io.EOFException ("Reached EOF, file size=" + fsize); 276 277 byte[] tmp = new byte[len]; 278 System.arraycopy(file, current, tmp, 0, len); 279 current += len; 280 final String encoding; 281 if ((tmp.length > 0) && (tmp[0] == 0)) { 282 encoding = "UnicodeBig"; 283 } else { 284 encoding = "ISO-8859-1"; 285 } 286 return new String (tmp, encoding); 287 } 288 289 293 public byte[] getBytes(int offset, 294 int length) throws java.io.IOException { 295 if ((offset + length) > fsize) 296 throw new java.io.IOException ("Reached EOF"); 297 298 byte[] ret = new byte[length]; 299 System.arraycopy(file, offset, ret, 0, length); 300 return ret; 301 } 302 303 304 } 305 | Popular Tags |