AGG 字体缓存管理器

方式三、使用字体缓存管理器

每次都重新读字模是很费时的,比如前面的例子,"C++" 里的两个'+' 就读两次字模,效率可以想象。

一个好的办法是把已读出来的字模缓存起来,下次再遇到这个字时就不用从字体引擎里读取了,AGG提供的font_cache_manager类就是 负责这项工作的。

头文件
  1. #include "agg_font_cache_manager.h"    
类型
  1. template<class FontEngine> class font_cache_manager;

模板参数FontEngine指定管理器所用的字体引擎。另外构造参数也是FontEngine。

成员方法
const glyph_cache* glyph(unsigned glyph_code);获得字模并缓存,glyph_cache类的定义是:
struct glyph_cache
{
 unsigned glyph_index;
 int8u* data;
 unsigned data_size;
 glyph_data_type data_type;
 rect_i bounds;
 double advance_x;
 double advance_y;
};
path_adaptor_type&   path_adaptor();字体引擎的path_adaptor_type实例
gray8_adaptor_type& gray8_adaptor();
gray8_scanline_type& gray8_scanline();
字体引擎的gray8_adaptor_type实例以及对应的Scanline
mono_adaptor_type&   mono_adaptor();
mono_scanline_type& mono_scanline();
字体引擎的mono_adaptor_type实例以及对应的Scanline
void init_embedded_adaptors(const glyph_cache* gl,
                            double x, double y,
                            double scale=1.0);
初始化上面的adaptor成员实例(与字体引擎的ren_type设置相关)
bool add_kerning(double* x, double* y);调整坐标
示例代码1-作为Rasterizer渲染:
显示效果

agg::font_engine_win32_tt_int16 font(dc);
agg::font_cache_manager<
agg::font_engine_win32_tt_int16
> font_manager(font);
font.height(72.0);
font.width(0);
font.italic(true);
font.flip_y(true);
font.hinting(true);

font.transform(agg::trans_affine_rotation(agg::deg2rad(4.0)));
font.create_font("宋体",agg::glyph_ren_agg_gray8);

double x=10, y=72; //起始位置
wchar_t *text = L"C++编程网";
// 画所有字符
for(;*text;text++)
{
 //取字模
 const agg::glyph_cache* glyph = font_manager.glyph(*text);
 if(glyph)
 {
  // 初始化gray8_adaptor实例
  font_manager.init_embedded_adaptors(glyph, x, y);

  agg::render_scanlines_aa_solid(font_manager.gray8_adaptor(),
   font_manager.gray8_scanline(),
   renb, agg::rgba8(0, 0, 0));

  // 前进
  x += glyph->advance_x;
  y += glyph->advance_y;
 }
}

示例代码2-作为顶点源渲染:
typedef agg::font_engine_win32_tt_int16 fe_type;
fe_type font(GetDC(0));
typedef agg::font_cache_manager<fe_type> fcman_type;
fcman_type font_manager(font);
font.height(72.0);
font.width(0);
font.italic(true);
font.flip_y(true);
font.hinting(true);
font.transform(agg::trans_affine_rotation(agg::deg2rad(4.0)));
font.create_font("宋体",agg::glyph_ren_outline);
double x=10, y=72; //起始位置
wchar_t *text = L"C++编程网";
// 画所有字符
for(;*text;text++)
{
 const agg::glyph_cache* glyph = font_manager.glyph(*text);
 if(glyph)
 {
  // 准备*_adaptor
  font_manager.init_embedded_adaptors(glyph, x, y);
  // 先用conv_curve
  typedef agg::conv_curve<
   fcman_type::path_adaptor_type
  > cc_pa_type;
  cc_pa_type ccpath(font_manager.path_adaptor());
  // 画轮廓
  typedef agg::conv_stroke<cc_pa_type> cs_cc_pa_type;
  cs_cc_pa_type csccpath(ccpath);
  agg::rasterizer_scanline_aa<> ras;
  agg::scanline_u8 sl;
  ras.add_path(csccpath);
  agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(0, 0, 0));
  // 前进
  x += glyph->advance_x;
  y += glyph->advance_y;
 }
}
显示效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值