PDA

View Full Version : freetype fonts are twisted.



LinGreg
11-12-2007, 06:52 PM
I'm using freetype2 to get bitmaps of characters, but they look 'twisted' and distorted. I can tell they are the correct characters, but they are just distorted. My code looks something like:

FT_Library library;
FT_Face face;

FT_Init_FreeType( &library );

FT_New_Face( library, "/usr/share/fonts/freefont/FreeMono.ttf", 0, &face );
FT_Set_Char_Size( face, 0, 64*64, 300, 300 );

FT_ULong charcode = static_cast<unsigned int>('A');
int glyph_index = FT_Get_Char_Index( face, charcode );
FT_Load_Glyph( face, glyph_index, FT_LOAD_NO_HINTING );
FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL);

gl stuff ...

glRasterPos3i(0,0, -2);
glDrawPixels(width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);

I trying explictly converting it into a bitmap, tried loading with and without hinting, but still the same problem.

Thanks,
Greg

Korval
11-12-2007, 07:25 PM
gl stuff ...

As fortune should have it, I did some stuff recently with uploading FreeType font data into textures. And I ran into a distortion problem myself with glTexSubImage.

FreeType font data is given as a flat array of unsigned bytes. Which means that the "gl stuff" you're doing needs to tell OpenGL functions that take bitmap data that it is getting a flat array of bytes. And using "GL_LUMINANCE, GL_UNSIGNED_BYTE" isn't enough.

By default, OpenGL assumes that every scanline of pixel data is 4-byte aligned. That is, if width is 14, OpenGL will read the first 14 bytes, skip two bytes, then read the next 14, skip two, etc.

You need to make OpenGL not do that. The page on glReadPixels (http://www.opengl.org/sdk/docs/man/xhtml/glReadPixels.xml) should help you; it has links to the functions that you use to set up pixel transfer modes.

LinGreg
11-12-2007, 10:02 PM
THANKS!

yup, I needed glPixelStorei( GL_UNPACK_ALIGNMENT, 1) (or I guess glPixelStorei( GL_UNPACK_ALIGNMENT, 2) if I add the alpha channel). The default is 8, so I guess opengl was trying to align the pixels as if each pixel had 4 bytes instead of just one (or 2 with alpha), so any 'extra' bytes left at the end of the row was wrapped around to the need row.

Thanks again,
Greg