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), GetOnCurve(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], onCurve[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 void GlyphPoints::SetPoint(const uint16 pointNum, const IPoint point)
585 if (pointNum >= numPoints)
588 WriteLogMsg("Exception: SetPoint(uint16, IPoint). pt=%u, numPoints=%u\xD\xA", pointNum, numPoints);
590 throw GP_OUT_OF_RANGE;
595 x[pointNum] = point.x, y[pointNum] = point.y, onCurve[pointNum] = point.onCurve;
599 uint16 GlyphPoints::GetPrev(uint16 pt)
601 // pt = 7, polyEnd = 4, 9, 15
602 uint16 min = 0, max = numPoints - 1;
604 for(int i=0; i<numPolys; i++)
606 if (pt <= polyEnd[i])
609 min = polyEnd[i - 1] + 1;
616 uint16 retVal = pt - 1;
625 uint16 GlyphPoints::GetNext(uint16 pt)
627 uint16 min = 0, max = numPoints - 1;
629 for(int i=0; i<numPolys; i++)
631 if (pt <= polyEnd[i])
634 min = polyEnd[i - 1] + 1;
641 uint16 retVal = pt + 1;
651 // Get previous point for this polygon using wraparound.
652 // Note that pt is a zero-based index!
654 uint16 GlyphPoints::GetPrev(uint16 poly, uint16 pt)
656 return (pt == 0 ? GetNumPoints(poly) - 1 : pt - 1);
661 // Get next point for this polygon using wraparound.
662 // Note that pt is a zero-based index!
664 uint16 GlyphPoints::GetNext(uint16 poly, uint16 pt)
666 return (pt == GetNumPoints(poly) - 1 ? 0 : pt + 1);
670 #warning "!!! This function returns incorrect results !!!"
671 uint16 GlyphPoints::GetPoly(uint16 pt)
676 WriteLogMsg("Exception: GetPoly(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
678 throw GP_OUT_OF_RANGE;
683 for(int i=0; i<numPolys; i++)
684 if (pt <= polyEnd[i])
691 void GlyphPoints::AddNewPolyAtEnd(void)
693 if (numPoints == 0) // By default, we already *have* a poly
696 uint16 * newPolyEnd = new uint16[numPolys + 1];
698 for(uint16 i=0; i<numPolys; i++)
699 newPolyEnd[i] = polyEnd[i];
701 newPolyEnd[numPolys] = newPolyEnd[numPolys - 1];
704 polyEnd = newPolyEnd;
708 IPoint GlyphPoints::GetMidpointToPrev(uint16 poly, uint16 pt)
710 uint16 prev = GetPrev(poly, pt);
712 int32 x1 = GetX(poly, pt), y1 = GetY(poly, pt);
713 int32 x2 = GetX(poly, prev), y2 = GetY(poly, prev);
715 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
719 IPoint GlyphPoints::GetMidpointToNext(uint16 poly, uint16 pt)
721 uint16 next = GetNext(poly, pt);
723 int32 x1 = GetX(poly, pt), y1 = GetY(poly, pt);
724 int32 x2 = GetX(poly, next), y2 = GetY(poly, next);
726 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
730 IPoint GlyphPoints::GetPrevPoint(uint16 poly, uint16 pt)
732 uint16 prevPt = GetPrev(poly, pt);
734 return IPoint(GetX(poly, prevPt), GetY(poly, prevPt));
738 IPoint GlyphPoints::GetNextPoint(uint16 poly, uint16 pt)
740 uint16 nextPt = GetNext(poly, pt);
742 return IPoint(GetX(poly, nextPt), GetY(poly, nextPt));
746 uint16 GlyphPoints::GetPolyForPoint(IPoint point)
750 for(uint16 i=0; i<numPoints; i++)
752 if (i > polyEnd[poly])
755 if (IPoint(x[i], y[i]) == point)
763 uint16 GlyphPoints::GetPolyForPointNumber(uint16 pointNumber)
765 // If there's only one poly, we know where the point is...
769 // Otherwise, do a linear search through the polys to find the right one
770 for(uint16 i=0; i<numPolys; i++)
772 if (pointNumber >= GetPolyStart(i) && pointNumber <= polyEnd[i])
781 // Rotate a point by "angle" around point "center"
783 IPoint GlyphPoints::RotatePoint(const double angle, const IPoint point, const IPoint center)
785 // Translate the point to the origin
786 double xt = (double)(point.x - center.x);
787 double yt = (double)(point.y - center.y);
789 // Rotate the point by angle
790 double xr = (xt * cos(angle)) - (yt * sin(angle));
791 double yr = (xt * sin(angle)) + (yt * cos(angle));
793 // Translate it back...
795 rotated.x = (int)(xr + 0.5) + center.x;
796 rotated.y = (int)(yr + 0.5) + center.y;
802 // Rotate all points in the glyph by "angle" around point "pt"
804 void GlyphPoints::RotatePoints(const double angle, const IPoint pt)
806 for(int i=0; i<numPoints; i++)
808 // Translate the point to the origin
809 double xt = (double)(x[i] - pt.x);
810 double yt = (double)(y[i] - pt.y);
812 // Rotate the point by angle
813 double xr = (xt * cos(angle)) - (yt * sin(angle));
814 double yr = (xt * sin(angle)) + (yt * cos(angle));
817 x[i] = (int)(xr + 0.5) + pt.x;
818 y[i] = (int)(yr + 0.5) + pt.y;
823 IPoint GlyphPoints::GetPolyCentroid(const int16 poly)
825 // We should throw an exception here, but meh
826 // (this actually short circuits the exception handling in all the GetPolyXXX() functions)
828 if (poly >= numPolys)
829 throw GP_OUT_OF_RANGE;
830 // return IPoint(0, 0);
832 // if (poly >= numPolys)
835 //WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
837 // throw GP_OUT_OF_RANGE;
842 IPoint centroid; // Initializes to (0, 0)
843 uint16 numPointsInPoly = GetNumPoints(poly);
845 for(uint16 i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
851 centroid.x /= numPointsInPoly;
852 centroid.y /= numPointsInPoly;
858 void GlyphPoints::RotatePolyAroundCentroid(const int16 poly, const double angle)
860 if (poly >= numPolys)
863 IPoint centroid = GetPolyCentroid(poly);
865 for(uint16 i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
867 IPoint rotated = RotatePoint(angle, IPoint(x[i], y[i]), centroid);
874 void GlyphPoints::InvertPolyDrawSequence(const uint16 poly)
876 if (poly >= numPolys)
877 throw GP_OUT_OF_RANGE;
879 uint16 pointNum1 = GetPolyStart(poly);
880 uint16 pointNum2 = GetPolyEnd(poly);
882 // Algorithm: Step through points in the polygon, swapping 1st and last, then
883 // 2nd and (last - 1), 3rd and (last - 2), etc. We only do this for half the
884 // points, as doing it for all would undo the swapping we did in the 1st half.
885 for(uint16 i=0; i<GetNumPoints(poly)/2; i++)
887 IPoint point1 = GetPoint(pointNum1 + i);
888 IPoint point2 = GetPoint(pointNum2 - i);
889 SetPoint(pointNum2 - i, point1);
890 SetPoint(pointNum1 + i, point2);
895 // really need to do checking on the results from fscanf...
896 bool GlyphPoints::LoadGlyphFromFile(FILE * file)
901 FreeAllocatedMemory();
903 fscanf(file, "%s V%f", line, &version);
904 fscanf(file, "%s %u", line, &numPoints);
905 x = new int[numPoints];
906 y = new int[numPoints];
907 onCurve = new bool[numPoints];
909 for(int i=0; i<numPoints; i++)
911 fscanf(file, "%d %d %s", &x[i], &y[i], (char *)&line);
912 onCurve[i] = (line[0] == 'T' ? true : false);
915 fscanf(file, "%s %u", line, &numPolys);
916 polyEnd = new uint16[numPolys];
918 for(int i=0; i<numPolys; i++)
920 fscanf(file, "%u", &polyEnd[i]);
927 bool GlyphPoints::SaveGlyphToFile(FILE * file)
929 // GlyphPoints glyph = editWnd->pts;
930 fprintf(file, "TTEGLYPH V1.0\n");
931 fprintf(file, "POINTS %u\n", numPoints);
933 for(int i=0; i<numPoints; i++)
935 fprintf(file, "%d %d %s\n", x[i], y[i], (onCurve[i] ? "T" : "F"));
938 fprintf(file, "POLYS %u\n", numPolys);
940 for(int i=0; i<numPolys; i++)
942 fprintf(file, "%u\n", polyEnd[i]);