Commit 35301593ddd7594c0dec4edaae9353b0c5b583c4

Frank Heckenbach 2018-01-29T03:55:37

FTContour: avoid NaN for angles close to 180 degrees (see Debian bug #589601, 5.)

diff --git a/src/FTContour.cpp b/src/FTContour.cpp
index c4a04af..931ca76 100644
--- a/src/FTContour.cpp
+++ b/src/FTContour.cpp
@@ -110,6 +110,13 @@ void FTContour::evaluateCubicCurve(FTPoint A, FTPoint B, FTPoint C, FTPoint D)
 //
 FTPoint FTContour::ComputeOutsetPoint(FTPoint A, FTPoint B, FTPoint C)
 {
+    // If the angle between 'ab' and 'bc' approaches 180 degrees,
+    // the outset point goes to infinity, giving an invalid result.
+    // Even for angles near 180 degrees, the point will be quite
+    // far away from A, B and C. To avoid ugly results, limit
+    // its distance to 64.0 * OutsetMax.
+    static const FTGL_DOUBLE OutsetMax = 5;
+
     /* Build the rotation matrix from 'ba' vector */
     FTPoint ba = (A - B).Normalise();
     FTPoint bc = C - B;
@@ -120,7 +127,11 @@ FTPoint FTContour::ComputeOutsetPoint(FTPoint A, FTPoint B, FTPoint C)
 
     /* Compute the vector bisecting 'abc' */
     FTGL_DOUBLE norm = sqrt(tmp.X() * tmp.X() + tmp.Y() * tmp.Y());
-    FTGL_DOUBLE dist = 64.0 * sqrt((norm - tmp.X()) / (norm + tmp.X()));
+    FTGL_DOUBLE dist;
+    if (norm - tmp.X() > (norm + tmp.X()) * OutsetMax * OutsetMax)
+      dist = 64.0 * OutsetMax;
+    else
+      dist = 64.0 * sqrt((norm - tmp.X()) / (norm + tmp.X()));
     tmp.X(tmp.Y() < 0.0 ? dist : -dist);
     tmp.Y(64.0);
 
diff --git a/test/FTContour-NaN.cpp b/test/FTContour-NaN.cpp
new file mode 100644
index 0000000..8d5cb34
--- /dev/null
+++ b/test/FTContour-NaN.cpp
@@ -0,0 +1,45 @@
+//$LIBS_PKG_CONFIG glu gl ftgl fontconfig
+//$LIBS -lglut
+
+#include <GL/glut.h>
+#include <FTGL/ftgl.h>
+
+FTFont *font;
+
+void display ()
+{
+  glClear (GL_COLOR_BUFFER_BIT);
+  glMatrixMode (GL_PROJECTION);
+  glLoadIdentity ();
+  gluPerspective (90, 1, 1, 1000);
+  glMatrixMode (GL_MODELVIEW);
+  glLoadIdentity ();
+  gluLookAt (0, 0, 200, 0, 0, 0, 0, 1, 0);
+  glPushMatrix ();
+  glColor3f (1, 1, 1);
+  glEnable (GL_BLEND);
+  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+  glTranslatef (-100, 0, 0);
+  glRasterPos2i (0, 0);
+  font->Render ("F", -1, FTPoint (), FTPoint (), FTGL::RENDER_FRONT | FTGL::RENDER_BACK);
+  glPopMatrix ();
+  glutSwapBuffers ();
+}
+
+int main (int argc, char **argv)
+{
+  glutInit (&argc, argv);
+  glutInitDisplayMode (GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
+  glutInitWindowPosition (50, 50);
+  glutInitWindowSize (400, 400);
+  glutCreateWindow ("FTGL Test");
+  glutDisplayFunc (display);
+  const char *file = "/usr/share/fonts/truetype/dustin/El_Abogado_Loco.ttf";
+  font = new FTExtrudeFont (file);
+  if (font->Error () || !font->FaceSize (100))
+    {
+      fprintf (stderr, "Failed to open font %s\n", file);
+      return 1;
+    }
+  glutMainLoop ();
+}