| 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 - Internal Objects |
| 15 |
and 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 | Internal 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 / III</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="internal-objects"> |
| 55 |
<h2>III. Internal Objects and Classes</h2> |
| 56 |
|
| 57 |
<p>Let us have a look now at the <em>internal</em> objects |
| 58 |
that FreeType 2 uses, i.e., those not directly |
| 59 |
available to client applications, and see how they fit |
| 60 |
into the picture.</p> |
| 61 |
|
| 62 |
|
| 63 |
<h3 id="section-1">1. Memory Management</h3> |
| 64 |
|
| 65 |
<p>Most memory management operations are performed through |
| 66 |
three specific routines of the base |
| 67 |
layer: <tt>FT_Alloc</tt>, <tt>FT_Realloc</tt>, |
| 68 |
and <tt>FT_Free</tt>. Each one of these functions expects |
| 69 |
a <tt>FT_Memory</tt> handle as its first parameter. Note, |
| 70 |
however, that there exist more, similar variants for |
| 71 |
specific purposes which we skip here for simplicity.</p> |
| 72 |
|
| 73 |
<p><tt>FT_Memory</tt> is a pointer to a simple object that |
| 74 |
describes the current memory pool or manager. It contains |
| 75 |
a small table of alloc, realloc, and free functions. A |
| 76 |
memory manager is created at library initialization time |
| 77 |
by <tt>FT_Init_FreeType</tt>, calling the (internal) |
| 78 |
function <tt>FT_New_Memory</tt> provided by |
| 79 |
the <tt>ftsystem</tt> component.</p> |
| 80 |
|
| 81 |
<p>By default, this manager uses the ANSI functions <tt>malloc</tt>, |
| 82 |
<tt>realloc</tt>, and <tt>free</tt>. However, |
| 83 |
as <tt>ftsystem</tt> is a replaceable part of the base |
| 84 |
layer, a specific build of the library could provide a |
| 85 |
different default memory manager.</p> |
| 86 |
|
| 87 |
<p>Even with a default build, client applications are still |
| 88 |
able to provide their own memory manager by not calling |
| 89 |
<tt>FT_Init_FreeType</tt> but follow these simple |
| 90 |
steps.</p> |
| 91 |
|
| 92 |
<ol> |
| 93 |
<li> |
| 94 |
<p>Create a new <tt>FT_Memory</tt> object by hand. The |
| 95 |
definition |
| 96 |
of <a href="../reference/ft2-system_interface.html#ft_memoryrec"><code>FT_MemoryRec</code></a> |
| 97 |
is located in the public header |
| 98 |
file <tt>ftsystem.h</tt>.</p> |
| 99 |
</li> |
| 100 |
<li> |
| 101 |
<p>Call <a href="../reference/ft2-module_management.html#ft_new_library"><code>FT_New_Library</code></a> |
| 102 |
to create a new library instance using your custom |
| 103 |
memory manager. This new library doesn't yet contain |
| 104 |
any registered modules.</p> |
| 105 |
</li> |
| 106 |
<li> |
| 107 |
<p>Register the set of default modules by calling the |
| 108 |
function <a href="../reference/ft2-module_management.html#ft_add_default_modules"><code>FT_Add_Default_Modules</code></a> |
| 109 |
provided by the <tt>ftinit</tt> component, or manually |
| 110 |
register your drivers by repeatedly |
| 111 |
calling <a href="../reference/ft2-module_management.html#ft_add_module"><code>FT_Add_Module</code></a>.</p> |
| 112 |
</li> |
| 113 |
</ol> |
| 114 |
|
| 115 |
|
| 116 |
<h3 id="section-2">2. Input Streams</h3> |
| 117 |
|
| 118 |
<p>Font files are always read through <tt>FT_Stream</tt> |
| 119 |
objects. The definition |
| 120 |
of <a href="../reference/ft2-system_interface.html#ft_streamrec"><code>FT_StreamRec</code></a> |
| 121 |
is located in the public header file <tt>ftsystem.h</tt>, |
| 122 |
which allows client developers to provide their own |
| 123 |
implementation of streams if they wish so.</p> |
| 124 |
|
| 125 |
<p>The |
| 126 |
function <a href="../reference/ft2-face_creation.html#ft_new_face"><code>FT_New_Face</code></a> |
| 127 |
always automatically creates a new stream object from the |
| 128 |
C pathname given as its second argument. This is |
| 129 |
achieved by calling the (internal) function |
| 130 |
<tt>FT_Stream_Open</tt> provided by the <tt>ftsystem</tt> |
| 131 |
component. As the latter is replaceable, the |
| 132 |
implementation of streams may vary greatly between |
| 133 |
platforms.</p> |
| 134 |
|
| 135 |
<p>As an example, the default implementation of streams is |
| 136 |
located in the file <tt>src/base/ftsystem.c</tt> and uses |
| 137 |
the ANSI functions <tt>fopen</tt>, <tt>fseek</tt>, |
| 138 |
and <tt>fread</tt>. However, the Unix build of |
| 139 |
FreeType 2 provides an alternative implementation |
| 140 |
that uses memory-mapped files, when available on the host |
| 141 |
platform, resulting in a significant access speed-up.</p> |
| 142 |
|
| 143 |
<p>FreeType distinguishes between memory-based and |
| 144 |
disk-based streams. In the first case, all data is |
| 145 |
directly accessed in memory (e.g., ROM-based, write-only |
| 146 |
static data, and memory-mapped files), while in the |
| 147 |
second, portions of the font files are read in chunks |
| 148 |
called <em>frames</em>, and temporarily buffered similarly |
| 149 |
through typical seek and read operations.</p> |
| 150 |
|
| 151 |
<p>The FreeType stream sub-system also implements extremely |
| 152 |
efficient algorithms to very quickly load structures from |
| 153 |
font files while ensuring complete safety in the case of a |
| 154 |
‘broken file’.</p> |
| 155 |
|
| 156 |
<p>The |
| 157 |
function <a href="../reference/ft2-face_creation.html#ft_new_memory_face"><code>FT_New_Memory_Face</code></a> |
| 158 |
can be used to directly create and open |
| 159 |
an <tt>FT_Face</tt> object from data that is readily |
| 160 |
available in memory (including ROM-based fonts).</p> |
| 161 |
|
| 162 |
<p>Finally, in the case where a custom input stream is |
| 163 |
needed, client applications can use the |
| 164 |
function <a href="../reference/ft2-face_creation.html#ft_open_face"><code>FT_Open_Face</code></a>, |
| 165 |
which can accept custom input streams. This may be useful |
| 166 |
in the case of compressed or remote font files, or even |
| 167 |
embedded font files that need to be extracted from certain |
| 168 |
documents.</p> |
| 169 |
|
| 170 |
<p>Note that each face owns a single stream, which is also |
| 171 |
destroyed |
| 172 |
by <a href="../reference/ft2-face_creation.html#ft_done_face"><code>FT_Done_Face</code></a>.</p> |
| 173 |
|
| 174 |
|
| 175 |
<h3 id="section-3">3. Modules</h3> |
| 176 |
|
| 177 |
<p>A FreeType 2 module is itself a piece of code. |
| 178 |
However, the library creates a single <tt>FT_Module</tt> |
| 179 |
object for each module that is registered |
| 180 |
when <tt>FT_Add_Module</tt> is called.</p> |
| 181 |
|
| 182 |
<p>The definition of <tt>FT_ModuleRec</tt> is not publicly |
| 183 |
available to client applications. However, |
| 184 |
each <em>module type</em> is described by a simple public |
| 185 |
structure |
| 186 |
named <a href="../reference/ft2-module_management.html#ft_module_class"><code>FT_Module_Class</code></a>, |
| 187 |
defined in header file |
| 188 |
<tt>ftmodule.h</tt>, and is described later in this |
| 189 |
document.</p> |
| 190 |
|
| 191 |
<p>You need a pointer to an <tt>FT_Module_Class</tt> |
| 192 |
structure when |
| 193 |
calling <a href="../reference/ft2-module_management.html#ft_add_module"><code>FT_Add_Module</code></a>.</p> |
| 194 |
|
| 195 |
<pre> |
| 196 |
FT_Error |
| 197 |
FT_Add_Module( FT_Library library, |
| 198 |
const FT_Module_Class* clazz );</pre> |
| 199 |
|
| 200 |
<p>This function does the following tasks.</p> |
| 201 |
|
| 202 |
<ul> |
| 203 |
<li> |
| 204 |
<p>Check whether the library already holds a module |
| 205 |
object corresponding to the same module name as the |
| 206 |
one found in <tt>FT_Module_Class</tt>.</p> |
| 207 |
</li> |
| 208 |
<li> |
| 209 |
<p>If this is the case, compare the module version |
| 210 |
number to see whether it is possible |
| 211 |
to <em>upgrade</em> the module to a new version. If |
| 212 |
the module class's version number is smaller than the |
| 213 |
already installed one, return immediately. Similarly, |
| 214 |
check that the version of FreeType 2 that is |
| 215 |
running is correct compared to the one required by the |
| 216 |
module.</p> |
| 217 |
</li> |
| 218 |
<li> |
| 219 |
<p>Create a new <tt>FT_Module</tt> object, using data |
| 220 |
and flags of the module class to determine its byte |
| 221 |
size and how to properly initialize it.</p> |
| 222 |
</li> |
| 223 |
<li> |
| 224 |
<p>If a module initializer is present in the module |
| 225 |
class, call it to complete the module object's |
| 226 |
initialization.</p> |
| 227 |
</li> |
| 228 |
<li> |
| 229 |
<p>Add the new module to the library's list of |
| 230 |
‘registered’ modules. In case of an |
| 231 |
upgrade, destroy the previous module object.</p> |
| 232 |
</li> |
| 233 |
</ul> |
| 234 |
|
| 235 |
<p>Note that this function doesn't return |
| 236 |
an <tt>FT_Module</tt> handle, given that module objects |
| 237 |
are completely internal to the library (and client |
| 238 |
applications shouldn't normally mess with |
| 239 |
them :-)</p> |
| 240 |
|
| 241 |
<p>Finally, it is important to understand that |
| 242 |
FreeType 2 recognizes and manages several kinds of |
| 243 |
modules. These will be explained later on in this |
| 244 |
document.</p> |
| 245 |
|
| 246 |
<ul> |
| 247 |
<li> |
| 248 |
<p><em>Renderer</em> modules are used to convert native |
| 249 |
glyph images to bitmaps or pixmaps. FreeType 2 |
| 250 |
comes with two renderer modules by default: one to |
| 251 |
generate monochrome bitmaps, the other to generate |
| 252 |
anti-aliased pixmaps.</p> |
| 253 |
</li> |
| 254 |
<li> |
| 255 |
<p><em>Font driver</em> modules are used to support one |
| 256 |
or more font formats. Typically, each font driver |
| 257 |
provides a specific implementation or derivative |
| 258 |
of <tt>FT_Face</tt>, <tt>FT_Size</tt>, |
| 259 |
<tt>FT_GlyphSlot</tt>, as well |
| 260 |
as <tt>FT_CharMap</tt>.</p> |
| 261 |
</li> |
| 262 |
<li> |
| 263 |
<p><em>Helper</em> modules are shared by several font |
| 264 |
drivers. For example, the <tt>sfnt</tt> module parses |
| 265 |
and manages tables found in SFNT-based font formats; |
| 266 |
it is then used by both the TrueType font and CFF |
| 267 |
drivers.</p> |
| 268 |
</li> |
| 269 |
<li> |
| 270 |
<p>Finally, the <em>auto-hinter</em> module has a |
| 271 |
specific place in the library's design, as its role is |
| 272 |
to process vectorial glyph outlines, independently of |
| 273 |
their native font format, to produce optimal results |
| 274 |
at small pixel sizes.</p> |
| 275 |
</li> |
| 276 |
</ul> |
| 277 |
|
| 278 |
<p>Note that every <tt>FT_Face</tt> object is <em>owned</em> |
| 279 |
by the corresponding font driver, depending on the |
| 280 |
original font file's format. This means that all face |
| 281 |
objects are destroyed when a module is removed or |
| 282 |
unregistered from a library instance (typically by calling |
| 283 |
the <a href="../reference/ft2-module_management.html#ft_remove_module"><code>FT_Remove_Module</code></a> |
| 284 |
function). Because of this, you should always take care |
| 285 |
that no <tt>FT_Face</tt> object is opened when you upgrade |
| 286 |
or remove a module from a library, as this could cause |
| 287 |
unexpected object deletion!</p> |
| 288 |
|
| 289 |
|
| 290 |
<h3 id="section-4">4. Summary</h3> |
| 291 |
|
| 292 |
<p>Finally, the following picture illustrates what has been |
| 293 |
said in this section, as well as the previous, by |
| 294 |
presenting the complete object graph of FreeType 2's |
| 295 |
base design.</p> |
| 296 |
|
| 297 |
<center> |
| 298 |
<img src="objects-2.svg" |
| 299 |
width="70%" |
| 300 |
height="70%" |
| 301 |
alt="Complete library model"> |
| 302 |
</center> |
| 303 |
</div> |
| 304 |
|
| 305 |
<!-- ************************************************** --> |
| 306 |
|
| 307 |
<div class="updated"> |
| 308 |
<p>Last update: 17-Oct-2022</p> |
| 309 |
</div> |
| 310 |
</div> |
| 311 |
</div> |
| 312 |
|
| 313 |
|
| 314 |
<!-- ************************************************** --> |
| 315 |
|
| 316 |
<div class="col2"> |
| 317 |
</div> |
| 318 |
</div> |
| 319 |
</div> |
| 320 |
|
| 321 |
|
| 322 |
<!-- ************************************************** --> |
| 323 |
|
| 324 |
<div id="TOC"> |
| 325 |
<ul> |
| 326 |
<li class="funding"> |
| 327 |
<form action="https://www.paypal.com/cgi-bin/webscr" |
| 328 |
method="post" |
| 329 |
target="_top"> |
| 330 |
<input type="hidden" |
| 331 |
name="cmd" |
| 332 |
value="_s-xclick"> |
| 333 |
<input type="hidden" |
| 334 |
name="hosted_button_id" |
| 335 |
value="SK827YKEALMT4"> |
| •336 |
<input type="submit" |
| 337 |
value="Donate" |
| 338 |
name="submit" |
| 339 |
alt="PayPal - The safer, easier way to pay online!"> |
| 340 |
</form> |
| 341 |
</li> |
| 342 |
<li class="primary"> |
| 343 |
<a href="http://freetype.org/index.html">Home</a> |
| 344 |
</li> |
| 345 |
<li class="primary"> |
| 346 |
<a href="http://freetype.org/index.html#news">News</a> |
| 347 |
</li> |
| 348 |
<li class="primary"> |
| 349 |
<a href="../index.html">Overview</a> |
| 350 |
</li> |
| 351 |
<li class="primary"> |
| 352 |
<a href="../documentation.html">Documentation</a> |
| 353 |
</li> |
| 354 |
<li class="primary"> |
| 355 |
<a href="http://freetype.org/developer.html">Development</a> |
| 356 |
</li> |
| 357 |
<li class="primary"> |
| 358 |
<a href="http://freetype.org/contact.html" |
| 359 |
class="emphasis">Contact</a> |
| 360 |
</li> |
| 361 |
|
| 362 |
<li> |
| 363 |
<!-- separate primary from secondary entries --> |
| 364 |
</li> |
| 365 |
|
| 366 |
<li class="secondary"> |
| 367 |
<a href="index.html">FreeType Design</a> |
| 368 |
</li> |
| 369 |
<li class="tertiary"> |
| 370 |
<a href="design-1.html">Introduction</a> |
| 371 |
</li> |
| 372 |
<li class="tertiary"> |
| 373 |
<a href="design-2.html">Components and APIs</a> |
| 374 |
</li> |
| 375 |
<li class="tertiary"> |
| 376 |
<a href="design-3.html">Public Objects and Classes</a> |
| 377 |
</li> |
| 378 |
<li class="tertiary"> |
| 379 |
<a href="design-4.html" class="current">Internal Objects and Classes</a> |
| 380 |
</li> |
| 381 |
<li class="tertiary"> |
| 382 |
<a href="design-5.html">Module Classes</a> |
| 383 |
</li> |
| 384 |
<li class="tertiary"> |
| 385 |
<a href="design-6.html">Interfaces and Services</a> |
| 386 |
</li> |
| 387 |
</ul> |
| 388 |
</div> |
| 389 |
|
| 390 |
</div> <!-- id="wrapper" --> |
| 391 |
|
| 392 |
<div id="TOC-bottom"> |
| 393 |
</div> |
| 394 |
|
| 395 |
</body> |
| 396 |
</html> |