使用 freetype 和 Opengl ES 2(iPhone 设备)渲染字体效果不佳
我使用 freetype 在 iPhone 设备中编写文本,结果很少见。正如您在图像中看到的那样,相同的字符(如“b”、“n”或“u”)渲染不均匀。
纹理始终相同。知道问题出在哪里或者发生了什么吗?
http://craneossgd.files.wordpress.com/2011/10/image_freetype1.png代码
:
FT_New_Face(getFTLibrary(), _filename, 0, &(_face))
FT_Select_Charmap( _face, FT_ENCODING_UNICODE );
FT_Set_Char_Size( _face, _pt<<6,_pt<<6, _dpi, _dpi);
for (i=0; i<255; i++)
{
if (!createGlyphTexture(i))
{
clean();
}
}
.....
createGlyphTexture(unsigned char ch){
....
FT_Load_Glyph(_face, FT_Get_Char_Index(_face,ch), FT_LOAD_DEFAULT)
FT_Get_Glyph(_face->glyph, &glyph)
....
// *** Transform glyph to bitmap
FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
// *** Get bitmap and glyph data
bitmap_glyph = (FT_BitmapGlyph)glyph;
bitmap = bitmap_glyph->bitmap;
// ***
width = pow2(bitmap.width);
height = pow2(bitmap.rows);
// *** Alloc memory for texture
expanded_data = (GLubyte *)malloc( sizeof(GLubyte)*2*width*height );
for (j=0; j<height;j++)
{
for (i=0; i<width; i++)
{
if ( (i>=(CRuint)bitmap.width) || (j>=(CRuint)bitmap.rows) ){
expanded_data[2*(i+j*width)] = 0;
expanded_data[2*(i+j*width)+1] = 0;
} else {
expanded_data[2*(i+j*width)] = bitmap.buffer[i+bitmap.width*j];
expanded_data[2*(i+j*width)+1] = bitmap.buffer[i+bitmap.width*j];
}
}
}
// *** Load texture into memory
glBindTexture(GL_TEXTURE_2D, _textures[ch]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_LUMINANCE_ALPHA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data);
....
}
谢谢!!
I'm using freetype for writing text in an IPhone device, the result are rare. As you can see in the image same characters(like 'b', 'n' or 'u') aren't rendered equally.
The texture is always the same. Any idea where is the problem or what's going on?
http://craneossgd.files.wordpress.com/2011/10/image_freetype1.png
Code:
FT_New_Face(getFTLibrary(), _filename, 0, &(_face))
FT_Select_Charmap( _face, FT_ENCODING_UNICODE );
FT_Set_Char_Size( _face, _pt<<6,_pt<<6, _dpi, _dpi);
for (i=0; i<255; i++)
{
if (!createGlyphTexture(i))
{
clean();
}
}
.....
createGlyphTexture(unsigned char ch){
....
FT_Load_Glyph(_face, FT_Get_Char_Index(_face,ch), FT_LOAD_DEFAULT)
FT_Get_Glyph(_face->glyph, &glyph)
....
// *** Transform glyph to bitmap
FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
// *** Get bitmap and glyph data
bitmap_glyph = (FT_BitmapGlyph)glyph;
bitmap = bitmap_glyph->bitmap;
// ***
width = pow2(bitmap.width);
height = pow2(bitmap.rows);
// *** Alloc memory for texture
expanded_data = (GLubyte *)malloc( sizeof(GLubyte)*2*width*height );
for (j=0; j<height;j++)
{
for (i=0; i<width; i++)
{
if ( (i>=(CRuint)bitmap.width) || (j>=(CRuint)bitmap.rows) ){
expanded_data[2*(i+j*width)] = 0;
expanded_data[2*(i+j*width)+1] = 0;
} else {
expanded_data[2*(i+j*width)] = bitmap.buffer[i+bitmap.width*j];
expanded_data[2*(i+j*width)+1] = bitmap.buffer[i+bitmap.width*j];
}
}
}
// *** Load texture into memory
glBindTexture(GL_TEXTURE_2D, _textures[ch]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_LUMINANCE_ALPHA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data);
....
}
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 freetype 参考中:
http://www.freetype.org/freetype2/docs/reference/ft2 -base_interface.html
FT_LOAD_TARGET_LIGHT。
用于非单色模式的更轻的提示算法。许多生成的字形更加模糊,但更接近其原始形状。有点像 Mac OS X 上的渲染。作为一个特殊的例外,此目标意味着 FT_LOAD_FORCE_AUTOHINT。
FT_RENDER_MODE_LIGHT。这相当于 FT_RENDER_MODE_NORMAL。它仅定义为单独的值,因为渲染模式也间接用于定义提示算法选择器。有关详细信息,请参阅 FT_LOAD_TARGET_XXX。
它使用下一个配置工作:
In freetype reference:
http://www.freetype.org/freetype2/docs/reference/ft2-base_interface.html
FT_LOAD_TARGET_LIGHT.
A lighter hinting algorithm for non-monochrome modes. Many generated glyphs are more fuzzy but better resemble its original shape. A bit like rendering on Mac OS X. As a special exception, this target implies FT_LOAD_FORCE_AUTOHINT.
FT_RENDER_MODE_LIGHT.This is equivalent to FT_RENDER_MODE_NORMAL. It is only defined as a separate value because render modes are also used indirectly to define hinting algorithm selectors. See FT_LOAD_TARGET_XXX for details.
It works using next configuration: