new example: glyphshape.c
[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 */\r
10 \r
11 #include <stdio.h>\r
12 #include <fcntl.h>\r
13 #include <math.h>\r
14 #include "../rfxswf.h"\r
15 \r
16 #include "demofont.c"  // six glyphs only:  f, l, o, s, t, w\r
17 //#include "stdfonts.c"\r
18 \r
19 #define BANNER_TEXT     "swftools" \r
20 #define ID_FONT         1000\r
21 #define ID_SHAPES       2000 // to 2255\r
22 \r
23 TAG * PlaceGlyph(TAG * t,SWFFONT * font,U8 glyph,U16 depth,int x,int y,int angle)\r
24 { MATRIX m;\r
25   t = swf_InsertTag(t,ST_PLACEOBJECT2);\r
26 \r
27   swf_GetMatrix(NULL,&m);\r
28   \r
29   if (angle)\r
30   { m.sy = m.sx = (int)(cos(((double)(angle))/128*3.14159)*0x10000);\r
31     m.r0 = (int)(sin(((double)(angle))/128*3.14159)*0x10000);\r
32     m.r1 = -m.r0;\r
33   }\r
34   else m.sx = m.sy = 0x10000;\r
35 \r
36   // move origin\r
37 \r
38   m.tx = x+((m.r1-m.sx)/0x100); // this is correct\r
39   m.ty = y+((m.sx-m.r0)/0x100);\r
40 \r
41 //  m.tx = x+((m.r1-m.sx)/0x40); // this looks crazier ;-)\r
42 //  m.ty = y+((m.sx-m.r0)/0x40);\r
43   \r
44 \r
45   if (font)\r
46     swf_ObjectPlace(t,ID_SHAPES+glyph,depth,&m,NULL,NULL);\r
47   else swf_ObjectMove(t,depth,&m,NULL);\r
48         \r
49   return t;\r
50 }\r
51 \r
52 // some helper functions which define the figure\r
53 \r
54 int posX(int count)\r
55 { return (int)(-sin(((double)count)/1024*3.14159)*3000);\r
56 }\r
57 int posY(int count)\r
58 { return (int)(sin(((double)count)/2048*3.14159)*3200);\r
59 }\r
60 int angle(int count)\r
61 { return (int)(pow(cos(((double)count)/2048*3.14159),3)*110);\r
62   // this is not the mathematical correct solution, but it works & looks nice\r
63 }\r
64 \r
65 int main(int argc, char ** argv)\r
66 { SWF swf;\r
67   TAG * t;\r
68   SRECT r;\r
69   RGBA rgb;\r
70   MATRIX m;\r
71   U8 abits, gbits;\r
72 \r
73   int f,i,frame;\r
74   int width = 400;\r
75   int height = 400;\r
76   char * s = BANNER_TEXT;\r
77 \r
78   FONTUSAGE use;\r
79   SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here\r
80 \r
81   swf_FontInitUsage(&use);\r
82   swf_FontUse(&use,s);        \r
83   swf_FontReduce(font,&use);            // make sure that gid's point to the specific glyphs\r
84   \r
85   memset(&swf,0x00,sizeof(SWF));\r
86 \r
87   swf.fileVersion    = 4;\r
88   swf.frameRate      = 0x1000;\r
89   swf.movieSize.xmax = 20*width;\r
90   swf.movieSize.ymax = 20*height;\r
91 \r
92   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);\r
93   t = swf.firstTag;\r
94 \r
95         rgb.r = 0xff;\r
96         rgb.g = 0xff;\r
97         rgb.b = 0xff;\r
98         swf_SetRGB(t,&rgb);\r
99 \r
100   // define Glyph Shapes\r
101 \r
102   for (i=0;i<font->numchars;i++)\r
103   { if (font->glyph[i].shape)\r
104     { SHAPE * s;\r
105       int fs;\r
106         \r
107       t = swf_InsertTag(t,ST_DEFINESHAPE);\r
108 \r
109         swf_ShapeNew(&s);\r
110         \r
111         rgb.r = rgb.g = rgb.b = i;\r
112         \r
113         swf_ShapeAddSolidFillStyle(s,&rgb);\r
114 \r
115         swf_SetU16(t,ID_SHAPES+i);\r
116 \r
117         r.xmin = -50*20;     r.ymin = -50*20;  // Glyph Shapes are placed into a 2000*2000 box\r
118         r.ymax =   50*20;    r.xmax =  50*20;  // where (1000,1000) is the origin\r
119 \r
120         swf_SetRect(t,&r);\r
121 \r
122         swf_SetShapeStyles(t,s);        // rest of header is set by swf_SetSimpleShape\r
123         swf_SetSimpleShape(t,font->glyph[i].shape);\r
124         swf_ShapeFree(s);\r
125         \r
126     }\r
127   }\r
128 \r
129   for (frame=0;frame<128;frame++)\r
130   { int cnt = 4096-frame*32;\r
131     for (i=0;i<strlen(s);i++)\r
132     { int glyph = font->ascii2glyph[s[i]];\r
133       int advance = font->glyph[glyph].advance/2;\r
134       int cnt2 = cnt+=advance/2;\r
135       t = PlaceGlyph(t,frame?NULL:font,glyph,i+1,\r
136                      200*20+posX(cnt2),\r
137                      200*20+posY(cnt2),\r
138                      angle(cnt2));\r
139       cnt+=advance;\r
140     }\r
141     t = swf_InsertTag(t,ST_SHOWFRAME);\r
142   }\r
143   \r
144   \r
145     t = swf_InsertTag(t,ST_END);\r
146  \r
147 //  swf_WriteCGI(&swf);\r
148  \r
149   f = open("glyphshape.swf",O_RDWR|O_CREAT|O_TRUNC,0644);\r
150   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");\r
151   close(f);\r
152 \r
153   swf_FreeTags(&swf);\r
154 \r
155 #ifdef __NT__\r
156   system("start ..\\glyphshape.swf");\r
157 #endif\r
158   \r
159   return 0;\r
160 }\r
161 \r
162 \r