打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
AGG 字体缓存管理器
AGG 字体缓存管理器
2010-07-11 22:13

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

每次都重新读字模是很费时的,比如前面的例子,"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渲染:

  1. agg::font_engine_win32_tt_int16 font(dc);
  2. agg::font_cache_manager<
  3.      agg::font_engine_win32_tt_int16
  4.      > font_manager(font);
  5. font.height(72.0);
  6. font.width(0);
  7. font.italic(true);
  8. font.flip_y(true);
  9. font.hinting(true);
  10. font.transform(agg::trans_affine_rotation(agg::deg2rad(4.0)));
  11. font.create_font("宋体",agg::glyph_ren_agg_gray8);
  12. double x=10, y=72; //起始位置
  13. wchar_t *text = L"C++编程网";
  14. // 画所有字符
  15. for(;*text;text++)
  16. {
  17.     //取字模
  18.     const agg::glyph_cache* glyph = font_manager.glyph(*text);
  19.     if(glyph)
  20.      {
  21.         // 初始化gray8_adaptor实例
  22.          font_manager.init_embedded_adaptors(glyph, x, y);
  23.          agg::render_scanlines_aa_solid(font_manager.gray8_adaptor(),
  24.                                font_manager.gray8_scanline(),
  25.                                renb, agg::rgba8(0, 0, 0));
  26.        
  27.         // 前进
  28.          x += glyph->advance_x;
  29.          y += glyph->advance_y;
  30.      }
  31. }

显示效果


示例代码2-作为顶点源渲染:

  1. typedef agg::font_engine_win32_tt_int16 fe_type;
  2. fe_type font(GetDC(0));
  3. typedef agg::font_cache_manager<fe_type> fcman_type;
  4. fcman_type font_manager(font);
  5. font.height(72.0);
  6. font.width(0);
  7. font.italic(true);
  8. font.flip_y(true);
  9. font.hinting(true);
  10. font.transform(agg::trans_affine_rotation(agg::deg2rad(4.0)));
  11. font.create_font("宋体",agg::glyph_ren_outline);
  12. double x=10, y=72; //起始位置
  13. wchar_t *text = L"C++编程网";
  14. // 画所有字符
  15. for(;*text;text++)
  16. {
  17.     const agg::glyph_cache* glyph = font_manager.glyph(*text);
  18.     if(glyph)
  19.      {
  20.         // 准备*_adaptor
  21.          font_manager.init_embedded_adaptors(glyph, x, y);
  22.        
  23.         // 先用conv_curve
  24.         typedef agg::conv_curve<
  25.              fcman_type::path_adaptor_type
  26.          > cc_pa_type;
  27.          cc_pa_type ccpath(font_manager.path_adaptor());
  28.         // 画轮廓
  29.         typedef agg::conv_stroke<cc_pa_type> cs_cc_pa_type;
  30.          cs_cc_pa_type csccpath(ccpath);
  31.          agg::rasterizer_scanline_aa<> ras;
  32.          agg::scanline_u8 sl;
  33.          ras.add_path(csccpath);
  34.          agg::render_scanlines_aa_solid(ras, sl, renb, agg::rgba8(0, 0, 0));
  35.         // 前进
  36.          x += glyph->advance_x;
  37.          y += glyph->advance_y;
  38.      }
  39. }

显示效果


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
NeHe OpenGL第四十三课:FreeType库
请查收,一份让你年薪突破20W的Python爬虫笔记!
woff字体图元结构剖析,自定义字体的制作与匹配和识别
卡通字体 fonts下载_精制字体下载_字体下载大宝库(第1页)_Font.knowsky...
33个最新的超酷字体设计免费下载
网页入门必看!!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服