aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2023-11-15 17:55:37 +0100
committerFederico Angelilli <code@fedang.net>2023-11-15 17:55:37 +0100
commit68ef49366f267bc9f93ba779639022a5e171d375 (patch)
treecc43c89682461addf6e2341ff8bd57c0029d0a45
parentb6537b2969450aa6ce2631ecbf3c340dd2e2c23a (diff)
Add pango for text rendering and move size calculations to main
-rw-r--r--Makefile3
-rw-r--r--comet.c14
-rw-r--r--draw.c53
-rw-r--r--draw.h2
4 files changed, 55 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index f657052..6ea520e 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,6 @@ OBJ = $(SRC:.c=.o)
BIN = comet.bin
DEPS = \
- cairo \
xcb \
xcb-icccm \
xcb-ewmh \
@@ -12,7 +11,7 @@ DEPS = \
xcb-errors \
"xcb-randr >= 1.5" \
"glib-2.0 >= 2.44" \
- gio-2.0
+ pangocairo
CFLAGS := $(shell pkg-config --cflags $(DEPS)) -ggdb
LDFLAGS := $(shell pkg-config --libs $(DEPS)) -lm
diff --git a/comet.c b/comet.c
index 5cc7734..85bdb27 100644
--- a/comet.c
+++ b/comet.c
@@ -1,3 +1,4 @@
+#include <math.h>
#include <glib.h>
#include <glib-unix.h>
@@ -5,6 +6,8 @@
#include "log.h"
#include "draw.h"
+#define EVEN(n) ((int)(n) - ((int)(n) % 2 != 0))
+
static gboolean mainloop_quit(gpointer data)
{
g_main_loop_quit(data);
@@ -25,6 +28,17 @@ int main(int argc, char **argv)
Window *win = window_create();
+ int screen_width, screen_height;
+ window_get_screen_size(win, &screen_width, &screen_height);
+
+ int height = EVEN(round(screen_height * 0.021));
+ int x_padding = EVEN(round(screen_width * 0.005));
+ int y_padding = EVEN(round(screen_height * 0.004));
+
+ log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding);
+
+ draw_init("Hack 12", height, x_padding, y_padding);
+
guint source_term = g_unix_signal_add(SIGTERM, mainloop_quit, mainloop);
guint source_int = g_unix_signal_add(SIGINT, mainloop_quit, mainloop);
diff --git a/draw.c b/draw.c
index 07aecb3..afd727e 100644
--- a/draw.c
+++ b/draw.c
@@ -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);
diff --git a/draw.h b/draw.h
index daccfde..929fecb 100644
--- a/draw.h
+++ b/draw.h
@@ -3,6 +3,8 @@
#include "x11.h"
+void draw_init(const char *font, int height, int x_padding, int y_padding);
+
void draw(Window *win);
#endif