| 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 - Interfaces and |
| 15 |
Services"> |
| 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 | Interfaces and Services</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 / V</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="module-classes"> |
| 55 |
<h2>V. Interfaces and Services</h2> |
| 56 |
|
| 57 |
<p>We shall now go into detail about interfaces and services |
| 58 |
in FreeType.</p> |
| 59 |
|
| 60 |
<p><em>Interfaces</em> in FreeType are analogous to those |
| 61 |
found in object-oriented programming. They can be thought |
| 62 |
of as internal public APIs, and are essentially tables of |
| 63 |
function pointers.</p> |
| 64 |
|
| 65 |
<p>Interfaces only describe the form and functionality, but |
| 66 |
the actual function body may be implemented elsewhere. |
| 67 |
The module that is implementing the interface will then |
| 68 |
pass the required function pointers to the table. This |
| 69 |
gives modularity and easy extendability.</p> |
| 70 |
|
| 71 |
<p>There are two main kinds of interfaces: <em>module</em> |
| 72 |
interfaces, and <em>services</em>.</p> |
| 73 |
|
| 74 |
<p>Module interfaces are defined for each module. For |
| 75 |
example, every font driver provides its own set of |
| 76 |
procedures for use in the base layer, which are registered |
| 77 |
in an <tt>FT_Driver</tt>. This way, very different font |
| 78 |
drivers can be used in the same way in the base layer.</p> |
| 79 |
|
| 80 |
<p>Services are cross-module interfaces. These provide |
| 81 |
functionality needed in several font drivers. </p> |
| 82 |
|
| 83 |
<p>Services are created when code from one module needs to |
| 84 |
be used in another. Rather than include files from |
| 85 |
another module, a service is created instead. Now, the |
| 86 |
other module just needs to include the header defining the |
| 87 |
interface.</p> |
| 88 |
|
| 89 |
<p>Helper modules are an extreme example of this; all their |
| 90 |
public functionality is made for use in other font drivers |
| 91 |
and hence are in a single service.</p> |
| 92 |
|
| 93 |
|
| 94 |
<h3>In-depth guide: Creating a service</h3> |
| 95 |
|
| 96 |
<p>This section will teach you how to write your own |
| 97 |
service.</p> |
| 98 |
|
| 99 |
<ol> |
| 100 |
<li> |
| 101 |
<p>Make the service interface header.</p> |
| 102 |
|
| 103 |
<p>We will be calling our service demo for demonstration |
| 104 |
purposes. First, create the header file, which goes |
| 105 |
in <tt>include/freetype/internal/services</tt>, and |
| 106 |
add the boilerplate.</p> |
| 107 |
|
| 108 |
<pre> |
| 109 |
#include FT_INTERNAL_SERVICE_H |
| 110 |
FT_BEGIN_HEADER |
| 111 |
#define FT_SERVICE_ID_DEMO "demo" |
| 112 |
|
| 113 |
/* ... */ |
| 114 |
|
| 115 |
FT_END_HEADER</pre> |
| 116 |
|
| 117 |
<p>This line in particular is required to register the |
| 118 |
new service later on.</p> |
| 119 |
|
| 120 |
<pre> |
| 121 |
#define <em>service-id service-tag</em></pre> |
| 122 |
|
| 123 |
</li> |
| 124 |
<li> |
| 125 |
<p>We will have identified some functions that are |
| 126 |
needed in another module. Extract the function |
| 127 |
signatures of these and place them in the header.</p> |
| 128 |
|
| 129 |
<pre> |
| 130 |
[typedef <em>return-type</em> |
| 131 |
(*<em>type-name</em>)(<em>function-signature</em>);]+</pre> |
| 132 |
|
| 133 |
<p>Example:</p> |
| 134 |
|
| 135 |
<pre> |
| 136 |
typedef FT_Error |
| 137 |
(*SampleDoSomethingFunc)( int foo ); |
| 138 |
typedef void |
| 139 |
(*SampleDoAnotherFunc)( int foo, |
| 140 |
float bar );</pre> |
| 141 |
|
| 142 |
</li> |
| 143 |
<li> |
| 144 |
<p>Define the service interface.</p> |
| 145 |
|
| 146 |
<p>Use the <tt>FT_DEFINE_SERVICE</tt> macro to do |
| 147 |
this.</p> |
| 148 |
|
| 149 |
<pre> |
| 150 |
FT_DEFINE_SERVICE( <em>service-name</em> ) |
| 151 |
{ |
| 152 |
[<em>type-name interface-entry</em>;]+ |
| 153 |
}</pre> |
| 154 |
|
| 155 |
<p>Example:</p> |
| 156 |
|
| 157 |
<pre> |
| 158 |
FT_DEFINE_SERVICE( Demo ) |
| 159 |
{ |
| 160 |
SampleDoSomethingFunc doSomething; |
| 161 |
SampleDoAnotherFunc doAnother; |
| 162 |
};</pre> |
| 163 |
|
| 164 |
<p>Here is the definition of the above macro |
| 165 |
(in <tt>ftserv.h</tt>).</p> |
| 166 |
|
| 167 |
<pre> |
| 168 |
#define FT_DEFINE_SERVICE( name ) \ |
| 169 |
typedef struct FT_Service_ ## name ## Rec_ \ |
| 170 |
FT_Service_ ## name ## Rec ; \ |
| 171 |
typedef struct FT_Service_ ## name ## Rec_ \ |
| 172 |
const * FT_Service_ ## name ; \ |
| 173 |
struct FT_Service_ ## name ## Rec_</pre> |
| 174 |
|
| 175 |
<p>This defines a new struct |
| 176 |
called <tt>FT_Service_DemoRec</tt>, along with a |
| 177 |
handle type for the |
| 178 |
struct, <tt>FT_Service_Demo</tt>.</p> |
| 179 |
</li> |
| 180 |
<li> |
| 181 |
<p>Register (and/or implement) the functions for the |
| 182 |
newly created interface.</p> |
| 183 |
|
| 184 |
<p>In the module implementing the interface, add a |
| 185 |
definition like the following</p> |
| 186 |
|
| 187 |
<pre> |
| 188 |
static const <em>service-record table-name</em> = |
| 189 |
{ |
| 190 |
<em>function-name</em> |
| 191 |
[,<em>function-name2</em> [,...]] |
| 192 |
}</pre> |
| 193 |
|
| 194 |
<p>which corresponds to the interface defined in |
| 195 |
step 3. Example:</p> |
| 196 |
|
| 197 |
<pre> |
| 198 |
static const FT_Service_DemoRec demo_service_rec = |
| 199 |
{ |
| 200 |
function1, |
| 201 |
function2 |
| 202 |
};</pre> |
| 203 |
|
| 204 |
<p>This initializes the function pointers table. In |
| 205 |
this example, <tt>function1</tt> has the function |
| 206 |
signature of <tt>SampleDoSomethingFunc</tt> and |
| 207 |
implements the <tt>doSomething</tt> functionality, and |
| 208 |
so on.</p> |
| 209 |
</li> |
| 210 |
<li> |
| 211 |
<p>Register the new service.</p> |
| 212 |
|
| 213 |
<p>Next, add this code to define the service list in |
| 214 |
this module, or append the new service to an existing |
| 215 |
list.</p> |
| 216 |
|
| 217 |
<pre> |
| 218 |
static const FT_ServiceDescRec <em>service-list-name</em>[] = |
| 219 |
{ |
| 220 |
{ <em>service-id</em>, &<em>table-name</em> }, |
| 221 |
[{ <em>service-id2</em>, &<em>table-name2</em> }, [...]] |
| 222 |
{ NULL, NULL } |
| 223 |
}</pre> |
| 224 |
|
| 225 |
<p>Example:</p> |
| 226 |
|
| 227 |
<pre> |
| 228 |
static const FT_ServiceDescRec demo_services[] = |
| 229 |
{ |
| 230 |
{ FT_SERVICE_ID_DEMO, &demo_service_rec }, |
| 231 |
{ NULL, NULL } |
| 232 |
};</pre> |
| 233 |
|
| 234 |
<p><em>service-id</em> is what we <tt>#define</tt>'d in |
| 235 |
the service header file in step 1. Note that |
| 236 |
the <tt>{NULL, NULL}</tt> sentinel value at the |
| 237 |
end is required.</p> |
| 238 |
|
| 239 |
<p>Now we need a way for other modules to find this |
| 240 |
service. First, we need to implement |
| 241 |
the <tt>get_interface</tt> function in |
| 242 |
<tt>FT_Module_Class</tt>. Here is a minimal example |
| 243 |
that does not do any validation.</p> |
| 244 |
|
| 245 |
<pre> |
| 246 |
FT_CALLBACK_DEF( FT_Module_Interface ) |
| 247 |
<em>get-interface-name</em>( FT_Module module, |
| 248 |
const char* module_interface ) |
| 249 |
{ |
| 250 |
return ft_service_list_lookup( <em>service-list-name</em>, |
| 251 |
module_interface ); |
| 252 |
}</pre> |
| 253 |
|
| 254 |
<p>Then, pass it into the <tt>FT_DEFINE_MODULE</tt> |
| 255 |
macro for this module.</p> |
| 256 |
|
| 257 |
<pre> |
| 258 |
(FT_Module_Requester) <em>get-interface-name</em></pre> |
| 259 |
|
| 260 |
<p>The last step is optional but recommended, which is |
| 261 |
to register the new service header |
| 262 |
in <tt>ftserv.h</tt>. |
| 263 |
|
| 264 |
<pre> |
| 265 |
#define FT_SERVICE_DEMO_H <freetype/internal/services/svdemo.h></pre> |
| 266 |
|
| 267 |
</li> |
| 268 |
<li> |
| 269 |
<p>Use the new service.</p> |
| 270 |
|
| 271 |
<p>Now, in the file that wants to use the service, add |
| 272 |
the following code to get the service.</p> |
| 273 |
|
| 274 |
<pre> |
| 275 |
<em>service-record-handle</em> service; |
| 276 |
|
| 277 |
|
| 278 |
FT_FACE_FIND_GLOBAL_SERVICE( face, |
| 279 |
service, |
| 280 |
<em>service-id-tail</em> );</pre> |
| 281 |
|
| 282 |
<p><tt>face</tt> should be of type <tt>FT_Face</tt>, |
| 283 |
which is usually the current face instance being used |
| 284 |
in the driver, and FreeType tries to find the service |
| 285 |
in the driver of this face first, before searching all |
| 286 |
other modules.</p> |
| 287 |
|
| 288 |
<p><em>service-id-tail</em> is the part |
| 289 |
of <em>service-id</em> |
| 290 |
following <tt>FT_SERVICE_ID_</tt>. |
| 291 |
|
| 292 |
<p>Now to call some function in the service.</p> |
| 293 |
|
| 294 |
<pre> |
| 295 |
service-><em>interface-entry</em>( <em>params</em> );</pre> |
| 296 |
|
| 297 |
<p>Example:</p> |
| 298 |
|
| 299 |
<pre> |
| 300 |
FT_Service_Demo demo; |
| 301 |
FT_Error error; |
| 302 |
|
| 303 |
|
| 304 |
FT_FACE_FIND_GLOBAL_SERVICE( face, demo, DEMO ); |
| 305 |
|
| 306 |
error = demo->doSomething(0); |
| 307 |
</pre> |
| 308 |
|
| 309 |
</li> |
| 310 |
</ol> |
| 311 |
</div> |
| 312 |
|
| 313 |
<!-- ************************************************** --> |
| 314 |
|
| 315 |
<div class="updated"> |
| 316 |
<p>Last update: 13-Feb-2018</p> |
| 317 |
</div> |
| 318 |
</div> |
| 319 |
</div> |
| 320 |
|
| 321 |
|
| 322 |
<!-- ************************************************** --> |
| 323 |
|
| 324 |
<div class="col2"> |
| 325 |
</div> |
| 326 |
</div> |
| 327 |
</div> |
| 328 |
|
| 329 |
|
| 330 |
<!-- ************************************************** --> |
| 331 |
|
| 332 |
<div id="TOC"> |
| 333 |
<ul> |
| 334 |
<li class="funding"> |
| 335 |
<form action="https://www.paypal.com/cgi-bin/webscr" |
| 336 |
method="post" |
| 337 |
target="_top"> |
| 338 |
<input type="hidden" |
| 339 |
name="cmd" |
| 340 |
value="_s-xclick"> |
| 341 |
<input type="hidden" |
| 342 |
name="hosted_button_id" |
| 343 |
value="SK827YKEALMT4"> |
| 344 |
<input type="image" |
| 345 |
src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" |
| 346 |
name="submit" |
| 347 |
alt="PayPal - The safer, easier way to pay online!"> |
| 348 |
</form> |
| 349 |
</li> |
| 350 |
<li class="primary"> |
| 351 |
<a href="http://freetype.org/index.html">Home</a> |
| 352 |
</li> |
| 353 |
<li class="primary"> |
| 354 |
<a href="http://freetype.org/index.html#news">News</a> |
| 355 |
</li> |
| 356 |
<li class="primary"> |
| 357 |
<a href="../index.html">Overview</a> |
| 358 |
</li> |
| 359 |
<li class="primary"> |
| 360 |
<a href="../documentation.html">Documentation</a> |
| 361 |
</li> |
| 362 |
<li class="primary"> |
| 363 |
<a href="http://freetype.org/developer.html">Development</a> |
| 364 |
</li> |
| 365 |
<li class="primary"> |
| 366 |
<a href="http://freetype.org/contact.html" |
| 367 |
class="emphasis">Contact</a> |
| 368 |
</li> |
| 369 |
|
| 370 |
<li> |
| 371 |
<!-- separate primary from secondary entries --> |
| 372 |
</li> |
| 373 |
|
| 374 |
<li class="secondary"> |
| 375 |
<a href="index.html">FreeType Design</a> |
| 376 |
</li> |
| 377 |
<li class="tertiary"> |
| 378 |
<a href="design-1.html">Introduction</a> |
| 379 |
</li> |
| 380 |
<li class="tertiary"> |
| 381 |
<a href="design-2.html">Components and APIs</a> |
| 382 |
</li> |
| 383 |
<li class="tertiary"> |
| 384 |
<a href="design-3.html">Public Objects and Classes</a> |
| 385 |
</li> |
| 386 |
<li class="tertiary"> |
| 387 |
<a href="design-4.html">Internal Objects and Classes</a> |
| 388 |
</li> |
| 389 |
<li class="tertiary"> |
| 390 |
<a href="design-5.html">Module Classes</a> |
| 391 |
</li> |
| 392 |
<li class="tertiary"> |
| 393 |
<a href="design-6.html" class="current">Interfaces and |
| 394 |
Services</a> |
| 395 |
</li> |
| 396 |
</ul> |
| 397 |
</div> |
| 398 |
|
| 399 |
</div> <!-- id="wrapper" --> |
| 400 |
|
| 401 |
<div id="TOC-bottom"> |
| 402 |
</div> |
| 403 |
|
| 404 |
</body> |
| 405 |
</html> |