AGG学习之十二----font_cache_manager使用(FreeType在vertex source层应用)

样例代码:

#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_int32		fe_type;
typedef agg::font_cache_manager<fe_type>	font_manager_type;
typedef fe_type::path_adaptor_type			vs_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_type	curve(font_manager.path_adaptor());
		  contour_type	contour(curve);
		  stroke_type	stroke(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后,再一次性绘制.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值