new examples: dumpfont and text
[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
48   int f;
49   int width = 170;
50   int height = 60;
51
52   int points = 50; // <- change global text size here
53   
54   int textsize = points*20;   // adjust height
55   int textscale = points*10;  // adjust spacing
56   
57   FONTUSAGE use;
58   SWFFONT * font = Font_Demo_Font(ID_FONT); // change font name here
59
60   swf_FontInitUsage(&use);
61   swf_FontUse(&use,BANNER_TEXT);        // SWF reduces font information to the used glyphs
62   swf_FontReduce(font,&use);  
63     
64   memset(&swf,0x00,sizeof(SWF));
65
66   swf.fileVersion    = 4;
67   swf.frameRate      = 0x4000;
68   swf.movieSize.xmax = 20*width;
69   swf.movieSize.ymax = 20*height;
70
71   swf.firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
72   t = swf.firstTag;
73
74         rgb.r = 0xff;
75         rgb.g = 0xff;
76         rgb.b = 0xff;
77         swf_SetRGB(t,&rgb);
78        
79   t = swf_InsertTag(t,ST_DEFINEFONT);
80
81         swf_FontSetDefine(t,font);
82
83   t = swf_InsertTag(t,ST_DEFINEFONTINFO);
84
85         swf_FontSetInfo(t,font);
86
87   t = swf_InsertTag(t,ST_DEFINETEXT);
88
89         swf_SetU16(t,ID_BANNER);            // ID
90
91         r.xmin = 0;
92         r.ymin = 0;
93         r.xmax = swf_TextGetWidth(font,BANNER_TEXT,textscale);
94         r.ymax = textsize;
95         
96         swf_SetRect(t,&r);
97
98         swf_SetMatrix(t,NULL);
99
100         swf_TextCountBits(font,BANNER_TEXT,textscale,&gbits,&abits);
101         
102         swf_SetU8(t,gbits);
103         swf_SetU8(t,abits);
104
105         rgb.r = 0x00;
106         rgb.g = 0x00;
107         rgb.b = 0x00;
108
109         swf_TextSetInfoRecord(t,font,textsize,&rgb,0,textsize);
110         swf_TextSetCharRecord(t,font,BANNER_TEXT,textscale,gbits,abits);
111
112         swf_SetU8(t,0);
113         
114       
115     t = swf_InsertTag(t,ST_PLACEOBJECT2);
116
117         swf_ObjectPlace(t,ID_BANNER,1,NULL,NULL,NULL);
118  
119     t = swf_InsertTag(t,ST_SHOWFRAME);
120   
121     t = swf_InsertTag(t,ST_END);
122  
123 //  swf_WriteCGI(&swf);
124  
125
126   f = open("text.swf",O_RDWR|O_CREAT|O_TRUNC,0644);
127   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
128   close(f);
129
130   swf_FreeTags(&swf);
131
132 #ifdef __NT__
133   system("start ..\\text.swf");
134 #endif
135   
136   return 0;
137 }
138
139