Skip to content

[rtext] Sometimes fails to pack characters into glyph atlas #5141

@OetkenPurveyorOfCode

Description

@OetkenPurveyorOfCode

I was trying to draw text at the native resolution by reloading the font when the font size changes, instead of using blurry scaling of the font.

#include "raylib.h"
#include <stdio.h>

void DrawFiraFont(char* msg, Vector2 pos, int size, int spacing, Color color) {
    static _Thread_local int old_font_size;
    static _Thread_local Font font;
    if (size != old_font_size) {
        int count;
        int* memory = LoadCodepoints(msg, &count);
        for (int i = 0; i < count; i++) {
            printf("%c, ", memory[i]);
        }
        char* f1 = "c:/Users/alexa/Downloads/fonts/CascadiaMono/CaskaydiaMonoNerdFont-BoldItalic.ttf";
        char* f2 = "c:/Users/alexa/Downloads/fonts/Fira_Mono/FiraMono-Bold.ttf";
        font = LoadFontEx(f2, size, memory, count);
        UnloadCodepoints(memory);
        old_font_size = size;
    }
    DrawTextEx(font, msg, pos, size, spacing, WHITE);
}

int main(void)
{
    unsigned int n = 8;
    InitWindow(800, 600, "Hello");
    SetWindowState(FLAG_WINDOW_RESIZABLE);
    SetTargetFPS(60);
    int scale = 100;
    int font_size = 100;
    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(BLACK);
        if (IsKeyDown(KEY_DOWN)) font_size--;
        if (IsKeyDown(KEY_UP)) font_size++;
        DrawFiraFont("Hello, World", (Vector2){100, 100}, font_size, 0, WHITE);
        DrawTextEx((Font){0}, "Hello, World", (Vector2){00, 00}, font_size, 10, WHITE);
        EndDrawing();
    }
    CloseWindow();
    return 0;
}

But when I used the arrow keys, sporadically I got warnings like these and the correspondings glyphs did not show up on screen.

WARNING: FONT: Failed to package character (9)
WARNING: FONT: Failed to package character (10)
WARNING: FONT: Failed to package character (11)

This happened only for certain font sizes, but I tested it with two different ttf files so I don't think it is related to the specific font file I was using.

Questions:

-> What is the proper way to draw a custom TTF font at a specific font size without using blurry scaling?
NOTE: I know there is an API to generate and the font atlas manually which would allow me to avoid reloading and reparsing the font file, also selecting the packing method 1 (stb_rectpack) gives no packing error.
-> Am I using the LoadFont API correctly? I am passing some codepoints multiple times. Are duplicates allowed?
NOTE: Even if the string contains no duplicate characters the error still shows up.

Expected behaviour:

-> Succesful packing and display of all the codepoints regardless of my specified font size.

Environment

Windows 11, OpenGL 4.5,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions