freetype与xft和xlib,改变字体大小不起作用

问题描述 投票:0回答:1

下面的代码使用 xlib 创建一个窗口,使用 freetype 在屏幕上使用字体并使用 xft 配置它,问题是代码不会更改字体大小,它始终保持静态相同的大小。我已经尝试了一切,甚至 chatgpt 也没有帮助我。

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/Xft/Xft.h>  // Adiciona o cabeçalho Xft
#include <chrono>

#define FONT_PATH "fixed"

void drawText(Display *display, Window window, const char *text, int x, int y, int fontSize) {
    XftFont *font;
    XftDraw *xftDraw;
    XftColor color;

    // Cria uma fonte Xft com o tamanho desejado
    font = XftFontOpenName(display, DefaultScreen(display), FONT_PATH);
    font->height = fontSize;  // Ajusta o tamanho da fonte

    // Cria um desenho Xft
    xftDraw = XftDrawCreate(display, window, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)));

    // Configura a cor
    XRenderColor xrenderColor;
    xrenderColor.red = 0xFFFF;
    xrenderColor.green = 0xFFFF;
    xrenderColor.blue = 0xFFFF;
    xrenderColor.alpha = 0xFFFF;
    XftColorAllocValue(display, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)), &xrenderColor, &color);

    // Desenha o texto
    XftDrawStringUtf8(xftDraw, &color, font, x, y + font->ascent, (XftChar8 *)text, strlen(text));

    // Libera recursos
    XftDrawDestroy(xftDraw);
    XftColorFree(display, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)), &color);
    XftFontClose(display, font);
}

int main() {
    Display *display = XOpenDisplay(nullptr);

    if (!display) {
        fprintf(stderr, "Erro ao abrir a exibição\n");
        return 1;
    }

    int windowWidth = 207;
    int windowHeight = 183;

    int screen = DefaultScreen(display);
    int screenWidth = DisplayWidth(display, screen);
    int screenHeight = DisplayHeight(display, screen);

    int windowX = (screenWidth - windowWidth) / 2;
    int windowY = (screenHeight - windowHeight) / 2;

    Window overlayWindow = XCreateSimpleWindow(display, DefaultRootWindow(display),
                                               windowX, windowY, windowWidth, windowHeight,
                                               0, 0, 0);

    XChangeProperty(display, overlayWindow, XInternAtom(display, "_NET_WM_STATE", False),
                    XA_ATOM, 32, PropModeReplace, (unsigned char *)&overlayWindow, 1);

    Atom atom_fullscreen = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
    XChangeProperty(display, overlayWindow, XInternAtom(display, "_NET_WM_STATE", False),
                    XA_ATOM, 32, PropModeReplace, (unsigned char *)&atom_fullscreen, 1);

    XSetWindowAttributes attr;
    attr.override_redirect = True;
    XChangeWindowAttributes(display, overlayWindow, CWOverrideRedirect, &attr);

    XCompositeRedirectWindow(display, overlayWindow, CompositeRedirectAutomatic);

    XMapWindow(display, overlayWindow);

    // Loop principal para manter a janela aberta
    while (1) {
        // Desenha o texto na janela
        drawText(display, overlayWindow, "Texto na Janela", 30, 30, 64);

    }

    XDestroyWindow(display, overlayWindow);
    XCloseDisplay(display);

    return 0;
}

我正在尝试更改字体和文本大小

c linux xlib freetype
1个回答
0
投票

您可以在加载字体时指定字体大小:

char name[BUFSIZ];
sprintf(name, "%s:pixelsize=%u", fontname, fontsize);
XftFont *fp=XftFontOpenName(display, screen, name);

如果想再次更改字体大小,可以先关闭字体。然后以指定的字体大小再次打开。

© www.soinside.com 2019 - 2024. All rights reserved.