5 static void draw_aligned_text(cairo_t *cr, const char *font, double x, double y,
6 double fontsize, const char *text, int alignment)
9 #define LEFT_JUSTIFIED 1
10 #define RIGHT_JUSTIFIED 2
12 double factor, direction;
13 cairo_text_extents_t extents;
30 cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
32 cairo_set_font_size(cr, fontsize);
33 cairo_text_extents(cr, text, &extents);
34 x = x + direction * (factor * extents.width + extents.x_bearing);
35 y = y - (extents.height / 2 + extents.y_bearing);
37 cairo_move_to(cr, x, y);
38 cairo_show_text(cr, text);
41 void draw_centered_text(cairo_t *cr, const char *font, double x, double y,
42 double fontsize, const char *text)
44 draw_aligned_text(cr, font, x, y, fontsize, text, CENTERED);
47 void draw_right_justified_text(cairo_t *cr, const char *font,
49 double fontsize, const char *text)
51 draw_aligned_text(cr, font, x, y, fontsize, text, RIGHT_JUSTIFIED);
54 void draw_left_justified_text(cairo_t *cr, const char *font,
56 double fontsize, const char *text)
58 draw_aligned_text(cr, font, x, y, fontsize, text, LEFT_JUSTIFIED);
61 void draw_vertical_centered_text(cairo_t *cr, const char *font, double x,
62 double y, double fontsize,
66 cairo_text_extents_t extents;
68 cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
70 cairo_set_font_size(cr, fontsize);
71 cairo_text_extents(cr, text, &extents);
74 y = y + (extents.width / 2.0 + extents.x_bearing);
75 x = x - (extents.height / 2.0 + extents.y_bearing);
77 cairo_move_to(cr, x, y);
79 cairo_translate(cr, -sx, -sy);
80 cairo_rotate(cr, -90.0 * M_PI / 180.0);
81 cairo_translate(cr, sx, sy);
82 cairo_show_text(cr, text);