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 //wouldn't it be better to treat this case as inserting at the end?
251 if (pt > numPoints) // > because we can insert at end...!
252 throw GP_OUT_OF_RANGE;
254 //This is kinda silly. We can do better than this! !!! FIX !!!
255 int * totX = new int[numPoints + 1];
256 int * totY = new int[numPoints + 1];
257 bool * totOnCurve = new bool[numPoints + 1];
259 for(int i=0; i<pt; i++)
260 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
262 for(int i=pt; i<numPoints; i++)
263 totX[i + 1] = x[i], totY[i + 1] = y[i], totOnCurve[i + 1] = onCurve[i];
265 totX[pt] = xx, totY[pt] = yy, totOnCurve[pt] = oc;
267 //A way to fix the kludge in GetPoly() would be to put a check here to see if
268 //we're adding to the end of the structure: [DONE, below]
269 int polyInsert = (pt == numPoints ? numPolys - 1 : GetPoly(pt));
270 for(int i=polyInsert; 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 Vector GlyphPoints::GetXY(uint16_t pt)
387 WriteLogMsg("Exception: GetXY(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
389 throw GP_OUT_OF_RANGE;
394 return Vector(x[pt], y[pt]);
398 bool GlyphPoints::GetOnCurve(uint16_t pt)
403 WriteLogMsg("Exception: GetOnCurve(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
405 throw GP_OUT_OF_RANGE;
414 int GlyphPoints::GetX(uint16_t poly, uint16_t pt)
416 if (pt >= GetNumPoints(poly))
419 WriteLogMsg("Exception: GetX(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
421 throw GP_OUT_OF_RANGE;
426 return x[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
430 int GlyphPoints::GetNextX(uint16_t poly, uint16_t pt)
432 return GetX(poly, GetNext(poly, pt));
436 int GlyphPoints::GetY(uint16_t poly, uint16_t pt)
438 if (pt >= GetNumPoints(poly))
441 WriteLogMsg("Exception: GetY(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
443 throw GP_OUT_OF_RANGE;
448 return y[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
452 int GlyphPoints::GetNextY(uint16_t poly, uint16_t pt)
454 return GetY(poly, GetNext(poly, pt));
458 IPoint GlyphPoints::GetPoint(uint16_t poly, uint16_t pt)
460 return IPoint(GetX(poly, pt), GetY(poly, pt), GetOnCurve(poly, pt));
464 IPoint GlyphPoints::GetPoint(uint16_t pointNumber)
466 if (pointNumber > numPoints)
467 throw GP_OUT_OF_RANGE;
469 return IPoint(x[pointNumber], y[pointNumber], onCurve[pointNumber]);
473 bool GlyphPoints::GetOnCurve(uint16_t poly, uint16_t pt)
475 if (pt >= GetNumPoints(poly))
478 WriteLogMsg("Exception: GetOnCurve(uint16_t, uint16_t). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
480 throw GP_OUT_OF_RANGE;
485 return onCurve[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
489 bool GlyphPoints::GetPrevOnCurve(uint16_t poly, uint16_t pt)
491 return GetOnCurve(poly, GetPrev(poly, pt));
495 bool GlyphPoints::GetNextOnCurve(uint16_t poly, uint16_t pt)
497 return GetOnCurve(poly, GetNext(poly, pt));
501 uint16_t GlyphPoints::GetPolyStart(uint16_t poly)
503 if (poly >= numPolys)
506 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
508 throw GP_OUT_OF_RANGE;
513 // If it's poly 0, return 0. Otherwise, get the previous poly's end & add one to it
514 return (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
518 uint16_t GlyphPoints::GetPolyEnd(uint16_t poly)
520 if (poly >= numPolys)
523 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
525 throw GP_OUT_OF_RANGE;
530 return polyEnd[poly];
534 void GlyphPoints::OffsetPoints(int xOff, int yOff)
536 for(int i=0; i<numPoints; i++)
537 x[i] += xOff, y[i] += yOff;
542 // Offset only a specific polygon in the glyph
544 void GlyphPoints::OffsetPoly(uint16_t poly, int32_t xOff, int32_t yOff)
546 if (poly >= numPolys)
549 WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
551 throw GP_OUT_OF_RANGE;
556 uint16_t polyStart = (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
558 for(int i=0; i<GetNumPoints(poly); i++)
559 x[polyStart + i] += xOff, y[polyStart + i] += yOff;
563 void GlyphPoints::ScalePoints(float sc)
565 for(int i=0; i<numPoints; i++)
566 x[i] = (int)(((float)x[i] * sc) + 0.5f),
567 y[i] = (int)(((float)y[i] * sc) + 0.5f);
571 void GlyphPoints::SetXY(uint16_t pt, int xx, int yy)
576 WriteLogMsg("Exception: SetXY(uint16_t, int, int). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
578 throw GP_OUT_OF_RANGE;
583 x[pt] = xx, y[pt] = yy;
587 void GlyphPoints::SetOnCurve(uint16_t pt, bool oc)
592 WriteLogMsg("Exception: SetOnCurve(uint16_t, bool). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
594 throw GP_OUT_OF_RANGE;
603 void GlyphPoints::SetPoint(const uint16_t pointNum, const IPoint point)
605 if (pointNum >= numPoints)
608 WriteLogMsg("Exception: SetPoint(uint16_t, IPoint). pt=%u, numPoints=%u\xD\xA", pointNum, numPoints);
610 throw GP_OUT_OF_RANGE;
615 x[pointNum] = point.x, y[pointNum] = point.y, onCurve[pointNum] = point.onCurve;
619 uint16_t GlyphPoints::GetPrev(uint16_t pt)
621 // pt = 7, polyEnd = 4, 9, 15
622 uint16_t min = 0, max = numPoints - 1;
624 for(int i=0; i<numPolys; i++)
626 if (pt <= polyEnd[i])
629 min = polyEnd[i - 1] + 1;
636 uint16_t retVal = pt - 1;
645 uint16_t GlyphPoints::GetNext(uint16_t pt)
647 uint16_t min = 0, max = numPoints - 1;
649 for(int i=0; i<numPolys; i++)
651 if (pt <= polyEnd[i])
654 min = polyEnd[i - 1] + 1;
661 uint16_t retVal = pt + 1;
671 // Get previous point for this polygon using wraparound.
672 // Note that pt is a zero-based index!
674 uint16_t GlyphPoints::GetPrev(uint16_t poly, uint16_t pt)
676 return (pt == 0 ? GetNumPoints(poly) - 1 : pt - 1);
681 // Get next point for this polygon using wraparound.
682 // Note that pt is a zero-based index!
684 uint16_t GlyphPoints::GetNext(uint16_t poly, uint16_t pt)
686 return (pt == GetNumPoints(poly) - 1 ? 0 : pt + 1);
690 #warning "!!! This function returns incorrect results !!!"
691 uint16_t GlyphPoints::GetPoly(uint16_t pt)
696 WriteLogMsg("Exception: GetPoly(uint16_t). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
698 throw GP_OUT_OF_RANGE;
703 for(int i=0; i<numPolys; i++)
704 if (pt <= polyEnd[i])
711 void GlyphPoints::AddNewPolyAtEnd(void)
713 if (numPoints == 0) // By default, we already *have* a poly
716 uint16_t * newPolyEnd = new uint16_t[numPolys + 1];
718 for(uint16_t i=0; i<numPolys; i++)
719 newPolyEnd[i] = polyEnd[i];
721 newPolyEnd[numPolys] = newPolyEnd[numPolys - 1];
724 polyEnd = newPolyEnd;
728 IPoint GlyphPoints::GetMidpointToPrev(uint16_t poly, uint16_t pt)
730 uint16_t prev = GetPrev(poly, pt);
732 int32_t x1 = GetX(poly, pt), y1 = GetY(poly, pt);
733 int32_t x2 = GetX(poly, prev), y2 = GetY(poly, prev);
735 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
739 IPoint GlyphPoints::GetMidpointToNext(uint16_t poly, uint16_t pt)
741 uint16_t next = GetNext(poly, pt);
743 int32_t x1 = GetX(poly, pt), y1 = GetY(poly, pt);
744 int32_t x2 = GetX(poly, next), y2 = GetY(poly, next);
746 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
750 IPoint GlyphPoints::GetPrevPoint(uint16_t poly, uint16_t pt)
752 uint16_t prevPt = GetPrev(poly, pt);
754 return IPoint(GetX(poly, prevPt), GetY(poly, prevPt));
758 IPoint GlyphPoints::GetNextPoint(uint16_t poly, uint16_t pt)
760 uint16_t nextPt = GetNext(poly, pt);
762 return IPoint(GetX(poly, nextPt), GetY(poly, nextPt));
766 uint16_t GlyphPoints::GetPolyForPoint(IPoint point)
770 for(uint16_t i=0; i<numPoints; i++)
772 if (i > polyEnd[poly])
775 if (IPoint(x[i], y[i]) == point)
783 uint16_t GlyphPoints::GetPolyForPointNumber(uint16_t pointNumber)
785 // If there's only one poly, we know where the point is...
789 // Otherwise, do a linear search through the polys to find the right one
790 for(uint16_t i=0; i<numPolys; i++)
792 if (pointNumber >= GetPolyStart(i) && pointNumber <= polyEnd[i])
801 // Rotate a point by "angle" around point "center"
803 IPoint GlyphPoints::RotatePoint(const double angle, const IPoint point, const IPoint center)
805 // Translate the point to the origin
806 double xt = (double)(point.x - center.x);
807 double yt = (double)(point.y - center.y);
809 // Rotate the point by angle
810 double xr = (xt * cos(angle)) - (yt * sin(angle));
811 double yr = (xt * sin(angle)) + (yt * cos(angle));
813 // Translate it back...
815 rotated.x = (int)(xr + 0.5) + center.x;
816 rotated.y = (int)(yr + 0.5) + center.y;
822 // Rotate all points in the glyph by "angle" around point "pt"
824 void GlyphPoints::RotatePoints(const double angle, const IPoint pt)
826 for(int i=0; i<numPoints; i++)
828 // Translate the point to the origin
829 double xt = (double)(x[i] - pt.x);
830 double yt = (double)(y[i] - pt.y);
832 // Rotate the point by angle
833 double xr = (xt * cos(angle)) - (yt * sin(angle));
834 double yr = (xt * sin(angle)) + (yt * cos(angle));
837 x[i] = (int)(xr + 0.5) + pt.x;
838 y[i] = (int)(yr + 0.5) + pt.y;
843 IPoint GlyphPoints::GetPolyCentroid(const int16_t poly)
845 // We should throw an exception here, but meh
846 // (this actually short circuits the exception handling in all the GetPolyXXX() functions)
848 if (poly >= numPolys)
849 throw GP_OUT_OF_RANGE;
850 // return IPoint(0, 0);
852 // if (poly >= numPolys)
855 //WriteLogMsg("Exception: GetPolyEnd(uint16_t). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
857 // throw GP_OUT_OF_RANGE;
862 IPoint centroid; // Initializes to (0, 0)
863 uint16_t numPointsInPoly = GetNumPoints(poly);
865 for(uint16_t i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
871 centroid.x /= numPointsInPoly;
872 centroid.y /= numPointsInPoly;
878 void GlyphPoints::RotatePolyAroundCentroid(const int16_t poly, const double angle)
880 if (poly >= numPolys)
883 IPoint centroid = GetPolyCentroid(poly);
885 for(uint16_t i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
887 IPoint rotated = RotatePoint(angle, IPoint(x[i], y[i]), centroid);
894 void GlyphPoints::InvertPolyDrawSequence(const uint16_t poly)
896 if (poly >= numPolys)
897 throw GP_OUT_OF_RANGE;
899 uint16_t pointNum1 = GetPolyStart(poly);
900 uint16_t pointNum2 = GetPolyEnd(poly);
902 // Algorithm: Step through points in the polygon, swapping 1st and last, then
903 // 2nd and (last - 1), 3rd and (last - 2), etc. We only do this for half the
904 // points, as doing it for all would undo the swapping we did in the 1st half.
905 for(uint16_t i=0; i<GetNumPoints(poly)/2; i++)
907 IPoint point1 = GetPoint(pointNum1 + i);
908 IPoint point2 = GetPoint(pointNum2 - i);
909 SetPoint(pointNum2 - i, point1);
910 SetPoint(pointNum1 + i, point2);
915 // really need to do checking on the results from fscanf...
916 bool GlyphPoints::LoadGlyphFromFile(FILE * file)
921 FreeAllocatedMemory();
923 fscanf(file, "%s V%f", line, &version);
924 fscanf(file, "%s %u", line, &numPoints);
925 x = new int[numPoints];
926 y = new int[numPoints];
927 onCurve = new bool[numPoints];
929 for(int i=0; i<numPoints; i++)
931 fscanf(file, "%d %d %s", &x[i], &y[i], (char *)&line);
932 onCurve[i] = (line[0] == 'T' ? true : false);
935 fscanf(file, "%s %u", line, &numPolys);
936 polyEnd = new uint16_t[numPolys];
938 for(int i=0; i<numPolys; i++)
940 fscanf(file, "%u", &polyEnd[i]);
947 bool GlyphPoints::SaveGlyphToFile(FILE * file)
949 // GlyphPoints glyph = editWnd->pts;
950 fprintf(file, "TTEGLYPH V1.0\n");
951 fprintf(file, "POINTS %u\n", numPoints);
953 for(int i=0; i<numPoints; i++)
955 fprintf(file, "%d %d %s\n", x[i], y[i], (onCurve[i] ? "T" : "F"));
958 fprintf(file, "POLYS %u\n", numPolys);
960 for(int i=0; i<numPolys; i++)
962 fprintf(file, "%u\n", polyEnd[i]);