1 /*
2  * FTGL - OpenGL font library
3  *
4  * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef     __FTCharmap__
27 #define     __FTCharmap__
28 
29 
30 #include <ft2build.h>
31 #include FT_FREETYPE_H
32 #include FT_GLYPH_H
33 
34 #include "FTGL/ftgl.h"
35 #include "FTCharToGlyphIndexMap.h"
36 
37 
38 /**
39  * FTCharmap takes care of specifying the encoding for a font and mapping
40  * character codes to glyph indices.
41  *
42  * It doesn't preprocess all indices, only on an as needed basis. This may
43  * seem like a performance penalty but it is quicker than using the 'raw'
44  * freetype calls and will save significant amounts of memory when dealing
45  * with unicode encoding
46  *
47  * @see "Freetype 2 Documentation"
48  *
49  */
50 
51 class FTFace;
52 
53 class FTCharmap
54 {
55     public:
56         /**
57          * Constructor
58          */
59         FTCharmap(FTFace* face);
60 
61         /**
62          * Destructor
63          */
64         virtual ~FTCharmap();
65 
66         /**
67          * Queries for the current character map code.
68          *
69          * @return  The current character map code.
70          */
Encoding()71         FT_Encoding Encoding() const { return ftEncoding; }
72 
73         /**
74          * Sets the character map for the face. If an error occurs the object is not modified.
75          * Valid encodings as at Freetype 2.0.4
76          *      ft_encoding_none
77          *      ft_encoding_symbol
78          *      ft_encoding_unicode
79          *      ft_encoding_latin_2
80          *      ft_encoding_sjis
81          *      ft_encoding_gb2312
82          *      ft_encoding_big5
83          *      ft_encoding_wansung
84          *      ft_encoding_johab
85          *      ft_encoding_adobe_standard
86          *      ft_encoding_adobe_expert
87          *      ft_encoding_adobe_custom
88          *      ft_encoding_apple_roman
89          *
90          * @param encoding  the Freetype encoding symbol. See above.
91          * @return          <code>true</code> if charmap was valid and set
92          *                  correctly.
93          */
94         bool CharMap(FT_Encoding encoding);
95 
96         /**
97          * Get the FTGlyphContainer index of the input character.
98          *
99          * @param characterCode The character code of the requested glyph in
100          *                      the current encoding eg apple roman.
101          * @return      The FTGlyphContainer index for the character or zero
102          *              if it wasn't found
103          */
104         unsigned int GlyphListIndex(const unsigned int characterCode);
105 
106         /**
107          * Get the font glyph index of the input character.
108          *
109          * @param characterCode The character code of the requested glyph in
110          *                      the current encoding eg apple roman.
111          * @return      The glyph index for the character.
112          */
113         unsigned int FontIndex(const unsigned int characterCode);
114 
115         /**
116          * Set the FTGlyphContainer index of the character code.
117          *
118          * @param characterCode  The character code of the requested glyph in
119          *                       the current encoding eg apple roman.
120          * @param containerIndex The index into the FTGlyphContainer of the
121          *                       character code.
122          */
123         void InsertIndex(const unsigned int characterCode,
124                          const size_t containerIndex);
125 
126         /**
127          * Queries for errors.
128          *
129          * @return  The current error code. Zero means no error.
130          */
Error()131         FT_Error Error() const { return err; }
132 
133     private:
134         /**
135          * Current character map code.
136          */
137         FT_Encoding ftEncoding;
138 
139         /**
140          * The current Freetype face.
141          */
142         const FT_Face ftFace;
143 
144         /**
145          * A structure that maps glyph indices to character codes
146          *
147          * < character code, face glyph index>
148          */
149         typedef FTCharToGlyphIndexMap CharacterMap;
150         CharacterMap charMap;
151 
152         /**
153          * Precomputed font indices.
154          */
155         static const unsigned int MAX_PRECOMPUTED = 128;
156         unsigned int charIndexCache[MAX_PRECOMPUTED];
157 
158         /**
159          * Current error code.
160          */
161         FT_Error err;
162 };
163 
164 
165 #endif  //  __FTCharmap__
166