+
+
+//
+// Rotate a point by "angle" around point "center"
+//
+IPoint GlyphPoints::RotatePoint(const double angle, const IPoint point, const IPoint center)
+{
+ // Translate the point to the origin
+ double xt = (double)(point.x - center.x);
+ double yt = (double)(point.y - center.y);
+
+ // Rotate the point by angle
+ double xr = (xt * cos(angle)) - (yt * sin(angle));
+ double yr = (xt * sin(angle)) + (yt * cos(angle));
+
+ // Translate it back...
+ IPoint rotated;
+ rotated.x = (int)(xr + 0.5) + center.x;
+ rotated.y = (int)(yr + 0.5) + center.y;
+ return rotated;
+}
+
+
+//
+// Rotate all points in the glyph by "angle" around point "pt"
+//
+void GlyphPoints::RotatePoints(const double angle, const IPoint pt)
+{
+ for(int i=0; i<numPoints; i++)
+ {
+ // Translate the point to the origin
+ double xt = (double)(x[i] - pt.x);
+ double yt = (double)(y[i] - pt.y);
+
+ // Rotate the point by angle
+ double xr = (xt * cos(angle)) - (yt * sin(angle));
+ double yr = (xt * sin(angle)) + (yt * cos(angle));
+
+ // Put it back...
+ x[i] = (int)(xr + 0.5) + pt.x;
+ y[i] = (int)(yr + 0.5) + pt.y;
+ }
+}
+
+
+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)
+ if (poly >= numPolys)
+ return IPoint(0, 0);
+
+// if (poly >= numPolys)
+//#ifdef DEBUG
+//{
+//WriteLogMsg("Exception: GetPolyEnd(uint16). poly=%u, numPolys=%u\xD\xA", poly, numPolys);
+//#endif
+// throw GP_OUT_OF_RANGE;
+//#ifdef DEBUG
+//}
+//#endif
+
+ IPoint centroid; // Initializes to (0, 0)
+ uint16 numPointsInPoly = GetNumPoints(poly);
+
+ for(uint16 i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
+ {
+ centroid.x += x[i];
+ centroid.y += y[i];
+ }
+
+ centroid.x /= numPointsInPoly;
+ centroid.y /= numPointsInPoly;
+
+ return centroid;
+}
+
+
+void GlyphPoints::RotatePolyAroundCentroid(const int16 poly, const double angle)
+{
+ if (poly >= numPolys)
+ return;
+
+ IPoint centroid = GetPolyCentroid(poly);
+
+ for(uint16 i=GetPolyStart(poly); i<=GetPolyEnd(poly); i++)
+ {
+ IPoint rotated = RotatePoint(angle, IPoint(x[i], y[i]), centroid);
+ x[i] = rotated.x;
+ y[i] = rotated.y;
+ }
+}
+
+
+// really need to do checking on the results from fscanf...
+bool GlyphPoints::LoadGlyphFromFile(FILE * file)
+{
+ char line[512];
+ float version;
+
+ FreeAllocatedMemory();
+
+ fscanf(file, "%s V%f", line, &version);
+ fscanf(file, "%s %u", line, &numPoints);
+ x = new int[numPoints];
+ y = new int[numPoints];
+ onCurve = new bool[numPoints];
+
+ for(int i=0; i<numPoints; i++)
+ {
+ fscanf(file, "%d %d %s", &x[i], &y[i], &line);
+ onCurve[i] = (line[0] == 'T' ? true : false);
+ }
+
+ fscanf(file, "%s %u", line, &numPolys);
+ polyEnd = new uint16[numPolys];
+
+ for(int i=0; i<numPolys; i++)
+ {
+ fscanf(file, "%u", &polyEnd[i]);
+ }
+
+ return true;
+}
+
+
+bool GlyphPoints::SaveGlyphToFile(FILE * file)
+{
+// GlyphPoints glyph = editWnd->pts;
+ fprintf(file, "TTEGLYPH V1.0\n");
+ fprintf(file, "POINTS %u\n", numPoints);
+
+ for(int i=0; i<numPoints; i++)
+ {
+ fprintf(file, "%d %d %s\n", x[i], y[i], (onCurve[i] ? "T" : "F"));
+ }
+
+ fprintf(file, "POLYS %u\n", numPolys);
+
+ for(int i=0; i<numPolys; i++)
+ {
+ fprintf(file, "%u\n", polyEnd[i]);
+ }
+
+ return true;
+}
+