# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: TTFGlyph2D.java
--- TTFGlyph2D.java Base (BASE)
+++ TTFGlyph2D.java Locally Modified (Based On LOCAL)
@@ -372,7 +372,8 @@
         int i = 0;
         boolean endOfContour = true;
         Point startingPoint = null;
-        Point lastCtrlPoint = null;
+        Point lastCtrlPoint = null; //TODO no longer needed
+        Point offCurveStartPoint = null; //TH
         while (i < numberOfPoints)
         {
             Point point = points[i % numberOfPoints];
@@ -389,18 +390,38 @@
                 }
                 // move to the starting point
                 path.moveTo(point.x, point.y);
+LOG.debug ("moveto: " + String.format ("%d,%d", point.x, point.y));
                 endOfContour = false;
                 startingPoint = point;
+                
+                //TH
+                offCurveStartPoint = null;
+                if (!point.onCurve && !nextPoint1.onCurve)
+                {
+                    // off curve start
+                    offCurveStartPoint = point;
+                    startingPoint = new Point (midValue(point.x, nextPoint1.x), midValue(point.y, nextPoint1.y));
+                    path.moveTo(startingPoint.x, startingPoint.y);
+LOG.debug ("off curve start");
+LOG.debug ("moveto: " + String.format ("%d,%d", startingPoint.x, startingPoint.y));
             }
+            }
+            
+            //TH
+            if (point.onCurve)
+                offCurveStartPoint = null;
+            
             // lineTo
             if (point.onCurve && nextPoint1.onCurve)
             {
                 path.lineTo(nextPoint1.x, nextPoint1.y);
+LOG.debug ("lineTo: " + String.format ("%d,%d", nextPoint1.x, nextPoint1.y));
                 i++;
                 if (point.endOfContour || nextPoint1.endOfContour)
                 {
                     endOfContour = true;
                     path.closePath();
+LOG.debug ("closePath");
                 }
                 continue;
             }
@@ -411,39 +432,101 @@
                 {
                     // use the starting point as end point
                     path.quadTo(nextPoint1.x, nextPoint1.y, startingPoint.x, startingPoint.y);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", nextPoint1.x, nextPoint1.y, startingPoint.x, startingPoint.y));
                 }
                 else
                 {
                     path.quadTo(nextPoint1.x, nextPoint1.y, nextPoint2.x, nextPoint2.y);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", nextPoint1.x, nextPoint1.y, nextPoint2.x, nextPoint2.y));
                 }
                 if (nextPoint1.endOfContour || nextPoint2.endOfContour)
                 {
                     endOfContour = true;
                     path.closePath();
+LOG.debug ("closePath");
                 }
                 i += 2;
                 lastCtrlPoint = nextPoint1;
                 continue;
             }
+            
+            //TH segment for curves that start with an off-curve point
+            if (offCurveStartPoint != null && !nextPoint1.onCurve && !nextPoint2.onCurve)
+            {
+                // interpolate endPoint
+                int endPointX = midValue(nextPoint1.x, nextPoint2.x);
+                int endPointY = midValue(nextPoint1.y, nextPoint2.y);
+                path.quadTo(nextPoint1.x, nextPoint1.y, endPointX, endPointY);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", nextPoint1.x, nextPoint1.y, endPointX, endPointY));
+                if (point.endOfContour || nextPoint1.endOfContour || nextPoint2.endOfContour)
+                {
+                    path.quadTo(nextPoint2.x, nextPoint2.y, midValue(nextPoint2.x, offCurveStartPoint.x), midValue(nextPoint2.y, offCurveStartPoint.y));
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", nextPoint2.x, nextPoint2.y, midValue(nextPoint2.x, offCurveStartPoint.x), midValue(nextPoint2.y, offCurveStartPoint.y)));
+                    path.quadTo(offCurveStartPoint.x, offCurveStartPoint.y, startingPoint.x, startingPoint.y);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", offCurveStartPoint.x, offCurveStartPoint.y, startingPoint.x, startingPoint.y));
+                    endOfContour = true;
+                    i += 2;
+                    continue;
+                }
+                ++i;
+                lastCtrlPoint = nextPoint1;
+                continue;
+            }
+
             if (point.onCurve && !nextPoint1.onCurve && !nextPoint2.onCurve)
             {
                 // interpolate endPoint
                 int endPointX = midValue(nextPoint1.x, nextPoint2.x);
                 int endPointY = midValue(nextPoint1.y, nextPoint2.y);
                 path.quadTo(nextPoint1.x, nextPoint1.y, endPointX, endPointY);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", nextPoint1.x, nextPoint1.y, endPointX, endPointY));
                 if (point.endOfContour || nextPoint1.endOfContour || nextPoint2.endOfContour)
                 {
                     path.quadTo(nextPoint2.x, nextPoint2.y, startingPoint.x, startingPoint.y);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", nextPoint2.x, nextPoint2.y, startingPoint.x, startingPoint.y));
                     endOfContour = true;
                     path.closePath();
+LOG.debug ("closePath");
                 }
                 i += 2;
                 lastCtrlPoint = nextPoint1;
                 continue;
             }
+            
+            //TH improved version of block below: the control point is never interpolated
             if (!point.onCurve && !nextPoint1.onCurve)
             {
+                int endPointX = midValue(point.x, nextPoint1.x);
+                int endPointY = midValue(point.y, nextPoint1.y);
+                path.quadTo(point.x, point.y, endPointX, endPointY);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", point.x, point.y, endPointX, endPointY));
+                if (point.endOfContour || nextPoint1.endOfContour)
+                {
+                    endOfContour = true;
+                    path.quadTo(nextPoint1.x, nextPoint1.y, startingPoint.x, startingPoint.y);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", nextPoint1.x, nextPoint1.y, startingPoint.x, startingPoint.y));
+                    lastCtrlPoint = nextPoint1;                    
+                }
+                i++;
+                continue;
+            }
+            
+            //TH this one disabled by block above
+            //TODO delete
+            if (!point.onCurve && !nextPoint1.onCurve)
+            {
                 Point2D lastEndPoint = path.getCurrentPoint();
+                //TH
+                if (lastCtrlPoint == null)
+                {
+                    LOG.warn("lastCtrlPoint is null in font " + name + " for glyph");
+                    LOG.warn("i = " + i);
+                    LOG.warn("Points:");
+                    for (Point p : points)
+                    {
+                        LOG.warn(p);
+                    }
+                }
                 // calculate new control point using the previous control point
                 lastCtrlPoint = new Point(midValue(lastCtrlPoint.x, (int) lastEndPoint.getX()), midValue(
                         lastCtrlPoint.y, (int) lastEndPoint.getY()));
@@ -451,10 +534,12 @@
                 int endPointX = midValue((int) lastEndPoint.getX(), nextPoint1.x);
                 int endPointY = midValue((int) lastEndPoint.getY(), nextPoint1.y);
                 path.quadTo(lastCtrlPoint.x, lastCtrlPoint.y, endPointX, endPointY);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", lastCtrlPoint.x, lastCtrlPoint.y, endPointX, endPointY));
                 if (point.endOfContour || nextPoint1.endOfContour)
                 {
                     endOfContour = true;
                     path.closePath();
+LOG.debug ("closePath");
                 }
                 i++;
                 continue;
@@ -462,10 +547,12 @@
             if (!point.onCurve && nextPoint1.onCurve)
             {
                 path.quadTo(point.x, point.y, nextPoint1.x, nextPoint1.y);
+LOG.debug("quadto: " + String.format ("%d,%d %d,%d", point.x, point.y, nextPoint1.x, nextPoint1.y));
                 if (point.endOfContour || nextPoint1.endOfContour)
                 {
                     endOfContour = true;
                     path.closePath();
+LOG.debug ("closePath");
                 }
                 i++;
                 lastCtrlPoint = point;
@@ -506,7 +593,13 @@
         {
             this(xValue, yValue, false, false);
         }
+
+        @Override
+        public String toString()
+        {
+            return String.format ("Point(%d,%d,%s,%s)", x, y, onCurve?"onCurve":"", endOfContour?"endOfContour":"");
     }
+    }
 
     /**
      * {@inheritDoc}
