more horizontal refactoring
[swftools.git] / lib / example / text.c
1 /* text.c
2
3    Example for including and using fonts 
4    
5    Part of the swftools package.
6
7    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
8  
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <math.h>
26 #include "../rfxswf.h"
27
28 #include "demofont.c"  // five glyphs only:  f, l, o, s, w
29
30 /*
31     Due to copyright reasons we don't include full typesets into
32     the swftools package. But you can easily create fontdumps
33     of your desired typo:
34
35     * Create a swf file with all characters of your desired fonts
36       (with any tool that can output swf files)
37
38     * use the dumpfont example in this directory to dump font code
39
40     * include dump code and adjust Font_<Fontname>() calls.
41
42     Note: pdf2swf (Version 0.1.0) doesn't write ST_DEFINEFONTINFO tags,
43     so you can't extract fonts out of documents made with pdf2swf. 
44    
45 */
46
47 #define BANNER_TEXT     "swftools" 
48 #define ID_FONT         2000
49 #define ID_BANNER       2001
50
51 int main(int argc, char ** argv)
52 { SWF swf;
53   TAG * t;
54   SRECT r;
55   RGBA rgb;
56   U8 abits, gbits;
57   int definefont2 = 1;
58
59   int f;
60   int width = 170;
61   int height = 60;
62
63   int points = 50; // <- change global text size here
64   
65   int textsize = points*20;   // adjust height
66   int textscale = points*10;  // adjust spacing
67   
68   SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here
69
70   /* adding layout to a font has the side effect that the 
71      advance information is stored in the font itself, not
72      in accompanying textfields- this is needed e.g. for
73      edittext tags */
74   if(definefont2)
75       swf_FontAddLayout(font,0,0,0);
76
77   swf_FontInitUsage(font);
78   swf_FontUse(font,BANNER_TEXT);        // SWF reduces font information to the used glyphs
79   swf_FontReduce(font);  
80     
81   memset(&swf,0x00,sizeof(SWF));
82
83   swf.fileVersion    = 4;
84   swf.frameRate      = 0x4000;
85   swf.movieSize.xmax = 20*width;
86   swf.movieSize.ymax = 20*height;
87
88   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
89   t = swf.firstTag;
90
91         rgb.r = 0xff;
92         rgb.g = 0xff;
93         rgb.b = 0xff;
94         swf_SetRGB(t,&rgb);
95   
96   if(definefont2) {
97       t = swf_InsertTag(t,ST_DEFINEFONT2);
98             swf_FontSetDefine2(t, font);
99   } else {
100       t = swf_InsertTag(t,ST_DEFINEFONT);
101             swf_FontSetDefine(t, font);
102       t = swf_InsertTag(t,ST_DEFINEFONTINFO);
103             swf_FontSetInfo(t, font);
104   }
105
106   t = swf_InsertTag(t,ST_DEFINETEXT);
107
108         swf_SetU16(t,ID_BANNER);            // ID
109
110         r.xmin = 0;
111         r.ymin = 0;
112         r.xmax = swf_TextGetWidth(font,BANNER_TEXT,textscale);
113         r.ymax = textsize;
114         
115         swf_SetRect(t,&r);
116
117         swf_SetMatrix(t,NULL);
118
119         swf_TextCountBits(font,BANNER_TEXT,textscale,&gbits,&abits);
120         
121         swf_SetU8(t,gbits);
122         swf_SetU8(t,abits);
123
124         rgb.r = 0x00;
125         rgb.g = 0x00;
126         rgb.b = 0x00;
127
128         swf_TextSetInfoRecord(t,font,textsize,&rgb,0,textsize);
129         swf_TextSetCharRecord(t,font,BANNER_TEXT,textscale,gbits,abits);
130
131         swf_SetU8(t,0);
132         
133       
134     t = swf_InsertTag(t,ST_PLACEOBJECT2);
135
136         swf_ObjectPlace(t,ID_BANNER,1,NULL,NULL,NULL);
137  
138     t = swf_InsertTag(t,ST_SHOWFRAME);
139   
140     t = swf_InsertTag(t,ST_END);
141  
142 //  swf_WriteCGI(&swf);
143  
144
145   f = open("text.swf",O_RDWR|O_CREAT|O_TRUNC|O_BINARY,0644);
146   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
147   close(f);
148
149   swf_FreeTags(&swf);
150 //  swf_FontFree(font);
151
152 #ifdef __NT__
153   system("start ..\\text.swf");
154 #endif
155   
156   return 0;
157 }
158
159