1 #include "cairo_text_helpers.h"
7 static void draw_aligned_text(cairo_t *cr, const char *font, double x, double y,
8 double fontsize, const char *text, int alignment)
11 #define LEFT_JUSTIFIED 1
12 #define RIGHT_JUSTIFIED 2
14 double factor, direction;
15 cairo_text_extents_t extents;
32 cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
34 cairo_set_font_size(cr, fontsize);
35 cairo_text_extents(cr, text, &extents);
36 x = x + direction * (factor * extents.width + extents.x_bearing);
37 y = y - (extents.height / 2 + extents.y_bearing);
39 cairo_move_to(cr, x, y);
40 cairo_show_text(cr, text);
43 void draw_centered_text(cairo_t *cr, const char *font, double x, double y,
44 double fontsize, const char *text)
46 draw_aligned_text(cr, font, x, y, fontsize, text, CENTERED);
49 void draw_right_justified_text(cairo_t *cr, const char *font,
51 double fontsize, const char *text)
53 draw_aligned_text(cr, font, x, y, fontsize, text, RIGHT_JUSTIFIED);
56 void draw_left_justified_text(cairo_t *cr, const char *font,
58 double fontsize, const char *text)
60 draw_aligned_text(cr, font, x, y, fontsize, text, LEFT_JUSTIFIED);
63 void draw_vertical_centered_text(cairo_t *cr, const char *font, double x,
64 double y, double fontsize,
68 cairo_text_extents_t extents;
70 cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
72 cairo_set_font_size(cr, fontsize);
73 cairo_text_extents(cr, text, &extents);
76 y = y + (extents.width / 2.0 + extents.x_bearing);
77 x = x - (extents.height / 2.0 + extents.y_bearing);
79 cairo_move_to(cr, x, y);
81 cairo_translate(cr, -sx, -sy);
82 cairo_rotate(cr, -90.0 * M_PI / 180.0);
83 cairo_translate(cr, sx, sy);
84 cairo_show_text(cr, text);