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