Commit 15d3cad5cc732fd71f6850e92ba615e9f35a0781

sammy 2008-04-22T17:06:49

* Fix FTTextureGlyph rendering offset so that it works with layouts.

diff --git a/demo/FTGLDemo.cpp b/demo/FTGLDemo.cpp
index 4373ab0..9f0bce3 100644
--- a/demo/FTGLDemo.cpp
+++ b/demo/FTGLDemo.cpp
@@ -563,7 +563,7 @@ void parseSpecialKey(int key, int x, int y)
         current_font = (current_font + 1) % 6;
         break;
     case GLUT_KEY_DOWN:
-        current_font = (current_font + 4) % 6;
+        current_font = (current_font + 5) % 6;
         break;
     case GLUT_KEY_PAGE_UP:
         currentLayout = (currentLayout + 1) % NumLayouts;
diff --git a/include/FTTextureGlyph.h b/include/FTTextureGlyph.h
index b6ee78e..a898756 100644
--- a/include/FTTextureGlyph.h
+++ b/include/FTTextureGlyph.h
@@ -10,10 +10,10 @@
  * distribute, sublicense, and/or sell copies of the Software, and to
  * permit persons to whom the Software is furnished to do so, subject to
  * the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be
  * included in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -47,7 +47,7 @@
 /**
  * FTTextureGlyph is a specialisation of FTGlyph for creating texture
  * glyphs.
- * 
+ *
  * @see FTGlyphContainer
  *
  */
@@ -67,7 +67,7 @@ class FTGL_EXPORT FTTextureGlyph : public FTGlyph
          * @param width     The width of the parent texture
          * @param height    The height (number of rows) of the parent texture
          */
-        FTTextureGlyph( FT_GlyphSlot glyph, int id, int xOffset, int yOffset, GLsizei width, GLsizei height);
+        FTTextureGlyph(FT_GlyphSlot glyph, int id, int xOffset, int yOffset, GLsizei width, GLsizei height);
 
         /**
          * Destructor
@@ -81,13 +81,14 @@ class FTGL_EXPORT FTTextureGlyph : public FTGlyph
          * @param renderMode  Render mode to display
          * @return      The advance distance for this glyph.
          */
-        virtual const FTPoint& Render( const FTPoint& pen, int renderMode);
+        virtual const FTPoint& Render(const FTPoint& pen, int renderMode);
 
         /**
-         * Reset the currently active texture to zero to get into a known state before
-         * drawing a string. This is to get round possible threading issues.
+         * Reset the currently active texture to zero to get into a known
+         * state before drawing a string. This is to get round possible
+         * threading issues.
          */
-        static void ResetActiveTexture(){ activeTextureID = 0;}
+        static void ResetActiveTexture() { activeTextureID = 0; }
 
     private:
         /**
@@ -118,12 +119,11 @@ class FTGL_EXPORT FTTextureGlyph : public FTGlyph
         /**
          * The texture index of the currently active texture
          *
-         * We keep track of the currently active texture to try to reduce the number
-         * of texture bind operations.
+         * We keep track of the currently active texture to try to reduce the
+         * number of texture bind operations.
          */
         static GLint activeTextureID;
-
 };
 
-
 #endif  //  __FTTextureGlyph__
+
diff --git a/src/FTTextureGlyph.cpp b/src/FTTextureGlyph.cpp
index 504874b..b478f5b 100644
--- a/src/FTTextureGlyph.cpp
+++ b/src/FTTextureGlyph.cpp
@@ -10,10 +10,10 @@
  * distribute, sublicense, and/or sell copies of the Software, and to
  * permit persons to whom the Software is furnished to do so, subject to
  * the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be
  * included in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -37,15 +37,15 @@
 #include "FTTextureGlyph.h"
 
 GLint FTTextureGlyph::activeTextureID = 0;
- 
-FTTextureGlyph::FTTextureGlyph( FT_GlyphSlot glyph, int id, int xOffset, int yOffset, GLsizei width, GLsizei height)
-:   FTGlyph( glyph),
+
+FTTextureGlyph::FTTextureGlyph(FT_GlyphSlot glyph, int id, int xOffset, int yOffset, GLsizei width, GLsizei height)
+:   FTGlyph(glyph),
     destWidth(0),
     destHeight(0),
     glTextureID(id)
 {
-    err = FT_Render_Glyph( glyph, FT_RENDER_MODE_NORMAL);
-    if( err || glyph->format != ft_glyph_format_bitmap)
+    err = FT_Render_Glyph(glyph, FT_RENDER_MODE_NORMAL);
+    if(err || glyph->format != ft_glyph_format_bitmap)
     {
         return;
     }
@@ -54,36 +54,36 @@ FTTextureGlyph::FTTextureGlyph( FT_GlyphSlot glyph, int id, int xOffset, int yOf
 
     destWidth  = bitmap.width;
     destHeight = bitmap.rows;
-    
-    if( destWidth && destHeight)
+
+    if(destWidth && destHeight)
     {
-        glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT);
-        glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE);
-        glPixelStorei( GL_UNPACK_ROW_LENGTH, 0);
-        glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
+        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, bitmap.buffer);
+        glBindTexture(GL_TEXTURE_2D, glTextureID);
+        glTexSubImage2D(GL_TEXTURE_2D, 0, xOffset, yOffset, destWidth, destHeight, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.buffer);
 
         glPopClientAttrib();
     }
 
 
-//      0    
+//      0
 //      +----+
 //      |    |
 //      |    |
 //      |    |
 //      +----+
 //           1
-    
-    uv[0].X( static_cast<float>(xOffset) / static_cast<float>(width));
-    uv[0].Y( static_cast<float>(yOffset) / static_cast<float>(height));
-    uv[1].X( static_cast<float>( xOffset + destWidth) / static_cast<float>(width));
-    uv[1].Y( static_cast<float>( yOffset + destHeight) / static_cast<float>(height));
-    
-    pos.X( glyph->bitmap_left);
-    pos.Y( glyph->bitmap_top);
+
+    uv[0].X(static_cast<float>(xOffset) / static_cast<float>(width));
+    uv[0].Y(static_cast<float>(yOffset) / static_cast<float>(height));
+    uv[1].X(static_cast<float>(xOffset + destWidth) / static_cast<float>(width));
+    uv[1].Y(static_cast<float>(yOffset + destHeight) / static_cast<float>(height));
+
+    pos.X(glyph->bitmap_left);
+    pos.Y(glyph->bitmap_top);
 }
 
 
@@ -91,30 +91,32 @@ FTTextureGlyph::~FTTextureGlyph()
 {}
 
 
-const FTPoint& FTTextureGlyph::Render( const FTPoint& pen, int renderMode)
+const FTPoint& FTTextureGlyph::Render(const FTPoint& pen, int renderMode)
 {
-    if( activeTextureID != glTextureID)
+    if(activeTextureID != glTextureID)
     {
-        glBindTexture( GL_TEXTURE_2D, (GLuint)glTextureID);
+        glBindTexture(GL_TEXTURE_2D, (GLuint)glTextureID);
         activeTextureID = glTextureID;
     }
-    
-    glTranslatef( pen.X(),  pen.Y(), 0.0f);
 
-    glBegin( GL_QUADS);
-        glTexCoord2f( uv[0].X(), uv[0].Y());
-        glVertex2f( pos.X(), pos.Y());
+    glTranslatef(pen.X(),  pen.Y(), 0.0f);
+
+    glBegin(GL_QUADS);
+        glTexCoord2f(uv[0].X(), uv[0].Y());
+        glVertex2f(pos.X(), pos.Y());
 
-        glTexCoord2f( uv[0].X(), uv[1].Y());
-        glVertex2f( pos.X(), pos.Y() - destHeight);
+        glTexCoord2f(uv[0].X(), uv[1].Y());
+        glVertex2f(pos.X(), pos.Y() - destHeight);
 
-        glTexCoord2f( uv[1].X(), uv[1].Y());
-        glVertex2f( destWidth + pos.X(), pos.Y() - destHeight);
-        
-        glTexCoord2f( uv[1].X(), uv[0].Y());
-        glVertex2f( destWidth + pos.X(), pos.Y());
+        glTexCoord2f(uv[1].X(), uv[1].Y());
+        glVertex2f(destWidth + pos.X(), pos.Y() - destHeight);
+
+        glTexCoord2f(uv[1].X(), uv[0].Y());
+        glVertex2f(destWidth + pos.X(), pos.Y());
     glEnd();
 
+    glTranslatef(-pen.X(), -pen.Y(), 0.0f);
+
     return advance;
 }