1 4 5 9 10 package org.openlaszlo.sc; 11 12 import org.openlaszlo.utils.FileUtils; 13 import org.openlaszlo.utils.StringUtils; 14 import org.openlaszlo.compiler.FontInfo; 15 import org.openlaszlo.iv.flash.api.*; 16 import org.openlaszlo.iv.flash.api.action.*; 17 import org.openlaszlo.iv.flash.api.image.*; 18 import org.openlaszlo.iv.flash.api.sound.*; 19 import org.openlaszlo.iv.flash.api.text.*; 20 import org.openlaszlo.iv.flash.util.*; 21 import org.openlaszlo.iv.flash.cache.*; 22 import org.apache.log4j.*; 23 24 import java.io.*; 25 import java.util.*; 26 27 import java.awt.geom.Rectangle2D ; 29 30 31 40 41 public class AddInputText { 42 private static Logger mLogger = Logger.getLogger(AddInputText.class); 43 44 45 private static final int TWIP = 20; 46 47 48 private static int mTextLeading = 2; 49 50 static public void main (String args[]) { 51 addtext(args[0], args[1], new String [] {args[2], args[3]} ); 52 } 53 54 static public void addtext (String infile, String outfile, String resourceNames[]) { 55 try { 56 FlashFile f = FlashFile.parse(infile); 57 OutputStream out = new FileOutputStream(outfile); 58 59 java.util.Enumeration defs = f.definitions(); 60 FontsCollector fc = new FontsCollector(); 61 62 HashMap fontsTable = new HashMap(); 63 64 while(defs.hasMoreElements()) { 65 FlashDef def = (FlashDef)defs.nextElement(); 66 if (def instanceof FontDef) { 67 FontDef fontDef = (FontDef)def; 68 Font font = fontDef.getFont(); 69 String bold = ((font.BOLD & font.flags) > 0 ? "bold" : ""); 70 String italic = ((font.ITALIC & font.flags) > 0 ? 71 "italic" : ""); 72 FontDef fdef = fc.addFont(font, null); 75 fdef.setWriteAllChars(true); 76 fdef.setWriteLayout(true); 77 fontsTable.put(font.getFontName() + "::"+bold+italic, font); 78 } 79 } 80 81 82 for (int i = 0; i < resourceNames.length; i++) { 84 String rsrc = resourceNames[i]; 85 System.out.println("addinputtext: processing "+rsrc); 86 String path[] = StringUtils.split(rsrc, "/"); 88 if (path.length < 7) { 89 Exception e = new RuntimeException ("inputtext resource name '"+rsrc+"' couldn't be parsed into a form like lzinputtext/lztahoe8/8/plain/398/157[/html][/passwd]/(m|s)"); 90 e.printStackTrace(); 91 throw e; 92 } 93 String fname = path[1]; 94 String fsize = path[2]; 95 String fstyle = path[3]; 96 int width = (int) Double.parseDouble(path[4]); 97 int height = (int) Double.parseDouble(path[5]); 98 Font font = (Font) fontsTable.get(fname + "::" + (fstyle.equals("plain") ? "" : fstyle)); 99 if (font == null) { 100 Exception e = new RuntimeException ("could not find font "+fname+" " +fstyle +" for inputtext resource named '"+rsrc); 101 e.printStackTrace(); 102 throw e; 103 } 104 boolean multiline = false; 105 boolean password = false; 106 107 for (int j = 6; j < path.length; j++) { 109 if (path[j].equals("passwd")) { 110 password = true; 111 } else if (path[j].equals("m")) { 112 multiline = true; 113 } 114 } 115 116 FontInfo finfo = new FontInfo(fname, fsize, fstyle); 117 addCustomInputText(f, font, finfo, width, height, multiline, password); 118 } 119 120 writeFlashFile(f, fc, out); 121 122 } catch (Exception e) { 123 mLogger.error("exception in AddInputText.addtext", e); 124 } 125 } 126 127 static void writeFlashFile(FlashFile f, FontsCollector fc, OutputStream out) { 128 try { 129 InputStream input; 130 input = f.generate(fc, null, false).getInputStream(); 131 FileUtils.send(input, out); 132 input.close(); 133 out.close(); 134 } catch (Exception e) { 135 mLogger.error("exception in AddInputText.writeFlashFile", e); 136 } 137 } 138 139 140 142 static public void addCustomInputText(FlashFile f, Font font, FontInfo fontInfo, int width, int height, 143 boolean multiline, boolean password) 144 { 145 String fontName = fontInfo.getName(); 146 String mstring; 147 mstring = multiline ? "/m" : "/s"; 148 149 String name = 150 "lzinputtext/" + 151 fontName + "/" + 152 fontInfo.getSize() + "/" + 153 fontInfo.getStyle() + "/" + width + "/" + height + 154 (password ? "/passwd" : "") + mstring; 155 156 Script movieClip = new Script(1); 159 Frame frame = movieClip.newFrame(); 160 161 TextField input = new TextField("", "text", 162 font, fontInfo.getSize()*TWIP, AlphaColor.black); 163 164 int flags = input.getFlags(); 165 166 if (password) { 167 flags |= TextField.PASSWORD; 168 } 169 170 input.setFlags(flags 171 | TextField.USEOUTLINES 172 | TextField.HASFONT 173 | (multiline ? TextField.MULTILINE : 0) 174 | (multiline ? TextField.WORDWRAP : 0) 175 ); 176 177 input.setLayout(0, 0, 0, 0, mTextLeading*TWIP); 181 182 input.setBounds(new Rectangle2D.Double (0, 0, width*TWIP, height*TWIP)); 183 184 frame.addInstance(input, 1, null, null); 185 frame.addStopAction(); 186 187 f.addDefToLibrary(name, movieClip); 189 ExportAssets ea = new ExportAssets(); 191 ea.addAsset(name, movieClip); 192 System.out.println("addCustomInputText: added "+name); 193 Timeline timeline = f.getMainScript().getTimeline(); 194 frame = timeline.getFrameAt(timeline.getFrameCount() - 1); 195 frame.addFlashObject(ea); 196 } 197 198 } 199 | Popular Tags |