Flesh out pango markup implementation

This commit is contained in:
Drew DeVault 2016-04-17 11:35:22 -04:00
parent 083316c5ce
commit 84fae94ab9
6 changed files with 33 additions and 18 deletions

View file

@ -23,6 +23,7 @@ struct status_block {
char *name, *instance; char *name, *instance;
bool separator; bool separator;
int separator_block_width; int separator_block_width;
bool markup;
// Airblader features // Airblader features
uint32_t background; uint32_t background;
uint32_t border; uint32_t border;

View file

@ -4,9 +4,11 @@
#include <cairo/cairo.h> #include <cairo/cairo.h>
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text); PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, bool markup);
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, const char *fmt, ...); void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
void pango_printf(cairo_t *cairo, const char *font, const char *fmt, ...); bool markup, const char *fmt, ...);
void pango_printf(cairo_t *cairo, const char *font, bool markup, const char *fmt, ...);
#endif #endif

View file

@ -85,7 +85,7 @@ int get_font_text_height(const char *font) {
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200); cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200);
cairo_t *cr = cairo_create(surface); cairo_t *cr = cairo_create(surface);
int width, height; int width, height;
get_text_size(cr, font, &width, &height, "Gg"); get_text_size(cr, font, &width, &height, false, "Gg");
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
cairo_destroy(cr); cairo_destroy(cr);
return height; return height;
@ -162,7 +162,7 @@ static void render_with_title_bar(swayc_t *view, cairo_t *cr, struct border_colo
int y = MIN(view->actual_geometry.origin.y - height - 2, 2); int y = MIN(view->actual_geometry.origin.y - height - 2, 2);
cairo_move_to(cr, x, y); cairo_move_to(cr, x, y);
cairo_set_source_u32(cr, colors->text); cairo_set_source_u32(cr, colors->text);
pango_printf(cr, config->font, "%s", view->name); pango_printf(cr, config->font, false, "%s", view->name);
} }
// header bottom line // header bottom line

View file

@ -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->cairo, window->font, "%s", block->full_text); pango_printf(window->cairo, window->font, block->markup, "%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->cairo, window->font, "%s", config->sep_symbol); pango_printf(window->cairo, window->font, false, "%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,
@ -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->cairo, window->font, "%s", name); pango_printf(window->cairo, window->font, false, "%s", name);
*x += width + ws_horizontal_padding * 2 + ws_spacing; *x += width + ws_horizontal_padding * 2 + ws_spacing;
@ -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->cairo, window->font, "%s", config->mode); pango_printf(window->cairo, window->font, false, "%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) {
@ -280,7 +280,7 @@ void render(struct output *output, struct config *config, struct status_line *li
if (line->protocol == TEXT) { if (line->protocol == TEXT) {
get_text_size(window->cairo, window->font, &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->cairo, window->font, "%s", line->text_line); pango_printf(window->cairo, window->font, true, "%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,8 @@ 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->cairo, window->font, &text_width, &text_height, "Test string for measuring purposes"); get_text_size(window->cairo, window->font, &text_width, &text_height, false,
"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

@ -71,7 +71,7 @@ static void parse_json(struct bar *bar, const char *text) {
json_object *full_text, *short_text, *color, *min_width, *align, *urgent; json_object *full_text, *short_text, *color, *min_width, *align, *urgent;
json_object *name, *instance, *separator, *separator_block_width; json_object *name, *instance, *separator, *separator_block_width;
json_object *background, *border, *border_top, *border_bottom; json_object *background, *border, *border_top, *border_bottom;
json_object *border_left, *border_right; json_object *border_left, *border_right, *markup;
json_object *json = json_object_array_get_idx(results, i); json_object *json = json_object_array_get_idx(results, i);
if (!json) { if (!json) {
@ -86,6 +86,7 @@ static void parse_json(struct bar *bar, const char *text) {
json_object_object_get_ex(json, "urgent", &urgent); json_object_object_get_ex(json, "urgent", &urgent);
json_object_object_get_ex(json, "name", &name); json_object_object_get_ex(json, "name", &name);
json_object_object_get_ex(json, "instance", &instance); json_object_object_get_ex(json, "instance", &instance);
json_object_object_get_ex(json, "markup", &markup);
json_object_object_get_ex(json, "separator", &separator); json_object_object_get_ex(json, "separator", &separator);
json_object_object_get_ex(json, "separator_block_width", &separator_block_width); json_object_object_get_ex(json, "separator_block_width", &separator_block_width);
json_object_object_get_ex(json, "background", &background); json_object_object_get_ex(json, "background", &background);
@ -139,6 +140,10 @@ static void parse_json(struct bar *bar, const char *text) {
new->instance = strdup(json_object_get_string(instance)); new->instance = strdup(json_object_get_string(instance));
} }
if (markup) {
new->markup = json_object_get_boolean(markup);
}
if (separator) { if (separator) {
new->separator = json_object_get_int(separator); new->separator = json_object_get_int(separator);
} else { } else {

View file

@ -4,11 +4,16 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include "log.h" #include "log.h"
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text) { PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, bool markup) {
PangoLayout *layout = pango_cairo_create_layout(cairo); PangoLayout *layout = pango_cairo_create_layout(cairo);
if (markup) {
pango_layout_set_markup(layout, text, -1); pango_layout_set_markup(layout, text, -1);
} else {
pango_layout_set_text(layout, text, -1);
}
PangoFontDescription *desc = pango_font_description_from_string(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);
@ -16,7 +21,8 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text
return layout; return layout;
} }
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, const char *fmt, ...) { void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
bool markup, const char *fmt, ...) {
char *buf = malloc(2048); char *buf = malloc(2048);
va_list args; va_list args;
@ -26,7 +32,7 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, co
} }
va_end(args); va_end(args);
PangoLayout *layout = get_pango_layout(cairo, font, buf); PangoLayout *layout = get_pango_layout(cairo, font, buf, markup);
pango_cairo_update_layout(cairo, layout); pango_cairo_update_layout(cairo, layout);
pango_layout_get_pixel_size(layout, width, height); pango_layout_get_pixel_size(layout, width, height);
@ -36,7 +42,7 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, co
free(buf); free(buf);
} }
void pango_printf(cairo_t *cairo, const char *font, const char *fmt, ...) { void pango_printf(cairo_t *cairo, const char *font, bool markup, const char *fmt, ...) {
char *buf = malloc(2048); char *buf = malloc(2048);
va_list args; va_list args;
@ -46,7 +52,7 @@ void pango_printf(cairo_t *cairo, const char *font, const char *fmt, ...) {
} }
va_end(args); va_end(args);
PangoLayout *layout = get_pango_layout(cairo, font, buf); PangoLayout *layout = get_pango_layout(cairo, font, buf, markup);
pango_cairo_update_layout(cairo, layout); pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout); pango_cairo_show_layout(cairo, layout);