This project renders fonts using OpenGL ES and the FreeType library. The FreeType library uses font hinting, which makes the fonts look better than your average anti-aliased fonts. This project is primarily designed for the iPhone, however the code is portable and should work on other platforms.
In addition, this code is designed to be efficient. All fonts are bin packed into one texture and draw call batching is implemented.
The standard way to render text in OpenGL is to create a textured quad for each character. You could have a separate texture and quad for each character, but this is very wasteful, because OpenGL textures can only be power of 2 (i.e. 32x32, 64x32,etc) and means there will a lot of unnecessary padding. A better way to do it is to bin pack all the characters into one single texture - known as a texture atlas.
Another advantage to having everything in the same texture is that it allows you to batch more OpenGL draw calls together. This reduces the workload of the GPU and increases performance.
Using the Code
The principle class is FontAtlas. To prepare a font for use, you must first include the TrueType font (i.e. the .ttf file) as a resource in your project. Then, you call FontAtlas::AddFont with the file name of the font, the desired point size and a list of the characters that you need. You call this function for every font you need, then you call FontAtlas::CreateAtlas(). The FontAtlas class retrieves the size of each character from the FreeType library, then it uses its bin packing algorithm to fit them into a OpenGL ES texture.
Initialising fonts:
const char* szLetters = " !\"#&'()*,-./0123456789:;
<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\_abcdefghijklmnopqrstuvwxyzÁáÉéÍíÑñÓóÚú";
m_pFontAtlas = new FontAtlas();
m_pFontAtlas->AddFont("arial.ttf", 12, szLetters);
m_pFontAtlas->AddFont("times.ttf", 18, szLetters);
m_pFontAtlas->AddFont("times.ttf", 16, szLetters);
m_pFontAtlas->CreateAtlas();
You can then render strings as follows:
FTBitmapFont* pArialFont = m_pFontAtlas->GetFTFont(ARIAL_IX);
pArialFont->DrawString(5, 0, "AV Arial font 12 point", BLACK);
Classes
FontAtlasThis is the principle class. It creates the texture atlas and holds all the
FTBitmapFontobjects. Each time you callAddFont, a newFTBitmapFontobject is created.FTBitmapFontFTBitmapFontholds a list of characters and kerning data from theFreeTypelibrary.Strings can be rendered via theDrawStringmethod.GetWidthreturns the width of astringrendered in the given font.FTBitmapCharFor each glyph (i.e. character) in the font, there is a
FTBitmapChar. This class copies the glyph bitmap into the texture and calculates the texture co-ordinates and vertex dimensions of the character.GLCallBatcherThis class is responsible for batching the draw calls. Every time a
FTBitmapCharwants to draw itself, it callsGLCallBatcher::AddQuad. Instead of drawing directly,GLCallBatcherstrings all the quads together in one array and only sends the mesh data to the GPU whenRenderCurris called, or when there a state change is needed. A state change is required, for example, if you change the alpha value, or colour of the text. In this way, the number of draw calls are reduced.TreeNodeThis class is used to implement the bin packing algorithm used to create the texture atlas. This is the algorithm in more detail.
Kerning
You can enable kerning by setting the useKerning flag before the atlas is created:
m_pFontAtlas->SetUseKerning(true);
Personally, I don't think kerning is worth the performance hit, as you hardly notice the difference in quality.
Compiling FreeType on XCode
The FreeType source is included in the project in the directory OpenGLFont\Classes\freetype-2.4.3. Freetype should really be built as a standalone library, but I didn't have time to do that (exercise left to reader
). I followed the instructions, detailed in INSTALL.ANY in the FreeType documentation. Removing all the bits that I didn't need flattened the directory structure, and disabled the modules that weren't used.
Building the Project
You need to download Boost and unzip it somewhere on your drive. Then set the include paths in xcode to point to the Boost include directory. Also, while you are in there, fix the path for FreeType (i.e. OpenGLFont/Classes/freetype-2.4.3/include/).
Quality
When rendered on a PC, the quality of the FreeType font is quite good, but not quite as good as in other applications (OpenOffice for instance). However, on the iPhone itself, it's a lot better - possibly because of the pixel density of the screen.
History
转自http://www.codeproject.com/Articles/125765/FreeType-on-OpenGL-ES-iPhone
本文介绍了一个项目,该项目利用FreeType库和OpenGLES在iPhone上渲染高质量字体,通过字体提示提高了显示效果,并采用纹理拼接和批处理技术优化了性能。文中详细解释了如何准备字体资源、使用FontAtlas类进行字体管理以及实现高效渲染流程。此外,还讨论了如何启用字间距调整并提供在不同设备上的质量对比。


463

被折叠的 条评论
为什么被折叠?



