changeset 874:9d874c1c4a58

Cleanups in fontconv.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 04 Feb 2015 17:49:04 +0200
parents 26ea35e914ca
children 8aa0d640af74
files tools/fontconv.c
diffstat 1 files changed, 38 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/tools/fontconv.c	Tue Feb 03 20:59:22 2015 +0200
+++ b/tools/fontconv.c	Wed Feb 04 17:49:04 2015 +0200
@@ -1,7 +1,7 @@
 /*
  * fontconv - Convert bitmap fonts
  * Programmed and designed by Matti 'ccr' Hamalainen
- * (C) Copyright 2012 Tecnic Software productions (TNSP)
+ * (C) Copyright 2012-2015 Tecnic Software productions (TNSP)
  *
  * Please read file 'COPYING' for information on license and distribution.
  */
@@ -134,7 +134,7 @@
     int nglyph, xc, yc, xglyphs, yglyphs;
     DMBitmapFont *font;
 
-    if (image->w < width || width < 4 || image->h < height || height < 4)
+    if (image->w < width || width < 2 || image->h < height || height < 2)
         return DMERR_INVALID_ARGS;
     
     xglyphs = image->w / width;
@@ -183,6 +183,7 @@
 
 int dmSaveBitmapFont(DMResource *res, DMBitmapFont *font)
 {
+    SDL_Surface *glyph;
     int maxglyph, nglyphs, n;
     if (font == NULL)
         return DMERR_NULLPTR;
@@ -209,7 +210,10 @@
             nglyphs++;
         }
     }
-    
+
+    if (nglyphs == 0)
+        return DMERR_INVALID_DATA;
+
     // Write the DMFONT header
     if (!dmf_write_str(res, (Uint8 *) DMFONT_MAGIC, 6))
         return DMERR_FWRITE;
@@ -219,45 +223,40 @@
     dmf_write_le16(res, maxglyph + 1);
     dmfputc(font->width, res);
     dmfputc(font->height, res);
-    
-    if (nglyphs > 0)
-    {
-        int i;
-        SDL_Surface *glyph = font->glyphs[maxglyph];
+
+    // Store glyph format data
+    glyph = font->glyphs[maxglyph];
+    dmfputc(glyph->format->BitsPerPixel, res);
+    dmf_write_le32(res, glyph->format->Rmask);
+    dmf_write_le32(res, glyph->format->Gmask);
+    dmf_write_le32(res, glyph->format->Bmask);
+    dmf_write_le32(res, glyph->format->Amask);
 
-        // If there are actual glyphs stored, save this
-        dmfputc(glyph->format->BitsPerPixel, res);
-        dmf_write_le32(res, glyph->format->Rmask);
-        dmf_write_le32(res, glyph->format->Gmask);
-        dmf_write_le32(res, glyph->format->Bmask);
-        dmf_write_le32(res, glyph->format->Amask);
+    for (n = 0; n < font->nglyphs; n++)
+    {
+        glyph = font->glyphs[n];
+        if (glyph != NULL)
+        {
+            int y;
+            Uint8 *pixels = glyph->pixels;
 
-        for (i = 0; i < font->nglyphs; i++)
-        {
-            glyph = font->glyphs[i];
-            if (glyph != NULL)
+            if (glyph->w < DMFONT_MIN_WIDTH ||
+                glyph->h < DMFONT_MIN_HEIGHT ||
+                glyph->w > DMFONT_MAX_WIDTH ||
+                glyph->h > DMFONT_MAX_HEIGHT)
+                continue;
+
+            // Each glyph has its table index and w/h stored
+            dmf_write_le16(res, n);
+            dmfputc(glyph->w, res);
+            dmfputc(glyph->h, res);
+
+            // Write the pixel data
+            for (y = 0; y < glyph->h; y++)
             {
-                int y;
-                Uint8 *pixels = glyph->pixels;
-                
-                if (glyph->w < DMFONT_MIN_WIDTH ||
-                    glyph->h < DMFONT_MIN_HEIGHT ||
-                    glyph->w > DMFONT_MAX_WIDTH ||
-                    glyph->h > DMFONT_MAX_HEIGHT)
-                    continue;
-
-                // Each glyph has its table index and w/h stored
-                dmf_write_le16(res, i);
-                dmfputc(glyph->w, res);
-                dmfputc(glyph->h, res);
-
-                // Write the pixel data
-                for (y = 0; y < glyph->h; y++)
-                {
-                    if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w)
-                        return DMERR_FWRITE;
-                    pixels += glyph->pitch;
-                }
+                if (dmfwrite(pixels, glyph->format->BytesPerPixel, glyph->w, res) != (size_t) glyph->w)
+                    return DMERR_FWRITE;
+                pixels += glyph->pitch;
             }
         }
     }