fixed two bugs in font2swf
[swftools.git] / src / as3compile.c
1 /* as3compile.c
2    Compiles ActionScript 3.0 (.as) files into .swf files.
3
4    Part of the swftools package.
5
6    Copyright (c) 2008/2009 Matthias Kramm <kramm@quiss.org>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
29 #include "../lib/q.h"
30 #include "../lib/os.h"
31
32 static char * filename = 0;
33 static char * outputname = 0;
34 static int override_outputname = 0;
35 static int do_cgi = 0;
36 static double framerate = 25.0;
37 static double width = 400;
38 static double height = 300;
39 static int flashversion = 9;
40 static int verbose = 1;
41 static char local_with_networking = 0;
42 static char local_with_filesystem = 0;
43
44 static struct options_t options[] = {
45 {"h", "help"},
46 {"V", "version"},
47 {"v", "verbose"},
48 {"q", "quiet"},
49 {"C", "cgi"},
50 {"X", "width"},
51 {"Y", "height"},
52 {"r", "rate"},
53 {"l", "library"},
54 {"I", "include"},
55 {"T", "flashversion"},
56 {"o", "output"},
57 {0,0}
58 };
59
60 int args_callback_option(char*name,char*val)
61 {
62     if(!strcmp(name, "V")) {
63         printf("swfc - part of %s %s\n", PACKAGE, VERSION);
64         exit(0);
65     }
66     else if(!strcmp(name, "o")) {
67         outputname = val;
68         override_outputname = 1;
69         return 1;
70     }
71     else if(!strcmp(name, "r")) {
72         framerate = atof(val);
73         return 1;
74     }
75     else if(!strcmp(name, "v")) {
76         verbose++;
77         return 0;
78     }
79     else if(!strcmp(name, "q")) {
80         verbose--;
81         return 0;
82     }
83     else if(!strcmp(name, "X")) {
84         width = atof(val);
85         return 1;
86     }
87     else if(!strcmp(name, "Y")) {
88         height = atof(val);
89         return 1;
90     }
91     else if(!strcmp(name, "T")) {
92         flashversion = atoi(val);
93         return 1;
94     }
95     else if(!strcmp(name, "C")) {
96         do_cgi = 1;
97         return 0;
98     }
99     else if(!strcmp(name, "-l")) {
100         as3_import_file(val);
101         return 1;
102     }
103     else if(!strcmp(name, "-I")) {
104         as3_add_include_dir(val);
105         return 1;
106     }
107     else if (!strcmp(name, "N"))
108     {
109         local_with_networking = 1;
110         return 0;
111     }
112     else if (!strcmp(name, "L"))
113     {
114         local_with_filesystem = 1;
115         return 0;
116     }
117     else {
118         printf("Unknown option: -%s\n", name);
119         exit(1);
120     }
121     return 0;
122 }
123 int args_callback_longoption(char*name,char*val)
124 {
125     return args_long2shortoption(options, name, val);
126 }
127 void args_callback_usage(char *name)
128 {
129     printf("\n");
130     printf("Usage: %s file.as [-o file.swf] \n", name);
131     printf("\n");
132     printf("-h , --help                    Print short help message and exit\n");
133     printf("-V , --version                 Print version info and exit\n");
134     printf("-v , --verbose                 Increase verbosity\n");
135     printf("-q , --quiet                   Decrease verbosity\n");
136     printf("-C , --cgi                     Output to stdout (for use in CGI environments)\n");
137     printf("-X , --width                   Set target SWF width\n");
138     printf("-Y , --height                  Set target SWF width\n");
139     printf("-r , --rate                    Set target SWF framerate\n");
140     printf("-l , --library <file>          Include library file <file>\n");
141     printf("-I , --include <dir>           Add include dir <dir>\n");
142     printf("-T , --flashversion <num>      Set target SWF flash version to <num>.\n");
143     printf("-o , --output <filename>       Set output file to <filename>.\n");
144     printf("\n");
145 }
146 int args_callback_command(char*name,char*val)
147 {
148     if(filename) {
149         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
150                  filename, name);
151     }
152     filename = name;
153     return 0;
154 }
155
156 void writeSWF(SWF*swf)
157 {
158     int fi = -1;
159     if(do_cgi || !strcmp(filename, "-"))
160         fi = fileno(stdout);
161     else
162         fi = open(outputname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
163     if(fi<0) {
164         fprintf(stderr, "couldn't create output file %s\n", filename);
165         exit(1);
166     }
167     if(do_cgi) {
168         if(swf_WriteCGI(swf)<0) {
169             fprintf(stderr, "WriteCGI failed.\n");
170             exit(1);
171         }
172     } else {
173         if(swf_WriteSWF(fi, swf)<0) {
174             fprintf(stderr, "WriteSWF() failed.\n");
175             exit(1);
176         }
177     }
178 }
179
180 int main (int argc,char ** argv)
181 {
182     char buf[512];
183     char*currentdir = getcwd(buf, 512);
184     if(!currentdir) {
185         as3_warning("Could not determine the current directory");
186     } else {
187         as3_add_include_dir(currentdir);
188     }
189     registry_init();
190
191     int t;
192     processargs(argc, argv);
193     as3_setverbosity(verbose);
194
195     if(!filename) {
196         args_callback_usage(argv[0]);
197         exit(1);
198     }
199     if(!outputname) {
200         outputname = stripFilename(filename, ".swf");
201         //as3_warning("output name not given, writing to %s", outputname);
202     }
203
204     as3_parse_file(filename);
205     void*code = as3_getcode();
206
207     SWF swf;
208     memset(&swf, 0, sizeof(swf));
209     swf.fileVersion = flashversion;
210     swf.frameRate = framerate*0x100;
211     swf.movieSize.xmin = 0;
212     swf.movieSize.ymin = 0;
213     swf.movieSize.xmax = width*20;
214     swf.movieSize.ymax = height*20;
215     TAG*tag = swf.firstTag = swf_InsertTag(0, ST_DOABC);
216     swf_WriteABC(tag, code);
217
218     if(as3_getglobalclass()) {
219         tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
220         swf_SetU16(tag, 1);
221         swf_SetU16(tag, 0);
222         swf_SetString(tag, as3_getglobalclass());
223     } else {
224         as3_warning("no global public MovieClip subclass");
225     }
226     
227     as3_destroy();
228
229     tag = swf_InsertTag(tag, ST_SHOWFRAME);
230     tag = swf_InsertTag(tag, ST_END);
231
232     swf_FreeABC(code);
233     
234     if(local_with_filesystem)
235         swf.fileAttributes &= ~FILEATTRIBUTE_USENETWORK;
236     if(local_with_networking)
237         swf.fileAttributes |= FILEATTRIBUTE_USENETWORK;
238
239     writeSWF(&swf);
240     swf_FreeTags(&swf);
241
242     return 0;
243 }
244