diff options
| author | Federico Angelilli <code@fedang.net> | 2023-11-15 17:55:37 +0100 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2023-11-15 17:55:37 +0100 |
| commit | 68ef49366f267bc9f93ba779639022a5e171d375 (patch) | |
| tree | cc43c89682461addf6e2341ff8bd57c0029d0a45 /draw.c | |
| parent | b6537b2969450aa6ce2631ecbf3c340dd2e2c23a (diff) | |
Add pango for text rendering and move size calculations to main
Diffstat (limited to 'draw.c')
| -rw-r--r-- | draw.c | 53 |
1 files changed, 38 insertions, 15 deletions
@@ -1,10 +1,25 @@ #include <math.h> -#include <cairo.h> +#include <pango/pangocairo.h> +#include <pango/pango-font.h> +#include <pango/pango-types.h> #include "x11.h" #include "log.h" -#define EVEN(n) ((int)(n) - ((int)(n) % 2 != 0)) +// Make this a state passed to draw +int draw_height, draw_x_padding, draw_y_padding; +PangoFontDescription *desc; + +void draw_init(const char *font, int height, int x_padding, int y_padding) +{ + log_debug("Pango loading font description '%s'", font); + desc = pango_font_description_from_string(font); + log_debug("Pango found matching font '%s'", pango_font_description_get_family(desc)); + + draw_height = height; + draw_x_padding = x_padding; + draw_y_padding = y_padding; +} void draw(Window *win) { @@ -13,8 +28,8 @@ void draw(Window *win) int screen_width, screen_height; window_get_screen_size(win, &screen_width, &screen_height); - int width = EVEN(round(screen_width * 0.9875 * scale)); - int height = EVEN(round(screen_height * 0.0225 * scale)); + int width = round((screen_width - 2 * draw_x_padding) * scale); + int height = round(draw_height * scale); cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); @@ -43,6 +58,9 @@ void draw(Window *win) for (int i = 0; i < 9; i++) { + PangoLayout *layout = pango_cairo_create_layout (cr); + pango_layout_set_font_description(layout, desc); + int x = (height + cairo_get_line_width(cr) * scale * 2) * i; // purple @@ -54,24 +72,29 @@ void draw(Window *win) cairo_arc(cr, x + radius, radius, radius - 1, 0 * degree, 360 * degree); cairo_stroke(cr); - cairo_select_font_face(cr, "Hack", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_source_rgb(cr, 1, 1, 1); - cairo_set_font_size(cr, 20.0); - char btn[] = { '1' + i, '\0' }; + pango_layout_set_text(layout, btn, -1); + + + int text_w, text_h; + pango_layout_get_pixel_size(layout, &text_w, &text_h); + text_w = ceil(text_w / scale); + text_h = ceil(text_h / scale); + + int text_x = x + radius - (text_w / 2); + int text_y = radius - (text_h / 2); + cairo_move_to(cr, text_x, text_y); - cairo_text_extents_t te; - cairo_text_extents(cr, btn, &te); + pango_cairo_update_layout(cr, layout); + pango_cairo_show_layout(cr, layout); - cairo_move_to(cr, x + (height / 2) - te.x_bearing - te.width / 2, - (height / 2) - te.y_bearing - te.height / 2); - cairo_show_text(cr, btn); + g_object_unref(layout); } cairo_destroy(cr); - int x = EVEN((screen_width - width) / 2.0); - int y = EVEN(screen_height * 0.005); + int x = draw_x_padding; + int y = draw_y_padding; // TODO: Move these somewhere else window_move(win, x, y); |
