1 /*
2 Ming, an SWF output library
3 Copyright (C) 2002 Opaque Industries - http://www.opaque.net/
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /* $Id$ */
21
22 #ifndef __C2MAN__
23 #include <stdlib.h>
24 #include <string.h>
25 #endif
26
27 #include "browserfont.h"
28 #include "font.h"
29 #include "character.h"
30 #include "libming.h"
31
32
33 struct SWFBrowserFont_s
34 {
35 struct SWFCharacter_s character;
36 SWFOutput out;
37 char *name;
38 };
39
40
41 static
writeSWFBrowserFontToMethod(SWFBlock block,SWFByteOutputMethod method,void * data)42 void writeSWFBrowserFontToMethod(SWFBlock block,
43 SWFByteOutputMethod method, void *data)
44 {
45 SWFOutput out = ((SWFBrowserFont)block)->out;
46 SWFOutput_writeToMethod(out, method, data);
47 }
48
49
50 static void
finishBrowserFont(SWFBrowserFont font)51 finishBrowserFont(SWFBrowserFont font)
52 {
53 unsigned int i;
54 SWFOutput out;
55 SWF_assert(BLOCK(font)->swfVersion);
56
57 out = newSWFOutput();
58 font->out = out;
59
60 SWFOutput_writeUInt16(out, CHARACTERID(font));
61 if(BLOCK(font)->swfVersion > 5) /* maybe italic or bold flag? */
62 SWFOutput_writeUInt8(out, SWF_FONT_WIDECODES);
63 else
64 SWFOutput_writeUInt8(out, 0);
65
66 SWFOutput_writeUInt8(out, 0); /* reserved flags */
67 SWFOutput_writeUInt8(out, strlen(font->name));
68
69 for ( i=0; i<strlen(font->name); ++i )
70 SWFOutput_writeUInt8(out, font->name[i]);
71
72 SWFOutput_writeUInt16(out, 0); /* number of glyphs */
73 SWFOutput_writeSInt16(out, 2); /* offset */
74
75 SWFOutput_byteAlign(out);
76 }
77
78 static int
completeSWFBrowserFont(SWFBlock block)79 completeSWFBrowserFont(SWFBlock block)
80 {
81 SWFBrowserFont font = (SWFBrowserFont)block;
82 if(font->out == NULL)
83 finishBrowserFont(font);
84
85 BLOCK(font)->type = SWF_DEFINEFONT2; /* see below */
86 return SWFOutput_getLength(font->out);
87 }
88
89 /**
90 * destroys a SWFBrowserFont instance.
91 */
92 void
destroySWFBrowserFont(SWFBrowserFont font)93 destroySWFBrowserFont(SWFBrowserFont font)
94 {
95 destroySWFOutput(font->out);
96 free(font->name);
97 free(font);
98 }
99
100 /**
101 * creates a browser font instance
102 * This Function allows the usage of built in fonts like "_sans".
103 * Takes the name of the font as an argument.
104 */
105 SWFBrowserFont
newSWFBrowserFont(const char * name)106 newSWFBrowserFont(const char *name)
107 {
108 SWFBrowserFont font = (SWFBrowserFont) malloc(sizeof(struct SWFBrowserFont_s));
109
110 SWFCharacterInit((SWFCharacter)font);
111
112 BLOCK(font)->writeBlock = writeSWFBrowserFontToMethod;
113 BLOCK(font)->complete = completeSWFBrowserFont;
114 BLOCK(font)->dtor = (destroySWFBlockMethod) destroySWFBrowserFont;
115
116 BLOCK(font)->type = SWF_BROWSERFONT;
117 CHARACTERID(font) = ++SWF_gNumCharacters;
118 font->out = NULL;
119 font->name = strdup(name);
120
121 return font;
122 }
123
124 SWFOutput
SWFBrowserFont_getOutput(SWFBrowserFont font)125 SWFBrowserFont_getOutput(SWFBrowserFont font)
126 {
127 if(font->out == NULL) // in case complete is called twice
128 finishBrowserFont(font);
129
130 return font->out;
131 }
132
133
134 /*
135 * Local variables:
136 * tab-width: 2
137 * c-basic-offset: 2
138 * End:
139 */
140