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::GetY(uint16 poly, uint16 pt)
373 if (pt >= GetNumPoints(poly))
376 WriteLogMsg("Exception: GetY(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
378 throw GP_OUT_OF_RANGE;
383 return y[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
386 bool GlyphPoints::GetOnCurve(uint16 poly, uint16 pt)
388 if (pt >= GetNumPoints(poly))
391 WriteLogMsg("Exception: GetOnCurve(uint16, uint16). poly= %u, pt=%u, numPoints=%u\xD\xA", poly, pt, numPoints);
393 throw GP_OUT_OF_RANGE;
398 return onCurve[pt + (poly == 0 ? 0 : polyEnd[poly - 1] + 1)];
401 uint16 GlyphPoints::GetPolyEnd(uint16 poly)
403 if (poly >= numPolys)
406 WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
408 throw GP_OUT_OF_RANGE;
413 return polyEnd[poly];
416 void GlyphPoints::OffsetPoints(int xOff, int yOff)
418 for(int i=0; i<numPoints; i++)
419 x[i] += xOff, y[i] += yOff;
423 // Offset only a specific polygon in the glyph
425 void GlyphPoints::OffsetPoly(uint16 poly, int32 xOff, int32 yOff)
427 if (poly >= numPolys)
430 WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
432 throw GP_OUT_OF_RANGE;
437 uint16 polyStart = (poly == 0 ? 0 : polyEnd[poly - 1] + 1);
439 for(int i=0; i<GetNumPoints(poly); i++)
440 x[polyStart + i] += xOff, y[polyStart + i] += yOff;
443 void GlyphPoints::ScalePoints(float sc)
445 for(int i=0; i<numPoints; i++)
446 x[i] = (int)(((float)x[i] * sc) + 0.5f),
447 y[i] = (int)(((float)y[i] * sc) + 0.5f);
450 void GlyphPoints::SetXY(uint16 pt, int xx, int yy)
455 WriteLogMsg("Exception: SetXY(uint16, int, int). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
457 throw GP_OUT_OF_RANGE;
462 x[pt] = xx, y[pt] = yy;
465 void GlyphPoints::SetOnCurve(uint16 pt, bool oc)
470 WriteLogMsg("Exception: SetOnCurve(uint16, bool). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
472 throw GP_OUT_OF_RANGE;
480 uint16 GlyphPoints::GetPrev(uint16 pt)
482 // pt = 7, polyEnd = 4, 9, 15
483 uint16 min = 0, max = numPoints - 1;
485 for(int i=0; i<numPolys; i++)
487 if (pt <= polyEnd[i])
490 min = polyEnd[i - 1] + 1;
497 uint16 retVal = pt - 1;
505 uint16 GlyphPoints::GetNext(uint16 pt)
507 uint16 min = 0, max = numPoints - 1;
509 for(int i=0; i<numPolys; i++)
511 if (pt <= polyEnd[i])
514 min = polyEnd[i - 1] + 1;
521 uint16 retVal = pt + 1;
530 // Get previous point for this polygon using wraparound.
531 // Note that pt is a zero-based index!
533 uint16 GlyphPoints::GetPrev(uint16 poly, uint16 pt)
535 return (pt == 0 ? GetNumPoints(poly) - 1 : pt - 1);
539 // Get next point for this polygon using wraparound.
540 // Note that pt is a zero-based index!
542 uint16 GlyphPoints::GetNext(uint16 poly, uint16 pt)
544 return (pt == GetNumPoints(poly) - 1 ? 0 : pt + 1);
547 uint16 GlyphPoints::GetPoly(uint16 pt)
552 WriteLogMsg("Exception: GetPoly(uint16). pt=%u, numPoints=%u\xD\xA", pt, numPoints);
554 throw GP_OUT_OF_RANGE;
559 for(int i=0; i<numPolys; i++)
560 if (pt <= polyEnd[i])
566 void GlyphPoints::AddNewPolyAtEnd(void)
568 if (numPoints == 0) // By default, we already *have* a poly
571 uint16 * newPolyEnd = new uint16[numPolys + 1];
573 for(uint16 i=0; i<numPolys; i++)
574 newPolyEnd[i] = polyEnd[i];
576 newPolyEnd[numPolys] = newPolyEnd[numPolys - 1];
579 polyEnd = newPolyEnd;