merged in fontalignzones branch
[swftools.git] / lib / example / alignzones.c
1 /* hexfont.c
2
3    Example for how to construct an SWF font from another SWF font.
4    
5    Part of the swftools package.
6
7    Copyright (c) 2004 Matthias Kramm <kramm@quiss.org>
8  
9    This file is distributed under the GPL, see file COPYING for details 
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 static void write_font(SWFFONT * font, char *filename)
31 {
32     SWF swf;
33     TAG *t;
34     SRECT r;
35     RGBA rgb;
36     int f;
37     int useDefineFont2 = 1;
38     int storeGlyphNames = 1;
39    
40 #define WRITEFONTID 8888
41     font->id = WRITEFONTID;
42
43     memset(&swf, 0x00, sizeof(SWF));
44
45     swf.fileVersion = 9;
46     swf.frameRate = 0x4000;
47
48     t = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
49     swf.firstTag = t;
50     rgb.r = 0xef;
51     rgb.g = 0xef;
52     rgb.b = 0xff;
53     swf_SetRGB(t, &rgb);
54     
55     t = swf_InsertTag(t, ST_DEFINEFONT3);
56     swf_FontSetDefine2(t, font);
57
58     t = swf_InsertTag(t, ST_DEFINEFONTALIGNZONES);
59     swf_SetU16(t, font->id);
60     swf_SetU8(t, 0); //thin
61     int i;
62     for(i=0;i<256;i++) {
63         swf_SetU8(t, 2);
64         swf_SetF16(t, 82.0 / 1024.0);
65         swf_SetF16(t, 82.0 / 1024.0);
66         swf_SetF16(t, ((i%16/2)*82.0) / 1024.0);
67         swf_SetF16(t, ((i/16/2)*82.0) / 1024.0);
68         /*
69         if(i<128 && (i&15)<8) {
70             swf_SetF16(t, 0.0);
71             swf_SetF16(t, 0.0);
72             swf_SetF16(t, 640.0 / 1024.0);
73             swf_SetF16(t, 640.0 / 1024.0);
74         } else if(i<128 && (i&15)>=8) {
75             swf_SetF16(t, 0.0 / 1024.0);
76             swf_SetF16(t, 0.0   / 1024.0);
77             swf_SetF16(t, 330.0 / 1024.0);
78             swf_SetF16(t, 640.0 / 1024.0);
79         } else if(i>=128 && (i&15)<8) {
80             swf_SetF16(t, 0.0   / 1024.0);
81             swf_SetF16(t, 0.0 / 1024.0);
82             swf_SetF16(t, 640.0 / 1024.0);
83             swf_SetF16(t, 330.0 / 1024.0);
84         } else {
85             swf_SetF16(t, 0.0 / 1024.0);
86             swf_SetF16(t, 0.0 / 1024.0);
87             swf_SetF16(t, 330.0 / 1024.0);
88             swf_SetF16(t, 330.0 / 1024.0);
89         }*/
90         swf_SetU8(t, 3); // x and y
91     }
92
93     int s;
94     int xmax = 0;
95     int ymax = 0;
96     int ypos = 1;
97     U8 gbits, abits;
98     int x, y, c;
99     int range = font->maxascii;
100
101     c = 0;
102     range = 256;
103
104     xmax = 1280;
105     ymax = 1280*20;
106
107     swf.movieSize.xmax = xmax * 20;
108     swf.movieSize.ymax = ymax;
109
110     t = swf_InsertTag(t, ST_DEFINETEXT);
111     swf_SetU16(t, font->id + 1);        // ID
112     r.xmin = 0;
113     r.ymin = 0;
114     r.xmax = swf.movieSize.xmax;
115     r.ymax = swf.movieSize.ymax;
116     swf_SetRect(t, &r);
117     swf_SetMatrix(t, NULL);
118     abits = swf_CountBits(xmax * 16, 0);
119     gbits = 8;
120     swf_SetU8(t, gbits);
121     swf_SetU8(t, abits);
122
123     rgb.r = 0x00;
124     rgb.g = 0x00;
125     rgb.b = 0x00;
126     ypos = 2;
127     
128     int textscale = 1024;
129     for (y = 0; y < ((range + 15) / 16); y++) {
130         for (x = 0; x < 16; x++) {
131             //swf_TextSetInfoRecord(t, font, textscale, &rgb, x*64*20+64*20+10+(x+y)*20, y*64*20+128*20+10+(x^y)*20);
132             swf_TextSetInfoRecord(t, font, textscale, &rgb, x*64*20+64*20+10, y*64*20+128*20+10);
133             int g = y * 16 + x;
134             swf_SetU8(t, 1);
135             swf_SetBits(t, g, gbits);
136             swf_SetBits(t, 0, abits);
137             swf_ResetWriteBits(t);
138         }
139     }
140     swf_SetU8(t, 0);
141                  
142     t = swf_InsertTag(t, ST_CSMTEXTSETTINGS);
143     swf_SetU16(t, font->id + 1);
144     swf_SetU8(t, (1<<3)//grid
145                  |0x40//flashtype
146                  );
147     swf_SetU32(t, 0x20000);//thickness
148     swf_SetU32(t, 0x800000);//sharpness
149     swf_SetU8(t, 0);//reserved
150
151     t = swf_InsertTag(t, ST_PLACEOBJECT2);
152     swf_ObjectPlace(t, font->id + 1, 1, NULL, NULL, NULL);
153
154     t = swf_InsertTag(t, ST_SHOWFRAME);
155     t = swf_InsertTag(t, ST_END);
156
157     f = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644);
158     if FAILED
159         (swf_WriteSWF(f, &swf)) fprintf(stderr, "WriteSWF() failed in writeFont().\n");
160     close(f);
161
162     swf_FreeTags(&swf);
163 }
164
165
166 int main()
167 {
168     SWFFONT hexfont;
169
170     memset(&hexfont, 0, sizeof(hexfont));
171     hexfont.name = (U8*)"HexFont";
172     hexfont.layout = malloc(sizeof(SWFLAYOUT));
173     hexfont.numchars = 256;
174     hexfont.maxascii = 256;
175     hexfont.encoding = 32;
176     hexfont.glyph2ascii = malloc(sizeof(U16)*256);
177     hexfont.ascii2glyph = malloc(sizeof(int)*256);
178     hexfont.glyph = malloc(sizeof(SWFGLYPH)*256);
179     hexfont.glyphnames = malloc(sizeof(char*)*256);
180     hexfont.layout->bounds = malloc(sizeof(SRECT)*256);
181     hexfont.layout->kerningcount = 0;
182     hexfont.layout->kerning = 0;
183     int t;
184     int ymax =-0x7fffffff;
185     int ymin = 0x7fffffff;
186     for(t=0;t<256;t++) 
187     {
188         drawer_t _draw,*draw=&_draw;
189         swf_Shape01DrawerInit(draw, 0);
190         int x,y;
191         FPOINT d;
192         int rx = 0;//t&15;
193         int ry = 0;//(t&15)^0x05;
194 #define S {d.x*=20*4+2;d.y*=20*4+2;}
195         if(1) {
196             for(x=0;x<8;x++) 
197             for(y=0;y<8;y++) {
198                 if((x^y)&1) {
199                     d.x=x;d.y=-y;     S;draw->moveTo(draw, &d);
200                     d.x=x+1;d.y=-y;   S;draw->lineTo(draw, &d);
201                     d.x=x+1;d.y=-y-1; S;draw->lineTo(draw, &d);
202                     d.x=x;d.y=-y-1;   S;draw->lineTo(draw, &d);
203                     d.x=x;d.y=-y;     S;draw->lineTo(draw, &d);
204                 }
205             }
206         } else {
207             d.x=0;d.y=-0;   S;draw->moveTo(draw, &d);
208             d.x=0;d.y=-8;   S;draw->lineTo(draw, &d);
209             d.x=8;d.y=-8;   S;draw->lineTo(draw, &d);
210             d.x=8;d.y=-0;   S;draw->lineTo(draw, &d);
211             d.x=0;d.y=-0;   S;draw->lineTo(draw, &d);
212
213             d.x=1;d.y=-1;   S;draw->moveTo(draw, &d);
214             d.x=7;d.y=-1;   S;draw->lineTo(draw, &d);
215             d.x=7;d.y=-7;   S;draw->lineTo(draw, &d);
216             d.x=1;d.y=-7;   S;draw->lineTo(draw, &d);
217             d.x=1;d.y=-1;   S;draw->lineTo(draw, &d);
218         }
219         draw->finish(draw);
220         hexfont.glyph[t].shape = swf_ShapeDrawerToShape(draw);
221         hexfont.layout->bounds[t] = swf_ShapeDrawerGetBBox(draw);
222         hexfont.glyph[t].advance = hexfont.layout->bounds[t].xmax + hexfont.layout->bounds[t].xmin;
223         draw->dealloc(draw);
224
225         hexfont.glyph2ascii[t] = t;
226         hexfont.ascii2glyph[t] = t;
227         hexfont.glyphnames[t] = 0;
228         if(hexfont.layout->bounds[t].ymax > ymax)
229             ymax = hexfont.layout->bounds[t].ymax;
230         if(hexfont.layout->bounds[t].ymin < ymin)
231             ymin = hexfont.layout->bounds[t].ymin;
232
233         if(t>=0xe4) {
234             /* breaks flashtype, but not (non-flashtype) definefont2 */
235             //hexfont.glyph2ascii[t] = 0;
236         }
237     }
238     hexfont.layout->ascent = ymin<0?-ymin:0;
239     hexfont.layout->descent = ymax>0?ymax:0;
240     hexfont.layout->leading = hexfont.layout->ascent + hexfont.layout->descent;
241
242     write_font(&hexfont, "alignzones.swf");
243     return 0;
244 }
245