001: /*
002: * $Id: InsertTextCommand.java,v 1.5 2002/04/29 06:19:46 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.commands;
052:
053: import java.io.*;
054: import java.util.*;
055: import java.awt.geom.*;
056:
057: import org.openlaszlo.iv.flash.parser.*;
058: import org.openlaszlo.iv.flash.api.*;
059: import org.openlaszlo.iv.flash.api.image.*;
060: import org.openlaszlo.iv.flash.api.shape.*;
061: import org.openlaszlo.iv.flash.api.text.*;
062: import org.openlaszlo.iv.flash.util.*;
063: import org.openlaszlo.iv.flash.cache.*;
064: import org.openlaszlo.iv.flash.url.*;
065:
066: import org.openlaszlo.iv.flash.context.Context;
067:
068: /**
069: * Insert Text generator command<BR>
070: *
071: * @author Dmitry Skavish
072: */
073: public class InsertTextCommand extends GenericCommand {
074:
075: public InsertTextCommand() {
076: }
077:
078: public void doCommand(FlashFile file, Context context,
079: Script parent, int frameNum) throws IVException {
080: String filename = getParameter(context, "filename");
081: String text = getParameter(context, "text");
082: boolean mask = getBoolParameter(context, "mask", false); // true, false
083: boolean html = getBoolParameter(context, "html", false); // true, false
084: String embedset = getParameter(context, "embedset", "[all]");
085: AlphaColor color = getColorParameter(context, "fontColor",
086: AlphaColor.red);
087: int fontSize = getIntParameter(context, "fontSize", 12) * 20;
088: String fontType = getParameter(context, "fontType", "Arial");
089: String fftFile = getParameter(context, "fftFilePath"); // name of fft file
090: boolean bold = getBoolParameter(context, "bold", false); // true, false
091: boolean italic = getBoolParameter(context, "italic", false); // true, false
092: String alignment = getParameter(context, "alignment", "left"); // left, right, center, justify
093: double spacing = getDoubleParameter(context, "letterSpacing",
094: 0.0) * 10; // in half pixels
095: int lineSpacing = getIntParameter(context, "lineSpacing", 0) * 20; // in points
096: int transparency = getIntParameter(context, "transparency", 100); // 0-100
097: boolean cache = getBoolParameter(context, "cache", false);
098: String instancename = getParameter(context, "instancename");
099:
100: Instance inst = getInstance();
101:
102: // -----------------------------------------------------------
103: // read text from file if needed
104: // -----------------------------------------------------------
105: if (text == null) {
106: if (filename == null) {
107: throw new IVException(Resource.NOTEXT);
108: }
109: IVUrl url = IVUrl.newUrl(filename, file);
110:
111: // check in cache first
112: if (cache) {
113: text = (String) MediaCache.getMedia(url);
114: }
115:
116: if (text == null) {
117: LineReader reader = null;
118: try {
119: reader = Util.getUrlReader(file, url);
120: StringBuffer sb = new StringBuffer(100);
121: String s = reader.readLine();
122: while (s != null) {
123: sb.append(s);
124: sb.append(Util.lineSeparator);
125: s = reader.readLine();
126: }
127: text = sb.toString();
128: } catch (IOException e) {
129: throw new IVException(Resource.ERRCMDFILEREAD,
130: new Object[] { url.getName(),
131: getCommandName() }, e);
132: } finally {
133: try {
134: if (reader != null)
135: reader.close();
136: } catch (IOException e) {
137: }
138: }
139: MediaCache.addMedia(url, text, text.length(), cache);
140: }
141: } else {
142: String encoding = file.getEncoding() != null ? file
143: .getEncoding() : PropertyManager.defaultEncoding;
144: try {
145: text = encoding != null ? new String(text.getBytes(),
146: encoding) : text;
147: } catch (UnsupportedEncodingException e) {
148: Log.log(e);
149: }
150: }
151:
152: // ---------------------------------------------------------------
153: // get the font (it's a little bit ugly, needs to be changed!!!)
154: // ---------------------------------------------------------------
155: if (fftFile == null) {
156: fftFile = fontType.replace(' ', 'G') + ".fft";
157: if (italic)
158: fftFile = "I" + fftFile;
159: if (bold)
160: fftFile = "B" + fftFile;
161: }
162:
163: fftFile = Util.translatePath(fftFile);
164: File fontFile = new File(fftFile);
165: if (!fontFile.exists()) {
166: fftFile = Util.concatFileNames(PropertyManager.fontPath,
167: fftFile);
168: fontFile = new File(Util.getInstallDir(), fftFile);
169: }
170: fftFile = fontFile.getAbsolutePath();
171:
172: Font font = FontDef.load(fftFile, file);
173:
174: // create text block and set parameters
175: Text t_block = Text.newText();
176: color.setAlpha((transparency * 255) / 100);
177: TextItem t_item = new TextItem(text, font, fontSize, color);
178:
179: if (alignment.equals("left"))
180: t_item.align = 0;
181: else if (alignment.equals("right"))
182: t_item.align = 1;
183: else if (alignment.equals("center"))
184: t_item.align = 2;
185: else if (alignment.equals("justify"))
186: t_item.align = 3;
187:
188: t_item.kerning = (int) spacing;
189: t_item.linesp = lineSpacing;
190:
191: t_block.addTextItem(t_item);
192:
193: // calculate text bounds
194: Rectangle2D winBounds = GeomHelper.getTransformedSize(
195: inst.matrix, GeomHelper.newRectangle(-1024, -1024,
196: 2048, 2048));
197: int winWidth = (int) winBounds.getWidth();
198: int winHeight = (int) winBounds.getHeight();
199:
200: t_block.setBounds(GeomHelper.newRectangle(0, 0, winWidth,
201: winHeight));
202:
203: GeomHelper.deScaleMatrix(inst.matrix);
204: inst.matrix.translate(-winWidth / 2, -winHeight / 2);
205:
206: // copy script and add the text there
207: Script script = inst.copyScript();
208: Frame textFrame = script.newFrame();
209: Instance textInst = textFrame.addInstance(t_block, 1, null,
210: null);
211:
212: // add mask
213: if (mask) {
214: //addMask(parent, frameNum, inst, winWidth, winHeight);
215: addMask(script, 0, textInst, winWidth, winHeight);
216: }
217:
218: if (instancename != null) {
219: inst.name = instancename;
220: }
221: }
222:
223: }
|