]> Shamusworld >> Repos - ttedit/blob - src/glyphpoints.cpp
0b0575253914002506c9186279f2b0a2c37a9d1b
[ttedit] / src / glyphpoints.cpp
1 //
2 // GLYPHPOINTS.CPP
3 //
4 // Class implementation. Nothing too inexplicable going on here. Fairly
5 // straightforward stuff.
6 //
7 // by James L. Hammons
8 // (C) 2004 Underground Software
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  ------------------------------------------------------------
14 // JLH  ??/??/200?  Created this file
15 // JLH  05/18/2004  Added pure point adding, inserting, better polygon handling
16 //
17
18 // Uncomment this for debugging...
19 #define DEBUG
20
21 #include "glyphpoints.h"
22 #ifdef DEBUG
23 #include "debug.h"
24 #endif
25 #include <math.h>
26
27
28 /*GlyphPoints::GlyphPoints(void)
29 {
30         GlyphPoints(0, 0, NULL, NULL, NULL, NULL);
31 }*/
32
33 GlyphPoints::GlyphPoints(int nPts/*=0*/, int nPlys/*=0*/, int * xa/*=null*/, int * ya/*=null*/,
34         bool * oca/*=null*/, uint16_t * pa/*=null*/): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
35 //GlyphPoints::GlyphPoints(int nPts, int nPlys/*=0*/, int * xa/*=null*/, int * ya/*=null*/,
36 //      bool * oca/*=null*/, uint16_t * pa/*=null*/): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
37 {
38         AllocateAndCopy(nPts, nPlys, xa, ya, oca, pa);
39
40         if (nPlys == 0)
41         {
42                 numPolys = 1;
43                 polyEnd = new uint16_t[numPolys];
44                 polyEnd[0] = numPoints - 1;
45         }
46 #ifdef DEBUG
47 WriteLogMsg("GlyphPoints: Default constructor. %u points, %u polys.\n", numPoints, numPolys);
48 #endif
49 }
50
51
52 GlyphPoints::GlyphPoints(int xx, int yy, bool oc)
53 {
54 //Hmm. What to do with this...?
55         AllocateAndCopy(1, 0, &xx, &yy, &oc, NULL);
56 #ifdef DEBUG
57 WriteLogMsg("GlyphPoints: Single point constructor. %u points, %u polys.\n", numPoints, numPolys);
58 #endif
59 }
60
61
62 // Copy constructor (needed for deep copying)
63
64 //GlyphPoints::GlyphPoints(GlyphPoints &c): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
65 GlyphPoints::GlyphPoints(const GlyphPoints &c): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
66 {
67         *this = c;                                                                      // Use overloaded operator=
68 #ifdef DEBUG
69 WriteLogMsg("GlyphPoints: Copy constructor. %u points, %u polys.\n", numPoints, numPolys);
70 #endif
71 }
72
73
74 GlyphPoints::~GlyphPoints()
75 {
76         FreeAllocatedMemory();
77 #if 0
78         if (x)
79                 delete[] x;
80
81         if (y)
82                 delete[] y;
83
84         if (onCurve)
85                 delete[] onCurve;
86
87         if (polyEnd)
88                 delete[] polyEnd;
89 #endif
90 }
91
92
93 void GlyphPoints::AllocateAndCopy(int nPts, int nPlys, int * xa, int * ya, bool * oca, uint16_t * pa)
94 {
95         numPoints = nPts, numPolys = nPlys;
96
97         if (nPts)
98         {
99                 x = new int[numPoints];
100                 y = new int[numPoints];
101                 onCurve = new bool[numPoints];
102
103                 if (xa)                                                                 // Copy points in if they're passed in...
104                         for(int i=0; i<nPts; i++)
105                                 x[i] = xa[i];
106
107                 if (ya)
108                         for(int i=0; i<nPts; i++)
109                                 y[i] = ya[i];
110
111                 if (oca)
112                         for(int i=0; i<nPts; i++)
113                                 onCurve[i] = oca[i];
114         }
115
116         if (numPolys)
117         {
118                 polyEnd = new uint16_t[numPolys];
119
120                 if (pa)                                                                 // Copy poly ends in if they're passed in...
121                         for(int i=0; i<nPlys; i++)
122                                 polyEnd[i] = pa[i];
123         }
124 }
125
126
127 void GlyphPoints::FreeAllocatedMemory(void)
128 {
129         if (x)
130                 delete[] x;
131
132         if (y)
133                 delete[] y;
134
135         if (onCurve)
136                 delete[] onCurve;
137
138         if (polyEnd)
139                 delete[] polyEnd;
140 }
141
142
143 GlyphPoints& GlyphPoints::operator=(const GlyphPoints &c)
144 {
145         if (this == &c)
146                 return *this;                                                   // Take care of self-assignment
147
148         FreeAllocatedMemory();
149         AllocateAndCopy(c.numPoints, c.numPolys, c.x, c.y, c.onCurve, c.polyEnd);
150
151         return *this;
152 }
153
154
155 // Add another GlyphPoints' points to this one...
156
157 GlyphPoints GlyphPoints::operator+(const GlyphPoints &c)
158 {
159         int totPoints = numPoints + c.numPoints, totPolys = numPolys + c.numPolys;
160
161         int * totX = new int[totPoints];
162         int * totY = new int[totPoints];
163         bool * totOnCurve = new bool[totPoints];
164         uint16_t * totPolyEnd = new uint16_t[totPolys];
165
166         for(int i=0; i<numPoints; i++)
167         {
168                 totX[i] = x[i];
169                 totY[i] = y[i];
170                 totOnCurve[i] = onCurve[i];
171         }
172
173         for(int i=0; i<numPolys; i++)
174                 totPolyEnd[i] = polyEnd[i];
175
176         for(int i=0; i<c.numPoints; i++)
177         {
178                 totX[numPoints+i] = c.x[i];
179                 totY[numPoints+i] = c.y[i];
180                 totOnCurve[numPoints+i] = c.onCurve[i];
181         }
182
183         for(int i=0; i<c.numPolys; i++)
184                 totPolyEnd[numPolys+i] = numPoints + c.polyEnd[i];  // Need to adjust the "added in"
185                                                         // poly's end points...
186
187         GlyphPoints retVal(totPoints, totPolys, totX, totY, totOnCurve, totPolyEnd);
188         delete[] totX;
189         delete[] totY;
190         delete[] totOnCurve;
191         delete[] totPolyEnd;
192
193         return retVal;
194 }
195
196
197 // Add a point to this GlyphPoints...
198
199 GlyphPoints GlyphPoints::operator+(const IPoint &c)
200 {
201 //This is kinda silly. We can do better than this! !!! FIX !!!
202         int * totX = new int[numPoints + 1];
203         int * totY = new int[numPoints + 1];
204         bool * totOnCurve = new bool[numPoints + 1];
205         uint16_t * totPolyEnd = new uint16_t[numPolys];
206
207         for(int i=0; i<numPoints; i++)
208                 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
209
210         totX[numPoints] = c.x, totY[numPoints] = c.y, totOnCurve[numPoints] = c.onCurve;
211
212         for(int i=0; i<numPolys; i++)
213                 totPolyEnd[i] = polyEnd[i];
214
215         totPolyEnd[numPolys - 1]++;                                     // Bump polygon's end point
216         GlyphPoints retVal(numPoints + 1, numPolys, totX, totY, totOnCurve, totPolyEnd);
217
218         delete[] totX;
219         delete[] totY;
220         delete[] totOnCurve;
221
222         return retVal;
223 }
224
225
226 GlyphPoints& GlyphPoints::operator+=(const IPoint &p)
227 {
228         InsertPoint(numPoints, p.x, p.y, p.onCurve);
229
230         return *this;
231 }
232
233
234 void GlyphPoints::Clear(void)
235 {
236         FreeAllocatedMemory();
237         x = y = NULL;
238         onCurve = NULL;
239         polyEnd = NULL;
240         numPoints = numPolys = pointsAllocated = polysAllocated = 0;
241
242         numPolys = 1;
243         polyEnd = new uint16_t[numPolys];
244         polyEnd[0] = numPoints - 1;
245 }
246
247
248 void GlyphPoints::InsertPoint(uint16_t pt, int xx, int yy, bool oc)
249 {
250         if (pt > numPoints)                                                     // > because we can insert at end...!
251                 throw GP_OUT_OF_RANGE;
252
253 //This is kinda silly. We can do better than this! !!! FIX !!!
254         int * totX = new int[numPoints + 1];
255         int * totY = new int[numPoints + 1];
256         bool * totOnCurve = new bool[numPoints + 1];
257
258         for(int i=0; i<pt; i++)
259                 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
260
261         for(int i=pt; i<numPoints; i++)
262                 totX[i + 1] = x[i], totY[i + 1] = y[i], totOnCurve[i + 1] = onCurve[i];
263
264         totX[pt] = xx, totY[pt] = yy, totOnCurve[pt] = oc;
265
266 //A way to fix the kludge in GetPoly() would be to put a check here to see if
267 //we're adding to the end of the structure: [DONE, below]
268         int polyInsert = (pt == numPoints ? numPolys - 1 : GetPoly(pt));
269         for(int i=polyInsert; i<numPolys; i++)
270 //      for(int i=GetPoly(pt); i<numPolys; i++)
271                 polyEnd[i]++;                                                   // Bump polygons' end point
272
273         numPoints++;
274
275         delete[] x;
276         delete[] y;
277         delete[] onCurve;
278
279         x = totX, y = totY, onCurve = totOnCurve;
280 }
281
282
283 void GlyphPoints::InsertPoint(uint16_t pt, const IPoint &p)
284 {
285         InsertPoint(pt, p.x, p.y, p.onCurve);
286 }
287
288
289 //
290 // Delete a point from the glyph
291 // Note that we don't bother to reallocate anything here, just bump the
292 // size counters down as needed. In the future, we'll keep track so we
293 // don't have to reallocate *every* damn time a point is added...
294 //
295 void GlyphPoints::DeletePoint(uint16_t pt)
296 {
297         // Adjust polygon ends appropriately
298         uint16_t poly = GetPoly(pt);
299
300         for(int i=poly; i<numPolys; i++)
301                 polyEnd[i]--;
302
303 //Need to check here if we're deleting the last point in the glyph or not.
304 //!!! FIX !!! [DONE]
305         if (GetNumPoints(poly) == 0 && numPoints > 0)
306         {
307                 numPolys--;
308
309                 for(int i=poly; i<numPolys; i++)
310                         polyEnd[i] = polyEnd[i + 1];
311         }
312
313 //This causes a crash becuz GetPoly() uses numPoints... !!! FIX !!! [DONE by switching poly delete & point delete]
314         // Close up the gap left by the current point
315         numPoints--;
316
317         for(int i=pt; i<numPoints; i++)
318                 x[i] = x[i + 1], y[i] = y[i + 1], onCurve[i] = onCurve[i + 1];
319 }
320
321
322 uint16_t GlyphPoints::GetNumPoints(void)
323 {
324         return numPoints;
325 }
326
327
328 uint16_t GlyphPoints::GetNumPoints(uint16_t poly)
329 {
330         if (poly >= numPolys)
331 #ifdef DEBUG
332 {
333 WriteLogMsg("Exception: GetNumPoints(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
334 #endif
335                 throw GP_OUT_OF_RANGE;
336 #ifdef DEBUG
337 }
338 #endif
339
340         return polyEnd[poly] - (poly == 0 ? -1 : polyEnd[poly - 1]);
341 }
342
343
344 uint16_t GlyphPoints::GetNumPolys(void)
345 {
346         return numPolys;
347 }
348
349
350 int GlyphPoints::GetX(uint16_t pt)
351 {
352         if (pt >= numPoints)
353 #ifdef DEBUG
354 {
355 WriteLogMsg("Exception: GetX(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
356 #endif
357                 throw GP_OUT_OF_RANGE;
358 #ifdef DEBUG
359 }
360 #endif
361
362         return x[pt];
363 }
364
365
366 int GlyphPoints::GetY(uint16_t pt)
367 {
368         if (pt >= numPoints)
369 #ifdef DEBUG
370 {
371 WriteLogMsg("Exception: GetY(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
372 #endif
373                 throw GP_OUT_OF_RANGE;
374 #ifdef DEBUG
375 }
376 #endif
377
378         return y[pt];
379 }
380
381
382 bool GlyphPoints::GetOnCurve(uint16_t pt)
383 {
384         if (pt >= numPoints)
385 #ifdef DEBUG
386 {
387 WriteLogMsg("Exception: GetOnCurve(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
388 #endif
389                 throw GP_OUT_OF_RANGE;
390 #ifdef DEBUG
391 }
392 #endif
393
394         return onCurve[pt];
395 }
396
397
398 int GlyphPoints::GetX(uint16_t poly, uint16_t pt)
399 {
400         if (pt >= GetNumPoints(poly))
401 #ifdef DEBUG
402 {
403 WriteLogMsg("Exception: GetX(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
404 #endif
405                 throw GP_OUT_OF_RANGE;
406 #ifdef DEBUG
407 }
408 #endif
409
410         return x[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
411 }
412
413
414 int GlyphPoints::GetNextX(uint16_t poly, uint16_t pt)
415 {
416         return GetX(poly, GetNext(poly, pt));
417 }
418
419
420 int GlyphPoints::GetY(uint16_t poly, uint16_t pt)
421 {
422         if (pt >= GetNumPoints(poly))
423 #ifdef DEBUG
424 {
425 WriteLogMsg("Exception: GetY(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
426 #endif
427                 throw GP_OUT_OF_RANGE;
428 #ifdef DEBUG
429 }
430 #endif
431
432         return y[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
433 }
434
435
436 int GlyphPoints::GetNextY(uint16_t poly, uint16_t pt)
437 {
438         return GetY(poly, GetNext(poly, pt));
439 }
440
441
442 IPoint GlyphPoints::GetPoint(uint16_t poly, uint16_t pt)
443 {
444         return IPoint(GetX(poly, pt), GetY(poly, pt), GetOnCurve(poly, pt));
445 }
446
447
448 IPoint GlyphPoints::GetPoint(uint16_t pointNumber)
449 {
450         if (pointNumber > numPoints)
451                 throw GP_OUT_OF_RANGE;
452
453         return IPoint(x[pointNumber], y[pointNumber], onCurve[pointNumber]);
454 }
455
456
457 bool GlyphPoints::GetOnCurve(uint16_t poly, uint16_t pt)
458 {
459         if (pt >= GetNumPoints(poly))
460 #ifdef DEBUG
461 {
462 WriteLogMsg("Exception: GetOnCurve(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
463 #endif
464                 throw GP_OUT_OF_RANGE;
465 #ifdef DEBUG
466 }
467 #endif
468
469         return onCurve[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
470 }
471
472
473 bool GlyphPoints::GetPrevOnCurve(uint16_t poly, uint16_t pt)
474 {
475         return GetOnCurve(poly, GetPrev(poly, pt));
476 }
477
478
479 bool GlyphPoints::GetNextOnCurve(uint16_t poly, uint16_t pt)
480 {
481         return GetOnCurve(poly, GetNext(poly, pt));
482 }
483
484
485 uint16_t GlyphPoints::GetPolyStart(uint16_t poly)
486 {
487         if (poly >= numPolys)
488 #ifdef DEBUG
489 {
490 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
491 #endif
492                 throw GP_OUT_OF_RANGE;
493 #ifdef DEBUG
494 }
495 #endif
496
497         // If it's poly 0, return 0. Otherwise, get the previous poly's end & add one to it
498         return (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
499 }
500
501
502 uint16_t GlyphPoints::GetPolyEnd(uint16_t poly)
503 {
504         if (poly >= numPolys)
505 #ifdef DEBUG
506 {
507 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
508 #endif
509                 throw GP_OUT_OF_RANGE;
510 #ifdef DEBUG
511 }
512 #endif
513
514         return polyEnd[poly];
515 }
516
517
518 void GlyphPoints::OffsetPoints(int xOff, int yOff)
519 {
520         for(int i=0; i<numPoints; i++)
521                 x[i] += xOff, y[i] += yOff;
522 }
523
524
525 //
526 // Offset only a specific polygon in the glyph
527 //
528 void GlyphPoints::OffsetPoly(uint16_t poly, int32_t xOff, int32_t yOff)
529 {
530         if (poly >= numPolys)
531 #ifdef DEBUG
532 {
533 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
534 #endif
535                 throw GP_OUT_OF_RANGE;
536 #ifdef DEBUG
537 }
538 #endif
539
540         uint16_t polyStart = (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
541
542         for(int i=0; i<GetNumPoints(poly); i++)
543                 x[polyStart + i] += xOff, y[polyStart + i] += yOff;
544 }
545
546
547 void GlyphPoints::ScalePoints(float sc)
548 {
549         for(int i=0; i<numPoints; i++)
550                 x[i] = (int)(((float)x[i] * sc) + 0.5f),
551                 y[i] = (int)(((float)y[i] * sc) + 0.5f);
552 }
553
554
555 void GlyphPoints::SetXY(uint16_t pt, int xx, int yy)
556 {
557         if (pt >= numPoints)
558 #ifdef DEBUG
559 {
560 WriteLogMsg("Exception: SetXY(uint16_t, int, int). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
561 #endif
562                 throw GP_OUT_OF_RANGE;
563 #ifdef DEBUG
564 }
565 #endif
566
567         x[pt] = xx, y[pt] = yy;
568 }
569
570
571 void GlyphPoints::SetOnCurve(uint16_t pt, bool oc)
572 {
573         if (pt >= numPoints)
574 #ifdef DEBUG
575 {
576 WriteLogMsg("Exception: SetOnCurve(uint16_t, bool). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
577 #endif
578                 throw GP_OUT_OF_RANGE;
579 #ifdef DEBUG
580 }
581 #endif
582
583         onCurve[pt] = oc;
584 }
585
586
587 void GlyphPoints::SetPoint(const uint16_t pointNum, const IPoint point)
588 {
589         if (pointNum >= numPoints)
590 #ifdef DEBUG
591 {
592 WriteLogMsg("Exception: SetPoint(uint16_t, IPoint). pt=%u, numPoints=%u\xD\xA", pointNum, numPoints);
593 #endif
594                 throw GP_OUT_OF_RANGE;
595 #ifdef DEBUG
596 }
597 #endif
598
599         x[pointNum] = point.x, y[pointNum] = point.y, onCurve[pointNum] = point.onCurve;
600 }
601
602
603 uint16_t GlyphPoints::GetPrev(uint16_t pt)
604 {
605 // pt = 7, polyEnd = 4, 9, 15
606         uint16_t min = 0, max = numPoints - 1;
607
608         for(int i=0; i<numPolys; i++)
609         {
610                 if (pt <= polyEnd[i])
611                 {
612                         if (i > 0)
613                                 min = polyEnd[i - 1] + 1;
614
615                         max = polyEnd[i];
616                         break;
617                 }
618         }
619
620         uint16_t retVal = pt - 1;
621
622         if (pt == min)
623                 retVal = max;
624
625         return retVal;
626 }
627
628
629 uint16_t GlyphPoints::GetNext(uint16_t pt)
630 {
631         uint16_t min = 0, max = numPoints - 1;
632
633         for(int i=0; i<numPolys; i++)
634         {
635                 if (pt <= polyEnd[i])
636                 {
637                         if (i > 0)
638                                 min = polyEnd[i - 1] + 1;
639
640                         max = polyEnd[i];
641                         break;
642                 }
643         }
644
645         uint16_t retVal = pt + 1;
646
647         if (pt == max)
648                 retVal = min;
649
650         return retVal;
651 }
652
653
654 //
655 // Get previous point for this polygon using wraparound.
656 // Note that pt is a zero-based index!
657 //
658 uint16_t GlyphPoints::GetPrev(uint16_t poly, uint16_t pt)
659 {
660         return (pt == 0 ? GetNumPoints(poly) - 1 : pt - 1);
661 }
662
663
664 //
665 // Get next point for this polygon using wraparound.
666 // Note that pt is a zero-based index!
667 //
668 uint16_t GlyphPoints::GetNext(uint16_t poly, uint16_t pt)
669 {
670         return (pt == GetNumPoints(poly) - 1 ? 0 : pt + 1);
671 }
672
673
674 #warning "!!! This function returns incorrect results !!!"
675 uint16_t GlyphPoints::GetPoly(uint16_t pt)
676 {
677         if (pt >= numPoints)
678 #ifdef DEBUG
679 {
680 WriteLogMsg("Exception: GetPoly(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
681 #endif
682                 throw GP_OUT_OF_RANGE;
683 #ifdef DEBUG
684 }
685 #endif
686
687         for(int i=0; i<numPolys; i++)
688                 if (pt <= polyEnd[i])
689                         return i;
690
691         return (uint16_t)-1;
692 }
693
694
695 void GlyphPoints::AddNewPolyAtEnd(void)
696 {
697         if (numPoints == 0)                                                     // By default, we already *have* a poly
698                 return;
699
700         uint16_t * newPolyEnd = new uint16_t[numPolys + 1];
701
702         for(uint16_t i=0; i<numPolys; i++)
703                 newPolyEnd[i] = polyEnd[i];
704
705         newPolyEnd[numPolys] = newPolyEnd[numPolys - 1];
706         numPolys++;
707         delete[] polyEnd;
708         polyEnd = newPolyEnd;
709 }
710
711
712 IPoint GlyphPoints::GetMidpointToPrev(uint16_t poly, uint16_t pt)
713 {
714         uint16_t prev = GetPrev(poly, pt);
715
716         int32_t x1 = GetX(poly, pt), y1 = GetY(poly, pt);
717         int32_t x2 = GetX(poly, prev), y2 = GetY(poly, prev);
718
719         return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
720 }
721
722
723 IPoint GlyphPoints::GetMidpointToNext(uint16_t poly, uint16_t pt)
724 {
725         uint16_t next = GetNext(poly, pt);
726
727         int32_t x1 = GetX(poly, pt), y1 = GetY(poly, pt);
728         int32_t x2 = GetX(poly, next), y2 = GetY(poly, next);
729
730         return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
731 }
732
733
734 IPoint GlyphPoints::GetPrevPoint(uint16_t poly, uint16_t pt)
735 {
736         uint16_t prevPt = GetPrev(poly, pt);
737
738         return IPoint(GetX(poly, prevPt), GetY(poly, prevPt));
739 }
740
741
742 IPoint GlyphPoints::GetNextPoint(uint16_t poly, uint16_t pt)
743 {
744         uint16_t nextPt = GetNext(poly, pt);
745
746         return IPoint(GetX(poly, nextPt), GetY(poly, nextPt));
747 }
748
749
750 uint16_t GlyphPoints::GetPolyForPoint(IPoint point)
751 {
752         uint16_t poly = 0;
753
754         for(uint16_t i=0; i<numPoints; i++)
755         {
756                 if (i > polyEnd[poly])
757                         poly++;
758
759                 if (IPoint(x[i], y[i]) == point)
760                         return poly;
761         }
762
763         return 0xFFFF;
764 }
765
766
767 uint16_t GlyphPoints::GetPolyForPointNumber(uint16_t pointNumber)
768 {
769         // If there's only one poly, we know where the point is...
770         if (numPolys <= 1)
771                 return 0;
772
773         // Otherwise, do a linear search through the polys to find the right one
774         for(uint16_t i=0; i<numPolys; i++)
775         {
776                 if (pointNumber >= GetPolyStart(i) && pointNumber <= polyEnd[i])
777                         return i;
778         }
779
780         return 0xFFFF;
781 }
782
783
784 //
785 // Rotate a point by "angle" around point "center"
786 //
787 IPoint GlyphPoints::RotatePoint(const double angle, const IPoint point, const IPoint center)
788 {
789         // Translate the point to the origin
790         double xt = (double)(point.x - center.x);
791         double yt = (double)(point.y - center.y);
792
793         // Rotate the point by angle
794         double xr = (xt * cos(angle)) - (yt * sin(angle));
795         double yr = (xt * sin(angle)) + (yt * cos(angle));
796
797         // Translate it back...
798         IPoint rotated;
799         rotated.x = (int)(xr + 0.5) + center.x;
800         rotated.y = (int)(yr + 0.5) + center.y;
801         return rotated;
802 }
803
804
805 //
806 // Rotate all points in the glyph by "angle" around point "pt"
807 //
808 void GlyphPoints::RotatePoints(const double angle, const IPoint pt)
809 {
810         for(int i=0; i<numPoints; i++)
811         {
812                 // Translate the point to the origin
813                 double xt = (double)(x[i] - pt.x);
814                 double yt = (double)(y[i] - pt.y);
815
816                 // Rotate the point by angle
817                 double xr = (xt * cos(angle)) - (yt * sin(angle));
818                 double yr = (xt * sin(angle)) + (yt * cos(angle));
819
820                 // Put it back...
821                 x[i] = (int)(xr + 0.5) + pt.x;
822                 y[i] = (int)(yr + 0.5) + pt.y;
823         }
824 }
825
826
827 IPoint GlyphPoints::GetPolyCentroid(const int16_t poly)
828 {
829         // We should throw an exception here, but meh
830         // (this actually short circuits the exception handling in all the GetPolyXXX() functions)
831         // [now we do!]
832         if (poly >= numPolys)
833                 throw GP_OUT_OF_RANGE;
834 //              return IPoint(0, 0);
835
836 //      if (poly >= numPolys)
837 //#ifdef DEBUG
838 //{
839 //WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
840 //#endif
841 //              throw GP_OUT_OF_RANGE;
842 //#ifdef DEBUG
843 //}
844 //#endif
845
846         IPoint centroid;                                                                // Initializes to (0, 0)
847         uint16_t numPointsInPoly = GetNumPoints(poly);
848
849         for(uint16_t i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
850         {
851                 centroid.x += x[i];
852                 centroid.y += y[i];
853         }
854
855         centroid.x /= numPointsInPoly;
856         centroid.y /= numPointsInPoly;
857
858         return centroid;
859 }
860
861
862 void GlyphPoints::RotatePolyAroundCentroid(const int16_t poly, const double angle)
863 {
864         if (poly >= numPolys)
865                 return;
866
867         IPoint centroid = GetPolyCentroid(poly);
868
869         for(uint16_t i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
870         {
871                 IPoint rotated = RotatePoint(angle, IPoint(x[i], y[i]), centroid);
872                 x[i] = rotated.x;
873                 y[i] = rotated.y;
874         }
875 }
876
877
878 void GlyphPoints::InvertPolyDrawSequence(const uint16_t poly)
879 {
880         if (poly >= numPolys)
881                 throw GP_OUT_OF_RANGE;
882
883         uint16_t pointNum1 = GetPolyStart(poly);
884         uint16_t pointNum2 = GetPolyEnd(poly);
885
886         // Algorithm: Step through points in the polygon, swapping 1st and last, then
887         // 2nd and (last - 1), 3rd and (last - 2), etc. We only do this for half the
888         // points, as doing it for all would undo the swapping we did in the 1st half.
889         for(uint16_t i=0; i<GetNumPoints(poly)/2; i++)
890         {
891                 IPoint point1 = GetPoint(pointNum1 + i);
892                 IPoint point2 = GetPoint(pointNum2 - i);
893                 SetPoint(pointNum2 - i, point1);
894                 SetPoint(pointNum1 + i, point2);
895         }
896 }
897
898
899 // really need to do checking on the results from fscanf...
900 bool GlyphPoints::LoadGlyphFromFile(FILE * file)
901 {
902         char line[512];
903         float version;
904
905         FreeAllocatedMemory();
906
907         fscanf(file, "%s V%f", line, &version);
908         fscanf(file, "%s %u", line, &numPoints);
909         x = new int[numPoints];
910         y = new int[numPoints];
911         onCurve = new bool[numPoints];
912
913         for(int i=0; i<numPoints; i++)
914         {
915                 fscanf(file, "%d %d %s", &x[i], &y[i], (char *)&line);
916                 onCurve[i] = (line[0] == 'T' ? true : false);
917         }
918
919         fscanf(file, "%s %u", line, &numPolys);
920         polyEnd = new uint16_t[numPolys];
921
922         for(int i=0; i<numPolys; i++)
923         {
924                 fscanf(file, "%u", &polyEnd[i]);
925         }
926
927         return true;
928 }
929
930
931 bool GlyphPoints::SaveGlyphToFile(FILE * file)
932 {
933 //      GlyphPoints glyph = editWnd->pts;
934         fprintf(file, "TTEGLYPH V1.0\n");
935         fprintf(file, "POINTS %u\n", numPoints);
936
937         for(int i=0; i<numPoints; i++)
938         {
939                 fprintf(file, "%d %d %s\n", x[i], y[i], (onCurve[i] ? "T" : "F")); 
940         }
941
942         fprintf(file, "POLYS %u\n", numPolys);
943
944         for(int i=0; i<numPolys; i++)
945         {
946                 fprintf(file, "%u\n", polyEnd[i]); 
947         }
948
949         return true;
950 }
951