#include #include #include #include #include #include "draw.h" #include "button.h" #include "log.h" static void layout_destroy(Layout *layout) { if (layout->pl != NULL) g_object_unref(layout->pl); g_free(layout); } Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, int line_w) { Drawable *draw = g_malloc(sizeof(Drawable)); g_assert_nonnull(draw); log_debug("Pango loading font description '%s'", font); draw->desc = pango_font_description_from_string(font); log_debug("Pango found matching font '%s'", pango_font_description_get_family(draw->desc)); draw->height = height; draw->left_pad = left_pad; draw->right_pad = right_pad; draw->top_pad = top_pad; draw->line_w = line_w; draw->layouts = NULL; log_debug("Draw context created [height=%d, left_pad=%d, right_pad=%d, top_pad=%d, line_w=%d]", height, left_pad, right_pad, top_pad, line_w); return draw; } static void compute_width(Drawable *draw, Window *win, int *width, int *height) { // FIXME: Does not work for scale != 1 //double scale = window_get_scale(win); double scale = 1; int screen_width = win->con->screen_size->width; int screen_height = win->con->screen_size->height; *width = round(screen_width - draw->right_pad - draw->left_pad * scale); *height = round(draw->height * scale); } void draw_paint(Drawable *draw, Window *win) { // FIXME: Does not work for scale != 1 //double scale = window_get_scale(win); double scale = 1; int width, height; compute_width(draw, win, &width, &height); cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); cairo_t *cr = cairo_create(surface); cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD); cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); double degree = M_PI / 180.0; cairo_set_source_rgba(cr, draw->background.r, draw->background.g, draw->background.b, draw->background.a); // TODO: Here we should paint the shape of the bar, however there is a problem with the surface // painted in the pixmap to mask the window shape in window_paint_corners. // // This is caused by some difference between the two shapes (that should technically be the same) // which causes a mismatch between the two layers and leaves some black pixels visible //int radius = height / 2; //cairo_arc(cr, radius, radius, radius, 90.0 * degree, 270 * degree); //cairo_arc(cr, width - radius, radius, radius, 270 * degree, 450 * degree); //cairo_fill(cr); cairo_paint(cr); int line_w = draw->line_w * scale; cairo_set_line_width(cr, line_w); for (GList *it = draw->layouts; it; it = it->next) { Layout *layout = it->data; int radius = layout->height / 2; Color color = layout->btn->color; cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); cairo_new_sub_path(cr); cairo_arc(cr, layout->x + radius, layout->y + radius, radius, 90 * degree, 270 * degree); cairo_arc(cr, layout->x + layout->width - radius, layout->y + radius, radius, 270 * degree, 450 * degree); cairo_close_path(cr); cairo_fill(cr); color = layout->btn->line_color; cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); cairo_new_sub_path(cr); cairo_arc(cr, layout->x + radius, layout->y + radius, radius - line_w , 90 * degree, 270 * degree); cairo_arc(cr, layout->x + layout->width - radius, layout->y + radius, radius - line_w, 270 * degree, 450 * degree); cairo_close_path(cr); cairo_stroke(cr); if (layout->btn->simple) { int text_x = layout->x + (layout->width / 2) - (layout->text_w / 2); int text_y = layout->y + (layout->height / 2) - (layout->text_h / 2); color = CAST(layout->btn, ButtonSimple)->text_color; cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a); cairo_move_to(cr, text_x, text_y); pango_cairo_update_layout(cr, layout->pl); pango_cairo_show_layout(cr, layout->pl); } } cairo_destroy(cr); // TODO: Move these somewhere else window_move(win, draw->left_pad, draw->top_pad); window_resize(win, width, draw->height); window_paint_surface(win, surface, width, height); cairo_surface_destroy(surface); } static gint align_compare(gconstpointer a, gconstpointer b) { PangoAlignment a_align = ((Button *)a)->align; PangoAlignment b_align = ((Button *)b)->align; if (a_align < b_align) return -1; if (a_align > b_align) return 1; return 0; } // Works only for simple buttons static void layout_text(Layout *layout, PangoFontDescription *desc, double scale, int height, int radius) { const char *text = CAST(layout->btn, ButtonSimple)->text; pango_layout_set_font_description(layout->pl, desc); pango_layout_set_text(layout->pl, text, -1); pango_layout_set_alignment(layout->pl, PANGO_ALIGN_CENTER); int text_w, text_h; pango_layout_get_pixel_size(layout->pl, &text_w, &text_h); layout->text_w = ceil(text_w / scale); layout->text_h = ceil(text_h / scale); // If there is only one glyph the button should be round size_t text_l = g_utf8_strlen(text, -1); layout->width = text_l == 1 ? height : layout->text_w + 2 * radius; layout->height = height; } void draw_compute_layout(Drawable *draw, Window *win, GList *btns) { g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy); draw->layouts = NULL; // FIXME: Does not work for scale != 1 //double scale = window_get_scale(win); double scale = 1; int width, height; compute_width(draw, win, &width, &height); int x = 0; int radius = height / 2; int sep = 10 * scale; int line_w = draw->line_w * scale; btns = g_list_sort(btns, align_compare); GList *adjust_center = NULL; GList *adjust_right = NULL; int end_xs[3] = { 0 }; Button *prev = NULL; for (GList *it = btns; it; it = it->next) { Button *btn = it->data; Layout *layout = g_malloc(sizeof(Layout)); layout->btn = btn; if (prev != NULL && prev->align == btn->align) x += sep; layout->x = x + btn->padding; layout->y = btn->padding; draw->layouts = g_list_prepend(draw->layouts, layout); if (!btn->simple) { ButtonGroup *group = CAST(btn, ButtonGroup); g_assert_nonnull(group->children); // If there is only one child treat a group like a single button if (group->children->next == NULL) { Button *child = group->children->data; g_assert_true(child->simple); layout->btn = child; } else { for (GList *it = group->children; it; it = it->next) { Button *btn = it->data; // Nested groups are not allowed g_assert_true(btn->simple); Layout *child = g_malloc(sizeof(Layout)); child->btn = btn; child->x = x + btn->padding; child->y = btn->padding;; child->pl = pango_cairo_create_layout(window_get_context(win)); layout_text(child, draw->desc, scale, height, radius); draw->layouts = g_list_prepend(draw->layouts, child); x += child->width + line_w * 2; child->width -= 2 * btn->padding; child->height -= 2 * btn->padding; if (it->next != NULL) x += sep; } layout->pl = NULL; layout->width = x - layout->x; layout->height = height; // FIXME: Temporary solution to make the antialiasing (or the circles not overlapping correctly) problem less noticeable bool fix_left = CAST(g_list_first(group->children)->data, Button)->padding == 0; bool fix_right = CAST(g_list_last(group->children)->data, Button)->padding == 0; if (fix_left) { layout->x += 1; layout->width -= 1; } if (fix_right) layout->width -= 1; goto end; } } layout->pl = pango_cairo_create_layout(window_get_context(win)); layout_text(layout, draw->desc, scale, height, radius); x += layout->width + line_w * 2; layout->width -= 2 * btn->padding; layout->height -= 2 * btn->padding; end: if (prev != NULL && prev->align != btn->align) { if (btn->align == PANGO_ALIGN_CENTER) adjust_center = draw->layouts; else if (btn->align == PANGO_ALIGN_RIGHT) adjust_right = draw->layouts; } prev = btn; end_xs[btn->align] = x; } draw->layouts = g_list_reverse(draw->layouts); int widths[3] = { end_xs[0], end_xs[1] - end_xs[0], end_xs[2] - MAX(end_xs[1], end_xs[0]), }; int start_xs[3] = { 0, end_xs[0], MAX(end_xs[1], end_xs[0]), }; int center = round(width / 2); int center_off = (center - widths[1] / 2) - start_xs[1]; log_debug("Aligning center layout [x=%d, off=%d]", center, center_off); for (GList *it = adjust_center; it != adjust_right; it = it->next) { Layout *layout = it->data; layout->x += center_off; } int right = width - widths[2]; // The width is off by 2 line width int right_off = right - start_xs[2] + 2 * line_w; log_debug("Aligning right layout [x=%d, off=%d]", right, right_off); for (GList *it = adjust_right; it != NULL; it = it->next) { Layout *layout = it->data; layout->x += right_off; } log_debug("Updated layouts"); } void draw_set_background(Drawable *draw, Color background) { draw->background = background; } void draw_destroy(Drawable *draw) { g_list_free_full(draw->layouts, (GDestroyNotify)layout_destroy); pango_font_description_free(draw->desc); g_free(draw); } // vim: ts=4 sw=4 et