Index: firmware/export/font.h
===================================================================
--- firmware/export/font.h	(revision 30613)
+++ firmware/export/font.h	(working copy)
@@ -126,7 +126,7 @@
 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(void);
 
 #else /* HAVE_LCD_BITMAP */
 
Index: firmware/font.c
===================================================================
--- firmware/font.c	(revision 30613)
+++ firmware/font.c	(working copy)
@@ -31,6 +31,7 @@
 #include <stdlib.h>
 #include "inttypes.h"
 #include "lcd.h"
+#include "lru.h"
 #include "system.h"
 #include "font.h"
 #include "file.h"
@@ -82,7 +83,6 @@
     unsigned char buffer[];
 };
 static int buflib_allocations[MAXFONTS];
-static int handle_for_glyphcache;
 
 static int buflibmove_callback(int handle, void* current, void* new)
 {
@@ -143,7 +143,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 */
@@ -360,7 +359,7 @@
     size_t size;
     struct font* pf = pf_from_handle(buflib_allocations[font_id]);
     /* save loaded glyphs */
-    glyph_cache_save(pf);
+    glyph_cache_save();
     /* Close font file handle */
     if (pf->fd >= 0)
         close(pf->fd);
@@ -530,9 +529,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);
 
@@ -566,8 +562,6 @@
             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;
     }
 }
@@ -720,19 +714,17 @@
     return bits;
 }
 static int cache_fd;
+static struct font *pf_cache;
 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);
     unsigned short ch;
     unsigned char tmp[2];
 
     if ( p->_char_code == 0xffff )
         return;
-    
-    ch = p->_char_code + pf->firstchar;
 
+    ch = p->_char_code + pf_cache->firstchar;
     if ( cache_fd >= 0) {
         tmp[0] = ch >> 8;
         tmp[1] = ch & 0xff;
@@ -745,18 +737,65 @@
 }
 
 /* save the char codes of the loaded glyphs to a file */
-void glyph_cache_save(struct font* pf)
+void glyph_cache_save()
 {
-    if (pf != pf_from_handle(handle_for_glyphcache))
+    int i;
+    int max_glyphs = -1;
+    int selected_font = -1;
+    struct font *pf;
+    static int g_file_size = 0;
+    
+    /* find the font with the most glyphs in memory */
+    for (i=0 ; i < MAXFONTS ; i++) 
+     {
+        if (buflib_allocations[i] != -1) 
+        {
+            pf = pf_from_handle(buflib_allocations[i]);
+            if (pf->fd < 0 )
+                continue;
+            if (max_glyphs < pf->cache._size)
+            {
+                selected_font = i;
+                pf_cache = pf;
+                max_glyphs = pf->cache._size;
+            }
+        }
+    }
+    if (selected_font == -1)
         return;
-    if (pf->fd >= 0) 
-    {
-        cache_fd = open(GLYPH_CACHE_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0666);
-        if (cache_fd < 0)
-            return;
 
-        lru_traverse(&pf->cache._lru, glyph_file_write);
+    /* cache init may have an extra 0xffff left over from cache init */
+    struct font_cache_entry *p;
+    p = lru_data(&pf_cache->cache._lru,max_glyphs-1);
+    if( p->_char_code == 0xFFFF )
+       max_glyphs--;
+    
+    /* need > 20 to bother saving */
+    if (max_glyphs > 20 && selected_font != -1)
+    {        
+        if ( ! g_file_size )
+        {
+            cache_fd = open( GLYPH_CACHE_FILE, O_RDONLY);
+            if (cache_fd >= 0)
+            {
+                g_file_size = filesize(cache_fd);
+                close(cache_fd);
+            }
+        }
 
+        /* don't truncate a larger glyph file */
+        if(g_file_size/2 < max_glyphs)
+        {
+            cache_fd = open(GLYPH_CACHE_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+            if (cache_fd >= 0)
+            {
+                lock_font_handle( buflib_allocations[selected_font], true);
+                lru_traverse(&pf_cache->cache._lru, glyph_file_write);
+                lock_font_handle( buflib_allocations[selected_font], false);
+                g_file_size = filesize(cache_fd);
+            }
+        }
+        
         if (cache_fd >= 0)
         {
             close(cache_fd);
@@ -805,10 +844,8 @@
 }
 static void glyph_cache_load(struct font* pf)
 {
-    if (handle_for_glyphcache <= 0)
-        return;
 #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];
@@ -846,16 +883,17 @@
                       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++) {
+                    if ( glyphs[i] != glyphs[i-1] )
                          font_get_bits(pf, glyphs[i]);
                 }
                 
                 /* redo to fix lru order */
                 for ( i = 0; i < size ; i++)
+                {
                     font_get_bits(pf, glyphs_lru_order[i]);
+                }
 
                 if ( size < sort_size )
                     break;
