2023-08-21 21:26:08 +01:00
|
|
|
// 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"
|
2024-01-19 19:06:07 +00:00
|
|
|
#include "common/string-helpers.h"
|
2024-11-28 19:21:18 +09:00
|
|
|
#include "img/img-svg.h"
|
2023-08-21 21:26:08 +01:00
|
|
|
#include "labwc.h"
|
|
|
|
|
|
2024-11-28 19:21:18 +09:00
|
|
|
RsvgHandle *
|
|
|
|
|
img_svg_load(const char *filename)
|
2023-08-21 21:26:08 +01:00
|
|
|
{
|
2024-06-10 00:05:43 +02:00
|
|
|
if (string_null_or_empty(filename)) {
|
2024-11-28 19:21:18 +09:00
|
|
|
return NULL;
|
2024-06-10 00:05:43 +02:00
|
|
|
}
|
2023-08-21 21:26:08 +01:00
|
|
|
|
|
|
|
|
GError *err = NULL;
|
|
|
|
|
RsvgHandle *svg = rsvg_handle_new_from_file(filename, &err);
|
|
|
|
|
if (err) {
|
2024-06-10 00:05:43 +02:00
|
|
|
wlr_log(WLR_DEBUG, "error reading svg %s-%s", filename, err->message);
|
2023-08-21 21:26:08 +01:00
|
|
|
g_error_free(err);
|
|
|
|
|
/*
|
|
|
|
|
* rsvg_handle_new_from_file() returns NULL if an error occurs,
|
|
|
|
|
* so there is no need to free svg here.
|
|
|
|
|
*/
|
2024-11-28 19:21:18 +09:00
|
|
|
return NULL;
|
2023-08-21 21:26:08 +01:00
|
|
|
}
|
2024-11-28 19:21:18 +09:00
|
|
|
return svg;
|
|
|
|
|
}
|
2023-08-21 21:26:08 +01:00
|
|
|
|
2024-11-28 19:21:18 +09:00
|
|
|
struct lab_data_buffer *
|
2025-01-09 17:20:08 +09:00
|
|
|
img_svg_render(RsvgHandle *svg, int w, int h, double scale)
|
2024-11-28 19:21:18 +09:00
|
|
|
{
|
|
|
|
|
struct lab_data_buffer *buffer = buffer_create_cairo(w, h, scale);
|
|
|
|
|
cairo_surface_t *image = buffer->surface;
|
2024-11-28 06:58:17 +09:00
|
|
|
cairo_t *cr = cairo_create(image);
|
2024-11-28 19:21:18 +09:00
|
|
|
GError *err = NULL;
|
2023-08-21 21:26:08 +01:00
|
|
|
|
2024-11-28 19:21:18 +09:00
|
|
|
RsvgRectangle viewport = {
|
2025-01-09 17:20:08 +09:00
|
|
|
.width = w,
|
2025-01-04 16:50:21 +09:00
|
|
|
.height = h,
|
2024-11-28 19:21:18 +09:00
|
|
|
};
|
2023-08-21 21:26:08 +01:00
|
|
|
rsvg_handle_render_document(svg, cr, &viewport, &err);
|
|
|
|
|
if (err) {
|
2024-11-28 19:21:18 +09:00
|
|
|
wlr_log(WLR_ERROR, "error rendering svg: %s", err->message);
|
2023-08-21 21:26:08 +01:00
|
|
|
g_error_free(err);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
if (cairo_surface_status(image)) {
|
2024-11-28 19:21:18 +09:00
|
|
|
wlr_log(WLR_ERROR, "error reading svg file");
|
2023-08-21 21:26:08 +01:00
|
|
|
goto error;
|
|
|
|
|
}
|
2024-11-28 19:21:18 +09:00
|
|
|
cairo_surface_flush(buffer->surface);
|
2024-11-28 06:58:17 +09:00
|
|
|
cairo_destroy(cr);
|
2023-08-21 21:26:08 +01:00
|
|
|
|
2024-11-28 19:21:18 +09:00
|
|
|
return buffer;
|
2023-08-21 21:26:08 +01:00
|
|
|
|
|
|
|
|
error:
|
2024-11-28 19:21:18 +09:00
|
|
|
wlr_buffer_drop(&buffer->base);
|
2024-11-28 06:58:17 +09:00
|
|
|
cairo_destroy(cr);
|
2024-11-28 19:21:18 +09:00
|
|
|
return NULL;
|
2023-08-21 21:26:08 +01:00
|
|
|
}
|