4 // Class implementation. Nothing too inexplicable going on here. Fairly
5 // straightforward stuff.
8 // (C) 2004 Underground Software
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- ------------------------------------------------------------
14 // JLH ??/??/200? Created this file
15 // JLH 05/18/2004 Added pure point adding, inserting, better polygon handling
18 // Uncomment this for debugging...
21 #include "glyphpoints.h"
28 /*GlyphPoints::GlyphPoints(void)
30 GlyphPoints(0, 0, NULL, NULL, NULL, NULL);
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)
38 AllocateAndCopy(nPts, nPlys, xa, ya, oca, pa);
43 polyEnd = new uint16_t[numPolys];
44 polyEnd[0] = numPoints - 1;
47 WriteLogMsg("GlyphPoints: Default constructor. %u points, %u polys.\n", numPoints, numPolys);
52 GlyphPoints::GlyphPoints(int xx, int yy, bool oc)
54 //Hmm. What to do with this...?
55 AllocateAndCopy(1, 0, &xx, &yy, &oc, NULL);
57 WriteLogMsg("GlyphPoints: Single point constructor. %u points, %u polys.\n", numPoints, numPolys);
62 // Copy constructor (needed for deep copying)
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)
67 *this = c; // Use overloaded operator=
69 WriteLogMsg("GlyphPoints: Copy constructor. %u points, %u polys.\n", numPoints, numPolys);
74 GlyphPoints::~GlyphPoints()
76 FreeAllocatedMemory();
93 void GlyphPoints::AllocateAndCopy(int nPts, int nPlys, int * xa, int * ya, bool * oca, uint16_t * pa)
95 numPoints = nPts, numPolys = nPlys;
99 x = new int[numPoints];
100 y = new int[numPoints];
101 onCurve = new bool[numPoints];
103 if (xa) // Copy points in if they're passed in...
104 for(int i=0; i<nPts; i++)
108 for(int i=0; i<nPts; i++)
112 for(int i=0; i<nPts; i++)
118 polyEnd = new uint16_t[numPolys];
120 if (pa) // Copy poly ends in if they're passed in...
121 for(int i=0; i<nPlys; i++)
127 void GlyphPoints::FreeAllocatedMemory(void)
143 GlyphPoints& GlyphPoints::operator=(const GlyphPoints &c)
146 return *this; // Take care of self-assignment
148 FreeAllocatedMemory();
149 AllocateAndCopy(c.numPoints, c.numPolys, c.x, c.y, c.onCurve, c.polyEnd);
155 // Add another GlyphPoints' points to this one...
157 GlyphPoints GlyphPoints::operator+(const GlyphPoints &c)
159 int totPoints = numPoints + c.numPoints, totPolys = numPolys + c.numPolys;
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];
166 for(int i=0; i<numPoints; i++)
170 totOnCurve[i] = onCurve[i];
173 for(int i=0; i<numPolys; i++)
174 totPolyEnd[i] = polyEnd[i];
176 for(int i=0; i<c.numPoints; i++)
178 totX[numPoints+i] = c.x[i];
179 totY[numPoints+i] = c.y[i];
180 totOnCurve[numPoints+i] = c.onCurve[i];
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...
187 GlyphPoints retVal(totPoints, totPolys, totX, totY, totOnCurve, totPolyEnd);
197 // Add a point to this GlyphPoints...
199 GlyphPoints GlyphPoints::operator+(const IPoint &c)
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];
207 for(int i=0; i<numPoints; i++)
208 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
210 totX[numPoints] = c.x, totY[numPoints] = c.y, totOnCurve[numPoints] = c.onCurve;
212 for(int i=0; i<numPolys; i++)
213 totPolyEnd[i] = polyEnd[i];
215 totPolyEnd[numPolys - 1]++; // Bump polygon's end point
216 GlyphPoints retVal(numPoints + 1, numPolys, totX, totY, totOnCurve, totPolyEnd);
226 GlyphPoints& GlyphPoints::operator+=(const IPoint &p)
228 InsertPoint(numPoints, p.x, p.y, p.onCurve);
234 void GlyphPoints::Clear(void)
236 FreeAllocatedMemory();
240 numPoints = numPolys = pointsAllocated = polysAllocated = 0;
243 polyEnd = new uint16_t[numPolys];
244 polyEnd[0] = numPoints - 1;
248 void GlyphPoints::InsertPoint(uint16_t pt, int xx, int yy, bool oc)
250 if (pt > numPoints) // > because we can insert at end...!
251 throw GP_OUT_OF_RANGE;
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];
258 for(int i=0; i<pt; i++)
259 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
261 for(int i=pt; i<numPoints; i++)
262 totX[i + 1] = x[i], totY[i + 1] = y[i], totOnCurve[i + 1] = onCurve[i];
264 totX[pt] = xx, totY[pt] = yy, totOnCurve[pt] = oc;
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
279 x = totX, y = totY, onCurve = totOnCurve;
283 void GlyphPoints::InsertPoint(uint16_t pt, const IPoint &p)
285 InsertPoint(pt, p.x, p.y, p.onCurve);
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...
295 void GlyphPoints::DeletePoint(uint16_t pt)
297 // Adjust polygon ends appropriately
298 uint16_t poly = GetPoly(pt);
300 for(int i=poly; i<numPolys; i++)
303 //Need to check here if we're deleting the last point in the glyph or not.
305 if (GetNumPoints(poly) == 0 && numPoints > 0)
309 for(int i=poly; i<numPolys; i++)
310 polyEnd[i] = polyEnd[i + 1];
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
317 for(int i=pt; i<numPoints; i++)
318 x[i] = x[i + 1], y[i] = y[i + 1], onCurve[i] = onCurve[i + 1];
322 uint16_t GlyphPoints::GetNumPoints(void)
328 uint16_t GlyphPoints::GetNumPoints(uint16_t poly)
330 if (poly >= numPolys)
333 WriteLogMsg("Exception: GetNumPoints(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
335 throw GP_OUT_OF_RANGE;
340 return polyEnd[poly] - (poly == 0 ? -1 : polyEnd[poly - 1]);
344 uint16_t GlyphPoints::GetNumPolys(void)
350 int GlyphPoints::GetX(uint16_t pt)
355 WriteLogMsg("Exception: GetX(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
357 throw GP_OUT_OF_RANGE;
366 int GlyphPoints::GetY(uint16_t pt)
371 WriteLogMsg("Exception: GetY(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
373 throw GP_OUT_OF_RANGE;
382 bool GlyphPoints::GetOnCurve(uint16_t pt)
387 WriteLogMsg("Exception: GetOnCurve(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
389 throw GP_OUT_OF_RANGE;
398 int GlyphPoints::GetX(uint16_t poly, uint16_t pt)
400 if (pt >= GetNumPoints(poly))
403 WriteLogMsg("Exception: GetX(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
405 throw GP_OUT_OF_RANGE;
410 return x[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
414 int GlyphPoints::GetNextX(uint16_t poly, uint16_t pt)
416 return GetX(poly, GetNext(poly, pt));
420 int GlyphPoints::GetY(uint16_t poly, uint16_t pt)
422 if (pt >= GetNumPoints(poly))
425 WriteLogMsg("Exception: GetY(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
427 throw GP_OUT_OF_RANGE;
432 return y[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
436 int GlyphPoints::GetNextY(uint16_t poly, uint16_t pt)
438 return GetY(poly, GetNext(poly, pt));
442 IPoint GlyphPoints::GetPoint(uint16_t poly, uint16_t pt)
444 return IPoint(GetX(poly, pt), GetY(poly, pt), GetOnCurve(poly, pt));
448 IPoint GlyphPoints::GetPoint(uint16_t pointNumber)
450 if (pointNumber > numPoints)
451 throw GP_OUT_OF_RANGE;
453 return IPoint(x[pointNumber], y[pointNumber], onCurve[pointNumber]);
457 bool GlyphPoints::GetOnCurve(uint16_t poly, uint16_t pt)
459 if (pt >= GetNumPoints(poly))
462 WriteLogMsg("Exception: GetOnCurve(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
464 throw GP_OUT_OF_RANGE;
469 return onCurve[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
473 bool GlyphPoints::GetPrevOnCurve(uint16_t poly, uint16_t pt)
475 return GetOnCurve(poly, GetPrev(poly, pt));
479 bool GlyphPoints::GetNextOnCurve(uint16_t poly, uint16_t pt)
481 return GetOnCurve(poly, GetNext(poly, pt));
485 uint16_t GlyphPoints::GetPolyStart(uint16_t poly)
487 if (poly >= numPolys)
490 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
492 throw GP_OUT_OF_RANGE;
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);
502 uint16_t GlyphPoints::GetPolyEnd(uint16_t poly)
504 if (poly >= numPolys)
507 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
509 throw GP_OUT_OF_RANGE;
514 return polyEnd[poly];
518 void GlyphPoints::OffsetPoints(int xOff, int yOff)
520 for(int i=0; i<numPoints; i++)
521 x[i] += xOff, y[i] += yOff;
526 // Offset only a specific polygon in the glyph
528 void GlyphPoints::OffsetPoly(uint16_t poly, int32_t xOff, int32_t yOff)
530 if (poly >= numPolys)
533 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
535 throw GP_OUT_OF_RANGE;
540 uint16_t polyStart = (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
542 for(int i=0; i<GetNumPoints(poly); i++)
543 x[polyStart + i] += xOff, y[polyStart + i] += yOff;
547 void GlyphPoints::ScalePoints(float sc)
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);
555 void GlyphPoints::SetXY(uint16_t pt, int xx, int yy)
560 WriteLogMsg("Exception: SetXY(uint16_t, int, int). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
562 throw GP_OUT_OF_RANGE;
567 x[pt] = xx, y[pt] = yy;
571 void GlyphPoints::SetOnCurve(uint16_t pt, bool oc)
576 WriteLogMsg("Exception: SetOnCurve(uint16_t, bool). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
578 throw GP_OUT_OF_RANGE;
587 void GlyphPoints::SetPoint(const uint16_t pointNum, const IPoint point)
589 if (pointNum >= numPoints)
592 WriteLogMsg("Exception: SetPoint(uint16_t, IPoint). pt=%u, numPoints=%u\xD\xA", pointNum, numPoints);
594 throw GP_OUT_OF_RANGE;
599 x[pointNum] = point.x, y[pointNum] = point.y, onCurve[pointNum] = point.onCurve;
603 uint16_t GlyphPoints::GetPrev(uint16_t pt)
605 // pt = 7, polyEnd = 4, 9, 15
606 uint16_t min = 0, max = numPoints - 1;
608 for(int i=0; i<numPolys; i++)
610 if (pt <= polyEnd[i])
613 min = polyEnd[i - 1] + 1;
620 uint16_t retVal = pt - 1;
629 uint16_t GlyphPoints::GetNext(uint16_t pt)
631 uint16_t min = 0, max = numPoints - 1;
633 for(int i=0; i<numPolys; i++)
635 if (pt <= polyEnd[i])
638 min = polyEnd[i - 1] + 1;
645 uint16_t retVal = pt + 1;
655 // Get previous point for this polygon using wraparound.
656 // Note that pt is a zero-based index!
658 uint16_t GlyphPoints::GetPrev(uint16_t poly, uint16_t pt)
660 return (pt == 0 ? GetNumPoints(poly) - 1 : pt - 1);
665 // Get next point for this polygon using wraparound.
666 // Note that pt is a zero-based index!
668 uint16_t GlyphPoints::GetNext(uint16_t poly, uint16_t pt)
670 return (pt == GetNumPoints(poly) - 1 ? 0 : pt + 1);
674 #warning "!!! This function returns incorrect results !!!"
675 uint16_t GlyphPoints::GetPoly(uint16_t pt)
680 WriteLogMsg("Exception: GetPoly(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
682 throw GP_OUT_OF_RANGE;
687 for(int i=0; i<numPolys; i++)
688 if (pt <= polyEnd[i])
695 void GlyphPoints::AddNewPolyAtEnd(void)
697 if (numPoints == 0) // By default, we already *have* a poly
700 uint16_t * newPolyEnd = new uint16_t[numPolys + 1];
702 for(uint16_t i=0; i<numPolys; i++)
703 newPolyEnd[i] = polyEnd[i];
705 newPolyEnd[numPolys] = newPolyEnd[numPolys - 1];
708 polyEnd = newPolyEnd;
712 IPoint GlyphPoints::GetMidpointToPrev(uint16_t poly, uint16_t pt)
714 uint16_t prev = GetPrev(poly, pt);
716 int32_t x1 = GetX(poly, pt), y1 = GetY(poly, pt);
717 int32_t x2 = GetX(poly, prev), y2 = GetY(poly, prev);
719 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
723 IPoint GlyphPoints::GetMidpointToNext(uint16_t poly, uint16_t pt)
725 uint16_t next = GetNext(poly, pt);
727 int32_t x1 = GetX(poly, pt), y1 = GetY(poly, pt);
728 int32_t x2 = GetX(poly, next), y2 = GetY(poly, next);
730 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
734 IPoint GlyphPoints::GetPrevPoint(uint16_t poly, uint16_t pt)
736 uint16_t prevPt = GetPrev(poly, pt);
738 return IPoint(GetX(poly, prevPt), GetY(poly, prevPt));
742 IPoint GlyphPoints::GetNextPoint(uint16_t poly, uint16_t pt)
744 uint16_t nextPt = GetNext(poly, pt);
746 return IPoint(GetX(poly, nextPt), GetY(poly, nextPt));
750 uint16_t GlyphPoints::GetPolyForPoint(IPoint point)
754 for(uint16_t i=0; i<numPoints; i++)
756 if (i > polyEnd[poly])
759 if (IPoint(x[i], y[i]) == point)
767 uint16_t GlyphPoints::GetPolyForPointNumber(uint16_t pointNumber)
769 // If there's only one poly, we know where the point is...
773 // Otherwise, do a linear search through the polys to find the right one
774 for(uint16_t i=0; i<numPolys; i++)
776 if (pointNumber >= GetPolyStart(i) && pointNumber <= polyEnd[i])
785 // Rotate a point by "angle" around point "center"
787 IPoint GlyphPoints::RotatePoint(const double angle, const IPoint point, const IPoint center)
789 // Translate the point to the origin
790 double xt = (double)(point.x - center.x);
791 double yt = (double)(point.y - center.y);
793 // Rotate the point by angle
794 double xr = (xt * cos(angle)) - (yt * sin(angle));
795 double yr = (xt * sin(angle)) + (yt * cos(angle));
797 // Translate it back...
799 rotated.x = (int)(xr + 0.5) + center.x;
800 rotated.y = (int)(yr + 0.5) + center.y;
806 // Rotate all points in the glyph by "angle" around point "pt"
808 void GlyphPoints::RotatePoints(const double angle, const IPoint pt)
810 for(int i=0; i<numPoints; i++)
812 // Translate the point to the origin
813 double xt = (double)(x[i] - pt.x);
814 double yt = (double)(y[i] - pt.y);
816 // Rotate the point by angle
817 double xr = (xt * cos(angle)) - (yt * sin(angle));
818 double yr = (xt * sin(angle)) + (yt * cos(angle));
821 x[i] = (int)(xr + 0.5) + pt.x;
822 y[i] = (int)(yr + 0.5) + pt.y;
827 IPoint GlyphPoints::GetPolyCentroid(const int16_t poly)
829 // We should throw an exception here, but meh
830 // (this actually short circuits the exception handling in all the GetPolyXXX() functions)
832 if (poly >= numPolys)
833 throw GP_OUT_OF_RANGE;
834 // return IPoint(0, 0);
836 // if (poly >= numPolys)
839 //WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
841 // throw GP_OUT_OF_RANGE;
846 IPoint centroid; // Initializes to (0, 0)
847 uint16_t numPointsInPoly = GetNumPoints(poly);
849 for(uint16_t i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
855 centroid.x /= numPointsInPoly;
856 centroid.y /= numPointsInPoly;
862 void GlyphPoints::RotatePolyAroundCentroid(const int16_t poly, const double angle)
864 if (poly >= numPolys)
867 IPoint centroid = GetPolyCentroid(poly);
869 for(uint16_t i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
871 IPoint rotated = RotatePoint(angle, IPoint(x[i], y[i]), centroid);
878 void GlyphPoints::InvertPolyDrawSequence(const uint16_t poly)
880 if (poly >= numPolys)
881 throw GP_OUT_OF_RANGE;
883 uint16_t pointNum1 = GetPolyStart(poly);
884 uint16_t pointNum2 = GetPolyEnd(poly);
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++)
891 IPoint point1 = GetPoint(pointNum1 + i);
892 IPoint point2 = GetPoint(pointNum2 - i);
893 SetPoint(pointNum2 - i, point1);
894 SetPoint(pointNum1 + i, point2);
899 // really need to do checking on the results from fscanf...
900 bool GlyphPoints::LoadGlyphFromFile(FILE * file)
905 FreeAllocatedMemory();
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];
913 for(int i=0; i<numPoints; i++)
915 fscanf(file, "%d %d %s", &x[i], &y[i], (char *)&line);
916 onCurve[i] = (line[0] == 'T' ? true : false);
919 fscanf(file, "%s %u", line, &numPolys);
920 polyEnd = new uint16_t[numPolys];
922 for(int i=0; i<numPolys; i++)
924 fscanf(file, "%u", &polyEnd[i]);
931 bool GlyphPoints::SaveGlyphToFile(FILE * file)
933 // GlyphPoints glyph = editWnd->pts;
934 fprintf(file, "TTEGLYPH V1.0\n");
935 fprintf(file, "POINTS %u\n", numPoints);
937 for(int i=0; i<numPoints; i++)
939 fprintf(file, "%d %d %s\n", x[i], y[i], (onCurve[i] ? "T" : "F"));
942 fprintf(file, "POLYS %u\n", numPolys);
944 for(int i=0; i<numPolys; i++)
946 fprintf(file, "%u\n", polyEnd[i]);