1 /*
2  * FTGL - OpenGL font library
3  *
4  * Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
5  * Copyright (c) 2008 Sam Hocevar <sam@zoy.org>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #include "config.h"
28 
29 #include "FTFace.h"
30 #include "FTLibrary.h"
31 
32 #include FT_TRUETYPE_TABLES_H
33 
FTFace(const char * fontFilePath,bool precomputeKerning)34 FTFace::FTFace(const char* fontFilePath, bool precomputeKerning)
35 :   numGlyphs(0),
36     fontEncodingList(0),
37     kerningCache(0),
38     err(0)
39 {
40     const FT_Long DEFAULT_FACE_INDEX = 0;
41     ftFace = new FT_Face;
42 
43     err = FT_New_Face(*FTLibrary::Instance().GetLibrary(), fontFilePath,
44                       DEFAULT_FACE_INDEX, ftFace);
45     if(err)
46     {
47         delete ftFace;
48         ftFace = 0;
49         return;
50     }
51 
52     numGlyphs = (*ftFace)->num_glyphs;
53     hasKerningTable = (FT_HAS_KERNING((*ftFace)) != 0);
54 
55     if(hasKerningTable && precomputeKerning)
56     {
57         BuildKerningCache();
58     }
59 }
60 
61 
FTFace(const unsigned char * pBufferBytes,size_t bufferSizeInBytes,bool precomputeKerning)62 FTFace::FTFace(const unsigned char *pBufferBytes, size_t bufferSizeInBytes,
63                bool precomputeKerning)
64 :   numGlyphs(0),
65     fontEncodingList(0),
66     kerningCache(0),
67     err(0)
68 {
69     const FT_Long DEFAULT_FACE_INDEX = 0;
70     ftFace = new FT_Face;
71 
72     err = FT_New_Memory_Face(*FTLibrary::Instance().GetLibrary(),
73                              (FT_Byte const *)pBufferBytes, (FT_Long)bufferSizeInBytes,
74                              DEFAULT_FACE_INDEX, ftFace);
75     if(err)
76     {
77         delete ftFace;
78         ftFace = 0;
79         return;
80     }
81 
82     numGlyphs = (*ftFace)->num_glyphs;
83     hasKerningTable = (FT_HAS_KERNING((*ftFace)) != 0);
84 
85     if(hasKerningTable && precomputeKerning)
86     {
87         BuildKerningCache();
88     }
89 }
90 
91 
~FTFace()92 FTFace::~FTFace()
93 {
94     if(kerningCache)
95     {
96         delete[] kerningCache;
97     }
98 
99     if(ftFace)
100     {
101         FT_Done_Face(*ftFace);
102         delete ftFace;
103         ftFace = 0;
104     }
105 }
106 
107 
Attach(const char * fontFilePath)108 bool FTFace::Attach(const char* fontFilePath)
109 {
110     err = FT_Attach_File(*ftFace, fontFilePath);
111     return !err;
112 }
113 
114 
Attach(const unsigned char * pBufferBytes,size_t bufferSizeInBytes)115 bool FTFace::Attach(const unsigned char *pBufferBytes,
116                     size_t bufferSizeInBytes)
117 {
118     FT_Open_Args open;
119 
120     open.flags = FT_OPEN_MEMORY;
121     open.memory_base = (FT_Byte const *)pBufferBytes;
122     open.memory_size = (FT_Long)bufferSizeInBytes;
123 
124     err = FT_Attach_Stream(*ftFace, &open);
125     return !err;
126 }
127 
128 
Size(const unsigned int size,const unsigned int res)129 const FTSize& FTFace::Size(const unsigned int size, const unsigned int res)
130 {
131     charSize.CharSize(ftFace, size, res, res);
132     err = charSize.Error();
133 
134     return charSize;
135 }
136 
137 
CharMapCount() const138 unsigned int FTFace::CharMapCount() const
139 {
140     return (*ftFace)->num_charmaps;
141 }
142 
143 
CharMapList()144 FT_Encoding* FTFace::CharMapList()
145 {
146     if(0 == fontEncodingList)
147     {
148         fontEncodingList = new FT_Encoding[CharMapCount()];
149         for(size_t i = 0; i < CharMapCount(); ++i)
150         {
151             fontEncodingList[i] = (*ftFace)->charmaps[i]->encoding;
152         }
153     }
154 
155     return fontEncodingList;
156 }
157 
158 
KernAdvance(unsigned int index1,unsigned int index2)159 FTPoint FTFace::KernAdvance(unsigned int index1, unsigned int index2)
160 {
161     float x, y;
162 
163     if(!hasKerningTable || !index1 || !index2)
164     {
165         return FTPoint(0.0f, 0.0f);
166     }
167 
168     if(kerningCache && index1 < FTFace::MAX_PRECOMPUTED
169         && index2 < FTFace::MAX_PRECOMPUTED)
170     {
171         x = kerningCache[2 * (index2 * FTFace::MAX_PRECOMPUTED + index1)];
172         y = kerningCache[2 * (index2 * FTFace::MAX_PRECOMPUTED + index1) + 1];
173         return FTPoint(x, y);
174     }
175 
176     FT_Vector kernAdvance;
177     kernAdvance.x = kernAdvance.y = 0;
178 
179     err = FT_Get_Kerning(*ftFace, index1, index2, ft_kerning_unfitted,
180                          &kernAdvance);
181     if(err)
182     {
183         return FTPoint(0.0f, 0.0f);
184     }
185 
186     x = static_cast<float>(kernAdvance.x) / 64.0f;
187     y = static_cast<float>(kernAdvance.y) / 64.0f;
188 
189     return FTPoint(x, y);
190 }
191 
192 
Glyph(unsigned int index,FT_Int load_flags)193 FT_GlyphSlot FTFace::Glyph(unsigned int index, FT_Int load_flags)
194 {
195     err = FT_Load_Glyph(*ftFace, index, load_flags);
196     if(err)
197     {
198         return NULL;
199     }
200 
201     return (*ftFace)->glyph;
202 }
203 
204 
BuildKerningCache()205 void FTFace::BuildKerningCache()
206 {
207     FT_Vector kernAdvance;
208     kernAdvance.x = 0;
209     kernAdvance.y = 0;
210     kerningCache = new float[FTFace::MAX_PRECOMPUTED
211                               * FTFace::MAX_PRECOMPUTED * 2];
212     for(unsigned int j = 0; j < FTFace::MAX_PRECOMPUTED; j++)
213     {
214         for(unsigned int i = 0; i < FTFace::MAX_PRECOMPUTED; i++)
215         {
216             err = FT_Get_Kerning(*ftFace, i, j, ft_kerning_unfitted,
217                                  &kernAdvance);
218             if(err)
219             {
220                 delete[] kerningCache;
221                 kerningCache = NULL;
222                 return;
223             }
224 
225             kerningCache[2 * (j * FTFace::MAX_PRECOMPUTED + i)] =
226                                 static_cast<float>(kernAdvance.x) / 64.0f;
227             kerningCache[2 * (j * FTFace::MAX_PRECOMPUTED + i) + 1] =
228                                 static_cast<float>(kernAdvance.y) / 64.0f;
229         }
230     }
231 }
232 
233