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     __FTFace__
27 #define     __FTFace__
28 
29 #include <ft2build.h>
30 #include FT_FREETYPE_H
31 #include FT_GLYPH_H
32 
33 #include "FTGL/ftgl.h"
34 
35 #include "FTSize.h"
36 
37 /**
38  * FTFace class provides an abstraction layer for the Freetype Face.
39  *
40  * @see "Freetype 2 Documentation"
41  *
42  */
43 class FTFace
44 {
45     public:
46         /**
47          * Opens and reads a face file. Error is set.
48          *
49          * @param fontFilePath  font file path.
50          */
51         FTFace(const char* fontFilePath, bool precomputeKerning = true);
52 
53         /**
54          * Read face data from an in-memory buffer. Error is set.
55          *
56          * @param pBufferBytes  the in-memory buffer
57          * @param bufferSizeInBytes  the length of the buffer in bytes
58          */
59         FTFace(const unsigned char *pBufferBytes, size_t bufferSizeInBytes,
60                bool precomputeKerning = true);
61 
62         /**
63          * Destructor
64          *
65          * Disposes of the current Freetype Face.
66          */
67         virtual ~FTFace();
68 
69         /**
70          * Attach auxilliary file to font (e.g., font metrics).
71          *
72          * @param fontFilePath  auxilliary font file path.
73          * @return          <code>true</code> if file has opened
74          *                  successfully.
75          */
76         bool Attach(const char* fontFilePath);
77 
78         /**
79          * Attach auxilliary data to font (e.g., font metrics) from memory
80          *
81          * @param pBufferBytes  the in-memory buffer
82          * @param bufferSizeInBytes  the length of the buffer in bytes
83          * @return          <code>true</code> if file has opened
84          *                  successfully.
85          */
86         bool Attach(const unsigned char *pBufferBytes,
87                     size_t bufferSizeInBytes);
88 
89         /**
90          * Get the freetype face object..
91          *
92          * @return pointer to an FT_Face.
93          */
Face()94         FT_Face* Face() const { return ftFace; }
95 
96         /**
97          * Sets the char size for the current face.
98          *
99          * This doesn't guarantee that the size was set correctly. Clients
100          * should check errors.
101          *
102          * @param size      the face size in points (1/72 inch)
103          * @param res       the resolution of the target device.
104          * @return          <code>FTSize</code> object
105          */
106         const FTSize& Size(const unsigned int size, const unsigned int res);
107 
108         /**
109          * Get the number of character maps in this face.
110          *
111          * @return character map count.
112          */
113         unsigned int CharMapCount() const;
114 
115         /**
116          * Get a list of character maps in this face.
117          *
118          * @return pointer to the first encoding.
119          */
120         FT_Encoding* CharMapList();
121 
122         /**
123          * Gets the kerning vector between two glyphs
124          */
125         FTPoint KernAdvance(unsigned int index1, unsigned int index2);
126 
127         /**
128          * Loads and creates a Freetype glyph.
129          */
130         FT_GlyphSlot Glyph(unsigned int index, FT_Int load_flags);
131 
132         /**
133          * Gets the number of glyphs in the current face.
134          */
GlyphCount()135         unsigned int GlyphCount() const { return numGlyphs; }
136 
137         /**
138          * Queries for errors.
139          *
140          * @return  The current error code.
141          */
Error()142         FT_Error Error() const { return err; }
143 
144     private:
145         /**
146          * The Freetype face
147          */
148         FT_Face* ftFace;
149 
150         /**
151          * The size object associated with this face
152          */
153         FTSize  charSize;
154 
155         /**
156          * The number of glyphs in this face
157          */
158         int numGlyphs;
159 
160         FT_Encoding* fontEncodingList;
161 
162         /**
163          * This face has kerning tables
164          */
165         bool hasKerningTable;
166 
167         /**
168          * If this face has kerning tables, we can cache them.
169          */
170         void BuildKerningCache();
171         static const unsigned int MAX_PRECOMPUTED = 128;
172         FTGL_DOUBLE* kerningCache;
173 
174         /**
175          * Current error code. Zero means no error.
176          */
177         FT_Error err;
178 };
179 
180 
181 #endif  //  __FTFace__
182