计算 bar 高度
This commit is contained in:
@@ -19,7 +19,15 @@ project(${APP_NAME}
|
|||||||
LANGUAGES C
|
LANGUAGES C
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${APP_NAME} wm.c utils.c buffer.c backtrace.c monitor.c client.c)
|
add_executable(${APP_NAME}
|
||||||
|
wm.c
|
||||||
|
utils.c
|
||||||
|
buffer.c
|
||||||
|
backtrace.c
|
||||||
|
monitor.c
|
||||||
|
client.c
|
||||||
|
text.c
|
||||||
|
)
|
||||||
|
|
||||||
# use C23
|
# use C23
|
||||||
set_property(TARGET ${APP_NAME} PROPERTY C_STANDARD 23)
|
set_property(TARGET ${APP_NAME} PROPERTY C_STANDARD 23)
|
||||||
@@ -34,6 +42,9 @@ pkg_check_modules(deps REQUIRED
|
|||||||
xcb-randr
|
xcb-randr
|
||||||
xcb-xfixes
|
xcb-xfixes
|
||||||
xcb-xinerama
|
xcb-xinerama
|
||||||
|
|
||||||
|
pango
|
||||||
|
pangocairo
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${APP_NAME} SYSTEM
|
target_include_directories(${APP_NAME} SYSTEM
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
static const char *const tags[] = {
|
static const char *const tags[] = {
|
||||||
"1", "2", "3", "4", "5", "6", "7", "8", "9", nullptr,
|
"1", "2", "3", "4", "5", "6", "7", "8", "9", nullptr,
|
||||||
};
|
};
|
||||||
|
static const uint32_t bar_y_padding = 1;
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
|
|
||||||
92
src/text.c
Normal file
92
src/text.c
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
#include "text.h"
|
||||||
|
|
||||||
|
#include <glib-object.h>
|
||||||
|
#include <glibconfig.h>
|
||||||
|
#include <pango/pango-attributes.h>
|
||||||
|
#include <pango/pango-context.h>
|
||||||
|
#include <pango/pango-font.h>
|
||||||
|
#include <pango/pango-fontmap.h>
|
||||||
|
#include <pango/pango-layout.h>
|
||||||
|
#include <pango/pango-types.h>
|
||||||
|
#include <pango/pangocairo.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static PangoContext *context = nullptr;
|
||||||
|
static PangoLayout *layout = nullptr;
|
||||||
|
static PangoAttrList *attr_list = nullptr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化绘制文字的 pango 环境
|
||||||
|
* @param family 多个字体用逗号分割
|
||||||
|
* @param size 字体大小
|
||||||
|
* @param dpi
|
||||||
|
* @return layout 高度,可用于确定 bar 高度
|
||||||
|
*/
|
||||||
|
int text_init_pango_layout(const char *family, uint32_t size, uint32_t dpi) {
|
||||||
|
static const char *layout_family = nullptr;
|
||||||
|
static uint32_t layout_size = 0;
|
||||||
|
static uint32_t layout_dpi = 0;
|
||||||
|
static int text_height = 0;
|
||||||
|
|
||||||
|
if (layout_family != nullptr && strcmp(layout_family, family) == 0 &&
|
||||||
|
layout_size == size && layout_dpi == dpi) {
|
||||||
|
printf("text pango layout config cached\n");
|
||||||
|
return text_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
text_clean_pango_layout();
|
||||||
|
|
||||||
|
layout_family = family;
|
||||||
|
layout_size = size;
|
||||||
|
layout_dpi = dpi;
|
||||||
|
|
||||||
|
PangoFontMap *fontmap = pango_cairo_font_map_new();
|
||||||
|
context = pango_font_map_create_context(fontmap);
|
||||||
|
pango_cairo_context_set_resolution(context, (double)dpi);
|
||||||
|
|
||||||
|
layout = pango_layout_new(context);
|
||||||
|
|
||||||
|
PangoFontDescription *desc = pango_font_description_from_string(family);
|
||||||
|
pango_font_description_set_size(desc, size * PANGO_SCALE);
|
||||||
|
PangoLanguage *lang = pango_context_get_language(context);
|
||||||
|
PangoFontMetrics *metrics = pango_context_get_metrics(context, desc, lang);
|
||||||
|
|
||||||
|
PangoAttribute *attr = pango_attr_font_desc_new(desc);
|
||||||
|
attr_list = pango_attr_list_new();
|
||||||
|
pango_attr_list_insert(attr_list, attr);
|
||||||
|
|
||||||
|
int height = pango_font_metrics_get_height(metrics);
|
||||||
|
int ascent = pango_font_metrics_get_ascent(metrics);
|
||||||
|
int descent = pango_font_metrics_get_descent(metrics);
|
||||||
|
text_height =
|
||||||
|
MAX(PANGO_PIXELS_CEIL(height), PANGO_PIXELS_CEIL(ascent + descent));
|
||||||
|
|
||||||
|
pango_font_metrics_unref(metrics);
|
||||||
|
pango_font_description_free(desc);
|
||||||
|
g_object_unref(fontmap);
|
||||||
|
|
||||||
|
PangoRectangle ink_rect, logical_rect;
|
||||||
|
pango_layout_get_pixel_extents(layout, &ink_rect, &logical_rect);
|
||||||
|
|
||||||
|
text_height = MAX(text_height, MAX(ink_rect.height, logical_rect.height));
|
||||||
|
|
||||||
|
return text_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void text_clean_pango_layout(void) {
|
||||||
|
if (attr_list) {
|
||||||
|
pango_attr_list_unref(attr_list);
|
||||||
|
attr_list = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (layout) {
|
||||||
|
g_object_unref(layout);
|
||||||
|
layout = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context) {
|
||||||
|
g_object_unref(context);
|
||||||
|
context = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/text.h
Normal file
6
src/text.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
int text_init_pango_layout(const char *family, uint32_t size, uint32_t dpi);
|
||||||
|
void text_clean_pango_layout(void);
|
||||||
6
src/wm.c
6
src/wm.c
@@ -20,6 +20,7 @@
|
|||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "default_config.h"
|
#include "default_config.h"
|
||||||
#include "monitor.h"
|
#include "monitor.h"
|
||||||
|
#include "text.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@@ -257,6 +258,11 @@ void wm_clean(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void wm_setup(void) {
|
static void wm_setup(void) {
|
||||||
|
int font_height = text_init_pango_layout("Terminus, Emacs Simsun", 10, 144);
|
||||||
|
wm.bar_height = (uint32_t)font_height + 2 * bar_y_padding;
|
||||||
|
|
||||||
|
printf("font height: %d, bar height: %u\n", font_height, wm.bar_height);
|
||||||
|
|
||||||
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
for (monitor_t *m = wm.monitor_list; m; m = m->next) {
|
||||||
monitor_initialize_tag(m, (const char **)tags);
|
monitor_initialize_tag(m, (const char **)tags);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user