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   {
103 #include FT_CONFIG_MODULES_H
104     FT_NUM_MODULE_CLASSES
105   };
106 
107   /* destroy all module classes */
108 #undef  FT_USE_MODULE
109 #define FT_USE_MODULE( type, x )  \
110   if ( classes[i] ) { FT_Destroy_Class_##x(library, classes[i]); } \
111   i++;                                                             \
112 
113   FT_BASE_DEF( void )
ft_destroy_default_module_classes(FT_Library library)114   ft_destroy_default_module_classes( FT_Library  library )
115   {
116     FT_Module_Class** classes;
117     FT_Memory         memory;
118     FT_UInt           i;
119     BasePIC*          pic_container = (BasePIC*)library->pic_container.base;
120 
121     if ( !pic_container->default_module_classes )
122       return;
123 
124     memory = library->memory;
125     classes = pic_container->default_module_classes;
126     i = 0;
127 
128 #include FT_CONFIG_MODULES_H
129 
130     FT_FREE( classes );
131     pic_container->default_module_classes = 0;
132   }
133 
134   /* initialize all module classes and the pointer table */
135 #undef  FT_USE_MODULE
136 #define FT_USE_MODULE( type, x )                \
137   error = FT_Create_Class_##x(library, &clazz); \
138   if (error) goto Exit;                         \
139   classes[i++] = clazz;
140 
141   FT_BASE_DEF( FT_Error )
ft_create_default_module_classes(FT_Library library)142   ft_create_default_module_classes( FT_Library  library )
143   {
144     FT_Error          error;
145     FT_Memory         memory;
146     FT_Module_Class** classes;
147     FT_Module_Class*  clazz;
148     FT_UInt           i;
149     BasePIC*          pic_container = (BasePIC*)library->pic_container.base;
150 
151     memory = library->memory;
152     pic_container->default_module_classes = 0;
153 
154     if ( FT_ALLOC(classes, sizeof(FT_Module_Class*) * (FT_NUM_MODULE_CLASSES + 1) ) )
155       return error;
156     /* initialize all pointers to 0, especially the last one */
157     for (i = 0; i < FT_NUM_MODULE_CLASSES; i++)
158       classes[i] = 0;
159     classes[FT_NUM_MODULE_CLASSES] = 0;
160 
161     i = 0;
162 
163 #include FT_CONFIG_MODULES_H
164 
165 Exit:
166     if (error) ft_destroy_default_module_classes( library );
167     else pic_container->default_module_classes = classes;
168 
169     return error;
170   }
171 
172 
173 #endif /* FT_CONFIG_OPTION_PIC */
174 
175   /* documentation is in ftmodapi.h */
176 
177   FT_EXPORT_DEF( void )
FT_Add_Default_Modules(FT_Library library)178   FT_Add_Default_Modules( FT_Library  library )
179   {
180     FT_Error                       error;
181     const FT_Module_Class* const*  cur;
182 
183 
184     /* test for valid `library' delayed to FT_Add_Module() */
185 
186     cur = FT_DEFAULT_MODULES_GET;
187     while ( *cur )
188     {
189       error = FT_Add_Module( library, *cur );
190       /* notify errors, but don't stop */
191       if ( error )
192         FT_TRACE0(( "FT_Add_Default_Module:"
193                     " Cannot install `%s', error = 0x%x\n",
194                     (*cur)->module_name, error ));
195       cur++;
196     }
197   }
198 
199 
200   /* documentation is in freetype.h */
201 
202   FT_EXPORT_DEF( FT_Error )
FT_Init_FreeType(FT_Library * alibrary)203   FT_Init_FreeType( FT_Library  *alibrary )
204   {
205     FT_Error   error;
206     FT_Memory  memory;
207 
208 
209     /* First of all, allocate a new system object -- this function is part */
210     /* of the system-specific component, i.e. `ftsystem.c'.                */
211 
212     memory = FT_New_Memory();
213     if ( !memory )
214     {
215       FT_ERROR(( "FT_Init_FreeType: cannot find memory manager\n" ));
216       return FT_Err_Unimplemented_Feature;
217     }
218 
219     /* build a library out of it, then fill it with the set of */
220     /* default drivers.                                        */
221 
222     error = FT_New_Library( memory, alibrary );
223     if ( error )
224       FT_Done_Memory( memory );
225     else
226       FT_Add_Default_Modules( *alibrary );
227 
228     return error;
229   }
230 
231 
232   /* documentation is in freetype.h */
233 
234   FT_EXPORT_DEF( FT_Error )
FT_Done_FreeType(FT_Library library)235   FT_Done_FreeType( FT_Library  library )
236   {
237     if ( library )
238     {
239       FT_Memory  memory = library->memory;
240 
241 
242       /* Discard the library object */
243       FT_Done_Library( library );
244 
245       /* discard memory manager */
246       FT_Done_Memory( memory );
247     }
248 
249     return FT_Err_Ok;
250   }
251 
252 
253 /* END */
254