can't render text in cairo
Asked Answered
P

1

8

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?

Pandanus answered 31/10, 2012 at 16:3 Comment(1)
The answer below does not - but it seems you are drawing with white color, is this intended (this turned out to be the solution to my similar problem, I had to specifically set the source_rgb)?Hubert
P
2

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;
}
Push answered 1/11, 2012 at 8:53 Comment(8)
Hi, Thanks. I am not sure what is going wrong at my end. I tried same code as written by u and I don't see anything. Hopefully, I will be able to sort is soon. Then, I will post here what is wrong.Pandanus
The full sample from above doesn't work for you? Which cairo version are you using? Could you add printf("%d\n", (int) cairo_status(cr)); before the call to cairo_destroy()? That should print 0 if everything is OK, I think.Push
I printed it and I get 35(which is, "cairo status device error"). But if I try to draw a rectangle i see it, then why error for text?Pandanus
I checked, this error means: "an operation to the device caused an unspecified error". This happens after show_text() call. The version of cairo that I am using is 1.10Pandanus
Which cairo backend are you working with? Looking at cairo's source code, CAIRO_STATUS_DEVICE_ERROR is always some kind of backend-specific error. If you got some debugger: Set a breakpoint on _cairo_error() to see where stuff is going wrong.Push
Hi, I am using gcm backend(so, I had used calls to cairo_gcm_device_create() and cairo_gcm_surface_create_for_texture() to create the surface). I ran it in debugger and found that gcm doesn't implement certain fns.Pandanus
the fns. that are not implemented are: _gcm_surface_has_show_text_glyphs not implemented yet _gcm_surface_get_font_options not implemented yet _gcm_surface_has_show_text_glyphs not implemented yet _gcm_surface_show_text_glyphs not implemented yet _gcm_surface_show_glyphs not implemented yet _gcm_surface_old_show_glyphs not implemented yet _gcm_surface_composite,154 not implemented yet. Is there a way to get around this problem?Pandanus
What the heck is gcm? That doesn't seem to be part of cairo (and I guess that means you are out of luck)Push

© 2022 - 2024 — McMap. All rights reserved.