Index: firmware/export/font.h
===================================================================
--- firmware/export/font.h	(revision 30738)
+++ firmware/export/font.h	(working copy)
@@ -120,13 +120,14 @@
 int font_load_ex(const char *path, size_t buffer_size);
 int font_glyphs_to_bufsize(const char *path, int glyphs);
 void font_unload(int font_id);
+void font_unload_all(void);
 
 struct font* font_get(int font);
 
 int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber);
 int font_get_width(struct font* ft, unsigned short ch);
 const unsigned char * font_get_bits(struct font* ft, unsigned short ch);
-void glyph_cache_save(struct font* pf);
+void glyph_cache_save(int font_id);
 
 #else /* HAVE_LCD_BITMAP */
 
Index: firmware/font.c
===================================================================
--- firmware/font.c	(revision 30738)
+++ firmware/font.c	(working copy)
@@ -40,6 +40,7 @@
 #include "rbunicode.h"
 #include "diacritic.h"
 #include "rbpaths.h"
+#include "strlcpy.h"
 
 #define MAX_FONTSIZE_FOR_16_BIT_OFFSETS 0xFFDB
 
@@ -70,6 +71,10 @@
 #define O_BINARY 0
 #endif
 
+/* Define this to try loading /.rockbox/.glyphcache  *
+ * when a font specific file fails.                  *
+//#define TRY_DEFAULT_GLYPHCACHE
+
 /* compiled-in font */
 extern struct font sysfont;
 
@@ -82,7 +87,6 @@
     unsigned char buffer[];
 };
 static int buflib_allocations[MAXFONTS];
-static int handle_for_glyphcache;
 
 static int buflibmove_callback(int handle, void* current, void* new)
 {
@@ -92,7 +96,7 @@
 
     if (alloc->handle_locked)
         return BUFLIB_CB_CANNOT_MOVE;
-
+    
 #define UPDATE(x) if (x) { x = PTR_ADD(x, diff); }
 
     UPDATE(alloc->font.bits);
@@ -132,7 +136,7 @@
 
 /* Font cache structures */
 static void cache_create(struct font* pf);
-static void glyph_cache_load(struct font* pf);
+static void glyph_cache_load(int font_id);
 /* End Font cache structures */
 
 void font_init(void)
@@ -140,7 +144,6 @@
     int i = 0;
     while (i<MAXFONTS)
         buflib_allocations[i++] = -1;
-    handle_for_glyphcache = -1;
 }
 
 /* Check if we have x bytes left in the file buffer */
@@ -356,14 +359,7 @@
 {
     size_t size;
     struct font* pf = pf_from_handle(buflib_allocations[font_id]);
-    /* save loaded glyphs */
-    glyph_cache_save(pf);
-    /* Close font file handle */
-    if (pf->fd >= 0)
-        close(pf->fd);
 
-    font_reset(font_id);
-
     /* open and read entire font file*/
     pf->fd = open(path, O_RDONLY|O_BINARY);
 
@@ -400,7 +396,7 @@
             return false;
         }
 
-        glyph_cache_load(pf);
+        glyph_cache_load(font_id);
     }
     else
     {
@@ -527,9 +523,6 @@
     if (*handle < 0)
         return -1;
 
-    if (handle_for_glyphcache < 0)
-        handle_for_glyphcache = *handle;
-
     buffer = buffer_from_handle(*handle);
     lock_font_handle(*handle, true);
 
@@ -560,23 +553,38 @@
 
 void font_unload(int font_id)
 {
-    int *handle = &buflib_allocations[font_id];
-    struct buflib_alloc_data *pdata = core_get_data(*handle);
+    int handle = buflib_allocations[font_id];
+    struct buflib_alloc_data *pdata = core_get_data(handle);
     struct font* pf = &pdata->font;
     pdata->refcount--;
     if (pdata->refcount < 1)
     {
         //printf("freeing id: %d %s\n", font_id, core_get_name(*handle));
-        if (pf && pf->fd >= 0)
+        if (pf->fd >= 0)
+        {        
+            glyph_cache_save(font_id);
             close(pf->fd);
-        if (*handle > 0)
-            core_free(*handle);
-        if (handle_for_glyphcache == *handle)
-            handle_for_glyphcache = -1; // should find the next available handle
-        *handle = -1;
+        }
+        if (handle > 0)
+            core_free(handle);
+        buflib_allocations[font_id] = -1;
     }
 }
 
+void font_unload_all(void)
+{
+    int i;
+    for (i=0; i<MAXFONTS; i++)
+    {
+        if (buflib_allocations[i] > 0)
+        {
+            struct buflib_alloc_data *alloc = core_get_data(buflib_allocations[i]);
+            alloc->refcount = 1; /* force unload */
+            font_unload(i);
+        }
+    }
+}
+
 /*
  * Return a pointer to an incore font structure.
  * If the requested font isn't loaded/compiled-in,
@@ -728,11 +736,11 @@
     return bits;
 }
 static int cache_fd;
+static struct font* cache_pf;
 static void glyph_file_write(void* data)
 {
     struct font_cache_entry* p = data;
-    int handle = handle_for_glyphcache;
-    struct font* pf = pf_from_handle(handle);
+    struct font* pf = cache_pf;
     unsigned short ch;
     unsigned char tmp[2];
 
@@ -753,25 +761,31 @@
 }
 
 /* save the char codes of the loaded glyphs to a file */
-void glyph_cache_save(struct font* pf)
+void glyph_cache_save(int font_id)
 {
-    if (pf != pf_from_handle(handle_for_glyphcache))
+    int handle = buflib_allocations[font_id];
+    if ( handle < 0 )
         return;
-    if (pf->fd >= 0) 
+    struct font *pf = pf_from_handle(handle);
+    if (pf && pf->fd >= 0) 
     {
-        cache_fd = open(GLYPH_CACHE_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+        char filename[MAX_PATH];
+        /* take full file name, cut extension, and add .glyphcache */
+        strlcpy(filename, font_filename(font_id), 
+                MAX_PATH);
+        filename[strlen(filename)-4] = '\0';
+        strcat(filename, ".glyphcache");
+        cache_fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
         if (cache_fd < 0)
             return;
-
-        lru_traverse(&pf->cache._lru, glyph_file_write);
-
-        if (cache_fd >= 0)
-        {
+        cache_pf = pf;
+        lru_traverse(&cache_pf->cache._lru, glyph_file_write);
+        if (cache_fd >= 0) 
+        { 
             close(cache_fd);
             cache_fd = -1;
         }
-    }
-    return;
+     }
 }
 
 int font_glyphs_to_bufsize(const char *path, int glyphs)
@@ -811,12 +825,14 @@
 {
     return ((int)(*(unsigned short*)a - *(unsigned short*)b));
 }
-static void glyph_cache_load(struct font* pf)
+static void glyph_cache_load(int font_id)
 {
-    if (handle_for_glyphcache <= 0)
+    int handle = buflib_allocations[font_id];   
+    if (handle < 0)
         return;
+    struct font* pf = pf_from_handle(handle);
 #define MAX_SORT 256
-    if (pf->fd >= 0 && pf == pf_from_handle(handle_for_glyphcache)) {
+    if (pf->fd >= 0) {
         int fd;
         int i, size;
         unsigned char tmp[2];
@@ -829,9 +845,18 @@
         if ( sort_size > MAX_SORT )
              sort_size = MAX_SORT;
 
-        fd = open(GLYPH_CACHE_FILE, O_RDONLY|O_BINARY);
+        char filename[MAX_PATH];
+        strlcpy(filename, font_filename(font_id), 
+                MAX_PATH);
+        filename[strlen(filename)-4] = '\0';
+        strcat(filename, ".glyphcache");
+        fd = open(filename, O_RDONLY|O_BINARY);
+#ifdef TRY_DEFAULT_GLYPHCACHE
+        /* if font specific file fails, try default */
+        if (fd < 0)
+            fd = open(GLYPH_CACHE_FILE, O_RDONLY|O_BINARY);
+#endif
         if (fd >= 0) {
-            
             /* only read what fits */
             glyph_file_size = filesize( fd );
             if ( glyph_file_size > 2*pf->cache._capacity ) {
@@ -854,12 +879,8 @@
                       ushortcmp );
 
                 /* load font bitmaps */
-                i = 0;
-                font_get_bits(pf, glyphs[i]);
-                for ( i = 1; i < size ; i++) {
-                     if ( glyphs[i] != glyphs[i-1] )
+                for ( i = 0; i < size ; i++)
                          font_get_bits(pf, glyphs[i]);
-                }
                 
                 /* redo to fix lru order */
                 for ( i = 0; i < size ; i++)
Index: firmware/powermgmt.c
===================================================================
--- firmware/powermgmt.c	(revision 30738)
+++ firmware/powermgmt.c	(working copy)
@@ -656,7 +656,7 @@
 
     if (battery_level_safe()) { /* do not save on critical battery */
 #ifdef HAVE_LCD_BITMAP
-        glyph_cache_save(NULL);
+        font_unload_all();
 #endif
 
 /* Commit pending writes if needed. Even though we don't do write caching,
