AGG 字型緩衝管理器

來源:互聯網
上載者:User
文章目錄
  • 方式三、使用字型緩衝管理器
方式三、使用字型緩衝管理器

每次都重新讀字模是很費時的,比如前面的例子,"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;
 }
} 顯示效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.