001: /*
002: * $RCSfile: FontExtrusion.java,v $
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:21 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.*;
035: import java.lang.Math;
036: import java.awt.Shape;
037: import java.awt.geom.PathIterator;
038: import java.util.ArrayList;
039:
040: /**
041: * The FontExtrusion object is used to describe the extrusion path
042: * for a Font3D object. The extrusion path is used in conjunction
043: * with a Font2D object. The extrusion path defines the edge contour
044: * of 3D text. This contour is perpendicular to the face of the text.
045: * The extrusion has it's origin at the edge of the glyph with 1.0 being
046: * the height of the tallest glyph. Contour must be monotonic in x.
047: * <P>
048: * The shape of the extrusion path is, by default, a straight line
049: * from 0.0 to 0.2 (known as a straight bevel). The shape may be
050: * modified via the extrusionShape parameter, a Shape object that
051: * describes the 3D contour of a Font3D object.
052: * <P>
053: * User is responsible for data sanity and must make sure that
054: * extrusionShape does not cause intersection of adjacent glyphs
055: * or within single glyph. Else undefined output may be generated.
056: *
057: * @see java.awt.Font
058: * @see Font3D
059: */
060: public class FontExtrusion extends Object {
061:
062: // Default FontExtrusion is a straight line of length .2
063: float length = 0.2f;
064: Shape shape;
065: Point2f[] pnts;
066:
067: double tessellationTolerance = 0.01;
068:
069: /**
070: * Constructs a FontExtrusion object with default parameters. The
071: * default parameters are as follows:
072: *
073: * <ul>
074: * extrusion shape : null<br>
075: * tessellation tolerance : 0.01<br>
076: * </ul>
077: *
078: * A null extrusion shape specifies that a straight line from 0.0
079: * to 0.2 (straight bevel) is used.
080: *
081: * @see Font3D
082: */
083: public FontExtrusion() {
084: shape = null;
085: }
086:
087: /**
088: * Constructs a FontExtrusion object with the specified shape, using
089: * the default tessellation tolerance. The
090: * specified shape is used to construct the edge
091: * contour of a Font3D object. Each shape begins with an implicit
092: * point at 0.0. Contour must be monotonic in x.
093: *
094: * @param extrusionShape the shape object to use to generate the
095: * extrusion path.
096: * A null shape specifies that a straight line from 0.0 to 0.2
097: * (straight bevel) is used.
098: *
099: * @exception IllegalArgumentException if multiple contours in
100: * extrusionShape, or contour is not monotonic or least x-value
101: * of a contour point is not 0.0f
102: *
103: * @see Font3D
104: */
105: public FontExtrusion(Shape extrusionShape) {
106: setExtrusionShape(extrusionShape);
107: }
108:
109: /**
110: * Constructs a FontExtrusion object with the specified shape, using
111: * the specified tessellation tolerance. The
112: * specified shape is used to construct the edge
113: * contour of a Font3D object. Each shape begins with an implicit
114: * point at 0.0. Contour must be monotonic in x.
115: *
116: * @param extrusionShape the shape object to use to generate the
117: * extrusion path.
118: * A null shape specifies that a straight line from 0.0 to 0.2
119: * (straight bevel) is used.
120: * @param tessellationTolerance the tessellation tolerance value
121: * used in tessellating the extrusion shape.
122: * This corresponds to the <code>flatness</code> parameter in
123: * the <code>java.awt.Shape.getPathIterator</code> method.
124: *
125: * @exception IllegalArgumentException if multiple contours in
126: * extrusionShape, or contour is not monotonic or least x-value
127: * of a contour point is not 0.0f
128: *
129: * @see Font3D
130: *
131: * @since Java 3D 1.2
132: */
133: public FontExtrusion(Shape extrusionShape,
134: double tessellationTolerance) {
135:
136: this .tessellationTolerance = tessellationTolerance;
137: setExtrusionShape(extrusionShape);
138: }
139:
140: /**
141: * Sets the FontExtrusion's shape parameter. This
142: * parameter is used to construct the 3D contour of a Font3D object.
143: *
144: * @param extrusionShape the shape object to use to generate the
145: * extrusion path.
146: * A null shape specifies that a straight line from 0.0 to 0.2
147: * (straight bevel) is used.
148: *
149: * @exception IllegalArgumentException if multiple contours in
150: * extrusionShape, or contour is not monotonic or least x-value
151: * of a contour point is not 0.0f
152: *
153: * @see Font3D
154: * @see java.awt.Shape
155: */
156: public void setExtrusionShape(Shape extrusionShape) {
157: shape = extrusionShape;
158: if (shape == null)
159: return;
160:
161: PathIterator pIt = shape.getPathIterator(null,
162: tessellationTolerance);
163: ArrayList coords = new ArrayList();
164: float tmpCoords[] = new float[6], prevX = 0.0f;
165: int flag, n = 0, inc = -1;
166:
167: // Extrusion shape is restricted to be single contour, monotonous
168: // increasing, non-self-intersecting curve. Throw exception otherwise
169: while (!pIt.isDone()) {
170: Point2f vertex = new Point2f();
171: flag = pIt.currentSegment(tmpCoords);
172: if (flag == PathIterator.SEG_LINETO) {
173: vertex.x = tmpCoords[0];
174: vertex.y = tmpCoords[1];
175: if (inc == -1) {
176: if (prevX < vertex.x)
177: inc = 0;
178: else if (prevX > vertex.x)
179: inc = 1;
180: }
181: //Flag 'inc' indicates if curve is monotonic increasing or
182: // monotonic decreasing. It is set to -1 initially and remains
183: // -1 if consecutive x values are same. Once 'inc' is set to
184: // 1 or 0, exception is thrown is curve changes direction.
185: if (((inc == 0) && (prevX > vertex.x))
186: || ((inc == 1) && (prevX < vertex.x)))
187: throw new IllegalArgumentException(J3dI18N
188: .getString("FontExtrusion0"));
189:
190: prevX = vertex.x;
191: n++;
192: coords.add(vertex);
193: } else if (flag == PathIterator.SEG_MOVETO) {
194: if (n != 0)
195: throw new IllegalArgumentException(J3dI18N
196: .getString("FontExtrusion3"));
197:
198: vertex.x = tmpCoords[0];
199: vertex.y = tmpCoords[1];
200: prevX = vertex.x;
201: n++;
202: coords.add(vertex);
203: }
204: pIt.next();
205: }
206:
207: //if (inc == 1){
208: //Point2f vertex = new Point2f(0.0f, 0.0f);
209: //coords.add(vertex);
210: //}
211: int i, num = coords.size();
212: pnts = new Point2f[num];
213: //System.err.println("num "+num+" inc "+inc);
214: if (inc == 0) {
215: for (i = 0; i < num; i++) {
216: pnts[i] = (Point2f) coords.get(i);
217: //System.err.println("i "+i+" x "+ pnts[i].x+" y "+pnts[i].y);
218: }
219: } else {
220: for (i = 0; i < num; i++) {
221: pnts[i] = (Point2f) coords.get(num - i - 1);
222: //System.err.println("i "+i+" x "+ pnts[i].x+" y "+pnts[i].y);
223: }
224: }
225:
226: //Force last y to be zero until Text3D face scaling is implemented
227: pnts[num - 1].y = 0.0f;
228: if (pnts[0].x != 0.0f)
229: throw new IllegalArgumentException(J3dI18N
230: .getString("FontExtrusion1"));
231:
232: //Compute straight line distance between first and last points.
233: float dx = (pnts[0].x - pnts[num - 1].x);
234: float dy = (pnts[0].y - pnts[num - 1].y);
235: length = (float) Math.sqrt(dx * dx + dy * dy);
236: }
237:
238: /**
239: * Gets the FontExtrusion's shape parameter. This
240: * parameter is used to construct the 3D contour of a Font3D object.
241: *
242: * @return extrusionShape the shape object used to generate the
243: * extrusion path
244: *
245: * @see Font3D
246: * @see java.awt.Shape
247: */
248: public Shape getExtrusionShape() {
249: return shape;
250: }
251:
252: /**
253: * Returns the tessellation tolerance with which this FontExtrusion was
254: * created.
255: * @return the tessellation tolerance used by this FontExtrusion
256: *
257: * @since Java 3D 1.2
258: */
259: public double getTessellationTolerance() {
260: return tessellationTolerance;
261: }
262:
263: }
|