[cairo] Cairo tutorial

Kalle Vahlman kalle.vahlman at gmail.com
Sat Jul 7 08:59:53 PDT 2007


2007/7/7, Nis Martensen <nis.martensen at web.de>:
> There are two problems:
>
>   1.  I cannot get any text-related code to work. My test program
>       (based on the C example in the FAQ) is this:
[snip]
>     13          cairo_text_extents_t *te;

1. You don't declare variables in the middle of functions in C, that's
a C++ thing.
2. This should not be a pointer, see below

>     14          cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
>     15          cairo_select_font_face (cr, "LMSans10",
>     16                          CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
>     17          cairo_set_font_size (cr, 1.2);
>     18          cairo_text_extents (cr, "a", te);

Here you pass the pointer "te" to cairo_text_extents(), but that
pointer actually points to some semi-random portion of memory (which
cairo_text_extents() tries to write to and causes a segfault). What
you should be doing instead is pass the address of a local variable.
So, like this:

13          cairo_text_extents_t te;

and

18          cairo_text_extents (cr, "a", &te);

And of course access the members with the '.' notation later.

>       Now I always get a segfault when it comes to cairo_text_extents()
>       at line 18. What am I doing wrong? And is there any way I can find
>       out which font family I can use on my system?

If you mean outside of your program, the fc-list utility lists all the
fonts fontconfig (and thus Cairo too) knows of.

-- 
Kalle Vahlman, zuh at iki.fi
Powered by http://movial.fi
Interesting stuff at http://syslog.movial.fi


More information about the cairo mailing list