Daniel,
    The following patch makes loadable fonts actually work (finally!).
It took me quite a while, but I finally figured out why the sim
worked and the target didn't: the SH1 processor won't read
longwords from a shortword alignment... I had to rev the .fnt
file to version 1.1 (requires remaking *.fnt files) in order
to fix this.  Please apply the following patch completely.
It's diffed against the latest CVS.

I've also attached rockbox-fonts-1.1.tar.gz which includes
known working *.fnt files, including a courB08 system.fnt,
for demonstration.

Now the real work can begin...  Although the new
system.fnt will work fine, if you try going to a really
big font (try copying courB14.fnt to system.fnt), then
you will find that it comes up and works in tree mode,
but will crash the system when going into WPS
mode...  I'm sure this is because of the low-level
lcd_bitmap not clipping properly when given a too-large
bitmap, which the characters become.  I haven't yet
tried to debug the low-level driver.  Of course, it all
works on the sim...

So the apps developers will now have to make sure that
all apps screen sizes may vary according to the loaded font.
The font height can be gotten through the lcd_getfontsize API.

Files patched in fonts-6.patch

1. apps/menu.c - LCD_PROPFONTS error (2nd resubmission on this, please checkin)

2. firmware/font.c - fixes and reformatting.  Please check this in as is,
my vi editor requires more reformatting changes since I left tabs in the
file, these are removed now (2nd resubmission on this, please checkin)

3. firmware/fonts.h - doc change on .fnt file format, .fnt version
number incremented.

4. firmware/loadfont.c - fixes to load font properly, typedefs
removed.

5. firmware/system.c - lcd_setfont(FONT_SYSFIXED) before
issuing error, otherwise font may not exist.

6. tools/bdf2c - fixes for correct output when filename starts
with a number, as well as when no DEFAULT_CHAR in .bdf
file.  (2nd resubmission on this, please checkin)

7. tools/writerbf.c - fixes for bugfixed fontfile format.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2294 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/menu.c b/apps/menu.c
index dd5b9ba..a42929c 100644
--- a/apps/menu.c
+++ b/apps/menu.c
@@ -125,7 +125,7 @@
 static void menu_draw(int m)
 {
     int i = 0;
-#if LCD_PROPFONTS
+#ifdef HAVE_LCD_BITMAP
     int fw, fh;
     int menu_lines;
     lcd_getfontsize(FONT_UI, &fw, &fh);
@@ -175,7 +175,7 @@
 static void put_cursor(int m, int target)
 {
     bool do_update = true;
-#if LCD_PROPFONTS
+#ifdef HAVE_LCD_BITMAP
     int fw, fh;
     int menu_lines;
     lcd_getfontsize(FONT_UI, &fw, &fh);
diff --git a/firmware/font.c b/firmware/font.c
index 5237f7d..e9c70cd 100644
--- a/firmware/font.c
+++ b/firmware/font.c
@@ -83,6 +83,8 @@
 {
     PMWCFONT pf;
 
+    if (font >= MAXFONTS)
+        font = 0;
     while (1) {
         pf = sysfonts[font].pf;
         if (pf && pf->height)
@@ -115,13 +117,13 @@
     int width = 0;
 
     while((ch = *str++)) {
-	/* check input range*/
-	if (ch < pf->firstchar || ch >= pf->firstchar+pf->size)
-		ch = pf->defaultchar;
-	ch -= pf->firstchar;
+        /* check input range*/
+        if (ch < pf->firstchar || ch >= pf->firstchar+pf->size)
+            ch = pf->defaultchar;
+        ch -= pf->firstchar;
 
-	/* get proportional width and glyph bits*/
-	width += pf->width? pf->width[ch]: pf->maxwidth;
+        /* get proportional width and glyph bits*/
+        width += pf->width? pf->width[ch]: pf->maxwidth;
     }
     *w = width;
     *h = pf->height;
@@ -150,7 +152,11 @@
 
         /* get proportional width and glyph bits*/
         width = pf->width? pf->width[ch]: pf->maxwidth;
-        if(x + width > LCD_WIDTH)
+        if (x + width > LCD_WIDTH)
+            break;
+
+        /* no partial-height drawing for now...*/
+        if (y + pf->height > LCD_HEIGHT)
             break;
         bits = pf->bits + (pf->offset? pf->offset[ch]: (pf->height * ch));
 
@@ -224,29 +230,29 @@
         MWIMAGEBITS srcmap;	/* current src input bit*/
         MWIMAGEBITS dstmap;	/* current dst output bit*/
         
-	/* calc src input bit*/
-	srcmap = 1 << (sizeof(MWIMAGEBITS)*8-1);
+        /* calc src input bit*/
+        srcmap = 1 << (sizeof(MWIMAGEBITS)*8-1);
 
-	/* calc dst output bit*/
+        /* calc dst output bit*/
         if (i>0 && (i%8==0)) {
             ++dst_col;
             dst_shift = 0;
         }
         dstmap = 1 << dst_shift++;
 
-	/* for each input column...*/
+        /* for each input column...*/
         for(j=0; j < width; j++) {
 
-	    /* calc input bitmask*/
+            /* calc input bitmask*/
             MWIMAGEBITS bit = srcmap >> j;
             if (bit==0) {
                 srcmap = 1 << (sizeof(MWIMAGEBITS)*8-1);
                 bit = srcmap >> (j % 16);
             }
 
-	    /* if set in input, set in rotated output*/
-	    if (bit & src[i]) {
-		/* input column j becomes output row*/
+            /* if set in input, set in rotated output*/
+            if (bit & src[i]) {
+                /* input column j becomes output row*/
                 dst[j*dst_linelen + dst_col] |= dstmap;
             }
             /*debugf((bit & src[i])? "*": ".");*/
@@ -259,6 +265,6 @@
 /* -----------------------------------------------------------------
  * local variables:
  * eval: (load-file "rockbox-mode.el")
- * vim: et sw=4 ts=4 sts=4 tw=78
+ * vim: et sw=4 ts=8 sts=4 tw=78
  * end:
  */
diff --git a/firmware/font.h b/firmware/font.h
index 8535ab2..645848e 100644
--- a/firmware/font.h
+++ b/firmware/font.h
@@ -46,7 +46,7 @@
 /*
  * .fnt (.rbf) loadable font file format definition
  *
- * format		      len	description
+ * format                     len	description
  * -------------------------  ----	------------------------------
  * UCHAR version[4]		4	magic number and version bytes
  * UCHAR name[64]	       64	font name, space padded
@@ -54,6 +54,7 @@
  * USHORT maxwidth		2	font max width in pixels
  * USHORT height		2	font height in pixels
  * USHORT ascent		2	font ascent (baseline) in pixels
+ * USHORT pad                   2       unused, pad to 32-bit boundary
  * ULONG firstchar		4	first character code in font
  * ULONG defaultchar		4	default character code in font
  * ULONG size			4	# characters in font
@@ -61,12 +62,13 @@
  * ULONG noffset		4	# longs offset data in file
  * ULONG nwidth			4	# bytes width data in file
  * MWIMAGEBITS bits	  nbits*2	image bits variable data
+ * [MWIMAGEBITS padded to 32-bit boundary]
  * ULONG offset         noffset*4	offset variable data
  * UCHAR width		 nwidth*1	width variable data
  */
 
 /* loadable font magic and version #*/
-#define VERSION		"RB10"
+#define VERSION		"RB11"
 
 /* MWIMAGEBITS helper macros*/
 #define MWIMAGE_WORDS(x)	(((x)+15)/16)	/* image size in words*/
@@ -122,5 +124,6 @@
 /* -----------------------------------------------------------------
  * local variables:
  * eval: (load-file "rockbox-mode.el")
+ * vim: et sw=4 ts=8 sts=4 tw=78
  * end:
  */
diff --git a/firmware/loadfont.c b/firmware/loadfont.c
index 7f572a4..e78f208 100644
--- a/firmware/loadfont.c
+++ b/firmware/loadfont.c
@@ -39,9 +39,8 @@
 /* static buffer allocation structures*/
 static unsigned char mbuf[MAX_FONT_SIZE];
 static unsigned char *freeptr = mbuf;
-typedef unsigned char CFILE;
-static CFILE *fileptr;
-static CFILE *eofptr;
+static unsigned char *fileptr;
+static unsigned char *eofptr;
 
 static int
 READSHORT(unsigned short *sp)
@@ -101,9 +100,9 @@
 rbf_load_font(char *path, PMWCFONT pf)
 {
     int fd, filesize;
-    unsigned short maxwidth, height, ascent;
+    unsigned short maxwidth, height, ascent, pad;
     unsigned long firstchar, defaultchar, size;
-    unsigned long nbits, noffset, nwidth;
+    unsigned long i, nbits, noffset, nwidth;
     char version[4+1];
     char copyright[256+1];
 
@@ -115,10 +114,12 @@
         DEBUGF("Can't open font: %s\n", path);
         return NULL;
     }
+freeptr = (unsigned char *)(((int)mbuf + 3) & ~3);
     fileptr = freeptr;
     filesize = read(fd, fileptr, MAX_FONT_SIZE);
-    freeptr += filesize;
     eofptr = fileptr + filesize;
+    //freeptr += filesize;
+    //freeptr = (unsigned char *)(freeptr + 3) & ~3;  /* pad freeptr*/
     close(fd);
     if (filesize == MAX_FONT_SIZE) {
         DEBUGF("Font %s too large: %d\n", path, filesize);
@@ -151,6 +152,8 @@
     if (!READSHORT(&ascent))
         return NULL;
     pf->ascent = ascent;
+    if (!READSHORT(&pad))
+        return NULL;
     if (!READLONG(&firstchar))
         return NULL;
     pf->firstchar = firstchar;
@@ -177,16 +180,22 @@
 
     /* variable font data*/
     pf->bits = (MWIMAGEBITS *)fileptr;
-    fileptr += nbits*sizeof(MWIMAGEBITS);
+    for (i=0; i<nbits; ++i)
+        if (!READSHORT(&pf->bits[i]))
+            return NULL;
+    /* pad to longword boundary*/
+    fileptr = (unsigned char *)(((int)fileptr + 3) & ~3);
 
     if (noffset) {
         pf->offset = (unsigned long *)fileptr;
-        fileptr += noffset*sizeof(unsigned long);
+        for (i=0; i<noffset; ++i)
+            if (!READLONG(&pf->offset[i]))
+                return NULL;
     } else pf->offset = NULL;
 
     if (nwidth) {
         pf->width = (unsigned char *)fileptr;
-        fileptr += noffset*sizeof(unsigned char);
+        fileptr += nwidth*sizeof(unsigned char);
     } else pf->width = NULL;
 
     if (fileptr > eofptr)
@@ -198,5 +207,6 @@
 /* -----------------------------------------------------------------
  * local variables:
  * eval: (load-file "rockbox-mode.el")
+ * vim: et sw=4 ts=8 sts=4 tw=78
  * end:
  */
diff --git a/firmware/system.c b/firmware/system.c
index 74e3fce..1dce076 100644
--- a/firmware/system.c
+++ b/firmware/system.c
@@ -19,7 +19,8 @@
 #include <stdio.h>
 #include "config.h"
 
-#include <lcd.h>
+#include "lcd.h"
+#include "font.h"
 #include "led.h"
 #include "system.h"
 
@@ -325,6 +326,7 @@
 
     /* clear screen */
     lcd_clear_display ();
+    lcd_setfont(FONT_SYSFIXED);
     /* output exception */
     n = (n - (unsigned)UIE0 - 4)>>2; // get exception or interrupt number
     snprintf(str,sizeof(str),"I%02x:%s",n,irqname[n]);
diff --git a/tools/bdf2c b/tools/bdf2c
index e6b8ee7..6832e5c 100755
--- a/tools/bdf2c
+++ b/tools/bdf2c
@@ -5,6 +5,7 @@
 #
 # from The Microwindows Project (http://microwindows.org)
 #
+# modified 09/13/02 correct output when no DEFAULT_CHAR, allow numeric font name
 # modified 09/12/02 added -limit <max_encode_hex_value> option
 # modified on 09/10/02 by G Haerr
 #	- fixed DWIDTH 0 parsing
@@ -79,7 +80,7 @@
 print "*/\n\n";
 
 print "/* Font character bitmap data. */\n";
-print "static MWIMAGEBITS ${font}_bits[] = {\n";
+print "static MWIMAGEBITS _${font}_bits[] = {\n";
 
 $ch_height = $font_ascent + $font_descent;
 $ofs = 0;
@@ -162,8 +163,10 @@
 
 ##print STDERR "Maximum character width=$maxwidth\n";
 
+$default_char = $firstchar if !defined $default_char;
+
 print "/* Character->glyph mapping. */\n";
-print "static unsigned long ${font}_offset[] = {\n";
+print "static unsigned long _${font}_offset[] = {\n";
 for (my $i = $firstchar; $i <= $lastchar; $i++) {
     my $char = $i;
     my $ofs = $encoding_tab[$i];
@@ -177,13 +180,13 @@
 for (my $i = $firstchar; $i <= $lastchar; $i++) {
     my $char = $i;
     my $width = $width[$i];
-    $width = $width[$default_char], $char = $default_char if !defined $encoding_tab[$i];
+    $width = $width[$default_char] if !defined $encoding_tab[$i];
     $gen_width_table = 1 if $width != $maxwidth
 }
 
 if ($gen_width_table) {
 	print "/* Character width data. */\n";
-	print "static unsigned char ${font}_width[] = {\n";
+	print "static unsigned char _${font}_width[] = {\n";
 	for (my $i = $firstchar; $i <= $lastchar; $i++) {
 	    my $char = $i;
 	    my $width = $width[$i];
@@ -204,11 +207,11 @@
 print "  $font_ascent,\n";
 print "  $firstchar,\n";
 print "  $size,\n";
-print "  ${font}_bits,\n";
-print "  ${font}_offset,\n";
+print "  _${font}_bits,\n";
+print "  _${font}_offset,\n";
 if ($gen_width_table) {
-	print "  ${font}_width,\n";
+	print "  _${font}_width,\n";
 } else { print "  0,  /* fixed width*/\n"; }
 print "  $default_char,\n";
-print "  sizeof(${font}_bits)/sizeof(MWIMAGEBITS),\n";
+print "  sizeof(_${font}_bits)/sizeof(MWIMAGEBITS),\n";
 print "};\n";
diff --git a/tools/writerbf.c b/tools/writerbf.c
index b3ba864..3bd55a7 100644
--- a/tools/writerbf.c
+++ b/tools/writerbf.c
@@ -80,6 +80,7 @@
 	WRITESHORT(ofp, pf->maxwidth);
 	WRITESHORT(ofp, pf->height);
 	WRITESHORT(ofp, pf->ascent);
+	WRITESHORT(ofp, 0);
 	WRITELONG(ofp, pf->firstchar);
 	WRITELONG(ofp, pf->defaultchar);
 	WRITELONG(ofp, pf->size);
@@ -92,9 +93,13 @@
 	/* variable font data*/
 	for (i=0; i<pf->bits_size; ++i)
 		WRITESHORT(ofp, pf->bits[i]);
+        if (ftell(ofp) & 2)
+		WRITESHORT(ofp, 0);		/* pad to 32-bit boundary*/
+
 	if (pf->offset)
 		for (i=0; i<pf->size; ++i)
 			WRITELONG(ofp, pf->offset[i]);
+
 	if (pf->width)
 		for (i=0; i<pf->size; ++i)
 			WRITEBYTE(ofp, pf->width[i]);