1d6b ,ˆl–ßi~”ÔD¨  »‚Á2¦-ÿ „„Ë__‡I|¥‰ÓMÀÆ/Ê FreeType Design / V

FreeType Design / V

V. Interfaces and Services

We shall now go into detail about interfaces and services in FreeType.

Interfaces in FreeType are analogous to those found in object-oriented programming. They can be thought of as internal public APIs, and are essentially tables of function pointers.

Interfaces only describe the form and functionality, but the actual function body may be implemented elsewhere. The module that is implementing the interface will then pass the required function pointers to the table. This gives modularity and easy extendability.

There are two main kinds of interfaces: module interfaces, and services.

Module interfaces are defined for each module. For example, every font driver provides its own set of procedures for use in the base layer, which are registered in an FT_Driver. This way, very different font drivers can be used in the same way in the base layer.

Services are cross-module interfaces. These provide functionality needed in several font drivers.

Services are created when code from one module needs to be used in another. Rather than include files from another module, a service is created instead. Now, the other module just needs to include the header defining the interface.

Helper modules are an extreme example of this; all their public functionality is made for use in other font drivers and hence are in a single service.

In-depth guide: Creating a service

This section will teach you how to write your own service.

  1. Make the service interface header.

    We will be calling our service demo for demonstration purposes. First, create the header file, which goes in include/freetype/internal/services, and add the boilerplate.

    #include FT_INTERNAL_SERVICE_H
    FT_BEGIN_HEADER
    #define FT_SERVICE_ID_DEMO  "demo"
    
    /* ...  */
    
    FT_END_HEADER

    This line in particular is required to register the new service later on.

    #define service-id service-tag