1ea57adf5b1423fe53cae4d29fc14618119e2e14
[hackover2013-badge-firmware.git] / badge / ui / font.c
1 #include "font.h"
2 #include "sprite.h"
3
4 #include <drivers/fatfs/ff.h>
5
6 static uint8_t badge_framebuffer_render_char_with_fd(badge_framebuffer *fb, int8_t pos_x, int8_t pos_y, char c, FIL *fd) {
7 UINT readbytes;
8 uint8_t buffer[BADGE_FONT_WIDTH];
9
10 if(c == 32) {
11 // intentionally left empty.
12 } else if(c >= 32 &&
13 FR_OK == f_lseek(fd, (c - 33) * BADGE_FONT_WIDTH) &&
14 FR_OK == f_read(fd, buffer, sizeof(buffer), &readbytes)) {
15 badge_sprite sp = { BADGE_FONT_WIDTH, BADGE_FONT_HEIGHT, buffer };
16 badge_framebuffer_blt(fb, pos_x, pos_y, &sp, 0);
17 } else {
18 return -1;
19 }
20
21 return 0;
22 }
23
24 uint8_t badge_framebuffer_render_char(badge_framebuffer *fb, int8_t pos_x, int8_t pos_y, char c) {
25 FIL fd;
26
27 if(FR_OK == f_open(&fd, "font.dat", FA_OPEN_EXISTING | FA_READ)) {
28 badge_framebuffer_render_char_with_fd(fb, pos_x, pos_y, c, &fd);
29 f_close(&fd);
30 return 0;
31 }
32
33 return -1;
34 }
35
36 uint8_t badge_framebuffer_render_text(badge_framebuffer *fb, int8_t pos_x, int8_t pos_y, char const *text) {
37 uint8_t count = 0;
38 FIL fd;
39
40 if(FR_OK == f_open(&fd, "font.dat", FA_OPEN_EXISTING | FA_READ)) {
41 while(*text) {
42 if(0 != badge_framebuffer_render_char_with_fd(fb, pos_x, pos_y, *text, &fd)) {
43 break;
44 }
45
46 ++count;
47 ++text;
48 pos_x += BADGE_FONT_WIDTH;
49 }
50
51 f_close(&fd);
52 }
53
54 return count;
55 }
This page took 0.057978 seconds and 3 git commands to generate.