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,但还未明白影响绘制效果的因果关系
相关文章推荐
- AGG学习之九----自带gsv_text对象raster方式绘制字符
- 字符设备驱动学习笔记----查询方式取得按键值
- 学习springmvc的第六天(@PathVariable 注解、Ant字符匹配、指定请求方式)
- 学习数码相框1.2.0.0字符的编码方式_显示点阵文字_freetype_在PC上测试freetype
- OpenGL ES(第一篇绘制方式) 学习
- OpenGL学习脚印: 基本图形绘制方式比较(glBegin,glCallList,glVertexPointer,VBO)
- 中英文数字字符等混合字符串精确测量字符大小并两种方式绘制比较
- 自定义控件学习之绘制刻度盘
- 正则表达式学习(四)匹配单个字符
- [原创]java WEB学习笔记44:Filter 简介,模型,创建,工作原理,相关API,过滤器的部署及映射的方式,Demo
- Android学习之程序创建桌面快捷方式
- 驱动学习之字符设备驱动的原理
- Android组件之Service学习(1)两种使用方式与生命周期
- 7天hadoop学习(一)之虚拟机的网络连接方式及linux的静态ip设置
- 有趣的学习方式
- 优秀程序员思考、学习新技术的原则和方式
- Spring学习(17)--- 三种装配Bean方式比较
- JavaScript学习12 JS中定义对象的几种方式
- Struts2学习(二)执行Action中方法的三种方式
- PetShop3.0学习(一)无法显示中文字符