2022-06-13 21:33:32 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <wayland-server-core.h>
|
|
|
|
|
#include <wlr/util/log.h>
|
|
|
|
|
#include "buffer.h"
|
|
|
|
|
#include "common/font.h"
|
2022-09-16 18:41:02 -04:00
|
|
|
#include "common/mem.h"
|
2024-05-22 09:56:16 +09:00
|
|
|
#include "common/scaled-scene-buffer.h"
|
|
|
|
|
#include "common/scaled-font-buffer.h"
|
2022-06-13 21:33:32 +02:00
|
|
|
|
|
|
|
|
static struct lab_data_buffer *
|
|
|
|
|
_create_buffer(struct scaled_scene_buffer *scaled_buffer, double scale)
|
|
|
|
|
{
|
2024-08-17 13:41:10 +01:00
|
|
|
struct lab_data_buffer *buffer = NULL;
|
2022-06-13 21:33:32 +02:00
|
|
|
struct scaled_font_buffer *self = scaled_buffer->data;
|
|
|
|
|
|
|
|
|
|
/* Buffer gets free'd automatically along the backing wlr_buffer */
|
|
|
|
|
font_buffer_create(&buffer, self->max_width, self->text,
|
2024-03-16 22:20:40 -04:00
|
|
|
&self->font, self->color, self->bg_color, self->arrow, scale);
|
2022-06-13 21:33:32 +02:00
|
|
|
|
2024-08-17 13:41:10 +01:00
|
|
|
if (!buffer) {
|
|
|
|
|
wlr_log(WLR_ERROR, "font_buffer_create() failed");
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 01:42:54 -04:00
|
|
|
self->width = buffer ? buffer->logical_width : 0;
|
|
|
|
|
self->height = buffer ? buffer->logical_height : 0;
|
2022-06-13 21:33:32 +02:00
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_destroy(struct scaled_scene_buffer *scaled_buffer)
|
|
|
|
|
{
|
|
|
|
|
struct scaled_font_buffer *self = scaled_buffer->data;
|
2023-10-14 22:26:40 +02:00
|
|
|
scaled_buffer->data = NULL;
|
|
|
|
|
|
2022-08-02 22:01:51 +01:00
|
|
|
zfree(self->text);
|
|
|
|
|
zfree(self->font.name);
|
2022-08-02 22:00:24 +01:00
|
|
|
zfree(self->arrow);
|
2023-10-14 22:26:40 +02:00
|
|
|
free(self);
|
2022-06-13 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct scaled_scene_buffer_impl impl = {
|
|
|
|
|
.create_buffer = _create_buffer,
|
|
|
|
|
.destroy = _destroy
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Public API */
|
|
|
|
|
struct scaled_font_buffer *
|
|
|
|
|
scaled_font_buffer_create(struct wlr_scene_tree *parent)
|
|
|
|
|
{
|
|
|
|
|
assert(parent);
|
2022-09-18 15:22:26 -04:00
|
|
|
struct scaled_font_buffer *self = znew(*self);
|
2024-11-19 05:03:26 +09:00
|
|
|
struct scaled_scene_buffer *scaled_buffer = scaled_scene_buffer_create(
|
|
|
|
|
parent, &impl, NULL, /* drop_buffer */ true);
|
2022-06-13 21:33:32 +02:00
|
|
|
if (!scaled_buffer) {
|
|
|
|
|
free(self);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scaled_buffer->data = self;
|
|
|
|
|
self->scaled_buffer = scaled_buffer;
|
|
|
|
|
self->scene_buffer = scaled_buffer->scene_buffer;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-08-02 22:00:24 +01:00
|
|
|
scaled_font_buffer_update(struct scaled_font_buffer *self, const char *text,
|
2024-03-16 22:20:40 -04:00
|
|
|
int max_width, struct font *font, const float *color,
|
|
|
|
|
const float *bg_color, const char *arrow)
|
2022-06-13 21:33:32 +02:00
|
|
|
{
|
|
|
|
|
assert(self);
|
|
|
|
|
assert(text);
|
|
|
|
|
assert(font);
|
|
|
|
|
assert(color);
|
|
|
|
|
|
|
|
|
|
/* Clean up old internal state */
|
2022-08-02 22:01:51 +01:00
|
|
|
zfree(self->text);
|
|
|
|
|
zfree(self->font.name);
|
2022-08-02 22:00:24 +01:00
|
|
|
zfree(self->arrow);
|
2022-06-13 21:33:32 +02:00
|
|
|
|
|
|
|
|
/* Update internal state */
|
2022-09-16 18:41:02 -04:00
|
|
|
self->text = xstrdup(text);
|
2022-06-13 21:33:32 +02:00
|
|
|
self->max_width = max_width;
|
|
|
|
|
if (font->name) {
|
2022-09-16 18:41:02 -04:00
|
|
|
self->font.name = xstrdup(font->name);
|
2022-06-13 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
self->font.size = font->size;
|
2022-09-15 10:53:49 -04:00
|
|
|
self->font.slant = font->slant;
|
|
|
|
|
self->font.weight = font->weight;
|
2022-06-13 21:33:32 +02:00
|
|
|
memcpy(self->color, color, sizeof(self->color));
|
2024-03-16 22:20:40 -04:00
|
|
|
memcpy(self->bg_color, bg_color, sizeof(self->bg_color));
|
2022-09-16 18:41:02 -04:00
|
|
|
self->arrow = arrow ? xstrdup(arrow) : NULL;
|
2022-06-13 21:33:32 +02:00
|
|
|
|
|
|
|
|
/* Invalidate cache and force a new render */
|
|
|
|
|
scaled_scene_buffer_invalidate_cache(self->scaled_buffer);
|
|
|
|
|
}
|
2022-12-05 14:38:16 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
scaled_font_buffer_set_max_width(struct scaled_font_buffer *self, int max_width)
|
|
|
|
|
{
|
|
|
|
|
self->max_width = max_width;
|
|
|
|
|
scaled_scene_buffer_invalidate_cache(self->scaled_buffer);
|
|
|
|
|
}
|