Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't render text in cairo

Tags:

cairo

I am new to cairo and I have read the tutorials/documentation on its website. Now I can make lines, rectangles, and basically I can render images but not text.

I am using the following code

cairo_select_font_face (cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAl);

    cairo_set_font_size (cr, 14);

    cairo_set_source_rgb (cr, 1, 1, 1);

    cairo_move_to (cr, 50, 50);

    cairo_show_text (cr, "Print Something");

Can anyone please point to my mistake?

like image 858
Techie Help Avatar asked Oct 31 '12 16:10

Techie Help


1 Answers

Same answer as on the cairo mailing list (where it seems to have been lost somewhere):

You aren't doing anything wrong (well, perhaps using the toy text API, but that should still work) and your code works fine for me. Here is the full code that I tested with:

#include <cairo.h>
int main()
{
        cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200);
        cairo_t *cr = cairo_create(surface);
        cairo_surface_destroy(surface);

        /* Fill everything with white */
        cairo_set_source_rgb(cr, 1, 1, 1);
        cairo_paint(cr);

        /* Draw some text */
        cairo_select_font_face (cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
        cairo_set_font_size (cr, 14);
        cairo_set_source_rgb (cr, 0, 0, 0);
        cairo_move_to (cr, 0, 50);
        cairo_show_text (cr, "Print Something");

        cairo_surface_write_to_png(cairo_get_target(cr), "out.png");
        cairo_destroy(cr);
        return 0;
}
like image 55
Uli Schlachter Avatar answered Nov 07 '22 08:11

Uli Schlachter