001: /* *****************************************************************************
002: * copyswf.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.test;
011:
012: import java.io.*;
013: import org.openlaszlo.iv.flash.api.*;
014: import org.openlaszlo.iv.flash.api.text.*;
015: import org.openlaszlo.iv.flash.util.*;
016: import org.openlaszlo.utils.FileUtils;
017:
018: public class copyswf {
019: static public void main(String args[]) {
020: try {
021: FlashFile f = FlashFile.parse(args[0]);
022: OutputStream out = new FileOutputStream(args[1]);
023:
024: java.util.Enumeration defs = f.definitions();
025: FontsCollector fc = new FontsCollector();
026: FontsCollector pfc = new FontsCollector();
027:
028: // Start out writing ito the preloader fc
029: FontsCollector curfc = pfc;
030:
031: // A lps-2.0 generated SWF has either
032: // 1 block of fonts or
033: // 2 blocks of fonts
034: // If there are 2 blocks, then we know there's a preloader.
035: // If there's only 1 block, then they're either all inthe
036: // preloader or there's no preloader.
037: //
038: // If the first tag isn't a definefont tag, then we know
039: // we have a preloader, too and can short circuit that
040: // case. Otherwise, we need to look for a break in
041: // the defs for one that isn't a font.
042:
043: int index = 0;
044: int lastIndex = -1;
045:
046: boolean hasPreloader = false;
047:
048: while (defs.hasMoreElements()) {
049: FlashDef def = (FlashDef) defs.nextElement();
050: index++;
051:
052: // Here we know we definitely have a preloader
053: // but no preloader fonts
054: if (index == 1 && !(def instanceof FontDef)) {
055: hasPreloader = true;
056: }
057:
058: if (def instanceof FontDef) {
059: // If we don't have a preloader
060: // (or we don't know if we have a preloader)
061: if (!hasPreloader) {
062: // First time in
063: if (lastIndex == -1) {
064: lastIndex = index;
065: } else {
066: // All other times
067: // See if we're on to a second group of fonts
068: if (index > lastIndex + 1) {
069: hasPreloader = true;
070: curfc = fc;
071: }
072: }
073: }
074: FontDef fontDef = (FontDef) def;
075: Font font = fontDef.getFont();
076: String bold = ((font.BOLD & font.flags) > 0 ? "bold"
077: : "");
078: String italic = ((font.ITALIC & font.flags) > 0 ? "italic"
079: : "");
080: System.out.println("Copying font "
081: + font.getFontName() + bold + italic);
082: FontDef fdef = curfc.addFont(font, null);
083: fdef.setWriteAllChars(true);
084: fdef.setWriteLayout(true);
085: }
086: index++;
087: }
088: InputStream input;
089:
090: // Ok we didn't have a preloader to the pfc is the regular
091: // fc and the pfc should be empty.
092: if (!hasPreloader) {
093: fc = pfc;
094: pfc = null;
095: }
096: input = f.generate(fc, pfc, hasPreloader).getInputStream();
097: FileUtils.send(input, out);
098: input.close();
099: out.close();
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103: }
104: }
|