RasterFuncs.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 create a new raster object.
  27. /// </summary>
  28. /// <remarks>
  29. /// The ‘memory’ parameter is a typeless pointer in order to avoid un-wanted dependencies on the rest of the
  30. /// FreeType code. In practice, it is an <see cref="Memory"/> object, i.e., a handle to the standard FreeType
  31. /// memory allocator. However, this field can be completely ignored by a given raster implementation.
  32. /// </remarks>
  33. /// <param name="memory">A handle to the memory allocator.</param>
  34. /// <param name="raster">A handle to the new raster object.</param>
  35. /// <returns>Error code. 0 means success.</returns>
  36. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  37. public delegate Error RasterNewFunc(IntPtr memory, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(RasterMarshaler))] Raster raster);
  38. /// <summary>
  39. /// A function used to destroy a given raster object.
  40. /// </summary>
  41. /// <param name="raster">A handle to the raster object.</param>
  42. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  43. public delegate void RasterDoneFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(RasterMarshaler))] Raster raster);
  44. /// <summary><para>
  45. /// FreeType provides an area of memory called the ‘render pool’, available to all registered rasters. This pool
  46. /// can be freely used during a given scan-conversion but is shared by all rasters. Its content is thus transient.
  47. /// </para><para>
  48. /// This function is called each time the render pool changes, or just after a new raster object is created.
  49. /// </para></summary>
  50. /// <remarks>
  51. /// Rasters can ignore the render pool and rely on dynamic memory allocation if they want to (a handle to the
  52. /// memory allocator is passed to the raster constructor). However, this is not recommended for efficiency
  53. /// purposes.
  54. /// </remarks>
  55. /// <param name="raster">A handle to the new raster object.</param>
  56. /// <param name="pool_base">The address in memory of the render pool.</param>
  57. /// <param name="pool_size">The size in bytes of the render pool.</param>
  58. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  59. public delegate void RasterResetFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(RasterMarshaler))] Raster raster, IntPtr pool_base, int pool_size);
  60. /// <summary>
  61. /// This function is a generic facility to change modes or attributes in a given raster. This can be used for
  62. /// debugging purposes, or simply to allow implementation-specific ‘features’ in a given raster module.
  63. /// </summary>
  64. /// <param name="raster">A handle to the new raster object.</param>
  65. /// <param name="mode">A 4-byte tag used to name the mode or property.</param>
  66. /// <param name="args">A pointer to the new mode/property to use.</param>
  67. [CLSCompliant(false)]
  68. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  69. public delegate void RasterSetModeFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(RasterMarshaler))] Raster raster, uint mode, IntPtr args);
  70. /// <summary>
  71. /// Invoke a given raster to scan-convert a given glyph image into a target
  72. /// bitmap.
  73. /// </summary>
  74. /// <remarks><para>
  75. /// The exact format of the source image depends on the raster's glyph format defined in its
  76. /// <see cref="RasterFuncs"/> structure. It can be an <see cref="Outline"/> or anything else in order to support a
  77. /// large array of glyph formats.
  78. /// </para><para>
  79. /// Note also that the render function can fail and return a <see cref="Error.UnimplementedFeature"/> error code if
  80. /// the raster used does not support direct composition.
  81. /// </para><para>
  82. /// XXX: For now, the standard raster doesn't support direct composition but this should change for the final
  83. /// release (see the files ‘demos/src/ftgrays.c’ and ‘demos/src/ftgrays2.c’ for examples of distinct
  84. /// implementations which support direct composition).
  85. /// </para></remarks>
  86. /// <param name="raster">A handle to the raster object.</param>
  87. /// <param name="params">
  88. /// A pointer to an <see cref="RasterParams"/> structure used to store the rendering parameters.
  89. /// </param>
  90. /// <returns>Error code. 0 means success.</returns>
  91. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  92. public delegate Error RasterRenderFunc([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(RasterMarshaler))] Raster raster, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(RasterParamsMarshaler))] RasterParams @params);
  93. /// <summary>
  94. /// A structure used to describe a given raster class to the library.
  95. /// </summary>
  96. public class RasterFuncs
  97. {
  98. #region Fields
  99. private IntPtr reference;
  100. private RasterFuncsRec rec;
  101. #endregion
  102. #region Constructors
  103. internal RasterFuncs(IntPtr reference)
  104. {
  105. Reference = reference;
  106. }
  107. internal RasterFuncs(IntPtr reference, IntPtr offset)
  108. : this(new IntPtr(reference.ToInt64() + offset.ToInt64()))
  109. {
  110. }
  111. #endregion
  112. #region Properties
  113. /// <summary>
  114. /// Gets the supported glyph format for this raster.
  115. /// </summary>
  116. [CLSCompliant(false)]
  117. public GlyphFormat Format
  118. {
  119. get
  120. {
  121. return rec.glyph_format;
  122. }
  123. }
  124. /// <summary>
  125. /// Gets the raster constructor.
  126. /// </summary>
  127. public RasterNewFunc New
  128. {
  129. get
  130. {
  131. return rec.raster_new;
  132. }
  133. }
  134. /// <summary>
  135. /// Gets a function used to reset the render pool within the raster.
  136. /// </summary>
  137. public RasterResetFunc Reset
  138. {
  139. get
  140. {
  141. return rec.raster_reset;
  142. }
  143. }
  144. /// <summary>
  145. /// Gets a function to set or change modes.
  146. /// </summary>
  147. [CLSCompliant(false)]
  148. public RasterSetModeFunc SetMode
  149. {
  150. get
  151. {
  152. return rec.raster_set_mode;
  153. }
  154. }
  155. /// <summary>
  156. /// Gets a function to render a glyph into a given bitmap.
  157. /// </summary>
  158. public RasterRenderFunc Render
  159. {
  160. get
  161. {
  162. return rec.raster_render;
  163. }
  164. }
  165. /// <summary>
  166. /// Gets the raster destructor.
  167. /// </summary>
  168. public RasterDoneFunc Done
  169. {
  170. get
  171. {
  172. return rec.raster_done;
  173. }
  174. }
  175. internal IntPtr Reference
  176. {
  177. get
  178. {
  179. return reference;
  180. }
  181. set
  182. {
  183. reference = value;
  184. rec = PInvokeHelper.PtrToStructure<RasterFuncsRec>(reference);
  185. }
  186. }
  187. #endregion
  188. }
  189. }