FTStream.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #region MIT License
  2. /*Copyright (c) 2012 Robert Rouhani <robert.rouhani@gmail.com>
  3. SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.*/
  19. #endregion
  20. using System;
  21. using System.Runtime.InteropServices;
  22. using SharpFont.Internal;
  23. namespace SharpFont
  24. {
  25. /// <summary>
  26. /// A function used to seek and read data from a given input stream.
  27. /// </summary>
  28. /// <remarks>
  29. /// This function might be called to perform a seek or skip operation with a ‘count’ of 0. A non-zero return value
  30. /// then indicates an error.
  31. /// </remarks>
  32. /// <param name="stream">A handle to the source stream.</param>
  33. /// <param name="offset">The offset of read in stream (always from start).</param>
  34. /// <param name="buffer">The address of the read buffer.</param>
  35. /// <param name="count">The number of bytes to read from the stream.</param>
  36. /// <returns>The number of bytes effectively read by the stream.</returns>
  37. [CLSCompliant(false)]
  38. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  39. public delegate uint StreamIOFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StreamMarshaler))] FTStream stream, uint offset, IntPtr buffer, uint count);
  40. /// <summary>
  41. /// A function used to close a given input stream.
  42. /// </summary>
  43. /// <param name="stream">A handle to the target stream.</param>
  44. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  45. public delegate void StreamCloseFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StreamMarshaler))] FTStream stream);
  46. /// <summary>
  47. /// A handle to an input stream.
  48. /// </summary>
  49. public sealed class FTStream
  50. {
  51. #region Fields
  52. private IntPtr reference;
  53. private StreamRec rec;
  54. #endregion
  55. #region Constructors
  56. internal FTStream(IntPtr reference)
  57. {
  58. Reference = reference;
  59. }
  60. #endregion
  61. #region Properties
  62. /// <summary>
  63. /// Gets base. For memory-based streams, this is the address of the first stream byte in memory. This field
  64. /// should always be set to NULL for disk-based streams.
  65. /// </summary>
  66. public IntPtr Base
  67. {
  68. get
  69. {
  70. return rec.@base;
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the stream size in bytes.
  75. /// </summary>
  76. [CLSCompliant(false)]
  77. public uint Size
  78. {
  79. get
  80. {
  81. return (uint)rec.size;
  82. }
  83. }
  84. /// <summary>
  85. /// Gets the current position within the stream.
  86. /// </summary>
  87. [CLSCompliant(false)]
  88. public uint Position
  89. {
  90. get
  91. {
  92. return (uint)rec.pos;
  93. }
  94. }
  95. /// <summary>
  96. /// Gets the descriptor. This field is a union that can hold an integer or a pointer. It is used by stream
  97. /// implementations to store file descriptors or ‘FILE*’ pointers.
  98. /// </summary>
  99. public StreamDesc Descriptor
  100. {
  101. get
  102. {
  103. return new StreamDesc(reference, Marshal.OffsetOf(typeof(StreamRec), "descriptor"));
  104. }
  105. }
  106. /// <summary>
  107. /// Gets the path name. This field is completely ignored by FreeType. However, it is often useful during
  108. /// debugging to use it to store the stream's filename (where available).
  109. /// </summary>
  110. public StreamDesc PathName
  111. {
  112. get
  113. {
  114. return new StreamDesc(reference, Marshal.OffsetOf(typeof(StreamRec), "pathname"));
  115. }
  116. }
  117. /// <summary>
  118. /// Gets the stream's input function.
  119. /// </summary>
  120. [CLSCompliant(false)]
  121. public StreamIOFunc Read
  122. {
  123. get
  124. {
  125. return rec.read;
  126. }
  127. }
  128. /// <summary>
  129. /// Gets the stream's close function.
  130. /// </summary>
  131. public StreamCloseFunc Close
  132. {
  133. get
  134. {
  135. return rec.close;
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the memory manager to use to preload frames. This is set internally by FreeType and shouldn't be
  140. /// touched by stream implementations.
  141. /// </summary>
  142. public Memory Memory
  143. {
  144. get
  145. {
  146. return new Memory(reference, Marshal.OffsetOf(typeof(StreamRec), "memory"));
  147. }
  148. }
  149. /// <summary>
  150. /// Gets the cursor. This field is set and used internally by FreeType when parsing frames.
  151. /// </summary>
  152. public IntPtr Cursor
  153. {
  154. get
  155. {
  156. return rec.cursor;
  157. }
  158. }
  159. /// <summary>
  160. /// Gets the limit. This field is set and used internally by FreeType when parsing frames.
  161. /// </summary>
  162. public IntPtr Limit
  163. {
  164. get
  165. {
  166. return rec.limit;
  167. }
  168. }
  169. internal IntPtr Reference
  170. {
  171. get
  172. {
  173. return reference;
  174. }
  175. set
  176. {
  177. reference = value;
  178. rec = PInvokeHelper.PtrToStructure<StreamRec>(reference);
  179. }
  180. }
  181. #endregion
  182. #region Methods
  183. #region GZIP Streams
  184. /// <summary>
  185. /// Open a new stream to parse gzip-compressed font files. This is mainly used to support the compressed
  186. /// ‘*.pcf.gz’ fonts that come with XFree86.
  187. /// </summary>
  188. /// <remarks><para>
  189. /// The source stream must be opened before calling this function.
  190. /// </para><para>
  191. /// Calling the internal function ‘FT_Stream_Close’ on the new stream will not call ‘FT_Stream_Close’ on the
  192. /// source stream. None of the stream objects will be released to the heap.
  193. /// </para><para>
  194. /// The stream implementation is very basic and resets the decompression process each time seeking backwards is
  195. /// needed within the stream.
  196. /// </para><para>
  197. /// In certain builds of the library, gzip compression recognition is automatically handled when calling
  198. /// <see cref="Library.NewFace"/> or <see cref="Library.OpenFace"/>. This means that if no font driver is
  199. /// capable of handling the raw compressed file, the library will try to open a gzipped stream from it and
  200. /// re-open the face with it.
  201. /// </para><para>
  202. /// This function may return <see cref="Error.UnimplementedFeature"/> if your build of FreeType was not
  203. /// compiled with zlib support.
  204. /// </para></remarks>
  205. /// <param name="source">The source stream.</param>
  206. public void OpenGzip(FTStream source)
  207. {
  208. Error err = FT.FT_Stream_OpenGzip(Reference, source.Reference);
  209. if (err != Error.Ok)
  210. throw new FreeTypeException(err);
  211. }
  212. #endregion
  213. #region LZW Streams
  214. /// <summary>
  215. /// Open a new stream to parse LZW-compressed font files. This is mainly used to support the compressed
  216. /// ‘*.pcf.Z’ fonts that come with XFree86.
  217. /// </summary>
  218. /// <remarks><para>
  219. /// The source stream must be opened before calling this function.
  220. /// </para><para>
  221. /// Calling the internal function ‘FT_Stream_Close’ on the new stream will not call ‘FT_Stream_Close’ on the
  222. /// source stream. None of the stream objects will be released to the heap.
  223. /// </para><para>
  224. /// The stream implementation is very basic and resets the decompression process each time seeking backwards is
  225. /// needed within the stream.
  226. /// </para><para>
  227. /// In certain builds of the library, LZW compression recognition is automatically handled when calling
  228. /// <see cref="Library.NewFace"/> or <see cref="Library.OpenFace"/>. This means that if no font driver is
  229. /// capable of handling the raw compressed file, the library will try to open a LZW stream from it and re-open
  230. /// the face with it.
  231. /// </para><para>
  232. /// This function may return <see cref="Error.UnimplementedFeature"/> if your build of FreeType was not
  233. /// compiled with LZW support.
  234. /// </para></remarks>
  235. /// <param name="source">The source stream.</param>
  236. public void OpenLzw(FTStream source)
  237. {
  238. Error err = FT.FT_Stream_OpenLZW(Reference, source.Reference);
  239. if (err != Error.Ok)
  240. throw new FreeTypeException(err);
  241. }
  242. #endregion
  243. #region BZIP2 Streams
  244. /// <summary>
  245. /// Open a new stream to parse bzip2-compressed font files. This is mainly used to support the compressed
  246. /// ‘*.pcf.bz2’ fonts that come with XFree86.
  247. /// </summary>
  248. /// <remarks><para>
  249. /// The source stream must be opened before calling this function.
  250. /// </para><para>
  251. /// Calling the internal function ‘FT_Stream_Close’ on the new stream will not call ‘FT_Stream_Close’ on the
  252. /// source stream. None of the stream objects will be released to the heap.
  253. /// </para><para>
  254. /// The stream implementation is very basic and resets the decompression process each time seeking backwards is
  255. /// needed within the stream.
  256. /// </para><para>
  257. /// In certain builds of the library, bzip2 compression recognition is automatically handled when calling
  258. /// <see cref="Library.NewFace"/> or <see cref="Library.OpenFace"/>. This means that if no font driver is
  259. /// capable of handling the raw compressed file, the library will try to open a bzip2 stream from it and
  260. /// re-open the face with it.
  261. /// </para><para>
  262. /// This function may return <see cref="Error.UnimplementedFeature"/> if your build of FreeType was not
  263. /// compiled with bzip2 support.
  264. /// </para></remarks>
  265. /// <param name="source">The source stream.</param>
  266. public void StreamOpenBzip2(FTStream source)
  267. {
  268. Error err = FT.FT_Stream_OpenBzip2(Reference, source.Reference);
  269. if (err != Error.Ok)
  270. throw new FreeTypeException(err);
  271. }
  272. #endregion
  273. #endregion
  274. }
  275. }