FON File Format

Fallout FON-files format.

This document describes FON- format files. The information was obtained in the course of reviewing relevant functions in the file Mapper2.exe. The names of the structures and their fields used in the program were received from the debugging information in the file Mapper2.exe.

Background
Fonts from FON- files are used for displaying information on Fallout worldmap (according to information from the Wasteland Ghost). Font is non-scalable, raster and does not contain an antialiasing information.

File Format
FON- file format is given in Table 2.1

To work with FON-files Mapper and Fallout use the following structure:

Header typedef struct { int num; 				// The number of images of characters in the file int height; 			// height of the font in points int spacing; 			// The distance between adjacent symbols at the points FontInfo* info; 			// Pointer to the array structures, describing characters unsigned char* data;		// Pointer to the array structures, describing symbol) } Font;

Symbol descriptor

typedef struct { int width;		// width character's points int offset;		// The image characters in a block of data } FontInfo;

The size of the block of data depicting characters determined as follows: last = font.num-1;		// Index of last character in the font size = font.info[last].offset + (font.info[last].width + 7) / 8 * font.height;

Through Construction (font.info[last].width + 7) / 8 determined by the number of bytes needed to store information on one line symbol image. Information on the image stored as a symbol bit matrix.

Example

Let there is a symbol of a size 8x16 points of the image which is described as follows block data 00 00 7e 81 a5 81 81 bd 99 81 81 7e 00 00 00 00 The number of bytes needed to store information on one line symbol image bytesPerLine = (8 + 7) / 8 = 1 Bit matrix takes the form of 00      00000000        ........ 00       00000000        ........  7e       01111110        .######. 81      10000001        #......#  a5       10100101        #.#..#.# 81      10000001        #......#  81       10000001        #......#  bd  ==>  10111101  ==>   #.####.# 99      10011001        #..##..#  81       10000001        #......#  81       10000001        #......#  7e       01111110        .######. 00      00000000        ........  00       00000000        ........  00       00000000        ........  00       00000000        ........

FON-viewer source code
The program compiled in MSVS 2003. NET. // Fallout FON-file viewer
 * 1) include 
 * 2) include 


 * 1) pragma pack(1)       // Struct memory alignment - 1 bytes

typedef struct { int width; int offset; } FontInfo;

typedef struct { int num; int height; int spacing; FontInfo* info; unsigned char* data; } Font;

int main(int argc, char* argv[]) {   FILE* fp; Font font; int last; int size; int i;

printf("Fallout FON-files viewer, version 1.0\n"); printf("Copyright (C) Anchorite (TeamX), 2005\n"); printf("anchorite2001@yandex.ru\n"); printf("\n");

if (argc < 2) { printf("Usage: %s file.fon\n", argv[0]); return 1; }

// Read file fp = fopen(argv[1], "rb");

if (!fp) { printf("Error: Unable open %s\n", argv[1]); return -1; }

if (fread(&font, sizeof(font), 1, fp) != 1) { printf("Error: Unable read FON-file header\n"); fclose(fp); return -1; }

font.info = (FontInfo*)malloc(font.num * sizeof(FontInfo));

if (font.info == NULL) { printf("Error: Unable allocate memory for glyphs info\n"); fclose(fp); return -1; }

if (fread(font.info, sizeof(FontInfo), font.num, fp) != font.num) { printf("Error: Unable read info about glyphs\n"); free(font.info); fclose(fp); return -1; }

last = font.num - 1; size = font.info[last].offset + (font.info[last].width + 7) / 8 * font.height;

font.data = (unsigned char*)malloc(size);

if (font.data == NULL) { printf("Error: Unable allocate memory for glyphs\n"); free(font.info); fclose(fp); return -1; }

if (fread(font.data, 1, size, fp) != size) { printf("Error: Unable read glyphs\n"); free(font.data); free(font.info); fclose(fp); return -1; }

fclose(fp);

// Font info printf("Number of glyphs: %d\n", font.num); printf("Height: %d\n", font.height); printf("Spacing: %d\n", font.spacing); printf("\n");

// Glyphs for(i = 0; i < font.num; i++) { printf("Glyph %d (0x%02X)\n", i, i); printf("================\n");

printf("Width: %d\n", font.info[i].width); printf("Offset: 0x%08X (%d)\n", font.info[i].offset, font.info[i].offset); printf("Offset from begin of file: 0x%08X (%d)\n",              sizeof(font) + sizeof(FontInfo) * font.num + font.info[i].offset,               sizeof(font) + sizeof(FontInfo) * font.num + font.info[i].offset); printf("\n");

if (font.height * font.info[i].width != 0) { int offset = font.info[i].offset; int bytesPerLine = (font.info[i].width + 7) / 8; int j;           int h;

for(h = 0; h < font.height; h++) { for(j = 0; j < font.info[i].width; j++) { if ((*(font.data + offset + h * bytesPerLine + (j / 8))) & (1 << (7 - (j % 8)))) { printf("#"); }                   else { printf("."); }               }

printf("\n"); }

printf("\n"); printf("\n"); }       else { printf("Empty\n"); printf("\n"); printf("\n"); }   }

// Free font data free(font.data); free(font.info);

return 0; }

Original document: http://www.teamx.ru/rus/bd/fon_format.shtml Original document author: Anchorite.