started implementing the remove_font_transforms filter
[swftools.git] / swfs / simple_viewer.c
1 /* simple_viewer.c
2
3    Creates the swf file simple_viewer.swf. 
4    I'm not much of an designer. Suggestions and improvements concerning
5    this file or simple_viewer.swf are highly welcome. -mk
6    
7    Part of the swftools package.
8
9    Copyright (c) 2000, 2001 Matthias Kramm <kramm@quiss.org>
10  
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
24
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <math.h>
28 #include "rfxswf.h"
29
30 TAG* tag;
31
32 int button_sizex = 20; //button size in pixels
33 int button_sizey = 20; 
34
35 RGBA button_colors[6]=
36 {{0,0x00,0x00,0x80}, //Idle      left
37  {0,0x00,0x00,0x80}, //   "      right
38  {0,0x20,0x20,0xc0}, //MouseOver left
39  {0,0x20,0x20,0xc0}, //   "      right
40  {0,0x10,0x10,0xff}, //Down      left
41  {0,0x10,0x10,0xff}};//   "      right
42
43 int useDefineButton2 = 0; // set this to 1 to use DefineButton2 Tags
44                           // instead of DefineButton1
45 #define SUBTITLES 1
46 //#define USE_WATERMARK 1   // set this flag to print-protect your swfs
47
48 #define ID_WATERMARK 64
49
50 int main (int argc,char ** argv)
51 { SWF swf;
52   RGBA rgb;
53   SRECT r;
54   SHAPE* s;
55   MATRIX m;
56   ActionTAG*a1,*a2,*a3;
57   S32 width=1024,height = 768;
58   
59   int f,i,ls1,fs1;
60   int count;
61   int t;
62
63   memset(&swf,0x00,sizeof(SWF));        // set global movie parameters
64
65   swf.fileVersion    = 4;               // make flash 4 compatible swf
66   swf.frameRate      = 0x1900;          // about 0x19 frames per second
67   
68   swf.movieSize.xmax = 20*width;        // flash units: 1 pixel = 20 units
69   swf.movieSize.ymax = 20*height;
70
71   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
72   tag = swf.firstTag;
73   rgb.r = 0xff;
74   rgb.g = 0xff;
75   rgb.b = 0xff;
76   swf_SetRGB(tag,&rgb);
77  
78   // initialize matrix
79   m.sx = 65536; //scale
80   m.sy = 65536;
81   m.r0 = 0; //rotate
82   m.r1 = 0;
83   m.tx = 0; //move
84   m.ty = 0;
85
86   /* the "viewport" object will be replaced by swfcombine
87      with the object to browse. It is placed at the
88      upper left corner (0,0), by setting m.tx and m.ty
89      to 0. Therefore, the buttons are "in" the viewport,
90      not above it*/
91   tag = swf_InsertTag(tag,ST_DEFINESPRITE);
92   swf_SetU16(tag, 23); //id
93   swf_SetU16(tag, 0); //frames
94   tag = swf_InsertTag(tag,ST_END);
95   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
96   m.tx = 0; //move
97   m.ty = 0;
98   swf_ObjectPlace(tag, 23, 1,&m,0,"viewport");
99     
100 #ifdef USE_WATERMARK
101
102   // Insert Alpha watermark to avoid printing
103
104   tag = swf_InsertTag(tag,ST_DEFINESHAPE3);
105   swf_ShapeNew(&s);
106   rgb.r = rgb.g = rgb.b = 0xff;
107   rgb.a = 0x08;
108   fs1 = swf_ShapeAddSolidFillStyle(s,&rgb);
109   swf_SetU16(tag,ID_WATERMARK);
110   r.xmin = r.ymin = 0;
111   r.xmax = 20*width;
112   r.ymax = 20*height;
113   swf_SetRect(tag,&r); // cover whole viewport
114   swf_SetShapeHeader(tag,s);
115   swf_ShapeSetAll(tag,s,0,0,0,fs1,0);
116   swf_ShapeSetLine(tag,s,20*width,0);
117   swf_ShapeSetLine(tag,s,0,20*height);
118   swf_ShapeSetLine(tag,s,-20*width,0);
119   swf_ShapeSetLine(tag,s,0,-20*height);
120   swf_ShapeSetEnd(tag);
121   swf_ShapeFree(s);  
122
123 #endif // USE_WATERMARK
124
125   for(count=0;count<6;count++)
126   {
127       tag = swf_InsertTag(tag,ST_DEFINESHAPE);
128       swf_ShapeNew(&s);               // create new shape instance
129       rgb.r = rgb.b = rgb.g = 0x00;           
130       ls1 = swf_ShapeAddLineStyle(s,40,&rgb);
131       rgb = button_colors[count];
132       fs1 = swf_ShapeAddSolidFillStyle(s,&rgb);
133       swf_SetU16(tag,32+count);                // now set character ID
134       r.xmin = 0;
135       r.ymin = 0;
136       r.xmax = 20*width;
137       r.ymax = 20*height;
138       swf_SetRect(tag,&r);              // set shape bounds
139       swf_SetShapeHeader(tag,s);        // write all styles to tag
140       if(count&1)
141       {
142           swf_ShapeSetAll(tag,s,0,0,ls1,fs1,0); // move to (0,0), select linestyle ls1 and no fillstyle
143           /* SetLine coordinates are relative.
144              It's important that the start and end points match, otherwise
145              the Macromedia Flash player will crash. */
146           swf_ShapeSetLine(tag,s,20*button_sizex,20*button_sizey);
147           swf_ShapeSetLine(tag,s,-20*button_sizex,20*button_sizey);
148           swf_ShapeSetLine(tag,s,0,-40*button_sizey);
149       } else {
150           swf_ShapeSetAll(tag,s,20*button_sizex,0,ls1,fs1,0);
151           swf_ShapeSetLine(tag,s,-20*button_sizex,20*button_sizey);
152           swf_ShapeSetLine(tag,s,20*button_sizex,20*button_sizey);
153           swf_ShapeSetLine(tag,s,0,-40*button_sizey);
154       }
155       swf_ShapeSetEnd(tag);   // finish drawing
156       swf_ShapeFree(s);   // clean shape structure (which isn't needed anymore after writing the tag)
157   }
158
159     a1 = action_SetTarget(0, "viewport");
160     a1 = action_PreviousFrame(a1);
161     a1 = action_SetTarget(a1, "");
162     a1 = action_End(a1);
163
164     a2 = action_SetTarget(0, "viewport");
165     a2 = action_NextFrame(a2);
166     a2 = action_SetTarget(a2,"");
167     a2 = action_End(a2);
168
169     a3 = action_SetTarget(0,"viewport");
170     a3 = action_Stop(a3);
171     a3 = action_SetTarget(a3,"");
172 #ifdef SUBTITLES
173     a3 = action_PushString(a3,"/:subtitle");
174     a3 = action_PushString(a3,""); //reset variable
175     a3 = action_SetVariable(a3);
176 #endif
177     a3 = action_End(a3);
178
179   for(t=0;t<2;t++)
180   {
181       if(!useDefineButton2)
182       {
183           tag = swf_InsertTag(tag,ST_DEFINEBUTTON);
184           swf_SetU16(tag,30+t); //id
185           swf_ButtonSetRecord(tag,BS_UP|BS_HIT,32+t,1,NULL,NULL);
186           swf_ButtonSetRecord(tag,BS_OVER,34+t,1,NULL,NULL);
187           swf_ButtonSetRecord(tag,BS_DOWN,36+t,1,NULL,NULL);
188           swf_SetU8(tag,0); // end of button records
189          
190           if(t)
191             swf_ActionSet(tag,a2);
192           else
193             swf_ActionSet(tag,a1);
194       }
195       else
196       {
197           tag = swf_InsertTag(tag,ST_DEFINEBUTTON2);
198           swf_SetU16(tag,30+t); //id
199           swf_ButtonSetFlags(tag, 0); //menu=no
200           swf_ButtonSetRecord(tag,BS_UP|BS_HIT,32+t,1,NULL,NULL);
201           swf_ButtonSetRecord(tag,BS_OVER,34+t,1,NULL,NULL);
202           swf_ButtonSetRecord(tag,BS_DOWN,36+t,1,NULL,NULL);
203           swf_SetU8(tag,0); // end of button records
204
205           swf_ButtonSetCondition(tag, BC_OVERDOWN_OVERUP);
206           if(t)
207             swf_ActionSet(tag,a2);
208           else
209             swf_ActionSet(tag,a1);
210            
211           swf_ButtonPostProcess(tag, 1); // don't forget!
212       }
213   }
214
215   tag = swf_InsertTag(tag,ST_DOACTION);
216   swf_ActionSet(tag,a3);
217
218   m.tx = 0; //move
219   m.ty = 0;
220   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
221   swf_ObjectPlace(tag, 30, 3,&m,0,0);
222   m.tx = button_sizex*30;
223   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
224   swf_ObjectPlace(tag, 31, 4,&m,0,0);
225
226 #ifdef USE_WATERMARK
227
228   // place watermark
229
230   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
231   swf_ObjectPlace(tag, ID_WATERMARK, 2, &m, 0,0);
232
233 #endif // USE_WATERMARK
234   
235   swf_ActionFree(a1);
236   swf_ActionFree(a2);
237   swf_ActionFree(a3);
238
239 #ifdef SUBTITLES
240   tag = swf_InsertTag(tag,ST_DEFINEFONT2); {
241       U8 data[] = {0x90, 0x00, 0x0f, 0x54, 0x69, 0x6d, 0x65, 0x73, 
242                    0x20, 0x4e, 0x65, 0x77, 0x20, 0x52, 0x6f, 0x6d, 
243                    0x61, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
244                    0x00, 0x00, 0x00, 0x00};
245       swf_SetU16(tag, 0x76); //id
246       swf_SetBlock(tag, data, sizeof(data));
247   }
248   tag = swf_InsertTag(tag,ST_DEFINEEDITTEXT); {
249       EditTextLayout layout;
250       layout.align = 0;
251       layout.leftmargin = 0;
252       layout.rightmargin = 0;
253       layout.indent = 0;
254       layout.leading = 0;
255
256       swf_SetU16(tag, 0x77);//id
257       r.xmin = button_sizex*60;
258       r.xmax = r.xmin+ 826*20;
259       r.ymin = 0;
260       r.ymax = button_sizey*80;
261       rgb.r = rgb.g = rgb.b = 0;
262       rgb.a = 255;
263       swf_SetEditText(tag, ET_MULTILINE|ET_READONLY, r, 0, &rgb, 0, 0x76, button_sizey*40, &layout, "/:subtitle");
264       m.tx = m.ty = 0;
265   }
266   tag = swf_InsertTag(tag,ST_PLACEOBJECT2);
267   swf_ObjectPlace(tag, 0x77, 5,&m,0,0);
268 #endif
269
270   tag = swf_InsertTag(tag,ST_SHOWFRAME);
271   tag = swf_InsertTag(tag,ST_END);
272   
273
274   f = open("simple_viewer.swf",O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
275   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
276   close(f);
277
278   swf_FreeTags(&swf);                       // cleanup
279   return 0;
280 }
281
282