Generated from configure.in
[swftools.git] / lib / example / glyphshape.c
1 /* glyphshape.c\r
2 \r
3    Part of the swftools package.\r
4 \r
5    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>\r
6  \r
7    This file is distributed under the GPL, see file COPYING for details \r
8 \r
9    This program is free software; you can redistribute it and/or modify\r
10    it under the terms of the GNU General Public License as published by\r
11    the Free Software Foundation; either version 2 of the License, or\r
12    (at your option) any later version.\r
13 \r
14    This program is distributed in the hope that it will be useful,\r
15    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17    GNU General Public License for more details.\r
18 \r
19    You should have received a copy of the GNU General Public License\r
20    along with this program; if not, write to the Free Software\r
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */\r
22 \r
23 #include <stdio.h>\r
24 #include <fcntl.h>\r
25 #include <math.h>\r
26 #include "../rfxswf.h"\r
27 \r
28 #include "demofont.c"  // six glyphs only:  f, l, o, s, t, w\r
29 //#include "stdfonts.c"\r
30 \r
31 #define BANNER_TEXT     "swftools" \r
32 #define ID_FONT         1000\r
33 #define ID_SHAPES       2000 // to 2255\r
34 \r
35 TAG * PlaceGlyph(TAG * t,SWFFONT * font,U8 glyph,U16 depth,int x,int y,int angle)\r
36 { MATRIX m;\r
37   t = swf_InsertTag(t,ST_PLACEOBJECT2);\r
38 \r
39   swf_GetMatrix(NULL,&m);\r
40   \r
41   if (angle)\r
42   { m.sy = m.sx = (int)(cos(((double)(angle))/128*3.14159)*0x10000);\r
43     m.r0 = (int)(sin(((double)(angle))/128*3.14159)*0x10000);\r
44     m.r1 = -m.r0;\r
45   }\r
46   else m.sx = m.sy = 0x10000;\r
47 \r
48   // move origin\r
49 \r
50   m.tx = x+((m.r1-m.sx)/0x100); // this is correct\r
51   m.ty = y+((m.sx-m.r0)/0x100);\r
52 \r
53 //  m.tx = x+((m.r1-m.sx)/0x40); // this looks crazier ;-)\r
54 //  m.ty = y+((m.sx-m.r0)/0x40);\r
55   \r
56 \r
57   if (font)\r
58     swf_ObjectPlace(t,ID_SHAPES+glyph,depth,&m,NULL,NULL);\r
59   else swf_ObjectMove(t,depth,&m,NULL);\r
60         \r
61   return t;\r
62 }\r
63 \r
64 // some helper functions which define the figure\r
65 \r
66 int posX(int count)\r
67 { return (int)(-sin(((double)count)/1024*3.14159)*3000);\r
68 }\r
69 int posY(int count)\r
70 { return (int)(sin(((double)count)/2048*3.14159)*3200);\r
71 }\r
72 int angle(int count)\r
73 { return (int)(pow(cos(((double)count)/2048*3.14159),3)*110);\r
74   // this is not the mathematical correct solution, but it works & looks nice\r
75 }\r
76 \r
77 int main(int argc, char ** argv)\r
78 { SWF swf;\r
79   TAG * t;\r
80   SRECT r;\r
81   RGBA rgb;\r
82   MATRIX m;\r
83   U8 abits, gbits;\r
84 \r
85   int f,i,frame;\r
86   int width = 400;\r
87   int height = 400;\r
88   char * s = BANNER_TEXT;\r
89 \r
90   FONTUSAGE use;\r
91   SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here\r
92 \r
93   swf_FontInitUsage(&use);\r
94   swf_FontUse(&use,s);        \r
95   swf_FontReduce(font,&use);            // make sure that gid's point to the specific glyphs\r
96   \r
97   memset(&swf,0x00,sizeof(SWF));\r
98 \r
99   swf.fileVersion    = 4;\r
100   swf.frameRate      = 0x1000;\r
101   swf.movieSize.xmax = 20*width;\r
102   swf.movieSize.ymax = 20*height;\r
103 \r
104   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);\r
105   t = swf.firstTag;\r
106 \r
107         rgb.r = 0xff;\r
108         rgb.g = 0xff;\r
109         rgb.b = 0xff;\r
110         swf_SetRGB(t,&rgb);\r
111 \r
112   // define Glyph Shapes\r
113 \r
114   for (i=0;i<font->numchars;i++)\r
115   { if (font->glyph[i].shape)\r
116     { SHAPE * s;\r
117       int fs;\r
118         \r
119       t = swf_InsertTag(t,ST_DEFINESHAPE);\r
120 \r
121         swf_ShapeNew(&s);\r
122         \r
123         rgb.r = rgb.g = rgb.b = i;\r
124         \r
125         swf_ShapeAddSolidFillStyle(s,&rgb);\r
126 \r
127         swf_SetU16(t,ID_SHAPES+i);\r
128 \r
129         r.xmin = -50*20;     r.ymin = -50*20;  // Glyph Shapes are placed into a 2000*2000 box\r
130         r.ymax =   50*20;    r.xmax =  50*20;  // where (1000,1000) is the origin\r
131 \r
132         swf_SetRect(t,&r);\r
133 \r
134         swf_SetShapeStyles(t,s);        // rest of header is set by swf_SetSimpleShape\r
135         swf_SetSimpleShape(t,font->glyph[i].shape);\r
136         swf_ShapeFree(s);\r
137         \r
138     }\r
139   }\r
140 \r
141   for (frame=0;frame<128;frame++)\r
142   { int cnt = 4096-frame*32;\r
143     for (i=0;i<strlen(s);i++)\r
144     { int glyph = font->ascii2glyph[s[i]];\r
145       int advance = font->glyph[glyph].advance/2;\r
146       int cnt2 = cnt+=advance/2;\r
147       t = PlaceGlyph(t,frame?NULL:font,glyph,i+1,\r
148                      200*20+posX(cnt2),\r
149                      200*20+posY(cnt2),\r
150                      angle(cnt2));\r
151       cnt+=advance;\r
152     }\r
153     t = swf_InsertTag(t,ST_SHOWFRAME);\r
154   }\r
155   \r
156   \r
157     t = swf_InsertTag(t,ST_END);\r
158  \r
159 //  swf_WriteCGI(&swf);\r
160  \r
161   f = open("glyphshape.swf",O_RDWR|O_CREAT|O_TRUNC,0644);\r
162   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");\r
163   close(f);\r
164 \r
165   swf_FreeTags(&swf);\r
166 \r
167 #ifdef __NT__\r
168   system("start ..\\glyphshape.swf");\r
169 #endif\r
170   \r
171   return 0;\r
172 }\r
173 \r
174 \r