File: FTGlyphPolygonizer.C

package info (click to toggle)
gltt 1.7-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 548 kB
  • ctags: 482
  • sloc: cpp: 4,537; sh: 1,733; makefile: 140
file content (295 lines) | stat: -rw-r--r-- 7,736 bytes parent folder | download | duplicates (2)
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
/*
 * gltt graphics library
 * Copyright (C) 1998 Stephane Rehel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <math.h>

// Reported by <da@skivs.ski.org>
#ifdef WIN32
#include <memory.h>
#endif

#include "FTGlyphPolygonizer.h"
#include "FTGlyph.h"

#include "freetype.h"

#include "GLTTminmax.h"

/////////////////////////////////////////////////////////////////////////////

FTGlyphPolygonizer::FTGlyphPolygonizer()
{
  glyph= 0;

  metrics= new TT_Glyph_Metrics;
  outline= new TT_Outline;

  max_points= 16;
  points= new POINT [ max_points ];
  nPoints= 0;

  precision= 4.;
}

/////////////////////////////////////////////////////////////////////////////

FTGlyphPolygonizer::~FTGlyphPolygonizer()
{
  delete outline;
  outline= 0;

  delete metrics;
  metrics= 0;

  delete points;
  points= 0;
  nPoints= 0;
  max_points= 0;
}

/////////////////////////////////////////////////////////////////////////////

GLTTboolean FTGlyphPolygonizer::init( FTGlyph* _glyph )
{
  glyph= _glyph;

  nContours= 0;
  cur_contour= 0;
  nPoints= 0;

  if( glyph == 0 )
    return 0;

  TT_Error error;

  error= TT_Get_Glyph_Metrics( *glyph->getGlyph(), metrics );
  if( error )
    return GLTT_FALSE;

  error= TT_Get_Glyph_Outline( *glyph->getGlyph(), outline );
  if( error )
    return GLTT_FALSE;

  nContours= outline->contours;

  nPoints= 0;

  return GLTT_TRUE;
}

/////////////////////////////////////////////////////////////////////////////

void FTGlyphPolygonizer::setPrecision( double _precision )
{
  precision= max( _precision, 0.01 );
}

/////////////////////////////////////////////////////////////////////////////

// 0 <= c < nContours
GLTTboolean FTGlyphPolygonizer::getContour( int c )
{
  if( c < 0 || c >= nContours )
    return GLTT_FALSE;

  cur_contour= c;

#define BEZIER(x1,y1,x2,y2,x3,y3)                                   \
          {                                                         \
          double extent= max( fabs(max(x1,x2,x3) - min(x1,x2,x3)),  \
                              fabs(max(y1,y2,y3) - min(y1,y2,y3)) );\
          double _subdivisions= extent / precision + .5;            \
          int subdivisions= int(_subdivisions);                     \
          if( subdivisions > 0 )                                    \
            {                                                       \
            add_point( x1, y1 );                                    \
            double dt= 1. / double(subdivisions);                   \
            register double t= dt;                                  \
            for( int a= 1; a < subdivisions; ++a, t+= dt )          \
              {                                                     \
              register double tt= 1. - t;                           \
              register double t1= tt * tt;                          \
              register double t2= 2. * t * tt;                      \
              register double t3= t * t;                            \
              register double x= t1 * (x1) + t2 * (x2) + t3 * (x3); \
              register double y= t1 * (y1) + t2 * (y2) + t3 * (y3); \
              add_point(x,y);                                       \
              }                                                     \
            }                                                       \
          }

  nPoints= 0;

  int first= (c==0) ? 0 : (outline->conEnds[c-1]+1);
  int last= outline->conEnds[c];

  int k1= first;
  int k2= k1+1;
  int on1= (outline->flag[k1] & 1);
  int on2= (outline->flag[k2] & 1);

  const double scale= 1. / 64.;
  double x1= double(outline->xCoord[k1]) * scale;
  double y1= double(outline->yCoord[k1]) * scale;
  double x2= double(outline->xCoord[k2]) * scale;
  double y2= double(outline->yCoord[k2]) * scale;
  int skip_next= 0;

  for( int k= first+1; k <= last; ++k )
    {
    int k3= (k==last) ? first : (k+1);
    int on3= (outline->flag[k3] & 1);
    double x3= double(outline->xCoord[k3]) * scale;
    double y3= double(outline->yCoord[k3]) * scale;

    if( ! skip_next )
      {
      if( on1 )
        {
        if( on2 )
          {
          add_point(x1,y1);
          if( k == last )
            add_point(x2,y2);
          }
         else
          {
          if( on3 )
            {
            BEZIER(x1,y1,x2,y2,x3,y3);
            if( k == last-1 )
              add_point(x3,y3);
            skip_next= 1;
            }
           else
            {
            double x23= (x2+x3) * .5;
            double y23= (y2+y3) * .5;
            BEZIER(x1,y1,x2,y2,x23,y23);
            }
          }
        }
       else
        {
        if( on2 )
          {
          }
         else
          {
          if( on3 )
            {
            double x12= (x1+x2) * .5;
            double y12= (y1+y2) * .5;
            BEZIER(x12,y12,x2,y2,x3,y3);
            if( k == last-1 )
              add_point(x3,y3);
            skip_next= 1;
            }
           else
            {
            double x12= (x1+x2) * .5;
            double y12= (y1+y2) * .5;
            double x23= (x2+x3) * .5;
            double y23= (y2+y3) * .5;
            BEZIER(x12,y12,x2,y2,x23,y23);
            }
          }
        }
      }

    k1= k2; k2= k3;
    x1= x2; x2= x3;
    y1= y2; y2= y3;
    on1=on2;on2=on3;
    skip_next= 0;
    }

#undef BEZIER

  return GLTT_TRUE;
}

/////////////////////////////////////////////////////////////////////////////

// 0 <= i < nPoints
FTGlyphPolygonizer::POINT* FTGlyphPolygonizer::getContourPoint( int i ) const
{
  if( i < 0 || i >= nPoints || points == 0 )
    return 0;

  return points + i;
}

/////////////////////////////////////////////////////////////////////////////

// 0 <= i < nPoints
void FTGlyphPolygonizer::getContourPoint( int i, double& x, double& y ) const
{
  POINT* p= getContourPoint(i);
  if( p == 0 )
    {
    x= y= 0.;
    return;
    }

  x= p[i].x;
  y= p[i].y;
}

/////////////////////////////////////////////////////////////////////////////

void FTGlyphPolygonizer::add_point( double x, double y )
{
  if( nPoints >= max_points )
    {
    int new_max_points= max_points + max_points / 2;
    POINT* new_points= new POINT [ new_max_points ];
    memcpy( (void*) new_points, (void*) points, nPoints * sizeof(points[0]) );
    points= new_points;
    max_points= new_max_points;
    }

  points[nPoints].x= x;
  points[nPoints].y= y;

  ++nPoints;
}

/////////////////////////////////////////////////////////////////////////////

double FTGlyphPolygonizer::getBearingX() const
{
  return (metrics==0) ? 0 : (double(metrics->bearingX)/64.);
}

/////////////////////////////////////////////////////////////////////////////

double FTGlyphPolygonizer::getBearingY() const
{
  return (metrics==0) ? 0 : (double(metrics->bearingY)/64.);
}

/////////////////////////////////////////////////////////////////////////////

double FTGlyphPolygonizer::getAdvance() const
{
  return (metrics==0) ? 0. : (double(metrics->advance)/64.);
}

/////////////////////////////////////////////////////////////////////////////