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

AGG学习之九----自带gsv_text对象raster方式绘制字符

2012-01-06 10:14 239 查看
使用AGG绘制字符方式有很多种:

使用AGG自带的 gsv_text 对象;
使用 WinAPI 字体引擎;
使用 FreeType 字体引擎;
使用字体缓存管理器。
首先,我们使用自带的gsv_text对象,在rastser方式下绘制字符。样例代码如下:

#include <agg_pixfmt_rgba.h>
#include <agg_rasterizer_scanline_aa.h>
#include <agg_scanline_p.h>
#include <agg_gsv_text.h>

#include <agg_rendering_buffer.h>
#include <agg_basics.h>
#include <platform/agg_platform_support.h>

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));

// gsv_text类
agg::gsv_text txt;
agg::conv_stroke<agg::gsv_text> cstxt(txt);

txt.flip(false); // 设置是否反转
txt.size(30); // 高、宽
txt.line_space(10); // 行间距
txt.space(5); // 字符间距

// 设置位置和文字
txt.start_point(100,100);
txt.text("program\nC++");

cstxt.width(3); // 线宽
cstxt.line_cap(agg::butt_cap); // 线端样式

// 输出红色绿色文字
ras.add_path(cstxt);
agg::render_scanlines_aa_solid(ras,sl,renb,agg::rgba(0,1,0));

// 红色十字定位,中心坐标为(100,100)
renb.copy_hline( 90, 100, 110, agg::rgba(1, 0, 0, 0) );
renb.copy_vline( 100, 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;
}


显示效果:



注意:

红色十字中心点坐标为(100,100),是绘制字符传入的起始坐标。
绘制效果图显示,传入的绘制起始坐标点并非是字符实际绘制的外框左上角点或左下角点。
所以,如果需要在指定位置绘制字符,则需要预先精确计算绘制实际位置、传入绘制坐标之间的差值。
gsv_text对象,自动处理了换行。
gsv_text对象,虽然可以设定行间距,带行间距并未减除字符边线的宽度,需要开发者注意计算。
gsv_text仅能处理西文字符,无法处理中文。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息