1*2d1272b8SAndroid Build Coastguard Worker<?xml version="1.0"?> 2*2d1272b8SAndroid Build Coastguard Worker<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN" 3*2d1272b8SAndroid Build Coastguard Worker "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [ 4*2d1272b8SAndroid Build Coastguard Worker <!ENTITY % local.common.attrib "xmlns:xi CDATA #FIXED 'http://www.w3.org/2003/XInclude'"> 5*2d1272b8SAndroid Build Coastguard Worker <!ENTITY version SYSTEM "version.xml"> 6*2d1272b8SAndroid Build Coastguard Worker]> 7*2d1272b8SAndroid Build Coastguard Worker<chapter id="object-model"> 8*2d1272b8SAndroid Build Coastguard Worker <title>The HarfBuzz object model</title> 9*2d1272b8SAndroid Build Coastguard Worker <section id="object-model-intro"> 10*2d1272b8SAndroid Build Coastguard Worker <title>An overview of data types in HarfBuzz</title> 11*2d1272b8SAndroid Build Coastguard Worker <para> 12*2d1272b8SAndroid Build Coastguard Worker HarfBuzz features two kinds of data types: non-opaque, 13*2d1272b8SAndroid Build Coastguard Worker pass-by-value types and opaque, heap-allocated types. This kind 14*2d1272b8SAndroid Build Coastguard Worker of separation is common in C libraries that have to provide 15*2d1272b8SAndroid Build Coastguard Worker API/ABI compatibility (almost) indefinitely. 16*2d1272b8SAndroid Build Coastguard Worker </para> 17*2d1272b8SAndroid Build Coastguard Worker <para> 18*2d1272b8SAndroid Build Coastguard Worker <emphasis>Value types:</emphasis> The non-opaque, pass-by-value 19*2d1272b8SAndroid Build Coastguard Worker types include integer types, enums, and small structs. Exposing 20*2d1272b8SAndroid Build Coastguard Worker a struct in the public API makes it impossible to expand the 21*2d1272b8SAndroid Build Coastguard Worker struct in the future. As such, exposing structs is reserved for 22*2d1272b8SAndroid Build Coastguard Worker cases where it’s extremely inefficient to do otherwise. 23*2d1272b8SAndroid Build Coastguard Worker </para> 24*2d1272b8SAndroid Build Coastguard Worker <para> 25*2d1272b8SAndroid Build Coastguard Worker In HarfBuzz, several structs, like <literal>hb_glyph_info_t</literal> and 26*2d1272b8SAndroid Build Coastguard Worker <literal>hb_glyph_position_t</literal>, fall into that efficiency-sensitive 27*2d1272b8SAndroid Build Coastguard Worker category and are non-opaque. 28*2d1272b8SAndroid Build Coastguard Worker </para> 29*2d1272b8SAndroid Build Coastguard Worker <para> 30*2d1272b8SAndroid Build Coastguard Worker For all non-opaque structs where future extensibility may be 31*2d1272b8SAndroid Build Coastguard Worker necessary, reserved members are included to hold space for 32*2d1272b8SAndroid Build Coastguard Worker possible future members. As such, it’s important to provide 33*2d1272b8SAndroid Build Coastguard Worker <function>equal()</function>, and <function>hash()</function> 34*2d1272b8SAndroid Build Coastguard Worker methods for such structs, allowing users of the API do 35*2d1272b8SAndroid Build Coastguard Worker effectively deal with the type without having to 36*2d1272b8SAndroid Build Coastguard Worker adapt their code to future changes. 37*2d1272b8SAndroid Build Coastguard Worker </para> 38*2d1272b8SAndroid Build Coastguard Worker <para> 39*2d1272b8SAndroid Build Coastguard Worker Important value types provided by HarfBuzz include the structs 40*2d1272b8SAndroid Build Coastguard Worker for working with Unicode code points, glyphs, and tags for font 41*2d1272b8SAndroid Build Coastguard Worker tables and features, as well as the enums for many Unicode and 42*2d1272b8SAndroid Build Coastguard Worker OpenType properties. 43*2d1272b8SAndroid Build Coastguard Worker </para> 44*2d1272b8SAndroid Build Coastguard Worker </section> 45*2d1272b8SAndroid Build Coastguard Worker 46*2d1272b8SAndroid Build Coastguard Worker <section id="object-model-object-types"> 47*2d1272b8SAndroid Build Coastguard Worker <title>Objects in HarfBuzz</title> 48*2d1272b8SAndroid Build Coastguard Worker <para> 49*2d1272b8SAndroid Build Coastguard Worker <emphasis>Object types:</emphasis> Opaque struct types are used 50*2d1272b8SAndroid Build Coastguard Worker for what HarfBuzz loosely calls "objects." This doesn’t have 51*2d1272b8SAndroid Build Coastguard Worker much to do with the terminology from object-oriented programming 52*2d1272b8SAndroid Build Coastguard Worker (OOP), although some of the concepts are similar. 53*2d1272b8SAndroid Build Coastguard Worker </para> 54*2d1272b8SAndroid Build Coastguard Worker <para> 55*2d1272b8SAndroid Build Coastguard Worker In HarfBuzz, all object types provide certain 56*2d1272b8SAndroid Build Coastguard Worker lifecycle-management APIs. Objects are reference-counted, and 57*2d1272b8SAndroid Build Coastguard Worker constructed with various <function>create()</function> methods, referenced via 58*2d1272b8SAndroid Build Coastguard Worker <function>reference()</function> and dereferenced using 59*2d1272b8SAndroid Build Coastguard Worker <function>destroy()</function>. 60*2d1272b8SAndroid Build Coastguard Worker </para> 61*2d1272b8SAndroid Build Coastguard Worker <para> 62*2d1272b8SAndroid Build Coastguard Worker For example, 63*2d1272b8SAndroid Build Coastguard Worker the <literal>hb_buffer_t</literal> object has 64*2d1272b8SAndroid Build Coastguard Worker <function>hb_buffer_create()</function> as its constructor, 65*2d1272b8SAndroid Build Coastguard Worker <function>hb_buffer_reference()</function> to reference, and 66*2d1272b8SAndroid Build Coastguard Worker <function>hb_buffer_destroy()</function> to dereference. 67*2d1272b8SAndroid Build Coastguard Worker </para> 68*2d1272b8SAndroid Build Coastguard Worker <para> 69*2d1272b8SAndroid Build Coastguard Worker After construction, each object's properties are accessible only 70*2d1272b8SAndroid Build Coastguard Worker through the setter and getter functions described in the API 71*2d1272b8SAndroid Build Coastguard Worker Reference manual. 72*2d1272b8SAndroid Build Coastguard Worker </para> 73*2d1272b8SAndroid Build Coastguard Worker <para> 74*2d1272b8SAndroid Build Coastguard Worker Note that many object types can be marked as read-only or immutable, 75*2d1272b8SAndroid Build Coastguard Worker facilitating their use in multi-threaded environments. 76*2d1272b8SAndroid Build Coastguard Worker </para> 77*2d1272b8SAndroid Build Coastguard Worker <para> 78*2d1272b8SAndroid Build Coastguard Worker Key object types provided by HarfBuzz include: 79*2d1272b8SAndroid Build Coastguard Worker </para> 80*2d1272b8SAndroid Build Coastguard Worker <itemizedlist spacing="compact"> 81*2d1272b8SAndroid Build Coastguard Worker <listitem> 82*2d1272b8SAndroid Build Coastguard Worker <para> 83*2d1272b8SAndroid Build Coastguard Worker <emphasis>blobs</emphasis>, which act as low-level wrappers around binary 84*2d1272b8SAndroid Build Coastguard Worker data. Blobs are typically used to hold the contents of a 85*2d1272b8SAndroid Build Coastguard Worker binary font file. 86*2d1272b8SAndroid Build Coastguard Worker </para> 87*2d1272b8SAndroid Build Coastguard Worker </listitem> 88*2d1272b8SAndroid Build Coastguard Worker <listitem> 89*2d1272b8SAndroid Build Coastguard Worker <para> 90*2d1272b8SAndroid Build Coastguard Worker <emphasis>faces</emphasis>, which represent typefaces from a 91*2d1272b8SAndroid Build Coastguard Worker font file, but without specific parameters (such as size) set. 92*2d1272b8SAndroid Build Coastguard Worker </para> 93*2d1272b8SAndroid Build Coastguard Worker </listitem> 94*2d1272b8SAndroid Build Coastguard Worker <listitem> 95*2d1272b8SAndroid Build Coastguard Worker <para> 96*2d1272b8SAndroid Build Coastguard Worker <emphasis>fonts</emphasis>, which represent instances of a 97*2d1272b8SAndroid Build Coastguard Worker face with all of their parameters specified. 98*2d1272b8SAndroid Build Coastguard Worker </para> 99*2d1272b8SAndroid Build Coastguard Worker </listitem> 100*2d1272b8SAndroid Build Coastguard Worker <listitem> 101*2d1272b8SAndroid Build Coastguard Worker <para> 102*2d1272b8SAndroid Build Coastguard Worker <emphasis>buffers</emphasis>, which hold Unicode code points 103*2d1272b8SAndroid Build Coastguard Worker for characters (before shaping) and the shaped glyph output 104*2d1272b8SAndroid Build Coastguard Worker (after shaping). 105*2d1272b8SAndroid Build Coastguard Worker </para> 106*2d1272b8SAndroid Build Coastguard Worker </listitem> 107*2d1272b8SAndroid Build Coastguard Worker <listitem> 108*2d1272b8SAndroid Build Coastguard Worker <para> 109*2d1272b8SAndroid Build Coastguard Worker <emphasis>shape plans</emphasis>, which store the settings 110*2d1272b8SAndroid Build Coastguard Worker that HarfBuzz will use when shaping a particular text 111*2d1272b8SAndroid Build Coastguard Worker segment. Shape plans are not generally used by client 112*2d1272b8SAndroid Build Coastguard Worker programs directly, but as we will see in a later chapter, 113*2d1272b8SAndroid Build Coastguard Worker they are still valuable to understand. 114*2d1272b8SAndroid Build Coastguard Worker </para> 115*2d1272b8SAndroid Build Coastguard Worker </listitem> 116*2d1272b8SAndroid Build Coastguard Worker </itemizedlist> 117*2d1272b8SAndroid Build Coastguard Worker 118*2d1272b8SAndroid Build Coastguard Worker </section> 119*2d1272b8SAndroid Build Coastguard Worker 120*2d1272b8SAndroid Build Coastguard Worker 121*2d1272b8SAndroid Build Coastguard Worker 122*2d1272b8SAndroid Build Coastguard Worker <section id="object-model-lifecycle"> 123*2d1272b8SAndroid Build Coastguard Worker <title>Object lifecycle management</title> 124*2d1272b8SAndroid Build Coastguard Worker <para> 125*2d1272b8SAndroid Build Coastguard Worker Each object type in HarfBuzz provides a 126*2d1272b8SAndroid Build Coastguard Worker <function>create()</function> method. Some object types provide 127*2d1272b8SAndroid Build Coastguard Worker additional variants of <function>create()</function> to handle 128*2d1272b8SAndroid Build Coastguard Worker special cases or to speed up common tasks; those variants are 129*2d1272b8SAndroid Build Coastguard Worker documented in the API reference. For example, 130*2d1272b8SAndroid Build Coastguard Worker <function>hb_blob_create_from_file()</function> constructs a new 131*2d1272b8SAndroid Build Coastguard Worker blob directly from the contents of a file. 132*2d1272b8SAndroid Build Coastguard Worker </para> 133*2d1272b8SAndroid Build Coastguard Worker <para> 134*2d1272b8SAndroid Build Coastguard Worker All objects are created with an initial reference count of 135*2d1272b8SAndroid Build Coastguard Worker <literal>1</literal>. Client programs can increase the reference 136*2d1272b8SAndroid Build Coastguard Worker count on an object by calling its 137*2d1272b8SAndroid Build Coastguard Worker <function>reference()</function> method. Whenever a client 138*2d1272b8SAndroid Build Coastguard Worker program is finished with an object, it should call its 139*2d1272b8SAndroid Build Coastguard Worker corresponding <function>destroy()</function> method. The destroy 140*2d1272b8SAndroid Build Coastguard Worker method will decrease the reference count on the object and, 141*2d1272b8SAndroid Build Coastguard Worker whenever the reference count reaches zero, it will also destroy 142*2d1272b8SAndroid Build Coastguard Worker the object and free all of the associated memory. 143*2d1272b8SAndroid Build Coastguard Worker </para> 144*2d1272b8SAndroid Build Coastguard Worker <para> 145*2d1272b8SAndroid Build Coastguard Worker All of HarfBuzz's object-lifecycle-management APIs are 146*2d1272b8SAndroid Build Coastguard Worker thread-safe (unless you compiled HarfBuzz from source with the 147*2d1272b8SAndroid Build Coastguard Worker <literal>HB_NO_MT</literal> configuration flag), even when the 148*2d1272b8SAndroid Build Coastguard Worker object as a whole is not thread-safe. 149*2d1272b8SAndroid Build Coastguard Worker It is also permissible to <function>reference()</function> or to 150*2d1272b8SAndroid Build Coastguard Worker <function>destroy()</function> the <literal>NULL</literal> 151*2d1272b8SAndroid Build Coastguard Worker value. 152*2d1272b8SAndroid Build Coastguard Worker </para> 153*2d1272b8SAndroid Build Coastguard Worker <para> 154*2d1272b8SAndroid Build Coastguard Worker Some objects are thread-safe after they have been constructed 155*2d1272b8SAndroid Build Coastguard Worker and set up. The general pattern is to 156*2d1272b8SAndroid Build Coastguard Worker <function>create()</function> the object, make a few 157*2d1272b8SAndroid Build Coastguard Worker <function>set_*()</function> calls to set up the 158*2d1272b8SAndroid Build Coastguard Worker object, and then use it without further modification. 159*2d1272b8SAndroid Build Coastguard Worker </para> 160*2d1272b8SAndroid Build Coastguard Worker <para> 161*2d1272b8SAndroid Build Coastguard Worker To ensure that such an object is not modified, client programs 162*2d1272b8SAndroid Build Coastguard Worker can explicitly mark an object as immutable. HarfBuzz provides 163*2d1272b8SAndroid Build Coastguard Worker <function>make_immutable()</function> methods to mark an object 164*2d1272b8SAndroid Build Coastguard Worker as immutable and <function>is_immutable()</function> methods to 165*2d1272b8SAndroid Build Coastguard Worker test whether or not an object is immutable. Attempts to use 166*2d1272b8SAndroid Build Coastguard Worker setter functions on immutable objects will fail silently; see the API 167*2d1272b8SAndroid Build Coastguard Worker Reference manual for specifics. 168*2d1272b8SAndroid Build Coastguard Worker </para> 169*2d1272b8SAndroid Build Coastguard Worker <para> 170*2d1272b8SAndroid Build Coastguard Worker Note also that there are no "make mutable" methods. If client 171*2d1272b8SAndroid Build Coastguard Worker programs need to alter an object previously marked as immutable, 172*2d1272b8SAndroid Build Coastguard Worker they will need to make a duplicate of the original. 173*2d1272b8SAndroid Build Coastguard Worker </para> 174*2d1272b8SAndroid Build Coastguard Worker <para> 175*2d1272b8SAndroid Build Coastguard Worker Finally, object constructors (and, indeed, as much of the 176*2d1272b8SAndroid Build Coastguard Worker shaping API as possible) will never return 177*2d1272b8SAndroid Build Coastguard Worker <literal>NULL</literal>. Instead, if there is an allocation 178*2d1272b8SAndroid Build Coastguard Worker error, each constructor will return an “empty” object 179*2d1272b8SAndroid Build Coastguard Worker singleton. 180*2d1272b8SAndroid Build Coastguard Worker </para> 181*2d1272b8SAndroid Build Coastguard Worker <para> 182*2d1272b8SAndroid Build Coastguard Worker These empty-object singletons are inert and safe (although 183*2d1272b8SAndroid Build Coastguard Worker typically useless) to pass around. This design choice avoids 184*2d1272b8SAndroid Build Coastguard Worker having to check for <literal>NULL</literal> pointers all 185*2d1272b8SAndroid Build Coastguard Worker throughout the code. 186*2d1272b8SAndroid Build Coastguard Worker </para> 187*2d1272b8SAndroid Build Coastguard Worker <para> 188*2d1272b8SAndroid Build Coastguard Worker In addition, this “empty” object singleton can also be accessed 189*2d1272b8SAndroid Build Coastguard Worker using the <function>get_empty()</function> method of the object 190*2d1272b8SAndroid Build Coastguard Worker type in question. 191*2d1272b8SAndroid Build Coastguard Worker </para> 192*2d1272b8SAndroid Build Coastguard Worker </section> 193*2d1272b8SAndroid Build Coastguard Worker 194*2d1272b8SAndroid Build Coastguard Worker 195*2d1272b8SAndroid Build Coastguard Worker <section id="object-model-user-data"> 196*2d1272b8SAndroid Build Coastguard Worker <title>User data</title> 197*2d1272b8SAndroid Build Coastguard Worker <para> 198*2d1272b8SAndroid Build Coastguard Worker To better integrate with client programs, HarfBuzz's objects 199*2d1272b8SAndroid Build Coastguard Worker offer a "user data" mechanism that can be used to attach 200*2d1272b8SAndroid Build Coastguard Worker arbitrary data to the object. User-data attachment can be 201*2d1272b8SAndroid Build Coastguard Worker useful for tying the lifecycles of various pieces of data 202*2d1272b8SAndroid Build Coastguard Worker together, or for creating language bindings. 203*2d1272b8SAndroid Build Coastguard Worker </para> 204*2d1272b8SAndroid Build Coastguard Worker <para> 205*2d1272b8SAndroid Build Coastguard Worker Each object type has a <function>set_user_data()</function> 206*2d1272b8SAndroid Build Coastguard Worker method and a <function>get_user_data()</function> method. The 207*2d1272b8SAndroid Build Coastguard Worker <function>set_user_data()</function> methods take a client-provided 208*2d1272b8SAndroid Build Coastguard Worker <literal>key</literal> and a pointer, 209*2d1272b8SAndroid Build Coastguard Worker <literal>user_data</literal>, pointing to the data itself. Once 210*2d1272b8SAndroid Build Coastguard Worker the key-data pair has been attached to the object, the 211*2d1272b8SAndroid Build Coastguard Worker <function>get_user_data()</function> method can be called with 212*2d1272b8SAndroid Build Coastguard Worker the key, returning the <function>user_data</function> pointer. 213*2d1272b8SAndroid Build Coastguard Worker </para> 214*2d1272b8SAndroid Build Coastguard Worker <para> 215*2d1272b8SAndroid Build Coastguard Worker The <function>set_user_data()</function> methods also support an 216*2d1272b8SAndroid Build Coastguard Worker optional <function>destroy</function> callback. Client programs 217*2d1272b8SAndroid Build Coastguard Worker can set the <function>destroy</function> callback and receive 218*2d1272b8SAndroid Build Coastguard Worker notification from HarfBuzz whenever the object is destructed. 219*2d1272b8SAndroid Build Coastguard Worker </para> 220*2d1272b8SAndroid Build Coastguard Worker <para> 221*2d1272b8SAndroid Build Coastguard Worker Finally, each <function>set_user_data()</function> method allows 222*2d1272b8SAndroid Build Coastguard Worker the client program to set a <literal>replace</literal> Boolean 223*2d1272b8SAndroid Build Coastguard Worker indicating whether or not the function call should replace any 224*2d1272b8SAndroid Build Coastguard Worker existing <literal>user_data</literal> 225*2d1272b8SAndroid Build Coastguard Worker associated with the specified key. 226*2d1272b8SAndroid Build Coastguard Worker </para> 227*2d1272b8SAndroid Build Coastguard Worker </section> 228*2d1272b8SAndroid Build Coastguard Worker 229*2d1272b8SAndroid Build Coastguard Worker 230*2d1272b8SAndroid Build Coastguard Worker 231*2d1272b8SAndroid Build Coastguard Worker <section id="object-model-blobs"> 232*2d1272b8SAndroid Build Coastguard Worker <title>Blobs</title> 233*2d1272b8SAndroid Build Coastguard Worker <para> 234*2d1272b8SAndroid Build Coastguard Worker While most of HarfBuzz's object types are specific to the 235*2d1272b8SAndroid Build Coastguard Worker shaping process, <emphasis>blobs</emphasis> are somewhat 236*2d1272b8SAndroid Build Coastguard Worker different. 237*2d1272b8SAndroid Build Coastguard Worker </para> 238*2d1272b8SAndroid Build Coastguard Worker <para> 239*2d1272b8SAndroid Build Coastguard Worker Blobs are an abstraction designed to negotiate lifecycle and 240*2d1272b8SAndroid Build Coastguard Worker permissions for raw pieces of data. For example, when you load 241*2d1272b8SAndroid Build Coastguard Worker the raw font data into memory and want to pass it to HarfBuzz, 242*2d1272b8SAndroid Build Coastguard Worker you do so in a <literal>hb_blob_t</literal> wrapper. 243*2d1272b8SAndroid Build Coastguard Worker </para> 244*2d1272b8SAndroid Build Coastguard Worker <para> 245*2d1272b8SAndroid Build Coastguard Worker This allows you to take advantage of HarfBuzz's 246*2d1272b8SAndroid Build Coastguard Worker reference-counting and <function>destroy</function> 247*2d1272b8SAndroid Build Coastguard Worker callbacks. If you allocated the memory for the data using 248*2d1272b8SAndroid Build Coastguard Worker <function>malloc()</function>, you would create the blob using 249*2d1272b8SAndroid Build Coastguard Worker </para> 250*2d1272b8SAndroid Build Coastguard Worker <programlisting language="C"> 251*2d1272b8SAndroid Build Coastguard Worker hb_blob_create (data, length, HB_MEMORY_MODE_WRITABLE, data, free) 252*2d1272b8SAndroid Build Coastguard Worker </programlisting> 253*2d1272b8SAndroid Build Coastguard Worker <para> 254*2d1272b8SAndroid Build Coastguard Worker That way, HarfBuzz will call <function>free()</function> on the 255*2d1272b8SAndroid Build Coastguard Worker allocated memory whenever the blob drops its last reference and 256*2d1272b8SAndroid Build Coastguard Worker is deconstructed. Consequently, the user code can stop worrying 257*2d1272b8SAndroid Build Coastguard Worker about freeing memory and let the reference-counting machinery 258*2d1272b8SAndroid Build Coastguard Worker take care of that. 259*2d1272b8SAndroid Build Coastguard Worker </para> 260*2d1272b8SAndroid Build Coastguard Worker <para> 261*2d1272b8SAndroid Build Coastguard Worker Most of the time, blobs are read-only, facilitating their use in 262*2d1272b8SAndroid Build Coastguard Worker immutable objects. 263*2d1272b8SAndroid Build Coastguard Worker </para> 264*2d1272b8SAndroid Build Coastguard Worker </section> 265*2d1272b8SAndroid Build Coastguard Worker 266*2d1272b8SAndroid Build Coastguard Worker</chapter> 267