summaryrefslogtreecommitdiff
path: root/src/base/ftextend.c
blob: f7ca3987b1d32916406a94d69a70eb7550a18b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/***************************************************************************/
/*                                                                         */
/*  ftextend.h                                                             */
/*                                                                         */
/*  FreeType extensions implementation (body).                             */
/*                                                                         */
/*  Copyright 1996-2000 by                                                 */
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
/*                                                                         */
/*  This file is part of the FreeType project, and may only be used,       */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/

  /*************************************************************************/
  /*                                                                       */
  /*  This is an updated version of the extension component, now located   */
  /*  in the main library's source directory.  It allows the dynamic       */
  /*  registration/use of various face object extensions through a simple  */
  /*  API.                                                                 */
  /*                                                                       */
  /*************************************************************************/


#include <freetype/internal/ftextend.h>
#include <freetype/internal/ftdebug.h>


  /*************************************************************************/
  /*                                                                       */
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  /* messages during execution.                                            */
  /*                                                                       */
#undef  FT_COMPONENT
#define FT_COMPONENT  trace_extend


  typedef struct  FT_Extension_Registry_
  {
    FT_Int              num_extensions;
    FT_Long             cur_offset;
    FT_Extension_Class  classes[FT_MAX_EXTENSIONS];

  } FT_Extension_Registry;


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    FT_Init_Extensions                                                 */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Initializes the extension component.                               */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    driver :: A handle to the driver object.                           */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  LOCAL_FUNC
  FT_Error  FT_Init_Extensions( FT_Driver  driver )
  {
    FT_Error                error;
    FT_Memory               memory;
    FT_Extension_Registry*  registry;


    memory = driver->library->memory;
    if ( ALLOC( registry, sizeof ( *registry ) ) )
      return error;

    registry->num_extensions = 0;
    registry->cur_offset     = 0;
    driver->extensions       = registry;

    FT_TRACE2(( "FT_Init_Extensions: success\n" ));

    return FT_Err_Ok;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    FT_Done_Extensions                                                 */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Finalizes the extension component.                                 */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    driver :: A handle to the driver object.                           */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  LOCAL_FUNC
  FT_Error  FT_Done_Extensions( FT_Driver  driver )
  {
    FT_Memory  memory = driver->memory;


    FREE( driver->extensions );
    return FT_Err_Ok;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    FT_Register_Extension                                              */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Registers a new extension.                                         */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    driver :: A handle to the driver object.                           */
  /*    class  :: A pointer to a class describing the extension.           */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  FT_EXPORT_FUNC( FT_Error )  FT_Register_Extension(
                                FT_Driver            driver,
                                FT_Extension_Class*  class )
  {
    FT_Extension_Registry*  registry;


    if ( !driver )
      return FT_Err_Invalid_Driver_Handle;

    if ( !class )
      return FT_Err_Invalid_Argument;

    registry = (FT_Extension_Registry*)driver->extensions;
    if ( registry )
    {
      FT_Int               n   = registry->num_extensions;
      FT_Extension_Class*  cur = registry->classes + n;


      if ( n >= FT_MAX_EXTENSIONS )
        return FT_Err_Too_Many_Extensions;

      *cur = *class;

      cur->offset  = registry->cur_offset;

      registry->num_extensions++;
      registry->cur_offset += ( cur->size + FT_ALIGNMENT-1 ) & -FT_ALIGNMENT;

      FT_TRACE1(( "FT_Register_Extension: `%s' successfully registered\n",
                  cur->id ));
    }

    return FT_Err_Ok;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    FT_Get_Extension                                                   */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Queries an extension block by an extension ID string.              */
  /*                                                                       */
  /* <Input>                                                               */
  /*    face                :: A handle to the face object.                */
  /*    extension_id        :: An ID string identifying the extension.     */
  /*                                                                       */
  /* <Output>                                                              */
  /*    extension_interface :: A generic pointer, usually pointing to a    */
  /*                           table of functions implementing the         */
  /*                           extension interface.                        */
  /*                                                                       */
  /* <Return>                                                              */
  /*    A pointer to the extension block.                                  */
  /*                                                                       */
  FT_EXPORT_FUNC( void* )  FT_Get_Extension(
                             FT_Face      face,
                             const char*  extension_id,
                             void*       *extension_interface )
  {
    FT_Extension_Registry*  registry;


    if ( !face || !extension_id || !extension_interface )
      return 0;

    registry = (FT_Extension_Registry*)face->driver->extensions;
    if ( registry && face->extensions )
    {
      FT_Extension_Class*  cur   = registry->classes;
      FT_Extension_Class*  limit = cur + registry->num_extensions;


      for ( ; cur < limit; cur++ )
        if ( strcmp( cur->id, extension_id ) == 0 )
        {
          *extension_interface = cur->interface;

          FT_TRACE1(( "FT_Get_Extension: got `%s'\n", extension_id ));

          return (void*)((char*)face->extensions + cur->offset);
        }
    }

    /* could not find the extension id */

    FT_ERROR(( "FT_Get_Extension: couldn't find `%s'\n", extension_id ));

    *extension_interface = 0;

    return 0;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    FT_Destroy_Extensions                                              */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Destroys all extensions within a face object.                      */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    face :: A handle to the face object.                               */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  /* <Note>                                                                */
  /*    Called by the face object destructor.                              */
  /*                                                                       */
  LOCAL_FUNC
  FT_Error  FT_Destroy_Extensions( FT_Face  face )
  {
    FT_Extension_Registry*  registry;
    FT_Memory               memory;


    registry = (FT_Extension_Registry*)face->driver->extensions;
    if ( registry && face->extensions )
    {
      FT_Extension_Class*  cur   = registry->classes;
      FT_Extension_Class*  limit = cur + registry->num_extensions;


      for ( ; cur < limit; cur++ )
      {
        char*  ext = (char*)face->extensions + cur->offset;

        if ( cur->finalize )
          cur->finalize( ext, face );
      }

      memory = face->driver->memory;
      FREE( face->extensions );
    }

    return FT_Err_Ok;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    FT_Create_Extensions                                               */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Creates an extension object within a face object for all           */
  /*    registered extensions.                                             */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    face :: A handle to the face object.                               */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  /* <Note>                                                                */
  /*    Called by the face object constructor.                             */
  /*                                                                       */
  LOCAL_FUNC
  FT_Error  FT_Create_Extensions( FT_Face  face )
  {
    FT_Extension_Registry*  registry;
    FT_Memory               memory;
    FT_Error                error;


    face->extensions = 0;

    /* load extensions registry, exit successfully if none is there */

    registry = (FT_Extension_Registry*)face->driver->extensions;
    if ( !registry )
      return FT_Err_Ok;

    memory = face->driver->memory;
    if ( ALLOC( face->extensions, registry->cur_offset ) )
      return error;

    {
      FT_Extension_Class*  cur   = registry->classes;
      FT_Extension_Class*  limit = cur + registry->num_extensions;


      for ( ; cur < limit; cur++ )
      {
        char*  ext = (char*)face->extensions + cur->offset;

        if ( cur->init )
        {
          error = cur->init( ext, face );
          if ( error )
            break;
        }
      }
    }

    return error;
  }


/* END */