Commit | Line | Data |
---|---|---|
13229219 DP |
1 | #include "cairo_text_helpers.h" |
2 | ||
ee2f55b2 SC |
3 | #include <cairo.h> |
4 | #include <gtk/gtk.h> | |
5 | #include <math.h> | |
6 | ||
7 | static void draw_aligned_text(cairo_t *cr, const char *font, double x, double y, | |
8 | double fontsize, const char *text, int alignment) | |
9 | { | |
10 | #define CENTERED 0 | |
11 | #define LEFT_JUSTIFIED 1 | |
12 | #define RIGHT_JUSTIFIED 2 | |
13 | ||
14 | double factor, direction; | |
15 | cairo_text_extents_t extents; | |
16 | ||
3c3ed070 | 17 | switch (alignment) { |
ee2f55b2 SC |
18 | case CENTERED: |
19 | direction = -1.0; | |
20 | factor = 0.5; | |
21 | break; | |
22 | case RIGHT_JUSTIFIED: | |
23 | direction = -1.0; | |
24 | factor = 1.0; | |
25 | break; | |
26 | case LEFT_JUSTIFIED: | |
27 | default: | |
28 | direction = 1.0; | |
c3a5b6dc | 29 | factor = 0.0; |
ee2f55b2 SC |
30 | break; |
31 | } | |
32 | cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); | |
33 | ||
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); | |
38 | ||
39 | cairo_move_to(cr, x, y); | |
40 | cairo_show_text(cr, text); | |
41 | } | |
42 | ||
43 | void draw_centered_text(cairo_t *cr, const char *font, double x, double y, | |
44 | double fontsize, const char *text) | |
45 | { | |
46 | draw_aligned_text(cr, font, x, y, fontsize, text, CENTERED); | |
47 | } | |
48 | ||
49 | void draw_right_justified_text(cairo_t *cr, const char *font, | |
50 | double x, double y, | |
51 | double fontsize, const char *text) | |
52 | { | |
53 | draw_aligned_text(cr, font, x, y, fontsize, text, RIGHT_JUSTIFIED); | |
54 | } | |
55 | ||
56 | void draw_left_justified_text(cairo_t *cr, const char *font, | |
57 | double x, double y, | |
58 | double fontsize, const char *text) | |
59 | { | |
60 | draw_aligned_text(cr, font, x, y, fontsize, text, LEFT_JUSTIFIED); | |
61 | } | |
62 | ||
3c3ed070 | 63 | void draw_vertical_centered_text(cairo_t *cr, const char *font, double x, |
ee2f55b2 SC |
64 | double y, double fontsize, |
65 | const char *text) | |
66 | { | |
67 | double sx, sy; | |
68 | cairo_text_extents_t extents; | |
69 | ||
70 | cairo_select_font_face(cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); | |
71 | ||
72 | cairo_set_font_size(cr, fontsize); | |
73 | cairo_text_extents(cr, text, &extents); | |
74 | sx = x; | |
75 | sy = y; | |
76 | y = y + (extents.width / 2.0 + extents.x_bearing); | |
77 | x = x - (extents.height / 2.0 + extents.y_bearing); | |
78 | ||
79 | cairo_move_to(cr, x, y); | |
80 | cairo_save(cr); | |
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); | |
85 | cairo_restore(cr); | |
86 | } | |
87 |