summaryrefslogtreecommitdiff
path: root/Build/source/libs/freetype/freetype-1.5/lib/ttextend.c
blob: 6a2813a80d78209dccb1a0a726afe3f5c7bcd4b8 (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
/*******************************************************************
 *
 *  ttextend.c                                                   2.0
 *
 *    Extensions Interface
 *
 *  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 "ttextend.h"
#include "ttengine.h"
#include "ttmemory.h"

/* required by the tracing mode */
#undef  TT_COMPONENT
#define TT_COMPONENT      trace_extend


  struct TExtension_Registry_
  {
    Int               num_extensions;
    Long              cur_offset;
    TExtension_Class  classes[TT_MAX_EXTENSIONS];
  };

  typedef struct TExtension_Registry_  TExtension_Registry;
  typedef TExtension_Registry*         PExtension_Registry;



  /* Initialize the extension component */

  FT_INTERNAL_FUNC( TT_Error )
  TTExtend_Init( PEngine_Instance  engine )
  {
    TT_Error             error;
    PExtension_Registry  exts;


    if ( ALLOC( exts, sizeof ( TExtension_Registry ) ) )
      return error;

    exts->num_extensions        = 0;
    exts->cur_offset            = 0;
    engine->extension_component = (void*)exts;

    return TT_Err_Ok;
  }


  /* Finalize the extension component */

  FT_INTERNAL_FUNC( TT_Error )
  TTExtend_Done( PEngine_Instance  engine )
  {
    FREE( engine->extension_component );
    return TT_Err_Ok;
  }


  /* Register a new extension */

  FT_EXPORT_FUNC( TT_Error )
  TT_Register_Extension( PEngine_Instance  engine,
                         Long              id,
                         Long              size,
                         PExt_Constructor  create,
                         PExt_Destructor   destroy )
  {
    PExtension_Registry  exts;
    PExtension_Class     clazz;
    Int                  p;


    exts = (PExtension_Registry)engine->extension_component;
    if ( !exts )
      return TT_Err_Ok;

    p = exts->num_extensions;

    if ( p >= TT_MAX_EXTENSIONS )
      return TT_Err_Too_Many_Extensions;

    clazz          = exts->classes + p;
    clazz->id      = id;
    clazz->size    = size;
    clazz->build   = create;
    clazz->destroy = destroy;

    clazz->offset  = exts->cur_offset;

    exts->num_extensions++;
    exts->cur_offset += ( size + ALIGNMENT-1 ) & -ALIGNMENT;

    return TT_Err_Ok;
  }


  /* Query an extension block by extension_ID */

  FT_EXPORT_FUNC( TT_Error )
  TT_Extension_Get( PFace   face,
                    Long    extension_id,
                    void**  extension_block )
  {
    PExtension_Registry  registry;
    PExtension_Class     clazz;
    Int                  n;


    if ( !face->extension )
      return TT_Err_Extensions_Unsupported;

    registry = (PExtension_Registry)face->engine->extension_component;

    for ( n = 0; n < face->n_extensions; n++ )
    {
      clazz = registry->classes + n;
      if ( clazz->id == extension_id )
      {
        *extension_block = (PByte)face->extension + clazz->offset;
        return TT_Err_Ok;
      }
    }

    return TT_Err_Invalid_Extension_Id;
  }


  /* Destroy all extensions within a face object.  Called by the */
  /* face object destructor.                                     */

  FT_INTERNAL_FUNC( TT_Error )
  Extension_Destroy( PFace  face )
  {
    PEngine_Instance     engine = face->engine;
    PExtension_Registry  registry;
    PExtension_Class     clazz;
    Int                  n;
    PByte                ext;


    registry = (PExtension_Registry)engine->extension_component;

    for ( n = 0; n < face->n_extensions; n++ )
    {
      clazz = registry->classes + n;
      ext   = (PByte)face->extension + clazz->offset;

      /* the destructor is optional */
      if ( clazz->destroy )
        clazz->destroy( (void*)ext, face );
    }

    /* destroy the face's extension block too */
    FREE( face->extension );
    face->n_extensions = 0;

    return TT_Err_Ok;
  }


  /* Create an extension within a face object.  Called by the */
  /* face object constructor.                                 */

  FT_INTERNAL_FUNC( TT_Error )
  Extension_Create( PFace  face )
  {
    PEngine_Instance     engine = face->engine;
    PExtension_Registry  registry;
    PExtension_Class     clazz;
    TT_Error             error;
    Int                  n;
    PByte                ext;


    registry = (PExtension_Registry)engine->extension_component;

    face->n_extensions = registry->num_extensions;
    if ( ALLOC( face->extension, registry->cur_offset ) )
      return error;

    for ( n = 0; n < face->n_extensions; n++ )
    {
      clazz = registry->classes + n;
      ext   = (PByte)face->extension + clazz->offset;
      error = clazz->build( (void*)ext, face );
      if ( error )
        goto Fail;
    }
    return TT_Err_Ok;

  Fail:
    Extension_Destroy( face );
    return error;
  }


/* END */