Customizing font functions: HarfBuzz Manual

## 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:

- `hb_font_get_font_h_extents_func_t`: returns the extents of the font for horizontal text.

- `hb_font_get_font_v_extents_func_t`: returns the extents of the font for vertical text.

- `hb_font_get_nominal_glyph_func_t`: returns the font's nominal glyph for a given code point.

- `hb_font_get_variation_glyph_func_t`: returns the font's glyph for a given code point when it is followed by a given Variation Selector.

- `hb_font_get_nominal_glyphs_func_t`: returns the font's nominal glyphs for a series of code points.

- `hb_font_get_glyph_advance_func_t`: returns the advance for a glyph.

- `hb_font_get_glyph_h_advance_func_t`: returns the advance for a glyph for horizontal text.

- `hb_font_get_glyph_v_advance_func_t`:returns the advance for a glyph for vertical text.

- `hb_font_get_glyph_advances_func_t`: returns the advances for a series of glyphs.

- `hb_font_get_glyph_h_advances_func_t`: returns the advances for a series of glyphs for horizontal text .

- `hb_font_get_glyph_v_advances_func_t`: returns the advances for a series of glyphs for vertical text.

- `hb_font_get_glyph_origin_func_t`: returns the origin coordinates of a glyph.

- `hb_font_get_glyph_h_origin_func_t`: returns the origin coordinates of a glyph for horizontal text.

- `hb_font_get_glyph_v_origin_func_t`: returns the origin coordinates of a glyph for vertical text.

- `hb_font_get_glyph_extents_func_t`: returns the extents for a glyph.

- `hb_font_get_glyph_contour_point_func_t`: returns the coordinates of a specific contour point from a glyph.

- `hb_font_get_glyph_name_func_t`: returns the name of a glyph (from its glyph index).

- `hb_font_get_glyph_from_name_func_t`: returns the glyph index that corresponds to a given glyph name.

- `hb_font_draw_glyph_or_fail_func_t`: gets the outlines of a glyph (by calling #hb_draw_funcs_t callbacks).

- `hb_font_paint_glyph_or_fail_func_t`: paints a glyph (by calling #hb_paint_funcs_t callbacks).

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);