1 /* Utils.java -- A collection of utility functions for the autofitter
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.java
.awt
.font
.autofit
;
41 import gnu
.java
.awt
.font
.opentype
.truetype
.Fixed
;
44 * A collection of utility methods used all around the auto fitter.
50 private static final int ATAN_BITS
= 8;
51 private static final byte[] ATAN
= new byte[]
53 0, 0, 1, 1, 1, 2, 2, 2,
54 3, 3, 3, 3, 4, 4, 4, 5,
55 5, 5, 6, 6, 6, 7, 7, 7,
56 8, 8, 8, 9, 9, 9, 10, 10,
57 10, 10, 11, 11, 11, 12, 12, 12,
58 13, 13, 13, 14, 14, 14, 14, 15,
59 15, 15, 16, 16, 16, 17, 17, 17,
60 18, 18, 18, 18, 19, 19, 19, 20,
61 20, 20, 21, 21, 21, 21, 22, 22,
62 22, 23, 23, 23, 24, 24, 24, 24,
63 25, 25, 25, 26, 26, 26, 26, 27,
64 27, 27, 28, 28, 28, 28, 29, 29,
65 29, 30, 30, 30, 30, 31, 31, 31,
66 31, 32, 32, 32, 33, 33, 33, 33,
67 34, 34, 34, 34, 35, 35, 35, 35,
68 36, 36, 36, 36, 37, 37, 37, 38,
69 38, 38, 38, 39, 39, 39, 39, 40,
70 40, 40, 40, 41, 41, 41, 41, 42,
71 42, 42, 42, 42, 43, 43, 43, 43,
72 44, 44, 44, 44, 45, 45, 45, 45,
73 46, 46, 46, 46, 46, 47, 47, 47,
74 47, 48, 48, 48, 48, 48, 49, 49,
75 49, 49, 50, 50, 50, 50, 50, 51,
76 51, 51, 51, 51, 52, 52, 52, 52,
77 52, 53, 53, 53, 53, 53, 54, 54,
78 54, 54, 54, 55, 55, 55, 55, 55,
79 56, 56, 56, 56, 56, 57, 57, 57,
80 57, 57, 57, 58, 58, 58, 58, 58,
81 59, 59, 59, 59, 59, 59, 60, 60,
82 60, 60, 60, 61, 61, 61, 61, 61,
83 61, 62, 62, 62, 62, 62, 62, 63,
84 63, 63, 63, 63, 63, 64, 64, 64
87 private static final int ANGLE_PI
= 256;
88 private static final int ANGLE_PI2
= ANGLE_PI
/ 2;
89 private static final int ANGLE_PI4
= ANGLE_PI
/ 4;
90 private static final int ANGLE_2PI
= ANGLE_PI
* 2;
93 * Computes the direction constant for the specified vector. The vector is
94 * given as differential value already.
96 * @param dx the x vector
97 * @param dy the y vector
99 * @return the direction of that vector, or DIR_NONE, if that vector is not
100 * approximating against one of the major axises
102 static int computeDirection(int dx
, int dy
)
111 else if (-dy
* 12 < -dx
)
118 else if (dy
* 12 < -dx
)
128 else if (-dy
* 12 < dx
)
135 else if (dy
* 12 < dx
)
142 public static int atan(int dx
, int dy
)
176 if (dx
== 0 && dy
== 0)
183 angle
+= ATAN
[Fixed
.div(dy
, dx
) << (ATAN_BITS
- 6)];
187 angle
+= ANGLE_PI2
- ATAN
[Fixed
.div(dx
, dy
) << (ATAN_BITS
- 6)];
190 if (angle
> ANGLE_PI
)
195 public static int angleDiff(int ang1
, int ang2
)
197 int delta
= ang2
- ang1
;
201 if (delta
> ANGLE_PI
)
206 static void sort(int num
, int[] array
)
209 for (int i
= 1; i
< num
; i
++)
211 for (int j
= i
; j
> 0; j
--)
213 if (array
[j
] > array
[j
- 1])
216 array
[j
] = array
[j
- 1];
222 static void sort(int num
, Width
[] array
)
225 for (int i
= 1; i
< num
; i
++)
227 for (int j
= 1; j
> 0; j
--)
229 if (array
[j
].org
> array
[j
- 1].org
)
232 array
[j
] = array
[j
- 1];
238 static int pixRound(int val
)
240 return pixFloor(val
+ 32);
243 static int pixFloor(int val
)
248 public static int mulDiv(int a
, int b
, int c
)
251 long div
= (prod
/ c
);