new example: glyphshape.c
[swftools.git] / lib / example / glyphshape.c
diff --git a/lib/example/glyphshape.c b/lib/example/glyphshape.c
new file mode 100644 (file)
index 0000000..0263ca0
--- /dev/null
@@ -0,0 +1,162 @@
+/* glyphshape.c\r
+\r
+   Part of the swftools package.\r
+\r
+   Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>\r
\r
+   This file is distributed under the GPL, see file COPYING for details \r
+\r
+*/\r
+\r
+#include <stdio.h>\r
+#include <fcntl.h>\r
+#include <math.h>\r
+#include "../rfxswf.h"\r
+\r
+#include "demofont.c"  // six glyphs only:  f, l, o, s, t, w\r
+//#include "stdfonts.c"\r
+\r
+#define BANNER_TEXT     "swftools" \r
+#define ID_FONT         1000\r
+#define ID_SHAPES       2000 // to 2255\r
+\r
+TAG * PlaceGlyph(TAG * t,SWFFONT * font,U8 glyph,U16 depth,int x,int y,int angle)\r
+{ MATRIX m;\r
+  t = swf_InsertTag(t,ST_PLACEOBJECT2);\r
+\r
+  swf_GetMatrix(NULL,&m);\r
+  \r
+  if (angle)\r
+  { m.sy = m.sx = (int)(cos(((double)(angle))/128*3.14159)*0x10000);\r
+    m.r0 = (int)(sin(((double)(angle))/128*3.14159)*0x10000);\r
+    m.r1 = -m.r0;\r
+  }\r
+  else m.sx = m.sy = 0x10000;\r
+\r
+  // move origin\r
+\r
+  m.tx = x+((m.r1-m.sx)/0x100); // this is correct\r
+  m.ty = y+((m.sx-m.r0)/0x100);\r
+\r
+//  m.tx = x+((m.r1-m.sx)/0x40); // this looks crazier ;-)\r
+//  m.ty = y+((m.sx-m.r0)/0x40);\r
+  \r
+\r
+  if (font)\r
+    swf_ObjectPlace(t,ID_SHAPES+glyph,depth,&m,NULL,NULL);\r
+  else swf_ObjectMove(t,depth,&m,NULL);\r
+        \r
+  return t;\r
+}\r
+\r
+// some helper functions which define the figure\r
+\r
+int posX(int count)\r
+{ return (int)(-sin(((double)count)/1024*3.14159)*3000);\r
+}\r
+int posY(int count)\r
+{ return (int)(sin(((double)count)/2048*3.14159)*3200);\r
+}\r
+int angle(int count)\r
+{ return (int)(pow(cos(((double)count)/2048*3.14159),3)*110);\r
+  // this is not the mathematical correct solution, but it works & looks nice\r
+}\r
+\r
+int main(int argc, char ** argv)\r
+{ SWF swf;\r
+  TAG * t;\r
+  SRECT r;\r
+  RGBA rgb;\r
+  MATRIX m;\r
+  U8 abits, gbits;\r
+\r
+  int f,i,frame;\r
+  int width = 400;\r
+  int height = 400;\r
+  char * s = BANNER_TEXT;\r
+\r
+  FONTUSAGE use;\r
+  SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here\r
+\r
+  swf_FontInitUsage(&use);\r
+  swf_FontUse(&use,s);        \r
+  swf_FontReduce(font,&use);            // make sure that gid's point to the specific glyphs\r
+  \r
+  memset(&swf,0x00,sizeof(SWF));\r
+\r
+  swf.fileVersion    = 4;\r
+  swf.frameRate      = 0x1000;\r
+  swf.movieSize.xmax = 20*width;\r
+  swf.movieSize.ymax = 20*height;\r
+\r
+  swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);\r
+  t = swf.firstTag;\r
+\r
+        rgb.r = 0xff;\r
+        rgb.g = 0xff;\r
+        rgb.b = 0xff;\r
+        swf_SetRGB(t,&rgb);\r
+\r
+  // define Glyph Shapes\r
+\r
+  for (i=0;i<font->numchars;i++)\r
+  { if (font->glyph[i].shape)\r
+    { SHAPE * s;\r
+      int fs;\r
+        \r
+      t = swf_InsertTag(t,ST_DEFINESHAPE);\r
+\r
+        swf_ShapeNew(&s);\r
+        \r
+        rgb.r = rgb.g = rgb.b = i;\r
+        \r
+        swf_ShapeAddSolidFillStyle(s,&rgb);\r
+\r
+        swf_SetU16(t,ID_SHAPES+i);\r
+\r
+        r.xmin = -50*20;     r.ymin = -50*20;  // Glyph Shapes are placed into a 2000*2000 box\r
+        r.ymax =   50*20;    r.xmax =  50*20;  // where (1000,1000) is the origin\r
+\r
+        swf_SetRect(t,&r);\r
+\r
+        swf_SetShapeStyles(t,s);        // rest of header is set by swf_SetSimpleShape\r
+        swf_SetSimpleShape(t,font->glyph[i].shape);\r
+        swf_ShapeFree(s);\r
+        \r
+    }\r
+  }\r
+\r
+  for (frame=0;frame<128;frame++)\r
+  { int cnt = 4096-frame*32;\r
+    for (i=0;i<strlen(s);i++)\r
+    { int glyph = font->ascii2glyph[s[i]];\r
+      int advance = font->glyph[glyph].advance/2;\r
+      int cnt2 = cnt+=advance/2;\r
+      t = PlaceGlyph(t,frame?NULL:font,glyph,i+1,\r
+                     200*20+posX(cnt2),\r
+                     200*20+posY(cnt2),\r
+                     angle(cnt2));\r
+      cnt+=advance;\r
+    }\r
+    t = swf_InsertTag(t,ST_SHOWFRAME);\r
+  }\r
+  \r
+  \r
+    t = swf_InsertTag(t,ST_END);\r
\r
+//  swf_WriteCGI(&swf);\r
\r
+  f = open("glyphshape.swf",O_RDWR|O_CREAT|O_TRUNC,0644);\r
+  if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");\r
+  close(f);\r
+\r
+  swf_FreeTags(&swf);\r
+\r
+#ifdef __NT__\r
+  system("start ..\\glyphshape.swf");\r
+#endif\r
+  \r
+  return 0;\r
+}\r
+\r
+\r