FTVector.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #if WIN64
  24. using FT_26Dot6 = System.Int32;
  25. using FT_Fixed = System.Int32;
  26. using FT_Long = System.Int32;
  27. using FT_Pos = System.Int32;
  28. using FT_ULong = System.UInt32;
  29. #else
  30. using FT_26Dot6 = System.IntPtr;
  31. using FT_Fixed = System.IntPtr;
  32. using FT_Long = System.IntPtr;
  33. using FT_Pos = System.IntPtr;
  34. using FT_ULong = System.UIntPtr;
  35. #endif
  36. namespace SharpFont
  37. {
  38. /// <summary>
  39. /// A simple structure used to store a 2D vector.
  40. /// </summary>
  41. [StructLayout(LayoutKind.Sequential)]
  42. public struct FTVector
  43. {
  44. #region Fields
  45. private FT_Long x;
  46. private FT_Long y;
  47. #endregion
  48. #region Constructors
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="FTVector"/> struct.
  51. /// </summary>
  52. /// <param name="x">The horizontal coordinate.</param>
  53. /// <param name="y">The vertical coordinate.</param>
  54. public FTVector(int x, int y)
  55. : this()
  56. {
  57. #if WIN64
  58. this.x = x;
  59. this.y = y;
  60. #else
  61. this.x = (IntPtr)x;
  62. this.y = (IntPtr)y;
  63. #endif
  64. }
  65. internal FTVector(IntPtr reference)
  66. : this()
  67. {
  68. #if WIN64
  69. this.x = Marshal.ReadInt32(reference);
  70. this.y = Marshal.ReadInt32(reference, sizeof(int));
  71. #else
  72. this.x = Marshal.ReadIntPtr(reference);
  73. this.y = Marshal.ReadIntPtr(reference, IntPtr.Size);
  74. #endif
  75. }
  76. #endregion
  77. #region Properties
  78. /// <summary>
  79. /// Gets or sets the horizontal coordinate.
  80. /// </summary>
  81. public int X
  82. {
  83. get
  84. {
  85. return (int)x;
  86. }
  87. set
  88. {
  89. #if WIN64
  90. x = value;
  91. #else
  92. x = (IntPtr)value;
  93. #endif
  94. }
  95. }
  96. /// <summary>
  97. /// Gets or sets the vertical coordinate.
  98. /// </summary>
  99. public int Y
  100. {
  101. get
  102. {
  103. return (int)y;
  104. }
  105. set
  106. {
  107. #if WIN64
  108. y = value;
  109. #else
  110. y = (IntPtr)value;
  111. #endif
  112. }
  113. }
  114. #endregion
  115. #region Methods
  116. /// <summary><para>
  117. /// Return the unit vector corresponding to a given angle. After the call, the value of ‘vec.x’ will be
  118. /// ‘sin(angle)’, and the value of ‘vec.y’ will be ‘cos(angle)’.
  119. /// </para><para>
  120. /// This function is useful to retrieve both the sinus and cosinus of a given angle quickly.
  121. /// </para></summary>
  122. /// <param name="angle">The address of angle.</param>
  123. /// <returns>The address of target vector.</returns>
  124. public static FTVector Unit(int angle)
  125. {
  126. FTVector vec;
  127. FT.FT_Vector_Unit(out vec, angle);
  128. return vec;
  129. }
  130. /// <summary>
  131. /// Compute vector coordinates from a length and angle.
  132. /// </summary>
  133. /// <param name="length">The vector length.</param>
  134. /// <param name="angle">The vector angle.</param>
  135. /// <returns>The address of source vector.</returns>
  136. public static FTVector FromPolar(int length, int angle)
  137. {
  138. FTVector vec;
  139. FT.FT_Vector_From_Polar(out vec, length, angle);
  140. return vec;
  141. }
  142. /// <summary>
  143. /// Transform a single vector through a 2x2 matrix.
  144. /// </summary>
  145. /// <remarks>
  146. /// The result is undefined if either ‘vector’ or ‘matrix’ is invalid.
  147. /// </remarks>
  148. /// <param name="matrix">A pointer to the source 2x2 matrix.</param>
  149. public void Transform(FTMatrix matrix)
  150. {
  151. FT.FT_Vector_Transform(ref this, ref matrix);
  152. }
  153. /// <summary>
  154. /// Rotate a vector by a given angle.
  155. /// </summary>
  156. /// <param name="angle">The address of angle.</param>
  157. public void Rotate(int angle)
  158. {
  159. FT.FT_Vector_Rotate(ref this, angle);
  160. }
  161. /// <summary>
  162. /// Return the length of a given vector.
  163. /// </summary>
  164. /// <returns>The vector length, expressed in the same units that the original vector coordinates.</returns>
  165. public int Length()
  166. {
  167. return FT.FT_Vector_Length(ref this);
  168. }
  169. /// <summary>
  170. /// Compute both the length and angle of a given vector.
  171. /// </summary>
  172. /// <param name="length">The vector length.</param>
  173. /// <param name="angle">The vector angle.</param>
  174. public void Polarize(out int length, out int angle)
  175. {
  176. FT.FT_Vector_Polarize(ref this, out length, out angle);
  177. }
  178. #endregion
  179. }
  180. }