0

我有一个演示代码来绘制 svg 文件或流到 cairo。现在它只适用于格式 CAIRO_FORMAT_ARGB32,但不适用于 CAIRO_FORMAT_A8。任何人都可以帮我解决这个问题或向我展示另一种方式,请参阅下面的代码:

cairo_surface_t  * surface = cairo_image_surface_create(CAIRO_FORMAT_A8, width , height ); //the svg content will show if using CAIRO_FORMAT_ARGB32
cairo_t * cr = cairo_create(surface);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_scale (cr, 1.5, 1.5); 
cairo_save(cr);
    
//clear cairo to white background
cairo_set_source_rgba(cr, 1, 1, 1, 1);
   cairo_paint(cr);  
cairo_restore(cr);
    
//init and draw svg
RsvgHandle *handle = rsvg_handle_new_from_file("/root/learn/cart.svg", 0);
cairo_set_source_rgba(cr, 0, 0, 0, 0);
rsvg_handle_render_cairo(handle, cr);
g_object_unref(handle);
    
//demo draw text
cairo_select_font_face(cr, "Arial", CAIRO_FONT_SLANT_NORMAL,CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 150);
cairo_move_to(cr, 20, 200 - 30);
cairo_show_text(cr, "DEMO");  
    
cairo_status_t status = cairo_surface_write_to_png(surface, "/root/learn/demo.png");
    
    
//release memory
cairo_destroy(cr);
cairo_surface_destroy(surface);
4

0 回答 0