AGG學習之十二—-font_cache_manager使用(FreeType在vertex source層應用)

來源:互聯網
上載者:User

範例代碼:

#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_conv_curve.h>#include <agg_conv_contour.h>#include <agg_conv_stroke.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_int32fe_type;typedef agg::font_cache_manager<fe_type>font_manager_type;typedef fe_type::path_adaptor_typevs_type;typedef agg::conv_curve<font_manager_type::path_adaptor_type>curve_type;typedef agg::conv_contour<agg::conv_curve<font_manager_type::path_adaptor_type> >contour_type;typedef agg::conv_stroke<agg::conv_curve<font_manager_type::path_adaptor_type> >stroke_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));  // font cache  fe_type fe;  font_manager_type font_manager(fe);  if( !fe.load_font( "./微軟雅黑.ttf", 0, agg::glyph_ren_outline) )  return;  // vertex source  curve_typecurve(font_manager.path_adaptor());  contour_typecontour(curve);  stroke_typestroke(curve);  // 調節字型的weight  contour.width(0.4*50*0.05);// 取值區間為 [1*height*0.05, -1*height*0.05]  fe.height(30);  fe.width(25);  fe.flip_y(false);  fe.hinting(true);  // 畫所有字元  const agg::glyph_cache *glyph;  wchar_t *text = L"測  試FreeType+Raster\0";  int x=50, y=50;  for( ; *text; ++text )  {  //取字模  glyph = font_manager.glyph( *text );  if(glyph)  {  font_manager.init_embedded_adaptors(glyph, x, y);  ras.add_path( contour );  x += glyph->advance_x;  y += glyph->advance_y;  }  }  agg::render_scanlines_aa_solid( ras, sl, renb, agg::rgba(0, 0, 0) );  // 字串範圍邊框繪製  double minx = ras.min_x();  double miny = ras.min_y();  double maxx = ras.max_x();  double maxy = ras.max_y();  ras.reset();  ras.move_to_d(minx, miny);  ras.line_to_d(minx, maxy);  ras.line_to_d(maxx, maxy);  ras.line_to_d(maxx, miny);  agg::render_scanlines_aa_solid( ras, sl, renb, agg::rgba(0,0,1,0.2) );  // 字串繪製起點:紅十字中心,座標為(50,50)  renb.copy_hline( 40, 50, 60, agg::rgba(1, 0, 0, 0) );  renb.copy_vline( 50, 40, 60, 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. vertex source方式可以調節字型的weight(通過con_stroke邊線width值調節)

     但筆畫調節過細後,有斷斷續續現象,字型不夠清晰;

     如果筆畫調節過粗,則需要調整字間距,避免字元間壓蓋.

2. 此種方法,可將所有字元緩衝為path後,再一次性繪製.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.