raysan5 / raylib

A simple and easy-to-use library to enjoy videogames programming

Home Page:http://www.raylib.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[rtext] Issues about font loading

wangxun2008 opened this issue · comments

Issue description

When using the latest versions of raylib (including 5.0 and 5.1-dev), issues have been encountered when importing fonts.
The problem arises when importing short string lengths at certain font sizes causing the last character of the string to potentially fail to import and leading to possible crashes.
Current solutions include adding extra characters to the string and adjusting the font size.

Issue Screenshot

image

Code Example

#include <raylib.h>
#include <bits/stdc++.h>
using namespace std;

int main() {
	
	InitWindow(800,600,"");
	int fileSize;
	unsigned char *fontFileData = LoadFileData("c:\\windows\\fonts\\simhei.ttf", &fileSize);
	int codepointsCount;
	int *codepoints=LoadCodepoints("head",&codepointsCount);
	Font font = LoadFontFromMemory(".ttf",fontFileData,fileSize,64,codepoints,codepointsCount);
	UnloadCodepoints(codepoints);
	SetTargetFPS(120);
	while (!WindowShouldClose()) {
		BeginDrawing();
		ClearBackground(WHITE);
		DrawTextEx(font,"head",(Vector2){50,50},64,5,RED);
		EndDrawing();
	}
	UnloadFont(font);
	UnloadFileData(fontFileData);
	return 0;
}
commented

@wangxun2008 did you try printing the generated font.texture atlas to check if required characters are exported successfully?

@wangxun2008 did you try printing the generated atlas to check if required characters are exported successfully?font.texture

I have tried the official example ("raylib [text] example - Codepoints loading"). The final character doesn't seem to have been successfully exported.
image

image

This is another official example. issues arise when shortening the string length and adjusting the font size to 64.

commented

@wangxun2008 Thanks for sharing, the issue is actually on the algorithm that calculates the font texture atlas size required to fit all the glyphs. You can just uncomment this line, recompile raylib and it should work.