123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /* $Id$ */
-
- package org.apache.fop.fonts.truetype;
-
- import java.io.IOException;
- import java.io.InputStream;
-
- import org.apache.commons.io.IOUtils;
-
- /**
- * Reads a TrueType font file into a byte array and
- * provides file like functions for array access.
- */
- public class FontFileReader {
-
- private final int fsize; // file size
- private int current; // current position in file
- private final byte[] file;
-
- /**
- * Constructor
- *
- * @param in InputStream to read from
- * @throws IOException In case of an I/O problem
- */
- public FontFileReader(InputStream in) throws IOException {
- this.file = IOUtils.toByteArray(in);
- this.fsize = this.file.length;
- this.current = 0;
- }
-
-
- /**
- * Set current file position to offset
- *
- * @param offset The new offset to set
- * @throws IOException In case of an I/O problem
- */
- public void seekSet(long offset) throws IOException {
- if (offset > fsize || offset < 0) {
- throw new java.io.EOFException("Reached EOF, file size=" + fsize
- + " offset=" + offset);
- }
- current = (int)offset;
- }
-
- /**
- * Skip a given number of bytes.
- *
- * @param add The number of bytes to advance
- * @throws IOException In case of an I/O problem
- */
- public void skip(long add) throws IOException {
- seekSet(current + add);
- }
-
- /**
- * Returns current file position.
- *
- * @return int The current position.
- */
- public int getCurrentPos() {
- return current;
- }
-
- /**
- * Returns the size of the file.
- *
- * @return int The filesize
- */
- public int getFileSize() {
- return fsize;
- }
-
- /**
- * Read 1 byte.
- *
- * @return One byte
- * @throws IOException If EOF is reached
- */
- private byte read() throws IOException {
- if (current >= fsize) {
- throw new java.io.EOFException("Reached EOF, file size=" + fsize);
- }
-
- final byte ret = file[current++];
- return ret;
- }
-
- /**
- * Read 1 signed byte.
- *
- * @return One byte
- * @throws IOException If EOF is reached
- */
- public final byte readTTFByte() throws IOException {
- return read();
- }
-
- /**
- * Read 1 unsigned byte.
- *
- * @return One unsigned byte
- * @throws IOException If EOF is reached
- */
- public final int readTTFUByte() throws IOException {
- final byte buf = read();
-
- if (buf < 0) {
- return (256 + buf);
- } else {
- return buf;
- }
- }
-
- /**
- * Read 2 bytes signed.
- *
- * @return One signed short
- * @throws IOException If EOF is reached
- */
- public final short readTTFShort() throws IOException {
- final int ret = (readTTFUByte() << 8) + readTTFUByte();
- final short sret = (short)ret;
- return sret;
- }
-
- /**
- * Read 2 bytes unsigned.
- *
- * @return One unsigned short
- * @throws IOException If EOF is reached
- */
- public final int readTTFUShort() throws IOException {
- final int ret = (readTTFUByte() << 8) + readTTFUByte();
- return ret;
- }
-
- /**
- * Write a USHort at a given position.
- *
- * @param pos The absolute position to write to
- * @param val The value to write
- * @throws IOException If EOF is reached
- */
- public final void writeTTFUShort(long pos, int val) throws IOException {
- if ((pos + 2) > fsize) {
- throw new java.io.EOFException("Reached EOF");
- }
- final byte b1 = (byte)((val >> 8) & 0xff);
- final byte b2 = (byte)(val & 0xff);
- final int fileIndex = (int) pos;
- file[fileIndex] = b1;
- file[fileIndex + 1] = b2;
- }
-
- /**
- * Read 2 bytes signed at position pos without changing current position.
- *
- * @param pos The absolute position to read from
- * @return One signed short
- * @throws IOException If EOF is reached
- */
- public final short readTTFShort(long pos) throws IOException {
- final long cp = getCurrentPos();
- seekSet(pos);
- final short ret = readTTFShort();
- seekSet(cp);
- return ret;
- }
-
- /**
- * Read 2 bytes unsigned at position pos without changing current position.
- *
- * @param pos The absolute position to read from
- * @return One unsigned short
- * @throws IOException If EOF is reached
- */
- public final int readTTFUShort(long pos) throws IOException {
- long cp = getCurrentPos();
- seekSet(pos);
- int ret = readTTFUShort();
- seekSet(cp);
- return ret;
- }
-
- /**
- * Read 4 bytes.
- *
- * @return One signed integer
- * @throws IOException If EOF is reached
- */
- public final int readTTFLong() throws IOException {
- long ret = readTTFUByte(); // << 8;
- ret = (ret << 8) + readTTFUByte();
- ret = (ret << 8) + readTTFUByte();
- ret = (ret << 8) + readTTFUByte();
-
- return (int)ret;
- }
-
- /**
- * Read 4 bytes.
- *
- * @return One unsigned integer
- * @throws IOException If EOF is reached
- */
- public final long readTTFULong() throws IOException {
- long ret = readTTFUByte();
- ret = (ret << 8) + readTTFUByte();
- ret = (ret << 8) + readTTFUByte();
- ret = (ret << 8) + readTTFUByte();
-
- return ret;
- }
-
- /**
- * Read a NUL terminated ISO-8859-1 string.
- *
- * @return A String
- * @throws IOException If EOF is reached
- */
- public final String readTTFString() throws IOException {
- int i = current;
- while (file[i++] != 0) {
- if (i >= fsize) {
- throw new java.io.EOFException("Reached EOF, file size="
- + fsize);
- }
- }
-
- byte[] tmp = new byte[i - current - 1];
- System.arraycopy(file, current, tmp, 0, i - current - 1);
- return new String(tmp, "ISO-8859-1");
- }
-
-
- /**
- * Read an ISO-8859-1 string of len bytes.
- *
- * @param len The length of the string to read
- * @return A String
- * @throws IOException If EOF is reached
- */
- public final String readTTFString(int len) throws IOException {
- if ((len + current) > fsize) {
- throw new java.io.EOFException("Reached EOF, file size=" + fsize);
- }
-
- byte[] tmp = new byte[len];
- System.arraycopy(file, current, tmp, 0, len);
- current += len;
- final String encoding;
- if ((tmp.length > 0) && (tmp[0] == 0)) {
- encoding = "UTF-16BE";
- } else {
- encoding = "ISO-8859-1";
- }
- return new String(tmp, encoding);
- }
-
- /**
- * Read an ISO-8859-1 string of len bytes.
- *
- * @param len The length of the string to read
- * @param encodingID the string encoding id (presently ignored; always uses UTF-16BE)
- * @return A String
- * @throws IOException If EOF is reached
- */
- public final String readTTFString(int len, int encodingID) throws IOException {
- if ((len + current) > fsize) {
- throw new java.io.EOFException("Reached EOF, file size=" + fsize);
- }
-
- byte[] tmp = new byte[len];
- System.arraycopy(file, current, tmp, 0, len);
- current += len;
- final String encoding;
- encoding = "UTF-16BE"; //Use this for all known encoding IDs for now
- return new String(tmp, encoding);
- }
-
- /**
- * Return a copy of the internal array
- *
- * @param offset The absolute offset to start reading from
- * @param length The number of bytes to read
- * @return An array of bytes
- * @throws IOException if out of bounds
- */
- public byte[] getBytes(int offset,
- int length) throws IOException {
- if ((offset + length) > fsize) {
- throw new java.io.IOException("Reached EOF");
- }
-
- byte[] ret = new byte[length];
- System.arraycopy(file, offset, ret, 0, length);
- return ret;
- }
- /**
- * Returns the full byte array representation of the file.
- * @return byte array.
- */
- public byte[] getAllBytes() {
- return file;
- }
- }
|