From 82d464bb90af7fed57a36aacc2dce22994849bac Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 29 Nov 2015 15:27:28 -0500 Subject: [PATCH] Add text rendering support to wayland clients --- include/client/pango.h | 12 +++++++++ include/client/window.h | 1 + swaybar/main.c | 13 +++++++-- wayland/pango.c | 59 +++++++++++++++++++++++++++++++++++++++++ wayland/window.c | 4 +++ 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 include/client/pango.h create mode 100644 wayland/pango.c diff --git a/include/client/pango.h b/include/client/pango.h new file mode 100644 index 00000000..e25a2211 --- /dev/null +++ b/include/client/pango.h @@ -0,0 +1,12 @@ +#ifndef _SWAY_CLIENT_PANGO_H +#define _SWAY_CLIENT_PANGO_H + +#include "client/window.h" +#include "client/buffer.h" +#include + +PangoLayout *get_pango_layout(struct window *window, struct buffer *buffer, const char *text); +void get_text_size(struct window *window, int *width, int *height, const char *fmt, ...); +void pango_printf(struct window *window, const char *fmt, ...); + +#endif diff --git a/include/client/window.h b/include/client/window.h index af954003..eff9032d 100644 --- a/include/client/window.h +++ b/include/client/window.h @@ -34,6 +34,7 @@ struct window { struct wl_callback *frame_cb; struct cursor cursor; uint32_t width, height; + char *font; cairo_t *cairo; }; diff --git a/swaybar/main.c b/swaybar/main.c index b25d8252..7a54f74b 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -2,8 +2,11 @@ #include #include "client/registry.h" #include "client/window.h" +#include "client/pango.h" #include "log.h" +#define MARGIN 5 + struct box_colors { uint32_t border; uint32_t background; @@ -71,15 +74,17 @@ void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { } void render() { - // Reset buffer cairo_save(window->cairo); cairo_set_operator(window->cairo, CAIRO_OPERATOR_CLEAR); cairo_paint(window->cairo); cairo_restore(window->cairo); - // Draw bar cairo_set_source_u32(window->cairo, colors.background); cairo_paint(window->cairo); + + cairo_move_to(window->cairo, MARGIN, MARGIN); + cairo_set_source_u32(window->cairo, colors.statusline); + pango_printf(window, "TODO: finish bar"); } int main(int argc, char **argv) { @@ -101,6 +106,10 @@ int main(int argc, char **argv) { desktop_shell_set_panel(registry->desktop_shell, output->output, window->surface); desktop_shell_set_panel_position(registry->desktop_shell, DESKTOP_SHELL_PANEL_POSITION_BOTTOM); + int width, height; + get_text_size(window, &width, &height, "Test string for measuring purposes"); + window->height = height + MARGIN * 2; + do { if (window_prerender(window) && window->cairo) { render(); diff --git a/wayland/pango.c b/wayland/pango.c new file mode 100644 index 00000000..9766be6a --- /dev/null +++ b/wayland/pango.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include "client/window.h" +#include "client/buffer.h" +#include "log.h" + +PangoLayout *get_pango_layout(struct window *window, const char *text) { + PangoLayout *layout = pango_cairo_create_layout(window->cairo); + pango_layout_set_text(layout, text, -1); + PangoFontDescription *desc = pango_font_description_from_string(window->font); + pango_layout_set_font_description(layout, desc); + pango_layout_set_single_paragraph_mode(layout, 1); + pango_font_description_free(desc); + return layout; +} + +void get_text_size(struct window *window, int *width, int *height, const char *fmt, ...) { + char *buf = malloc(2048); + + va_list args; + va_start(args, fmt); + if (vsnprintf(buf, 2048, fmt, args) >= 2048) { + strcpy(buf, "[buffer overflow]"); + } + va_end(args); + + PangoLayout *layout = get_pango_layout(window, buf); + pango_cairo_update_layout(window->cairo, layout); + + pango_layout_get_pixel_size(layout, width, height); + + g_object_unref(layout); + + free(buf); +} + +void pango_printf(struct window *window, const char *fmt, ...) { + char *buf = malloc(2048); + + va_list args; + va_start(args, fmt); + if (vsnprintf(buf, 2048, fmt, args) >= 2048) { + strcpy(buf, "[buffer overflow]"); + } + va_end(args); + + PangoLayout *layout = get_pango_layout(window, buf); + pango_cairo_update_layout(window->cairo, layout); + + pango_cairo_show_layout(window->cairo, layout); + + g_object_unref(layout); + + free(buf); +} diff --git a/wayland/window.c b/wayland/window.c index 13d4c7b2..916e3b57 100644 --- a/wayland/window.c +++ b/wayland/window.c @@ -63,6 +63,7 @@ struct window *window_setup(struct registry *registry, uint32_t width, uint32_t window->width = width; window->height = height; window->registry = registry; + window->font = "monospace 10"; window->surface = wl_compositor_create_surface(registry->compositor); if (shell_surface) { @@ -74,6 +75,8 @@ struct window *window_setup(struct registry *registry, uint32_t width, uint32_t wl_pointer_add_listener(registry->pointer, &pointer_listener, window); } + get_next_buffer(window); + window->cursor.cursor_theme = wl_cursor_theme_load("default", 32, registry->shm); // TODO: let you customize this window->cursor.cursor = wl_cursor_theme_get_cursor(window->cursor.cursor_theme, "left_ptr"); window->cursor.surface = wl_compositor_create_surface(registry->compositor); @@ -118,4 +121,5 @@ int window_render(struct window *window) { } void window_teardown(struct window *window) { + // TODO }