You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FontFileReader.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. /* $Id$ */
  18. package org.apache.fop.fonts.truetype;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import org.apache.commons.io.IOUtils;
  22. /**
  23. * Reads a TrueType font file into a byte array and
  24. * provides file like functions for array access.
  25. */
  26. public class FontFileReader {
  27. private final int fsize; // file size
  28. private int current; // current position in file
  29. private final byte[] file;
  30. /**
  31. * Constructor
  32. *
  33. * @param in InputStream to read from
  34. * @throws IOException In case of an I/O problem
  35. */
  36. public FontFileReader(InputStream in) throws IOException {
  37. this.file = IOUtils.toByteArray(in);
  38. this.fsize = this.file.length;
  39. this.current = 0;
  40. }
  41. /**
  42. * Set current file position to offset
  43. *
  44. * @param offset The new offset to set
  45. * @throws IOException In case of an I/O problem
  46. */
  47. public void seekSet(long offset) throws IOException {
  48. if (offset > fsize || offset < 0) {
  49. throw new java.io.EOFException("Reached EOF, file size=" + fsize
  50. + " offset=" + offset);
  51. }
  52. current = (int)offset;
  53. }
  54. /**
  55. * Skip a given number of bytes.
  56. *
  57. * @param add The number of bytes to advance
  58. * @throws IOException In case of an I/O problem
  59. */
  60. public void skip(long add) throws IOException {
  61. seekSet(current + add);
  62. }
  63. /**
  64. * Returns current file position.
  65. *
  66. * @return int The current position.
  67. */
  68. public int getCurrentPos() {
  69. return current;
  70. }
  71. /**
  72. * Returns the size of the file.
  73. *
  74. * @return int The filesize
  75. */
  76. public int getFileSize() {
  77. return fsize;
  78. }
  79. /**
  80. * Read 1 byte.
  81. *
  82. * @return One byte
  83. * @throws IOException If EOF is reached
  84. */
  85. private byte read() throws IOException {
  86. if (current >= fsize) {
  87. throw new java.io.EOFException("Reached EOF, file size=" + fsize);
  88. }
  89. final byte ret = file[current++];
  90. return ret;
  91. }
  92. /**
  93. * Read 1 signed byte.
  94. *
  95. * @return One byte
  96. * @throws IOException If EOF is reached
  97. */
  98. public final byte readTTFByte() throws IOException {
  99. return read();
  100. }
  101. /**
  102. * Read 1 unsigned byte.
  103. *
  104. * @return One unsigned byte
  105. * @throws IOException If EOF is reached
  106. */
  107. public final int readTTFUByte() throws IOException {
  108. final byte buf = read();
  109. if (buf < 0) {
  110. return (256 + buf);
  111. } else {
  112. return buf;
  113. }
  114. }
  115. /**
  116. * Read 2 bytes signed.
  117. *
  118. * @return One signed short
  119. * @throws IOException If EOF is reached
  120. */
  121. public final short readTTFShort() throws IOException {
  122. final int ret = (readTTFUByte() << 8) + readTTFUByte();
  123. final short sret = (short)ret;
  124. return sret;
  125. }
  126. /**
  127. * Read 2 bytes unsigned.
  128. *
  129. * @return One unsigned short
  130. * @throws IOException If EOF is reached
  131. */
  132. public final int readTTFUShort() throws IOException {
  133. final int ret = (readTTFUByte() << 8) + readTTFUByte();
  134. return ret;
  135. }
  136. /**
  137. * Write a USHort at a given position.
  138. *
  139. * @param pos The absolute position to write to
  140. * @param val The value to write
  141. * @throws IOException If EOF is reached
  142. */
  143. public final void writeTTFUShort(long pos, int val) throws IOException {
  144. if ((pos + 2) > fsize) {
  145. throw new java.io.EOFException("Reached EOF");
  146. }
  147. final byte b1 = (byte)((val >> 8) & 0xff);
  148. final byte b2 = (byte)(val & 0xff);
  149. final int fileIndex = (int) pos;
  150. file[fileIndex] = b1;
  151. file[fileIndex + 1] = b2;
  152. }
  153. /**
  154. * Read 2 bytes signed at position pos without changing current position.
  155. *
  156. * @param pos The absolute position to read from
  157. * @return One signed short
  158. * @throws IOException If EOF is reached
  159. */
  160. public final short readTTFShort(long pos) throws IOException {
  161. final long cp = getCurrentPos();
  162. seekSet(pos);
  163. final short ret = readTTFShort();
  164. seekSet(cp);
  165. return ret;
  166. }
  167. /**
  168. * Read 2 bytes unsigned at position pos without changing current position.
  169. *
  170. * @param pos The absolute position to read from
  171. * @return One unsigned short
  172. * @throws IOException If EOF is reached
  173. */
  174. public final int readTTFUShort(long pos) throws IOException {
  175. long cp = getCurrentPos();
  176. seekSet(pos);
  177. int ret = readTTFUShort();
  178. seekSet(cp);
  179. return ret;
  180. }
  181. /**
  182. * Read 4 bytes.
  183. *
  184. * @return One signed integer
  185. * @throws IOException If EOF is reached
  186. */
  187. public final int readTTFLong() throws IOException {
  188. long ret = readTTFUByte(); // << 8;
  189. ret = (ret << 8) + readTTFUByte();
  190. ret = (ret << 8) + readTTFUByte();
  191. ret = (ret << 8) + readTTFUByte();
  192. return (int)ret;
  193. }
  194. /**
  195. * Read 4 bytes.
  196. *
  197. * @return One unsigned integer
  198. * @throws IOException If EOF is reached
  199. */
  200. public final long readTTFULong() throws IOException {
  201. long ret = readTTFUByte();
  202. ret = (ret << 8) + readTTFUByte();
  203. ret = (ret << 8) + readTTFUByte();
  204. ret = (ret << 8) + readTTFUByte();
  205. return ret;
  206. }
  207. /**
  208. * Read a NUL terminated ISO-8859-1 string.
  209. *
  210. * @return A String
  211. * @throws IOException If EOF is reached
  212. */
  213. public final String readTTFString() throws IOException {
  214. int i = current;
  215. while (file[i++] != 0) {
  216. if (i >= fsize) {
  217. throw new java.io.EOFException("Reached EOF, file size="
  218. + fsize);
  219. }
  220. }
  221. byte[] tmp = new byte[i - current - 1];
  222. System.arraycopy(file, current, tmp, 0, i - current - 1);
  223. return new String(tmp, "ISO-8859-1");
  224. }
  225. /**
  226. * Read an ISO-8859-1 string of len bytes.
  227. *
  228. * @param len The length of the string to read
  229. * @return A String
  230. * @throws IOException If EOF is reached
  231. */
  232. public final String readTTFString(int len) throws IOException {
  233. if ((len + current) > fsize) {
  234. throw new java.io.EOFException("Reached EOF, file size=" + fsize);
  235. }
  236. byte[] tmp = new byte[len];
  237. System.arraycopy(file, current, tmp, 0, len);
  238. current += len;
  239. final String encoding;
  240. if ((tmp.length > 0) && (tmp[0] == 0)) {
  241. encoding = "UTF-16BE";
  242. } else {
  243. encoding = "ISO-8859-1";
  244. }
  245. return new String(tmp, encoding);
  246. }
  247. /**
  248. * Read an ISO-8859-1 string of len bytes.
  249. *
  250. * @param len The length of the string to read
  251. * @param encodingID the string encoding id (presently ignored; always uses UTF-16BE)
  252. * @return A String
  253. * @throws IOException If EOF is reached
  254. */
  255. public final String readTTFString(int len, int encodingID) throws IOException {
  256. if ((len + current) > fsize) {
  257. throw new java.io.EOFException("Reached EOF, file size=" + fsize);
  258. }
  259. byte[] tmp = new byte[len];
  260. System.arraycopy(file, current, tmp, 0, len);
  261. current += len;
  262. final String encoding;
  263. encoding = "UTF-16BE"; //Use this for all known encoding IDs for now
  264. return new String(tmp, encoding);
  265. }
  266. /**
  267. * Return a copy of the internal array
  268. *
  269. * @param offset The absolute offset to start reading from
  270. * @param length The number of bytes to read
  271. * @return An array of bytes
  272. * @throws IOException if out of bounds
  273. */
  274. public byte[] getBytes(int offset,
  275. int length) throws IOException {
  276. if ((offset + length) > fsize) {
  277. throw new java.io.IOException("Reached EOF");
  278. }
  279. byte[] ret = new byte[length];
  280. System.arraycopy(file, offset, ret, 0, length);
  281. return ret;
  282. }
  283. /**
  284. * Returns the full byte array representation of the file.
  285. * @return byte array.
  286. */
  287. public byte[] getAllBytes() {
  288. return file;
  289. }
  290. }