Debian Codegraph @

ft2docs/docs/tutorial/step1.html

raw symbols ◀ ▶ SCIP
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">
15 <meta name="Author"
16 content="David Turner">
17
18 <link rel="icon"
19 href="../image/favicon_-90.ico">
20 <link rel="shortcut icon"
21 href="../image/favicon_-90.ico">
22 <link rel="stylesheet"
23 type="text/css"
24 href="../css/freetype2_-90.css">
25
26 <script type="text/javascript"
27 src="../../../js/freetype2.js">
28 </script>
29
30 <title>FreeType Tutorial / I</title>
31 </head>
32
33
34 <body>
35
36 <div id="top"
37 class="bar">
38 <h1><a href="http://freetype.org/index.html">FreeType</a>
39 Tutorial&nbsp;/&nbsp;I</h1>
40 </div>
41
42
43 <div id="wrapper">
44
45 <div class="colmask leftmenu">
46 <div class="colright">
47 <div class="col1wrap">
48 <div class="col1">
49
50
51 <!-- ************************************************** -->
52
53 <div id="simple-glyph-loading">
54 <h2>I. Simple Glyph Loading</h2>
55
56 <h3 id="section-1">1. Header Files</h3>
57
58 <p>The following are instructions required to compile an
59 application that uses the FreeType&nbsp;2 library.</p>
60
61 <ol>
62 <li class="emph">
63 <p>Locate the FreeType&nbsp;2 <code>include</code>
64 directory.</p>
65
66 <p>You have to add it to your compilation include
67 path.</p>
68
69 <p>In Unix-like environments you can use the
70 <code>pkg-config</code> program to retrieve the
71 appropriate compilation flags; say.</p>
72
73 <pre>
74 pkg-config --cflags freetype2</pre>
75
76 <p>to get the compilation flags.</p>
77
78 <p>This program can also be used to check the version of
79 the library that is installed on your system, as well
80 as the required librarian and linker flags.</p>
81
82 <p>Another solution is the <code>freetype-config</code>
83 script. However, its use is deprecated since it can't
84 be used reliably for cross compilation.</p>
85
86 </li>
87
88 <li class="emph">
89 <p>Include the file named <code>ft2build.h</code>.</p>
90
91 <p>It contains various macro declarations that are later
92 used to <code>#include</code> the appropriate public
93 FreeType&nbsp;2 header files.</p>
94 </li>
95
96 <li class="emph">
97 <p>Include the main FreeType&nbsp;2 API header file.</p>
98
99 <p>You should do that using the
100 macro <code>FT_FREETYPE_H</code>, like in the
101 following example.</p>
102
103 <pre>
104 #include &lt;ft2build.h&gt;
105 #include FT_FREETYPE_H</pre>
106
107 <p><code>FT_FREETYPE_H</code> is a special macro defined
108 in file <code>ftheader.h</code>. It contains some
109 installation-specific macros to name other public
110 header files of the FreeType&nbsp;2 API.</p>
111
112 <p>You can
113 read <a href="../reference/ft2-header_file_macros.html">this
114 section of the FreeType&nbsp;2 API Reference</a> for a
115 complete listing of the header macros.</p>
116 </li>
117 </ol>
118
119 <p>The use of macros in <code>#include</code> statements is
120 ANSI-compliant. It is used for several reasons.</p>
121
122 <ul>
123 <li>It avoids conflicts with (deprecated) FreeType 1.x
124 public header files.</li>
125
126 <li>The macro names are not limited to the DOS 8.3 file
127 naming limit; names
128 like <code>FT_MULTIPLE_MASTERS_H</code>
129 or <code>FT_SFNT_NAMES_H</code> are a lot more readable
130 and explanatory than the real file names
131 <code>ftmm.h</code> and <code>ftsnames.h</code>.</li>
132
133 <li>It allows special installation tricks that will not be
134 discussed here.</li>
135 </ul>
136
137
138 <h3 id="section-2">2. Library Initialization</h3>
139
140 <p>To initialize the FreeType library, create a variable of
141 type <a href="../reference/ft2-library_setup.html#ft_library"><code>FT_Library</code></a>
142 named, for example, <code>library</code>, and call the
143 function <a href="../reference/ft2-library_setup.html#ft_init_freetype"><code>FT_Init_FreeType</code></a>.</p>
144
145 <pre>
146 #include &lt;ft2build.h&gt;
147 #include FT_FREETYPE_H
148
149 FT_Library library;
150
151
152 ...
153
154 error = FT_Init_FreeType( &amp;library );
155 if ( error )
156 {
157 ... an error occurred during library initialization ...
158 }</pre>
159
160 <p>This function is in charge of</p>
161
162 <ul>
163 <li>creating a new instance of the FreeType&nbsp;2 library
164 and setting the handle <code>library</code> to it,
165 and</li>
166
167 <li>loading each module that FreeType knows about in the
168 library. Among others, your new <code>library</code>
169 object is able to handle TrueType, Type&nbsp;1,
170 CID-keyed &amp; OpenType/CFF fonts gracefully.</li>
171 </ul>
172
173 <p>As you can see, the function returns an error code, like
174 most other functions of the FreeType API. An error code
175 of&nbsp;0 (also known
176 as <code>FT_Err_Ok</code>) <em>always</em> means that the
177 operation was successful; otherwise, the value describes
178 the error, and <code>library</code> is set to NULL.</p>
179
180 <p>A list of all FreeType error codes can be found in
181 file <code>fterrdef.h</code>.</p>
182
183
184 <h3 id="section-3">3. Loading a Font Face</h3>
185
186 <h4>a. From a Font File</h4>
187
188 <p>Create a new <code>face</code> object by
189 calling <a href="../reference/ft2-face_creation.html#ft_new_face"><code>FT_New_Face</code></a>.
190 A <em>face</em> describes a given typeface and style. For
191 example, &lsquo;Times New Roman Regular&rsquo; and
192 &lsquo;Times New Roman Italic&rsquo; correspond to two
193 different faces.</p>
194
195 <pre>
196 FT_Library library; <span class="comment">/* handle to library */</span>
197 FT_Face face; <span class="comment">/* handle to face object */</span>
198
199
200 error = FT_Init_FreeType( &amp;library );
201 if ( error ) { ... }
202
203 error = FT_New_Face( library,
204 "/usr/share/fonts/truetype/arial.ttf",
205 0,
206 &amp;face );
207 if ( error == FT_Err_Unknown_File_Format )
208 {
209 ... the font file could be opened and read, but it appears
210 ... that its font format is unsupported
211 }
212 else if ( error )
213 {
214 ... another error code means that the font file could not
215 ... be opened or read, or that it is broken...
216 }</pre>
217
218 <p>As you can certainly imagine, <code>FT_New_Face</code>
219 opens a font file, then tries to extract one face from it.
220 Its parameters are as follows.</p>
221
222 <dl>
223 <dt>library</dt>
224 <dd>A handle to the FreeType library instance where the
225 face object is created.</dd>
226
227 <dt>filepathname</dt>
228 <dd>The font file pathname (a standard C string).</dd>
229
230 <dt>face_index</dt>
231 <dd>
232 <p>Certain font formats allow several font faces to be
233 embedded in a single file.</p>
234
235 <p>This index tells which face you want to load. An
236 error is returned if its value is too large.</p>
237
238 <p>Index&nbsp;0 always works, though.</p>
239 </dd>
240
241 <dt>face</dt>
242 <dd>
243 <p>A <em>pointer</em> to the handle that is set to
244 describe the new face object.</p>
245
246 <p>It is set to NULL in case of error.</p>
247 </dd>
248 </dl>
249
250 <p>To know how many faces a given font file contains, set
251 <code>face_index</code> to&nbsp;<code>-1</code>, then
252 check the value of <code>face-&gt;num_faces</code>, which
253 indicates how many faces are embedded in the font
254 file.</p>
255
256 <h4>b. From Memory</h4>
257
258 <p>In the case where you have already loaded the font file
259 into memory, you can similarly create a new face object
260 for it by
261 calling <a href="../reference/ft2-face_creation.html#ft_new_memory_face"><code>FT_New_Memory_Face</code></a>.</p>
262
263 <pre>
264 FT_Library library; <span class="comment">/* handle to library */</span>
265 FT_Face face; <span class="comment">/* handle to face object */</span>
266
267
268 error = FT_Init_FreeType( &amp;library );
269 if ( error ) { ... }
270
271 error = FT_New_Memory_Face( library,
272 buffer, <span class="comment">/* first byte in memory */</span>
273 size, <span class="comment">/* size in bytes */</span>
274 0, <span class="comment">/* face_index */</span>
275 &amp;face );
276 if ( error ) { ... }</pre>
277
278 <p>As you can see, <code>FT_New_Memory_Face</code> takes a
279 pointer to the font file buffer and its size in bytes
280 instead of a file pathname. Other than that, it has
281 exactly the same semantics as
282 <code>FT_New_Face</code>.</p>
283
284 <p>Note that you must not deallocate the font file buffer
285 before calling
286 <a href="../reference/ft2-face_creation.html#ft_done_face"><code>FT_Done_Face</code></a>.</p>
287
288 <h4>c. From Other Sources (Compressed Files, Network,
289 etc.)</h4>
290
291 <p>There are cases where using a file pathname or preloading
292 the file into memory is not sufficient. With
293 FreeType&nbsp;2, it is possible to provide your own
294 implementation of I/O routines.</p>
295
296 <p>This is done through
297 the <a href="../reference/ft2-face_creation.html#ft_open_face"><code>FT_Open_Face</code></a>
298 function, which can be used to open a new font face with a
299 custom input stream, select a specific driver for opening,
300 or even pass extra parameters to the font driver when
301 creating the object. We advise you to look up
302 the <a href="../reference/index.html">FreeType&nbsp;2
303 reference manual</a> in order to learn how to use it.</p>
304
305
306 <h3 id="section-4">4. Accessing the Face Data</h3>
307
308 <p>A <em>face object</em> models all information that
309 globally describes the face. Usually, this data can be
310 accessed directly by dereferencing a handle, like
311 in <code>face&minus;&gt;num_glyphs</code>.</p>
312
313 <p>The complete list of available fields is in
314 the <a href="../reference/ft2-face_creation.html#ft_facerec"><code>FT_FaceRec</code></a>
315 structure description. However, we describe here a few of
316 them in more detail.</p>
317
318 <dl>
319 <dt>num_glyphs</dt>
320 <dd>This variable gives the number of <em>glyphs</em>
321 available in the font face. A glyph is a character
322 image, nothing more – it thus doesn't necessarily
323 correspond to a <em>character code</em>.</dd>
324
325 <dt>face_flags</dt>
326 <dd>A 32-bit integer containing bit flags that describe
327 some face properties. For example, the flag
328 <code>FT_FACE_FLAG_SCALABLE</code> indicates that the
329 face's font format is scalable and that glyph images can
330 be rendered for all character pixel sizes. For more
331 information on face flags, please read
332 the <a href="../reference/ft2-face_creation.html#ft_face_flag_xxx">FreeType&nbsp;2
333 API Reference</a>.</dd>
334
335 <dt>units_per_EM</dt>
336 <dd>This field is only valid for scalable formats (it is
337 set to&nbsp;0 otherwise). It indicates the number of
338 font units covered by the EM.</dd>
339
340 <dt>num_fixed_sizes</dt>
341 <dd>This field gives the number of embedded bitmap strikes
342 in the current face. A <em>strike</em> is a series of
343 glyph images for a given character pixel size. For
344 example, a font face could include strikes for pixel
345 sizes 10, 12, and&nbsp;14. Note that even scalable font
346 formats can have embedded bitmap strikes!</dd>
347
348 <dt>available_sizes</dt>
349 <dd>
350 <p>A pointer to an array
351 of <a href="../reference/ft2-sizing_and_scaling.html#ft_bitmap_size"><code>FT_Bitmap_Size</code></a>
352 elements. Each <code>FT_Bitmap_Size</code> indicates
353 the horizontal and vertical <em>character pixel
354 sizes</em> for each of the strikes that are present in
355 the face.</p>
356
357 <p class="warning">Note that, generally speaking, these
358 are <em>not</em> the <em>cell size</em> of the bitmap
359 strikes.</p>
360 </dd>
361 </dl>
362
363
364 <h3 id="section-5">5. Setting the Current Pixel Size</h3>
365
366 <p>FreeType&nbsp;2 uses <em>size objects</em> to model all
367 information related to a given character size for a given
368 face. For example, a size object holds the value of
369 certain metrics, such as <code>x_ppem</code>
370 and <code>y_ppem</code>, which are both expressed in
371 pixels, and the ascender, text height, and maximum
372 horizontal advance, which are each expressed in 1/64 of a
373 pixel (however, those values are rounded to integers,
374 i.e., multiples of 64).</p>
375
376 <p>When the <code>FT_New_Face</code> function is called (or
377 one of its siblings), it <em>automatically</em> creates a
378 new size object for the returned face. This size object
379 is directly accessible as
380 <code>face&minus;&gt;size</code>.</p>
381
382 <p class="note">NOTE: A single face object can deal with one
383 or more size objects at a time; however, this is something
384 that few programmers really need to do. We have thus
385 decided to make this feature available through additional
386 functions.</p>
387
388 <p>When a new face object is created, all elements are set
389 to&nbsp;0 during initialization. To populate the
390 structure with sensible values, you should
391 call <a href="../reference/ft2-sizing_and_scaling.html#ft_set_char_size"><code>FT_Set_Char_Size</code></a>.
392 Here is an example, setting the character size to 16pt for
393 a 300&times;300dpi device:</p>
394
395 <pre>
396 error = FT_Set_Char_Size(
397 face, <span class="comment">/* handle to face object */</span>
398 0, <span class="comment">/* char_width in 1/64 of points */</span>
399 16*64, <span class="comment">/* char_height in 1/64 of points */</span>
400 300, <span class="comment">/* horizontal device resolution */</span>
401 300 ); <span class="comment">/* vertical device resolution */</span></pre>
402
403 <p>Some notes.</p>
404
405 <ul>
406 <li>The character widths and heights are specified in 1/64
407 of points. A point is a <em>physical</em> distance,
408 equaling 1/72 of an inch. Normally, it is not
409 equivalent to a pixel.</li>
410
411 <li>Value of&nbsp;0 for the character width means
412 &lsquo;same as character height&rsquo;, value of&nbsp;0
413 for the character height means &lsquo;same as character
414 width&rsquo;. Otherwise, it is possible to specify
415 different character widths and heights.</li>
416
417 <li>The horizontal and vertical device resolutions are
418 expressed in <em>dots-per-inch</em>, or <em>dpi</em>.
419 Standard values are 72 or 96&nbsp;dpi for display
420 devices like the screen. The resolution is used to
421 compute the character pixel size from the character
422 point size.</li>
423
424 <li>Value of&nbsp;0 for the horizontal resolution means
425 &lsquo;same as vertical resolution&rsquo;, value
426 of&nbsp;0 for the vertical resolution means &lsquo;same
427 as horizontal resolution&rsquo;. If both values are
428 zero, 72&nbsp;dpi is used for both dimensions.</li>
429
430 <li>The first argument is a handle to a face object, not a
431 size object.</li>
432 </ul>
433
434 <p>This function computes the (possibly fractional)
435 character pixel size that corresponds to the character
436 width and height and device resolutions. A common acronym
437 for the pixel size is <em>ppem</em> (pixel per em).</p>
438
439 <p>If you want to specify the (integer) pixel sizes
440 yourself, you can call
441 <a href="../reference/ft2-sizing_and_scaling.html#ft_set_pixel_sizes"><code>FT_Set_Pixel_Sizes</code></a>.</p>
442
443 <pre>
444 error = FT_Set_Pixel_Sizes(
445 face, <span class="comment">/* handle to face object */</span>
446 0, <span class="comment">/* pixel_width */</span>
447 16 ); <span class="comment">/* pixel_height */</span></pre>
448
449 <p>This example sets the character pixel sizes to
450 16&times;16&nbsp;pixels. As previously, a value of&nbsp;0
451 for one of the dimensions means &lsquo;same as the
452 other&rsquo;.</p>
453
454 <p>Note that both functions return an error code. Usually,
455 an error occurs with a fixed-size font format (like FNT or
456 PCF) when trying to set the pixel size to a value that is
457 not listed in the <code>face-&gt;fixed_sizes</code>
458 array.</p>
459
460 <p>Be aware that fractional ppem values are not always
461 supported. For example, the native bytecode engine for
462 hinting TrueType fonts (TTFs) only supports integer ppem
463 values, and FreeType rounds fractional ppem values
464 accordingly.</p>
465
466
467 <h3 id="section-6">6. Loading a Glyph Image</h3>
468
469 <h4>a. Converting a Character Code Into a Glyph Index</h4>
470
471 <p>Normally, an application wants to load a glyph image
472 based on its <em>character code</em>, which is a unique
473 value that defines the character for a
474 given <em>encoding</em>. For example, code&nbsp;65 (0x41)
475 represents character &lsquo;A&rsquo; in ASCII
476 encoding.</p>
477
478 <p>A face object contains one or more tables, called
479 <em>charmaps</em>, to convert character codes to glyph
480 indices. For example, most older TrueType fonts contain
481 two charmaps: One is used to convert Unicode character
482 codes to glyph indices, the other one is used to convert
483 Apple Roman encoding to glyph indices. Such fonts can
484 then be used either on Windows (which uses Unicode) and
485 old MacOS versions (which use Apple Roman). Note also
486 that a given charmap might not map to all the glyphs
487 present in the font.</p>
488
489 <p>By default, when a new face object is created, FreeType
490 tries to select a Unicode charmap. It emulates a Unicode
491 charmap if the font doesn't contain such a charmap, based
492 on glyph names. Note that it is possible that the
493 emulation misses glyphs if glyph names are non-standard.
494 For some fonts like symbol fonts, no Unicode emulation is
495 possible at all.</p>
496
497 <p>Later on we will describe how to look for specific
498 charmaps in a face. For now, we assume that the face
499 contains at least a Unicode charmap that was selected
500 during a call to <code>FT_New_Face</code>. To convert a
501 Unicode character code to a font glyph index, we use
502 <a href="../reference/ft2-character_mapping.html#ft_get_char_index"><code>FT_Get_Char_Index</code></a>.</p>
503
504 <pre>
505 glyph_index = FT_Get_Char_Index( face, charcode );</pre>
506
507 <p>This code line looks up the glyph index corresponding to
508 the given <code>charcode</code> in the charmap that is
509 currently selected for the face. You should use the
510 UTF-32 representation form of Unicode; for example, if you
511 want to load character U+1F028, use value 0x1F028 as the
512 value for <code>charcode</code>.
513
514 <p>If no charmap was selected, the function returns
515 the charcode.</p>
516
517 <p>Note that this is one of the rare FreeType functions that
518 do not return an error code. However, when a given
519 character code has no glyph image in the face,
520 value&nbsp;0 is returned. By convention, it always
521 corresponds to a special glyph image called
522 the <em>missing glyph</em>, which is commonly displayed as
523 a box or a space.</p>
524
525 <h4>b. Loading a Glyph From the Face</h4>
526
527 <p>Once you have a glyph index, you can load the
528 corresponding glyph image. The latter can be stored in
529 various formats within the font file. For fixed-size
530 formats like FNT or PCF, each image is a bitmap. Scalable
531 formats like TrueType or CFF use vectorial shapes
532 (<em>outlines</em>) to describe each glyph. Some formats
533 may have even more exotic ways of representing glyphs
534 (e.g., MetaFont – but this format is not supported).
535 Fortunately, FreeType&nbsp;2 is flexible enough to support
536 any kind of glyph format through a simple API.</p>
537
538 <p>The glyph image is always stored in a special object called a
539 <em>glyph slot</em>. As its name suggests, a glyph slot
540 is a container that is able to hold one glyph image at a
541 time, be it a bitmap, an outline, or something else. Each
542 face object has a single glyph slot object that can be
543 accessed as <code>face-&gt;glyph</code>. Its fields are
544 explained by
545 the <a href="../reference/ft2-glyph_retrieval.html#ft_glyphslotrec"><code>FT_GlyphSlotRec</code></a>
546 structure documentation.</p>
547
548 <p>Loading a glyph image into the slot is performed by
549 calling <a href="../reference/ft2-glyph_retrieval.html#ft_load_glyph"><code>FT_Load_Glyph</code></a>.</p>
550
551 <pre>
552 error = FT_Load_Glyph(
553 face, <span class="comment">/* handle to face object */</span>
554 glyph_index, <span class="comment">/* glyph index */</span>
555 load_flags ); <span class="comment">/* load flags, see below */</span></pre>
556
557 <p>The <code>load_flags</code> value is a set of bit flags
558 to indicate some special operations. The default value
559 <code>FT_LOAD_DEFAULT</code> is&nbsp;0.</p>
560
561 <p>This function tries to load the corresponding glyph image
562 from the face.</p>
563
564 <ul>
565 <li>If a bitmap is found for the corresponding glyph and
566 pixel size, it is loaded into the slot. Embedded
567 bitmaps are always favoured over native image formats,
568 because we assume that they are higher-quality versions
569 of the same glyph. This can be changed by using
570 the <code>FT_LOAD_NO_BITMAP</code> flag.</li>
571
572 <li>Otherwise, a native image for the glyph is loaded. It
573 is also scaled to the current pixel size, as well as
574 hinted for certain formats like TrueType and
575 Type&nbsp;1.</li>
576 </ul>
577
578 <p>The
579 field <code>face&minus;&gt;glyph&minus;&gt;format</code>
580 describes the format used for storing the glyph image in
581 the slot. If it is
582 not <code>FT_GLYPH_FORMAT_BITMAP</code>, one can
583 immediately convert it to a bitmap
584 through <a href="../reference/ft2-glyph_retrieval.html#ft_render_glyph"><code>FT_Render_Glyph</code></a>.</p>
585
586 <pre>
587 error = FT_Render_Glyph( face-&gt;glyph, <span class="comment">/* glyph slot */</span>
588 render_mode ); <span class="comment">/* render mode */</span></pre>
589
590 <p>The parameter <code>render_mode</code> is a set of bit
591 flags to specify how to render the glyph image.
592 <code>FT_RENDER_MODE_NORMAL</code>, the default, renders
593 an anti-aliased coverage bitmap with 256 gray levels (also
594 called a <em>pixmap</em>), as this is the default. You
595 can alternatively use <code>FT_RENDER_MODE_MONO</code> if
596 you want to generate a 1-bit monochrome bitmap. More
597 values are available for
598 the <a href="../reference/ft2-glyph_retrieval.html#ft_render_mode"><code>FT_Render_Mode</code></a>
599 enumeration value.</p>
600
601 <p>Once you have a bitmapped glyph image, you can access it
602 directly through <code>glyph-&gt;bitmap</code> (a simple
603 descriptor for bitmaps or pixmaps), and position it
604 through <code>glyph-&gt;bitmap_left</code> and
605 <code>glyph-&gt;bitmap_top</code>. For optimal rendering
606 on a screen the bitmap should be used as an alpha channel
607 in linear blending with gamma correction.</p>
608
609 <p>Note that <code>bitmap_left</code> is the horizontal
610 distance from the current pen position to the leftmost
611 border of the glyph bitmap, while <code>bitmap_top</code>
612 is the vertical distance from the pen position (on the
613 baseline) to the topmost border of the glyph
614 bitmap. <em>It is positive to indicate an upwards
615 distance</em>.</p>
616
617 <h4>c. Using Other Charmaps</h4>
618
619 <p>As said before, when a new face object is created, it
620 looks for a Unicode charmap and select it. The currently
621 selected charmap can be accessed
622 via <code>face-&gt;charmap</code>. This field is NULL if
623 no charmap is selected, which typically happens when you
624 create a new <code>FT_Face</code> object from a font file
625 that doesn't contain a Unicode charmap (which is rather
626 infrequent today).</p>
627
628 <p>There are two ways to select a different charmap with
629 FreeType. It's easiest if the encoding you need already
630 has a corresponding enumeration defined
631 in <code>FT_FREETYPE_H</code>, for
632 example <code>FT_ENCODING_BIG5</code>. In this case, you
633 can call
634 <a href="../reference/ft2-character_mapping.html#ft_select_charmap"><code>FT_Select_Charmap</code></a>.</p>
635
636 <pre>
637 error = FT_Select_Charmap(
638 face, <span class="comment">/* target face object */</span>
639 FT_ENCODING_BIG5 ); <span class="comment">/* encoding */</span></pre>
640
641 <p>Another way is to manually parse the list of charmaps for
642 the face; this is accessible through the
643 fields <code>num_charmaps</code> and
644 <code>charmaps</code> (notice the &lsquo;s&rsquo;) of the
645 face object. As you could expect, the first is the number
646 of charmaps in the face, while the second is <em>a table
647 of pointers to the charmaps</em> embedded in the face.</p>
648
649 <p>Each charmap has a few visible fields to describe it more
650 precisely. The most important ones are
651 <code>charmap-&gt;platform_id</code>
652 and <code>charmap-&gt;encoding_id</code>, defining a pair
653 of values that describe the charmap in a rather generic
654 way: Each value pair corresponds to a given encoding. For
655 example, the pair (3,1) corresponds to Unicode. The list
656 is defined in the OpenType specification; you can also use
657 the file <code>FT_TRUETYPE_IDS_H</code>, which defines
658 several helpful constants to deal with them. Note that we
659 use the OpenType enumeration values for non-OpenType fonts
660 also (by defining additional constants where
661 necessary).</p>
662
663 <p>To select a specific encoding, you need to find a
664 corresponding value pair in the specification, then look
665 for it in the charmaps list.</p>
666
667 <pre>
668 FT_CharMap found = 0;
669 FT_CharMap charmap;
670 int n;
671
672
673 for ( n = 0; n &lt; face-&gt;num_charmaps; n++ )
674 {
675 charmap = face-&gt;charmaps[n];
676 if ( charmap-&gt;platform_id == my_platform_id &amp;&amp;
677 charmap-&gt;encoding_id == my_encoding_id )
678 {
679 found = charmap;
680 break;
681 }
682 }
683
684 if ( !found ) { ... }
685
686 <span class="comment">/* now, select the charmap for the face object */</span>
687 error = FT_Set_Charmap( face, found );
688 if ( error ) { ... }</pre>
689
690 <p>Once a charmap has been selected, either through
691 <code>FT_Select_Charmap</code>
692 or <a href="../reference/ft2-character_mapping.html#ft_set_charmap"><code>FT_Set_Charmap</code></a>,
693 it is used by all subsequent calls
694 to <code>FT_Get_Char_Index</code>.</p>
695
696 <h4>d. Glyph Transformations</h4>
697
698 <p>It is possible to specify an affine transformation with
699 <a href="../reference/ft2-sizing_and_scaling.html#ft_set_transform"><code>FT_Set_Transform</code></a>,
700 to be applied to glyph images when they are loaded. Of
701 course, this only works for scalable (vectorial) font
702 formats.</p>
703
704 <pre>
705 error = FT_Set_Transform(
706 face, <span class="comment">/* target face object */</span>
707 &amp;matrix, <span class="comment">/* pointer to 2x2 matrix */</span>
708 &amp;delta ); <span class="comment">/* pointer to 2d vector */</span></pre>
709
710 <p>This function sets the current transformation for a given
711 face object. Its second parameter is a pointer to an
712 <a href="../reference/ft2-basic_types.html#ft_matrix"><code>FT_Matrix</code></a>
713 structure that describes a 2&times;2 affine matrix. The
714 third parameter is a pointer to
715 an <a href="../reference/ft2-basic_types.html#ft_vector"><code>FT_Vector</code></a>
716 structure, describing a two-dimensional vector that
717 translates the glyph image <em>after</em> the 2&times;2
718 transformation.</p>
719
720 <p>Note that the matrix pointer can be set to NULL, in which
721 case the identity transformation is used. Coefficients of
722 the matrix are otherwise in 16.16 fixed-point units.</p>
723
724 <p>The vector pointer can also be set to NULL (in which case
725 a delta of (0,0) is used). The vector coordinates are
726 expressed in 1/64 of a pixel (also known as 26.6
727 fixed-point numbers).</p>
728
729 <p class="warning">The transformation is applied to every
730 glyph that is loaded through <code>FT_Load_Glyph</code>
731 and is <em>completely independent of any hinting
732 process</em>. This means that you won't get the same
733 results if you load a glyph at the size of 24&nbsp;pixels,
734 or a glyph at the size of 12&nbsp;pixels scaled by&nbsp;2
735 through a transformation, because the hints are computed
736 differently (except if you have disabled hints).</p>
737
738 <p>If you ever need to use a non-orthogonal transformation
739 with optimal hints, you first have to decompose your
740 transformation into a scaling part and a rotation/shearing
741 part. Use the scaling part to compute a new character
742 pixel size, then the other one to call
743 <code>FT_Set_Transform</code>. This is explained in more
744 detail in part&nbsp;II of this tutorial.</p>
745
746 <p class="warning">Rotation usually disables hinting.</p>
747
748 <p>Loading a glyph bitmap with a non-identity transformation
749 works; the transformation is ignored in this case.</p>
750
751
752 <h3 id="section-7">7. Simple Text Rendering</h3>
753
754 <p>We now present a simple example to render a string of
755 8-bit Latin-1 text, assuming a face that contains a
756 Unicode charmap.</p>
757
758 <p>The idea is to create a loop that loads one glyph image
759 on each iteration, converts it to a pixmap, draws it on
760 the target surface, then increments the current pen
761 position.</p>
762
763 <h4 id="basic-code">a. Basic Code</h4>
764
765 <p>The following code performs our simple text rendering
766 with the functions previously described.</p>
767
768 <pre>
769 FT_GlyphSlot slot = face-&gt;glyph; <span class="comment">/* a small shortcut */</span>
770 int pen_x, pen_y, n;
771
772
773 ... initialize library ...
774 ... create face object ...
775 ... set character size ...
776
777 pen_x = 300;
778 pen_y = 200;
779
780 for ( n = 0; n &lt; num_chars; n++ )
781 {
782 FT_UInt glyph_index;
783
784
785 <span class="comment">/* retrieve glyph index from character code */</span>
786 glyph_index = FT_Get_Char_Index( face, text[n] );
787
788 <span class="comment">/* load glyph image into the slot (erase previous one) */</span>
789 error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
790 if ( error )
791 continue; <span class="comment">/* ignore errors */</span>
792
793 <span class="comment">/* convert to an anti-aliased bitmap */</span>
794 error = FT_Render_Glyph( face-&gt;glyph, FT_RENDER_MODE_NORMAL );
795 if ( error )
796 continue;
797
798 <span class="comment">/* now, draw to our target surface */</span>
799 my_draw_bitmap( &amp;slot-&gt;bitmap,
800 pen_x + slot-&gt;bitmap_left,
801 pen_y - slot-&gt;bitmap_top );
802
803 <span class="comment">/* increment pen position */</span>
804 pen_x += slot-&gt;advance.x >> 6;
805 pen_y += slot-&gt;advance.y >> 6; <span class="comment">/* not useful for now */</span>
806 }</pre>
807
808 <p>This code needs a few explanations.</p>
809
810 <ul>
811 <li>We define a handle named <code>slot</code> that points
812 to the face object's glyph slot. (The
813 type <a href="../reference/ft2-glyph_retrieval.html#ft_glyphslot"><code>FT_GlyphSlot</code></a>
814 is a pointer). That is a convenience to avoid using
815 <code>face-&gt;glyph-&gt;XXX</code> every time.</li>
816
817 <li>We increment the pen position with the vector
818 <code>slot-&gt;advance</code>, which correspond to the
819 glyph's <em>advance width</em> (also known as
820 its <em>escapement</em>). The advance vector is
821 expressed in 1/64 of pixels, and is truncated to integer
822 pixels on each iteration.</li>
823
824 <li>The function <code>my_draw_bitmap</code> is not part
825 of FreeType but must be provided by the application to
826 draw the bitmap to the target surface. In this example,
827 it takes a pointer to
828 an <a href="../reference/ft2-basic_types.html#ft_bitmap"><code>FT_Bitmap</code></a>
829 descriptor and the position of its top-left corner as
830 arguments. For ideal rendering on a screen this
831 function should perform linear blending with gamma
832 correction, using the bitmap as an alpha channel.</li>
833
834 <li>The value of <code>slot-&gt;bitmap_top</code> is
835 positive for an <em>upwards</em> vertical distance.
836 Assuming that the coordinates taken
837 by <code>my_draw_bitmap</code> use the opposite
838 convention (increasing&nbsp;Y corresponds to downwards
839 scanlines), we subtract it from <code>pen_y</code>,
840 instead of adding to it.</li>
841 </ul>
842
843 <h4>b.Refined code</h4>
844
845 <p>The following code is a refined version of the example
846 above. It uses features and functions of FreeType that
847 have not yet been introduced, and which are explained
848 below.</p>
849
850 <pre>
851 FT_GlyphSlot slot = face-&gt;glyph; <span class="comment">/* a small shortcut */</span>
852 FT_UInt glyph_index;
853 int pen_x, pen_y, n;
854
855
856 ... initialize library ...
857 ... create face object ...
858 ... set character size ...
859
860 pen_x = 300;
861 pen_y = 200;
862
863 for ( n = 0; n &lt; num_chars; n++ )
864 {
865 <span class="comment">/* load glyph image into the slot (erase previous one) */</span>
866 error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
867 if ( error )
868 continue; <span class="comment">/* ignore errors */</span>
869
870 <span class="comment">/* now, draw to our target surface */</span>
871 my_draw_bitmap( &amp;slot-&gt;bitmap,
872 pen_x + slot-&gt;bitmap_left,
873 pen_y - slot-&gt;bitmap_top );
874
875 <span class="comment">/* increment pen position */</span>
876 pen_x += slot-&gt;advance.x >> 6;
877 }</pre>
878
879 <p>We have reduced the size of our code, but it does exactly
880 the same thing.</p>
881
882 <ul>
883 <li>We use the
884 function <a href="../reference/ft2-character_mapping.html#ft_load_char"><code>FT_Load_Char</code></a>
885 instead of <code>FT_Load_Glyph</code>. As you probably
886 imagine, it is equivalent to
887 calling <code>FT_Get_Char_Index</code>, then
888 <code>FT_Load_Glyph</code>.</li>
889
890 <li>
891 <p>We do not use <code>FT_LOAD_DEFAULT</code> for the
892 loading mode, but the bit
893 flag <code>FT_LOAD_RENDER</code>. It indicates that
894 the glyph image must be immediately converted to an
895 anti-aliased bitmap. This is of course a shortcut
896 that avoids calling <code>FT_Render_Glyph</code>
897 explicitly but is strictly equivalent.</p>
898
899 <p>Note that you can also specify that you want a
900 monochrome bitmap instead by using the
901 additional <code>FT_LOAD_MONOCHROME</code> load
902 flag.</p>
903 </li>
904 </ul>
905
906 <h4 id="transformed-text">c. More Advanced Rendering</h4>
907
908 <p>Let us try to render transformed text now (for example
909 through a rotation). We can do this
910 using <code>FT_Set_Transform</code>.</p>
911
912 <pre>
913 FT_GlyphSlot slot;
914 FT_Matrix matrix; <span class="comment">/* transformation matrix */</span>
915 FT_UInt glyph_index;
916 FT_Vector pen; <span class="comment">/* untransformed origin */</span>
917 int n;
918
919
920 ... initialize library ...
921 ... create face object ...
922 ... set character size ...
923
924 slot = face-&gt;glyph; <span class="comment">/* a small shortcut */</span>
925
926 <span class="comment">/* set up matrix */</span>
927 matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
928 matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
929 matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
930 matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
931
932 <span class="comment">/* the pen position in 26.6 cartesian space coordinates */</span>
933 <span class="comment">/* start at (300,200) */</span>
934 pen.x = 300 * 64;
935 pen.y = ( my_target_height - 200 ) * 64;
936
937 for ( n = 0; n &lt; num_chars; n++ )
938 {
939 <span class="comment">/* set transformation */</span>
940 FT_Set_Transform( face, &amp;matrix, &amp;pen );
941
942 <span class="comment">/* load glyph image into the slot (erase previous one) */</span>
943 error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
944 if ( error )
945 continue; <span class="comment">/* ignore errors */</span>
946
947 <span class="comment">/* now, draw to our target surface (convert position) */</span>
948 my_draw_bitmap( &amp;slot-&gt;bitmap,
949 slot-&gt;bitmap_left,
950 my_target_height - slot-&gt;bitmap_top );
951
952 <span class="comment">/* increment pen position */</span>
953 pen.x += slot-&gt;advance.x;
954 pen.y += slot-&gt;advance.y;
955 }</pre>
956
957 <p>Some remarks.</p>
958
959 <ul>
960 <li>We now use a vector of type <code>FT_Vector</code> to
961 store the pen position, with coordinates expressed as
962 1/64 of pixels, hence a multiplication. The position is
963 expressed in cartesian space.</li>
964
965 <li>Glyph images are always loaded, transformed, and
966 described in the cartesian coordinate system within
967 FreeType (which means that increasing&nbsp;Y corresponds
968 to upper scanlines), unlike the system typically used
969 for bitmaps (where the topmost scanline has
970 coordinate&nbsp;0). We must thus convert between the
971 two systems when we define the pen position, and when we
972 compute the topleft position of the bitmap.</li>
973
974 <li>We set the transformation on each glyph to indicate
975 the rotation matrix as well as a delta that moves the
976 transformed image to the current pen position (in
977 cartesian space, not bitmap space). As a consequence,
978 the values of <code>bitmap_left</code>
979 and <code>bitmap_top</code> correspond to the bitmap
980 origin in target space pixels. We thus don't
981 add <code>pen.x</code> or <code>pen.y</code> to their
982 values when calling <code>my_draw_bitmap</code>.</li>
983
984 <li>The advance width is always returned transformed,
985 which is why it can be directly added to the current pen
986 position. Note that it is <em>not</em> rounded this
987 time.</li>
988 </ul>
989
990 <p>A complete source code example can be found
991 <a href="example1.c">here</a>.</p>
992
993 <p>It is important to note that, while this example is a bit
994 more complex than the previous one, it is strictly
995 equivalent for the case where the transformation is the
996 identity. Hence it can be used as a replacement (but a
997 more powerful one).</p>
998
999 <p>The still present few shortcomings will be explained, and
1000 solved, in the next part of this tutorial.</p>
1001 </div>
1002
1003 <!-- ************************************************** -->
1004
1005 <div class="updated">
1006 <p>Last update: 24-Oct-2022</p>
1007 </div>
1008 </div>
1009 </div>
1010
1011
1012 <!-- ************************************************** -->
1013
1014 <div class="col2">
1015 </div>
1016 </div>
1017 </div>
1018
1019
1020 <!-- ************************************************** -->
1021
1022 <div id="TOC">
1023 <ul>
1024 <li class="funding">
1025 <form action="https://www.paypal.com/cgi-bin/webscr"
1026 method="post"
1027 target="_top">
1028 <input type="hidden"
1029 name="cmd"
1030 value="_s-xclick">
1031 <input type="hidden"
1032 name="hosted_button_id"
1033 value="SK827YKEALMT4">
•1034 <input type="submit"
1035 value="Donate"
1036 name="submit"
1037 alt="PayPal - The safer, easier way to pay online!">
1038 </form>
1039 </li>
1040 <li class="primary">
1041 <a href="http://freetype.org/index.html">Home</a>
1042 </li>
1043 <li class="primary">
1044 <a href="http://freetype.org/index.html#news">News</a>
1045 </li>
1046 <li class="primary">
1047 <a href="../index.html">Overview</a>
1048 </li>
1049 <li class="primary">
1050 <a href="../documentation.html">Documentation</a>
1051 </li>
1052 <li class="primary">
1053 <a href="http://freetype.org/developer.html">Development</a>
1054 </li>
1055 <li class="primary">
1056 <a href="http://freetype.org/contact.html"
1057 class="emphasis">Contact</a>
1058 </li>
1059
1060 <li>
1061 &nbsp; <!-- separate primary from secondary entries -->
1062 </li>
1063
1064 <li class="secondary">
1065 <a href="index.html">FreeType Tutorial</a>
1066 </li>
1067 <li class="tertiary">
1068 <a href="step1.html" class="current">Simple Glyph Loading</a>
1069 </li>
1070 <li class="tertiary">
1071 <a href="step2.html">Managing Glyphs</a>
1072 </li>
1073 <li class="tertiary">
1074 <a href="step3.html">Examples</a>
1075 </li>
1076 </ul>
1077 </div>
1078
1079 </div> <!-- id="wrapper" -->
1080
1081 <div id="TOC-bottom">
1082 </div>
1083
1084 </body>
1085 </html>