AGG学习之十----FreeType库 raster方式绘制字符

本文介绍了如何在Windows环境下利用FreeType库2.4.4版本编译静态库,并展示了如何引用库进行字符绘制。重点提及了绘制起点、空格字符处理、转义字符、字符宽高设定以及分辨率设置等关键点,同时指出FreeType库不支持类似Windows GDI的粗体设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FreeType库的引用:

  1. 下载FreeType源码,  载地址为: http://www.freetype.org/index2.html, 我使用的版本为2.4.4.
  2. 在VS2008下编译成静态库
  3. 使用FreeType库
  • 设置FreeType头文件目录   //freetype//includes//
  • 引用库   #pragma comment(lib, "freetype.lib")  



样例代码:

#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_rendering_buffer.h>
#include <agg_basics.h>
#include <platform/agg_platform_support.h>

#include <vector>

using namespace std;


typedef agg::font_engine_freetype_int32		fe_type;
typedef fe_type::path_adaptor_type			vs_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));

		
		  // 装载字模,并设置字体参数
		  fe_type fe;
		  if( !fe.load_font( "./微软雅黑.ttf", 0, agg::glyph_ren_outline) ) 
			  return;

		  fe.height(25);
		  fe.width(23);
		  fe.flip_y(false);
		  fe.hinting(true); 
		  fe.resolution(0);


		  // 字符串绘制
		  wchar_t *s = L"测 试AGG\\+FreeType\0";
		  vector<agg::int8u>	data;
		  vs_type				vs;
		  agg::conv_curve<vs_type>	ccvs(vs);
		  int x=50, y=100;
		  for( ; *s; ++s )
		  {
			  if (*s==' ')
			  {
				  x += fe.width();
				  continue;
			  }

			  if(!fe.prepare_glyph(*s)) 
				  continue;


			  data.resize( fe.data_size() );
			  fe.write_glyph_to( &data[0] );
			  vs.init(&data[0], data.size(), x, y);
			  x += fe.advance_x();
			  y += fe.advance_y();

			  ras.add_path(ccvs);
		  }
		  agg::render_scanlines_aa_solid( ras, sl, renb, agg::rgba(0,1,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.08) );


		  // 红色十字定位,中心坐标为 (50,50)
		  renb.copy_hline( 40, 100, 60, agg::rgba(1, 0, 0, 0) );
		  renb.copy_vline( 50, 90, 110, 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.  红色十字中心为绘制文字传入起点参数.

2. 绘制中,需要对空格字符特殊处理,否则程序crash

3. "\"字符会被作为转义符处理,但无法实现换行

4. 传入的绘制起点不是绘制字符串外包的角点.

5. FreeType中无类似Windows GDI中weight参数,无法设置粗体

6. 绘制的字符宽高可任意设定

7. 可以设置resolution,但还未明白影响绘制效果的因果关系


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值