SFont / contrib /

Full commit
#!/usr/bin/env python
# -*- coding: ascii -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab

"""Generate a default font from a BMP file (and can only be BMP)
for use with SFont and SDL. See

It sort of defeats the purpose of SFont in that it creates code
without a file system dependency but this is useful for a default

Basically an automated version of bin2h on a BMP and code generator.

Possible ideas, use PIL to load any file and then convert to BMP.
BMP is preferred as SDL supportds BMP out of the box, other image
formats require SDL_image

Recommended usage:

    #include "default_data.h"
    SDL_Surface *font_bitmap_surface=NULL;
    SFont_Font* Font;
    font_bitmap_surface = SDL_LoadBMP("24P_Copperplate_Blue.bmp");
        fprintf(stderr, "Using default");
        font_bitmap_surface = get_default_data_font();
        fprintf(stderr, "An error occured while loading the font.");
    Font = SFont_InitFont(font_bitmap_surface);
        fprintf(stderr, "An error occured while setting up font.");


import sys
import os

c_header_template = """/*
** Generated with for use with
** SFont
#ifndef __%(hdr_name)s_h__
#define __%(hdr_name)s_h__

#ifdef __cplusplus
extern "C" {

#include "SDL.h"
#include "SDL_video.h"


#define get_%(hdr_name)s_font() SDL_LoadBMP_RW(SDL_RWFromMem(%(hdr_name)s, sizeof(%(hdr_name)s)), 1);
%(type_name)s   %(hdr_name)s[%(byte_len)d] = {

#ifdef __cplusplus



def bmp2header(bmp_filename, name=None, use_c99_types=False):
    name = name or 'default_data'
    f = open(bmp_filename)
    file_bytes =
    byte_len = len(file_bytes)

    file_bytes_str = ['0x%02x'%ord(x) for x in file_bytes]  # list comprehension python 2.4 feature

    hdr_name = name
    if use_c99_types:
        hdr_include = '#include <stdint.h>'
        type_name = 'uint8_t'
        hdr_include = ''
        type_name = 'unsigned char'
    hex_byte_str_list = ', '.join(file_bytes_str)  ## TODO add newlines
    print c_header_template % locals()

def main(argv=None):
    if argv is None:
        argv = sys.argv
    bmp_filename = '2bit_8pxfont.bmp'
        bmp_filename = argv[1]
    except IndexError:
        print ''
        print 'Error missing file name'
        print ''
        print 'Usage: file_name.bmp'
        print ''
        return 1
    name, extn = os.path.splitext(os.path.basename(bmp_filename))
    if extn.lower() != '.bmp':
        print 'WARNING! file name does not contain BMP.'
    name = '_'+name
    bmp2header(bmp_filename, name)
    return 0

if __name__ == "__main__":