remove display-leftovers and remove the memory size question for iRiver
[kugel-rb.git] / firmware / font.c
blob9d46e0d4cef7d962f8c0700ac8535af953c58f5c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2002 by Greg Haerr <greg@censoft.com>
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 * Rockbox startup font initialization
21 * This file specifies which fonts get compiled-in and
22 * loaded at startup, as well as their mapping into
23 * the FONT_SYSFIXED, FONT_UI and FONT_MP3 ids.
25 #include "config.h"
27 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
29 #include <stdio.h>
30 #include <string.h>
31 #include "lcd.h"
32 #include "font.h"
33 #include "file.h"
34 #include "debug.h"
35 #include "panic.h"
37 #ifndef O_BINARY
38 #define O_BINARY 0
39 #endif
41 /* compiled-in font */
42 extern struct font sysfont;
44 /* structure filled in by font_load */
45 static struct font font_ui;
47 /* system font table, in order of FONT_xxx definition */
48 static struct font* const sysfonts[MAXFONTS] = { &sysfont, &font_ui };
50 /* static buffer allocation structures */
51 static unsigned char mbuf[MAX_FONT_SIZE];
52 static unsigned char *freeptr = mbuf;
53 static unsigned char *fileptr;
54 static unsigned char *eofptr;
56 void font_init(void)
58 memset(&font_ui, 0, sizeof(struct font));
61 static int readshort(unsigned short *sp)
63 unsigned short s;
65 s = *fileptr++ & 0xff;
66 *sp = (*fileptr++ << 8) | s;
67 return (fileptr <= eofptr);
70 static int readlong(unsigned long *lp)
72 unsigned long l;
74 l = *fileptr++ & 0xff;
75 l |= *fileptr++ << 8;
76 l |= *fileptr++ << 16;
77 *lp = (*fileptr++ << 24) | l;
78 return (fileptr <= eofptr);
81 /* read count bytes*/
82 static int readstr(char *buf, int count)
84 int n = count;
86 while (--n >= 0)
87 *buf++ = *fileptr++;
88 return (fileptr <= eofptr)? count: 0;
91 void font_reset(void)
93 memset(&font_ui, 0, sizeof(struct font));
96 /* read and load font into incore font structure*/
97 struct font* font_load(const char *path)
99 int fd, filesize;
100 unsigned short maxwidth, height, ascent, pad;
101 unsigned long firstchar, defaultchar, size;
102 unsigned long i, nbits, noffset, nwidth;
103 char version[4+1];
104 struct font* pf = &font_ui;
106 /* open and read entire font file*/
107 fd = open(path, O_RDONLY|O_BINARY);
108 if (fd < 0) {
109 DEBUGF("Can't open font: %s\n", path);
110 return NULL;
113 font_reset();
115 /* currently, font loading replaces earlier font allocation*/
116 freeptr = (unsigned char *)(((int)mbuf + 3) & ~3);
118 fileptr = freeptr;
119 filesize = read(fd, fileptr, MAX_FONT_SIZE);
120 eofptr = fileptr + filesize;
122 /* no need for multiple font loads currently*/
123 /*freeptr += filesize;*/
124 /*freeptr = (unsigned char *)(freeptr + 3) & ~3;*/ /* pad freeptr*/
126 close(fd);
127 if (filesize == MAX_FONT_SIZE) {
128 DEBUGF("Font %s too large: %d\n", path, filesize);
129 return NULL;
132 /* read magic and version #*/
133 memset(version, 0, sizeof(version));
134 if (readstr(version, 4) != 4)
135 return NULL;
136 if (strcmp(version, VERSION) != 0)
137 return NULL;
139 /* font info*/
140 if (!readshort(&maxwidth))
141 return NULL;
142 pf->maxwidth = maxwidth;
143 if (!readshort(&height))
144 return NULL;
145 pf->height = height;
146 if (!readshort(&ascent))
147 return NULL;
148 pf->ascent = ascent;
149 if (!readshort(&pad))
150 return NULL;
151 if (!readlong(&firstchar))
152 return NULL;
153 pf->firstchar = firstchar;
154 if (!readlong(&defaultchar))
155 return NULL;
156 pf->defaultchar = defaultchar;
157 if (!readlong(&size))
158 return NULL;
159 pf->size = size;
161 /* get variable font data sizes*/
162 /* # words of bitmap_t*/
163 if (!readlong(&nbits))
164 return NULL;
166 /* # longs of offset*/
167 if (!readlong(&noffset))
168 return NULL;
170 /* # bytes of width*/
171 if (!readlong(&nwidth))
172 return NULL;
174 /* variable font data*/
175 pf->bits = (unsigned char *)fileptr;
176 fileptr += nbits*sizeof(unsigned char);
178 /* pad to 16 bit boundary*/
179 fileptr = (unsigned char *)(((int)fileptr + 1) & ~1);
181 if (noffset) {
182 pf->offset = (unsigned short *)fileptr;
183 for (i=0; i<noffset; ++i)
185 unsigned short offset;
186 if (!readshort(&offset))
187 return NULL;
188 ((unsigned short*)(pf->offset))[i] = (unsigned short)offset;
191 else
192 pf->offset = NULL;
194 if (nwidth) {
195 pf->width = (unsigned char *)fileptr;
196 fileptr += nwidth*sizeof(unsigned char);
198 else
199 pf->width = NULL;
201 if (fileptr > eofptr)
202 return NULL;
204 return pf; /* success!*/
208 * Return a pointer to an incore font structure.
209 * If the requested font isn't loaded/compiled-in,
210 * decrement the font number and try again.
212 struct font* font_get(int font)
214 struct font* pf;
216 if (font >= MAXFONTS)
217 font = 0;
219 while (1) {
220 pf = sysfonts[font];
221 if (pf && pf->height)
222 return pf;
223 if (--font < 0)
224 panicf("No font!");
228 #endif /* HAVE_LCD_BITMAP */
230 /* -----------------------------------------------------------------
231 * vim: et sw=4 ts=8 sts=4 tw=78