KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > test > copyswf


1 /* *****************************************************************************
2  * copyswf.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10
11 package org.openlaszlo.test;
12
13 import java.io.*;
14 import org.openlaszlo.iv.flash.api.*;
15 import org.openlaszlo.iv.flash.api.text.*;
16 import org.openlaszlo.iv.flash.util.*;
17 import org.openlaszlo.utils.FileUtils;
18
19
20 public class copyswf {
21    static public void main (String JavaDoc args[]) {
22        try {
23            FlashFile f = FlashFile.parse(args[0]);
24            OutputStream out = new FileOutputStream(args[1]);
25
26            java.util.Enumeration JavaDoc defs = f.definitions();
27            FontsCollector fc = new FontsCollector();
28            FontsCollector pfc = new FontsCollector();
29
30            // Start out writing ito the preloader fc
31
FontsCollector curfc = pfc;
32
33            // A lps-2.0 generated SWF has either
34
// 1 block of fonts or
35
// 2 blocks of fonts
36
// If there are 2 blocks, then we know there's a preloader.
37
// If there's only 1 block, then they're either all inthe
38
// preloader or there's no preloader.
39
//
40
// If the first tag isn't a definefont tag, then we know
41
// we have a preloader, too and can short circuit that
42
// case. Otherwise, we need to look for a break in
43
// the defs for one that isn't a font.
44

45            int index = 0;
46            int lastIndex = -1;
47
48            boolean hasPreloader = false;
49
50            while(defs.hasMoreElements()) {
51                FlashDef def = (FlashDef)defs.nextElement();
52                index++;
53
54                // Here we know we definitely have a preloader
55
// but no preloader fonts
56
if (index == 1 && !(def instanceof FontDef)) {
57                    hasPreloader = true;
58                }
59
60                if (def instanceof FontDef) {
61                    // If we don't have a preloader
62
// (or we don't know if we have a preloader)
63
if (!hasPreloader) {
64                        // First time in
65
if (lastIndex == -1) {
66                            lastIndex = index;
67                        }
68                        else {
69                            // All other times
70
// See if we're on to a second group of fonts
71
if (index > lastIndex + 1) {
72                                hasPreloader = true;
73                                curfc = fc;
74                            }
75                        }
76                    }
77                    FontDef fontDef = (FontDef)def;
78                    Font font = fontDef.getFont();
79                    String JavaDoc bold = ((font.BOLD & font.flags) > 0 ? "bold" : "");
80                    String JavaDoc italic = ((font.ITALIC & font.flags) > 0 ?
81                            "italic" : "");
82                    System.out.println("Copying font " + font.getFontName()
83                                + bold + italic);
84                    FontDef fdef = curfc.addFont(font, null);
85                    fdef.setWriteAllChars(true);
86                    fdef.setWriteLayout(true);
87                }
88                index++;
89            }
90            InputStream input;
91
92            // Ok we didn't have a preloader to the pfc is the regular
93
// fc and the pfc should be empty.
94
if (!hasPreloader) {
95                fc = pfc;
96                pfc = null;
97            }
98            input = f.generate(fc, pfc, hasPreloader).getInputStream();
99            FileUtils.send(input, out);
100            input.close();
101            out.close();
102        } catch (Exception JavaDoc e) {
103            e.printStackTrace();
104        }
105    }
106 }
107
Popular Tags