1cb93a386Sopenharmony_ci<?xml version="1.0"?> 2cb93a386Sopenharmony_ci<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" 3cb93a386Sopenharmony_ci "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [ 4cb93a386Sopenharmony_ci <!ENTITY % local.common.attrib "xmlns:xi CDATA #FIXED 'http://www.w3.org/2003/XInclude'"> 5cb93a386Sopenharmony_ci <!ENTITY version SYSTEM "version.xml"> 6cb93a386Sopenharmony_ci]> 7cb93a386Sopenharmony_ci<chapter id="fonts-and-faces"> 8cb93a386Sopenharmony_ci <title>Fonts, faces, and output</title> 9cb93a386Sopenharmony_ci <para> 10cb93a386Sopenharmony_ci In the previous chapter, we saw how to set up a buffer and fill 11cb93a386Sopenharmony_ci it with text as Unicode code points. In order to shape this 12cb93a386Sopenharmony_ci buffer text with HarfBuzz, you will need also need a font 13cb93a386Sopenharmony_ci object. 14cb93a386Sopenharmony_ci </para> 15cb93a386Sopenharmony_ci <para> 16cb93a386Sopenharmony_ci HarfBuzz provides abstractions to help you cache and reuse the 17cb93a386Sopenharmony_ci heavier parts of working with binary fonts, so we will look at 18cb93a386Sopenharmony_ci how to do that. We will also look at how to work with the 19cb93a386Sopenharmony_ci FreeType font-rendering library and at how you can customize 20cb93a386Sopenharmony_ci HarfBuzz to work with other libraries. 21cb93a386Sopenharmony_ci </para> 22cb93a386Sopenharmony_ci <para> 23cb93a386Sopenharmony_ci Finally, we will look at how to work with OpenType variable 24cb93a386Sopenharmony_ci fonts, the latest update to the OpenType font format, and at 25cb93a386Sopenharmony_ci some other recent additions to OpenType. 26cb93a386Sopenharmony_ci </para> 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_ci <section id="fonts-and-faces-objects"> 29cb93a386Sopenharmony_ci <title>Font and face objects</title> 30cb93a386Sopenharmony_ci <para> 31cb93a386Sopenharmony_ci The outcome of shaping a run of text depends on the contents of 32cb93a386Sopenharmony_ci a specific font file (such as the substitutions and positioning 33cb93a386Sopenharmony_ci moves in the 'GSUB' and 'GPOS' tables), so HarfBuzz makes 34cb93a386Sopenharmony_ci accessing those internals fast. 35cb93a386Sopenharmony_ci </para> 36cb93a386Sopenharmony_ci <para> 37cb93a386Sopenharmony_ci An <type>hb_face_t</type> represents a <emphasis>face</emphasis> 38cb93a386Sopenharmony_ci in HarfBuzz. This data type is a wrapper around an 39cb93a386Sopenharmony_ci <type>hb_blob_t</type> blob that holds the contents of a binary 40cb93a386Sopenharmony_ci font file. Since HarfBuzz supports TrueType Collections and 41cb93a386Sopenharmony_ci OpenType Collections (each of which can include multiple 42cb93a386Sopenharmony_ci typefaces), a HarfBuzz face also requires an index number 43cb93a386Sopenharmony_ci specifying which typeface in the file you want to use. Most of 44cb93a386Sopenharmony_ci the font files you will encounter in the wild include just a 45cb93a386Sopenharmony_ci single face, however, so most of the time you would pass in 46cb93a386Sopenharmony_ci <literal>0</literal> as the index when you create a face: 47cb93a386Sopenharmony_ci </para> 48cb93a386Sopenharmony_ci <programlisting language="C"> 49cb93a386Sopenharmony_ci hb_blob_t* blob = hb_blob_create_from_file(file); 50cb93a386Sopenharmony_ci ... 51cb93a386Sopenharmony_ci hb_face_t* face = hb_face_create(blob, 0); 52cb93a386Sopenharmony_ci </programlisting> 53cb93a386Sopenharmony_ci <para> 54cb93a386Sopenharmony_ci On its own, a face object is not quite ready to use for 55cb93a386Sopenharmony_ci shaping. The typeface must be set to a specific point size in 56cb93a386Sopenharmony_ci order for some details (such as hinting) to work. In addition, 57cb93a386Sopenharmony_ci if the font file in question is an OpenType Variable Font, then 58cb93a386Sopenharmony_ci you may need to specify one or variation-axis settings (or a 59cb93a386Sopenharmony_ci named instance) in order to get the output you need. 60cb93a386Sopenharmony_ci </para> 61cb93a386Sopenharmony_ci <para> 62cb93a386Sopenharmony_ci In HarfBuzz, you do this by creating a <emphasis>font</emphasis> 63cb93a386Sopenharmony_ci object from your face. 64cb93a386Sopenharmony_ci </para> 65cb93a386Sopenharmony_ci <para> 66cb93a386Sopenharmony_ci Font objects also have the advantage of being considerably 67cb93a386Sopenharmony_ci lighter-weight than face objects (remember that a face contains 68cb93a386Sopenharmony_ci the contents of a binary font file mapped into memory). As a 69cb93a386Sopenharmony_ci result, you can cache and reuse a font object, but you could 70cb93a386Sopenharmony_ci also create a new one for each additional size you needed. 71cb93a386Sopenharmony_ci Creating new fonts incurs some additional overhead, of course, 72cb93a386Sopenharmony_ci but whether or not it is excessive is your call in the end. In 73cb93a386Sopenharmony_ci contrast, face objects are substantially larger, and you really 74cb93a386Sopenharmony_ci should cache them and reuse them whenever possible. 75cb93a386Sopenharmony_ci </para> 76cb93a386Sopenharmony_ci <para> 77cb93a386Sopenharmony_ci You can create a font object from a face object: 78cb93a386Sopenharmony_ci </para> 79cb93a386Sopenharmony_ci <programlisting language="C"> 80cb93a386Sopenharmony_ci hb_font_t* hb_font = hb_font_create(hb_face); 81cb93a386Sopenharmony_ci </programlisting> 82cb93a386Sopenharmony_ci <para> 83cb93a386Sopenharmony_ci After creating a font, there are a few properties you should 84cb93a386Sopenharmony_ci set. Many fonts enable and disable hints based on the size it 85cb93a386Sopenharmony_ci is used at, so setting this is important for font 86cb93a386Sopenharmony_ci objects. <function>hb_font_set_ppem(font, x_ppem, 87cb93a386Sopenharmony_ci y_ppem)</function> sets the pixels-per-EM value of the font. You 88cb93a386Sopenharmony_ci can also set the point size of the font with 89cb93a386Sopenharmony_ci <function>hb_font_set_ptem(font, ptem)</function>. HarfBuzz uses the 90cb93a386Sopenharmony_ci industry standard 72 points per inch. 91cb93a386Sopenharmony_ci </para> 92cb93a386Sopenharmony_ci <para> 93cb93a386Sopenharmony_ci HarfBuzz lets you specify the degree subpixel precision you want 94cb93a386Sopenharmony_ci through a scaling factor. You can set horizontal and 95cb93a386Sopenharmony_ci vertical scaling factors on the 96cb93a386Sopenharmony_ci font by calling <function>hb_font_set_scale(font, x_scale, 97cb93a386Sopenharmony_ci y_scale)</function>. 98cb93a386Sopenharmony_ci </para> 99cb93a386Sopenharmony_ci <para> 100cb93a386Sopenharmony_ci There may be times when you are handed a font object and need to 101cb93a386Sopenharmony_ci access the face object that it comes from. For that, you can call 102cb93a386Sopenharmony_ci </para> 103cb93a386Sopenharmony_ci <programlisting language="C"> 104cb93a386Sopenharmony_ci hb_face = hb_font_get_face(hb_font); 105cb93a386Sopenharmony_ci </programlisting> 106cb93a386Sopenharmony_ci <para> 107cb93a386Sopenharmony_ci You can also create a font object from an existing font object 108cb93a386Sopenharmony_ci using the <function>hb_font_create_sub_font()</function> 109cb93a386Sopenharmony_ci function. This creates a child font object that is initiated 110cb93a386Sopenharmony_ci with the same attributes as its parent; it can be used to 111cb93a386Sopenharmony_ci quickly set up a new font for the purpose of overriding a specific 112cb93a386Sopenharmony_ci font-functions method. 113cb93a386Sopenharmony_ci </para> 114cb93a386Sopenharmony_ci <para> 115cb93a386Sopenharmony_ci All face objects and font objects are lifecycle-managed by 116cb93a386Sopenharmony_ci HarfBuzz. After creating a face, you increase its reference 117cb93a386Sopenharmony_ci count with <function>hb_face_reference(face)</function> and 118cb93a386Sopenharmony_ci decrease it with 119cb93a386Sopenharmony_ci <function>hb_face_destroy(face)</function>. Likewise, you 120cb93a386Sopenharmony_ci increase the reference count on a font with 121cb93a386Sopenharmony_ci <function>hb_font_reference(font)</function> and decrease it 122cb93a386Sopenharmony_ci with <function>hb_font_destroy(font)</function>. 123cb93a386Sopenharmony_ci </para> 124cb93a386Sopenharmony_ci <para> 125cb93a386Sopenharmony_ci You can also attach user data to face objects and font objects. 126cb93a386Sopenharmony_ci </para> 127cb93a386Sopenharmony_ci </section> 128cb93a386Sopenharmony_ci 129cb93a386Sopenharmony_ci <section id="fonts-and-faces-custom-functions"> 130cb93a386Sopenharmony_ci <title>Customizing font functions</title> 131cb93a386Sopenharmony_ci <para> 132cb93a386Sopenharmony_ci During shaping, HarfBuzz frequently needs to query font objects 133cb93a386Sopenharmony_ci to get at the contents and parameters of the glyphs in a font 134cb93a386Sopenharmony_ci file. It includes a built-in set of functions that is tailored 135cb93a386Sopenharmony_ci to working with OpenType fonts. However, as was the case with 136cb93a386Sopenharmony_ci Unicode functions in the buffers chapter, HarfBuzz also wants to 137cb93a386Sopenharmony_ci make it easy for you to assign a substitute set of font 138cb93a386Sopenharmony_ci functions if you are developing a program to work with a library 139cb93a386Sopenharmony_ci or platform that provides its own font functions. 140cb93a386Sopenharmony_ci </para> 141cb93a386Sopenharmony_ci <para> 142cb93a386Sopenharmony_ci Therefore, the HarfBuzz API defines a set of virtual 143cb93a386Sopenharmony_ci methods for accessing font-object properties, and you can 144cb93a386Sopenharmony_ci replace the defaults with your own selections without 145cb93a386Sopenharmony_ci interfering with the shaping process. Each font object in 146cb93a386Sopenharmony_ci HarfBuzz includes a structure called 147cb93a386Sopenharmony_ci <literal>font_funcs</literal> that serves as a vtable for the 148cb93a386Sopenharmony_ci font object. The virtual methods in 149cb93a386Sopenharmony_ci <literal>font_funcs</literal> are: 150cb93a386Sopenharmony_ci </para> 151cb93a386Sopenharmony_ci <itemizedlist> 152cb93a386Sopenharmony_ci <listitem> 153cb93a386Sopenharmony_ci <para> 154cb93a386Sopenharmony_ci <function>hb_font_get_font_h_extents_func_t</function>: returns 155cb93a386Sopenharmony_ci the extents of the font for horizontal text. 156cb93a386Sopenharmony_ci </para> 157cb93a386Sopenharmony_ci </listitem> 158cb93a386Sopenharmony_ci <listitem> 159cb93a386Sopenharmony_ci <para> 160cb93a386Sopenharmony_ci <function>hb_font_get_font_v_extents_func_t</function>: returns 161cb93a386Sopenharmony_ci the extents of the font for vertical text. 162cb93a386Sopenharmony_ci </para> 163cb93a386Sopenharmony_ci </listitem> 164cb93a386Sopenharmony_ci <listitem> 165cb93a386Sopenharmony_ci <para> 166cb93a386Sopenharmony_ci <function>hb_font_get_nominal_glyph_func_t</function>: returns 167cb93a386Sopenharmony_ci the font's nominal glyph for a given code point. 168cb93a386Sopenharmony_ci </para> 169cb93a386Sopenharmony_ci </listitem> 170cb93a386Sopenharmony_ci <listitem> 171cb93a386Sopenharmony_ci <para> 172cb93a386Sopenharmony_ci <function>hb_font_get_variation_glyph_func_t</function>: returns 173cb93a386Sopenharmony_ci the font's glyph for a given code point when it is followed by a 174cb93a386Sopenharmony_ci given Variation Selector. 175cb93a386Sopenharmony_ci </para> 176cb93a386Sopenharmony_ci </listitem> 177cb93a386Sopenharmony_ci <listitem> 178cb93a386Sopenharmony_ci <para> 179cb93a386Sopenharmony_ci <function>hb_font_get_nominal_glyphs_func_t</function>: returns 180cb93a386Sopenharmony_ci the font's nominal glyphs for a series of code points. 181cb93a386Sopenharmony_ci </para> 182cb93a386Sopenharmony_ci </listitem> 183cb93a386Sopenharmony_ci <listitem> 184cb93a386Sopenharmony_ci <para> 185cb93a386Sopenharmony_ci <function>hb_font_get_glyph_advance_func_t</function>: returns 186cb93a386Sopenharmony_ci the advance for a glyph. 187cb93a386Sopenharmony_ci </para> 188cb93a386Sopenharmony_ci </listitem> 189cb93a386Sopenharmony_ci <listitem> 190cb93a386Sopenharmony_ci <para> 191cb93a386Sopenharmony_ci <function>hb_font_get_glyph_h_advance_func_t</function>: returns 192cb93a386Sopenharmony_ci the advance for a glyph for horizontal text. 193cb93a386Sopenharmony_ci </para> 194cb93a386Sopenharmony_ci </listitem> 195cb93a386Sopenharmony_ci <listitem> 196cb93a386Sopenharmony_ci <para> 197cb93a386Sopenharmony_ci <function>hb_font_get_glyph_v_advance_func_t</function>:returns 198cb93a386Sopenharmony_ci the advance for a glyph for vertical text. 199cb93a386Sopenharmony_ci </para> 200cb93a386Sopenharmony_ci </listitem> 201cb93a386Sopenharmony_ci <listitem> 202cb93a386Sopenharmony_ci <para> 203cb93a386Sopenharmony_ci <function>hb_font_get_glyph_advances_func_t</function>: returns 204cb93a386Sopenharmony_ci the advances for a series of glyphs. 205cb93a386Sopenharmony_ci </para> 206cb93a386Sopenharmony_ci </listitem> 207cb93a386Sopenharmony_ci <listitem> 208cb93a386Sopenharmony_ci <para> 209cb93a386Sopenharmony_ci <function>hb_font_get_glyph_h_advances_func_t</function>: returns 210cb93a386Sopenharmony_ci the advances for a series of glyphs for horizontal text . 211cb93a386Sopenharmony_ci </para> 212cb93a386Sopenharmony_ci </listitem> 213cb93a386Sopenharmony_ci <listitem> 214cb93a386Sopenharmony_ci <para> 215cb93a386Sopenharmony_ci <function>hb_font_get_glyph_v_advances_func_t</function>: returns 216cb93a386Sopenharmony_ci the advances for a series of glyphs for vertical text. 217cb93a386Sopenharmony_ci </para> 218cb93a386Sopenharmony_ci </listitem> 219cb93a386Sopenharmony_ci <listitem> 220cb93a386Sopenharmony_ci <para> 221cb93a386Sopenharmony_ci <function>hb_font_get_glyph_origin_func_t</function>: returns 222cb93a386Sopenharmony_ci the origin coordinates of a glyph. 223cb93a386Sopenharmony_ci </para> 224cb93a386Sopenharmony_ci </listitem> 225cb93a386Sopenharmony_ci <listitem> 226cb93a386Sopenharmony_ci <para> 227cb93a386Sopenharmony_ci <function>hb_font_get_glyph_h_origin_func_t</function>: returns 228cb93a386Sopenharmony_ci the origin coordinates of a glyph for horizontal text. 229cb93a386Sopenharmony_ci </para> 230cb93a386Sopenharmony_ci </listitem> 231cb93a386Sopenharmony_ci <listitem> 232cb93a386Sopenharmony_ci <para> 233cb93a386Sopenharmony_ci <function>hb_font_get_glyph_v_origin_func_t</function>: returns 234cb93a386Sopenharmony_ci the origin coordinates of a glyph for vertical text. 235cb93a386Sopenharmony_ci </para> 236cb93a386Sopenharmony_ci </listitem> 237cb93a386Sopenharmony_ci <listitem> 238cb93a386Sopenharmony_ci <para> 239cb93a386Sopenharmony_ci <function>hb_font_get_glyph_extents_func_t</function>: returns 240cb93a386Sopenharmony_ci the extents for a glyph. 241cb93a386Sopenharmony_ci </para> 242cb93a386Sopenharmony_ci </listitem> 243cb93a386Sopenharmony_ci <listitem> 244cb93a386Sopenharmony_ci <para> 245cb93a386Sopenharmony_ci <function>hb_font_get_glyph_contour_point_func_t</function>: 246cb93a386Sopenharmony_ci returns the coordinates of a specific contour point from a glyph. 247cb93a386Sopenharmony_ci </para> 248cb93a386Sopenharmony_ci </listitem> 249cb93a386Sopenharmony_ci <listitem> 250cb93a386Sopenharmony_ci <para> 251cb93a386Sopenharmony_ci <function>hb_font_get_glyph_name_func_t</function>: returns the 252cb93a386Sopenharmony_ci name of a glyph (from its glyph index). 253cb93a386Sopenharmony_ci </para> 254cb93a386Sopenharmony_ci </listitem> 255cb93a386Sopenharmony_ci <listitem> 256cb93a386Sopenharmony_ci <para> 257cb93a386Sopenharmony_ci <function>hb_font_get_glyph_from_name_func_t</function>: returns 258cb93a386Sopenharmony_ci the glyph index that corresponds to a given glyph name. 259cb93a386Sopenharmony_ci </para> 260cb93a386Sopenharmony_ci </listitem> 261cb93a386Sopenharmony_ci </itemizedlist> 262cb93a386Sopenharmony_ci <para> 263cb93a386Sopenharmony_ci You can create new font-functions by calling 264cb93a386Sopenharmony_ci <function>hb_font_funcs_create()</function>: 265cb93a386Sopenharmony_ci </para> 266cb93a386Sopenharmony_ci <programlisting language="C"> 267cb93a386Sopenharmony_ci hb_font_funcs_t *ffunctions = hb_font_funcs_create (); 268cb93a386Sopenharmony_ci hb_font_set_funcs (font, ffunctions, font_data, destroy); 269cb93a386Sopenharmony_ci </programlisting> 270cb93a386Sopenharmony_ci <para> 271cb93a386Sopenharmony_ci The individual methods can each be set with their own setter 272cb93a386Sopenharmony_ci function, such as 273cb93a386Sopenharmony_ci <function>hb_font_funcs_set_nominal_glyph_func(ffunctions, 274cb93a386Sopenharmony_ci func, user_data, destroy)</function>. 275cb93a386Sopenharmony_ci </para> 276cb93a386Sopenharmony_ci <para> 277cb93a386Sopenharmony_ci Font-functions structures can be reused for multiple font 278cb93a386Sopenharmony_ci objects, and can be reference counted with 279cb93a386Sopenharmony_ci <function>hb_font_funcs_reference()</function> and 280cb93a386Sopenharmony_ci <function>hb_font_funcs_destroy()</function>. Just like other 281cb93a386Sopenharmony_ci objects in HarfBuzz, you can set user-data for each 282cb93a386Sopenharmony_ci font-functions structure and assign a destroy callback for 283cb93a386Sopenharmony_ci it. 284cb93a386Sopenharmony_ci </para> 285cb93a386Sopenharmony_ci <para> 286cb93a386Sopenharmony_ci You can also mark a font-functions structure as immutable, 287cb93a386Sopenharmony_ci with <function>hb_font_funcs_make_immutable()</function>. This 288cb93a386Sopenharmony_ci is especially useful if your code is a library or framework that 289cb93a386Sopenharmony_ci will have its own client programs. By marking your 290cb93a386Sopenharmony_ci font-functions structures as immutable, you prevent your client 291cb93a386Sopenharmony_ci programs from changing the configuration and introducing 292cb93a386Sopenharmony_ci inconsistencies and errors downstream. 293cb93a386Sopenharmony_ci </para> 294cb93a386Sopenharmony_ci <para> 295cb93a386Sopenharmony_ci To override only some functions while using the default implementation 296cb93a386Sopenharmony_ci for the others, you will need to create a sub-font. By default, the 297cb93a386Sopenharmony_ci sub-font uses the font functions of its parent except for the functions 298cb93a386Sopenharmony_ci that were explicitly set. The following code will override only the 299cb93a386Sopenharmony_ci <function>hb_font_get_nominal_glyph_func_t</function> for the sub-font: 300cb93a386Sopenharmony_ci </para> 301cb93a386Sopenharmony_ci <programlisting language="C"> 302cb93a386Sopenharmony_ci hb_font_t *subfont = hb_font_create_sub_font (font) 303cb93a386Sopenharmony_ci hb_font_funcs_t *ffunctions = hb_font_funcs_create (); 304cb93a386Sopenharmony_ci hb_font_funcs_set_nominal_glyph_func (ffunctions, func, user_data, destroy); 305cb93a386Sopenharmony_ci hb_font_set_funcs (subfont, ffunctions, font_data, destroy); 306cb93a386Sopenharmony_ci hb_font_funcs_destroy (ffunctions); 307cb93a386Sopenharmony_ci </programlisting> 308cb93a386Sopenharmony_ci </section> 309cb93a386Sopenharmony_ci 310cb93a386Sopenharmony_ci <section id="fonts-and-faces-native-opentype"> 311cb93a386Sopenharmony_ci <title>Font objects and HarfBuzz's native OpenType implementation</title> 312cb93a386Sopenharmony_ci <para> 313cb93a386Sopenharmony_ci By default, whenever HarfBuzz creates a font object, it will 314cb93a386Sopenharmony_ci configure the font to use a built-in set of font functions that 315cb93a386Sopenharmony_ci supports contemporary OpenType font internals. If you want to 316cb93a386Sopenharmony_ci work with OpenType or TrueType fonts, you should be able to use 317cb93a386Sopenharmony_ci these functions without difficulty. 318cb93a386Sopenharmony_ci </para> 319cb93a386Sopenharmony_ci <para> 320cb93a386Sopenharmony_ci Many of the methods in the font-functions structure deal with 321cb93a386Sopenharmony_ci the fundamental properties of glyphs that are required for 322cb93a386Sopenharmony_ci shaping text: extents (the maximums and minimums on each axis), 323cb93a386Sopenharmony_ci origins (the <literal>(0,0)</literal> coordinate point which 324cb93a386Sopenharmony_ci glyphs are drawn in reference to), and advances (the amount that 325cb93a386Sopenharmony_ci the cursor needs to be moved after drawing each glyph, including 326cb93a386Sopenharmony_ci any empty space for the glyph's side bearings). 327cb93a386Sopenharmony_ci </para> 328cb93a386Sopenharmony_ci <para> 329cb93a386Sopenharmony_ci As you can see in the list of functions, there are separate "horizontal" 330cb93a386Sopenharmony_ci and "vertical" variants depending on whether the text is set in 331cb93a386Sopenharmony_ci the horizontal or vertical direction. For some scripts, fonts 332cb93a386Sopenharmony_ci that are designed to support text set horizontally or vertically (for 333cb93a386Sopenharmony_ci example, in Japanese) may include metrics for both text 334cb93a386Sopenharmony_ci directions. When fonts don't include this information, HarfBuzz 335cb93a386Sopenharmony_ci does its best to transform what the font provides. 336cb93a386Sopenharmony_ci </para> 337cb93a386Sopenharmony_ci <para> 338cb93a386Sopenharmony_ci In addition to the direction-specific functions, HarfBuzz 339cb93a386Sopenharmony_ci provides some higher-level functions for fetching information 340cb93a386Sopenharmony_ci like extents and advances for a glyph. If you call 341cb93a386Sopenharmony_ci </para> 342cb93a386Sopenharmony_ci <programlisting language="C"> 343cb93a386Sopenharmony_ci hb_font_get_glyph_advance_for_direction(font, direction, extents); 344cb93a386Sopenharmony_ci </programlisting> 345cb93a386Sopenharmony_ci <para> 346cb93a386Sopenharmony_ci then you can provide any <type>hb_direction_t</type> as the 347cb93a386Sopenharmony_ci <parameter>direction</parameter> parameter, and HarfBuzz will 348cb93a386Sopenharmony_ci use the correct function variant for the text direction. There 349cb93a386Sopenharmony_ci are similar higher-level versions of the functions for fetching 350cb93a386Sopenharmony_ci extents, origin coordinates, and contour-point 351cb93a386Sopenharmony_ci coordinates. There are also addition and subtraction functions 352cb93a386Sopenharmony_ci for moving points with respect to the origin. 353cb93a386Sopenharmony_ci </para> 354cb93a386Sopenharmony_ci <para> 355cb93a386Sopenharmony_ci There are also methods for fetching the glyph ID that 356cb93a386Sopenharmony_ci corresponds to a Unicode code point (possibly when followed by a 357cb93a386Sopenharmony_ci variation-selector code point), fetching the glyph name from the 358cb93a386Sopenharmony_ci font, and fetching the glyph ID that corresponds to a glyph name 359cb93a386Sopenharmony_ci you already have. 360cb93a386Sopenharmony_ci </para> 361cb93a386Sopenharmony_ci <para> 362cb93a386Sopenharmony_ci HarfBuzz also provides functions for converting between glyph 363cb93a386Sopenharmony_ci names and string 364cb93a386Sopenharmony_ci variables. <function>hb_font_glyph_to_string(font, glyph, s, 365cb93a386Sopenharmony_ci size)</function> retrieves the name for the glyph ID 366cb93a386Sopenharmony_ci <parameter>glyph</parameter> from the font object. It generates a 367cb93a386Sopenharmony_ci generic name of the form <literal>gidDDD</literal> (where DDD is 368cb93a386Sopenharmony_ci the glyph index) if there is no name for the glyph in the 369cb93a386Sopenharmony_ci font. The <function>hb_font_glyph_from_string(font, s, len, 370cb93a386Sopenharmony_ci glyph)</function> takes an input string <parameter>s</parameter> 371cb93a386Sopenharmony_ci and looks for a glyph with that name in the font, returning its 372cb93a386Sopenharmony_ci glyph ID in the <parameter>glyph</parameter> 373cb93a386Sopenharmony_ci output parameter. It automatically parses 374cb93a386Sopenharmony_ci <literal>gidDDD</literal> and <literal>uniUUUU</literal> strings. 375cb93a386Sopenharmony_ci </para> 376cb93a386Sopenharmony_ci </section> 377cb93a386Sopenharmony_ci 378cb93a386Sopenharmony_ci 379cb93a386Sopenharmony_ci <!-- Commenting out FreeType integration section-holder for now. May move 380cb93a386Sopenharmony_ci to the full-blown Integration Chapter. --> 381cb93a386Sopenharmony_ci 382cb93a386Sopenharmony_ci <!-- <section id="fonts-and-faces-freetype"> 383cb93a386Sopenharmony_ci <title>Using FreeType</title> 384cb93a386Sopenharmony_ci <para> 385cb93a386Sopenharmony_ci 386cb93a386Sopenharmony_ci </para> 387cb93a386Sopenharmony_ci <para> 388cb93a386Sopenharmony_ci 389cb93a386Sopenharmony_ci </para> 390cb93a386Sopenharmony_ci </section> --> 391cb93a386Sopenharmony_ci 392cb93a386Sopenharmony_ci <section id="fonts-and-faces-variable"> 393cb93a386Sopenharmony_ci <title>Working with OpenType Variable Fonts</title> 394cb93a386Sopenharmony_ci <para> 395cb93a386Sopenharmony_ci If you are working with OpenType Variable Fonts, there are a few 396cb93a386Sopenharmony_ci additional functions you should use to specify the 397cb93a386Sopenharmony_ci variation-axis settings of your font object. Without doing so, 398cb93a386Sopenharmony_ci your variable font's font object can still be used, but only at 399cb93a386Sopenharmony_ci the default setting for every axis (which, of course, is 400cb93a386Sopenharmony_ci sometimes what you want, but does not cover general usage). 401cb93a386Sopenharmony_ci </para> 402cb93a386Sopenharmony_ci <para> 403cb93a386Sopenharmony_ci HarfBuzz manages variation settings in the 404cb93a386Sopenharmony_ci <type>hb_variation_t</type> data type, which holds a <property>tag</property> for the 405cb93a386Sopenharmony_ci variation-axis identifier tag and a <property>value</property> for its 406cb93a386Sopenharmony_ci setting. You can retrieve the list of variation axes in a font 407cb93a386Sopenharmony_ci binary from the face object (not from a font object, notably) by 408cb93a386Sopenharmony_ci calling <function>hb_ot_var_get_axis_count(face)</function> to 409cb93a386Sopenharmony_ci find the number of axes, then using 410cb93a386Sopenharmony_ci <function>hb_ot_var_get_axis_infos()</function> to collect the 411cb93a386Sopenharmony_ci axis structures: 412cb93a386Sopenharmony_ci </para> 413cb93a386Sopenharmony_ci <programlisting language="C"> 414cb93a386Sopenharmony_ci axes = hb_ot_var_get_axis_count(face); 415cb93a386Sopenharmony_ci ... 416cb93a386Sopenharmony_ci hb_ot_var_get_axis_infos(face, 0, axes, axes_array); 417cb93a386Sopenharmony_ci </programlisting> 418cb93a386Sopenharmony_ci <para> 419cb93a386Sopenharmony_ci For each axis returned in the array, you can can access the 420cb93a386Sopenharmony_ci identifier in its <property>tag</property>. HarfBuzz also has 421cb93a386Sopenharmony_ci tag definitions predefined for the five standard axes specified 422cb93a386Sopenharmony_ci in OpenType (<literal>ital</literal> for italic, 423cb93a386Sopenharmony_ci <literal>opsz</literal> for optical size, 424cb93a386Sopenharmony_ci <literal>slnt</literal> for slant, <literal>wdth</literal> for 425cb93a386Sopenharmony_ci width, and <literal>wght</literal> for weight). Each axis also 426cb93a386Sopenharmony_ci has a <property>min_value</property>, a 427cb93a386Sopenharmony_ci <property>default_value</property>, and a <property>max_value</property>. 428cb93a386Sopenharmony_ci </para> 429cb93a386Sopenharmony_ci <para> 430cb93a386Sopenharmony_ci To set your font object's variation settings, you call the 431cb93a386Sopenharmony_ci <function>hb_font_set_variations()</function> function with an 432cb93a386Sopenharmony_ci array of <type>hb_variation_t</type> variation settings. Let's 433cb93a386Sopenharmony_ci say our font has weight and width axes. We need to specify each 434cb93a386Sopenharmony_ci of the axes by tag and assign a value on the axis: 435cb93a386Sopenharmony_ci </para> 436cb93a386Sopenharmony_ci <programlisting language="C"> 437cb93a386Sopenharmony_ci unsigned int variation_count = 2; 438cb93a386Sopenharmony_ci hb_variation_t variation_data[variation_count]; 439cb93a386Sopenharmony_ci variation_data[0].tag = HB_OT_TAG_VAR_AXIS_WIDTH; 440cb93a386Sopenharmony_ci variation_data[1].tag = HB_OT_TAG_VAR_AXIS_WEIGHT; 441cb93a386Sopenharmony_ci variation_data[0].value = 80; 442cb93a386Sopenharmony_ci variation_data[1].value = 750; 443cb93a386Sopenharmony_ci ... 444cb93a386Sopenharmony_ci hb_font_set_variations(font, variation_data, variation_count); 445cb93a386Sopenharmony_ci </programlisting> 446cb93a386Sopenharmony_ci <para> 447cb93a386Sopenharmony_ci That should give us a slightly condensed font ("normal" on the 448cb93a386Sopenharmony_ci <literal>wdth</literal> axis is 100) at a noticeably bolder 449cb93a386Sopenharmony_ci weight ("regular" is 400 on the <literal>wght</literal> axis). 450cb93a386Sopenharmony_ci </para> 451cb93a386Sopenharmony_ci <para> 452cb93a386Sopenharmony_ci In practice, though, you should always check that the value you 453cb93a386Sopenharmony_ci want to set on the axis is within the 454cb93a386Sopenharmony_ci [<property>min_value</property>,<property>max_value</property>] 455cb93a386Sopenharmony_ci range actually implemented in the font's variation axis. After 456cb93a386Sopenharmony_ci all, a font might only provide lighter-than-regular weights, and 457cb93a386Sopenharmony_ci setting a heavier value on the <literal>wght</literal> axis will 458cb93a386Sopenharmony_ci not change that. 459cb93a386Sopenharmony_ci </para> 460cb93a386Sopenharmony_ci <para> 461cb93a386Sopenharmony_ci Once your variation settings are specified on your font object, 462cb93a386Sopenharmony_ci however, shaping with a variable font is just like shaping a 463cb93a386Sopenharmony_ci static font. 464cb93a386Sopenharmony_ci </para> 465cb93a386Sopenharmony_ci </section> 466cb93a386Sopenharmony_ci 467cb93a386Sopenharmony_ci </chapter> 468