Generic.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /// Describe a function used to destroy the ‘client’ data of any FreeType object. See the description of the
  27. /// <see cref="Generic"/> type for details of usage.
  28. /// </summary>
  29. /// <param name="object">
  30. /// The address of the FreeType object which is under finalization. Its client data is accessed through its
  31. /// ‘generic’ field.
  32. /// </param>
  33. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  34. public delegate void GenericFinalizer(IntPtr @object);
  35. /// <summary><para>
  36. /// Client applications often need to associate their own data to a variety of FreeType core objects. For example,
  37. /// a text layout API might want to associate a glyph cache to a given size object.
  38. /// </para><para>
  39. /// Most FreeType object contains a ‘generic’ field, of type <see cref="Generic"/>, which usage is left to client
  40. /// applications and font servers.
  41. /// </para><para>
  42. /// It can be used to store a pointer to client-specific data, as well as the address of a ‘finalizer’ function,
  43. /// which will be called by FreeType when the object is destroyed (for example, the previous client example would
  44. /// put the address of the glyph cache destructor in the ‘finalizer’ field).
  45. /// </para></summary>
  46. public class Generic
  47. {
  48. #region Fields
  49. private GenericRec rec;
  50. #endregion
  51. #region Constructors
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="Generic"/> class.
  54. /// </summary>
  55. /// <param name="data">
  56. /// A typeless pointer to some client data. The data it cointains must stay fixed until finalizer is called.
  57. /// </param>
  58. /// <param name="finalizer">A delegate that gets called when the contained object gets finalized.</param>
  59. public Generic(IntPtr data, GenericFinalizer finalizer)
  60. {
  61. rec.data = data;
  62. rec.finalizer = finalizer;
  63. }
  64. internal Generic(GenericRec genInternal)
  65. {
  66. rec = genInternal;
  67. }
  68. internal Generic(IntPtr reference)
  69. {
  70. rec = PInvokeHelper.PtrToStructure<GenericRec>(reference);
  71. }
  72. internal Generic(IntPtr reference, int offset)
  73. : this(new IntPtr(reference.ToInt64() + offset))
  74. {
  75. }
  76. #endregion
  77. #region Properties
  78. /// <summary>
  79. /// Gets the size of a <see cref="Generic"/>, in bytes.
  80. /// </summary>
  81. public static int SizeInBytes
  82. {
  83. get
  84. {
  85. return Marshal.SizeOf(typeof(GenericRec));
  86. }
  87. }
  88. /// <summary>
  89. /// Gets or sets a typeless pointer to any client-specified data. This field is completely ignored by the
  90. /// FreeType library.
  91. /// </summary>
  92. public IntPtr Data
  93. {
  94. get
  95. {
  96. return rec.data;
  97. }
  98. set
  99. {
  100. rec.data = value;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets or sets a pointer to a <see cref="GenericFinalizer"/> function, which will be called when the object
  105. /// is destroyed. If this field is set to NULL, no code will be called.
  106. /// </summary>
  107. public GenericFinalizer Finalizer
  108. {
  109. get
  110. {
  111. return rec.finalizer;
  112. }
  113. set
  114. {
  115. rec.finalizer = value;
  116. }
  117. }
  118. #endregion
  119. #region Methods
  120. //TODO make this private and build it into the setters if the reference isn't IntPtr.Zero.
  121. internal void WriteToUnmanagedMemory(IntPtr location)
  122. {
  123. Marshal.WriteIntPtr(location, rec.data);
  124. Marshal.WriteIntPtr(location, IntPtr.Size, Marshal.GetFunctionPointerForDelegate(rec.finalizer));
  125. }
  126. #endregion
  127. }
  128. }