1 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 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 filename = getParameter( context, "filename" ); 79 String text = getParameter( context, "text" ); 80 boolean mask = getBoolParameter( context, "mask", false ); boolean html = getBoolParameter( context, "html", false ); String embedset = getParameter( context, "embedset", "[all]" ); 83 AlphaColor color = getColorParameter( context, "fontColor", AlphaColor.red ); 84 int fontSize = getIntParameter( context, "fontSize", 12 ) * 20; 85 String fontType = getParameter( context, "fontType", "Arial" ); 86 String fftFile = getParameter( context, "fftFilePath" ); boolean bold = getBoolParameter( context, "bold", false ); boolean italic = getBoolParameter( context, "italic", false ); String alignment = getParameter( context, "alignment", "left" ); double spacing = getDoubleParameter( context, "letterSpacing", 0.0 ) * 10; int lineSpacing = getIntParameter( context, "lineSpacing", 0 ) * 20; int transparency = getIntParameter( context, "transparency", 100 ); boolean cache = getBoolParameter( context, "cache", false ); 94 String instancename = getParameter( context, "instancename" ); 95 96 Instance inst = getInstance(); 97 98 if( text == null ) { 102 if( filename == null ) { 103 throw new IVException( Resource.NOTEXT ); 104 } 105 IVUrl url = IVUrl.newUrl(filename, file); 106 107 if( cache ) { 109 text = (String ) MediaCache.getMedia( url ); 110 } 111 112 if( text == null ) { 113 LineReader reader = null; 114 try { 115 reader = Util.getUrlReader(file, url); 116 StringBuffer sb = new StringBuffer (100); 117 String 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 [] {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 encoding = file.getEncoding()!=null?file.getEncoding():PropertyManager.defaultEncoding; 135 try { 136 text = encoding!=null? new String (text.getBytes(), encoding): text; 137 } catch( UnsupportedEncodingException e ) { 138 Log.log(e); 139 } 140 } 141 142 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 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 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 Script script = inst.copyScript(); 189 Frame textFrame = script.newFrame(); 190 Instance textInst = textFrame.addInstance( t_block, 1, null, null ); 191 192 if( mask ) { 194 addMask(script, 0, textInst, winWidth, winHeight); 196 } 197 198 if( instancename != null ) { 199 inst.name = instancename; 200 } 201 } 202 203 } 204 205 | Popular Tags |