FT.Public.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Collections.Generic;
  22. using System.Runtime.InteropServices;
  23. using SharpFont.Internal;
  24. namespace SharpFont
  25. {
  26. /// <summary>
  27. /// Provides an API very similar to the original FreeType API.
  28. /// </summary>
  29. /// <remarks>
  30. /// Useful for porting over C code that relies on FreeType. For everything else, use the instance methods of the
  31. /// classes provided by SharpFont, they are designed to follow .NET naming and style conventions.
  32. /// </remarks>
  33. public static partial class FT
  34. {
  35. #region Computations
  36. /// <summary>
  37. /// The angle pi expressed in FT_Angle units.
  38. /// </summary>
  39. public const int AnglePI = 180 << 16;
  40. /// <summary>
  41. /// The angle 2*pi expressed in FT_Angle units.
  42. /// </summary>
  43. public const int Angle2PI = AnglePI * 2;
  44. /// <summary>
  45. /// The angle pi/2 expressed in FT_Angle units.
  46. /// </summary>
  47. public const int AnglePI2 = AnglePI / 2;
  48. /// <summary>
  49. /// The angle pi/4 expressed in FT_Angle units.
  50. /// </summary>
  51. public const int AnglePI4 = AnglePI / 4;
  52. /// <summary><para>
  53. /// A very simple function used to perform the computation ‘(a*b)/c’ with maximal accuracy (it uses a 64-bit
  54. /// intermediate integer whenever necessary).
  55. /// </para><para>
  56. /// This function isn't necessarily as fast as some processor specific operations, but is at least completely
  57. /// portable.
  58. /// </para></summary>
  59. /// <param name="a">The first multiplier.</param>
  60. /// <param name="b">The second multiplier.</param>
  61. /// <param name="c">The divisor.</param>
  62. /// <returns>
  63. /// The result of ‘(a*b)/c’. This function never traps when trying to divide by zero; it simply returns
  64. /// ‘MaxInt’ or ‘MinInt’ depending on the signs of ‘a’ and ‘b’.
  65. /// </returns>
  66. public static int MulDiv(int a, int b, int c)
  67. {
  68. return FT_MulDiv(a, b, c);
  69. }
  70. /// <summary>
  71. /// A very simple function used to perform the computation ‘(a*b)/0x10000’ with maximal accuracy. Most of the
  72. /// time this is used to multiply a given value by a 16.16 fixed float factor.
  73. /// </summary>
  74. /// <remarks><para>
  75. /// This function has been optimized for the case where the absolute value of ‘a’ is less than 2048, and ‘b’ is
  76. /// a 16.16 scaling factor. As this happens mainly when scaling from notional units to fractional pixels in
  77. /// FreeType, it resulted in noticeable speed improvements between versions 2.x and 1.x.
  78. /// </para><para>
  79. /// As a conclusion, always try to place a 16.16 factor as the second argument of this function; this can make
  80. /// a great difference.
  81. /// </para></remarks>
  82. /// <param name="a">The first multiplier.</param>
  83. /// <param name="b">The second multiplier. Use a 16.16 factor here whenever possible (see note below).</param>
  84. /// <returns>The result of ‘(a*b)/0x10000’.</returns>
  85. public static int MulFix(int a, int b)
  86. {
  87. return FT_MulFix(a, b);
  88. }
  89. /// <summary>
  90. /// A very simple function used to perform the computation ‘(a*0x10000)/b’ with maximal accuracy. Most of the
  91. /// time, this is used to divide a given value by a 16.16 fixed float factor.
  92. /// </summary>
  93. /// <remarks>
  94. /// The optimization for <see cref="DivFix"/> is simple: If (a &lt;&lt; 16) fits in 32 bits, then the division
  95. /// is computed directly. Otherwise, we use a specialized version of <see cref="MulDiv"/>.
  96. /// </remarks>
  97. /// <param name="a">The first multiplier.</param>
  98. /// <param name="b">The second multiplier. Use a 16.16 factor here whenever possible (see note below).</param>
  99. /// <returns>The result of ‘(a*0x10000)/b’.</returns>
  100. public static int DivFix(int a, int b)
  101. {
  102. return FT_DivFix(a, b);
  103. }
  104. /// <summary>
  105. /// A very simple function used to round a 16.16 fixed number.
  106. /// </summary>
  107. /// <param name="a">The number to be rounded.</param>
  108. /// <returns>The result of ‘(a + 0x8000) &amp; -0x10000’.</returns>
  109. public static int RoundFix(int a)
  110. {
  111. return FT_RoundFix(a);
  112. }
  113. /// <summary>
  114. /// A very simple function used to compute the ceiling function of a 16.16 fixed number.
  115. /// </summary>
  116. /// <param name="a">The number for which the ceiling function is to be computed.</param>
  117. /// <returns>The result of ‘(a + 0x10000 - 1) &amp; -0x10000’.</returns>
  118. public static int CeilFix(int a)
  119. {
  120. return FT_CeilFix(a);
  121. }
  122. /// <summary>
  123. /// A very simple function used to compute the floor function of a 16.16 fixed number.
  124. /// </summary>
  125. /// <param name="a">The number for which the floor function is to be computed.</param>
  126. /// <returns>The result of ‘a &amp; -0x10000’.</returns>
  127. public static int FloorFix(int a)
  128. {
  129. return FT_FloorFix(a);
  130. }
  131. /// <summary>
  132. /// Return the sinus of a given angle in fixed point format.
  133. /// </summary>
  134. /// <remarks>
  135. /// If you need both the sinus and cosinus for a given angle, use the function <see cref="FTVector.Unit"/>.
  136. /// </remarks>
  137. /// <param name="angle">The input angle.</param>
  138. /// <returns>The sinus value.</returns>
  139. public static int Sin(int angle)
  140. {
  141. return FT_Sin(angle);
  142. }
  143. /// <summary>
  144. /// Return the cosinus of a given angle in fixed point format.
  145. /// </summary>
  146. /// <remarks>
  147. /// If you need both the sinus and cosinus for a given angle, use the function <see cref="FTVector.Unit"/>.
  148. /// </remarks>
  149. /// <param name="angle">The input angle.</param>
  150. /// <returns>The cosinus value.</returns>
  151. public static int Cos(int angle)
  152. {
  153. return FT_Cos(angle);
  154. }
  155. /// <summary>
  156. /// Return the tangent of a given angle in fixed point format.
  157. /// </summary>
  158. /// <param name="angle">The input angle.</param>
  159. /// <returns>The tangent value.</returns>
  160. public static int Tan(int angle)
  161. {
  162. return FT_Tan(angle);
  163. }
  164. /// <summary>
  165. /// Return the arc-tangent corresponding to a given vector (x,y) in the 2d plane.
  166. /// </summary>
  167. /// <param name="x">The horizontal vector coordinate.</param>
  168. /// <param name="y">The vertical vector coordinate.</param>
  169. /// <returns>The arc-tangent value (i.e. angle).</returns>
  170. public static int Atan2(int x, int y)
  171. {
  172. return FT_Atan2(x, y);
  173. }
  174. /// <summary>
  175. /// Return the difference between two angles. The result is always constrained to the [-PI..PI] interval.
  176. /// </summary>
  177. /// <param name="angle1">First angle.</param>
  178. /// <param name="angle2">Second angle.</param>
  179. /// <returns>Constrained value of ‘value2-value1’.</returns>
  180. public static int AngleDiff(int angle1, int angle2)
  181. {
  182. return FT_Angle_Diff(angle1, angle2);
  183. }
  184. #endregion
  185. #region Mac Specific Interface
  186. /// <summary>
  187. /// Return an FSSpec for the disk file containing the named font.
  188. /// </summary>
  189. /// <param name="fontName">Mac OS name of the font (e.g., Times New Roman Bold).</param>
  190. /// <param name="faceIndex">Index of the face. For passing to <see cref="Library.NewFaceFromFSSpec"/>.</param>
  191. /// <returns>FSSpec to the file. For passing to <see cref="Library.NewFaceFromFSSpec"/>.</returns>
  192. public static IntPtr GetFileFromMacName(string fontName, out int faceIndex)
  193. {
  194. IntPtr fsspec;
  195. Error err = FT_GetFile_From_Mac_Name(fontName, out fsspec, out faceIndex);
  196. if (err != Error.Ok)
  197. throw new FreeTypeException(err);
  198. return fsspec;
  199. }
  200. /// <summary>
  201. /// Return an FSSpec for the disk file containing the named font.
  202. /// </summary>
  203. /// <param name="fontName">Mac OS name of the font in ATS framework.</param>
  204. /// <param name="faceIndex">Index of the face. For passing to <see cref="Library.NewFaceFromFSSpec"/>.</param>
  205. /// <returns>FSSpec to the file. For passing to <see cref="Library.NewFaceFromFSSpec"/>.</returns>
  206. public static IntPtr GetFileFromMacAtsName(string fontName, out int faceIndex)
  207. {
  208. IntPtr fsspec;
  209. Error err = FT_GetFile_From_Mac_ATS_Name(fontName, out fsspec, out faceIndex);
  210. if (err != Error.Ok)
  211. throw new FreeTypeException(err);
  212. return fsspec;
  213. }
  214. /// <summary>
  215. /// Return a pathname of the disk file and face index for given font name which is handled by ATS framework.
  216. /// </summary>
  217. /// <param name="fontName">Mac OS name of the font in ATS framework.</param>
  218. /// <param name="path">
  219. /// Buffer to store pathname of the file. For passing to <see cref="Library.NewFace"/>. The client must
  220. /// allocate this buffer before calling this function.
  221. /// </param>
  222. /// <returns>Index of the face. For passing to <see cref="Library.NewFace"/>.</returns>
  223. public static unsafe int GetFilePathFromMacAtsName(string fontName, byte[] path)
  224. {
  225. int faceIndex;
  226. fixed (void* ptr = path)
  227. {
  228. Error err = FT_GetFilePath_From_Mac_ATS_Name(fontName, (IntPtr)ptr, path.Length, out faceIndex);
  229. if (err != Error.Ok)
  230. throw new FreeTypeException(err);
  231. }
  232. return faceIndex;
  233. }
  234. #endregion
  235. }
  236. }