changeset 91:e1e308167991

Various improvements in the bitmapped font loading and saving functions.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Oct 2012 17:50:28 +0300
parents 1ab3fd8b9afc
children e5796ffce131
files dmtext.h dmtext_bm.c
diffstat 2 files changed, 44 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/dmtext.h	Tue Oct 02 16:37:58 2012 +0300
+++ b/dmtext.h	Tue Oct 02 17:50:28 2012 +0300
@@ -32,6 +32,8 @@
 #define DMFONT_MAX_HEIGHT    128
 #define DMFONT_MAX_GLYPHS    1024
 
+#define DMFONT_NPALETTE      256
+
 // Legacy TSFONT loading support
 #define TSFONT_MAGIC         "TSFONT"
 #define TSFONT_VERSION       0x0205
--- a/dmtext_bm.c	Tue Oct 02 16:37:58 2012 +0300
+++ b/dmtext_bm.c	Tue Oct 02 17:50:28 2012 +0300
@@ -200,11 +200,14 @@
             return DMERR_VERSION;
     }
     
-    // Check other data
+    // Read other header data
     if (tsfont)
     {
+        // TSFONT only has number of glyphs stored in the file
         nglyphs = dmfgetc(res);
-        maxglyph = 256;
+        
+        // Maximum glyph number
+        maxglyph = 255;
     }
     else
     {
@@ -217,7 +220,7 @@
 
     if (tsfont)
     {
-        // TSFONT color assigns (boolean) .. we discard this.
+        // TSFONT color assignments (boolean) .. we discard this.
         dmfgetc(res);
     }
 
@@ -237,7 +240,18 @@
     // Read glyph data, if any
     if (nglyphs > 0)
     {
-        Uint32 i, BitsPerPixel, Rmask, Gmask, Bmask, Amask;
+        int n, i;
+        Uint32 BitsPerPixel, Rmask, Gmask, Bmask, Amask;
+        SDL_Color pal[DMFONT_NPALETTE];
+
+        // Setup palette for 8bpp fonts
+        for (n = 0; n < DMFONT_NPALETTE; n++)
+        {
+            pal[n].r = n * 16;
+            pal[n].g = n * 16;
+            pal[n].b = n * 16;
+            pal[n].unused = n > 0 ? n * 16 : 0;
+        }
 
         if (tsfont)
         {
@@ -260,30 +274,37 @@
             Uint8 *pixels;
             SDL_Surface *glyph;
 
+            // TSFONT format has only byte sized index
             if (tsfont)
                 index = dmfgetc(res);
             else
                 dmf_read_le16(res, &index);
 
+            // Read dimensions
             width = dmfgetc(res);
             height = dmfgetc(res);
-            
+
             if (width < DMFONT_MIN_WIDTH ||
                 height < DMFONT_MIN_HEIGHT ||
                 width > DMFONT_MAX_WIDTH ||
                 height > DMFONT_MAX_HEIGHT ||
-                index > DMFONT_MAX_GLYPHS)
+                index > maxglyph)
                 return DMERR_INVALID_DATA;
-            
+
+            // Allocate bitmap
             font->glyphs[index] = glyph = SDL_CreateRGBSurface(
                 SDL_SWSURFACE, width, height,
                 BitsPerPixel, Rmask, Gmask,
                 Bmask,
                 Amask);
-            
+
             if (glyph == NULL)
                 return DMERR_MALLOC;
 
+            if (BitsPerPixel == 8)
+                SDL_SetColors(glyph, pal, 0, DMFONT_NPALETTE);
+
+            // Read pixel data
             pixels = glyph->pixels;
             for (y = 0; y < glyph->h; y++)
             {
@@ -300,7 +321,7 @@
 
 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
 {
-    int count, n;
+    int maxglyph, nglyphs, n;
     if (font == NULL)
         return DMERR_NULLPTR;
 
@@ -312,23 +333,29 @@
         return DMERR_INVALID_DATA;
 
     // Count number of actually existing glyphs
-    for (count = n = 0; n < font->nglyphs; n++)
-        if (font->glyphs[n] != NULL) count++;
+    for (maxglyph = nglyphs = n = 0; n < font->nglyphs; n++)
+    {
+        if (font->glyphs[n] != NULL)
+        {
+            nglyphs++;
+            maxglyph = n;
+        }
+    }
     
     // Write the DMFONT header
     if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6))
         return DMERR_FWRITE;
 
     dmf_write_le16(res, DMFONT_VERSION);
-    dmf_write_le16(res, count);          // # of glyphs actually
-    dmf_write_le16(res, font->nglyphs);  // Max glyph #
+    dmf_write_le16(res, nglyphs);
+    dmf_write_le16(res, maxglyph);
     dmfputc(font->width, res);
     dmfputc(font->height, res);
     
-    if (font->nglyphs > 0)
+    if (nglyphs > 0)
     {
         int i;
-        SDL_Surface *glyph = font->glyphs[0];
+        SDL_Surface *glyph = font->glyphs[maxglyph];
 
         // If there are actual glyphs stored, save thi
         dmfputc(glyph->format->BitsPerPixel, res);