0a5625e08c0c5baded937983e7aacbcfe97bb865
[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 file is distributed under the GPL, see file COPYING for details 
10
11 */
12
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <math.h>
16 #include "../rfxswf.h"
17
18 #include "demofont.c"  // five glyphs only:  f, l, o, s, w
19
20 /*
21     Due to copyright reasons we don't include full typesets into
22     the swftools package. But you can easily create fontdumps
23     of your desired typo:
24
25     * Create a swf file with all characters of your desired fonts
26       (with any tool that can output swf files)
27
28     * use the dumpfont example in this directory to dump font code
29
30     * include dump code and adjust Font_<Fontname>() calls.
31
32     Note: pdf2swf (Version 0.1.0) doesn't write ST_DEFINEFONTINFO tags,
33     so you can't extract fonts out of documents made with pdf2swf. 
34    
35 */
36
37 #define BANNER_TEXT     "swftools" 
38 #define ID_FONT         2000
39 #define ID_BANNER       2001
40
41 int main(int argc, char ** argv)
42 { SWF swf;
43   TAG * t;
44   SRECT r;
45   RGBA rgb;
46   U8 abits, gbits;
47   int definefont2 = 1;
48
49   int f;
50   int width = 170;
51   int height = 60;
52
53   int points = 50; // <- change global text size here
54   
55   int textsize = points*20;   // adjust height
56   int textscale = points*10;  // adjust spacing
57   
58   FONTUSAGE use;
59   SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here
60
61   /* adding layout to a font has the side effect that the 
62      advance information is stored in the font itself, not
63      in accompanying textfields- this is needed e.g. for
64      edittext tags */
65   if(definefont2)
66       swf_FontAddLayout(font,0,0,0);
67
68   swf_FontInitUsage(&use);
69   swf_FontUse(&use,BANNER_TEXT);        // SWF reduces font information to the used glyphs
70   swf_FontReduce(font,&use);  
71     
72   memset(&swf,0x00,sizeof(SWF));
73
74   swf.fileVersion    = 4;
75   swf.frameRate      = 0x4000;
76   swf.movieSize.xmax = 20*width;
77   swf.movieSize.ymax = 20*height;
78
79   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
80   t = swf.firstTag;
81
82         rgb.r = 0xff;
83         rgb.g = 0xff;
84         rgb.b = 0xff;
85         swf_SetRGB(t,&rgb);
86   
87   if(definefont2) {
88       t = swf_InsertTag(t,ST_DEFINEFONT2);
89             swf_FontSetDefine2(t, font);
90   } else {
91       t = swf_InsertTag(t,ST_DEFINEFONT);
92             swf_FontSetDefine(t, font);
93       t = swf_InsertTag(t,ST_DEFINEFONTINFO);
94             swf_FontSetInfo(t, font);
95   }
96
97   t = swf_InsertTag(t,ST_DEFINETEXT);
98
99         swf_SetU16(t,ID_BANNER);            // ID
100
101         r.xmin = 0;
102         r.ymin = 0;
103         r.xmax = swf_TextGetWidth(font,BANNER_TEXT,textscale);
104         r.ymax = textsize;
105         
106         swf_SetRect(t,&r);
107
108         swf_SetMatrix(t,NULL);
109
110         swf_TextCountBits(font,BANNER_TEXT,textscale,&gbits,&abits);
111         
112         swf_SetU8(t,gbits);
113         swf_SetU8(t,abits);
114
115         rgb.r = 0x00;
116         rgb.g = 0x00;
117         rgb.b = 0x00;
118
119         swf_TextSetInfoRecord(t,font,textsize,&rgb,0,textsize);
120         swf_TextSetCharRecord(t,font,BANNER_TEXT,textscale,gbits,abits);
121
122         swf_SetU8(t,0);
123         
124       
125     t = swf_InsertTag(t,ST_PLACEOBJECT2);
126
127         swf_ObjectPlace(t,ID_BANNER,1,NULL,NULL,NULL);
128  
129     t = swf_InsertTag(t,ST_SHOWFRAME);
130   
131     t = swf_InsertTag(t,ST_END);
132  
133 //  swf_WriteCGI(&swf);
134  
135
136   f = open("text.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
137   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
138   close(f);
139
140   swf_FreeTags(&swf);
141 //  swf_FontFree(font);
142
143 #ifdef __NT__
144   system("start ..\\text.swf");
145 #endif
146   
147   return 0;
148 }
149
150