# HG changeset patch # User Matti Hamalainen # Date 1349189428 -10800 # Node ID e1e30816799136ff8d59780b560e29eac433abe0 # Parent 1ab3fd8b9afc1392be9408cb43ae71cdb25afd98 Various improvements in the bitmapped font loading and saving functions. diff -r 1ab3fd8b9afc -r e1e308167991 dmtext.h --- 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 diff -r 1ab3fd8b9afc -r e1e308167991 dmtext_bm.c --- 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);