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@hocevar.net>
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 #ifndef     __FTVectoriser__
28 #define     __FTVectoriser__
29 
30 #include "FTGL/ftgl.h"
31 
32 #include "FTContour.h"
33 #include "FTList.h"
34 #include "FTVector.h"
35 
36 
37 #ifndef CALLBACK
38 #define CALLBACK
39 #endif
40 
41 
42 /**
43  * FTTesselation captures points that are output by OpenGL's gluTesselator.
44  */
45 class FTTesselation
46 {
47     public:
48         /**
49          * Default constructor
50          */
FTTesselation(GLenum m)51         FTTesselation(GLenum m)
52         : meshType(m)
53         {
54             pointList.reserve(128);
55         }
56 
57         /**
58          *  Destructor
59          */
~FTTesselation()60         ~FTTesselation()
61         {
62             pointList.clear();
63         }
64 
65         /**
66          * Add a point to the mesh.
67          */
AddPoint(const FTGL_DOUBLE x,const FTGL_DOUBLE y,const FTGL_DOUBLE z)68         void AddPoint(const FTGL_DOUBLE x, const FTGL_DOUBLE y,
69                       const FTGL_DOUBLE z)
70         {
71             pointList.push_back(FTPoint(x, y, z));
72         }
73 
74         /**
75          * The number of points in this mesh
76          */
PointCount()77         size_t PointCount() const { return pointList.size(); }
78 
79         /**
80          *
81          */
Point(unsigned int index)82         const FTPoint& Point(unsigned int index) const
83         { return pointList[index]; }
84 
85         /**
86          * Return the OpenGL polygon type.
87          */
PolygonType()88         GLenum PolygonType() const { return meshType; }
89 
90     private:
91         /**
92          * Points generated by gluTesselator.
93          */
94         typedef FTVector<FTPoint> PointVector;
95         PointVector pointList;
96 
97         /**
98          * OpenGL primitive type from gluTesselator.
99          */
100         GLenum meshType;
101 };
102 
103 
104 /**
105  * FTMesh is a container of FTTesselation's that make up a polygon glyph
106  */
107 class FTMesh
108 {
109         typedef FTVector<FTTesselation*> TesselationVector;
110         typedef FTList<FTPoint> PointList;
111 
112     public:
113         /**
114          * Default constructor
115          */
116         FTMesh();
117 
118         /**
119          *  Destructor
120          */
121         ~FTMesh();
122 
123         /**
124          * Add a point to the mesh
125          */
126         void AddPoint(const FTGL_DOUBLE x, const FTGL_DOUBLE y,
127                       const FTGL_DOUBLE z);
128 
129         /**
130          *  Create a combine point for the gluTesselator
131          */
132         const FTGL_DOUBLE* Combine(const FTGL_DOUBLE x, const FTGL_DOUBLE y,
133                                    const FTGL_DOUBLE z);
134 
135         /**
136          * Begin a new polygon
137          */
138         void Begin(GLenum meshType);
139 
140         /**
141          * End a polygon
142          */
143         void End();
144 
145         /**
146          * Record a gluTesselation error
147          */
Error(GLenum e)148         void Error(GLenum e) { err = e; }
149 
150         /**
151          * The number of tesselations in the mesh
152          */
TesselationCount()153         size_t TesselationCount() const { return tesselationList.size(); }
154 
155         /**
156          * Get a tesselation by index
157          */
158         FTTesselation const * Tesselation(size_t index) const;
159 
160         /**
161          * Return the temporary point list. For testing only.
162          */
TempPointList()163         const PointList& TempPointList() const { return tempPointList; }
164 
165         /**
166          * Get the GL ERROR returned by the glu tesselator
167          */
Error()168         GLenum Error() const { return err; }
169 
170     private:
171         /**
172          * The current sub mesh that we are constructing.
173          */
174         FTTesselation* currentTesselation;
175 
176         /**
177          * Holds each sub mesh that comprises this glyph.
178          */
179         TesselationVector tesselationList;
180 
181         /**
182          * Holds extra points created by gluTesselator. See ftglCombine.
183          */
184         PointList tempPointList;
185 
186         /**
187          * GL ERROR returned by the glu tesselator
188          */
189         GLenum err;
190 
191 };
192 
193 const FTGL_DOUBLE FTGL_FRONT_FACING = 1.0;
194 const FTGL_DOUBLE FTGL_BACK_FACING = -1.0;
195 
196 /**
197  * FTVectoriser class is a helper class that converts font outlines into
198  * point data.
199  *
200  * @see FTExtrudeGlyph
201  * @see FTOutlineGlyph
202  * @see FTPolygonGlyph
203  * @see FTContour
204  * @see FTPoint
205  *
206  */
207 class FTVectoriser
208 {
209     public:
210         /**
211          * Constructor
212          *
213          * @param glyph The freetype glyph to be processed
214          */
215         FTVectoriser(const FT_GlyphSlot glyph);
216 
217         /**
218          *  Destructor
219          */
220         virtual ~FTVectoriser();
221 
222         /**
223          * Build an FTMesh from the vector outline data.
224          *
225          * @param zNormal   The direction of the z axis of the normal
226          *                  for this mesh
227          * FIXME: change the following for a constant
228          * @param outsetType Specify the outset type contour
229          *  0 : Original
230          *  1 : Front
231          *  2 : Back
232          * @param outsetSize Specify the outset size contour
233          */
234         void MakeMesh(FTGL_DOUBLE zNormal = FTGL_FRONT_FACING, int outsetType = 0, float outsetSize = 0.0f);
235 
236         /**
237          * Get the current mesh.
238          */
GetMesh()239         FTMesh const * GetMesh() const { return mesh; }
240 
241         /**
242          * Get the total count of points in this outline
243          *
244          * @return the number of points
245          */
246         size_t PointCount();
247 
248         /**
249          * Get the count of contours in this outline
250          *
251          * @return the number of contours
252          */
ContourCount()253         size_t ContourCount() const { return ftContourCount; }
254 
255         /**
256          * Return a contour at index
257          *
258          * @return the number of contours
259          */
260          FTContour const * Contour(size_t index) const;
261 
262         /**
263          * Get the number of points in a specific contour in this outline
264          *
265          * @param c     The contour index
266          * @return      the number of points in contour[c]
267          */
ContourSize(int c)268         size_t ContourSize(int c) const { return contourList[c]->PointCount(); }
269 
270         /**
271          * Get the flag for the tesselation rule for this outline
272          *
273          * @return The contour flag
274          */
ContourFlag()275         int ContourFlag() const { return contourFlag; }
276 
277     private:
278         /**
279          * Process the freetype outline data into contours of points
280          *
281          * @param front front outset distance
282          * @param back back outset distance
283          */
284         void ProcessContours();
285 
286         /**
287          * The list of contours in the glyph
288          */
289         FTContour** contourList;
290 
291         /**
292          * A Mesh for tesselations
293          */
294         FTMesh* mesh;
295 
296         /**
297          * The number of contours reported by Freetype
298          */
299         short ftContourCount;
300 
301         /**
302          * A flag indicating the tesselation rule for the glyph
303          */
304         int contourFlag;
305 
306         /**
307          * A Freetype outline
308          */
309         FT_Outline outline;
310 };
311 
312 
313 #endif  //  __FTVectoriser__
314