summaryrefslogblamecommitdiff
path: root/src/static_libs/freetype/sw_ft_math.h
blob: 95a5c4257ef299fcfb456d3cc3416d0939c34ede (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438





















































































































































































































































































































































































































































                                                                              
#ifndef SW_FT_MATH_H
#define SW_FT_MATH_H

/***************************************************************************/
/*                                                                         */
/*  fttrigon.h                                                             */
/*                                                                         */
/*    FreeType trigonometric functions (specification).                    */
/*                                                                         */
/*  Copyright 2001, 2003, 2005, 2007, 2013 by                              */
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
/*                                                                         */
/*  This file is part of the FreeType project, and may only be used,       */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/

#include "sw_ft_types.h"


/*************************************************************************/
/*                                                                       */
/* The min and max functions missing in C.  As usual, be careful not to  */
/* write things like SW_FT_MIN( a++, b++ ) to avoid side effects.           */
/*                                                                       */
#define SW_FT_MIN( a, b )  ( (a) < (b) ? (a) : (b) )
#define SW_FT_MAX( a, b )  ( (a) > (b) ? (a) : (b) )

#define SW_FT_ABS( a )     ( (a) < 0 ? -(a) : (a) )

/*
 *  Approximate sqrt(x*x+y*y) using the `alpha max plus beta min'
 *  algorithm.  We use alpha = 1, beta = 3/8, giving us results with a
 *  largest error less than 7% compared to the exact value.
 */
#define SW_FT_HYPOT( x, y )                 \
        ( x = SW_FT_ABS( x ),             \
          y = SW_FT_ABS( y ),             \
          x > y ? x + ( 3 * y >> 3 )   \
                : y + ( 3 * x >> 3 ) )

/*************************************************************************/
/*                                                                       */
/* <Function>                                                            */
/*    SW_FT_MulFix                                                          */
/*                                                                       */
/* <Description>                                                         */
/*    A very simple function used to perform the computation             */
/*    `(a*b)/0x10000' with maximum accuracy.  Most of the time this is   */
/*    used to multiply a given value by a 16.16 fixed-point factor.      */
/*                                                                       */
/* <Input>                                                               */
/*    a :: The first multiplier.                                         */
/*    b :: The second multiplier.  Use a 16.16 factor here whenever      */
/*         possible (see note below).                                    */
/*                                                                       */
/* <Return>                                                              */
/*    The result of `(a*b)/0x10000'.                                     */
/*                                                                       */
/* <Note>                                                                */
/*    This function has been optimized for the case where the absolute   */
/*    value of `a' is less than 2048, and `b' is a 16.16 scaling factor. */
/*    As this happens mainly when scaling from notional units to         */
/*    fractional pixels in FreeType, it resulted in noticeable speed     */
/*    improvements between versions 2.x and 1.x.                         */
/*                                                                       */
/*    As a conclusion, always try to place a 16.16 factor as the         */
/*    _second_ argument of this function; this can make a great          */
/*    difference.                                                        */
/*                                                                       */
SW_FT_Long
SW_FT_MulFix( SW_FT_Long  a,
           SW_FT_Long  b );

/*************************************************************************/
/*                                                                       */
/* <Function>                                                            */
/*    SW_FT_MulDiv                                                          */
/*                                                                       */
/* <Description>                                                         */
/*    A very simple function used to perform the computation `(a*b)/c'   */
/*    with maximum accuracy (it uses a 64-bit intermediate integer       */
/*    whenever necessary).                                               */
/*                                                                       */
/*    This function isn't necessarily as fast as some processor specific */
/*    operations, but is at least completely portable.                   */
/*                                                                       */
/* <Input>                                                               */
/*    a :: The first multiplier.                                         */
/*    b :: The second multiplier.                                        */
/*    c :: The divisor.                                                  */
/*                                                                       */
/* <Return>                                                              */
/*    The result of `(a*b)/c'.  This function never traps when trying to */
/*    divide by zero; it simply returns `MaxInt' or `MinInt' depending   */
/*    on the signs of `a' and `b'.                                       */
/*                                                                       */
SW_FT_Long
SW_FT_MulDiv( SW_FT_Long  a,
           SW_FT_Long  b,
           SW_FT_Long  c );

/*************************************************************************/
/*                                                                       */
/* <Function>                                                            */
/*    SW_FT_DivFix                                                          */
/*                                                                       */
/* <Description>                                                         */
/*    A very simple function used to perform the computation             */
/*    `(a*0x10000)/b' with maximum accuracy.  Most of the time, this is  */
/*    used to divide a given value by a 16.16 fixed-point factor.        */
/*                                                                       */
/* <Input>                                                               */
/*    a :: The numerator.                                                */
/*    b :: The denominator.  Use a 16.16 factor here.                    */
/*                                                                       */
/* <Return>                                                              */
/*    The result of `(a*0x10000)/b'.                                     */
/*                                                                       */
SW_FT_Long
SW_FT_DivFix( SW_FT_Long  a,
           SW_FT_Long  b );



  /*************************************************************************/
  /*                                                                       */
  /* <Section>                                                             */
  /*   computations                                                        */
  /*                                                                       */
  /*************************************************************************/


  /*************************************************************************
   *
   * @type:
   *   SW_FT_Angle
   *
   * @description:
   *   This type is used to model angle values in FreeType.  Note that the
   *   angle is a 16.16 fixed-point value expressed in degrees.
   *
   */
  typedef SW_FT_Fixed  SW_FT_Angle;


  /*************************************************************************
   *
   * @macro:
   *   SW_FT_ANGLE_PI
   *
   * @description:
   *   The angle pi expressed in @SW_FT_Angle units.
   *
   */
#define SW_FT_ANGLE_PI  ( 180L << 16 )


  /*************************************************************************
   *
   * @macro:
   *   SW_FT_ANGLE_2PI
   *
   * @description:
   *   The angle 2*pi expressed in @SW_FT_Angle units.
   *
   */
#define SW_FT_ANGLE_2PI  ( SW_FT_ANGLE_PI * 2 )


  /*************************************************************************
   *
   * @macro:
   *   SW_FT_ANGLE_PI2
   *
   * @description:
   *   The angle pi/2 expressed in @SW_FT_Angle units.
   *
   */
#define SW_FT_ANGLE_PI2  ( SW_FT_ANGLE_PI / 2 )


  /*************************************************************************
   *
   * @macro:
   *   SW_FT_ANGLE_PI4
   *
   * @description:
   *   The angle pi/4 expressed in @SW_FT_Angle units.
   *
   */
#define SW_FT_ANGLE_PI4  ( SW_FT_ANGLE_PI / 4 )


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Sin
   *
   * @description:
   *   Return the sinus of a given angle in fixed-point format.
   *
   * @input:
   *   angle ::
   *     The input angle.
   *
   * @return:
   *   The sinus value.
   *
   * @note:
   *   If you need both the sinus and cosinus for a given angle, use the
   *   function @SW_FT_Vector_Unit.
   *
   */
  SW_FT_Fixed
  SW_FT_Sin( SW_FT_Angle  angle );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Cos
   *
   * @description:
   *   Return the cosinus of a given angle in fixed-point format.
   *
   * @input:
   *   angle ::
   *     The input angle.
   *
   * @return:
   *   The cosinus value.
   *
   * @note:
   *   If you need both the sinus and cosinus for a given angle, use the
   *   function @SW_FT_Vector_Unit.
   *
   */
  SW_FT_Fixed
  SW_FT_Cos( SW_FT_Angle  angle );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Tan
   *
   * @description:
   *   Return the tangent of a given angle in fixed-point format.
   *
   * @input:
   *   angle ::
   *     The input angle.
   *
   * @return:
   *   The tangent value.
   *
   */
  SW_FT_Fixed
  SW_FT_Tan( SW_FT_Angle  angle );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Atan2
   *
   * @description:
   *   Return the arc-tangent corresponding to a given vector (x,y) in
   *   the 2d plane.
   *
   * @input:
   *   x ::
   *     The horizontal vector coordinate.
   *
   *   y ::
   *     The vertical vector coordinate.
   *
   * @return:
   *   The arc-tangent value (i.e. angle).
   *
   */
  SW_FT_Angle
  SW_FT_Atan2( SW_FT_Fixed  x,
            SW_FT_Fixed  y );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Angle_Diff
   *
   * @description:
   *   Return the difference between two angles.  The result is always
   *   constrained to the ]-PI..PI] interval.
   *
   * @input:
   *   angle1 ::
   *     First angle.
   *
   *   angle2 ::
   *     Second angle.
   *
   * @return:
   *   Constrained value of `value2-value1'.
   *
   */
  SW_FT_Angle
  SW_FT_Angle_Diff( SW_FT_Angle  angle1,
                 SW_FT_Angle  angle2 );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Vector_Unit
   *
   * @description:
   *   Return the unit vector corresponding to a given angle.  After the
   *   call, the value of `vec.x' will be `sin(angle)', and the value of
   *   `vec.y' will be `cos(angle)'.
   *
   *   This function is useful to retrieve both the sinus and cosinus of a
   *   given angle quickly.
   *
   * @output:
   *   vec ::
   *     The address of target vector.
   *
   * @input:
   *   angle ::
   *     The input angle.
   *
   */
  void
  SW_FT_Vector_Unit( SW_FT_Vector*  vec,
                  SW_FT_Angle    angle );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Vector_Rotate
   *
   * @description:
   *   Rotate a vector by a given angle.
   *
   * @inout:
   *   vec ::
   *     The address of target vector.
   *
   * @input:
   *   angle ::
   *     The input angle.
   *
   */
  void
  SW_FT_Vector_Rotate( SW_FT_Vector*  vec,
                    SW_FT_Angle    angle );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Vector_Length
   *
   * @description:
   *   Return the length of a given vector.
   *
   * @input:
   *   vec ::
   *     The address of target vector.
   *
   * @return:
   *   The vector length, expressed in the same units that the original
   *   vector coordinates.
   *
   */
  SW_FT_Fixed
  SW_FT_Vector_Length( SW_FT_Vector*  vec );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Vector_Polarize
   *
   * @description:
   *   Compute both the length and angle of a given vector.
   *
   * @input:
   *   vec ::
   *     The address of source vector.
   *
   * @output:
   *   length ::
   *     The vector length.
   *
   *   angle ::
   *     The vector angle.
   *
   */
  void
  SW_FT_Vector_Polarize( SW_FT_Vector*  vec,
                      SW_FT_Fixed   *length,
                      SW_FT_Angle   *angle );


  /*************************************************************************
   *
   * @function:
   *   SW_FT_Vector_From_Polar
   *
   * @description:
   *   Compute vector coordinates from a length and angle.
   *
   * @output:
   *   vec ::
   *     The address of source vector.
   *
   * @input:
   *   length ::
   *     The vector length.
   *
   *   angle ::
   *     The vector angle.
   *
   */
  void
  SW_FT_Vector_From_Polar( SW_FT_Vector*  vec,
                        SW_FT_Fixed    length,
                        SW_FT_Angle    angle );


#endif // SW_FT_MATH_H