Sign in to follow this  
too_many_stars

Bitmap Font Class Help

Recommended Posts

Hi Guys,

 

I am looking for some general help.

 

Here's the scenario.

 

We are given a bitmap font image file with equally spaced characters (ie the user knows the width and height of each cell) but in some random order with a uniform background color (which does not match any color of an image, font in this case).

 

We are able to iterate through a double for loop and access each cell, and get the pixel data.

 

Is there a way, to parse a cell, say for the letter "z" and store the ASCII integer value?

 

I am thinking this would be very difficult to pull of given the conditions above (ie random ordering with only pixel data) but I am just thinking what other more experienced programers do in these cases.

 

Of course, an easier solution is just to use a predictable ordering such as 16 x16 ASCII order.

 

Thanks,

 

Mike

Share this post


Link to post
Share on other sites

There are several solutions I can think of:

 

1.) An encoding vector, separate from the bitmap, maps code points to pixel co-ordinates. This is a widely used method. It allows for unequally spaced packing as well. Using it as a map allows further for efficient searching.

 

2.) Use a gap of 1 pixel size, e.g. in horizontal direction, so that your grid is no longer W x H but W x (H+1) pixels. Then write the code point like a pixel value just above the respective clip into the bitmap itself.

 

3.) Combine 1.) and 2.) by writing an encoding vector as pixel row at the top of the bitmap.

 

4.) Since type recognition systems exist, it is indeed possible to investigate the pixel data itself for feature vectors, so that characters are recognized automatically. However that is a) difficult, b) error prone, and c) not worth the effort in this case.

 

5.) You can go with a fixed order, of course, but that would restrict you to use a small subset of code points. Well, you mentioned ASCII (which consists of only 128 characters, by the way; anything above is already an extension). Although that is possible, of course, I want to hint at the reasons to introduce Unicode.

Share this post


Link to post
Share on other sites

You could grab some character recognition code form the web and build it into your code, but why do you want to?

 

Is there some special reason that means you cannot control the format of the fonts?

 

To be honest, since the appearance of signed distance field font rendering, I tend to just use fonts generated with tools like BMFont from AngleCode.

 

http://www.angelcode.com/products/bmfont/

 

Maybe an easier solution to your problem would be to specify a font generation tool and then you know the format.

 

If you give us some more details about what you need to do, maybe we can help some more.

Share this post


Link to post
Share on other sites

I think storing the required information to find the correct position in the grid for a given character is the only sensible solution. Your font image must have been created by some tool anyway, it could just as easily print a list of the character's positions in the bitmap. It could be as simple as just an index (which you can transform into x and y coordinates by means of integer division and module), or as elaborate as storing the pixel position, size, vertical offset from the baseline and the distance you have to advance your pen position after drawing this character on screen. This is what tools like hiero and bmfont do. The resulting font texture atlases look a bit strange when viewed, but they can actually save a lot of space (especially precious video memory) but cramming more characters into a smaller texture.

 

That's also what Löve2D (and probably lots of other game engines/frameworks) do, even though it allows generating the bitmap on the fly from a TrueType font.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this