Hash :
35301593
Author :
Date :
2018-01-29T03:55:37
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
/*
* FTGL - OpenGL font library
*
* Copyright (c) 2001-2004 Henry Maddocks <ftgl@opengl.geek.nz>
* Copyright (c) 2008 Sam Hocevar <sam@hocevar.net>
* Copyright (c) 2008 Éric Beets <ericbeets@free.fr>
*
* 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 "FTContour.h"
#include <math.h>
static const unsigned int BEZIER_STEPS = 5;
void FTContour::AddPoint(FTPoint point)
{
if(pointList.empty() || (point != pointList[pointList.size() - 1]
&& point != pointList[0]))
{
pointList.push_back(point);
}
}
void FTContour::AddOutsetPoint(FTPoint point)
{
outsetPointList.push_back(point);
}
void FTContour::AddFrontPoint(FTPoint point)
{
frontPointList.push_back(point);
}
void FTContour::AddBackPoint(FTPoint point)
{
backPointList.push_back(point);
}
void FTContour::evaluateQuadraticCurve(FTPoint A, FTPoint B, FTPoint C)
{
for(unsigned int i = 1; i < BEZIER_STEPS; i++)
{
float t = static_cast<float>(i) / BEZIER_STEPS;
FTPoint U = (1.0f - t) * A + t * B;
FTPoint V = (1.0f - t) * B + t * C;
AddPoint((1.0f - t) * U + t * V);
}
}
void FTContour::evaluateCubicCurve(FTPoint A, FTPoint B, FTPoint C, FTPoint D)
{
for(unsigned int i = 0; i < BEZIER_STEPS; i++)
{
float t = static_cast<float>(i) / BEZIER_STEPS;
FTPoint U = (1.0f - t) * A + t * B;
FTPoint V = (1.0f - t) * B + t * C;
FTPoint W = (1.0f - t) * C + t * D;
FTPoint M = (1.0f - t) * U + t * V;
FTPoint N = (1.0f - t) * V + t * W;
AddPoint((1.0f - t) * M + t * N);
}
}
// This function is a bit tricky. Given a path ABC, it returns the
// coordinates of the outset point facing B on the left at a distance
// of 64.0.
// M
// - - - - - - X
// ^ / '
// | 64.0 / '
// X---->-----X ==> X--v-------X '
// A B \ A B \ .>'
// \ \<' 64.0
// \ \ .
// \ \ .
// C X C X
//
FTPoint FTContour::ComputeOutsetPoint(FTPoint A, FTPoint B, FTPoint C)
{
// If the angle between 'ab' and 'bc' approaches 180 degrees,
// the outset point goes to infinity, giving an invalid result.
// Even for angles near 180 degrees, the point will be quite
// far away from A, B and C. To avoid ugly results, limit
// its distance to 64.0 * OutsetMax.
static const FTGL_DOUBLE OutsetMax = 5;
/* Build the rotation matrix from 'ba' vector */
FTPoint ba = (A - B).Normalise();
FTPoint bc = C - B;
/* Rotate bc to the left */
FTPoint tmp(bc.X() * -ba.X() + bc.Y() * -ba.Y(),
bc.X() * ba.Y() + bc.Y() * -ba.X());
/* Compute the vector bisecting 'abc' */
FTGL_DOUBLE norm = sqrt(tmp.X() * tmp.X() + tmp.Y() * tmp.Y());
FTGL_DOUBLE dist;
if (norm - tmp.X() > (norm + tmp.X()) * OutsetMax * OutsetMax)
dist = 64.0 * OutsetMax;
else
dist = 64.0 * sqrt((norm - tmp.X()) / (norm + tmp.X()));
tmp.X(tmp.Y() < 0.0 ? dist : -dist);
tmp.Y(64.0);
/* Rotate the new bc to the right */
return FTPoint(tmp.X() * -ba.X() + tmp.Y() * ba.Y(),
tmp.X() * -ba.Y() + tmp.Y() * -ba.X());
}
void FTContour::SetParity(int parity)
{
size_t size = PointCount();
FTPoint vOutset;
if(((parity & 1) && clockwise) || (!(parity & 1) && !clockwise))
{
// Contour orientation is wrong! We must reverse all points.
// FIXME: could it be worth writing FTVector::reverse() for this?
for(size_t i = 0; i < size / 2; i++)
{
FTPoint tmp = pointList[i];
pointList[i] = pointList[size - 1 - i];
pointList[size - 1 -i] = tmp;
}
clockwise = !clockwise;
}
for(size_t i = 0; i < size; i++)
{
size_t prev, cur, next;
prev = (i + size - 1) % size;
cur = i;
next = (i + size + 1) % size;
vOutset = ComputeOutsetPoint(Point(prev), Point(cur), Point(next));
AddOutsetPoint(vOutset);
}
}
FTContour::FTContour(FT_Vector* contour, char* tags, unsigned int n)
{
FTPoint prev, cur(contour[(n - 1) % n]), next(contour[0]);
FTPoint a, b = next - cur;
double olddir, dir = atan2((next - cur).Y(), (next - cur).X());
double angle = 0.0;
// See http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-6.html
// for a full description of FreeType tags.
for(unsigned int i = 0; i < n; i++)
{
prev = cur;
cur = next;
next = FTPoint(contour[(i + 1) % n]);
olddir = dir;
dir = atan2((next - cur).Y(), (next - cur).X());
// Compute our path's new direction.
double t = dir - olddir;
if(t < -M_PI) t += 2 * M_PI;
if(t > M_PI) t -= 2 * M_PI;
angle += t;
// Only process point tags we know.
if(n < 2 || FT_CURVE_TAG(tags[i]) == FT_Curve_Tag_On)
{
AddPoint(cur);
}
else if(FT_CURVE_TAG(tags[i]) == FT_Curve_Tag_Conic)
{
FTPoint prev2 = prev, next2 = next;
// Previous point is either the real previous point (an "on"
// point), or the midpoint between the current one and the
// previous "conic off" point.
if(FT_CURVE_TAG(tags[(i - 1 + n) % n]) == FT_Curve_Tag_Conic)
{
prev2 = (cur + prev) * 0.5;
AddPoint(prev2);
}
// Next point is either the real next point or the midpoint.
if(FT_CURVE_TAG(tags[(i + 1) % n]) == FT_Curve_Tag_Conic)
{
next2 = (cur + next) * 0.5;
}
evaluateQuadraticCurve(prev2, cur, next2);
}
else if(FT_CURVE_TAG(tags[i]) == FT_Curve_Tag_Cubic
&& FT_CURVE_TAG(tags[(i + 1) % n]) == FT_Curve_Tag_Cubic)
{
evaluateCubicCurve(prev, cur, next,
FTPoint(contour[(i + 2) % n]));
}
}
// If final angle is positive (+2PI), it's an anti-clockwise contour,
// otherwise (-2PI) it's clockwise.
clockwise = (angle < 0.0);
}
void FTContour::buildFrontOutset(float outset)
{
frontPointList.clear();
for(size_t i = 0; i < PointCount(); ++i)
{
AddFrontPoint(Point(i) + Outset(i) * outset);
}
}
void FTContour::buildBackOutset(float outset)
{
backPointList.clear();
for(size_t i = 0; i < PointCount(); ++i)
{
AddBackPoint(Point(i) + Outset(i) * outset);
}
}