Debian Codegraph @

ft2docs/docs/design/design-3.html

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/html4/loose.dtd">
3
4 <html lang="en">
5
6 <head>
7 <meta http-equiv="Content-Type"
8 content="text/html; charset=utf-8">
9 <meta http-equiv="Content-Style-Type"
10 content="text/css">
11 <meta http-equiv="Content-Script-Type"
12 content="text/javascript">
13 <meta name="description"
14 content="FreeType Documentation - Design - Public Objects and
15 Classes">
16 <meta name="Author"
17 content="David Turner">
18
19 <link rel="icon"
20 href="../image/favicon_-90.ico">
21 <link rel="shortcut icon"
22 href="../image/favicon_-90.ico">
23 <link rel="stylesheet"
24 type="text/css"
25 href="../css/freetype2_-90.css">
26
27 <script type="text/javascript"
28 src="../../../js/freetype2.js">
29 </script>
30
31 <title>FreeType Design | Public Objects and Classes</title>
32 </head>
33
34
35 <body>
36
37 <div id="top"
38 class="bar">
39 <h1><a href="http://freetype.org/index.html">FreeType</a>
40 Design&nbsp;/&nbsp;II</h1>
41 </div>
42
43
44 <div id="wrapper">
45
46 <div class="colmask leftmenu">
47 <div class="colright">
48 <div class="col1wrap">
49 <div class="col1">
50
51
52 <!-- ************************************************** -->
53
54 <div id="public-objects">
55 <h2>II. Public Objects and Classes</h2>
56
57 <p>We will now explain the abstractions provided by
58 FreeType&nbsp;2 to client applications to manage font
59 files and data. As you would normally expect, these are
60 implemented through objects and classes.</p>
61
62
63 <h3 id="section-1">1. Object Orientation in
64 FreeType&nbsp;2</h3>
65
66 <p>Though written in ANSI&nbsp;C, the library employs a few
67 techniques, inherited from object-oriented programming, to
68 make it easy to extend. Hence, the following conventions
69 apply in the FreeType&nbsp;2 source code.</p>
70
71 <ol>
72 <li>
73 <p>Almost all object types or classes have a
74 corresponding <em>structure type</em> <b>and</b> a
75 corresponding <em>structure pointer type</em>. The
76 latter is called the <em>handle type</em> for the type
77 or class.</p>
78
79 <p>Consider that we need to manage objects of type
80 &lsquo;foo&rsquo; in FreeType&nbsp;2. We would define
81 the following structure and handle types as
82 follows.</p>
83
84 <pre>
85 typedef struct FT_FooRec_* FT_Foo;
86
87 typedef struct FT_FooRec_
88 {
89 /* fields for the 'foo' class */
90 ...
91
92 } FT_FooRec;</pre>
93
94 <p>As a convention, handle types use simple but
95 meaningful identifiers beginning with <tt>FT_</tt>, as
96 in <tt>FT_Foo</tt>, while structures use the same name
97 with a <tt>Rec</tt> suffix appended to it
98 (&lsquo;Rec&rsquo; is short for
99 &lsquo;record&rsquo;).</p>
100 </li>
101 <li>
102 <p>Class derivation is achieved internally by wrapping
103 base class structures into new ones. As an example,
104 we define a &lsquo;foobar&rsquo; class that is derived
105 from &lsquo;foo&rsquo;. We would do something
106 like this.</p>
107
108 <pre>
109 typedef struct FT_FooBarRec_* FT_FooBar;
110
111 typedef struct FT_FooBarRec_
112 {
113 /* the base 'foo' class fields */
114 FT_FooRec root;
115
116 /* fields proper to the 'foobar' class */
117 ...
118 } FT_FooBarRec;</pre>
119
120 <p>As you can see, we ensure that a &lsquo;foobar&rsquo;
121 object is also a &lsquo;foo&rsquo; object by placing
122 a <tt>FT_FooRec</tt> at the start of the
123 <tt>FT_FooBarRec</tt> definition. It is
124 called <b>root</b> by convention.</p>
125
126 <p>Note that an <tt>FT_FooBar</tt> handle also points to
127 a &lsquo;foo&rsquo; object and can be typecast
128 to <tt>FT_Foo</tt>. Similarly, when the library
129 returns an <tt>FT_Foo</tt> handle to client
130 applications, the object can be really implemented as
131 <tt>FT_FooBar</tt> or any derived class from
132 &lsquo;foo&rsquo;.</p>
133 </li>
134 </ol>
135
136 <p>In the following sections of this chapter, we will refer
137 to &lsquo;the <tt>FT_Foo</tt> class&rsquo; to indicate the
138 type of objects handled through <tt>FT_Foo</tt> pointers,
139 be they implemented as &lsquo;foo&rsquo; or
140 &lsquo;foobar&rsquo;.</p>
141
142
143 <h3 id="section-2">2. The <tt>FT_Library</tt> class</h3>
144
145 <p>This type corresponds to a handle to a single instance of
146 the library. Note that the corresponding
147 structure <tt>FT_LibraryRec</tt> is not defined in public
148 header files, making client applications unable to access
149 its internal fields.</p>
150
151 <p>The library object is the <em>parent</em> of all other
152 objects in FreeType&nbsp;2. You need to create a new
153 library instance before doing anything else with the
154 library. Similarly, destroying it will automatically
155 destroy all its children (i.e., faces and modules).</p>
156
157 <p>Typical client applications should
158 call <a href="../reference/ft2-library_setup.html#ft_init_freetype"><code>FT_Init_FreeType</code></a>
159 in order to create a new library object, ready to be used
160 for further actions.</p>
161
162 <p>Another alternative is to create a fresh new library
163 instance by calling the function
164 <a href="../reference/ft2-module_management.html#ft_new_library"><code>FT_New_Library</code></a>,
165 defined in the
166 <tt>ftmodule.h</tt> public header file. This function
167 will however return an &lsquo;empty&rsquo; library
168 instance with no module registered in it. You can
169 &lsquo;install&rsquo; modules in the instance by calling
170 <a href="../reference/ft2-module_management.html#ft_add_module"><code>FT_Add_Module</code></a>
171 manually.</p>
172
173 <p>Calling <tt>FT_Init_FreeType</tt> is a lot more
174 convenient, because this function basically registers a
175 set of default modules into each new library instance.
176 The way this list is accessed or computed is determined at
177 build time, and depends on the content of
178 the <tt>ftinit</tt> component. This process is explained
179 in details later in this document.</p>
180
181 <p>For now, one should consider that library objects are
182 created with <tt>FT_Init_FreeType</tt>, and destroyed
183 along with all children
184 with <a href="../reference/ft2-library_setup.html#ft_done_freetype"><code>FT_Done_FreeType</code></a>.</p>
185
186
187 <h3 id="section-3">3. The <tt>FT_Face</tt> class</h3>
188
189 <p>A face object corresponds to a single <em>font face</em>,
190 i.e., a specific typeface with a specific style. For
191 example, &lsquo;Arial&rsquo; and &lsquo;Arial
192 Italic&rsquo; correspond to two distinct faces.</p>
193
194 <p>A face object is normally created
195 through <a href="../reference/ft2-face_creation.html#ft_new_face"><code>FT_New_Face</code></a>.
196 This function takes the following parameters:
197 an <tt>FT_Library</tt> handle, a C file pathname used to
198 indicate which font file to open, an index used to decide
199 which face to load from the file (a single file may
200 contain several faces in certain cases), and the address
201 of an <tt>FT_Face</tt> handle. It returns an error
202 code.</p>
203
204 <pre>
205 FT_Error FT_New_Face( FT_Library library,
206 const char* filepathname,
207 FT_Long face_index,
208 FT_Face* face );</pre>
209
210 <p>In case of success, the function
211 returns <tt>FT_Err_Ok</tt> (which is value&nbsp;0), and
212 the handle pointed to by the <tt>face</tt> parameter is
213 set to a non-NULL value.</p>
214
215 <p>Note that the face object contains several fields used to
216 describe global font data that can be accessed directly by
217 client applications, for example, the total number of
218 glyphs in the face, the face's family name, style name,
219 the EM size for scalable formats, etc. For more details,
220 look at
221 the <a href="../reference/ft2-face_creation.html#ft_facerec"><code>FT_FaceRec</code></a>
222 definition in the FreeType&nbsp;2 API Reference.</p>
223
224
225 <h3 id="section-4">4. The <tt>FT_Size</tt> class</h3>
226
227 <p>Each <tt>FT_Face</tt> object has one or more
228 <tt>FT_Size</tt> objects. A <em>size object</em> stores
229 data specific to a given character width and height. Each
230 newly created face object has one size, which is directly
231 accessible as <tt>face-&gt;size</tt>.</p>
232
233 <p>The contents of a size object can be changed by calling
234 <a href="../reference/ft2-sizing_and_scaling.html#ft_request_size"><code>FT_Request_Size</code></a>, <a href="../reference/ft2-sizing_and_scaling.html#ft_set_pixel_sizes"><code>FT_Set_Pixel_Sizes</code></a>,
235 or <a href="../reference/ft2-sizing_and_scaling.html#ft_set_char_size"><code>FT_Set_Char_Size</code></a>.</p>
236
237 <p>A new size object can be created
238 with <a href="../reference/ft2-sizes_management.html#ft_new_size"><code>FT_New_Size</code></a>,
239 and destroyed manually
240 with <a href="../reference/ft2-sizes_management.html#ft_done_size"><code>FT_Done_Size</code></a>.
241 Note that typical applications don't need to do this
242 normally: usually it is fully sufficient to use the
243 default size object provided with
244 each <tt>FT_Face</tt>.</p>
245
246 <p>The public fields of <tt>FT_Size</tt> objects are defined
247 in a very small structure
248 named <a href="../reference/ft2-sizing_and_scaling.html#ft_sizerec"><code>FT_SizeRec</code></a>.
249 However, it is important to understand that some font
250 drivers define their own derivatives of <tt>FT_Size</tt>
251 to store important internal data that is re-computed each
252 time the character size changes. Most of the time, these
253 are size-specific <em>font hints</em>.</p>
254
255 <p>For example, the TrueType driver stores the scaled CVT
256 (Control Value Table) data that results from the execution
257 of the &lsquo;prep&rsquo; program in a <tt>TT_Size</tt>
258 structure, while the Type&nbsp;1 driver stores scaled
259 global metrics (like blue zones) in a <tt>T1_Size</tt>
260 object. Don't worry if you don't understand the current
261 paragraph; most of this stuff is highly font format
262 specific and doesn't need to be explained to client
263 developers&nbsp;:-)</p>
264
265
266 <h3 id="section-5">5. The <tt>FT_GlyphSlot</tt> class</h3>
267
268 <p>The purpose of a <em>glyph slot</em> is to provide a
269 place where glyph images can be loaded one by one easily,
270 independently of the glyph image format (bitmap, vector
271 outline, or anything else).</p>
272
273 <p>Ideally, once a glyph slot is created, any glyph image
274 can be loaded into it without additional memory
275 allocation. In practice, this is only possible with
276 certain formats like TrueType which explicitly provide
277 data to compute a slot's maximum size.</p>
278
279 <p>Another reason for glyph slots is that they are also used
280 to hold format-specific hints for a given glyphs as well
281 as all other data necessary to correctly load the
282 glyph.</p>
283
284 <p>The
285 base <a href="../reference/ft2-glyph_retrieval.html#ft_glyphslotrec"><code>FT_GlyphSlotRec</code></a>
286 structure only presents glyph metrics and images to client
287 applications, while the actual implementation may contain
288 more sophisticated data.</p>
289
290 <p>As an example, the
291 TrueType-specific <tt>TT_GlyphSlotRec</tt> structure
292 contains additional fields to hold glyph-specific
293 bytecode, transient outlines used during the hinting
294 process, and a few other things. The
295 Type&nbsp;1-specific <tt>T1_GlyphSlotRec</tt> structure
296 holds glyph hints during glyph loading, as well as
297 additional logic used to properly hint the glyphs when a
298 native Type&nbsp;1 hinter is used.</p>
299
300 <p>Each face object has a single glyph slot that is directly
301 accessible as <tt>face-&gt;glyph</tt>.</p>
302
303
304 <h3 id="section-6">6. The <tt>FT_CharMap</tt> class</h3>
305
306 <p>The <tt>FT_CharMap</tt> type is a handle to character map
307 objects, or <em>charmaps</em>. A charmap is simply some
308 sort of table or dictionary to translate character codes
309 in a given encoding into glyph indices for the font.</p>
310
311 <p>A single face may contain several charmaps. Each one of
312 them corresponds to a given character repertoire, like
313 Unicode, Apple Roman, Windows codepages, and other
314 encodings.</p>
315
316 <p>Each <tt>FT_CharMap</tt> object contains a
317 &lsquo;platform&rsquo; and an &lsquo;encoding&rsquo; field
318 to precisely identify the character repertoire
319 corresponding to it.</p>
320
321 <p>Each font format provides its own derivative of
322 <a href="../reference/ft2-character_mapping.html#ft_charmaprec"><code>FT_CharMapRec</code></a>
323 and thus needs to implement these objects.</p>
324
325
326 <h3 id="section-7">7. Objects Relationship</h3>
327
328 <p>The following diagram summarizes what we have just said
329 regarding the public objects managed by the library; it
330 also describes their relationship.</p>
331
332 <center>
333 <img src="objects-1.svg"
334 width="75%"
335 height="75%"
336 alt="Simple library model">
337 </center>
338
339 <p>Note that this picture will be updated at the end of the
340 next chapter, related to <em>internal objects</em>.</p>
341 </div>
342
343 <!-- ************************************************** -->
344
345 <div class="updated">
346 <p>Last update: 17-Oct-2022</p>
347 </div>
348 </div>
349 </div>
350
351
352 <!-- ************************************************** -->
353
354 <div class="col2">
355 </div>
356 </div>
357 </div>
358
359
360 <!-- ************************************************** -->
361
362 <div id="TOC">
363 <ul>
364 <li class="funding">
365 <form action="https://www.paypal.com/cgi-bin/webscr"
366 method="post"
367 target="_top">
368 <input type="hidden"
369 name="cmd"
370 value="_s-xclick">
371 <input type="hidden"
372 name="hosted_button_id"
373 value="SK827YKEALMT4">
374 <input type="image"
375 src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif"
376 name="submit"
377 alt="PayPal - The safer, easier way to pay online!">
378 </form>
379 </li>
380 <li class="primary">
381 <a href="http://freetype.org/index.html">Home</a>
382 </li>
383 <li class="primary">
384 <a href="http://freetype.org/index.html#news">News</a>
385 </li>
386 <li class="primary">
387 <a href="../index.html">Overview</a>
388 </li>
389 <li class="primary">
390 <a href="../documentation.html">Documentation</a>
391 </li>
392 <li class="primary">
393 <a href="http://freetype.org/developer.html">Development</a>
394 </li>
395 <li class="primary">
396 <a href="http://freetype.org/contact.html"
397 class="emphasis">Contact</a>
398 </li>
399
400 <li>
401 &nbsp; <!-- separate primary from secondary entries -->
402 </li>
403
404 <li class="secondary">
405 <a href="index.html">FreeType Design</a>
406 </li>
407 <li class="tertiary">
408 <a href="design-1.html">Introduction</a>
409 </li>
410 <li class="tertiary">
411 <a href="design-2.html">Components and APIs</a>
412 </li>
413 <li class="tertiary">
414 <a href="design-3.html" class="current">Public Objects and Classes</a>
415 </li>
416 <li class="tertiary">
417 <a href="design-4.html">Internal Objects and Classes</a>
418 </li>
419 <li class="tertiary">
420 <a href="design-5.html">Module Classes</a>
421 </li>
422 <li class="tertiary">
423 <a href="design-6.html">Interfaces and Services</a>
424 </li>
425 </ul>
426 </div>
427
428 </div> <!-- id="wrapper" -->
429
430 <div id="TOC-bottom">
431 </div>
432
433 </body>
434 </html>