[cairo] Cairo tutorial

Nis Martensen nis.martensen at web.de
Sat Jul 7 08:46:10 PDT 2007


Hi,

I am currently trying to translate Michael Urman's cairo tutorial
<http://www.tortall.net/mu/wiki/CairoTutorial> from python to C. My plan
is to integrate the C version into the cairo wiki. (Michael has given his
permission to do that.)

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:

     1	#include <cairo.h>
     2	
     3	int
     4	main (int argc, char *argv[])
     5	{
     6		cairo_surface_t *surface;
     7		cairo_t *cr;
     8	
     9		surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 120, 120);
    10		cr = cairo_create (surface);
    11		cairo_scale (cr, 120, 120);
    12	
    13		cairo_text_extents_t *te;
    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);
    19		cairo_move_to (cr, 0.5 - te->width / 2 - te->x_bearing,
    20				0.5 - te->height / 2 - te->y_bearing);
    21		cairo_show_text (cr, "a");
    22	
    23		cairo_surface_write_to_png (surface, "example.png");
    24		cairo_destroy (cr);
    25		cairo_surface_destroy (surface);
    26	
    27		return 0;
    28	}

      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?


  2.  I do not understand the following paragraph, the last part of the
      "Understanding Text" section:

      | When working in GTK+, there is also the pangocairo.CairoContext.
      | I think using cairo's builtin font functionality is more
      | suitable for handling text with picky size restrictions and
      | complex positioning, whereas pangocairo.CairoContext may more
      | suitable when you're trying to create widget text that looks
      | like other GTK+ widget text.

      Could somebody please explain?


Thanks,
 Nis


More information about the cairo mailing list