FTMatrix.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 2x2 matrix. Coefficients are in 16.16 fixed float format. The computation
  40. /// performed is:
  41. /// <code>
  42. /// x' = x*xx + y*xy
  43. /// y' = x*yx + y*yy
  44. /// </code>
  45. /// </summary>
  46. [StructLayout(LayoutKind.Sequential)]
  47. public struct FTMatrix
  48. {
  49. #region Fields
  50. private FT_Fixed xx, xy;
  51. private FT_Fixed yx, yy;
  52. #endregion
  53. #region Constructors
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="FTMatrix"/> struct.
  56. /// </summary>
  57. /// <param name="xx">Matrix coefficient XX.</param>
  58. /// <param name="xy">Matrix coefficient XY.</param>
  59. /// <param name="yx">Matrix coefficient YX.</param>
  60. /// <param name="yy">Matrix coefficient YY.</param>
  61. public FTMatrix(int xx, int xy, int yx, int yy)
  62. : this()
  63. {
  64. #if WIN64
  65. this.xx = xx;
  66. this.xy = xy;
  67. this.yx = yx;
  68. this.yy = yy;
  69. #else
  70. this.xx = (IntPtr)xx;
  71. this.xy = (IntPtr)xy;
  72. this.yx = (IntPtr)yx;
  73. this.yy = (IntPtr)yy;
  74. #endif
  75. }
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="FTMatrix"/> struct.
  78. /// </summary>
  79. /// <param name="row0">Matrix coefficients XX, XY.</param>
  80. /// <param name="row1">Matrix coefficients YX, YY.</param>
  81. public FTMatrix(FTVector row0, FTVector row1)
  82. : this(row0.X, row0.Y, row1.X, row1.Y)
  83. {
  84. }
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="FTMatrix"/> struct.
  87. /// </summary>
  88. /// <param name="reference">A pointer to a matrix.</param>
  89. internal FTMatrix(IntPtr reference)
  90. : this()
  91. {
  92. #if WIN64
  93. xx = Marshal.ReadInt32(reference);
  94. xy = Marshal.ReadInt32(reference, sizeof(int));
  95. yx = Marshal.ReadInt32(reference, sizeof(int) * 2);
  96. yy = Marshal.ReadInt32(reference, sizeof(int) * 3);
  97. #else
  98. xx = Marshal.ReadIntPtr(reference);
  99. xy = Marshal.ReadIntPtr(reference, IntPtr.Size);
  100. yx = Marshal.ReadIntPtr(reference, IntPtr.Size * 2);
  101. yy = Marshal.ReadIntPtr(reference, IntPtr.Size * 3);
  102. #endif
  103. }
  104. #endregion
  105. #region Properties
  106. /// <summary>
  107. /// Gets or sets the matrix coefficient.
  108. /// </summary>
  109. public int XX
  110. {
  111. get
  112. {
  113. return (int)xx;
  114. }
  115. set
  116. {
  117. #if WIN64
  118. xx = value;
  119. #else
  120. xx = (IntPtr)value;
  121. #endif
  122. }
  123. }
  124. /// <summary>
  125. /// Gets or sets the matrix coefficient.
  126. /// </summary>
  127. public int XY
  128. {
  129. get
  130. {
  131. return (int)xy;
  132. }
  133. set
  134. {
  135. #if WIN64
  136. xy = value;
  137. #else
  138. xy = (IntPtr)value;
  139. #endif
  140. }
  141. }
  142. /// <summary>
  143. /// Gets or sets the matrix coefficient.
  144. /// </summary>
  145. public int YX
  146. {
  147. get
  148. {
  149. return (int)yx;
  150. }
  151. set
  152. {
  153. #if WIN64
  154. yx = value;
  155. #else
  156. yx = (IntPtr)value;
  157. #endif
  158. }
  159. }
  160. /// <summary>
  161. /// Gets or sets the matrix coefficient.
  162. /// </summary>
  163. public int YY
  164. {
  165. get
  166. {
  167. return (int)yy;
  168. }
  169. set
  170. {
  171. #if WIN64
  172. yy = value;
  173. #else
  174. yy = (IntPtr)value;
  175. #endif
  176. }
  177. }
  178. #endregion
  179. #region Methods
  180. /// <summary>
  181. /// Perform the matrix operation ‘b = a*b’.
  182. /// </summary>
  183. /// <remarks>
  184. /// The result is undefined if either ‘a’ or ‘b’ is zero.
  185. /// </remarks>
  186. /// <param name="a">A pointer to matrix ‘a’.</param>
  187. /// <param name="b">A pointer to matrix ‘b’.</param>
  188. public static void Multiply(FTMatrix a, FTMatrix b)
  189. {
  190. FT.FT_Matrix_Multiply(ref a, ref b);
  191. }
  192. /// <summary>
  193. /// Perform the matrix operation ‘b = a*b’.
  194. /// </summary>
  195. /// <remarks>
  196. /// The result is undefined if either ‘a’ or ‘b’ is zero.
  197. /// </remarks>
  198. /// <param name="b">A pointer to matrix ‘b’.</param>
  199. public void Multiply(FTMatrix b)
  200. {
  201. FT.FT_Matrix_Multiply(ref this, ref b);
  202. }
  203. /// <summary>
  204. /// Invert a 2x2 matrix. Return an error if it can't be inverted.
  205. /// </summary>
  206. public void Invert()
  207. {
  208. Error err = FT.FT_Matrix_Invert(ref this);
  209. if (err != Error.Ok)
  210. throw new FreeTypeException(err);
  211. }
  212. #endregion
  213. }
  214. }