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"
26 /*GlyphPoints::GlyphPoints(void)
28 GlyphPoints(0, 0, NULL, NULL, NULL, NULL);
31 GlyphPoints::GlyphPoints(int nPts/*=0*/, int nPlys/*=0*/, int * xa/*=null*/, int * ya/*=null*/,
32 bool * oca/*=null*/, uint16 * pa/*=null*/): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
33 //GlyphPoints::GlyphPoints(int nPts, int nPlys/*=0*/, int * xa/*=null*/, int * ya/*=null*/,
34 // bool * oca/*=null*/, uint16 * pa/*=null*/): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
36 AllocateAndCopy(nPts, nPlys, xa, ya, oca, pa);
41 polyEnd = new uint16[numPolys];
42 polyEnd[0] = numPoints - 1;
45 WriteLogMsg("GlyphPoints: Default constructor. %u points, %u polys.\n", numPoints, numPolys);
49 GlyphPoints::GlyphPoints(int xx, int yy, bool oc)
51 //Hmm. What to do with this...?
52 AllocateAndCopy(1, 0, &xx, &yy, &oc, NULL);
54 WriteLogMsg("GlyphPoints: Single point constructor. %u points, %u polys.\n", numPoints, numPolys);
58 // Copy constructor (needed for deep copying)
60 //GlyphPoints::GlyphPoints(GlyphPoints &c): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
61 GlyphPoints::GlyphPoints(const GlyphPoints &c): x(NULL), y(NULL), onCurve(NULL), polyEnd(NULL)
63 *this = c; // Use overloaded operator=
65 WriteLogMsg("GlyphPoints: Copy constructor. %u points, %u polys.\n", numPoints, numPolys);
69 GlyphPoints::~GlyphPoints()
84 void GlyphPoints::AllocateAndCopy(int nPts, int nPlys, int * xa, int * ya, bool * oca, uint16 * pa)
86 numPoints = nPts, numPolys = nPlys;
90 x = new int[numPoints];
91 y = new int[numPoints];
92 onCurve = new bool[numPoints];
94 if (xa) // Copy points in if they're passed in...
95 for(int i=0; i<nPts; i++)
99 for(int i=0; i<nPts; i++)
103 for(int i=0; i<nPts; i++)
109 polyEnd = new uint16[numPolys];
111 if (pa) // Copy poly ends in if they're passed in...
112 for(int i=0; i<nPlys; i++)
117 GlyphPoints& GlyphPoints::operator=(const GlyphPoints &c)
120 return *this; // Take care of self-assignment
134 AllocateAndCopy(c.numPoints, c.numPolys, c.x, c.y, c.onCurve, c.polyEnd);
139 // Add another GlyphPoints' points to this one...
141 GlyphPoints GlyphPoints::operator+(const GlyphPoints &c)
143 int totPoints = numPoints + c.numPoints, totPolys = numPolys + c.numPolys;
145 int * totX = new int[totPoints];
146 int * totY = new int[totPoints];
147 bool * totOnCurve = new bool[totPoints];
148 uint16 * totPolyEnd = new uint16[totPolys];
150 for(int i=0; i<numPoints; i++)
154 totOnCurve[i] = onCurve[i];
157 for(int i=0; i<numPolys; i++)
158 totPolyEnd[i] = polyEnd[i];
160 for(int i=0; i<c.numPoints; i++)
162 totX[numPoints+i] = c.x[i];
163 totY[numPoints+i] = c.y[i];
164 totOnCurve[numPoints+i] = c.onCurve[i];
167 for(int i=0; i<c.numPolys; i++)
168 totPolyEnd[numPolys+i] = numPoints + c.polyEnd[i]; // Need to adjust the "added in"
169 // poly's end points...
171 GlyphPoints retVal(totPoints, totPolys, totX, totY, totOnCurve, totPolyEnd);
180 // Add a point to this GlyphPoints...
182 GlyphPoints GlyphPoints::operator+(const IPoint &c)
184 //This is kinda silly. We can do better than this! !!! FIX !!!
185 int * totX = new int[numPoints + 1];
186 int * totY = new int[numPoints + 1];
187 bool * totOnCurve = new bool[numPoints + 1];
188 uint16 * totPolyEnd = new uint16[numPolys];
190 for(int i=0; i<numPoints; i++)
191 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
193 totX[numPoints] = c.x, totY[numPoints] = c.y, totOnCurve[numPoints] = c.onCurve;
195 for(int i=0; i<numPolys; i++)
196 totPolyEnd[i] = polyEnd[i];
198 totPolyEnd[numPolys - 1]++; // Bump polygon's end point
199 GlyphPoints retVal(numPoints + 1, numPolys, totX, totY, totOnCurve, totPolyEnd);
208 GlyphPoints& GlyphPoints::operator+=(const IPoint &p)
210 InsertPoint(numPoints, p.x, p.y, p.onCurve);
215 void GlyphPoints::InsertPoint(uint16 pt, int xx, int yy, bool oc)
217 if (pt > numPoints) // > because we can insert at end...!
218 throw GP_OUT_OF_RANGE;
220 //This is kinda silly. We can do better than this! !!! FIX !!!
221 int * totX = new int[numPoints + 1];
222 int * totY = new int[numPoints + 1];
223 bool * totOnCurve = new bool[numPoints + 1];
225 for(int i=0; i<pt; i++)
226 totX[i] = x[i], totY[i] = y[i], totOnCurve[i] = onCurve[i];
228 for(int i=pt; i<numPoints; i++)
229 totX[i + 1] = x[i], totY[i + 1] = y[i], totOnCurve[i + 1] = onCurve[i];
231 totX[pt] = xx, totY[pt] = yy, totOnCurve[pt] = oc;
233 //A way to fix the kludge in GetPoly() would be to put a check here to see if
234 //we're adding to the end of the structure: [DONE, below]
235 int polyInsert = (pt == numPoints ? numPolys - 1 : GetPoly(pt));
236 for(int i=polyInsert; i<numPolys; i++)
237 // for(int i=GetPoly(pt); i<numPolys; i++)
238 polyEnd[i]++; // Bump polygons' end point
246 x = totX, y = totY, onCurve = totOnCurve;
249 void GlyphPoints::InsertPoint(uint16 pt, const IPoint &p)
251 InsertPoint(pt, p.x, p.y, p.onCurve);
255 // Delete a point from the glyph
256 // Note that we don't bother to reallocate anything here, just bump the
257 // size counters down as needed. In the future, we'll keep track so we
258 // don't have to reallocate *every* damn time a point is added...
260 void GlyphPoints::DeletePoint(uint16 pt)
262 // Adjust polygon ends appropriately
263 uint16 poly = GetPoly(pt);
265 for(int i=poly; i<numPolys; i++)
268 //Need to check here if we're deleting the last point in the glyph or not.
270 if (GetNumPoints(poly) == 0 && numPoints > 0)
274 for(int i=poly; i<numPolys; i++)
275 polyEnd[i] = polyEnd[i + 1];
278 //This causes a crash becuz GetPoly() uses numPoints... !!! FIX !!! [DONE by switching poly delete & point delete]
279 // Close up the gap left by the current point
282 for(int i=pt; i<numPoints; i++)
283 x[i] = x[i + 1], y[i] = y[i + 1], onCurve[i] = onCurve[i + 1];
286 uint16 GlyphPoints::GetNumPoints(void)
291 uint16 GlyphPoints::GetNumPoints(uint16 poly)
293 if (poly >= numPolys)
296 WriteLogMsg("Exception: GetNumPoints(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
298 throw GP_OUT_OF_RANGE;
303 return polyEnd[poly] - (poly == 0 ? -1 : polyEnd[poly - 1]);
306 uint16 GlyphPoints::GetNumPolys(void)
311 int GlyphPoints::GetX(uint16 pt)
316 WriteLogMsg("Exception: GetX(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
318 throw GP_OUT_OF_RANGE;
326 int GlyphPoints::GetY(uint16 pt)
331 WriteLogMsg("Exception: GetY(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
333 throw GP_OUT_OF_RANGE;
341 bool GlyphPoints::GetOnCurve(uint16 pt)
346 WriteLogMsg("Exception: GetOnCurve(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
348 throw GP_OUT_OF_RANGE;
356 int GlyphPoints::GetX(uint16 poly, uint16 pt)
358 if (pt >= GetNumPoints(poly))
361 WriteLogMsg("Exception: GetX(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
363 throw GP_OUT_OF_RANGE;
368 return x[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
371 int GlyphPoints::GetNextX(uint16 poly, uint16 pt)
373 return GetX(poly, GetNext(poly, pt));
376 int GlyphPoints::GetY(uint16 poly, uint16 pt)
378 if (pt >= GetNumPoints(poly))
381 WriteLogMsg("Exception: GetY(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
383 throw GP_OUT_OF_RANGE;
388 return y[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
391 int GlyphPoints::GetNextY(uint16 poly, uint16 pt)
393 return GetY(poly, GetNext(poly, pt));
396 IPoint GlyphPoints::GetPoint(uint16 poly, uint16 pt)
398 return IPoint(GetX(poly, pt), GetY(poly, pt));
401 bool GlyphPoints::GetOnCurve(uint16 poly, uint16 pt)
403 if (pt >= GetNumPoints(poly))
406 WriteLogMsg("Exception: GetOnCurve(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
408 throw GP_OUT_OF_RANGE;
413 return onCurve[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
416 bool GlyphPoints::GetPrevOnCurve(uint16 poly, uint16 pt)
418 return GetOnCurve(poly, GetPrev(poly, pt));
421 bool GlyphPoints::GetNextOnCurve(uint16 poly, uint16 pt)
423 return GetOnCurve(poly, GetNext(poly, pt));
426 uint16 GlyphPoints::GetPolyEnd(uint16 poly)
428 if (poly >= numPolys)
431 WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
433 throw GP_OUT_OF_RANGE;
438 return polyEnd[poly];
441 void GlyphPoints::OffsetPoints(int xOff, int yOff)
443 for(int i=0; i<numPoints; i++)
444 x[i] += xOff, y[i] += yOff;
448 // Offset only a specific polygon in the glyph
450 void GlyphPoints::OffsetPoly(uint16 poly, int32 xOff, int32 yOff)
452 if (poly >= numPolys)
455 WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
457 throw GP_OUT_OF_RANGE;
462 uint16 polyStart = (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
464 for(int i=0; i<GetNumPoints(poly); i++)
465 x[polyStart + i] += xOff, y[polyStart + i] += yOff;
468 void GlyphPoints::ScalePoints(float sc)
470 for(int i=0; i<numPoints; i++)
471 x[i] = (int)(((float)x[i] * sc) + 0.5f),
472 y[i] = (int)(((float)y[i] * sc) + 0.5f);
475 void GlyphPoints::SetXY(uint16 pt, int xx, int yy)
480 WriteLogMsg("Exception: SetXY(uint16, int, int). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
482 throw GP_OUT_OF_RANGE;
487 x[pt] = xx, y[pt] = yy;
490 void GlyphPoints::SetOnCurve(uint16 pt, bool oc)
495 WriteLogMsg("Exception: SetOnCurve(uint16, bool). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
497 throw GP_OUT_OF_RANGE;
505 uint16 GlyphPoints::GetPrev(uint16 pt)
507 // pt = 7, polyEnd = 4, 9, 15
508 uint16 min = 0, max = numPoints - 1;
510 for(int i=0; i<numPolys; i++)
512 if (pt <= polyEnd[i])
515 min = polyEnd[i - 1] + 1;
522 uint16 retVal = pt - 1;
530 uint16 GlyphPoints::GetNext(uint16 pt)
532 uint16 min = 0, max = numPoints - 1;
534 for(int i=0; i<numPolys; i++)
536 if (pt <= polyEnd[i])
539 min = polyEnd[i - 1] + 1;
546 uint16 retVal = pt + 1;
555 // Get previous point for this polygon using wraparound.
556 // Note that pt is a zero-based index!
558 uint16 GlyphPoints::GetPrev(uint16 poly, uint16 pt)
560 return (pt == 0 ? GetNumPoints(poly) - 1 : pt - 1);
564 // Get next point for this polygon using wraparound.
565 // Note that pt is a zero-based index!
567 uint16 GlyphPoints::GetNext(uint16 poly, uint16 pt)
569 return (pt == GetNumPoints(poly) - 1 ? 0 : pt + 1);
572 uint16 GlyphPoints::GetPoly(uint16 pt)
577 WriteLogMsg("Exception: GetPoly(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
579 throw GP_OUT_OF_RANGE;
584 for(int i=0; i<numPolys; i++)
585 if (pt <= polyEnd[i])
591 void GlyphPoints::AddNewPolyAtEnd(void)
593 if (numPoints == 0) // By default, we already *have* a poly
596 uint16 * newPolyEnd = new uint16[numPolys + 1];
598 for(uint16 i=0; i<numPolys; i++)
599 newPolyEnd[i] = polyEnd[i];
601 newPolyEnd[numPolys] = newPolyEnd[numPolys - 1];
604 polyEnd = newPolyEnd;
607 IPoint GlyphPoints::GetMidpointToPrev(uint16 poly, uint16 pt)
609 uint16 prev = GetPrev(poly, pt);
611 int32 x1 = GetX(poly, pt), y1 = GetY(poly, pt);
612 int32 x2 = GetX(poly, prev), y2 = GetY(poly, prev);
614 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
617 IPoint GlyphPoints::GetMidpointToNext(uint16 poly, uint16 pt)
619 uint16 next = GetNext(poly, pt);
621 int32 x1 = GetX(poly, pt), y1 = GetY(poly, pt);
622 int32 x2 = GetX(poly, next), y2 = GetY(poly, next);
624 return IPoint((x1 + x2) / 2.0f, (y1 + y2) / 2.0f);
627 IPoint GlyphPoints::GetPrevPoint(uint16 poly, uint16 pt)
629 uint16 prevPt = GetPrev(poly, pt);
631 return IPoint(GetX(poly, prevPt), GetY(poly, prevPt));
634 IPoint GlyphPoints::GetNextPoint(uint16 poly, uint16 pt)
636 uint16 nextPt = GetNext(poly, pt);
638 return IPoint(GetX(poly, nextPt), GetY(poly, nextPt));