Commit 9cc187e98294020ebf9e1d9699d12274745a24ad

Frank Heckenbach 2018-01-29T03:11:13

FTTextureGlyphImpl: fix garbage with bitmap fonts (see Debian bug #589601, 4.)

diff --git a/src/FTGlyph/FTTextureGlyph.cpp b/src/FTGlyph/FTTextureGlyph.cpp
index 28189dc..64b28e7 100644
--- a/src/FTGlyph/FTTextureGlyph.cpp
+++ b/src/FTGlyph/FTTextureGlyph.cpp
@@ -133,9 +133,32 @@ FTTextureGlyphImpl::FTTextureGlyphImpl(FT_GlyphSlot glyph, int id, int xOffset,
         }
         if (destHeight >= 0)
         {
+            // convert bitmap fonts
+            std::vector <unsigned char> data_converted;
+            if(bitmap.num_grays == 1)
+            {
+                bBox = FTBBox(0, 0, 0, destWidth, destHeight, 0);
+                data_converted.resize(destWidth * destHeight, 0);
+                int n = 0;
+                for(int y = 0; y < destHeight; ++y)
+                {
+                    unsigned char* src = bitmap.pitch < 0
+                      ? bitmap.buffer + (y - destHeight + 1) * bitmap.pitch
+                      : bitmap.buffer + y * bitmap.pitch;
+                    unsigned char c;
+                    for(int x = 0; x < destWidth; ++x)
+                    {
+                        if (x % 8 == 0)
+                          c = *src++;
+                        data_converted[n++] = ((c >> (7 - (x % 8))) & 1) * 255;
+                    }
+                }
+            }
+
             glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset,
                             destWidth, destHeight, GL_ALPHA, GL_UNSIGNED_BYTE,
-                            bitmap.buffer);
+                            !data_converted.empty() ? data_converted.data()
+                                                    : bitmap.buffer);
         }
 
         glPopClientAttrib();
diff --git a/test/FTTextureFont-Bitmap.cpp b/test/FTTextureFont-Bitmap.cpp
new file mode 100644
index 0000000..825ba37
--- /dev/null
+++ b/test/FTTextureFont-Bitmap.cpp
@@ -0,0 +1,44 @@
+//$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);
+  glTranslatef (-100, 100, 0);
+  glEnable (GL_BLEND);
+  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+  font->Render ("Test", -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/X11/100dpi/timR12-ISO8859-1.pcf.gz";
+  font = new FTTextureFont (file);
+  if (font->Error () || !font->FaceSize (17))
+    {
+      fprintf (stderr, "Failed to open font %s\n", file);
+      return 1;
+    }
+  glutMainLoop ();
+}