创建每个屏幕对应的信息条窗口

This commit is contained in:
2025-08-15 19:28:40 +08:00
parent be94f90c94
commit 8472fda46a
12 changed files with 414 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
set(LIB_RENDERER "zswm-renderer")
set(LIB_RENDERER_NAME ${LIB_RENDERER} PARENT_SCOPE)
add_library(${LIB_RENDERER} STATIC image.c)
add_library(${LIB_RENDERER} STATIC image.c text.c)
target_include_directories(${LIB_RENDERER} SYSTEM
PRIVATE ${RENDERER_INCLUDE_DIRS}
)

51
src/renderer/text.c Normal file
View File

@@ -0,0 +1,51 @@
#include <glib-object.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>
#include "renderer.h"
static PangoContext *context = NULL;
uint32_t get_font_height(const char *font_family, const uint32_t font_size,
const uint32_t dpi) {
clean_pango_context();
PangoFontMap *fontmap = pango_cairo_font_map_new();
context = pango_font_map_create_context(fontmap);
pango_cairo_context_set_resolution(context, dpi);
PangoLanguage *lang = pango_context_get_language(context);
PangoAttrList *attr_list = pango_attr_list_new();
PangoFontDescription *desc = pango_font_description_from_string(font_family);
pango_font_description_set_size(desc, font_size * PANGO_SCALE);
PangoAttribute *attr = pango_attr_font_desc_new(desc);
pango_attr_list_insert(attr_list, attr);
pango_attr_list_unref(attr_list);
PangoFontMetrics *metrics = pango_context_get_metrics(context, desc, lang);
int ascent = pango_font_metrics_get_ascent(metrics);
int descent = pango_font_metrics_get_descent(metrics);
uint32_t font_height = PANGO_PIXELS(ascent + descent);
pango_font_metrics_unref(metrics);
pango_font_description_free(desc);
g_object_unref(fontmap);
return font_height;
}
void clean_pango_context(void) {
if (!context) return;
g_object_unref(context);
context = NULL;
}