Commit 2dee4458bc6e5cb49dd5ec58ca88bac5c5f2dde3

Frank Heckenbach 2018-01-29T03:49:54

FTBufferGlyph: fix garbage with bitmap fonts (text is still clipped, that seems to be another problem)

diff --git a/src/FTGlyph/FTBufferGlyph.cpp b/src/FTGlyph/FTBufferGlyph.cpp
index 41c0e56..d85146f 100644
--- a/src/FTGlyph/FTBufferGlyph.cpp
+++ b/src/FTGlyph/FTBufferGlyph.cpp
@@ -104,15 +104,32 @@ const FTPoint& FTBufferGlyphImpl::RenderImpl(const FTPoint& pen, int renderMode)
             // FIXME: change the loop bounds instead of doing this test
             if(y + dy < 0 || y + dy >= buffer->Height()) continue;
 
-            for(int x = 0; x < bitmap.width; x++)
+            if(bitmap.num_grays == 1)
             {
-                if(x + dx < 0 || x + dx >= buffer->Width()) continue;
+                for(int x = 0; x < bitmap.width; x++)
+                {
+                    if(x + dx < 0 || x + dx >= buffer->Width()) continue;
 
-                unsigned char p = pixels[y * bitmap.pitch + x];
+                    unsigned char p = pixels[y * bitmap.pitch + x / 8];
 
-                if(p)
+                    if((p << (x % 8)) & 0x80)
+                    {
+                        dest[y * buffer->Width() + x] = 255;
+                    }
+                }
+            }
+            else
+            {
+                for(int x = 0; x < bitmap.width; x++)
                 {
-                    dest[y * buffer->Width() + x] = p;
+                    if(x + dx < 0 || x + dx >= buffer->Width()) continue;
+
+                    unsigned char p = pixels[y * bitmap.pitch + x];
+
+                    if(p)
+                    {
+                        dest[y * buffer->Width() + x] = p;
+                    }
                 }
             }
         }
diff --git a/test/FTBufferFont-Bitmap.cpp b/test/FTBufferFont-Bitmap.cpp
new file mode 100644
index 0000000..5e8d759
--- /dev/null
+++ b/test/FTBufferFont-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);
+  glEnable (GL_BLEND);
+  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+  glTranslatef (-100, 100, 0);
+  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 FTBufferFont (file);
+  if (font->Error () || !font->FaceSize (17))
+    {
+      fprintf (stderr, "Failed to open font %s\n", file);
+      return 1;
+    }
+  glutMainLoop ();
+}