001: /*
002: * $RCSfile: Text.java,v $
003: *
004: * @(#)Text.java 1.16 98/11/05 20:35:24
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: *
032: * $Revision: 1.2 $
033: * $Date: 2005/02/03 23:07:03 $
034: * $State: Exp $
035: */
036: /*
037: * @Author: Rick Goldberg
038: * @Author: Doug Gehringer
039: *
040: */
041: package org.jdesktop.j3d.loaders.vrml97.impl;
042:
043: import com.sun.j3d.utils.geometry.Text2D;
044: import java.awt.Font;
045: import javax.media.j3d.Shape3D;
046: import javax.media.j3d.Transform3D;
047:
048: import javax.media.j3d.TransformGroup;
049: import javax.vecmath.Color3f;
050:
051: /** Description of the Class */
052: public class Text extends Geometry {
053:
054: // exposedField
055: MFString string;
056: SFNode fontStyle;
057: MFFloat length;
058: SFFloat maxExtent;
059:
060: static String vrmlSerif = "SERIF";
061: static String vrmlSans = "SANS";
062: static String vrmlFixed = "TYPEWRITER";
063:
064: static String j3dSerif = "Serif";
065: static String j3dSans = "Sans";
066: static String j3dFixed = "Courier";
067:
068: static String vrmlPlain = "PLAIN";
069: static String vrmlBold = "BOLD";
070: static String vrmlItalic = "ITALIC";
071: static String vrmlBoldItalic = "BOLDITALIC";
072:
073: final static int RASTER_SIZE = 24;
074:
075: TransformGroup impl = null;
076:
077: /**
078: *Constructor for the Text object
079: *
080: *@param loader Description of the Parameter
081: */
082: public Text(Loader loader) {
083: super (loader);
084: string = new MFString();
085: fontStyle = new SFNode();
086: length = new MFFloat();
087: maxExtent = new SFFloat(0.0f);
088: initFields();
089: }
090:
091: /**
092: *Constructor for the Text object
093: *
094: *@param loader Description of the Parameter
095: *@param string Description of the Parameter
096: *@param fontStyle Description of the Parameter
097: *@param length Description of the Parameter
098: *@param maxExtent Description of the Parameter
099: */
100: public Text(Loader loader, MFString string, SFNode fontStyle,
101: MFFloat length, SFFloat maxExtent) {
102:
103: super (loader);
104: this .string = string;
105: this .fontStyle = fontStyle;
106: this .length = length;
107: this .maxExtent = maxExtent;
108: initFields();
109: }
110:
111: /**
112: * Description of the Method
113: *
114: *@param app Description of the Parameter
115: *@return Description of the Return Value
116: */
117: javax.media.j3d.Node createText2D(javax.media.j3d.Appearance app) {
118: if (loader.debug) {
119: System.out.println("Text.createText2D() called");
120: }
121: if ((string.strings != null) && (string.strings.length > 0)) {
122: if (loader.debug) {
123: System.out.println("creating Text2D object");
124: }
125:
126: FontStyle fs = null;
127: if ((fontStyle.node != null)
128: && (fontStyle.node instanceof FontStyle)) {
129: fs = (FontStyle) fontStyle.node;
130: } else {
131: fs = new FontStyle(loader);
132: }
133:
134: // Derive color from Appearance
135: Color3f fontColor = new Color3f(1.0f, 1.0f, 1.0f);
136: if (app != null) {
137: javax.media.j3d.Material mat = app.getMaterial();
138: if (mat != null) {
139: mat.getDiffuseColor(fontColor);
140: }
141: }
142:
143: String fontName = j3dSerif;
144: if (fs.family.strings.length > 0) {
145: boolean found = false;
146: for (int i = 0; ((i < fs.family.strings.length) && !found); i++) {
147: if (vrmlSerif.equals(fs.family.strings[i])) {
148: fontName = j3dSerif;
149: found = true;
150: } else if (vrmlSans.equals(fs.family.strings[i])) {
151: fontName = j3dSans;
152: found = true;
153: } else if (vrmlFixed.equals(fs.family.strings[i])) {
154: fontName = j3dFixed;
155: found = true;
156: }
157: }
158: }
159: int fontStyleId = Font.PLAIN;
160: if (fs.style.string != null) {
161: if (vrmlPlain.equals(fs.style.string)) {
162: fontStyleId = Font.PLAIN;
163: } else if (vrmlBold.equals(fs.style.string)) {
164: fontStyleId = Font.BOLD;
165: } else if (vrmlItalic.equals(fs.style.string)) {
166: fontStyleId = Font.ITALIC;
167: } else if (vrmlBoldItalic.equals(fs.style.string)) {
168: fontStyleId = Font.BOLD + Font.ITALIC;
169: }
170: }
171:
172: impl = new TransformGroup();
173:
174: // TODO: handle multiple strings by making multiple Text3D's
175: // in transform groups to offset them correctly
176: Text2D text = new Text2D(string.strings[0], fontColor,
177: fontName, RASTER_SIZE, fontStyleId);
178:
179: impl.addChild(text);
180:
181: Transform3D textXform = new Transform3D();
182:
183: float fontSize = RASTER_SIZE
184: * text.getRectangleScaleFactor();
185: float scale = fs.size.value / fontSize;
186:
187: textXform.setScale(scale);
188: impl.setTransform(textXform);
189:
190: implNode = impl;
191: }
192: return impl;
193: }
194:
195: /**
196: * Gets the implGeom attribute of the Text object
197: *
198: *@return The implGeom value
199: */
200: public javax.media.j3d.Geometry getImplGeom() {
201: return null;
202: }
203:
204: /**
205: * Description of the Method
206: *
207: *@return Description of the Return Value
208: */
209: public boolean haveTexture() {
210: return false;
211: }
212:
213: /**
214: * Gets the numTris attribute of the Text object
215: *
216: *@return The numTris value
217: */
218: public int getNumTris() {
219: return 0;
220: }
221:
222: /**
223: * Gets the boundingBox attribute of the Text object
224: *
225: *@return The boundingBox value
226: */
227: public javax.media.j3d.BoundingBox getBoundingBox() {
228: return null;
229: }
230:
231: /**
232: * Gets the type attribute of the Text object
233: *
234: *@return The type value
235: */
236: public String getType() {
237: return "Text";
238: }
239:
240: /**
241: * Description of the Method
242: *
243: *@return Description of the Return Value
244: */
245: public Object clone() {
246: return new Text(loader, (MFString) string.clone(),
247: (SFNode) fontStyle.clone(), (MFFloat) length.clone(),
248: (SFFloat) maxExtent.clone());
249: }
250:
251: /**
252: * Description of the Method
253: *
254: *@param eventInName Description of the Parameter
255: *@param time Description of the Parameter
256: */
257: public void notifyMethod(String eventInName, double time) {
258: System.out.println("Text: unimplemented event " + eventInName);
259: }
260:
261: /** Description of the Method */
262: void initFields() {
263: string.init(this , FieldSpec, Field.EXPOSED_FIELD, "string");
264: fontStyle.init(this , FieldSpec, Field.EXPOSED_FIELD,
265: "fontStyle");
266: length.init(this , FieldSpec, Field.EXPOSED_FIELD, "length");
267: maxExtent.init(this , FieldSpec, Field.EXPOSED_FIELD,
268: "maxExtent");
269: }
270: }
|