Add text rendering support to wayland clients

This commit is contained in:
Drew DeVault 2015-11-29 15:27:28 -05:00
parent 5a369b3132
commit 82d464bb90
5 changed files with 87 additions and 2 deletions

12
include/client/pango.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef _SWAY_CLIENT_PANGO_H
#define _SWAY_CLIENT_PANGO_H
#include "client/window.h"
#include "client/buffer.h"
#include <stdarg.h>
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

View file

@ -34,6 +34,7 @@ struct window {
struct wl_callback *frame_cb;
struct cursor cursor;
uint32_t width, height;
char *font;
cairo_t *cairo;
};

View file

@ -2,8 +2,11 @@
#include <stdlib.h>
#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();

59
wayland/pango.c Normal file
View file

@ -0,0 +1,59 @@
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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);
}

View file

@ -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
}