aboutsummaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/FontExtrusion.java
blob: d32615407a778cceccd90495829647c2609c9ad5 (plain)
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
/*
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package javax.media.j3d;
import java.awt.Shape;
import java.awt.geom.PathIterator;
import java.util.ArrayList;

import javax.vecmath.Point2f;

    /**
     * The FontExtrusion object is used to describe the extrusion path
     * for a Font3D object.  The extrusion path is used in conjunction
     * with a Font2D object.  The extrusion path defines the edge contour
     * of 3D text.  This contour is perpendicular to the face of the text.
     * The extrusion has it's origin at the edge of the glyph with 1.0 being
     * the height of the tallest glyph. Contour must be monotonic in x.
     * <P>
     * The shape of the extrusion path is, by default, a straight line
     * from 0.0 to 0.2 (known as a straight bevel). The shape may be
     * modified via the extrusionShape parameter, a Shape object that
     * describes the 3D contour of a Font3D object.
     * <P>
     * User is responsible for data sanity and must make sure that
     * extrusionShape does not cause intersection of adjacent glyphs
     * or within single glyph. Else undefined output may be generated.
     *
     * @see java.awt.Font
     * @see Font3D
     */
public class FontExtrusion extends Object {

  // Default FontExtrusion is a straight line of length .2
  float length = 0.2f;
  Shape shape;
  Point2f [] pnts;

    double tessellationTolerance = 0.01;

    /**
     * Constructs a FontExtrusion object with default parameters.  The
     * default parameters are as follows:
     *
     * <ul>
     * extrusion shape : null<br>
     * tessellation tolerance : 0.01<br>
     * </ul>
     *
     * A null extrusion shape specifies that a straight line from 0.0
     * to 0.2 (straight bevel) is used.
     *
     * @see Font3D
     */
    public FontExtrusion() {
      shape = null;
    }

    /**
     * Constructs a FontExtrusion object with the specified shape, using
     * the default tessellation tolerance.  The
     * specified shape is used to construct the edge
     * contour of a Font3D object.  Each shape begins with an implicit
     * point at 0.0. Contour must be monotonic in x.
     *
     * @param extrusionShape the shape object to use to generate the
     * extrusion path.
     * A null shape specifies that a straight line from 0.0 to 0.2
     * (straight bevel) is used.
     *
     * @exception IllegalArgumentException if multiple contours in
     * extrusionShape, or contour is not monotonic or least x-value
     * of a contour point is not 0.0f
     *
     * @see Font3D
     */
    public FontExtrusion(Shape extrusionShape) {
      setExtrusionShape(extrusionShape);
    }


    /**
     * Constructs a FontExtrusion object with the specified shape, using
     * the specified tessellation tolerance.  The
     * specified shape is used to construct the edge
     * contour of a Font3D object.  Each shape begins with an implicit
     * point at 0.0. Contour must be monotonic in x.
     *
     * @param extrusionShape the shape object to use to generate the
     * extrusion path.
     * A null shape specifies that a straight line from 0.0 to 0.2
     * (straight bevel) is used.
     * @param tessellationTolerance the tessellation tolerance value
     * used in tessellating the extrusion shape.
     * This corresponds to the <code>flatness</code> parameter in
     * the <code>java.awt.Shape.getPathIterator</code> method.
     *
     * @exception IllegalArgumentException if multiple contours in
     * extrusionShape, or contour is not monotonic or least x-value
     * of a contour point is not 0.0f
     *
     * @see Font3D
     *
     * @since Java 3D 1.2
     */
    public FontExtrusion(Shape extrusionShape,
			 double tessellationTolerance) {

	this.tessellationTolerance = tessellationTolerance;
	setExtrusionShape(extrusionShape);
    }


    /**
     * Sets the FontExtrusion's shape parameter.  This
     * parameter is used to construct the 3D contour of a Font3D object.
     *
     * @param extrusionShape the shape object to use to generate the
     * extrusion path.
     * A null shape specifies that a straight line from 0.0 to 0.2
     * (straight bevel) is used.
     *
     * @exception IllegalArgumentException if multiple contours in
     * extrusionShape, or contour is not monotonic or least x-value
     * of a contour point is not 0.0f
     *
     * @see Font3D
     * @see java.awt.Shape
     */
    public void setExtrusionShape(Shape extrusionShape) {
	shape = extrusionShape;
	if (shape == null) return;

	PathIterator pIt = shape.getPathIterator(null, tessellationTolerance);
	ArrayList  coords = new ArrayList();
	float tmpCoords[] = new float[6], prevX = 0.0f;
	int flag, n = 0, inc = -1;

	// Extrusion shape is restricted to be single contour, monotonous
	// increasing, non-self-intersecting curve. Throw exception otherwise
	while (!pIt.isDone()) {
	   Point2f vertex = new Point2f();
	   flag = pIt.currentSegment(tmpCoords);
	   if (flag == PathIterator.SEG_LINETO){
	     vertex.x = tmpCoords[0];
	     vertex.y = tmpCoords[1];
	     if (inc == -1){
	        if (prevX < vertex.x) inc = 0;
		else if (prevX > vertex.x) inc = 1;
	     }
	     //Flag 'inc' indicates if curve is monotonic increasing or
	     // monotonic decreasing. It is set to -1 initially and remains
	     // -1 if consecutive x values are same. Once 'inc' is set to
	     // 1 or 0, exception is thrown is curve changes direction.
	     if (((inc == 0) && (prevX > vertex.x)) ||
		 ((inc == 1) && (prevX < vertex.x)))
		 throw new IllegalArgumentException(J3dI18N.getString("FontExtrusion0"));

	     prevX = vertex.x;
	     n++;
	     coords.add(vertex);
	   }else if (flag == PathIterator.SEG_MOVETO){
	     if (n != 0)
		throw new IllegalArgumentException(J3dI18N.getString("FontExtrusion3"));

	     vertex.x = tmpCoords[0];
	     vertex.y = tmpCoords[1];
	     prevX = vertex.x;
	     n++;
	     coords.add(vertex);
	   }
	   pIt.next();
	}

	//if (inc == 1){
	//Point2f vertex = new Point2f(0.0f, 0.0f);
	//coords.add(vertex);
	//}
	int i, num = coords.size();
	pnts = new Point2f[num];
	//System.err.println("num "+num+" inc "+inc);
	if (inc == 0){
	  for (i=0;i < num;i++){
		pnts[i] = (Point2f)coords.get(i);
		//System.err.println("i "+i+" x "+ pnts[i].x+" y "+pnts[i].y);
	  }
	}
	else {
	  for (i=0;i < num;i++) {
		pnts[i] = (Point2f)coords.get(num - i -1);
		//System.err.println("i "+i+" x "+ pnts[i].x+" y "+pnts[i].y);
	  }
	}

	//Force last y to be zero until Text3D face scaling is implemented
	pnts[num-1].y = 0.0f;
	if (pnts[0].x != 0.0f)
	  throw new IllegalArgumentException(J3dI18N.getString("FontExtrusion1"));

	//Compute straight line distance between first and last points.
	float dx = (pnts[0].x - pnts[num-1].x);
	float dy = (pnts[0].y - pnts[num-1].y);
	length = (float)Math.sqrt(dx*dx + dy*dy);
    }


    /**
     * Gets the FontExtrusion's shape parameter.  This
     * parameter is used to construct the 3D contour of a Font3D object.
     *
     * @return extrusionShape the shape object used to generate the
     *  extrusion path
     *
     * @see Font3D
     * @see java.awt.Shape
     */
    public Shape getExtrusionShape() {
      return shape;
    }


    /**
     * Returns the tessellation tolerance with which this FontExtrusion was
     * created.
     * @return the tessellation tolerance used by this FontExtrusion
     *
     * @since Java 3D 1.2
     */
    public double getTessellationTolerance() {
	return tessellationTolerance;
    }

}