KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > commands > InsertTextCommand


1 /*
2  * $Id: InsertTextCommand.java,v 1.5 2002/04/29 06:19:46 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.commands;
52
53 import java.io.*;
54 import java.util.*;
55 import java.awt.geom.*;
56
57 import org.openlaszlo.iv.flash.parser.*;
58 import org.openlaszlo.iv.flash.api.*;
59 import org.openlaszlo.iv.flash.api.image.*;
60 import org.openlaszlo.iv.flash.api.shape.*;
61 import org.openlaszlo.iv.flash.api.text.*;
62 import org.openlaszlo.iv.flash.util.*;
63 import org.openlaszlo.iv.flash.cache.*;
64 import org.openlaszlo.iv.flash.url.*;
65
66 import org.openlaszlo.iv.flash.context.Context;
67
68 /**
69  * Insert Text generator command<BR>
70  *
71  * @author Dmitry Skavish
72  */

73 public class InsertTextCommand extends GenericCommand {
74
75     public InsertTextCommand() {}
76
77     public void doCommand( FlashFile file, Context context, Script parent, int frameNum ) throws IVException {
78         String JavaDoc filename = getParameter( context, "filename" );
79         String JavaDoc text = getParameter( context, "text" );
80         boolean mask = getBoolParameter( context, "mask", false ); // true, false
81
boolean html = getBoolParameter( context, "html", false ); // true, false
82
String JavaDoc embedset = getParameter( context, "embedset", "[all]" );
83         AlphaColor color = getColorParameter( context, "fontColor", AlphaColor.red );
84         int fontSize = getIntParameter( context, "fontSize", 12 ) * 20;
85         String JavaDoc fontType = getParameter( context, "fontType", "Arial" );
86         String JavaDoc fftFile = getParameter( context, "fftFilePath" ); // name of fft file
87
boolean bold = getBoolParameter( context, "bold", false ); // true, false
88
boolean italic = getBoolParameter( context, "italic", false ); // true, false
89
String JavaDoc alignment = getParameter( context, "alignment", "left" ); // left, right, center, justify
90
double spacing = getDoubleParameter( context, "letterSpacing", 0.0 ) * 10; // in half pixels
91
int lineSpacing = getIntParameter( context, "lineSpacing", 0 ) * 20; // in points
92
int transparency = getIntParameter( context, "transparency", 100 ); // 0-100
93
boolean cache = getBoolParameter( context, "cache", false );
94         String JavaDoc instancename = getParameter( context, "instancename" );
95
96         Instance inst = getInstance();
97
98         // -----------------------------------------------------------
99
// read text from file if needed
100
// -----------------------------------------------------------
101
if( text == null ) {
102             if( filename == null ) {
103                 throw new IVException( Resource.NOTEXT );
104             }
105             IVUrl url = IVUrl.newUrl(filename, file);
106
107             // check in cache first
108
if( cache ) {
109                 text = (String JavaDoc) MediaCache.getMedia( url );
110             }
111
112             if( text == null ) {
113                 LineReader reader = null;
114                 try {
115                     reader = Util.getUrlReader(file, url);
116                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
117                     String JavaDoc s = reader.readLine();
118                     while( s != null ) {
119                         sb.append(s);
120                         sb.append(Util.lineSeparator);
121                         s = reader.readLine();
122                     }
123                     text = sb.toString();
124                 } catch( IOException e ) {
125                     throw new IVException(Resource.ERRCMDFILEREAD, new Object JavaDoc[] {url.getName(), getCommandName()}, e);
126                 } finally {
127                     try {
128                         if( reader != null ) reader.close();
129                     } catch( IOException e ) {}
130                 }
131                 MediaCache.addMedia(url, text, text.length(), cache);
132             }
133         } else {
134             String JavaDoc encoding = file.getEncoding()!=null?file.getEncoding():PropertyManager.defaultEncoding;
135             try {
136                 text = encoding!=null? new String JavaDoc(text.getBytes(), encoding): text;
137             } catch( UnsupportedEncodingException e ) {
138                 Log.log(e);
139             }
140         }
141
142         // ---------------------------------------------------------------
143
// get the font (it's a little bit ugly, needs to be changed!!!)
144
// ---------------------------------------------------------------
145
if( fftFile == null ) {
146             fftFile = fontType.replace( ' ', 'G' )+".fft";
147             if( italic ) fftFile = "I"+fftFile;
148             if( bold ) fftFile = "B"+fftFile;
149         }
150
151         fftFile = Util.translatePath( fftFile );
152         File fontFile = new File( fftFile );
153         if( !fontFile.exists() ) {
154             fftFile = Util.concatFileNames( PropertyManager.fontPath, fftFile );
155             fontFile = new File( Util.getInstallDir(), fftFile );
156         }
157         fftFile = fontFile.getAbsolutePath();
158
159         Font font = FontDef.load( fftFile, file );
160
161         // create text block and set parameters
162
Text t_block = Text.newText();
163         color.setAlpha( (transparency * 255)/100 );
164         TextItem t_item = new TextItem( text, font, fontSize, color );
165
166         if( alignment.equals("left") ) t_item.align = 0;
167         else if( alignment.equals("right") ) t_item.align = 1;
168         else if( alignment.equals("center") ) t_item.align = 2;
169         else if( alignment.equals("justify") ) t_item.align = 3;
170
171         t_item.kerning = (int) spacing;
172         t_item.linesp = lineSpacing;
173
174         t_block.addTextItem( t_item );
175
176         // calculate text bounds
177
Rectangle2D winBounds = GeomHelper.getTransformedSize( inst.matrix,
178             GeomHelper.newRectangle(-1024, -1024, 2048, 2048) );
179         int winWidth = (int) winBounds.getWidth();
180         int winHeight = (int) winBounds.getHeight();
181
182         t_block.setBounds( GeomHelper.newRectangle(0,0,winWidth,winHeight) );
183
184         GeomHelper.deScaleMatrix( inst.matrix );
185         inst.matrix.translate( -winWidth/2, -winHeight/2 );
186
187         // copy script and add the text there
188
Script script = inst.copyScript();
189         Frame textFrame = script.newFrame();
190         Instance textInst = textFrame.addInstance( t_block, 1, null, null );
191
192         // add mask
193
if( mask ) {
194             //addMask(parent, frameNum, inst, winWidth, winHeight);
195
addMask(script, 0, textInst, winWidth, winHeight);
196         }
197
198         if( instancename != null ) {
199             inst.name = instancename;
200         }
201     }
202
203 }
204
205
Popular Tags