summaryrefslogtreecommitdiff
path: root/lib/example/glyphshape.c
blob: d1e8d0176489e8048592c9a31c2b5690c7b65bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* glyphshape.c

   Part of the swftools package.

   Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
 
   This file is distributed under the GPL, see file COPYING for details 

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include "../rfxswf.h"

#include "demofont.c"  // six glyphs only:  f, l, o, s, t, w
//#include "stdfonts.c"

#define BANNER_TEXT     "swftools" 
#define ID_FONT         1000
#define ID_SHAPES       2000 // to 2255

TAG * PlaceGlyph(TAG * t,SWFFONT * font,U8 glyph,U16 depth,int x,int y,int angle)
{ MATRIX m;
  t = swf_InsertTag(t,ST_PLACEOBJECT2);

  swf_GetMatrix(NULL,&m);
  
  if (angle)
  { m.sy = m.sx = (int)(cos(((double)(angle))/128*3.14159)*0x10000);
    m.r0 = (int)(sin(((double)(angle))/128*3.14159)*0x10000);
    m.r1 = -m.r0;
  }
  else m.sx = m.sy = 0x10000;

  // move origin

  m.tx = x+((m.r1-m.sx)/0x100); // this is correct
  m.ty = y+((m.sx-m.r0)/0x100);

//  m.tx = x+((m.r1-m.sx)/0x40); // this looks crazier ;-)
//  m.ty = y+((m.sx-m.r0)/0x40);
  

  if (font)
    swf_ObjectPlace(t,ID_SHAPES+glyph,depth,&m,NULL,NULL);
  else swf_ObjectMove(t,depth,&m,NULL);
        
  return t;
}

// some helper functions which define the figure

int posX(int count)
{ return (int)(-sin(((double)count)/1024*3.14159)*3000);
}
int posY(int count)
{ return (int)(sin(((double)count)/2048*3.14159)*3200);
}
int angle(int count)
{ return (int)(pow(cos(((double)count)/2048*3.14159),3)*110);
  // this is not the mathematical correct solution, but it works & looks nice
}

int main(int argc, char ** argv)
{ SWF swf;
  TAG * t;
  SRECT r;
  RGBA rgb;
  MATRIX m;
  U8 abits, gbits;

  int f,i,frame;
  int width = 400;
  int height = 400;
  char * s = BANNER_TEXT;

  SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here

  swf_FontInitUsage(font);
  swf_FontUse(font,s);        
  swf_FontReduce(font);            // make sure that gid's point to the specific glyphs
  
  memset(&swf,0x00,sizeof(SWF));

  swf.fileVersion    = 4;
  swf.frameRate      = 0x1000;
  swf.movieSize.xmax = 20*width;
  swf.movieSize.ymax = 20*height;

  swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
  t = swf.firstTag;

        rgb.r = 0xff;
        rgb.g = 0xff;
        rgb.b = 0xff;
        swf_SetRGB(t,&rgb);

  // define Glyph Shapes

  for (i=0;i<font->numchars;i++)
  { if (font->glyph[i].shape)
    { SHAPE * s;
      int fs;
        
      t = swf_InsertTag(t,ST_DEFINESHAPE);

        swf_ShapeNew(&s);
        
        rgb.r = rgb.g = rgb.b = i;
        
        swf_ShapeAddSolidFillStyle(s,&rgb);

        swf_SetU16(t,ID_SHAPES+i);

        r.xmin = -50*20;     r.ymin = -50*20;  // Glyph Shapes are placed into a 2000*2000 box
        r.ymax =   50*20;    r.xmax =  50*20;  // where (1000,1000) is the origin

        swf_SetRect(t,&r);

        swf_SetShapeStyles(t,s);        // rest of header is set by swf_SetSimpleShape
        swf_SetSimpleShape(t,font->glyph[i].shape);
        swf_ShapeFree(s);
        
    }
  }

  for (frame=0;frame<128;frame++)
  { int cnt = 4096-frame*32;
    for (i=0;i<strlen(s);i++)
    { int glyph = font->ascii2glyph[s[i]];
      int advance = font->glyph[glyph].advance/2;
      int cnt2 = cnt+=advance/2;
      t = PlaceGlyph(t,frame?NULL:font,glyph,i+1,
                     200*20+posX(cnt2),
                     200*20+posY(cnt2),
                     angle(cnt2));
      cnt+=advance;
    }
    t = swf_InsertTag(t,ST_SHOWFRAME);
  }
  
  
    t = swf_InsertTag(t,ST_END);
 
//  swf_WriteCGI(&swf);
 
  f = open("glyphshape.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
  if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
  close(f);

  swf_FreeTags(&swf);

#ifdef __NT__
  system("start ..\\glyphshape.swf");
#endif
  
  return 0;
}