Custom bitmap fonts in blender.

Some of you might find this interesting… example of horrid commodore/amiga fonts.

http://download.blender.org/ftp/incoming/bitmap.png

Heres a script that can convert BDF bitmap fonts into C++ data files blender uses to draw bitmap fonts.
These files can replace… just be sure to set the -name option to one matching blenders.
intern/bmfont/intern/BMF_font_helv12.cpp

eg python bdf2bmf.py -name=helv12 myfile.bdf

BDF fonts are easy to find, this is also interesting to have a simple BDF font reader in 30 lines.

At the moment theres a problem with fonts over 8px wide because blender stores each row as a char, easy to change.

BTW, People still like bitmap fonts? - would be nicer just to be able to use the OS’s bitmap fonts, probably not that hard to add. Glut alredy does this.

bdf2bmf.py


#!/usr/bin/python
HELP_TXT = 'usage "python bdf2bmf.py -name=SomeName myfile.bdf"'

import sys
def parse_bdf(f, MAX_CHARS=256):
    lines = [l.strip().upper().split() for l in f.readlines()]

    is_bitmap = False
    dummy = {'BITMAP':[]}
    char_data = [dummy.copy() for i in xrange(MAX_CHARS)]
    context_bitmap = []

    for l in lines:
        if l[0]=='ENCODING':        enc = int(l[1])
        elif l[0]=='BBX':            bbx = [int(c) for c in l[1:]]
        elif l[0]=='DWIDTH':        dwidth = int(l[1])
        elif l[0]=='BITMAP':        is_bitmap = True
        elif l[0]=='ENDCHAR':
            if enc < MAX_CHARS:
                char_data[enc]['BBX'] = bbx
                char_data[enc]['DWIDTH'] = dwidth
                char_data[enc]['BITMAP'] = context_bitmap
                
            context_bitmap = []
            enc = bbx = None
            is_bitmap = False
        else:
            # None of the above, Ok, were reading a bitmap
            if is_bitmap and enc < MAX_CHARS:
                context_bitmap.append( int(l[0], 16) )
    
    return char_data

# -------- end simple BDF parser



def convert_to_blender(bdf_dict, font_name, origfilename, MAX_CHARS=256):
    
    # first get a global width/height, also set the offsets
    xmin = ymin =  10000000
    xmax = ymax = -10000000
    
    bitmap_offsets = [-1] * MAX_CHARS
    bitmap_tot = 0
    for i, c in enumerate(bdf_dict):
        if c.has_key('BBX'):
            bbx = c['BBX']
            xmax = max(bbx[0], xmax)
            ymax = max(bbx[1], ymax)
            xmin = min(bbx[2], xmin)
            ymin = min(bbx[3], ymin)
            
            bitmap_offsets[i] = bitmap_tot
            bitmap_tot += len(c['BITMAP'])
        
        c['BITMAP'].reverse()
    
    # Now we can write. Ok if we have no .'s in the path.
    f = open(origfilename.split('.')[0] + '.cpp', 'w')
    
    f.write('''
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "BMF_FontData.h"

#include "BMF_Settings.h"
''')
    
    f.write('#if BMF_INCLUDE_%s

' % font_name.upper())
    f.write('static unsigned char bitmap_data[]= {')
    newline = 8
    
    for i, c in enumerate(bdf_dict):
    
        for cdata in c['BITMAP']:
            # Just formatting
            newline+=1
            if newline >= 8:
                newline = 0
                f.write('
	')
            # End formatting
            
            f.write('0x%.2hx,' % cdata) # 0x80 <- format
            
    f.write("
};
")
    
    f.write("BMF_FontData BMF_font_%s = {
" % font_name)
    f.write('	%d, %d,
' % (xmin, ymin))
    f.write('	%d, %d,
' % (xmax, ymax))
    
    f.write('	{
')
    

    for i, c in enumerate(bdf_dict):
        if bitmap_offsets[i] == -1 or c.has_key('BBX') == False:
            f.write('		{0,0,0,0,0, -1},
')
        else:
            bbx = c['BBX']
            f.write('		{%d,%d,%d,%d,%d, %d},
' % (bbx[0], bbx[1], -bbx[2], -bbx[3], c['DWIDTH'], bitmap_offsets[i]))
    
    f.write('''
    },
    bitmap_data
};

#endif
''')

def main():
    # replace "[-name=foo]" with  "[-name] [foo]"
    args = []
    for arg in sys.argv:
        for a in arg.replace('=', ' ').split():
            args.append(a)
    
    name = 'untitled'
    done_anything = False
    for i, arg in enumerate(args):
        if arg == '-name':
            if i==len(args)-1:
                print 'no arg given for -name, aborting'
                return
            else:
                name = args[i+1]
        
        elif arg.lower().endswith('.bdf'):
            try:
                f = open(arg)
                print '...Writing to:', arg
            except:
                print 'could not open "%s", aborting' % arg
            
            
            bdf_dict = parse_bdf(f)
            convert_to_blender(bdf_dict, name, arg)
            done_anything = True
    
    if not done_anything:
        print HELP_TXT
        print '...nothing to do'

if __name__ == '__main__':
    main()

Awesome! What a blast seeing the old Amiga fonts! lol

:smiley:

wordless!

Thank you Campbell !!!

I’m creating bdf fonts with this software:
http://www.math.nmsu.edu/~mleisher/Software/otf2bdf/