Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtext] New pair of functions to automatically prefetch all possible codepoints of any font (except empty ones) #4632

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Michael-Ilyin
Copy link

@Michael-Ilyin Michael-Ilyin commented Dec 22, 2024

Hi @raysan5. Thanks for the cool set of libs! And this is my first PR ever, so I apologize if I'm forming something wrong.

I've tried all the appropriate functions to load fonts, but I still haven't found how to load a font with all available glyphs at once.

After analyzing the code, I realized that the LoadFontEx or LoadFontFromMemory functions, when specifying NULL, 0 for codepoints in the arguments, load only 95 glyphs (although according to the words from the cheatsheet

use NULL for codepoints and 0 for codepointCount to load the default character set

I thought default was a full set of all available characters). But it turns out that to load fully all available glyphs, I have to manually collect an array of codepoints, but this is obviously very time-consuming and inconvenient.

So I propose these two functions, which can be used to pre-count the number of available glyphs and automatically collect an array of codepoints. And then use them for LoadFontEx or LoadFontFromMemory functions. For example like this:

int *codepoints;
int codepointsCount = GetFontAvailableCodepoints("data/font.ttf", &codepoints);
fontDefault = LoadFontEx("data/font.ttf", (int)(fontSize*fontScale), codepoints, codepointsCount);
free(codepoints);

Of course, before writing these two functions, I tried changing the code of the LoadFontEx etc. functions (where I met places about the number of codepoints), so that when arguments NULL, 0 were specified, the font would load all available glyphs/codepoints at once, not just 95 pieces, but it turned out to be not so simple. Besides, it wasn't good decision, because some glyphs were not loaded correctly, so I decided not to touch the existing code, and probably it's better to leave 95 glyphs for those who don't need Unicode.

That's why it's better to first collect the necessary codepoints into an array and pass them when loading the font using additional functions, it's safer. And these two functions do it automatically and I didn't find any problems. At least the labels and text boxes in raygui display unicode glyphs well when typing text. I tried it on several fonts (only TTF though). I haven't done anything with the BDF type yet, because I just switched from 5.0 to 5.5 and discovered this new type, which honestly I've never heard of it before 🙂

Of course, we could just rasterize all 65536 possible glyphs of any font, but that's too memory expensive (if I understand correctly, empty glyphs will be rasterized as well). And these new functions allow to collect only the available glyphs in a font, not all 65536.

I don't know if you'll accept my PR, but I will definitely use these functions for myself, because fonts are different, and it's very inconvenient to choose codepoints for each random font. But stb_truetype still allows to pre-gather an array of codepoints before rasterization. Besides, I've seen enough requests for correct and automatic unicode text on the web. So it may be useful.

…points of font file and update the array of codepoints themselves

Added two new functions to get the number of all available codepoints of a font file (or font data in memory) and update the array of codepoints themselves, passed via an argument. By default, the LoadFontEx() or LoadFontFromMemory() functions do not have this feature and load only 95 code points when specifying "NULL, 0" in the arguments. But if the user wants to automatically collect all possible code points of a font and their number, he can use these functions. Having collected them with these functions, he will only have to pass them to the LoadFontEx() or LoadFontFromMemory() functions.
…er of all available codepoints of font file and update the array of codepoints themselves

Added implementations for two new functions to get the number of all available codepoints of a font file (or font data in memory) and update the array of codepoints themselves, passed via an argument. By default, the LoadFontEx() or LoadFontFromMemory() functions do not have this feature and load only 95 code points when specifying "NULL, 0" in the arguments. But if the user wants to automatically collect all possible code points of a font and their number, he can use these functions. Having collected them with these functions, he will only have to pass them to the LoadFontEx() or LoadFontFromMemory() functions.
Added constant MAX_GLYPHS for autoscanning all possible glyphs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant