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 * 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 * pa/*=null*/): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
38 AllocateAndCopy(nPts, nPlys, xa, ya, oca, pa);
43 polyEnd = new uint16[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 * 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[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 * totPolyEnd = new uint16[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 * totPolyEnd = new uint16[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;
244 void GlyphPoints::InsertPoint(uint16 pt, int xx, int yy, bool oc)
246 if (pt > numPoints) // > because we can insert at end...!
247 throw GP_OUT_OF_RANGE;
249 //This is kinda silly. We can do better than this! !!! FIX !!!
250 int * totX = new int[numPoints + 1];
251 int * totY = new int[numPoints + 1];
252 bool * totOnCurve = new bool[numPoints + 1];
254 for(int i=0; i<pt; i++)
255 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
257 for(int i=pt; i<numPoints; i++)
258 totX[i + 1] = x[i], totY[i + 1] = y[i], totOnCurve[i + 1] = onCurve[i];
260 totX[pt] = xx, totY[pt] = yy, totOnCurve[pt] = oc;
262 //A way to fix the kludge in GetPoly() would be to put a check here to see if
263 //we're adding to the end of the structure: [DONE, below]
264 int polyInsert = (pt == numPoints ? numPolys - 1 : GetPoly(pt));
265 for(int i=polyInsert; i<numPolys; i++)
266 // for(int i=GetPoly(pt); i<numPolys; i++)
267 polyEnd[i]++; // Bump polygons' end point
275 x = totX, y = totY, onCurve = totOnCurve;
279 void GlyphPoints::InsertPoint(uint16 pt, const IPoint &p)
281 InsertPoint(pt, p.x, p.y, p.onCurve);
286 // Delete a point from the glyph
287 // Note that we don't bother to reallocate anything here, just bump the
288 // size counters down as needed. In the future, we'll keep track so we
289 // don't have to reallocate *every* damn time a point is added...
291 void GlyphPoints::DeletePoint(uint16 pt)
293 // Adjust polygon ends appropriately
294 uint16 poly = GetPoly(pt);
296 for(int i=poly; i<numPolys; i++)
299 //Need to check here if we're deleting the last point in the glyph or not.
301 if (GetNumPoints(poly) == 0 && numPoints > 0)
305 for(int i=poly; i<numPolys; i++)
306 polyEnd[i] = polyEnd[i + 1];
309 //This causes a crash becuz GetPoly() uses numPoints... !!! FIX !!! [DONE by switching poly delete & point delete]
310 // Close up the gap left by the current point
313 for(int i=pt; i<numPoints; i++)
314 x[i] = x[i + 1], y[i] = y[i + 1], onCurve[i] = onCurve[i + 1];
318 uint16 GlyphPoints::GetNumPoints(void)
324 uint16 GlyphPoints::GetNumPoints(uint16 poly)
326 if (poly >= numPolys)
329 WriteLogMsg("Exception: GetNumPoints(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
331 throw GP_OUT_OF_RANGE;
336 return polyEnd[poly] - (poly == 0 ? -1 : polyEnd[poly - 1]);
340 uint16 GlyphPoints::GetNumPolys(void)
346 int GlyphPoints::GetX(uint16 pt)
351 WriteLogMsg("Exception: GetX(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
353 throw GP_OUT_OF_RANGE;
362 int GlyphPoints::GetY(uint16 pt)
367 WriteLogMsg("Exception: GetY(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
369 throw GP_OUT_OF_RANGE;
378 bool GlyphPoints::GetOnCurve(uint16 pt)
383 WriteLogMsg("Exception: GetOnCurve(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
385 throw GP_OUT_OF_RANGE;
394 int GlyphPoints::GetX(uint16 poly, uint16 pt)
396 if (pt >= GetNumPoints(poly))
399 WriteLogMsg("Exception: GetX(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
401 throw GP_OUT_OF_RANGE;
406 return x[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
410 int GlyphPoints::GetNextX(uint16 poly, uint16 pt)
412 return GetX(poly, GetNext(poly, pt));
416 int GlyphPoints::GetY(uint16 poly, uint16 pt)
418 if (pt >= GetNumPoints(poly))
421 WriteLogMsg("Exception: GetY(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
423 throw GP_OUT_OF_RANGE;
428 return y[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
432 int GlyphPoints::GetNextY(uint16 poly, uint16 pt)
434 return GetY(poly, GetNext(poly, pt));
438 IPoint GlyphPoints::GetPoint(uint16 poly, uint16 pt)
440 return IPoint(GetX(poly, pt), GetY(poly, pt));
444 IPoint GlyphPoints::GetPoint(uint16 pointNumber)
446 if (pointNumber > numPoints)
447 throw GP_OUT_OF_RANGE;
449 return IPoint(x[pointNumber], y[pointNumber]);
453 bool GlyphPoints::GetOnCurve(uint16 poly, uint16 pt)
455 if (pt >= GetNumPoints(poly))
458 WriteLogMsg("Exception: GetOnCurve(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
460 throw GP_OUT_OF_RANGE;
465 return onCurve[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
469 bool GlyphPoints::GetPrevOnCurve(uint16 poly, uint16 pt)
471 return GetOnCurve(poly, GetPrev(poly, pt));
475 bool GlyphPoints::GetNextOnCurve(uint16 poly, uint16 pt)
477 return GetOnCurve(poly, GetNext(poly, pt));
481 uint16 GlyphPoints::GetPolyStart(uint16 poly)
483 if (poly >= numPolys)
486 WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
488 throw GP_OUT_OF_RANGE;
493 // If it's poly 0, return 0. Otherwise, get the previous poly's end & add one to it
494 return (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
498 uint16 GlyphPoints::GetPolyEnd(uint16 poly)
500 if (poly >= numPolys)
503 WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
505 throw GP_OUT_OF_RANGE;
510 return polyEnd[poly];
514 void GlyphPoints::OffsetPoints(int xOff, int yOff)
516 for(int i=0; i<numPoints; i++)
517 x[i] += xOff, y[i] += yOff;
522 // Offset only a specific polygon in the glyph
524 void GlyphPoints::OffsetPoly(uint16 poly, int32 xOff, int32 yOff)
526 if (poly >= numPolys)
529 WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
531 throw GP_OUT_OF_RANGE;
536 uint16 polyStart = (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
538 for(int i=0; i<GetNumPoints(poly); i++)
539 x[polyStart + i] += xOff, y[polyStart + i] += yOff;
543 void GlyphPoints::ScalePoints(float sc)
545 for(int i=0; i<numPoints; i++)
546 x[i] = (int)(((float)x[i] * sc) + 0.5f),
547 y[i] = (int)(((float)y[i] * sc) + 0.5f);
551 void GlyphPoints::SetXY(uint16 pt, int xx, int yy)
556 WriteLogMsg("Exception: SetXY(uint16, int, int). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
558 throw GP_OUT_OF_RANGE;
563 x[pt] = xx, y[pt] = yy;
567 void GlyphPoints::SetOnCurve(uint16 pt, bool oc)
572 WriteLogMsg("Exception: SetOnCurve(uint16, bool). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
574 throw GP_OUT_OF_RANGE;
583 uint16 GlyphPoints::GetPrev(uint16 pt)
585 // pt = 7, polyEnd = 4, 9, 15
586 uint16 min = 0, max = numPoints - 1;
588 for(int i=0; i<numPolys; i++)
590 if (pt <= polyEnd[i])
593 min = polyEnd[i - 1] + 1;
600 uint16 retVal = pt - 1;
609 uint16 GlyphPoints::GetNext(uint16 pt)
611 uint16 min = 0, max = numPoints - 1;
613 for(int i=0; i<numPolys; i++)
615 if (pt <= polyEnd[i])
618 min = polyEnd[i - 1] + 1;
625 uint16 retVal = pt + 1;
635 // Get previous point for this polygon using wraparound.
636 // Note that pt is a zero-based index!
638 uint16 GlyphPoints::GetPrev(uint16 poly, uint16 pt)
640 return (pt == 0 ? GetNumPoints(poly) - 1 : pt - 1);
645 // Get next point for this polygon using wraparound.
646 // Note that pt is a zero-based index!
648 uint16 GlyphPoints::GetNext(uint16 poly, uint16 pt)
650 return (pt == GetNumPoints(poly) - 1 ? 0 : pt + 1);
654 #warning "!!! This function returns incorrect results !!!"
655 uint16 GlyphPoints::GetPoly(uint16 pt)
660 WriteLogMsg("Exception: GetPoly(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
662 throw GP_OUT_OF_RANGE;
667 for(int i=0; i<numPolys; i++)
668 if (pt <= polyEnd[i])
675 void GlyphPoints::AddNewPolyAtEnd(void)
677 if (numPoints == 0) // By default, we already *have* a poly
680 uint16 * newPolyEnd = new uint16[numPolys + 1];
682 for(uint16 i=0; i<numPolys; i++)
683 newPolyEnd[i] = polyEnd[i];
685 newPolyEnd[numPolys] = newPolyEnd[numPolys - 1];
688 polyEnd = newPolyEnd;
692 IPoint GlyphPoints::GetMidpointToPrev(uint16 poly, uint16 pt)
694 uint16 prev = GetPrev(poly, pt);
696 int32 x1 = GetX(poly, pt), y1 = GetY(poly, pt);
697 int32 x2 = GetX(poly, prev), y2 = GetY(poly, prev);
699 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
703 IPoint GlyphPoints::GetMidpointToNext(uint16 poly, uint16 pt)
705 uint16 next = GetNext(poly, pt);
707 int32 x1 = GetX(poly, pt), y1 = GetY(poly, pt);
708 int32 x2 = GetX(poly, next), y2 = GetY(poly, next);
710 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
714 IPoint GlyphPoints::GetPrevPoint(uint16 poly, uint16 pt)
716 uint16 prevPt = GetPrev(poly, pt);
718 return IPoint(GetX(poly, prevPt), GetY(poly, prevPt));
722 IPoint GlyphPoints::GetNextPoint(uint16 poly, uint16 pt)
724 uint16 nextPt = GetNext(poly, pt);
726 return IPoint(GetX(poly, nextPt), GetY(poly, nextPt));
730 uint16 GlyphPoints::GetPolyForPoint(IPoint point)
734 for(uint16 i=0; i<numPoints; i++)
736 if (i > polyEnd[poly])
739 if (IPoint(x[i], y[i]) == point)
747 uint16 GlyphPoints::GetPolyForPointNumber(uint16 pointNumber)
749 // If there's only one poly, we know where the point is...
753 // Otherwise, do a linear search through the polys to find the right one
754 for(uint16 i=0; i<numPolys; i++)
756 if (pointNumber >= GetPolyStart(i) && pointNumber <= polyEnd[i])
765 // Rotate a point by "angle" around point "center"
767 IPoint GlyphPoints::RotatePoint(const double angle, const IPoint point, const IPoint center)
769 // Translate the point to the origin
770 double xt = (double)(point.x - center.x);
771 double yt = (double)(point.y - center.y);
773 // Rotate the point by angle
774 double xr = (xt * cos(angle)) - (yt * sin(angle));
775 double yr = (xt * sin(angle)) + (yt * cos(angle));
777 // Translate it back...
779 rotated.x = (int)(xr + 0.5) + center.x;
780 rotated.y = (int)(yr + 0.5) + center.y;
786 // Rotate all points in the glyph by "angle" around point "pt"
788 void GlyphPoints::RotatePoints(const double angle, const IPoint pt)
790 for(int i=0; i<numPoints; i++)
792 // Translate the point to the origin
793 double xt = (double)(x[i] - pt.x);
794 double yt = (double)(y[i] - pt.y);
796 // Rotate the point by angle
797 double xr = (xt * cos(angle)) - (yt * sin(angle));
798 double yr = (xt * sin(angle)) + (yt * cos(angle));
801 x[i] = (int)(xr + 0.5) + pt.x;
802 y[i] = (int)(yr + 0.5) + pt.y;
807 IPoint GlyphPoints::GetPolyCentroid(const int16 poly)
809 // We should throw an exception here, but meh
810 // (this actually short circuits the exception handling in all the GetPolyXXX() functions)
811 if (poly >= numPolys)
814 // if (poly >= numPolys)
817 //WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
819 // throw GP_OUT_OF_RANGE;
824 IPoint centroid; // Initializes to (0, 0)
825 uint16 numPointsInPoly = GetNumPoints(poly);
827 for(uint16 i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
833 centroid.x /= numPointsInPoly;
834 centroid.y /= numPointsInPoly;
840 void GlyphPoints::RotatePolyAroundCentroid(const int16 poly, const double angle)
842 if (poly >= numPolys)
845 IPoint centroid = GetPolyCentroid(poly);
847 for(uint16 i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
849 IPoint rotated = RotatePoint(angle, IPoint(x[i], y[i]), centroid);
856 // really need to do checking on the results from fscanf...
857 bool GlyphPoints::LoadGlyphFromFile(FILE * file)
862 FreeAllocatedMemory();
864 fscanf(file, "%s V%f", line, &version);
865 fscanf(file, "%s %u", line, &numPoints);
866 x = new int[numPoints];
867 y = new int[numPoints];
868 onCurve = new bool[numPoints];
870 for(int i=0; i<numPoints; i++)
872 fscanf(file, "%d %d %s", &x[i], &y[i], &line);
873 onCurve[i] = (line[0] == 'T' ? true : false);
876 fscanf(file, "%s %u", line, &numPolys);
877 polyEnd = new uint16[numPolys];
879 for(int i=0; i<numPolys; i++)
881 fscanf(file, "%u", &polyEnd[i]);
888 bool GlyphPoints::SaveGlyphToFile(FILE * file)
890 // GlyphPoints glyph = editWnd->pts;
891 fprintf(file, "TTEGLYPH V1.0\n");
892 fprintf(file, "POINTS %u\n", numPoints);
894 for(int i=0; i<numPoints; i++)
896 fprintf(file, "%d %d %s\n", x[i], y[i], (onCurve[i] ? "T" : "F"));
899 fprintf(file, "POLYS %u\n", numPolys);
901 for(int i=0; i<numPolys; i++)
903 fprintf(file, "%u\n", polyEnd[i]);