Sync with trunk r47367
[reactos.git] / lib / 3rdparty / freetype / src / base / ftinit.c
1 /***************************************************************************/
2 /* */
3 /* ftinit.c */
4 /* */
5 /* FreeType initialization layer (body). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2005, 2007, 2009 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
17
18 /*************************************************************************/
19 /* */
20 /* The purpose of this file is to implement the following two */
21 /* functions: */
22 /* */
23 /* FT_Add_Default_Modules(): */
24 /* This function is used to add the set of default modules to a */
25 /* fresh new library object. The set is taken from the header file */
26 /* `freetype/config/ftmodule.h'. See the document `FreeType 2.0 */
27 /* Build System' for more information. */
28 /* */
29 /* FT_Init_FreeType(): */
30 /* This function creates a system object for the current platform, */
31 /* builds a library out of it, then calls FT_Default_Drivers(). */
32 /* */
33 /* Note that even if FT_Init_FreeType() uses the implementation of the */
34 /* system object defined at build time, client applications are still */
35 /* able to provide their own `ftsystem.c'. */
36 /* */
37 /*************************************************************************/
38
39
40 #include <ft2build.h>
41 #include FT_CONFIG_CONFIG_H
42 #include FT_INTERNAL_OBJECTS_H
43 #include FT_INTERNAL_DEBUG_H
44 #include FT_MODULE_H
45 #include "basepic.h"
46
47
48 /*************************************************************************/
49 /* */
50 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
51 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
52 /* messages during execution. */
53 /* */
54 #undef FT_COMPONENT
55 #define FT_COMPONENT trace_init
56
57 #ifndef FT_CONFIG_OPTION_PIC
58
59 #undef FT_USE_MODULE
60 #ifdef __cplusplus
61 #define FT_USE_MODULE( type, x ) extern "C" const type x;
62 #else
63 #define FT_USE_MODULE( type, x ) extern const type x;
64 #endif
65
66
67 #include FT_CONFIG_MODULES_H
68
69
70 #undef FT_USE_MODULE
71 #define FT_USE_MODULE( type, x ) (const FT_Module_Class*)&(x),
72
73 static
74 const FT_Module_Class* const ft_default_modules[] =
75 {
76 #include FT_CONFIG_MODULES_H
77 0
78 };
79
80 #else /* FT_CONFIG_OPTION_PIC */
81
82 #ifdef __cplusplus
83 #define FT_EXTERNC extern "C"
84 #else
85 #define FT_EXTERNC extern
86 #endif
87
88 /* declare the module's class creation/destruction functions */
89 #undef FT_USE_MODULE
90 #define FT_USE_MODULE( type, x ) \
91 FT_EXTERNC FT_Error FT_Create_Class_##x( FT_Library library, FT_Module_Class** output_class ); \
92 FT_EXTERNC void FT_Destroy_Class_##x( FT_Library library, FT_Module_Class* clazz );
93
94 #include FT_CONFIG_MODULES_H
95
96
97 /* count all module classes */
98 #undef FT_USE_MODULE
99 #define FT_USE_MODULE( type, x ) MODULE_CLASS_##x,
100
101 enum {
102 #include FT_CONFIG_MODULES_H
103 FT_NUM_MODULE_CLASSES
104 };
105
106 /* destroy all module classes */
107 #undef FT_USE_MODULE
108 #define FT_USE_MODULE( type, x ) \
109 if ( classes[i] ) { FT_Destroy_Class_##x(library, classes[i]); } \
110 i++; \
111
112 FT_BASE_DEF( void )
113 ft_destroy_default_module_classes( FT_Library library )
114 {
115 FT_Module_Class** classes;
116 FT_Memory memory;
117 FT_UInt i;
118 BasePIC* pic_container = (BasePIC*)library->pic_container.base;
119
120 if ( !pic_container->default_module_classes )
121 return;
122
123 memory = library->memory;
124 classes = pic_container->default_module_classes;
125 i = 0;
126
127 #include FT_CONFIG_MODULES_H
128
129 FT_FREE( classes );
130 pic_container->default_module_classes = 0;
131 }
132
133 /* initialize all module classes and the pointer table */
134 #undef FT_USE_MODULE
135 #define FT_USE_MODULE( type, x ) \
136 error = FT_Create_Class_##x(library, &clazz); \
137 if (error) goto Exit; \
138 classes[i++] = clazz;
139
140 FT_BASE_DEF( FT_Error )
141 ft_create_default_module_classes( FT_Library library )
142 {
143 FT_Error error;
144 FT_Memory memory;
145 FT_Module_Class** classes;
146 FT_Module_Class* clazz;
147 FT_UInt i;
148 BasePIC* pic_container = (BasePIC*)library->pic_container.base;
149
150 memory = library->memory;
151 pic_container->default_module_classes = 0;
152
153 if ( FT_ALLOC(classes, sizeof(FT_Module_Class*) * (FT_NUM_MODULE_CLASSES + 1) ) )
154 return error;
155 /* initialize all pointers to 0, especially the last one */
156 for (i = 0; i < FT_NUM_MODULE_CLASSES; i++)
157 classes[i] = 0;
158 classes[FT_NUM_MODULE_CLASSES] = 0;
159
160 i = 0;
161
162 #include FT_CONFIG_MODULES_H
163
164 Exit:
165 if (error) ft_destroy_default_module_classes( library );
166 else pic_container->default_module_classes = classes;
167
168 return error;
169 }
170
171
172 #endif /* FT_CONFIG_OPTION_PIC */
173
174 /* documentation is in ftmodapi.h */
175
176 FT_EXPORT_DEF( void )
177 FT_Add_Default_Modules( FT_Library library )
178 {
179 FT_Error error;
180 const FT_Module_Class* const* cur;
181
182
183 /* test for valid `library' delayed to FT_Add_Module() */
184
185 cur = FT_DEFAULT_MODULES_GET;
186 while ( *cur )
187 {
188 error = FT_Add_Module( library, *cur );
189 /* notify errors, but don't stop */
190 if ( error )
191 FT_TRACE0(( "FT_Add_Default_Module:"
192 " Cannot install `%s', error = 0x%x\n",
193 (*cur)->module_name, error ));
194 cur++;
195 }
196 }
197
198
199 /* documentation is in freetype.h */
200
201 FT_EXPORT_DEF( FT_Error )
202 FT_Init_FreeType( FT_Library *alibrary )
203 {
204 FT_Error error;
205 FT_Memory memory;
206
207
208 /* First of all, allocate a new system object -- this function is part */
209 /* of the system-specific component, i.e. `ftsystem.c'. */
210
211 memory = FT_New_Memory();
212 if ( !memory )
213 {
214 FT_ERROR(( "FT_Init_FreeType: cannot find memory manager\n" ));
215 return FT_Err_Unimplemented_Feature;
216 }
217
218 /* build a library out of it, then fill it with the set of */
219 /* default drivers. */
220
221 error = FT_New_Library( memory, alibrary );
222 if ( error )
223 FT_Done_Memory( memory );
224 else
225 FT_Add_Default_Modules( *alibrary );
226
227 return error;
228 }
229
230
231 /* documentation is in freetype.h */
232
233 FT_EXPORT_DEF( FT_Error )
234 FT_Done_FreeType( FT_Library library )
235 {
236 if ( library )
237 {
238 FT_Memory memory = library->memory;
239
240
241 /* Discard the library object */
242 FT_Done_Library( library );
243
244 /* discard memory manager */
245 FT_Done_Memory( memory );
246 }
247
248 return FT_Err_Ok;
249 }
250
251
252 /* END */