Customizing font functions

During shaping, HarfBuzz frequently needs to query font objects to get at the contents and parameters of the glyphs in a font file. It includes a built-in set of functions that is tailored to working with OpenType fonts. However, as was the case with Unicode functions in the buffers chapter, HarfBuzz also wants to make it easy for you to assign a substitute set of font functions if you are developing a program to work with a library or platform that provides its own font functions.

Therefore, the HarfBuzz API defines a set of virtual methods for accessing font-object properties, and you can replace the defaults with your own selections without interfering with the shaping process. Each font object in HarfBuzz includes a structure called font_funcs that serves as a vtable for the font object. The virtual methods in font_funcs are:

You can create new font-functions by calling hb_font_funcs_create():

      hb_font_funcs_t *ffunctions = hb_font_funcs_create ();
      hb_font_set_funcs (font, ffunctions, font_data, destroy);
    

The individual methods can each be set with their own setter function, such as hb_font_funcs_set_nominal_glyph_func(ffunctions, func, user_data, destroy).

Font-functions structures can be reused for multiple font objects, and can be reference counted with hb_font_funcs_reference() and hb_font_funcs_destroy(). Just like other objects in HarfBuzz, you can set user-data for each font-functions structure and assign a destroy callback for it.

You can also mark a font-functions structure as immutable, with hb_font_funcs_make_immutable(). This is especially useful if your code is a library or framework that will have its own client programs. By marking your font-functions structures as immutable, you prevent your client programs from changing the configuration and introducing inconsistencies and errors downstream.

To override only some functions while using the default implementation for the others, you will need to create a sub-font. By default, the sub-font uses the font functions of its parent except for the functions that were explicitly set. The following code will override only the hb_font_get_nominal_glyph_func_t for the sub-font:

      hb_font_t *subfont = hb_font_create_sub_font (font)
      hb_font_funcs_t *ffunctions = hb_font_funcs_create ();
      hb_font_funcs_set_nominal_glyph_func (ffunctions, func, user_data, destroy);
      hb_font_set_funcs (subfont, ffunctions, font_data, destroy);
      hb_font_funcs_destroy (ffunctions);