Hash :
dde5c75a
Author :
Date :
2015-02-18T15:28:10
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
/*
* FTGL - OpenGL font library
*
* Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
* Copyright (c) 2008 Éric Beets <ericbeets@free.fr>
* Copyright (c) 2008 Sam Hocevar <sam@hocevar.net>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include "FTInternals.h"
#include "FTVectoriser.h"
#ifndef CALLBACK
#define CALLBACK
#endif
#if defined __APPLE_CC__ && __APPLE_CC__ < 5465
typedef GLvoid (*GLUTesselatorFunction) (...);
#elif defined _WIN32 && !defined __CYGWIN__
typedef GLvoid (CALLBACK *GLUTesselatorFunction) ();
#else
typedef GLvoid (*GLUTesselatorFunction) ();
#endif
void CALLBACK ftglError(GLenum errCode, FTMesh* mesh)
{
mesh->Error(errCode);
}
void CALLBACK ftglVertex(void* data, FTMesh* mesh)
{
FTGL_DOUBLE* vertex = static_cast<FTGL_DOUBLE*>(data);
mesh->AddPoint(vertex[0], vertex[1], vertex[2]);
}
void CALLBACK ftglCombine(FTGL_DOUBLE coords[3], void* vertex_data[4], GLfloat weight[4], void** outData, FTMesh* mesh)
{
(void)vertex_data; (void)weight;
const FTGL_DOUBLE* vertex = static_cast<const FTGL_DOUBLE*>(coords);
*outData = const_cast<FTGL_DOUBLE*>(mesh->Combine(vertex[0], vertex[1], vertex[2]));
}
void CALLBACK ftglBegin(GLenum type, FTMesh* mesh)
{
mesh->Begin(type);
}
void CALLBACK ftglEnd(FTMesh* mesh)
{
mesh->End();
}
FTMesh::FTMesh()
: currentTesselation(0),
err(0)
{
tesselationList.reserve(16);
}
FTMesh::~FTMesh()
{
for(size_t t = 0; t < tesselationList.size(); ++t)
{
delete tesselationList[t];
}
tesselationList.clear();
}
void FTMesh::AddPoint(const FTGL_DOUBLE x, const FTGL_DOUBLE y, const FTGL_DOUBLE z)
{
currentTesselation->AddPoint(x, y, z);
}
const FTGL_DOUBLE* FTMesh::Combine(const FTGL_DOUBLE x, const FTGL_DOUBLE y, const FTGL_DOUBLE z)
{
tempPointList.push_back(FTPoint(x, y,z));
return static_cast<const FTGL_DOUBLE*>(tempPointList.back());
}
void FTMesh::Begin(GLenum meshType)
{
currentTesselation = new FTTesselation(meshType);
}
void FTMesh::End()
{
tesselationList.push_back(currentTesselation);
}
FTTesselation const * FTMesh::Tesselation(size_t index) const
{
return (index < tesselationList.size()) ? tesselationList[index] : NULL;
}
FTVectoriser::FTVectoriser(const FT_GlyphSlot glyph)
: contourList(0),
mesh(0),
ftContourCount(0),
contourFlag(0)
{
if(glyph)
{
outline = glyph->outline;
ftContourCount = outline.n_contours;
contourList = 0;
contourFlag = outline.flags;
ProcessContours();
}
}
FTVectoriser::~FTVectoriser()
{
for(size_t c = 0; c < ContourCount(); ++c)
{
delete contourList[c];
}
delete [] contourList;
delete mesh;
}
void FTVectoriser::ProcessContours()
{
short contourLength = 0;
short startIndex = 0;
short endIndex = 0;
contourList = new FTContour*[ftContourCount];
for(int i = 0; i < ftContourCount; ++i)
{
FT_Vector* pointList = &outline.points[startIndex];
char* tagList = &outline.tags[startIndex];
endIndex = outline.contours[i];
contourLength = (endIndex - startIndex) + 1;
FTContour* contour = new FTContour(pointList, tagList, contourLength);
contourList[i] = contour;
startIndex = endIndex + 1;
}
// Compute each contour's parity. FIXME: see if FT_Outline_Get_Orientation
// can do it for us.
for(int i = 0; i < ftContourCount; i++)
{
FTContour *c1 = contourList[i];
// 1. Find the leftmost point.
FTPoint leftmost(65536.0, 0.0);
for(size_t n = 0; n < c1->PointCount(); n++)
{
FTPoint p = c1->Point(n);
if(p.X() < leftmost.X())
{
leftmost = p;
}
}
// 2. Count how many other contours we cross when going further to
// the left.
int parity = 0;
for(int j = 0; j < ftContourCount; j++)
{
if(j == i)
{
continue;
}
FTContour *c2 = contourList[j];
for(size_t n = 0; n < c2->PointCount(); n++)
{
FTPoint p1 = c2->Point(n);
FTPoint p2 = c2->Point((n + 1) % c2->PointCount());
/* FIXME: combinations of >= > <= and < do not seem stable */
if((p1.Y() < leftmost.Y() && p2.Y() < leftmost.Y())
|| (p1.Y() >= leftmost.Y() && p2.Y() >= leftmost.Y())
|| (p1.X() > leftmost.X() && p2.X() > leftmost.X()))
{
continue;
}
else if(p1.X() < leftmost.X() && p2.X() < leftmost.X())
{
parity++;
}
else
{
FTPoint a = p1 - leftmost;
FTPoint b = p2 - leftmost;
if(b.X() * a.Y() > b.Y() * a.X())
{
parity++;
}
}
}
}
// 3. Make sure the glyph has the proper parity.
c1->SetParity(parity);
}
}
size_t FTVectoriser::PointCount()
{
size_t s = 0;
for(size_t c = 0; c < ContourCount(); ++c)
{
s += contourList[c]->PointCount();
}
return s;
}
FTContour const * FTVectoriser::Contour(size_t index) const
{
return (index < ContourCount()) ? contourList[index] : NULL;
}
void FTVectoriser::MakeMesh(FTGL_DOUBLE zNormal, int outsetType, float outsetSize)
{
if(mesh)
{
delete mesh;
}
mesh = new FTMesh;
GLUtesselator* tobj = gluNewTess();
gluTessCallback(tobj, GLU_TESS_BEGIN_DATA, (GLUTesselatorFunction)ftglBegin);
gluTessCallback(tobj, GLU_TESS_VERTEX_DATA, (GLUTesselatorFunction)ftglVertex);
gluTessCallback(tobj, GLU_TESS_COMBINE_DATA, (GLUTesselatorFunction)ftglCombine);
gluTessCallback(tobj, GLU_TESS_END_DATA, (GLUTesselatorFunction)ftglEnd);
gluTessCallback(tobj, GLU_TESS_ERROR_DATA, (GLUTesselatorFunction)ftglError);
if(contourFlag & ft_outline_even_odd_fill) // ft_outline_reverse_fill
{
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
}
else
{
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO);
}
gluTessProperty(tobj, GLU_TESS_TOLERANCE, 0);
gluTessNormal(tobj, 0.0f, 0.0f, zNormal);
gluTessBeginPolygon(tobj, mesh);
for(size_t c = 0; c < ContourCount(); ++c)
{
/* Build the */
switch(outsetType)
{
case 1 : contourList[c]->buildFrontOutset(outsetSize); break;
case 2 : contourList[c]->buildBackOutset(outsetSize); break;
}
const FTContour* contour = contourList[c];
gluTessBeginContour(tobj);
for(size_t p = 0; p < contour->PointCount(); ++p)
{
const FTGL_DOUBLE* d;
switch(outsetType)
{
case 1: d = contour->FrontPoint(p); break;
case 2: d = contour->BackPoint(p); break;
case 0: default: d = contour->Point(p); break;
}
// XXX: gluTessVertex doesn't modify the data but does not
// specify "const" in its prototype, so we cannot cast to
// a const type.
gluTessVertex(tobj, const_cast<GLdouble*>(d), (GLvoid *)d);
}
gluTessEndContour(tobj);
}
gluTessEndPolygon(tobj);
gluDeleteTess(tobj);
}