freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Freetype] Distorted characters


From: John Churchill
Subject: [Freetype] Distorted characters
Date: Fri, 23 Aug 2002 02:24:20 -0700 (PDT)

Hi,

I'm having some problems rendering characters with freetype in opengl.  I 
intended to simply render a character as a bitmap and cache it in an 
opengl display list.  The problem is that I'm getting distortion in my 
bitmaps.  The really weird part is as I change the character size some 
characters will become less distorted.  At some sizes the only problem is 
a stray pixel or two and at others the character is almost unidentifiable.  
So have I missed some steps in freetype?  Anyone have any ideas what I'm 
doing wrong?  Any help at all would be appreciated.

I'm also having to flip the rendered glyph.  I assume this is because I'm 
using openGL and the coordinate system is reversed and not because I 
screwed something up with freetype.

I put together the following code after going through the tutorial:  

void InitFont() {
        FT_UInt index;
        FT_GlyphSlot slot;
        int pitch, start, end;
        unsigned char *bitmap, i;

        /* Freetype init. */
        if ( FT_Init_FreeType(&Library) ) {
                printf("Failed to init freetype.\n");
                exit(1);
        }

        if ( FT_New_Face(Library, "fonts/arial.ttf", 0, &face) )
                printf("Unable to load font.\n");

        /* A 47 point is very clear for me. */
        if ( FT_Set_Char_Size(face, 0, 22*64, 0, 0) )
                printf("Unable to set character size.\n");

        fontOffset = glGenLists(128);
        
        /* Load and render characters into display list. */
        for (i = 'A'; i < 'A' + 4; i++) {
                index = FT_Get_Char_Index(face, i);

                if ( FT_Load_Glyph(face, index, FT_LOAD_DEFAULT) )
                        printf("Unable to load glyph face.\n");
                
                if ( FT_Render_Glyph(face->glyph, ft_render_mode_mono) )
                        printf("Unable to render glyph\n");

                /* Typing savers. */
                slot   = face->glyph;
                pitch  = slot->bitmap.pitch;
                
                /* Space for flipped glyph. */
                bitmap = (unsigned char *) malloc(slot->bitmap.rows * pitch);
                
                if (bitmap == NULL) {
                        printf("Out of memory.\n");
                        free(bitmap);
                        FT_Done_FreeType(Library);
                        exit(1);
                }
                /* Zero the bitmap. */
                memset(bitmap, 0, slot->bitmap.rows * pitch);
                
                /* Flip the rendered glyph. */
                for (start=0, end=slot->bitmap.rows; end>0; end--, start++) {
                        memcpy(bitmap + pitch * start,
                               slot->bitmap.buffer + end * pitch, pitch);
                }

                /* Put character into display list. */
                glNewList(fontOffset + (int) i, GL_COMPILE);
                glBitmap(slot->bitmap.width, slot->bitmap.rows, 0.0, 0.0,
                         (float) (slot->bitmap.width + 2), 0.0, bitmap);
                glEndList();

                free(bitmap);

        }

        FT_Done_FreeType(Library);
}


void printGL(char *string) {

        glPushAttrib(GL_LIST_BIT);
        glListBase(fontOffset);
        glCallLists(strlen(string), GL_UNSIGNED_BYTE, (GLubyte *) string);
        glPopAttrib();

}


void DrawScene() {
        static GLubyte white[] = {255, 255, 255};

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        glColor3ubv(white);
        glRasterPos2i(100, 300);
        printGL("ABCD");

        SDL_GL_SwapBuffers();

}


Thanks,
-John




reply via email to

[Prev in Thread] Current Thread [Next in Thread]