Commit 934106a42bbeeabbcddb674b721721cf62c814a4

henry 2002-12-31T04:07:34

Fixed alignment issues for bitmap and texture glyphs

diff --git a/include/FTBitmapGlyph.h b/include/FTBitmapGlyph.h
index dd1290b..ad88b00 100755
--- a/include/FTBitmapGlyph.h
+++ b/include/FTBitmapGlyph.h
@@ -53,7 +53,12 @@ class FTGL_EXPORT FTBitmapGlyph : public FTGlyph
          * The height of the glyph 'image'
          */
         unsigned int destHeight;
-        
+
+        /**
+         * The pitch of the glyph 'image'
+         */
+        unsigned int destPitch;
+
         /**
          * Vector from the pen position to the topleft corner of the bitmap
          */
diff --git a/src/FTBitmapGlyph.cpp b/src/FTBitmapGlyph.cpp
index 8ff3dbf..9f48fb7 100755
--- a/src/FTBitmapGlyph.cpp
+++ b/src/FTBitmapGlyph.cpp
@@ -1,3 +1,5 @@
+#include    <string>
+
 #include "FTBitmapGlyph.h"
 
 FTBitmapGlyph::FTBitmapGlyph( FT_Glyph glyph)
@@ -6,7 +8,6 @@ FTBitmapGlyph::FTBitmapGlyph( FT_Glyph glyph)
     destHeight(0),
     data(0)
 {
-    // This function will always fail if the glyph's format isn't scalable????
     err = FT_Glyph_To_Bitmap( &glyph, ft_render_mode_mono, 0, 1);
     if( err || ft_glyph_format_bitmap != glyph->format)
     {
@@ -16,40 +17,32 @@ FTBitmapGlyph::FTBitmapGlyph( FT_Glyph glyph)
     FT_BitmapGlyph  bitmap = (FT_BitmapGlyph)glyph;
     FT_Bitmap*      source = &bitmap->bitmap;
 
-    //check the pixel mode
-    //ft_pixel_mode_grays
-        
     unsigned int srcWidth = source->width;
     unsigned int srcHeight = source->rows;
     unsigned int srcPitch = source->pitch;
     
-   // FIXME What about dest alignment?
     destWidth = srcWidth;
     destHeight = srcHeight;
-    
+    destPitch = srcPitch;    
+
     if( destWidth && destHeight)
     {
-        data = new unsigned char[srcPitch * destHeight];
-        unsigned char* dest = data + (( destHeight - 1) * srcPitch);
+        data = new unsigned char[destPitch * destHeight];
+        unsigned char* dest = data + (( destHeight - 1) * destPitch);
 
         unsigned char* src = source->buffer;
-        size_t destStep = srcPitch * 2;
-        
+
         for( unsigned int y = 0; y < srcHeight; ++y)
         {
-            for( unsigned int x = 0; x < srcPitch; ++x)
-            {
-                *dest++ = *src++;
-            }
-            
-            dest -= destStep;
+            memcpy( dest, src, srcPitch);
+            dest -= destPitch;
+            src += srcPitch;
         }
     }
     
     pos.x = bitmap->left;
     pos.y = static_cast<int>(srcHeight) - bitmap->top;
-    
-    // Is this the right place to do this?
+
     FT_Done_Glyph( glyph );
 }
 
@@ -64,12 +57,11 @@ float FTBitmapGlyph::Render( const FTPoint& pen)
 {
     if( data)
     {
-        // Move the glyph origin
         glBitmap( 0, 0, 0.0, 0.0, pen.x + pos.x, pen.y - pos.y, (const GLubyte*)0 );
 
+        glPixelStorei( GL_UNPACK_ROW_LENGTH, destPitch * 8);
         glBitmap( destWidth, destHeight, 0.0f, 0.0, 0.0, 0.0, (const GLubyte*)data);
 
-        // Restore the glyph origin
         glBitmap( 0, 0, 0.0, 0.0, -pen.x - pos.x, -pen.y + pos.y, (const GLubyte*)0 );
     }
     
diff --git a/src/FTGLBitmapFont.cpp b/src/FTGLBitmapFont.cpp
index 3f8627a..ddca168 100755
--- a/src/FTGLBitmapFont.cpp
+++ b/src/FTGLBitmapFont.cpp
@@ -37,7 +37,6 @@ void FTGLBitmapFont::Render( const char* string)
     glPushAttrib( GL_ENABLE_BIT);
     
     glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE);
-    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0);
     glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
 
     glDisable( GL_BLEND);
@@ -55,7 +54,6 @@ void FTGLBitmapFont::Render( const wchar_t* string)
     glPushAttrib( GL_ENABLE_BIT);
     
     glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE);
-    glPixelStorei( GL_UNPACK_ROW_LENGTH, 0);
     glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
     
     glDisable( GL_BLEND);
diff --git a/src/FTGLTextureFont.cpp b/src/FTGLTextureFont.cpp
index 73d4b0c..902e2e2 100755
--- a/src/FTGLTextureFont.cpp
+++ b/src/FTGLTextureFont.cpp
@@ -132,7 +132,6 @@ GLuint FTGLTextureFont::CreateTexture()
     GLuint textID;
     glGenTextures( 1, (GLuint*)&textID);
 
-    glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
     glBindTexture( GL_TEXTURE_2D, textID);
     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
diff --git a/src/FTTextureGlyph.cpp b/src/FTTextureGlyph.cpp
index f6a13c7..250249c 100755
--- a/src/FTTextureGlyph.cpp
+++ b/src/FTTextureGlyph.cpp
@@ -22,9 +22,15 @@ FTTextureGlyph::FTTextureGlyph( FT_Glyph glyph, int id, int xOffset, int yOffset
     
     if( destWidth && destHeight)
     {
-        glBindTexture( GL_TEXTURE_2D, glTextureID);
+        glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT);
+        glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE);
         glPixelStorei( GL_UNPACK_ROW_LENGTH, 0);
+        glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
+
+        glBindTexture( GL_TEXTURE_2D, glTextureID);
         glTexSubImage2D( GL_TEXTURE_2D, 0, xOffset, yOffset, destWidth, destHeight, GL_ALPHA, GL_UNSIGNED_BYTE, source->buffer);
+
+        glPopClientAttrib();
     }
 
 
@@ -75,6 +81,5 @@ float FTTextureGlyph::Render( const FTPoint& pen)
     glEnd();
 
     return advance;
-
 }