font.c 1.4 KB
/*
 * Font library that takes a font, string and translates it into a sprite
 */
#include <PR/mbi.h>
#include <PR/sp.h>
#include "font.h"

static char *__strchr(char *str, char target)
{
    while(*str && (*str != target)) {
	str++;
    }

    if(*str) {
	return (str);
    }
    
    return NULL;
}

/*
 * The public API for generating sprites from strings using a font.
 * 
 * XXX Return codes: error for not enough space for bms, etc
 */
int drawString(char *str, Font *fnt, int width, int length, Sprite *sp)
{
    int i, ci;
    int x;
    int y;
    char *indx;
    Bitmap *bm;

    /*
     * Actual width and height translated from characters to pixels
     */
    sp->width = width * fnt->width;
    sp->height = length * fnt->height;

    bm = sp->bitmap;

    i = 0;
    ci = 0;
    for (y=0; y<length; y++) {
	for (x=0; x<width; x++, i++, ci++) {
            if (i >= sp->nbitmaps) {
                return -1;
            }
	    if(str[ci] == '\0') {
		bm[i] = fnt->bitmaps[0];
		bm[i].width = -1;
		sp->nbitmaps = i;
		return 0;
	    };
	    if(str[ci] == '\n') {
		bm[i] = fnt->bitmaps[0];
		bm[i].buf = NULL;
		ci--;
		continue;
	    };

	    if((indx = __strchr(fnt->index_string, str[ci])) != NULL) {
		bm[i] = fnt->bitmaps[indx-fnt->index_string];
	    } else {
		bm[i] = fnt->bitmaps[0];
		bm[i].buf = NULL;
	    };
	};

	if(str[ci] == '\n') {
	    ci++;
        }
    };

    sp->nbitmaps = i;

    return 0;
}