Make client/pango.h not depend on client/window.h

This commit is contained in:
Mikkel Oscar Lyderik 2016-03-28 22:22:46 +02:00
parent da567317ab
commit cefcce48aa
3 changed files with 26 additions and 28 deletions

View file

@ -1,12 +1,12 @@
#ifndef _SWAY_CLIENT_PANGO_H #ifndef _SWAY_CLIENT_PANGO_H
#define _SWAY_CLIENT_PANGO_H #define _SWAY_CLIENT_PANGO_H
#include "client/window.h" #include <cairo/cairo.h>
#include "client/buffer.h" #include <pango/pangocairo.h>
#include <stdarg.h> #include <stdarg.h>
PangoLayout *get_pango_layout(struct window *window, struct buffer *buffer, const char *text); PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text);
void get_text_size(struct window *window, int *width, int *height, const char *fmt, ...); void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, const char *fmt, ...);
void pango_printf(struct window *window, const char *fmt, ...); void pango_printf(cairo_t *cairo, const char *font, const char *fmt, ...);
#endif #endif

View file

@ -50,7 +50,7 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y
static void render_block(struct window *window, struct config *config, struct status_block *block, double *x, bool edge) { static void render_block(struct window *window, struct config *config, struct status_block *block, double *x, bool edge) {
int width, height, sep_width; int width, height, sep_width;
get_text_size(window, &width, &height, "%s", block->full_text); get_text_size(window->cairo, window->font, &width, &height, "%s", block->full_text);
int textwidth = width; int textwidth = width;
double block_width = width; double block_width = width;
@ -74,7 +74,7 @@ static void render_block(struct window *window, struct config *config, struct st
// Add separator // Add separator
if (!edge) { if (!edge) {
if (config->sep_symbol) { if (config->sep_symbol) {
get_text_size(window, &sep_width, &height, "%s", config->sep_symbol); get_text_size(window->cairo, window->font, &sep_width, &height, "%s", config->sep_symbol);
if (sep_width > block->separator_block_width) { if (sep_width > block->separator_block_width) {
block->separator_block_width = sep_width + margin * 2; block->separator_block_width = sep_width + margin * 2;
} }
@ -136,7 +136,7 @@ static void render_block(struct window *window, struct config *config, struct st
cairo_move_to(window->cairo, offset, margin); cairo_move_to(window->cairo, offset, margin);
cairo_set_source_u32(window->cairo, block->color); cairo_set_source_u32(window->cairo, block->color);
pango_printf(window, "%s", block->full_text); pango_printf(window->cairo, window->font, "%s", block->full_text);
pos += width; pos += width;
@ -159,7 +159,7 @@ static void render_block(struct window *window, struct config *config, struct st
if (config->sep_symbol) { if (config->sep_symbol) {
offset = pos + (block->separator_block_width - sep_width) / 2; offset = pos + (block->separator_block_width - sep_width) / 2;
cairo_move_to(window->cairo, offset, margin); cairo_move_to(window->cairo, offset, margin);
pango_printf(window, "%s", config->sep_symbol); pango_printf(window->cairo, window->font, "%s", config->sep_symbol);
} else { } else {
cairo_set_line_width(window->cairo, 1); cairo_set_line_width(window->cairo, 1);
cairo_move_to(window->cairo, pos + block->separator_block_width/2, cairo_move_to(window->cairo, pos + block->separator_block_width/2,
@ -201,7 +201,7 @@ static void render_workspace_button(struct window *window, struct config *config
char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name);
int width, height; int width, height;
get_text_size(window, &width, &height, "%s", name); get_text_size(window->cairo, window->font, &width, &height, "%s", name);
struct box_colors box_colors; struct box_colors box_colors;
if (ws->urgent) { if (ws->urgent) {
box_colors = config->colors.urgent_workspace; box_colors = config->colors.urgent_workspace;
@ -228,7 +228,7 @@ static void render_workspace_button(struct window *window, struct config *config
// text // text
cairo_set_source_u32(window->cairo, box_colors.text); cairo_set_source_u32(window->cairo, box_colors.text);
cairo_move_to(window->cairo, (int)*x + ws_horizontal_padding, margin); cairo_move_to(window->cairo, (int)*x + ws_horizontal_padding, margin);
pango_printf(window, "%s", name); pango_printf(window->cairo, window->font, "%s", name);
*x += width + ws_horizontal_padding * 2 + ws_spacing; *x += width + ws_horizontal_padding * 2 + ws_spacing;
@ -237,7 +237,7 @@ static void render_workspace_button(struct window *window, struct config *config
static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) { static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) {
int width, height; int width, height;
get_text_size(window, &width, &height, "%s", config->mode); get_text_size(window->cairo, window->font, &width, &height, "%s", config->mode);
// background // background
cairo_set_source_u32(window->cairo, config->colors.binding_mode.background); cairo_set_source_u32(window->cairo, config->colors.binding_mode.background);
@ -254,7 +254,7 @@ static void render_binding_mode_indicator(struct window *window, struct config *
// text // text
cairo_set_source_u32(window->cairo, config->colors.binding_mode.text); cairo_set_source_u32(window->cairo, config->colors.binding_mode.text);
cairo_move_to(window->cairo, (int)pos + ws_horizontal_padding, margin); cairo_move_to(window->cairo, (int)pos + ws_horizontal_padding, margin);
pango_printf(window, "%s", config->mode); pango_printf(window->cairo, window->font, "%s", config->mode);
} }
void render(struct output *output, struct config *config, struct status_line *line) { void render(struct output *output, struct config *config, struct status_line *line) {
@ -278,9 +278,9 @@ void render(struct output *output, struct config *config, struct status_line *li
int width, height; int width, height;
if (line->protocol == TEXT) { if (line->protocol == TEXT) {
get_text_size(window, &width, &height, "%s", line->text_line); get_text_size(window->cairo, window->font, &width, &height, "%s", line->text_line);
cairo_move_to(cairo, window->width - margin - width, margin); cairo_move_to(cairo, window->width - margin - width, margin);
pango_printf(window, "%s", line->text_line); pango_printf(window->cairo, window->font, "%s", line->text_line);
} else if (line->protocol == I3BAR && line->block_line) { } else if (line->protocol == I3BAR && line->block_line) {
double pos = window->width - 0.5; double pos = window->width - 0.5;
bool edge = true; bool edge = true;
@ -312,7 +312,7 @@ void render(struct output *output, struct config *config, struct status_line *li
void set_window_height(struct window *window, int height) { void set_window_height(struct window *window, int height) {
int text_width, text_height; int text_width, text_height;
get_text_size(window, &text_width, &text_height, "Test string for measuring purposes"); get_text_size(window->cairo, window->font, &text_width, &text_height, "Test string for measuring purposes");
if (height > 0) { if (height > 0) {
margin = (height - text_height) / 2; margin = (height - text_height) / 2;
ws_vertical_padding = margin - 1.5; ws_vertical_padding = margin - 1.5;

View file

@ -4,21 +4,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "client/window.h"
#include "client/buffer.h"
#include "log.h" #include "log.h"
PangoLayout *get_pango_layout(struct window *window, const char *text) { PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text) {
PangoLayout *layout = pango_cairo_create_layout(window->cairo); PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_layout_set_text(layout, text, -1); pango_layout_set_text(layout, text, -1);
PangoFontDescription *desc = pango_font_description_from_string(window->font); PangoFontDescription *desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc); pango_layout_set_font_description(layout, desc);
pango_layout_set_single_paragraph_mode(layout, 1); pango_layout_set_single_paragraph_mode(layout, 1);
pango_font_description_free(desc); pango_font_description_free(desc);
return layout; return layout;
} }
void get_text_size(struct window *window, int *width, int *height, const char *fmt, ...) { void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, const char *fmt, ...) {
char *buf = malloc(2048); char *buf = malloc(2048);
va_list args; va_list args;
@ -28,8 +26,8 @@ void get_text_size(struct window *window, int *width, int *height, const char *f
} }
va_end(args); va_end(args);
PangoLayout *layout = get_pango_layout(window, buf); PangoLayout *layout = get_pango_layout(cairo, font, buf);
pango_cairo_update_layout(window->cairo, layout); pango_cairo_update_layout(cairo, layout);
pango_layout_get_pixel_size(layout, width, height); pango_layout_get_pixel_size(layout, width, height);
@ -38,7 +36,7 @@ void get_text_size(struct window *window, int *width, int *height, const char *f
free(buf); free(buf);
} }
void pango_printf(struct window *window, const char *fmt, ...) { void pango_printf(cairo_t *cairo, const char *font, const char *fmt, ...) {
char *buf = malloc(2048); char *buf = malloc(2048);
va_list args; va_list args;
@ -48,10 +46,10 @@ void pango_printf(struct window *window, const char *fmt, ...) {
} }
va_end(args); va_end(args);
PangoLayout *layout = get_pango_layout(window, buf); PangoLayout *layout = get_pango_layout(cairo, font, buf);
pango_cairo_update_layout(window->cairo, layout); pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(window->cairo, layout); pango_cairo_show_layout(cairo, layout);
g_object_unref(layout); g_object_unref(layout);