/src/freetype/src/autofit/afangles.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 292 lines · 195 code · 60 blank · 37 comment · 38 complexity · a8f08b0f00ff60bec5ff01e557e658da MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* afangles.c */
  4. /* */
  5. /* Routines used to compute vector angles with limited accuracy */
  6. /* and very high speed. It also contains sorting routines (body). */
  7. /* */
  8. /* Copyright 2003-2006, 2011 by */
  9. /* David Turner, Robert Wilhelm, and Werner Lemberg. */
  10. /* */
  11. /* This file is part of the FreeType project, and may only be used, */
  12. /* modified, and distributed under the terms of the FreeType project */
  13. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  14. /* this file you indicate that you have read the license and */
  15. /* understand and accept it fully. */
  16. /* */
  17. /***************************************************************************/
  18. #include "aftypes.h"
  19. #if 0
  20. FT_LOCAL_DEF( FT_Int )
  21. af_corner_is_flat( FT_Pos x_in,
  22. FT_Pos y_in,
  23. FT_Pos x_out,
  24. FT_Pos y_out )
  25. {
  26. FT_Pos ax = x_in;
  27. FT_Pos ay = y_in;
  28. FT_Pos d_in, d_out, d_corner;
  29. if ( ax < 0 )
  30. ax = -ax;
  31. if ( ay < 0 )
  32. ay = -ay;
  33. d_in = ax + ay;
  34. ax = x_out;
  35. if ( ax < 0 )
  36. ax = -ax;
  37. ay = y_out;
  38. if ( ay < 0 )
  39. ay = -ay;
  40. d_out = ax + ay;
  41. ax = x_out + x_in;
  42. if ( ax < 0 )
  43. ax = -ax;
  44. ay = y_out + y_in;
  45. if ( ay < 0 )
  46. ay = -ay;
  47. d_corner = ax + ay;
  48. return ( d_in + d_out - d_corner ) < ( d_corner >> 4 );
  49. }
  50. FT_LOCAL_DEF( FT_Int )
  51. af_corner_orientation( FT_Pos x_in,
  52. FT_Pos y_in,
  53. FT_Pos x_out,
  54. FT_Pos y_out )
  55. {
  56. FT_Pos delta;
  57. delta = x_in * y_out - y_in * x_out;
  58. if ( delta == 0 )
  59. return 0;
  60. else
  61. return 1 - 2 * ( delta < 0 );
  62. }
  63. #endif /* 0 */
  64. /*
  65. * We are not using `af_angle_atan' anymore, but we keep the source
  66. * code below just in case...
  67. */
  68. #if 0
  69. /*
  70. * The trick here is to realize that we don't need a very accurate angle
  71. * approximation. We are going to use the result of `af_angle_atan' to
  72. * only compare the sign of angle differences, or check whether its
  73. * magnitude is very small.
  74. *
  75. * The approximation
  76. *
  77. * dy * PI / (|dx|+|dy|)
  78. *
  79. * should be enough, and much faster to compute.
  80. */
  81. FT_LOCAL_DEF( AF_Angle )
  82. af_angle_atan( FT_Fixed dx,
  83. FT_Fixed dy )
  84. {
  85. AF_Angle angle;
  86. FT_Fixed ax = dx;
  87. FT_Fixed ay = dy;
  88. if ( ax < 0 )
  89. ax = -ax;
  90. if ( ay < 0 )
  91. ay = -ay;
  92. ax += ay;
  93. if ( ax == 0 )
  94. angle = 0;
  95. else
  96. {
  97. angle = ( AF_ANGLE_PI2 * dy ) / ( ax + ay );
  98. if ( dx < 0 )
  99. {
  100. if ( angle >= 0 )
  101. angle = AF_ANGLE_PI - angle;
  102. else
  103. angle = -AF_ANGLE_PI - angle;
  104. }
  105. }
  106. return angle;
  107. }
  108. #elif 0
  109. /* the following table has been automatically generated with */
  110. /* the `mather.py' Python script */
  111. #define AF_ATAN_BITS 8
  112. static const FT_Byte af_arctan[1L << AF_ATAN_BITS] =
  113. {
  114. 0, 0, 1, 1, 1, 2, 2, 2,
  115. 3, 3, 3, 3, 4, 4, 4, 5,
  116. 5, 5, 6, 6, 6, 7, 7, 7,
  117. 8, 8, 8, 9, 9, 9, 10, 10,
  118. 10, 10, 11, 11, 11, 12, 12, 12,
  119. 13, 13, 13, 14, 14, 14, 14, 15,
  120. 15, 15, 16, 16, 16, 17, 17, 17,
  121. 18, 18, 18, 18, 19, 19, 19, 20,
  122. 20, 20, 21, 21, 21, 21, 22, 22,
  123. 22, 23, 23, 23, 24, 24, 24, 24,
  124. 25, 25, 25, 26, 26, 26, 26, 27,
  125. 27, 27, 28, 28, 28, 28, 29, 29,
  126. 29, 30, 30, 30, 30, 31, 31, 31,
  127. 31, 32, 32, 32, 33, 33, 33, 33,
  128. 34, 34, 34, 34, 35, 35, 35, 35,
  129. 36, 36, 36, 36, 37, 37, 37, 38,
  130. 38, 38, 38, 39, 39, 39, 39, 40,
  131. 40, 40, 40, 41, 41, 41, 41, 42,
  132. 42, 42, 42, 42, 43, 43, 43, 43,
  133. 44, 44, 44, 44, 45, 45, 45, 45,
  134. 46, 46, 46, 46, 46, 47, 47, 47,
  135. 47, 48, 48, 48, 48, 48, 49, 49,
  136. 49, 49, 50, 50, 50, 50, 50, 51,
  137. 51, 51, 51, 51, 52, 52, 52, 52,
  138. 52, 53, 53, 53, 53, 53, 54, 54,
  139. 54, 54, 54, 55, 55, 55, 55, 55,
  140. 56, 56, 56, 56, 56, 57, 57, 57,
  141. 57, 57, 57, 58, 58, 58, 58, 58,
  142. 59, 59, 59, 59, 59, 59, 60, 60,
  143. 60, 60, 60, 61, 61, 61, 61, 61,
  144. 61, 62, 62, 62, 62, 62, 62, 63,
  145. 63, 63, 63, 63, 63, 64, 64, 64
  146. };
  147. FT_LOCAL_DEF( AF_Angle )
  148. af_angle_atan( FT_Fixed dx,
  149. FT_Fixed dy )
  150. {
  151. AF_Angle angle;
  152. /* check trivial cases */
  153. if ( dy == 0 )
  154. {
  155. angle = 0;
  156. if ( dx < 0 )
  157. angle = AF_ANGLE_PI;
  158. return angle;
  159. }
  160. else if ( dx == 0 )
  161. {
  162. angle = AF_ANGLE_PI2;
  163. if ( dy < 0 )
  164. angle = -AF_ANGLE_PI2;
  165. return angle;
  166. }
  167. angle = 0;
  168. if ( dx < 0 )
  169. {
  170. dx = -dx;
  171. dy = -dy;
  172. angle = AF_ANGLE_PI;
  173. }
  174. if ( dy < 0 )
  175. {
  176. FT_Pos tmp;
  177. tmp = dx;
  178. dx = -dy;
  179. dy = tmp;
  180. angle -= AF_ANGLE_PI2;
  181. }
  182. if ( dx == 0 && dy == 0 )
  183. return 0;
  184. if ( dx == dy )
  185. angle += AF_ANGLE_PI4;
  186. else if ( dx > dy )
  187. angle += af_arctan[FT_DivFix( dy, dx ) >> ( 16 - AF_ATAN_BITS )];
  188. else
  189. angle += AF_ANGLE_PI2 -
  190. af_arctan[FT_DivFix( dx, dy ) >> ( 16 - AF_ATAN_BITS )];
  191. if ( angle > AF_ANGLE_PI )
  192. angle -= AF_ANGLE_2PI;
  193. return angle;
  194. }
  195. #endif /* 0 */
  196. FT_LOCAL_DEF( void )
  197. af_sort_pos( FT_UInt count,
  198. FT_Pos* table )
  199. {
  200. FT_UInt i, j;
  201. FT_Pos swap;
  202. for ( i = 1; i < count; i++ )
  203. {
  204. for ( j = i; j > 0; j-- )
  205. {
  206. if ( table[j] > table[j - 1] )
  207. break;
  208. swap = table[j];
  209. table[j] = table[j - 1];
  210. table[j - 1] = swap;
  211. }
  212. }
  213. }
  214. FT_LOCAL_DEF( void )
  215. af_sort_widths( FT_UInt count,
  216. AF_Width table )
  217. {
  218. FT_UInt i, j;
  219. AF_WidthRec swap;
  220. for ( i = 1; i < count; i++ )
  221. {
  222. for ( j = i; j > 0; j-- )
  223. {
  224. if ( table[j].org > table[j - 1].org )
  225. break;
  226. swap = table[j];
  227. table[j] = table[j - 1];
  228. table[j - 1] = swap;
  229. }
  230. }
  231. }
  232. /* END */