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 9.6KB

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