re PR libgcj/37636 (java tools are unable to find resource files)
[gcc.git] / libjava / classpath / gnu / java / awt / font / autofit / Utils.java
1 /* Utils.java -- A collection of utility functions for the autofitter
2 Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38
39 package gnu.java.awt.font.autofit;
40
41 import gnu.java.awt.font.opentype.truetype.Fixed;
42
43 /**
44 * A collection of utility methods used all around the auto fitter.
45 */
46 class Utils
47 implements Constants
48 {
49
50 private static final int ATAN_BITS = 8;
51 private static final byte[] ATAN = new byte[]
52 {
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
85 };
86
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;
91
92 /**
93 * Computes the direction constant for the specified vector. The vector is
94 * given as differential value already.
95 *
96 * @param dx the x vector
97 * @param dy the y vector
98 *
99 * @return the direction of that vector, or DIR_NONE, if that vector is not
100 * approximating against one of the major axises
101 */
102 static int computeDirection(int dx, int dy)
103 {
104 int dir = DIR_NONE;
105 if (dx < 0)
106 {
107 if (dy < 0)
108 {
109 if (-dx * 12 < -dy)
110 dir = DIR_UP;
111 else if (-dy * 12 < -dx)
112 dir = DIR_LEFT;
113 }
114 else // dy >= 0 .
115 {
116 if (-dx * 12 < dy)
117 dir = DIR_DOWN;
118 else if (dy * 12 < -dx)
119 dir = DIR_LEFT;
120 }
121 }
122 else // dx >= 0 .
123 {
124 if (dy < 0)
125 {
126 if (dx * 12 < -dy)
127 dir = DIR_UP;
128 else if (-dy * 12 < dx)
129 dir = DIR_RIGHT;
130 }
131 else // dy >= 0 .
132 {
133 if (dx * 12 < dy)
134 dir = DIR_DOWN;
135 else if (dy * 12 < dx)
136 dir = DIR_RIGHT;
137 }
138 }
139 return dir;
140 }
141
142 public static int atan(int dx, int dy)
143 {
144 int angle;
145 // Trivial cases.
146 if (dy == 0)
147 {
148 angle = 0;
149 if (dx < 0)
150 angle = ANGLE_PI;
151 return angle;
152 }
153 else if (dx == 0)
154 {
155 angle = ANGLE_PI2;
156 if (dy < 0)
157 angle = - ANGLE_PI2;
158 return angle;
159 }
160
161
162 angle = 0;
163 if (dx < 0)
164 {
165 dx = -dx;
166 dy = -dy;
167 angle = ANGLE_PI;
168 }
169 if (dy < 0)
170 {
171 int tmp = dx;
172 dx = -dy;
173 dy = tmp;
174 angle -= ANGLE_PI2;
175 }
176 if (dx == 0 && dy == 0)
177 return 0;
178
179 if (dx == dy)
180 angle += ANGLE_PI4;
181 else if (dx > dy)
182 {
183 angle += ATAN[Fixed.div(dy, dx) << (ATAN_BITS - 6)];
184 }
185 else
186 {
187 angle += ANGLE_PI2 - ATAN[Fixed.div(dx, dy) << (ATAN_BITS - 6)];
188 }
189
190 if (angle > ANGLE_PI)
191 angle -= ANGLE_2PI;
192 return angle;
193 }
194
195 public static int angleDiff(int ang1, int ang2)
196 {
197 int delta = ang2 - ang1;
198 delta %= ANGLE_2PI;
199 if (delta < 0)
200 delta += ANGLE_2PI;
201 if (delta > ANGLE_PI)
202 delta -= ANGLE_2PI;
203 return delta;
204 }
205
206 static void sort(int num, int[] array)
207 {
208 int swap;
209 for (int i = 1; i < num; i++)
210 {
211 for (int j = i; j > 0; j--)
212 {
213 if (array[j] > array[j - 1])
214 break;
215 swap = array[j];
216 array[j] = array[j - 1];
217 array[j - 1] = swap;
218 }
219 }
220 }
221
222 static void sort(int num, Width[] array)
223 {
224 Width swap;
225 for (int i = 1; i < num; i++)
226 {
227 for (int j = 1; j > 0; j--)
228 {
229 if (array[j].org > array[j - 1].org)
230 break;
231 swap = array[j];
232 array[j] = array[j - 1];
233 array[j - 1] = swap;
234 }
235 }
236 }
237
238 static int pixRound(int val)
239 {
240 return pixFloor(val + 32);
241 }
242
243 static int pixFloor(int val)
244 {
245 return val & ~63;
246 }
247
248 public static int mulDiv(int a, int b, int c)
249 {
250 long prod = a * b;
251 long div = (prod / c);
252 return (int) div;
253 }
254
255 }