freetype with xft and xlib, changing the font size does not work

90 Views Asked by At

The code below creates a window with xlib, uses a font on the screen with freetype and configures it with xft, the problem is that the code does not change the font size, it always stays static at the same size. I've tried everything, not even chatgpt helped me with this.

#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;
}

I'm trying to get the font and text size to change

0

There are 0 best solutions below