Normal Cˆl–ßi~”ÔD¨  »‚Á2¦-ÿ¿ „ ¬¸ß_‡I|¥‰ÓMa–Ðz¾”Šj"Tâ�nãn\eõ1hßÃ?÷ FreeType Design / II

FreeType Design / II

II. Public Objects and Classes

We will now explain the abstractions provided by FreeType 2 to client applications to manage font files and data. As you would normally expect, these are implemented through objects and classes.

1. Object Orientation in FreeType 2

Though written in ANSI C, the library employs a few techniques, inherited from object-oriented programming, to make it easy to extend. Hence, the following conventions apply in the FreeType 2 source code.

  1. Almost all object types or classes have a corresponding structure type and a corresponding structure pointer type. The latter is called the handle type for the type or class.

    Consider that we need to manage objects of type ‘foo’ in FreeType 2. We would define the following structure and handle types as follows.

    typedef struct FT_FooRec_*  FT_Foo;
    
    typedef struct  FT_FooRec_
    {
      /* fields for the 'foo' class */
      ...
    
    } FT_FooRec;

    As a convention, handle types use simple but meaningful identifiers beginning with FT_, as in FT_Foo, while structures use the same name with a Rec suffix appended to it (‘Rec’ is short for ‘record’).

  2. Class derivation is achieved internally by wrapping base class structures into new ones. As an example, we define a ‘foobar’ class that is derived from ‘foo’. We would do something like this.

    typedef struct FT_FooBarRec_*  FT_FooBar;
    
    typedef struct  FT_FooBarRec_
    {
      /* the base 'foo' class fields */
      FT_FooRec  root;
    
      /* fields proper to the 'foobar' class */
      ...
    } FT_FooBarRec;

    As you can see, we ensure that a ‘foobar’ object is also a ‘foo’ object by placing a FT_FooRec at the start of the FT_FooBarRec definition. It is called root by convention.