diff options
author | Jonathan Gordon <rockbox@jdgordon.info> | 2011-09-24 14:52:16 +0000 |
---|---|---|
committer | Jonathan Gordon <rockbox@jdgordon.info> | 2011-09-24 14:52:16 +0000 |
commit | 83cfbf4e5102f1a72b4af2ae47a893168dc7cd02 (patch) | |
tree | f4a836b576902b2dd9cc2877268bfaf6f99bcb80 /firmware/font.c | |
parent | 0c521cffd4b8875a7fa42912f2b86582d37494d5 (diff) | |
download | rockbox-83cfbf4e5102f1a72b4af2ae47a893168dc7cd02.tar.gz rockbox-83cfbf4e5102f1a72b4af2ae47a893168dc7cd02.zip |
Allow fonts to use smaller buffers than the default size. use font_load_ex() to speficiy the buffer size. If the font is already loaded with a smaller buffer it will be reloaded to use the new size. Also fix an issue where handles would get lost if fonts fail to load in skins
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30592 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/font.c')
-rw-r--r-- | firmware/font.c | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/firmware/font.c b/firmware/font.c index fea210a3f1..2fda8483b5 100644 --- a/firmware/font.c +++ b/firmware/font.c @@ -79,7 +79,7 @@ struct buflib_alloc_data { struct font font; bool handle_locked; /* is the buflib handle currently locked? */ int refcount; /* how many times has this font been loaded? */ - unsigned char buffer[MAX_FONT_SIZE]; + unsigned char buffer[]; }; static int buflib_allocations[MAXFONTS]; static int handle_for_glyphcache; @@ -349,7 +349,7 @@ static void font_reset(int font_id) static bool internal_load_font(int font_id, const char *path, char *buf, size_t buf_size) { - int size; + size_t size; struct font* pf = pf_from_handle(buflib_allocations[font_id]); /* save loaded glyphs */ glyph_cache_save(pf); @@ -433,15 +433,16 @@ static int find_font_index(const char* path) return FONT_SYSFIXED; } -static int alloc_and_init(int font_idx, const char* name) +static int alloc_and_init(int font_idx, const char* name, size_t size) { int *phandle = &buflib_allocations[font_idx]; int handle = *phandle; struct buflib_alloc_data *pdata; struct font *pf; + size_t alloc_size = size + sizeof(struct buflib_alloc_data); if (handle > 0) return handle; - *phandle = core_alloc_ex(name, sizeof(struct buflib_alloc_data), &buflibops); + *phandle = core_alloc_ex(name, alloc_size, &buflibops); handle = *phandle; if (handle < 0) return handle; @@ -451,7 +452,7 @@ static int alloc_and_init(int font_idx, const char* name) pdata->handle_locked = false; pdata->refcount = 1; pf->buffer_position = pf->buffer_start = buffer_from_handle(handle); - pf->buffer_size = MAX_FONT_SIZE; + pf->buffer_size = size; return handle; } @@ -465,17 +466,44 @@ const char* font_filename(int font_id) /* read and load font into incore font structure, * returns the font number on success, -1 on failure */ -int font_load(const char *path) +int font_load_ex(const char *path, size_t buffer_size) { int font_id = find_font_index(path); char *buffer; - size_t buffer_size; int *handle; if (font_id > FONT_SYSFIXED) { /* already loaded, no need to reload */ struct buflib_alloc_data *pd = core_get_data(buflib_allocations[font_id]); + if (pd->font.buffer_size < buffer_size) + { + int old_refcount, old_id; + /* reload the font: + * 1) save of refcont and id + * 2) force unload (set refcount to 1 to make sure it get unloaded) + * 3) reload with the larger buffer + * 4) restore the id and refcount + */ + old_id = font_id; + old_refcount = pd->refcount; + pd->refcount = 1; + font_unload(font_id); + font_id = font_load_ex(path, buffer_size); + if (font_id < 0) + { + // not much we can do here, maybe try reloading with the small buffer again + return -1; + } + if (old_id != font_id) + { + buflib_allocations[old_id] = buflib_allocations[font_id]; + buflib_allocations[font_id] = -1; + font_id = old_id; + } + pd = core_get_data(buflib_allocations[font_id]); + pd->refcount = old_refcount; + } pd->refcount++; //printf("reusing handle %d for %s (count: %d)\n", font_id, path, pd->refcount); return font_id; @@ -490,7 +518,7 @@ int font_load(const char *path) } } handle = &buflib_allocations[font_id]; - *handle = alloc_and_init(font_id, path); + *handle = alloc_and_init(font_id, path, buffer_size); if (*handle < 0) return -1; @@ -498,7 +526,6 @@ int font_load(const char *path) handle_for_glyphcache = *handle; buffer = buffer_from_handle(*handle); - buffer_size = MAX_FONT_SIZE; //FIXME lock_font_handle(*handle, true); if (!internal_load_font(font_id, path, buffer, buffer_size)) @@ -513,6 +540,10 @@ int font_load(const char *path) //printf("%s -> [%d] -> %d\n", path, font_id, *handle); return font_id; /* success!*/ } +int font_load(const char *path) +{ + return font_load_ex(path, MAX_FONT_SIZE); +} void font_unload(int font_id) { |