您的位置:首页 > 移动开发

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

2012-01-06 17:02 459 查看

FreeType库的引用:

下载FreeType源码,  载地址为: http://www.freetype.org/index2.html, 我使用的版本为2.4.4.
在VS2008下编译成静态库
使用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,但还未明白影响绘制效果的因果关系
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息