您的位置:首页 > 移动开发

AGG学习之十一----font_cache_manager使用(FreeType在Rasterizer层应用)

2012-01-09 13:46 459 查看

font_cache_manager

1.  定义: template<class FontEngine> class font_cache_manager;

         可以对FreeType与win32两种font Engine进行包装.

2.

typedef path_adaptor_type把字体数据包装成顶点源的类
typedef gray8_adaptor_type把字体数据包装成Scanline Rasterizer的类
typedef mono_adaptor_type把字体数据包装成Scanline Rasterizer的类,但无AA效果
gray8_adaptor_type类型adaptor. 与glyph_ren_agg_gray8类型graph对应
3.

    字体高度,单位为Point

    字体宽度,单位为Point*2.4.  0表示规则大小(height/2.4)

样例代码

#pragma comment(lib, "freetype.lib")

#include <agg_pixfmt_rgba.h>
#include <agg_rasterizer_scanline_aa.h>
#include <agg_scanline_p.h>
#include <agg_font_freetype.h>

#include <agg_rendering_buffer.h>
#include <agg_basics.h>
#include <platform/agg_platform_support.h>
#include "agg_font_cache_manager.h"

#include <vector>

using namespace std;

typedef agg::font_engine_freetype_int32		fe_type;
typedef fe_type::path_adaptor_type			vs_type;

class the_application : public agg::platform_support
{
public:
the_application(agg::pix_format_e format, bool flip_y):
agg::platform_support(format,flip_y)
{}
virtual ~the_application()
{
}
virtual void on_init(){}
virtual void on_draw()
{
agg::rasterizer_scanline_aa<> ras;
agg::scanline_p8 sl;

agg::rendering_buffer rbuf = this->rbuf_window();
agg::pixfmt_rgba32 pixf(rbuf);
agg::renderer_base<agg::pixfmt_rgba32> renb(pixf);

// 屏幕清空为白色
renb.clear(agg::rgba(1, 1, 1, 0));

fe_type fe;
agg::font_cache_manager<fe_type> font_manager(fe);

if( !fe.load_font( "./微软雅黑.ttf", 0, agg::glyph_ren_agg_gray8) ) // 第三个参数描述包装字体数据方式不同
return;

fe.height(50);
fe.width(25.5);
fe.flip_y(false);
fe.hinting(true);

// 画所有字符
vector<agg::rect_i>  bounds;
const agg::glyph_cache *glyph;
wchar_t *text = L"测 试FreeType+Raster\0";
int x=50, y=100;
for( ; *text; ++text )
{
//取字模
glyph = font_manager.glyph( *text );
if(glyph)
{
font_manager.init_embedded_adaptors(glyph, x, y);
agg::render_scanlines_aa_solid( font_manager.gray8_adaptor(), font_manager.gray8_scanline(), renb, agg::rgba(0, 0, 0) );

x += glyph->advance_x;
y += glyph->advance_y;
}
}

// 红色十字定位,中心坐标为 (50,50)
renb.copy_hline( 40, 100, 60, agg::rgba(1, 0, 0, 0) );
renb.copy_vline( 50, 90, 110, agg::rgba(1, 0, 0, 0) );
}
};

int agg_main(int argc, char* argv[])
{
the_application app(agg::pix_format_rgba32, true);
app.caption("My AGG Demo");
if(app.init(320, 200, agg::window_resize ))
{
app.run();
}
return 1;
}

效果图



1. 红色十字中心为绘制起始点。 绘制起始点不是文字的外包的角点.

2. 显示效果不够精细,似乎有"毛刺",反锯齿效果不理想.

3. 可调节字体width,使字体横纵比可不一致.

4. 未找到将所有文字graph都取得后,一次性绘制的方法。只能每一个字符调用一次绘制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息