| 11 | static uint32 *uni_8to32(const char *); |
| 12 | |
| 13 | struct BitmapFont * |
| 14 | load_font(const char *filename) |
| 15 | { |
| 16 | FILE *fp; |
| 17 | struct BitmapFont *font = NULL; |
| 18 | unsigned char header[32]; |
| 19 | size_t size; |
| 20 | uint32 magic; |
| 21 | uint32 version; |
| 22 | uint32 headersize; |
| 23 | uint32 fontflags; |
| 24 | uint32 length; |
| 25 | uint32 charsize; |
| 26 | uint32 height; |
| 27 | uint32 width; |
| 28 | uint32 bwidth, memsize; |
| 29 | uint32 i; |
| 30 | |
| 31 | fp = fopen(filename, "rb"); |
| 32 | if (fp == NULL) goto error; |
| 33 | |
| 34 | /* Read the PSF header */ |
| 35 | size = fread(header, 1, sizeof(header), fp); |
| 36 | if (size != sizeof(header)) goto error; |
| 37 | |
| 38 | /* Convert from little endian order */ |
| 39 | magic = read_u32(header + 0); |
| 40 | if (magic != 0x864AB572) goto error; |
| 41 | version = read_u32(header + 4); |
| 42 | if (version != 0) goto error; |
| 43 | headersize = read_u32(header + 8); |
| 44 | if (headersize < sizeof(header)) goto error; |
| 45 | fontflags = read_u32(header + 12); |
| 46 | length = read_u32(header + 16); |
| 47 | charsize = read_u32(header + 20); |
| 48 | height = read_u32(header + 24); |
| 49 | width = read_u32(header + 28); |
| 50 | |
| 51 | /* Check that the declared character size can hold the declared width |
| 52 | and height */ |
| 53 | bwidth = (width + 7) / 8; |
| 54 | memsize = bwidth * height; |
| 55 | if (memsize > charsize) goto error; |
| 56 | |
| 57 | /* Allocate a font structure */ |
| 58 | font = (struct BitmapFont *) alloc(sizeof(struct BitmapFont)); |
| 59 | memset(font, 0, sizeof(struct BitmapFont)); |
| 60 | |
| 61 | /* Dimensions of the font */ |
| 62 | font->width = width; |
| 63 | font->height = height; |
| 64 | font->num_glyphs = length; |
| 65 | |
| 66 | /* The glyph array */ |
| 67 | font->glyphs = (unsigned char **) alloc(length * sizeof(unsigned char *)); |
| 68 | memset(font->glyphs, 0, length * sizeof(unsigned char *)); |
| 69 | |
| 70 | /* Read the glyphs */ |