//sudo apt-get install libcairo2-dev //pkg-config --cflags --libs cairo //-I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -lcairo #include <iostream> #include <cairo-svg.h> #define ANGLE(ang) (ang * 3.1415926 / 180.0) int main(int argc, char **argv) { cairo_t *cr; cairo_surface_t *surface; int cheight = 400, cwidth = cheight; surface = (cairo_surface_t *)cairo_svg_surface_create("Cairo_example.svg", cheight, cwidth); cr = cairo_create(surface); cairo_pattern_t *pattern; cairo_text_extents_t text; int x,y; //填充背景黑色 cairo_set_source_rgb (cr, 0, 0, 0); cairo_rectangle(cr, 0, 0, cwidth, cheight); cairo_fill(cr); // cairo_set_source_rgb (cr, 0.5, 0.5, 0.5); // pattern = cairo_pattern_create_radial(50, 50, 5, 50, 50, 50); // cairo_pattern_add_color_stop_rgb(pattern, 0, 0.75, 0.15, 0.99); // cairo_pattern_add_color_stop_rgb(pattern, 0.9, 1, 1, 1); // cairo_set_source(cr, pattern); // cairo_fill(cr); /* Writing in the foreground */ cairo_set_font_size (cr, 15); cairo_select_font_face (cr, "Georgia", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_source_rgb (cr, 0, 0, 1); cairo_move_to(cr, 10, 25); cairo_show_text(cr, "你好"); cairo_move_to(cr, 10, 75); cairo_show_text(cr, "Wikipedia!"); cairo_set_source_rgb ( cr, 0, 1, 0 ); cairo_set_antialias(cr, CAIRO_ANTIALIAS_GOOD); cairo_set_line_width(cr, 2); cairo_move_to(cr, 30, 10); cairo_line_to(cr, 100, 80); cairo_stroke(cr); cairo_move_to(cr, 30, 10); cairo_line_to(cr, 230, 80); cairo_stroke(cr); cairo_rectangle_int_t rect; rect.x = 200; rect.y = 200; rect.width = 180; rect.height = 160; cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height); cairo_stroke(cr); //cairo_fill(cr); cairo_set_source_rgba(cr, 1, 0, 1, 0.5); cairo_set_line_width(cr, 15); int cx = 250, cy = 250, R = 130; cairo_arc(cr, cx, cy, R, ANGLE(0), ANGLE(360)); cairo_stroke(cr); cairo_surface_write_to_png ( surface, "demo1.png" ) ; cairo_destroy (cr); cairo_surface_destroy (surface); return 0; }
Ubuntu下的编译:
g++ `pkg-config --cflags cairo` test_cairo.cpp `pkg-config --libs cairo`
注意:上面的符号`,不是单引號,而是键盘上ESC键的以下, !/1键的左边的按键.
执行结果: