labwc/src/img/img-svg.c
tokyo4j a745f91169 ssd: support window icons
The default `titleLayout` is updated to `icon:iconify,max,close` which
replaces the window menu button with the window icon.

When the icon file is not found or could not be loaded, the window menu
icon as before is shown.

The icon theme can be selected with `<theme><icon>`.

This commit adds libsfdo as an optional dependency. `-Dicon=disabled` can
be passsed to `meson setup` command in order to disable window icon, in
which case the window icon is always replaced with a window menu button.
2024-09-25 18:04:09 +09:00

69 lines
1.7 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) Johan Malm 2023
*/
#define _POSIX_C_SOURCE 200809L
#include <cairo.h>
#include <librsvg/rsvg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <wlr/util/log.h>
#include "buffer.h"
#include "img/img-svg.h"
#include "common/string-helpers.h"
#include "labwc.h"
void
img_svg_load(const char *filename, struct lab_data_buffer **buffer,
int size)
{
if (*buffer) {
wlr_buffer_drop(&(*buffer)->base);
*buffer = NULL;
}
if (string_null_or_empty(filename)) {
return;
}
GError *err = NULL;
RsvgRectangle viewport = { .width = size, .height = size };
RsvgHandle *svg = rsvg_handle_new_from_file(filename, &err);
if (err) {
wlr_log(WLR_DEBUG, "error reading svg %s-%s", filename, err->message);
g_error_free(err);
/*
* rsvg_handle_new_from_file() returns NULL if an error occurs,
* so there is no need to free svg here.
*/
return;
}
cairo_surface_t *image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
cairo_t *cr = cairo_create(image);
rsvg_handle_render_document(svg, cr, &viewport, &err);
if (err) {
wlr_log(WLR_ERROR, "error rendering svg %s-%s\n", filename, err->message);
g_error_free(err);
goto error;
}
if (cairo_surface_status(image)) {
wlr_log(WLR_ERROR, "error reading svg button '%s'", filename);
goto error;
}
cairo_surface_flush(image);
double w = cairo_image_surface_get_width(image);
double h = cairo_image_surface_get_height(image);
*buffer = buffer_create_cairo((int)w, (int)h, 1.0, /* free_on_destroy */ true);
cairo_t *cairo = (*buffer)->cairo;
cairo_set_source_surface(cairo, image, 0, 0);
cairo_paint_with_alpha(cairo, 1.0);
error:
cairo_destroy(cr);
cairo_surface_destroy(image);
g_object_unref(svg);
}