update font loader to the new FS API

This commit is contained in:
Gabor Kiss-Vamosi
2020-09-02 10:25:46 +02:00
parent 7cf51010b0
commit 572974b5e0
3 changed files with 35 additions and 32 deletions
+9 -9
View File
@@ -93,11 +93,11 @@ lv_font_t * lv_font_load(const char * font_name)
lv_font_t * font = lv_mem_alloc(sizeof(lv_font_t)); lv_font_t * font = lv_mem_alloc(sizeof(lv_font_t));
memset(font, 0, sizeof(lv_font_t)); memset(font, 0, sizeof(lv_font_t));
lv_fs_file_t file; lv_fs_file_t * file;
lv_fs_res_t res = lv_fs_open(&file, font_name, LV_FS_MODE_RD); file = lv_fs_open(font_name, LV_FS_MODE_RD);
if(res == LV_FS_RES_OK) { if(file) {
success = lvgl_load_font(&file, font); success = lvgl_load_font(file, font);
} }
if(!success) { if(!success) {
@@ -111,7 +111,7 @@ lv_font_t * lv_font_load(const char * font_name)
font = NULL; font = NULL;
} }
lv_fs_close(&file); lv_fs_close(file);
return font; return font;
} }
@@ -234,7 +234,7 @@ static int read_bits_signed(bit_iterator_t * it, int n_bits, lv_fs_res_t * res)
static int read_label(lv_fs_file_t * fp, int start, const char * label) static int read_label(lv_fs_file_t * fp, int start, const char * label)
{ {
lv_fs_seek(fp, start); lv_fs_seek(fp, start, LV_FS_SEEK_SET);
uint32_t length; uint32_t length;
char buf[4]; char buf[4];
@@ -265,7 +265,7 @@ static bool load_cmaps_tables(lv_fs_file_t * fp, lv_font_fmt_txt_dsc_t * font_ds
} }
for(unsigned int i = 0; i < font_dsc->cmap_num; ++i) { for(unsigned int i = 0; i < font_dsc->cmap_num; ++i) {
lv_fs_res_t res = lv_fs_seek(fp, cmaps_start + cmap_table[i].data_offset); lv_fs_res_t res = lv_fs_seek(fp, cmaps_start + cmap_table[i].data_offset, LV_FS_SEEK_SET);
if(res != LV_FS_RES_OK) { if(res != LV_FS_RES_OK) {
return false; return false;
} }
@@ -379,7 +379,7 @@ static int32_t load_glyph(lv_fs_file_t * fp, lv_font_fmt_txt_dsc_t * font_dsc,
for(unsigned int i = 0; i < loca_count; ++i) { for(unsigned int i = 0; i < loca_count; ++i) {
lv_font_fmt_txt_glyph_dsc_t * gdsc = &glyph_dsc[i]; lv_font_fmt_txt_glyph_dsc_t * gdsc = &glyph_dsc[i];
lv_fs_res_t res = lv_fs_seek(fp, start + glyph_offset[i]); lv_fs_res_t res = lv_fs_seek(fp, start + glyph_offset[i], LV_FS_SEEK_SET);
if(res != LV_FS_RES_OK) { if(res != LV_FS_RES_OK) {
return -1; return -1;
} }
@@ -445,7 +445,7 @@ static int32_t load_glyph(lv_fs_file_t * fp, lv_font_fmt_txt_dsc_t * font_dsc,
cur_bmp_size = 0; cur_bmp_size = 0;
for(unsigned int i = 1; i < loca_count; ++i) { for(unsigned int i = 1; i < loca_count; ++i) {
lv_fs_res_t res = lv_fs_seek(fp, start + glyph_offset[i]); lv_fs_res_t res = lv_fs_seek(fp, start + glyph_offset[i], LV_FS_SEEK_SET);
if(res != LV_FS_RES_OK) { if(res != LV_FS_RES_OK) {
return -1; return -1;
} }
+3 -3
View File
@@ -51,9 +51,9 @@ extern lv_font_t font_3;
void lv_test_font_loader(void) void lv_test_font_loader(void)
{ {
#if LV_USE_FILESYSTEM && LV_FONT_FMT_TXT_LARGE == 0 #if LV_USE_FILESYSTEM && LV_FONT_FMT_TXT_LARGE == 0
lv_font_t * font_1_bin = lv_font_load("f:font_1.fnt"); lv_font_t * font_1_bin = lv_font_load("F:font_1.fnt");
lv_font_t * font_2_bin = lv_font_load("f:font_2.fnt"); lv_font_t * font_2_bin = lv_font_load("F:font_2.fnt");
lv_font_t * font_3_bin = lv_font_load("f:font_3.fnt"); lv_font_t * font_3_bin = lv_font_load("F:font_3.fnt");
compare_fonts(&font_1, font_1_bin); compare_fonts(&font_1, font_1_bin);
compare_fonts(&font_2, font_2_bin); compare_fonts(&font_2, font_2_bin);
+23 -20
View File
@@ -28,23 +28,21 @@ int main(void)
#if LV_USE_FILESYSTEM #if LV_USE_FILESYSTEM
static lv_fs_res_t open_cb(struct _lv_fs_drv_t * drv, void * file_p, const char * path, lv_fs_mode_t mode) static void * open_cb(struct _lv_fs_drv_t * drv, const char * path, lv_fs_mode_t mode)
{ {
(void) drv; (void) drv;
(void) mode; (void) mode;
FILE * fp = fopen(path, "rb"); // only reading is supported FILE * fp = fopen(path, "rb"); // only reading is supported
*((FILE **)file_p) = fp; return fp;
return NULL == fp ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
} }
static lv_fs_res_t close_cb(struct _lv_fs_drv_t * drv, void * file_p) static lv_fs_res_t close_cb(struct _lv_fs_drv_t * drv, void * file_p)
{ {
(void) drv; (void) drv;
FILE * fp = *((FILE **) file_p); fclose(file_p);
fclose(fp);
return LV_FS_RES_OK; return LV_FS_RES_OK;
} }
@@ -52,17 +50,30 @@ static lv_fs_res_t read_cb(struct _lv_fs_drv_t * drv, void * file_p, void * buf,
{ {
(void) drv; (void) drv;
FILE * fp = *((FILE **) file_p); *br = fread(buf, 1, btr, file_p);
*br = fread(buf, 1, btr, fp);
return (*br <= 0) ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK; return (*br <= 0) ? LV_FS_RES_UNKNOWN : LV_FS_RES_OK;
} }
static lv_fs_res_t seek_cb(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos) static lv_fs_res_t seek_cb(struct _lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t w)
{ {
(void) drv; (void) drv;
FILE * fp = *((FILE **) file_p); uint32_t w2;
fseek (fp, pos, SEEK_SET); switch(w) {
case LV_FS_SEEK_SET:
w2 = SEEK_SET;
break;
case LV_FS_SEEK_CUR:
w2 = SEEK_CUR;
break;
case LV_FS_SEEK_END:
w2 = SEEK_END;
break;
default:
w2 = SEEK_SET;
}
fseek (file_p, pos, w2);
return LV_FS_RES_OK; return LV_FS_RES_OK;
} }
@@ -71,17 +82,11 @@ static lv_fs_res_t tell_cb(struct _lv_fs_drv_t * drv, void * file_p, uint32_t *
{ {
(void) drv; (void) drv;
FILE * fp = *((FILE **) file_p); *pos_p = ftell(file_p);
*pos_p = ftell(fp);
return LV_FS_RES_OK; return LV_FS_RES_OK;
} }
static bool ready_cb(struct _lv_fs_drv_t * drv)
{
(void) drv;
return true;
}
#endif #endif
static void hal_init(void) static void hal_init(void)
@@ -101,9 +106,7 @@ static void hal_init(void)
lv_fs_drv_t drv; lv_fs_drv_t drv;
lv_fs_drv_init(&drv); /*Basic initialization*/ lv_fs_drv_init(&drv); /*Basic initialization*/
drv.letter = 'f'; /*An uppercase letter to identify the drive */ drv.letter = 'F'; /*An uppercase letter to identify the drive */
drv.file_size = sizeof(FILE *); /*Size required to store a file object*/
drv.ready_cb = ready_cb; /*Callback to tell if the drive is ready to use */
drv.open_cb = open_cb; /*Callback to open a file */ drv.open_cb = open_cb; /*Callback to open a file */
drv.close_cb = close_cb; /*Callback to close a file */ drv.close_cb = close_cb; /*Callback to close a file */
drv.read_cb = read_cb; /*Callback to read a file */ drv.read_cb = read_cb; /*Callback to read a file */