more horizontal refactoring
[swftools.git] / lib / example / hexfont.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 int main()
31 {
32     char* hex = "0123456789ABCDEF";
33     SWFFONT* font = swf_LoadFont("../../doc/Courier.swf");
34     SWFFONT hexfont;
35
36     memset(&hexfont, 0, sizeof(hexfont));
37     hexfont.name = (char*)"HexFont";
38     hexfont.layout = malloc(sizeof(SWFLAYOUT));
39     hexfont.numchars = 256;
40     hexfont.maxascii = 256;
41     hexfont.encoding = 32;
42     hexfont.glyph2ascii = malloc(sizeof(U16)*256);
43     hexfont.ascii2glyph = malloc(sizeof(int)*256);
44     hexfont.glyph = malloc(sizeof(SWFGLYPH)*256);
45     hexfont.glyphnames = malloc(sizeof(char*)*256);
46     hexfont.layout->bounds = malloc(sizeof(SRECT)*256);
47     hexfont.layout->kerningcount = 0;
48     hexfont.layout->kerning = 0;
49     int t;
50     int ymax =-0x7fffffff;
51     int ymin = 0x7fffffff;
52     for(t=0;t<256;t++) 
53     {
54         char nr[5];
55         nr[0] = '[';
56         nr[1] = hex[t/16];
57         nr[2] = hex[t&15];
58         nr[3] = ']';
59         nr[4] = 0;
60         drawer_t draw;
61         swf_Shape01DrawerInit(&draw, 0);
62         swf_DrawText(&draw, font, (int)(10*20), nr);
63         draw.finish(&draw);
64         hexfont.glyph[t].shape = swf_ShapeDrawerToShape(&draw);
65         hexfont.layout->bounds[t] = swf_ShapeDrawerGetBBox(&draw);
66         hexfont.glyph[t].advance = hexfont.layout->bounds[t].xmax + hexfont.layout->bounds[t].xmin;
67         draw.dealloc(&draw);
68         hexfont.glyph2ascii[t] = t;
69         hexfont.ascii2glyph[t] = t;
70         hexfont.glyphnames[t] = strdup(nr);
71         if(hexfont.layout->bounds[t].ymax > ymax)
72             ymax = hexfont.layout->bounds[t].ymax;
73         if(hexfont.layout->bounds[t].ymin < ymin)
74             ymin = hexfont.layout->bounds[t].ymin;
75         if(t>=0xe4) {
76             hexfont.glyph2ascii[t] = 0;
77         }
78     }
79
80     hexfont.layout->ascent = ymin<0?-ymin:0;
81     hexfont.layout->descent = ymax>0?ymax:0;
82     hexfont.layout->leading = hexfont.layout->ascent + hexfont.layout->descent;
83
84     swf_WriteFont(&hexfont, "hexfont.swf");
85     return 0;
86 }
87