font.c
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* 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;
}