当前位置: 首页 > 工具软件 > Cairo > 使用案例 >

cairo设置

穆劲
2023-12-01
C++绘图工具cairo的基本教程:https://www.cairographics.org/tutorial/

配置Anjuta:
"构建"->"配置项目" 里的 "Configure Options"里的CXXFLAGS里加上以下配置:
-I /usr/include/cairo -L/usr/lib -lcairo
注:第一个-I是大写的i,最后一个-lcairo是小写的L

配置好的CXXFLAGS是这样的:
'CXXFLAGS=-g -O0 -I /usr/include/cairo -L/usr/lib -lcairo'

然后在需要使用cairo的源文件里(比如main.cc)包含头文件cairo.h:
#include <cairo.h>

测试代码:
int main ()
{
       cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
cairo_t *cr = cairo_create (surface);
cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 32.0);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_show_text (cr, "Hello, world");

cairo_destroy (cr);
cairo_surface_write_to_png (surface, "hello.png");
cairo_surface_destroy (surface);
        return 0;
}

运行上述代码后会在项目的文件夹里生产一个hello.png文件


以下是实际使用时的代码示例:

//全局变量

cairo_surface_t *surface;
cairo_t *cr;


//帮助函数:

//InitCairo用于初始化画图设置,例如画笔宽度,字体大小,背景颜色

void InitCairo(double canvasWidth)

{
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, canvasWidth, canvasWidth);
cr = cairo_create (surface);


//line
cairo_set_line_width(cr, 2);


//font
cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 18.0);


//white background
cairo_rectangle (cr, 0,0,canvasWidth, canvasWidth);
cairo_set_source_rgb (cr, 255, 255, 255);
cairo_fill (cr);
}

//CompleteCairo函数用于保存画图,回收资源
void CompleteCairo(const char* fileName)
{
cairo_destroy (cr);
cairo_surface_write_to_png (surface, fileName);
cairo_surface_destroy (surface);
}

//示例

int main()

{

InitCairo (800);
cairo_move_to(cr, 0,0);
cairo_line_to(cr, 50,50);
cairo_set_source_rgb(cr, 0,0,0);
cairo_stroke(cr);
CompleteCairo ("test.png");

}


 类似资料: