feat(bar): 新增 text 文字排版模块并将其集成到 bar 生命周期中
This commit is contained in:
@@ -31,6 +31,7 @@ add_executable(${APP_NAME}
|
||||
|
||||
${SOURCE_DIR}/bar/bar.c
|
||||
${SOURCE_DIR}/bar/cell.c
|
||||
${SOURCE_DIR}/bar/text.c
|
||||
${SOURCE_DIR}/bar/workspaces.c
|
||||
|
||||
${SOURCE_DIR}/base/color.c
|
||||
@@ -81,6 +82,8 @@ pkg_check_modules(deps REQUIRED
|
||||
|
||||
cairo
|
||||
cairo-xcb
|
||||
pango
|
||||
pangocairo
|
||||
)
|
||||
|
||||
target_include_directories(${APP_NAME} SYSTEM
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <zdwm/bar.h>
|
||||
#include <zdwm/types.h>
|
||||
|
||||
#include "bar/text.h"
|
||||
#include "bar/types.h"
|
||||
#include "bar/workspaces.h"
|
||||
#include "base/array.h"
|
||||
@@ -59,6 +60,9 @@ void bar_output_add_workspace(
|
||||
}
|
||||
|
||||
void bar_init(bar_t *bar) {
|
||||
auto c = &bar->config;
|
||||
bar->ctx = text_context_create(c->font_family, c->font_size, c->dpi);
|
||||
|
||||
for (size_t i = 0; i < bar->count; ++i) {
|
||||
auto bar_output = &bar->bars[i];
|
||||
bar_output_add_workspace(bar_output, &bar->config);
|
||||
@@ -73,6 +77,9 @@ static void bar_side_cleanup(bar_side_t *side) {
|
||||
}
|
||||
|
||||
void bar_cleanup(bar_t *bar) {
|
||||
text_context_destory(bar->ctx);
|
||||
bar->ctx = nullptr;
|
||||
|
||||
for (size_t i = 0; i < bar->count; ++i) {
|
||||
auto bar_output = &bar->bars[i];
|
||||
for (size_t j = 0; j < ZDWM_BAR_SIDE_COUNT; ++j) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <zdwm/bar.h>
|
||||
#include <zdwm/types.h>
|
||||
|
||||
#include "bar/text.h"
|
||||
#include "bar/types.h"
|
||||
#include "base/color.h"
|
||||
|
||||
@@ -27,6 +28,7 @@ typedef struct bar_t {
|
||||
|
||||
zdwm_bar_config_t config;
|
||||
bar_palette_t palette;
|
||||
text_context_t *ctx;
|
||||
} bar_t;
|
||||
|
||||
void bar_init(bar_t *bar);
|
||||
|
||||
69
src/bar/text.c
Normal file
69
src/bar/text.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "bar/text.h"
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <pango/pango-attributes.h>
|
||||
#include <pango/pango-context.h>
|
||||
#include <pango/pango-font.h>
|
||||
#include <pango/pango-layout.h>
|
||||
#include <pango/pango-types.h>
|
||||
#include <pango/pangocairo.h>
|
||||
|
||||
#include "base/memory.h"
|
||||
|
||||
struct text_context_t {
|
||||
PangoContext *context;
|
||||
PangoLayout *layout;
|
||||
PangoAttrList *attr_list;
|
||||
};
|
||||
|
||||
text_context_t *
|
||||
text_context_create(const char *family, uint32_t size, uint32_t dpi) {
|
||||
auto fontmap = pango_cairo_font_map_new();
|
||||
auto context = pango_font_map_create_context(fontmap);
|
||||
pango_cairo_context_set_resolution(context, (double)dpi);
|
||||
|
||||
auto desc = pango_font_description_from_string(family);
|
||||
pango_font_description_set_size(desc, size * PANGO_SCALE);
|
||||
auto attr = pango_attr_font_desc_new(desc);
|
||||
|
||||
auto attr_list = pango_attr_list_new();
|
||||
pango_attr_list_insert(attr_list, attr);
|
||||
|
||||
auto layout = pango_layout_new(context);
|
||||
pango_layout_set_attributes(layout, attr_list);
|
||||
pango_layout_set_wrap(layout, PANGO_WRAP_NONE);
|
||||
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
|
||||
|
||||
pango_font_description_free(desc);
|
||||
g_object_unref(fontmap);
|
||||
|
||||
auto ctx = p_new(text_context_t, 1);
|
||||
*ctx = (text_context_t){
|
||||
.context = context,
|
||||
.layout = layout,
|
||||
.attr_list = attr_list,
|
||||
};
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void text_context_destory(text_context_t *context) {
|
||||
if (!context) return;
|
||||
|
||||
if (context->layout) {
|
||||
g_object_unref(context->layout);
|
||||
context->layout = nullptr;
|
||||
}
|
||||
|
||||
if (context->attr_list) {
|
||||
pango_attr_list_unref(context->attr_list);
|
||||
context->attr_list = nullptr;
|
||||
}
|
||||
|
||||
if (context->context) {
|
||||
g_object_unref(context->context);
|
||||
context->context = nullptr;
|
||||
}
|
||||
|
||||
p_delete(&context);
|
||||
}
|
||||
9
src/bar/text.h
Normal file
9
src/bar/text.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct text_context_t text_context_t;
|
||||
|
||||
text_context_t *
|
||||
text_context_create(const char *family, uint32_t size, uint32_t dpi);
|
||||
void text_context_destory(text_context_t *context);
|
||||
Reference in New Issue
Block a user