Skip to content

[rtext] Add Support for Disabling Anti-Aliasing on TTF Loaded Fonts #5385

@XenoMustache

Description

@XenoMustache

Issue description

Currently in Raylib there seems to be no way to disable anti-aliasing on TTF loaded fonts. This can pose an issue with non-uniform scaling pixelated fonts such as Monogram. As far as I can tell the mechanism for loading TTF fonts does not provide a mechanism to disable this functionality, however it can be very much disabled manually by implementing your own font loading mechanism. Here is an example of such:

Click to view code example
Font LoadFontFromMemoryBitmap(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount) {
	Font font = { 0 };
	char fileExtLower[16] = { 0 };
	strncpy(fileExtLower, TextToLower(fileType), 16 - 1);

	font.baseSize = fontSize;
	font.glyphPadding = 0;
	font.glyphs = LoadFontData(fileData, dataSize, font.baseSize, codepoints, (codepointCount > 0)? codepointCount : 95, FONT_BITMAP, &font.glyphCount);

	if (font.glyphs != NULL) {
		font.glyphPadding = 4;

		Image atlas = GenImageFontAtlas(font.glyphs, &font.recs, font.glyphCount, font.baseSize, font.glyphPadding, 0);
		font.texture = LoadTextureFromImage(atlas);

		for (int i = 0; i < font.glyphCount; i++) {
			UnloadImage(font.glyphs[i].image);
			font.glyphs[i].image = ImageFromImage(atlas, font.recs[i]);
		}

		UnloadImage(atlas);
	}
	else font = GetFontDefault();
	return font;
}

Font LoadFontTTF(const char *fileName, int fontSize, const int *codepoints, int codepointCount) {
	Font font = { 0 };
	int dataSize = 0;
	unsigned char *fileData = LoadFileData(fileName, &dataSize);

	if (fileData != NULL) {
		font = LoadFontFromMemoryBitmap(GetFileExtension(fileName), fileData, dataSize, fontSize, codepoints, codepointCount);
		UnloadFileData(fileData);
	}

	return font;
}

This is a modification of the rtext module's font loading mechanism (without all the frills and safety checks). My proposal is to introduce some sort of way to specify using FONT_BITMAP or FONT_DEFAULT in the LoadFontData function when loading TTF files. This can be as simple or as high level as adding an additional bool to LoadFontEx or some sort of way to configure it within the Font struct itself. You could also add a function like DisableAntiAliasing or a macro to disable to functionality altogether for all TTF loaded fonts, however that seems like a rather rigid approach.

Issue Screenshot

Here are a couple of screenshots of the code in action. The left image demonstrates loading Monogram with LoadFontEx, while the right demonstrates loading the font with the above LoadFontTTF.

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