summaryrefslogtreecommitdiff
path: root/include/freetype/internal/ftobject.h
blob: f285d9e207bd76048e66831a263d98be0529e37a (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#ifndef __FT_OBJECT_H__
#define __FT_OBJECT_H__

#include <ft2build.h>
#include FT_FREETYPE_H

FT_BEGIN_HEADER

 /**************************************************************
  *
  * @type: FT_Object
  *
  * @description:
  *   handle to a FreeType Object. See @FT_ObjectRec
  */
  typedef struct FT_ObjectRec_*        FT_Object;


 /**************************************************************
  *
  * @type: FT_Class
  *
  * @description:
  *   handle to a constant class handle to a FreeType Object.
  *
  *   Note that a class is itself a @FT_Object and are dynamically
  *   allocated on the heap.
  *
  * @also:
  *  @FT_ClassRec, @FT_Object, @FT_ObjectRec, @FT_Type, @FT_TypeRec
  */
  typedef const struct FT_ClassRec_*   FT_Class;


 /**************************************************************
  *
  * @type: FT_Type
  *
  * @description:
  *   handle to a constant structure (of type @FT_TypeRec) used
  *   to describe a given @FT_Class type to the FreeType object
  *   sub-system.
  */
  typedef const struct FT_TypeRec_*    FT_Type;



 /**************************************************************
  *
  * @struct: FT_ObjectRec
  *
  * @description:
  *   a structure describing the root fields of all @FT_Object
  *   class instances in FreeType
  *
  * @fields:
  *   clazz     :: handle to the object's class
  *   ref_count :: object's reference count. Starts at 1
  */
  typedef struct FT_ObjectRec_
  {
    FT_Class  clazz;
    FT_Int    ref_count;

  } FT_ObjectRec;


 /**************************************************************
  *
  * @macro: FT_OBJECT (x)
  *
  * @description:
  *   a useful macro to type-cast anything to a @FT_Object
  *   handle. No check performed..
  */
#define  FT_OBJECT(x)    ((FT_Object)(x))


 /**************************************************************
  *
  * @macro: FT_OBJECT_P (x)
  *
  * @description:
  *   a useful macro to type-cast anything to a pointer to
  *   @FT_Object handle.
  */
#define  FT_OBJECT_P(x)  ((FT_Object*)(x))


 /**************************************************************
  *
  * @macro: FT_OBJECT__CLASS (obj)
  *
  * @description:
  *   a useful macro to return the class of any object
  */
#define  FT_OBJECT__CLASS(x)      FT_OBJECT(x)->clazz


 /**************************************************************
  *
  * @macro: FT_OBJECT__REF_COUNT (obj)
  *
  * @description:
  *   a useful macro to return the reference count of any object
  */
#define  FT_OBJECT__REF_COUNT(x)  FT_OBJECT(x)->ref_count


 /**************************************************************
  *
  * @macro: FT_OBJECT__MEMORY (obj)
  *
  * @description:
  *   a useful macro to return a handle to the memory manager
  *   used to allocate a given object
  */
#define  FT_OBJECT__MEMORY(x)     FT_CLASS__MEMORY(FT_OBJECT(x)->clazz)


 /**************************************************************
  *
  * @macro: FT_OBJECT__LIBRARY (obj)
  *
  * @description:
  *   a useful macro to return a handle to the library handle
  *   that owns the object
  */
#define  FT_OBJECT__LIBRARY(x)    FT_CLASS__LIBRARY(FT_OBJECT(x)->clazz)


 /**************************************************************
  *
  * @functype: FT_Object_InitFunc
  *
  * @description:
  *   a function used to initialize a new object
  *
  * @input:
  *   object    :: target object handle
  *   init_data :: optional pointer to initialization data
  *
  * @return:
  *   error code. 0 means success
  */
  typedef FT_Error  (*FT_Object_InitFunc)( FT_Object   object,
                                           FT_Pointer  init_data );

 /**************************************************************
  *
  * @functype: FT_Object_DoneFunc
  *
  * @description:
  *   a function used to finalize a given object
  *
  * @input:
  *   object    :: handle to target object
  */
  typedef void  (*FT_Object_DoneFunc)( FT_Object   object );


 /**************************************************************
  *
  * @struct: FT_ClassRec
  *
  * @description:
  *   a structure used to describe a given object class within
  *   FreeType
  *
  * @fields:
  *   object   :: root @FT_ObjectRec fields, since each class is
  *               itself an object. (it's an instance of the
  *               "metaclass", a special object of the FreeType
  *               object sub-system.)
  *
  *   magic    :: a 32-bit magic number used for decoding
  *   super    :: pointer to super class
  *   type     :: the @FT_Type descriptor of this class
  *   memory   :: the current memory manager handle
  *   library  :: the current library handle
  *   info     :: an opaque pointer to class-specific information
  *               managed by the FreeType object sub-system
  *
  *   class_done :: the class destructor function
  *
  *   obj_size :: size of class instances in bytes
  *   obj_init :: class instance constructor
  *   obj_done :: class instance destructor
  */
  typedef struct FT_ClassRec_
  {
    FT_ObjectRec        object;
    FT_UInt32           magic;
    FT_Class            super;
    FT_Type             type;
    FT_Memory           memory;
    FT_Library          library;
    FT_Pointer          info;

    FT_Object_DoneFunc  class_done;

    FT_UInt             obj_size;
    FT_Object_InitFunc  obj_init;
    FT_Object_DoneFunc  obj_done;

  } FT_ClassRec;


 /**************************************************************
  *
  * @macro: FT_CLASS (x)
  *
  * @description:
  *   a useful macro to convert anything to a class handle
  *   without checks
  */
#define  FT_CLASS(x)    ((FT_Class)(x))


 /**************************************************************
  *
  * @macro: FT_CLASS_P (x)
  *
  * @description:
  *   a useful macro to convert anything to a pointer to a class
  *   handle without checks
  */
#define  FT_CLASS_P(x)  ((FT_Class*)(x))


 /**************************************************************
  *
  * @macro: FT_CLASS__MEMORY (clazz)
  *
  * @description:
  *   a useful macro to return the memory manager handle of a
  *   given class
  */
#define  FT_CLASS__MEMORY(x)   FT_CLASS(x)->memory


 /**************************************************************
  *
  * @macro: FT_CLASS__LIBRARY (clazz)
  *
  * @description:
  *   a useful macro to return the library handle of a
  *   given class
  */
#define  FT_CLASS__LIBRARY(x)  FT_CLASS(x)->library



 /**************************************************************
  *
  * @macro: FT_CLASS__TYPE (clazz)
  *
  * @description:
  *   a useful macro to return the type of a given class
  *   given class
  */
#define  FT_CLASS__TYPE(x)     FT_CLASS(x)->type

 /* */
#define  FT_CLASS__INFO(x)     FT_CLASS(x)->info
#define  FT_CLASS__MAGIC(x)    FT_CLASS(x)->magic


 /**************************************************************
  *
  * @struct: FT_TypeRec
  *
  * @description:
  *   a structure used to describe a given class to the FreeType
  *   object sub-system.
  *
  * @fields:
  *   name       :: class name. only used for debugging
  *   super      :: type of super-class. NULL if none
  *
  *   class_size :: size of class structure in bytes
  *   class_init :: class constructor
  *   class_done :: class finalizer
  *
  *   obj_size   :: instance size in bytes
  *   obj_init   :: instance constructor. can be NULL
  *   obj_done   :: instance destructor. can be NULL
  *
  * @note:
  *   if 'obj_init' is NULL, the class will use it's parent
  *   constructor, if any
  *
  *   if 'obj_done' is NULL, the class will use it's parent
  *   finalizer, if any
  *
  *   the object sub-system allocates a new class, copies
  *   the content of its super-class into the new structure,
  *   _then_ calls 'clazz_init'.
  *
  *   'class_init' and 'class_done' can be NULL, in which case
  *   the parent's class constructor and destructor wil be used
  */
  typedef struct FT_TypeRec_
  {
    const char*         name;
    FT_Type             super;

    FT_UInt             class_size;
    FT_Object_InitFunc  class_init;
    FT_Object_DoneFunc  class_done;

    FT_UInt             obj_size;
    FT_Object_InitFunc  obj_init;
    FT_Object_DoneFunc  obj_done;

  } FT_TypeRec;


 /**************************************************************
  *
  * @macro: FT_TYPE (x)
  *
  * @description:
  *   a useful macro to convert anything to a class type handle
  *   without checks
  */
#define  FT_TYPE(x)  ((FT_Type)(x))


 /**************************************************************
  *
  * @function: ft_object_check
  *
  * @description:
  *   checks that a handle points to a valid @FT_Object
  *
  * @input:
  *   obj :: handle/pointer
  *
  * @return:
  *   1 iff the handle points to a valid object. 0 otherwise
  */
  FT_BASE( FT_Int )
  ft_object_check( FT_Pointer  obj );


 /**************************************************************
  *
  * @function: ft_object_is_a
  *
  * @description:
  *   checks that a handle points to a valid @FT_Object that
  *   is an instance of a given class (or of any of its sub-classes)
  *
  * @input:
  *   obj   :: handle/pointer
  *   clazz :: class handle to check
  *
  * @return:
  *   1 iff the handle points to a valid 'clazz' instance. 0
  *   otherwise.
  */
  FT_BASE( FT_Int )
  ft_object_is_a( FT_Pointer  obj,
                  FT_Class    clazz );


 /**************************************************************
  *
  * @function: ft_object_create
  *
  * @description:
  *   create a new object (class instance)
  *
  * @output:
  *   aobject   :: new object handle. NULL in case of error
  *
  * @input:
  *   clazz     :: object's class pointer
  *   init_data :: optional pointer to initialization data
  *
  * @return:
  *   error code. 0 means success
  */
  FT_BASE( FT_Error )
  ft_object_create( FT_Object  *aobject,
                    FT_Class    clazz,
                    FT_Pointer  init_data );


 /**************************************************************
  *
  * @function: ft_object_create_from_type
  *
  * @description:
  *   create a new object (class instance) from a @FT_Type
  *
  * @output:
  *   aobject   :: new object handle. NULL in case of error
  *
  * @input:
  *   type      :: object's type descriptor
  *   init_data :: optional pointer to initialization data
  *
  * @return:
  *   error code. 0 means success
  *
  * @note:
  *   this function is slower than @ft_object_create
  *
  *   this is equivalent to calling @ft_class_from_type followed by
  *   @ft_object_create
  */
  FT_BASE( FT_Error )
  ft_object_create_from_type( FT_Object  *aobject,
                              FT_Type     type,
                              FT_Pointer  init_data,
                              FT_Library  library );



 /**************************************************************
  *
  * @macro FT_OBJ_CREATE (object,class,init)
  *
  * @description:
  *   a convenient macro used to create new objects. see
  *   @ft_object_create for details
  */
#define  FT_OBJ_CREATE( _obj, _clazz, _init )   \
               ft_object_create( FT_OBJECT_P(&(_obj)), _clazz, _init )


 /**************************************************************
  *
  * @macro FT_CREATE (object,class,init)
  *
  * @description:
  *   a convenient macro used to create new objects. It also
  *   sets an _implicit_ local variable named "error" to the error
  *   code returned by the object constructor.
  */
#define  FT_CREATE( _obj, _clazz, _init )  \
             FT_SET_ERROR( FT_OBJ_CREATE( _obj, _clazz, _init ) )

 /**************************************************************
  *
  * @macro FT_OBJ_CREATE_FROM_TYPE (object,type,init)
  *
  * @description:
  *   a convenient macro used to create new objects. see
  *   @ft_object_create_from_type for details
  */
#define  FT_OBJ_CREATE_FROM_TYPE( _obj, _type, _init, _lib )   \
               ft_object_create_from_type( FT_OBJECT_P(&(_obj)), _type, _init, _lib )


 /**************************************************************
  *
  * @macro FT_CREATE_FROM_TYPE (object,type,init)
  *
  * @description:
  *   a convenient macro used to create new objects. It also
  *   sets an _implicit_ local variable named "error" to the error
  *   code returned by the object constructor.
  */
#define  FT_CREATE_FROM_TYPE( _obj, _type, _init, _lib )  \
             FT_SET_ERROR( FT_OBJ_CREATE_FROM_TYPE( _obj, _type, _init, _lib ) )


 /* */

 /**************************************************************
  *
  * @function: ft_class_from_type
  *
  * @description:
  *   retrieves the class object corresponding to a given type
  *   descriptor. The class is created when needed
  *
  * @output:
  *   aclass  :: handle to corresponding class object. NULL in
  *              case of error
  *
  * @input:
  *   type    :: type descriptor handle
  *   library :: library handle
  *
  * @return:
  *   error code. 0 means success
  */
  FT_BASE( FT_Error )
  ft_class_from_type( FT_Class   *aclass,
                      FT_Type     type,
                      FT_Library  library );


 /* */

#include FT_INTERNAL_HASH_H

  typedef struct FT_ClassHNodeRec_*  FT_ClassHNode;

  typedef struct FT_ClassHNodeRec_
  {
    FT_HashNodeRec  hnode;
    FT_Type         type;
    FT_Class        clazz;

  } FT_ClassHNodeRec;

  typedef struct FT_MetaClassRec_
  {
    FT_ClassRec   clazz;         /* the meta-class is a class itself */
    FT_HashRec    type_to_class; /* the type => class hash table */

  } FT_MetaClassRec, *FT_MetaClass;


 /* initialize meta class */
  FT_BASE( FT_Error )
  ft_metaclass_init( FT_MetaClass  meta,
                     FT_Library    library );

 /* finalize meta class - destroy all registered class objects */
  FT_BASE( void )
  ft_metaclass_done( FT_MetaClass  meta );

 /* */

FT_END_HEADER

#endif /* __FT_OBJECT_H__ */