#include #include #include int main(void) { int i, width = 300, height = 200; cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); cairo_t *context = cairo_create(surface); cairo_font_extents_t font_extents; cairo_text_extents_t text_extents; cairo_rectangle_int_t rect_whole = {0, 0, width, height}; cairo_rectangle_int_t rect_inside = {10, 50, width - 20, height - 100}; const char *text = "Ala ma kota i dużego psa"; cairo_region_t *region; cairo_set_source_rgb(context, 1.0, 1.0, 1.0); cairo_paint(context); cairo_set_source_rgba(context, 1.0, 0.0, 0.0, 0.5); cairo_rectangle(context, rect_inside.x, rect_inside.y, rect_inside.width, rect_inside.height); cairo_fill(context); cairo_select_font_face(context, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); cairo_set_font_size(context, 18); cairo_font_extents(context, &font_extents); cairo_text_extents(context, text, &text_extents); region = cairo_region_create_rectangle(&rect_whole); cairo_region_subtract_rectangle(region, &rect_inside); for (i = 0; i < cairo_region_num_rectangles(region); i++) { cairo_rectangle_int_t rectangle; cairo_region_get_rectangle(region, i, &rectangle); cairo_rectangle(context, rectangle.x, rectangle.y, rectangle.width, rectangle.height); } cairo_clip(context); for (i = 0; i < (height - font_extents.height) / font_extents.height; i++) { cairo_move_to(context, (width - text_extents.width) / 2 + text_extents.x_bearing, font_extents.ascent + i * font_extents.height); cairo_set_source_rgb(context, 0.0, 0.0, 0.0); cairo_show_text(context, text); } cairo_surface_write_to_png(surface, "image.png"); cairo_region_destroy(region); cairo_destroy(context); cairo_surface_destroy(surface); return EXIT_SUCCESS; }