akrinke / Font-Stash

A dynamic font glyph cache for OpenGL.
114 stars 18 forks source link

Bitmap support #9

Closed raptor closed 9 years ago

raptor commented 10 years ago

Hi,

I'm looking to use bitmap support in fontstash, however the example in the main.c file looks quite primitive.

I used the AngelCode creator to generate the .fnt file with associated .tga. Can fontstash load that .fnt file and set up the bitmap font automatically without having to write my own surface code and adding each glyph individually? If so, is there an example somewhere?

Thanks!

akrinke commented 10 years ago

Main.c contains only a very simple example using SDL_image to load the bitmap and manual definitions of the letters that are used. You have to add your own code for loading and parsing of fnt files. Alternatively, you could simply convert the fnt file to a list of calls to sth_add_glyph and include this list in your program.

The image loading using SDL_image is just an example. There are countless ways to load a texture, so I think this should not be part of a library like Font-Stash.

raptor commented 10 years ago

Hi,

I suppose I was looking for an easy way to load a .fnt much like a .ttf does with the integration with stb_truetype. Doesn't it take care of the texture loading already?

Also, what is the relationship with the bitmap texture I have to manually load to the font stash? does the glyph get copied into fontstash's own texture cache or is it just pulled from the one I create?

Thanks!

akrinke commented 10 years ago

Hello,

stb_truetype loads truetype fonts and indeed, it creates appropriate textures. The point is, these textures are created directly.

The problem with bitmap fonts is, that the image file could be of any format. Therefore, we need some kind of library to perform the texture creation (or we have to greatly reduce the supported file types). Because all applications using Font-Stash almost definitely already use some kind of image loading library, it would not make any sense to add another one to its dependencies. For demonstration purposes, main.c uses SDL_image for image loading, but this is just an example.

Font-Stash is a library, and therefore, the current choice is to require the host application to load the image file using some library and give a OpenGL texture ID to Font-Stash. Although, we could change this to a callback based approach where the user provides a function which gets a path to an image file and returns a texture ID.

If the problem of image loading is solved (or you accept the current solution of providing the image loading mechanism yourself :) ) there is no problem in adding support for bitmap fonts which are defined by fnt files.

raptor commented 10 years ago

Ah OK. I did not realize that bitmap fonts can be any format, but this makes sense. I will continue to research if there is an easy-to-use .fnt parser somewhere.