範例代碼:
#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後,再一次性繪製.