opt: Reduce unnecessary text drawing

This commit is contained in:
DreamMaoMao 2026-06-17 08:35:44 +08:00
parent a515ad9b91
commit a7acc7f5f3
10 changed files with 308 additions and 194 deletions

View file

@ -1,24 +1,50 @@
#ifndef MANGO_TEXT_NODE_H
#define MANGO_TEXT_NODE_H
#include <cairo.h>
#include <pango/pangocairo.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_scene.h>
// 自定义 wlr_buffer仅用于包装 cairo surface不负责 surface 的销毁
struct mango_text_buffer {
struct wlr_buffer base;
cairo_surface_t *surface;
};
struct mango_text_node {
struct wlr_scene_buffer *scene_buffer;
int logical_width;
int logical_height;
const char *font_desc;
// 外观属性
float fg_color[4]; // 文本颜色 RGBA默认白色
float bg_color[4]; // 背景色 RGBA默认透明
float border_color[4]; // 边框色 RGBA默认透明
int32_t border_width; // 边框宽度(逻辑像素),<=0 则不绘制
int32_t corner_radius; // 圆角半径(逻辑像素),<0 时自动取
// min(width,height)/2 形成胶囊
int32_t padding_x; // 文本左右内边距(逻辑像素)
int32_t padding_y; // 文本上下内边距(逻辑像素)
float fg_color[4];
float bg_color[4];
float border_color[4];
int32_t border_width;
int32_t corner_radius;
int32_t padding_x, padding_y;
char *font_desc;
char *cached_text;
float cached_scale;
float cached_fg_color[4];
float cached_bg_color[4];
float cached_border_color[4];
float cached_border_width;
float cached_corner_radius;
float cached_padding_x, cached_padding_y;
char *cached_font_desc;
cairo_surface_t *surface;
struct mango_text_buffer *buffer;
int32_t surface_pixel_w, surface_pixel_h;
cairo_surface_t *measure_surface;
cairo_t *measure_cr;
PangoContext *measure_context;
PangoLayout *measure_layout;
float measure_scale;
int32_t logical_width;
int32_t logical_height;
};
typedef struct {
@ -38,15 +64,15 @@ void mango_text_node_destroy(struct mango_text_node *node);
void mango_text_node_update(struct mango_text_node *node, const char *text,
float scale);
// 外观设置接口
void mango_text_node_set_background(struct mango_text_node *node, float r,
float g, float b, float a);
void mango_text_node_set_border(struct mango_text_node *node, float r, float g,
float b, float a, float width, float radius);
void mango_text_node_set_padding(struct mango_text_node *node, float pad_x,
float pad_y);
float b, float a, int32_t width,
int32_t radius);
void mango_text_node_set_padding(struct mango_text_node *node, int32_t pad_x,
int32_t pad_y);
void mango_text_node_destroy(struct mango_text_node *node);
void mango_text_global_finish(void);
#endif