labwc/src/img/img-svg.c
tokyo4j 9e05147bd3 img: apply padding around window icon only horizontally
16dbdc64 changed the padding around the app icon in the titlebar to be
applied both vertically and horizontally rather than only horizontally
because it was more natural from a developer's perspective, but some users
complained about the smaller icons in certain configurations.

So let's undo the change in 16dbdc64 and apply the icon padding only
horizontally for now.

We can add configurations for the icon padding (or icon size independent
from window.button.{width,height}?) later.
2025-01-04 17:31:03 +09:00

72 lines
1.6 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 "common/string-helpers.h"
#include "img/img-svg.h"
#include "labwc.h"
RsvgHandle *
img_svg_load(const char *filename)
{
if (string_null_or_empty(filename)) {
return NULL;
}
GError *err = NULL;
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 NULL;
}
return svg;
}
struct lab_data_buffer *
img_svg_render(RsvgHandle *svg, int w, int h, int padding_x, double scale)
{
struct lab_data_buffer *buffer = buffer_create_cairo(w, h, scale);
cairo_surface_t *image = buffer->surface;
cairo_t *cr = cairo_create(image);
GError *err = NULL;
RsvgRectangle viewport = {
.x = padding_x,
.y = 0,
.width = w - 2 * padding_x,
.height = h,
};
rsvg_handle_render_document(svg, cr, &viewport, &err);
if (err) {
wlr_log(WLR_ERROR, "error rendering svg: %s", err->message);
g_error_free(err);
goto error;
}
if (cairo_surface_status(image)) {
wlr_log(WLR_ERROR, "error reading svg file");
goto error;
}
cairo_surface_flush(buffer->surface);
cairo_destroy(cr);
return buffer;
error:
wlr_buffer_drop(&buffer->base);
cairo_destroy(cr);
g_object_unref(svg);
return NULL;
}