/* * This file is part of wl-overlay: show overlays with images and text in Wayland * Copyright © 2022 Midgard * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include "common.h" #include "state.h" #define float_is_zero(number) (number < 1e-8) static void set_source_argb_cairo(cairo_t *cairo, struct color_argb *color) { cairo_set_source_rgba(cairo, color->r, color->g, color->b, color->a); } static void draw_rounded_rectangle(cairo_t *cr, int x, int y, int width, int height, int radius) { cairo_new_sub_path(cr); cairo_arc(cr, x + radius, y + radius, radius, M_PI, 3*M_PI/2); cairo_arc(cr, x + width - radius, y + radius, radius, 3*M_PI/2, 0); cairo_arc(cr, x + width - radius, y + height - radius, radius, 0, M_PI/2); cairo_arc(cr, x + radius, y + height - radius, radius, M_PI/2, M_PI); cairo_close_path(cr); } void draw_frame_into(uint32_t *data, struct user_request *user_request, const int width, const int height) { cairo_surface_t *cairo_target_surface = cairo_image_surface_create_for_data( (unsigned char *)data, CAIRO_FORMAT_ARGB32, width, height, width * 4); cairo_t *cairo = cairo_create(cairo_target_surface); /* Draw background */ if (!float_is_zero(user_request->backdrop.a)) { set_source_argb_cairo(cairo, &user_request->backdrop); draw_rounded_rectangle(cairo, 0, 0, width, height, user_request->border_radius); cairo_fill(cairo); } /* Load and draw PNG */ if (strcmp("", user_request->graphics_filename) != STR_EQUAL) { cairo_save(cairo); cairo_surface_t *cairo_png_surface = cairo_image_surface_create_from_png(user_request->graphics_filename); if (cairo_surface_status(cairo_png_surface) != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "Failed to open PNG image.\n"); exit(1); } /* Set transformations to fit image in box */ int img_width = cairo_image_surface_get_width(cairo_png_surface); int img_height = cairo_image_surface_get_height(cairo_png_surface); double width_scale = ((double)width - 2*user_request->padding) / img_width; double height_scale = (((double)height - 2*user_request->padding) * 3 / 4.0) / img_height; double scale = MIN(width_scale, height_scale); if (scale > 1.0) scale = 1.0; cairo_translate(cairo, (width - img_width *scale)/2.0, (3*height/4.0 - img_height*scale)/2.0); cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, cairo_png_surface, 0.0, 0.0); cairo_paint(cairo); cairo_surface_destroy(cairo_png_surface); cairo_restore(cairo); } /* Draw text */ if (user_request->text) { PangoLayout *layout = pango_cairo_create_layout(cairo); pango_layout_set_text(layout, user_request->text, -1); pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); pango_layout_set_width(layout, (width - 2*user_request->padding) * PANGO_SCALE); PangoFontDescription *desc = pango_font_description_from_string(user_request->font); pango_layout_set_font_description(layout, desc); pango_font_description_free(desc); set_source_argb_cairo(cairo, &user_request->text_color); int text_width = 0, text_height = 0; pango_layout_get_size(layout, &text_width, &text_height); cairo_move_to(cairo, user_request->padding, 3*height/4.0); pango_cairo_show_layout(cairo, layout); g_object_unref(layout); } cairo_surface_destroy(cairo_target_surface); cairo_destroy(cairo); }