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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. * Set current file position to offset
  81. *
  82. * @param add The number of bytes to advance
  83. * @throws IOException In case of an I/O problem
  84. */
  85. public void seekAdd(long add) throws IOException {
  86. seekSet(current + add);
  87. }
  88. /**
  89. * Skip a given number of bytes.
  90. *
  91. * @param add The number of bytes to advance
  92. * @throws IOException In case of an I/O problem
  93. */
  94. public void skip(long add) throws IOException {
  95. seekAdd(add);
  96. }
  97. /**
  98. * Returns current file position.
  99. *
  100. * @return int The current position.
  101. */
  102. public int getCurrentPos() {
  103. return current;
  104. }
  105. /**
  106. * Returns the size of the file.
  107. *
  108. * @return int The filesize
  109. */
  110. public int getFileSize() {
  111. return fsize;
  112. }
  113. /**
  114. * Read 1 byte.
  115. *
  116. * @return One byte
  117. * @throws IOException If EOF is reached
  118. */
  119. public byte read() throws IOException {
  120. if (current >= fsize) {
  121. throw new java.io.EOFException("Reached EOF, file size=" + fsize);
  122. }
  123. final byte ret = file[current++];
  124. return ret;
  125. }
  126. /**
  127. * Read 1 signed byte.
  128. *
  129. * @return One byte
  130. * @throws IOException If EOF is reached
  131. */
  132. public final byte readTTFByte() throws IOException {
  133. return read();
  134. }
  135. /**
  136. * Read 1 unsigned byte.
  137. *
  138. * @return One unsigned byte
  139. * @throws IOException If EOF is reached
  140. */
  141. public final int readTTFUByte() throws IOException {
  142. final byte buf = read();
  143. if (buf < 0) {
  144. return (int)(256 + buf);
  145. } else {
  146. return (int)buf;
  147. }
  148. }
  149. /**
  150. * Read 2 bytes signed.
  151. *
  152. * @return One signed short
  153. * @throws IOException If EOF is reached
  154. */
  155. public final short readTTFShort() throws IOException {
  156. final int ret = (readTTFUByte() << 8) + readTTFUByte();
  157. final short sret = (short)ret;
  158. return sret;
  159. }
  160. /**
  161. * Read 2 bytes unsigned.
  162. *
  163. * @return One unsigned short
  164. * @throws IOException If EOF is reached
  165. */
  166. public final int readTTFUShort() throws IOException {
  167. final int ret = (readTTFUByte() << 8) + readTTFUByte();
  168. return (int)ret;
  169. }
  170. /**
  171. * Write a USHort at a given position.
  172. *
  173. * @param pos The absolute position to write to
  174. * @param val The value to write
  175. * @throws IOException If EOF is reached
  176. */
  177. public final void writeTTFUShort(long pos, int val) throws IOException {
  178. if ((pos + 2) > fsize) {
  179. throw new java.io.EOFException("Reached EOF");
  180. }
  181. final byte b1 = (byte)((val >> 8) & 0xff);
  182. final byte b2 = (byte)(val & 0xff);
  183. final int fileIndex = (int) pos;
  184. file[fileIndex] = b1;
  185. file[fileIndex + 1] = b2;
  186. }
  187. /**
  188. * Read 2 bytes signed at position pos without changing current position.
  189. *
  190. * @param pos The absolute position to read from
  191. * @return One signed short
  192. * @throws IOException If EOF is reached
  193. */
  194. public final short readTTFShort(long pos) throws IOException {
  195. final long cp = getCurrentPos();
  196. seekSet(pos);
  197. final short ret = readTTFShort();
  198. seekSet(cp);
  199. return ret;
  200. }
  201. /**
  202. * Read 2 bytes unsigned at position pos without changing current position.
  203. *
  204. * @param pos The absolute position to read from
  205. * @return One unsigned short
  206. * @throws IOException If EOF is reached
  207. */
  208. public final int readTTFUShort(long pos) throws IOException {
  209. long cp = getCurrentPos();
  210. seekSet(pos);
  211. int ret = readTTFUShort();
  212. seekSet(cp);
  213. return ret;
  214. }
  215. /**
  216. * Read 4 bytes.
  217. *
  218. * @return One signed integer
  219. * @throws IOException If EOF is reached
  220. */
  221. public final int readTTFLong() throws IOException {
  222. long ret = readTTFUByte(); // << 8;
  223. ret = (ret << 8) + readTTFUByte();
  224. ret = (ret << 8) + readTTFUByte();
  225. ret = (ret << 8) + readTTFUByte();
  226. return (int)ret;
  227. }
  228. /**
  229. * Read 4 bytes.
  230. *
  231. * @return One unsigned integer
  232. * @throws IOException If EOF is reached
  233. */
  234. public final long readTTFULong() throws IOException {
  235. long ret = readTTFUByte();
  236. ret = (ret << 8) + readTTFUByte();
  237. ret = (ret << 8) + readTTFUByte();
  238. ret = (ret << 8) + readTTFUByte();
  239. return ret;
  240. }
  241. /**
  242. * Read a NUL terminated ISO-8859-1 string.
  243. *
  244. * @return A String
  245. * @throws IOException If EOF is reached
  246. */
  247. public final String readTTFString() throws IOException {
  248. int i = current;
  249. while (file[i++] != 0) {
  250. if (i > fsize) {
  251. throw new java.io.EOFException("Reached EOF, file size="
  252. + fsize);
  253. }
  254. }
  255. byte[] tmp = new byte[i - current];
  256. System.arraycopy(file, current, tmp, 0, i - current);
  257. return new String(tmp, "ISO-8859-1");
  258. }
  259. /**
  260. * Read an ISO-8859-1 string of len bytes.
  261. *
  262. * @param len The length of the string to read
  263. * @return A String
  264. * @throws IOException If EOF is reached
  265. */
  266. public final String readTTFString(int len) throws IOException {
  267. if ((len + current) > fsize) {
  268. throw new java.io.EOFException("Reached EOF, file size=" + fsize);
  269. }
  270. byte[] tmp = new byte[len];
  271. System.arraycopy(file, current, tmp, 0, len);
  272. current += len;
  273. final String encoding;
  274. if ((tmp.length > 0) && (tmp[0] == 0)) {
  275. encoding = "UTF-16BE";
  276. } else {
  277. encoding = "ISO-8859-1";
  278. }
  279. return new String(tmp, encoding);
  280. }
  281. /**
  282. * Read an ISO-8859-1 string of len bytes.
  283. *
  284. * @param len The length of the string to read
  285. * @param encodingID the string encoding id (presently ignored; always uses UTF-16BE)
  286. * @return A String
  287. * @throws IOException If EOF is reached
  288. */
  289. public final String readTTFString(int len, int encodingID) throws IOException {
  290. if ((len + current) > fsize) {
  291. throw new java.io.EOFException("Reached EOF, file size=" + fsize);
  292. }
  293. byte[] tmp = new byte[len];
  294. System.arraycopy(file, current, tmp, 0, len);
  295. current += len;
  296. final String encoding;
  297. encoding = "UTF-16BE"; //Use this for all known encoding IDs for now
  298. return new String(tmp, encoding);
  299. }
  300. /**
  301. * Return a copy of the internal array
  302. *
  303. * @param offset The absolute offset to start reading from
  304. * @param length The number of bytes to read
  305. * @return An array of bytes
  306. * @throws IOException if out of bounds
  307. */
  308. public byte[] getBytes(int offset,
  309. int length) throws IOException {
  310. if ((offset + length) > fsize) {
  311. throw new java.io.IOException("Reached EOF");
  312. }
  313. byte[] ret = new byte[length];
  314. System.arraycopy(file, offset, ret, 0, length);
  315. return ret;
  316. }
  317. }