rougier / freetype-gl

OpenGL text using one vertex buffer, one texture and FreeType
Other
1.65k stars 266 forks source link

Error returns not adequate for a library #165

Open forthy42 opened 7 years ago

forthy42 commented 7 years ago

Best example is texture_font_load_glyph, which returns either success or failure, but there are four different reasons for failure. Especially the failure "Texture atlas is full" is one where you could just enlarge the atlas, and try again.

Unfortunately, the API is even that 0 return is failure, and !=0 return (1) is success. Returning 0 on success and an error code on failure would be much better. In any case, there should be a number of error codes and a char* freetype_errstr(int errno) function to display errors when and in the form the application wants to.

I'm using freetype-gl on Android, where I need to patch the fprintf(stderr,.. to LOGE(, because that's how Android libraries are supposed to print their error messages. Don't print error messages in a library.

The other issue is already discussed here, it's the FreeType object created and freed for every single glyph. What I suggest would be to add a library element to the texture_font_t, open it up when needed, and keep it open until the user of the library tells to close it.

I would volunteer to implement those changes.

rougier commented 7 years ago

Your proposal makes sense. If you're willing to make a PR that's even better. My only advice would be to (try to) keep the change small if possible.

forthy42 commented 7 years ago

The errno-style pull request is now ready to merge. There's full compatibility with existing use, except that the printout of error messages changed slightly, and that fatal "can't allocates" don't lead to exit() - libraries shouldn't exit.

I've already made some measurements and experiments with the FreeType objects creation/deletion, atlas resizing and stuff, and there's clearly some more work to do.

Resizing an atlas fixes only one font's glyph, while you can have multiple fonts bound to the same atlas/texture (which is a good idea, as you can render everything with a single draw call). So an atlas object needs a list of all the associated fonts, and walk through them to scale the glyph positions. Either that way, or store the glyph positions as pixels, and convert them into the actual dimensions on the fly; which I would prefer (no recalculation necessary if the atlas changes size).

What I want to have is one FreeType library object for all differently scaled (and otherwise parametrized) fonts, so that loading is only necessary once for them. The loading should happen when needed, and the end user application should unload faces and libraries if it thinks there is no immediate further need for them.