Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (c) 2002 by Greg Haerr <greg@censoft.com> |
| 11 | * |
| 12 | * All files in this archive are subject to the GNU General Public License. |
| 13 | * See the file COPYING in the source tree root for full license agreement. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | /* |
| 20 | * Load an rbf font, store in incore format. |
| 21 | */ |
| 22 | #include "config.h" |
| 23 | |
| 24 | #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR) |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include "font.h" |
| 29 | #include "file.h" |
| 30 | |
| 31 | #ifndef DEBUGF |
| 32 | #include "debug.h" |
| 33 | #endif |
| 34 | |
| 35 | #ifndef O_BINARY |
| 36 | #define O_BINARY 0 |
| 37 | #endif |
| 38 | |
| 39 | /* static buffer allocation structures*/ |
| 40 | static unsigned char mbuf[MAX_FONT_SIZE]; |
| 41 | static unsigned char *freeptr = mbuf; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 42 | static unsigned char *fileptr; |
| 43 | static unsigned char *eofptr; |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 44 | |
| 45 | static int |
| 46 | READSHORT(unsigned short *sp) |
| 47 | { |
| 48 | unsigned short s; |
| 49 | |
| 50 | s = *fileptr++ & 0xff; |
| 51 | *sp = (*fileptr++ << 8) | s; |
| 52 | return (fileptr <= eofptr); |
| 53 | } |
| 54 | |
| 55 | static int |
| 56 | READLONG(unsigned long *lp) |
| 57 | { |
| 58 | unsigned long l; |
| 59 | |
| 60 | l = *fileptr++ & 0xff; |
| 61 | l |= *fileptr++ << 8; |
| 62 | l |= *fileptr++ << 16; |
| 63 | *lp = (*fileptr++ << 24) | l; |
| 64 | return (fileptr <= eofptr); |
| 65 | } |
| 66 | |
| 67 | /* read count bytes*/ |
| 68 | static int |
| 69 | READSTR(char *buf, int count) |
| 70 | { |
| 71 | int n = count; |
| 72 | |
| 73 | while (--n >= 0) |
| 74 | *buf++ = *fileptr++; |
| 75 | return (fileptr <= eofptr)? count: 0; |
| 76 | } |
| 77 | |
| 78 | /* read totlen bytes, return NUL terminated string*/ |
| 79 | /* may write 1 past buf[totlen]; removes blank pad*/ |
| 80 | static int |
| 81 | READSTRPAD(char *buf, int totlen) |
| 82 | { |
| 83 | char *p = buf; |
| 84 | int n = totlen; |
| 85 | |
| 86 | while (--n >= 0) |
| 87 | *p++ = *fileptr++; |
| 88 | if (fileptr > eofptr) |
| 89 | return 0; |
| 90 | |
| 91 | p = &buf[totlen]; |
| 92 | *p-- = 0; |
| 93 | while (*p == ' ' && p >= buf) |
| 94 | *p-- = '\0'; |
| 95 | return totlen; |
| 96 | } |
| 97 | |
| 98 | /* read and load font into incore font structure*/ |
| 99 | PMWCFONT |
| 100 | rbf_load_font(char *path, PMWCFONT pf) |
| 101 | { |
| 102 | int fd, filesize; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 103 | unsigned short maxwidth, height, ascent, pad; |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 104 | unsigned long firstchar, defaultchar, size; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 105 | unsigned long i, nbits, noffset, nwidth; |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 106 | char version[4+1]; |
| 107 | char copyright[256+1]; |
| 108 | |
| 109 | memset(pf, 0, sizeof(MWCFONT)); |
| 110 | |
| 111 | /* open and read entire font file*/ |
| 112 | fd = open(path, O_RDONLY|O_BINARY); |
| 113 | if (fd < 0) { |
| 114 | DEBUGF("Can't open font: %s\n", path); |
| 115 | return NULL; |
| 116 | } |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 117 | freeptr = (unsigned char *)(((int)mbuf + 3) & ~3); |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 118 | fileptr = freeptr; |
| 119 | filesize = read(fd, fileptr, MAX_FONT_SIZE); |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 120 | eofptr = fileptr + filesize; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 121 | //freeptr += filesize; |
| 122 | //freeptr = (unsigned char *)(freeptr + 3) & ~3; /* pad freeptr*/ |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 123 | close(fd); |
| 124 | if (filesize == MAX_FONT_SIZE) { |
| 125 | DEBUGF("Font %s too large: %d\n", path, filesize); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | /* read magic and version #*/ |
| 130 | memset(version, 0, sizeof(version)); |
| 131 | if (READSTR(version, 4) != 4) |
| 132 | return NULL; |
| 133 | if (strcmp(version, VERSION) != 0) |
| 134 | return NULL; |
| 135 | |
| 136 | /* internal font name*/ |
| 137 | pf->name = fileptr; |
| 138 | if (READSTRPAD(pf->name, 64) != 64) |
| 139 | return NULL; |
| 140 | |
| 141 | /* copyright, not currently stored*/ |
| 142 | if (READSTRPAD(copyright, 256) != 256) |
| 143 | return NULL; |
| 144 | |
| 145 | /* font info*/ |
| 146 | if (!READSHORT(&maxwidth)) |
| 147 | return NULL; |
| 148 | pf->maxwidth = maxwidth; |
| 149 | if (!READSHORT(&height)) |
| 150 | return NULL; |
| 151 | pf->height = height; |
| 152 | if (!READSHORT(&ascent)) |
| 153 | return NULL; |
| 154 | pf->ascent = ascent; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 155 | if (!READSHORT(&pad)) |
| 156 | return NULL; |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 157 | if (!READLONG(&firstchar)) |
| 158 | return NULL; |
| 159 | pf->firstchar = firstchar; |
| 160 | if (!READLONG(&defaultchar)) |
| 161 | return NULL; |
| 162 | pf->defaultchar = defaultchar; |
| 163 | if (!READLONG(&size)) |
| 164 | return NULL; |
| 165 | pf->size = size; |
| 166 | |
| 167 | /* get variable font data sizes*/ |
| 168 | /* # words of MWIMAGEBITS*/ |
| 169 | if (!READLONG(&nbits)) |
| 170 | return NULL; |
| 171 | pf->bits_size = nbits; |
| 172 | |
| 173 | /* # longs of offset*/ |
| 174 | if (!READLONG(&noffset)) |
| 175 | return NULL; |
| 176 | |
| 177 | /* # bytes of width*/ |
| 178 | if (!READLONG(&nwidth)) |
| 179 | return NULL; |
| 180 | |
| 181 | /* variable font data*/ |
| 182 | pf->bits = (MWIMAGEBITS *)fileptr; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 183 | for (i=0; i<nbits; ++i) |
| 184 | if (!READSHORT(&pf->bits[i])) |
| 185 | return NULL; |
| 186 | /* pad to longword boundary*/ |
| 187 | fileptr = (unsigned char *)(((int)fileptr + 3) & ~3); |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 188 | |
| 189 | if (noffset) { |
| 190 | pf->offset = (unsigned long *)fileptr; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 191 | for (i=0; i<noffset; ++i) |
| 192 | if (!READLONG(&pf->offset[i])) |
| 193 | return NULL; |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 194 | } else pf->offset = NULL; |
| 195 | |
| 196 | if (nwidth) { |
| 197 | pf->width = (unsigned char *)fileptr; |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 198 | fileptr += nwidth*sizeof(unsigned char); |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 199 | } else pf->width = NULL; |
| 200 | |
| 201 | if (fileptr > eofptr) |
| 202 | return NULL; |
| 203 | return pf; /* success!*/ |
| 204 | } |
| 205 | #endif /* HAVE_LCD_BITMAP */ |
| 206 | |
| 207 | /* ----------------------------------------------------------------- |
| 208 | * local variables: |
| 209 | * eval: (load-file "rockbox-mode.el") |
Eric Linenberg | 038df5c | 2002-09-16 03:18:49 +0000 | [diff] [blame^] | 210 | * vim: et sw=4 ts=8 sts=4 tw=78 |
Daniel Stenberg | 93b231c | 2002-09-12 13:33:59 +0000 | [diff] [blame] | 211 | * end: |
| 212 | */ |