]> Shamusworld >> Repos - ttedit/blobdiff - src/glyphpoints.cpp
Added implementation of Polygon Rotate and Flip Winding tools.
[ttedit] / src / glyphpoints.cpp
index e6dc52c11ca5ec7ffef3dad62ddcbef2fa1c0fe0..a11b09ca3c5b82ba0221a832ab3052eefefda0cd 100755 (executable)
@@ -145,19 +145,6 @@ GlyphPoints& GlyphPoints::operator=(const GlyphPoints &c)
        if (this == &c)
                return *this;                                                   // Take care of self-assignment
 
-#if 0
-       if (x)
-               delete[] x;
-
-       if (y)
-               delete[] y;
-
-       if (onCurve)
-               delete[] onCurve;
-
-       if (polyEnd)
-               delete[] polyEnd;
-#endif
        FreeAllocatedMemory();
        AllocateAndCopy(c.numPoints, c.numPolys, c.x, c.y, c.onCurve, c.polyEnd);
 
@@ -450,7 +437,16 @@ int GlyphPoints::GetNextY(uint16 poly, uint16 pt)
 
 IPoint GlyphPoints::GetPoint(uint16 poly, uint16 pt)
 {
-       return IPoint(GetX(poly, pt), GetY(poly, pt));
+       return IPoint(GetX(poly, pt), GetY(poly, pt), GetOnCurve(poly, pt));
+}
+
+
+IPoint GlyphPoints::GetPoint(uint16 pointNumber)
+{
+       if (pointNumber > numPoints)
+               throw GP_OUT_OF_RANGE;
+
+       return IPoint(x[pointNumber], y[pointNumber], onCurve[pointNumber]);
 }
 
 
@@ -584,6 +580,22 @@ WriteLogMsg("Exception: SetOnCurve(uint16, bool). pt=%u, numPoints=%u\xD\xA", pt
 }
 
 
+void GlyphPoints::SetPoint(const uint16 pointNum, const IPoint point)
+{
+       if (pointNum >= numPoints)
+#ifdef DEBUG
+{
+WriteLogMsg("Exception: SetPoint(uint16, IPoint). pt=%u, numPoints=%u\xD\xA", pointNum, numPoints);
+#endif
+               throw GP_OUT_OF_RANGE;
+#ifdef DEBUG
+}
+#endif
+
+       x[pointNum] = point.x, y[pointNum] = point.y, onCurve[pointNum] = point.onCurve;
+}
+
+
 uint16 GlyphPoints::GetPrev(uint16 pt)
 {
 // pt = 7, polyEnd = 4, 9, 15
@@ -655,6 +667,7 @@ uint16 GlyphPoints::GetNext(uint16 poly, uint16 pt)
 }
 
 
+#warning "!!! This function returns incorrect results !!!"
 uint16 GlyphPoints::GetPoly(uint16 pt)
 {
        if (pt >= numPoints)
@@ -730,6 +743,40 @@ IPoint GlyphPoints::GetNextPoint(uint16 poly, uint16 pt)
 }
 
 
+uint16 GlyphPoints::GetPolyForPoint(IPoint point)
+{
+       uint16 poly = 0;
+
+       for(uint16 i=0; i<numPoints; i++)
+       {
+               if (i > polyEnd[poly])
+                       poly++;
+
+               if (IPoint(x[i], y[i]) == point)
+                       return poly;
+       }
+
+       return 0xFFFF;
+}
+
+
+uint16 GlyphPoints::GetPolyForPointNumber(uint16 pointNumber)
+{
+       // If there's only one poly, we know where the point is...
+       if (numPolys <= 1)
+               return 0;
+
+       // Otherwise, do a linear search through the polys to find the right one
+       for(uint16 i=0; i<numPolys; i++)
+       {
+               if (pointNumber >= GetPolyStart(i) && pointNumber <= polyEnd[i])
+                       return i;
+       }
+
+       return 0xFFFF;
+}
+
+
 //
 // Rotate a point by "angle" around point "center"
 //
@@ -777,8 +824,10 @@ IPoint GlyphPoints::GetPolyCentroid(const int16 poly)
 {
        // We should throw an exception here, but meh
        // (this actually short circuits the exception handling in all the GetPolyXXX() functions)
+       // [now we do!]
        if (poly >= numPolys)
-               return IPoint(0, 0);
+               throw GP_OUT_OF_RANGE;
+//             return IPoint(0, 0);
 
 //     if (poly >= numPolys)
 //#ifdef DEBUG
@@ -822,6 +871,27 @@ void GlyphPoints::RotatePolyAroundCentroid(const int16 poly, const double angle)
 }
 
 
+void GlyphPoints::InvertPolyDrawSequence(const uint16 poly)
+{
+       if (poly >= numPolys)
+               throw GP_OUT_OF_RANGE;
+
+       uint16 pointNum1 = GetPolyStart(poly);
+       uint16 pointNum2 = GetPolyEnd(poly);
+
+       // Algorithm: Step through points in the polygon, swapping 1st and last, then
+       // 2nd and (last - 1), 3rd and (last - 2), etc. We only do this for half the
+       // points, as doing it for all would undo the swapping we did in the 1st half.
+       for(uint16 i=0; i<GetNumPoints(poly)/2; i++)
+       {
+               IPoint point1 = GetPoint(pointNum1 + i);
+               IPoint point2 = GetPoint(pointNum2 - i);
+               SetPoint(pointNum2 - i, point1);
+               SetPoint(pointNum1 + i, point2);
+       }
+}
+
+
 // really need to do checking on the results from fscanf...
 bool GlyphPoints::LoadGlyphFromFile(FILE * file)
 {
@@ -838,7 +908,7 @@ bool GlyphPoints::LoadGlyphFromFile(FILE * file)
 
        for(int i=0; i<numPoints; i++)
        {
-               fscanf(file, "%d %d %s", &x[i], &y[i], &line);
+               fscanf(file, "%d %d %s", &x[i], &y[i], (char *)&line);
                onCurve[i] = (line[0] == 'T' ? true : false);
        }