FTList.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /// An <see cref="FTList"/> iterator function which is called during a list parse by <see cref="FTList.Iterate"/>.
  27. /// </summary>
  28. /// <param name="node">The current iteration list node.</param>
  29. /// <param name="user">
  30. /// A typeless pointer passed to <see cref="ListIterator"/>. Can be used to point to the iteration's state.
  31. /// </param>
  32. /// <returns>Error code.</returns>
  33. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  34. public delegate Error ListIterator([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ListNodeMarshaler))]ListNode node, IntPtr user);
  35. /// <summary>
  36. /// An <see cref="FTList"/> iterator function which is called during a list finalization by
  37. /// <see cref="FTList.Finalize"/> to destroy all elements in a given list.
  38. /// </summary>
  39. /// <param name="memory">The current system object.</param>
  40. /// <param name="data">The current object to destroy.</param>
  41. /// <param name="user">
  42. /// A typeless pointer passed to <see cref="FTList.Iterate"/>. It can be used to point to the iteration's state.
  43. /// </param>
  44. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  45. public delegate void ListDestructor([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MemoryMarshaler))]Memory memory, IntPtr data, IntPtr user);
  46. /// <summary>
  47. /// A structure used to hold a simple doubly-linked list. These are used in many parts of FreeType.
  48. /// </summary>
  49. public class FTList
  50. {
  51. #region Fields
  52. private IntPtr reference;
  53. private ListRec rec;
  54. #endregion
  55. #region Constructors
  56. internal FTList(IntPtr reference)
  57. {
  58. Reference = reference;
  59. }
  60. #endregion
  61. #region Properties
  62. /// <summary>
  63. /// Gets the head (first element) of doubly-linked list.
  64. /// </summary>
  65. public ListNode Head
  66. {
  67. get
  68. {
  69. return new ListNode(rec.head);
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the tail (last element) of doubly-linked list.
  74. /// </summary>
  75. public ListNode Tail
  76. {
  77. get
  78. {
  79. return new ListNode(rec.tail);
  80. }
  81. }
  82. internal IntPtr Reference
  83. {
  84. get
  85. {
  86. return reference;
  87. }
  88. set
  89. {
  90. reference = value;
  91. rec = PInvokeHelper.PtrToStructure<ListRec>(reference);
  92. }
  93. }
  94. #endregion
  95. #region Methods
  96. /// <summary>
  97. /// Find the list node for a given listed object.
  98. /// </summary>
  99. /// <param name="data">The address of the listed object.</param>
  100. /// <returns>List node. NULL if it wasn't found.</returns>
  101. public ListNode Find(IntPtr data)
  102. {
  103. return new ListNode(FT.FT_List_Find(Reference, data));
  104. }
  105. /// <summary>
  106. /// Append an element to the end of a list.
  107. /// </summary>
  108. /// <param name="node">The node to append.</param>
  109. public void Add(ListNode node)
  110. {
  111. FT.FT_List_Add(Reference, node.Reference);
  112. }
  113. /// <summary>
  114. /// Insert an element at the head of a list.
  115. /// </summary>
  116. /// <param name="node">The node to insert.</param>
  117. public void Insert(ListNode node)
  118. {
  119. FT.FT_List_Insert(Reference, node.Reference);
  120. }
  121. /// <summary>
  122. /// Remove a node from a list. This function doesn't check whether the node is in the list!
  123. /// </summary>
  124. /// <param name="node">The node to remove.</param>
  125. public void Remove(ListNode node)
  126. {
  127. FT.FT_List_Remove(Reference, node.Reference);
  128. }
  129. /// <summary>
  130. /// Move a node to the head/top of a list. Used to maintain LRU lists.
  131. /// </summary>
  132. /// <param name="node">The node to move.</param>
  133. public void Up(ListNode node)
  134. {
  135. FT.FT_List_Up(Reference, node.Reference);
  136. }
  137. /// <summary>
  138. /// Parse a list and calls a given iterator function on each element. Note that parsing is stopped as soon as
  139. /// one of the iterator calls returns a non-zero value.
  140. /// </summary>
  141. /// <param name="iterator">An iterator function, called on each node of the list.</param>
  142. /// <param name="user">A user-supplied field which is passed as the second argument to the iterator.</param>
  143. public void Iterate(ListIterator iterator, IntPtr user)
  144. {
  145. Error err = FT.FT_List_Iterate(Reference, iterator, user);
  146. if (err != Error.Ok)
  147. throw new FreeTypeException(err);
  148. }
  149. /// <summary>
  150. /// Destroy all elements in the list as well as the list itself.
  151. /// </summary>
  152. /// <remarks>
  153. /// This function expects that all nodes added by <see cref="Add"/> or <see cref="Insert"/> have been
  154. /// dynamically allocated.
  155. /// </remarks>
  156. /// <param name="destroy">A list destructor that will be applied to each element of the list.</param>
  157. /// <param name="memory">The current memory object which handles deallocation.</param>
  158. /// <param name="user">A user-supplied field which is passed as the last argument to the destructor.</param>
  159. public void Finalize(ListDestructor destroy, Memory memory, IntPtr user)
  160. {
  161. FT.FT_List_Finalize(Reference, destroy, memory.Reference, user);
  162. }
  163. #endregion
  164. }
  165. }